NatsDevice.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package NatsServer
  2. import (
  3. "Cold_GoodsOrder/lib"
  4. "Cold_GoodsOrder/models/Device"
  5. "errors"
  6. "fmt"
  7. "github.com/vmihailenco/msgpack/v5"
  8. "time"
  9. )
  10. func ReadDeviceByT_sn(T_sn string) (d Device.Device, err error) {
  11. msg, err := lib.Nats.Request("Cold_ReadDeviceByT_sn", []byte(T_sn), 3*time.Second)
  12. if err != nil {
  13. return d, err
  14. }
  15. //fmt.Printf("Cold_ReadDeviceByT_sn: %s\n", string(msg.Data))
  16. type T_R struct {
  17. Code int16 `xml:"Code"`
  18. Msg string `xml:"Msg"`
  19. Data Device.Device `xml:"Data"` // 泛型
  20. }
  21. var t_R T_R
  22. err = msgpack.Unmarshal(msg.Data, &t_R)
  23. if err != nil {
  24. return d, err
  25. }
  26. fmt.Printf("Cold_ReadDeviceByT_sn : %+v\n", t_R)
  27. if t_R.Code != 200 {
  28. return d, errors.New(t_R.Msg)
  29. }
  30. return t_R.Data, nil
  31. }
  32. func ReadDeviceSensorALLByT_sn(T_sn string) (d []Device.DeviceSensor) {
  33. msg, err := lib.Nats.Request("Cold_ReadDeviceSensorALLByT_sn", []byte(T_sn), 3*time.Second)
  34. if err != nil {
  35. return d
  36. }
  37. //fmt.Printf("Cold_ReadDeviceSensorALLByT_sn: %s\n", string(msg.Data))
  38. type T_R struct {
  39. Code int16 `xml:"Code"`
  40. Msg string `xml:"Msg"`
  41. Data []Device.DeviceSensor `xml:"Data"` // 泛型
  42. }
  43. var t_R T_R
  44. err = msgpack.Unmarshal(msg.Data, &t_R)
  45. if err != nil {
  46. return d
  47. }
  48. fmt.Printf("Cold_ReadDeviceSensorALLByT_sn : %+v\n", t_R)
  49. return t_R.Data
  50. }
  51. func ReadDeviceDataListBy_T_snid(T_snid string, Time_start string, Time_end string, page int, page_z int) (r []Device.DeviceData_R, cnt int64) {
  52. type T_Req struct {
  53. T_snid string `xml:"T_snid"`
  54. Time_start string `xml:"Time_start"`
  55. Time_end string `xml:"Time_end"`
  56. Page int `xml:"Page"`
  57. Page_z int `xml:"Page_z"`
  58. }
  59. t_Req := T_Req{
  60. T_snid: T_snid,
  61. Time_start: Time_start,
  62. Time_end: Time_end,
  63. Page: page,
  64. Page_z: page_z,
  65. }
  66. b, _ := msgpack.Marshal(&t_Req)
  67. // 请求-响应, 向 verification 发布一个 `ToKey` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
  68. msg, err := lib.Nats.Request("Cold_ReadDeviceDataListBy_T_snid", b, 3*time.Second)
  69. //fmt.Printf("Cold_ReadDeviceDataListBy_T_snid : %s\n", string(msg.Data))
  70. if err != nil {
  71. return r, cnt
  72. }
  73. type T_R struct {
  74. Code int16 `xml:"Code"`
  75. Msg string `xml:"Msg"`
  76. Count int64 `xml:"Count"`
  77. Data []Device.DeviceData_R `xml:"Data"` // 泛型
  78. }
  79. var t_R T_R
  80. err = msgpack.Unmarshal(msg.Data, &t_R)
  81. if err != nil {
  82. return r, cnt
  83. }
  84. fmt.Printf("Cold_ReadDeviceDataListBy_T_snid : %+v\n", t_R)
  85. return t_R.Data, t_R.Count
  86. }