123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- 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, 120*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
- }
- func Read_DeviceTask_List_By_Condition(T_sn string) (data DeviceTask, err error) {
- type T_R struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- Data DeviceTask `xml:"Data"` // 泛型
- }
- type T_Req struct {
- T_sn string `xml:"T_sn"`
- }
- if len(T_sn) == 0 {
- err = errors.New("sn不能为空!")
- return
- }
- t_req := T_Req{
- T_sn: T_sn,
- }
- b, _ := msgpack.Marshal(&t_req)
- msg, err := nats.Nats.Request("Read_DeviceTask_List_By_Condition", b, 3*time.Second)
- if err != nil {
- return
- }
- 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
- }
- func Read_New_DeviceData(T_sn string, T_id int) (data DeviceSensor, err error) {
- type T_R struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- Data DeviceSensor `xml:"Data"` // 泛型
- }
- type T_Req struct {
- T_sn string `xml:"T_sn"`
- T_id int `xml:"T_id"`
- }
- if len(T_sn) == 0 {
- err = errors.New("sn不能为空!")
- return
- }
- t_req := T_Req{
- T_sn: T_sn,
- T_id: T_id,
- }
- b, _ := msgpack.Marshal(&t_req)
- msg, err := nats.Nats.Request("Read_New_DeviceData", b, 3*time.Second)
- if err != nil {
- return
- }
- 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
- }
- func Read_Start_Time_DeviceData(T_sn string, T_id int, StartTime, EndTime string) (data []DeviceData_, err error) {
- type T_R struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- Data []DeviceData_ `xml:"Data"` // 泛型
- }
- type T_Req struct {
- T_sn string `xml:"T_sn"`
- T_id int `xml:"T_id"`
- StartTime string `xml:"StartTime"`
- EndTime string `xml:"EndTime"`
- }
- if len(T_sn) == 0 {
- err = errors.New("sn不能为空!")
- return
- }
- t_req := T_Req{
- T_sn: T_sn,
- T_id: T_id,
- StartTime: StartTime,
- EndTime: EndTime,
- }
- b, _ := msgpack.Marshal(&t_req)
- msg, err := nats.Nats.Request("Read_Start_Time_DeviceData", b, 3*time.Second)
- if err != nil {
- return
- }
- 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
- }
|