123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package MqttServer
- import (
- "ColdP_server/controllers/WebSocket"
- "ColdP_server/controllers/lib"
- "ColdP_server/models/Device"
- "encoding/json"
- "fmt"
- "strings"
- "time"
- )
- func AsyncFunc(r_Device Device.Device, Ms_project Ms_Project, msg []byte) {
- //t1 := time.Now()
- //
- //SN := lib.To_string(Ms_project.Sn)
- //Type := lib.To_int(Ms_project.Type)
- //Msid := lib.To_int(Ms_project.Msid)
- //
- switch lib.To_int(Ms_project.Type) {
- case 3: // 3:设备参数
- var Ms_parameter Ms_Parameter
- err := json.Unmarshal(msg, &Ms_parameter)
- if err != nil {
- fmt.Println("JSON反序列化失败[Ms_Parameter],err=", err)
- return
- }
- // 反馈透传
- jsonStu_map, err := json.Marshal(Ms_parameter)
- if err != nil {
- fmt.Println("反馈透传 [Ms_project_0],err=", err)
- return
- }
- _, ok := lib.CountrySnMap[Ms_project.Sn] /*如果确定是真实的,则存在,否则不存在 */
- if ok {
- for _, v := range lib.CountrySnMap[Ms_project.Sn].Uuid_list {
- fmt.Println("转发数据! SN:", Ms_project.Sn, " Uuid:", v)
- jsonStu_map_ := strings.Replace(string(jsonStu_map), "\"{", "{", -1)
- //jsonStu_map_ =strings.Replace(jsonStu_map_, "}\"", "}", -1)
- //jsonStu_map_ =strings.Replace(jsonStu_map_, "\\\"", "\"", -1)
- println(jsonStu_map_)
- WebSocket.Send_WebSocket(v, jsonStu_map_)
- }
- }
- break
- case 4: // 消息反馈
- var Ms_parameter_r Ms_Parameter_r
- err := json.Unmarshal(msg, &Ms_parameter_r)
- if err != nil {
- fmt.Println("JSON反序列化失败[Ms_Parameter_r],err=", err)
- return
- }
- // 反馈透传
- jsonStu_map, err := json.Marshal(Ms_parameter_r)
- if err != nil {
- fmt.Println("反馈透传 [Ms_project_0],err=", err)
- return
- }
- _, ok := lib.CountrySnMap[Ms_project.Sn] /*如果确定是真实的,则存在,否则不存在 */
- if ok {
- for _, v := range lib.CountrySnMap[Ms_project.Sn].Uuid_list {
- fmt.Println("转发数据! SN:", Ms_project.Sn, " Uuid:", v)
- jsonStu_map_ := strings.Replace(string(jsonStu_map), "\"{", "{", -1)
- jsonStu_map_ = strings.Replace(jsonStu_map_, "}\"", "}", -1)
- jsonStu_map_ = strings.Replace(jsonStu_map_, "\\\"", "\"", -1)
- WebSocket.Send_WebSocket(v, jsonStu_map_)
- }
- }
- break
- }
- //t2 := time.Now()
- //fmt.Println("线程-》 MQTT RunTime:",t2.Sub(t1))
- }
- // 循环刷新
- func Realtime() {
- //fmt.Println("=====================Realtime GO===============")
- time.Sleep(time.Second * 10)
- for true {
- //fmt.Println("=====================Realtime GO===============")
- //fmt.Println("CountrySnMap_z:", len(lib.CountrySnMap))
- for k, _ := range lib.CountrySnMap {
- //fmt.Println("CountrySnMap:",k)
- Get_Device_Realtime(k)
- time.Sleep(time.Millisecond * 100)
- }
- time.Sleep(time.Second * 10)
- }
- }
- func Get_Device_Realtime(T_sn string) {
- // base 读取基本参数
- Msid := lib.Random(1, 9999)
- Rt_realtime := Rt_Realtime{
- Sn: T_sn,
- Type: 0,
- Msid: Msid,
- }
- // 回复
- jsonStu, err := json.Marshal(Rt_realtime)
- if err != nil {
- fmt.Println("回复失败 [Pu_DeviceParameter_Compensate],err=", err)
- }
- fmt.Println(string(jsonStu))
- //Mqtt_publish(T_sn, string(jsonStu))
- return
- }
|