package nats_server import ( "cold-delivery/common/nats" "errors" "github.com/vmihailenco/msgpack/v5" "sort" "time" ) func Cold_CompanyDeviceSensor_List_ByKey(T_sn, T_key string) (data []DeviceSensor_R, count int64, err error) { if len(T_sn) == 0 { err = errors.New("sn不能为空!") return } if len(T_key) == 0 { err = errors.New("公司秘钥不能为空!") return } type T_Req struct { T_sn string `xml:"T_sn"` T_key string `xml:"T_key"` } t_Req := T_Req{ T_sn: T_sn, T_key: T_key, } b, _ := msgpack.Marshal(&t_Req) msg, err := nats.Nats.Request("Cold_CompanyDeviceSensor_List_ByKey", b, 3*time.Second) if err != nil { return } type T_R struct { Code int16 `xml:"Code"` Msg string `xml:"Msg"` Count int64 `xml:"Count"` Data []DeviceSensor_R `xml:"Data"` // 泛型 } var t_R T_R err = msgpack.Unmarshal(msg.Data, &t_R) if err != nil { return } if t_R.Code != 200 { err = errors.New(t_R.Msg) return } return t_R.Data, t_R.Count, nil } func Cold_ReadDeviceDataListBy_T_snid(T_snid, startTime, endTime string, page, page_z int) (data []DeviceData_R, count int64, err error) { type T_Req struct { T_snid string `xml:"T_snid"` Time_start string `xml:"Time_start"` Time_end string `xml:"Time_end"` Page int `xml:"Page"` Page_z int `xml:"Page_z"` } t_Req := T_Req{ T_snid: T_snid, Time_start: startTime, Time_end: endTime, Page: page, Page_z: page_z, } b, _ := msgpack.Marshal(&t_Req) msg, err := nats.Nats.Request("Cold_ReadDeviceDataListBy_T_snid", b, 3*time.Second) if err != nil { return } type T_R struct { Code int16 `xml:"Code"` Msg string `xml:"Msg"` Count int64 `xml:"Count"` Data []DeviceData_R `xml:"Data"` // 泛型 } var t_R T_R err = msgpack.Unmarshal(msg.Data, &t_R) if err != nil { return } if t_R.Code != 200 { err = errors.New(t_R.Msg) return } list := t_R.Data sort.Slice(list, func(i, j int) bool { // 先按 T_time 字段排序,如果 T_time 相同则按 T_name 字段排序 if list[i].T_time == list[j].T_time { return list[i].T_name < list[j].T_name } return list[i].T_time > list[j].T_time }) return list, t_R.Count, nil } func Cold_ReadDeviceDataBy_T_snid_T_time(T_sn string, T_id int, Time string) (data DeviceData_, err error) { type T_Req struct { T_sn string `xml:"T_sn"` T_id int `xml:"T_id"` Time string `xml:"Time"` } t_Req := T_Req{ T_sn: T_sn, T_id: T_id, Time: Time, } b, _ := msgpack.Marshal(&t_Req) msg, err := nats.Nats.Request("Cold_ReadDeviceDataBy_T_snid_T_time", b, 3*time.Second) if err != nil { return } type T_R struct { Code int16 `xml:"Code"` Msg string `xml:"Msg"` Data DeviceData_ `xml:"Data"` // 泛型 } var t_R T_R err = msgpack.Unmarshal(msg.Data, &t_R) if err != nil { return } if t_R.Code != 200 { err = errors.New(t_R.Msg) return } return t_R.Data, nil } // 查询轨迹信息 func Cold_ReadDeviceDataListBy_T_snidForLocus(T_snid, startTime, endTime string, page, page_z int) (data []DeviceData_R2, count int64, err error) { type T_Req struct { T_snid string `xml:"T_snid"` Time_start string `xml:"Time_start"` Time_end string `xml:"Time_end"` Page int `xml:"Page"` Page_z int `xml:"Page_z"` } t_Req := T_Req{ T_snid: T_snid, Time_start: startTime, Time_end: endTime, Page: page, Page_z: page_z, } b, _ := msgpack.Marshal(&t_Req) msg, err := nats.Nats.Request("Cold_ReadDeviceDataListBy_T_snid", b, 3*time.Second) if err != nil { return } type T_R struct { Code int16 `xml:"Code"` Msg string `xml:"Msg"` Count int64 `xml:"Count"` Data []DeviceData_R2 `xml:"Data"` // 泛型 } var t_R T_R err = msgpack.Unmarshal(msg.Data, &t_R) if err != nil { return } if t_R.Code != 200 { err = errors.New(t_R.Msg) return } return t_R.Data, t_R.Count, nil } func Cold_ReadDevice_List(name, T_key string, page, page_z int) (data []Device_R, count int64, err error) { type T_Req struct { Key string `xml:"Key"` Name string `xml:"Code"` Page int `xml:"Page"` Page_z int `xml:"Page_z"` } t_Req := T_Req{ Key: T_key, Name: name, Page: page, Page_z: page_z, } b, _ := msgpack.Marshal(&t_Req) msg, err := nats.Nats.Request("Cold_ReadDevice_List", b, 3*time.Second) if err != nil { return } type T_R struct { Code int16 `xml:"Code"` Msg string `xml:"Msg"` Count int64 `xml:"Count"` Data []Device_R `xml:"Data"` // 泛型 } var t_R T_R err = msgpack.Unmarshal(msg.Data, &t_R) if err != nil { return } if t_R.Code != 200 { err = errors.New(t_R.Msg) return } list := t_R.Data return list, t_R.Count, nil } func Cold_ReadCompanyByT_key(T_key string) (data Company_R, err error) { msg, err := nats.Nats.Request("Cold_ReadCompanyByT_key", []byte(T_key), 3*time.Second) if err != nil { return } type T_R struct { Code int16 `xml:"Code"` Msg string `xml:"Msg"` Data Company_R `xml:"Data"` // 泛型 } var t_R T_R err = msgpack.Unmarshal(msg.Data, &t_R) if err != nil { return } if t_R.Code != 200 { err = errors.New(t_R.Msg) return } return t_R.Data, nil } func Cold_ReadDeviceByT_sn(T_sn string) (data Device, err error) { msg, err := nats.Nats.Request("Cold_ReadDeviceByT_sn", []byte(T_sn), 3*time.Second) if err != nil { return } type T_R struct { Code int16 `xml:"Code"` Msg string `xml:"Msg"` Data Device `xml:"Data"` // 泛型 } var t_R T_R err = msgpack.Unmarshal(msg.Data, &t_R) if err != nil { return } if t_R.Code != 200 { err = errors.New(t_R.Msg) return } data = t_R.Data return data, nil }