1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package NatsServer
- import (
- "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_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
- }
- Device_list := Device_r.Lists_All()
- b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Success, Msg: "ok!", Data: Device_list})
- _ = 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 {
- logs.PrintlnError("Nats:", err)
- b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
- _ = lib.Nats.Publish(m.Reply, b)
- return
- }
- if len(DeviceData_Form.T_sn) < 1 {
- logs.PrintlnError("Nats:", err)
- b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
- _ = lib.Nats.Publish(m.Reply, b)
- return
- }
- if len(DeviceData_Form.T_jointTab) < 1 {
- logs.PrintlnError("Nats:", err)
- b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
- _ = 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(&lib.JSONR{Code: lib.Success, Msg: "ok!", Data: JSONR_r})
- _ = lib.Nats.Publish(m.Reply, b)
- })
- }
|