123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- 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
- }
|