Device.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. }