123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package NatsServer
- import (
- "Yunlot/Handle"
- "Yunlot/lib"
- "Yunlot/logs"
- "Yunlot/models/Device"
- "github.com/nats-io/nats.go"
- "github.com/vmihailenco/msgpack/v5"
- )
- func NatsServer_Device() {
- // 请求-响应
- _, _ = lib.Nats.Subscribe("/Device/Device/Get", func(m *nats.Msg) {
- logs.Println("Nats /Device/Device/Get: %s\n", string(m.Data))
- var Device_r Device.Device
- err := msgpack.Unmarshal(m.Data, &Device_r)
- if err != nil {
- logs.PrintlnError("Nats:", err)
- b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
- _ = lib.Nats.Publish(m.Reply, b)
- return
- }
- if !Device_r.Read_Tidy() {
- b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "SN E!"})
- _ = lib.Nats.Publish(m.Reply, b)
- return
- }
- b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Success, Msg: "ok!", Data: Device_r})
- _ = lib.Nats.Publish(m.Reply, b)
- })
- // 请求-响应
- _, _ = lib.Nats.Subscribe("/Device/Device/List", func(m *nats.Msg) {
- logs.Println("Nats /Device/Device/List: %s\n", string(m.Data))
- var Device_Form_r Device.Device_Form
- err := msgpack.Unmarshal(m.Data, &Device_Form_r)
- if err != nil {
- logs.PrintlnError("Nats:", err)
- b, _ := msgpack.Marshal(&lib.JSONS{})
- _ = lib.Nats.Publish(m.Reply, b)
- return
- }
- Device_r := Device.Device{T_ProductID: Device_Form_r.T_ProductID, T_sn: Device_Form_r.T_sn, T_online: Device_Form_r.T_online}
- Device_list, Total := Device_r.Lists(Device_Form_r.PageIndex, Device_Form_r.PageSize)
- b, _ := msgpack.Marshal(lib.C_Page(Device_list, Device_Form_r.PageIndex, Device_Form_r.PageSize, Total))
- _ = lib.Nats.Publish(m.Reply, b)
- })
- // 请求-响应
- _, _ = lib.Nats.Subscribe("/Device/Device/Data", func(m *nats.Msg) {
- logs.Println("Nats /Device/Device/Data: %s\n", string(m.Data))
- var DeviceData_Form Device.DeviceData_Form
- err := msgpack.Unmarshal(m.Data, &DeviceData_Form)
- if err != nil {
- b, _ := msgpack.Marshal(&lib.JSONS{})
- _ = lib.Nats.Publish(m.Reply, b)
- return
- }
- JSONR_r := Device.Data_List(DeviceData_Form.T_sn+"_"+DeviceData_Form.T_jointTab, DeviceData_Form.T_jsonFind, DeviceData_Form.T_jsonSort, DeviceData_Form.PageIndex, DeviceData_Form.PageSize)
- b, _ := msgpack.Marshal(JSONR_r)
- _ = lib.Nats.Publish(m.Reply, b)
- })
- // 请求-响应
- _, _ = lib.Nats.Subscribe("/Device/Device/Push", func(m *nats.Msg) {
- logs.Println("Nats /Device/Device/Push: %s\n", string(m.Data))
- var Device_r Device.Device
- err := msgpack.Unmarshal(m.Data, &Device_r)
- if err != nil {
- logs.PrintlnError("Nats:", err)
- b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
- _ = lib.Nats.Publish(m.Reply, b)
- return
- }
- if len(Device_r.T_data) == 0 {
- b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "T_data E!"})
- _ = lib.Nats.Publish(m.Reply, b)
- return
- }
- Device_r_ := Device.Device{T_sn: Device_r.T_sn}
- if !Device_r_.Read_Tidy() {
- b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "SN E!"})
- _ = lib.Nats.Publish(m.Reply, b)
- return
- }
- Handle.PushHandle(&Device_r_, Device_r.T_sn, Device_r.T_data)
- b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Success, Msg: "ok!"})
- _ = lib.Nats.Publish(m.Reply, b)
- })
- }
|