Device.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package NatsServer
  2. import (
  3. "Yunlot/lib"
  4. "Yunlot/logs"
  5. "Yunlot/models/Device"
  6. "github.com/nats-io/nats.go"
  7. "github.com/vmihailenco/msgpack/v5"
  8. )
  9. func NatsServer_Device() {
  10. // 请求-响应
  11. _, _ = lib.Nats.Subscribe("/Device/Device/Get", func(m *nats.Msg) {
  12. logs.Println("Nats /Device/Device/Get: %s\n", string(m.Data))
  13. var Device_r Device.Device
  14. err := msgpack.Unmarshal(m.Data, &Device_r)
  15. if err != nil {
  16. logs.PrintlnError("Nats:", err)
  17. b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
  18. _ = lib.Nats.Publish(m.Reply, b)
  19. return
  20. }
  21. if !Device_r.Read_Tidy() {
  22. b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "SN E!"})
  23. _ = lib.Nats.Publish(m.Reply, b)
  24. return
  25. }
  26. b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Success, Msg: "ok!", Data: Device_r})
  27. _ = lib.Nats.Publish(m.Reply, b)
  28. })
  29. // 请求-响应
  30. _, _ = lib.Nats.Subscribe("/Device/Device/List", func(m *nats.Msg) {
  31. logs.Println("Nats /Device/Device/List: %s\n", string(m.Data))
  32. var Device_r Device.Device
  33. err := msgpack.Unmarshal(m.Data, &Device_r)
  34. if err != nil {
  35. logs.PrintlnError("Nats:", err)
  36. b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
  37. _ = lib.Nats.Publish(m.Reply, b)
  38. return
  39. }
  40. Device_list := Device_r.Lists_All()
  41. b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Success, Msg: "ok!", Data: Device_list})
  42. _ = lib.Nats.Publish(m.Reply, b)
  43. })
  44. // 请求-响应
  45. _, _ = lib.Nats.Subscribe("/Device/Device/Data", func(m *nats.Msg) {
  46. logs.Println("Nats /Device/Device/Data: %s\n", string(m.Data))
  47. var DeviceData_Form Device.DeviceData_Form
  48. err := msgpack.Unmarshal(m.Data, &DeviceData_Form)
  49. if err != nil {
  50. logs.PrintlnError("Nats:", err)
  51. b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
  52. _ = lib.Nats.Publish(m.Reply, b)
  53. return
  54. }
  55. if len(DeviceData_Form.T_sn) < 1 {
  56. logs.PrintlnError("Nats:", err)
  57. b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
  58. _ = lib.Nats.Publish(m.Reply, b)
  59. return
  60. }
  61. if len(DeviceData_Form.T_jointTab) < 1 {
  62. logs.PrintlnError("Nats:", err)
  63. b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
  64. _ = lib.Nats.Publish(m.Reply, b)
  65. return
  66. }
  67. 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)
  68. b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Success, Msg: "ok!", Data: JSONR_r})
  69. _ = lib.Nats.Publish(m.Reply, b)
  70. })
  71. }