Device.go 3.1 KB

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