NatsDevice.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. fmt.Println("Cold_ReadDeviceByT_sn err : %+v\n", t_R)
  25. return d, err
  26. }
  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. return t_R.Data
  49. }
  50. func ReadDeviceDataListBy_T_snid(T_snid string, Time_start string, Time_end string, page int, page_z int) (r []Device.DeviceData_R, cnt int64) {
  51. type T_Req struct {
  52. T_snid string `xml:"T_snid"`
  53. Time_start string `xml:"Time_start"`
  54. Time_end string `xml:"Time_end"`
  55. Page int `xml:"Page"`
  56. Page_z int `xml:"Page_z"`
  57. }
  58. t_Req := T_Req{
  59. T_snid: T_snid,
  60. Time_start: Time_start,
  61. Time_end: Time_end,
  62. Page: page,
  63. Page_z: page_z,
  64. }
  65. b, _ := msgpack.Marshal(&t_Req)
  66. // 请求-响应, 向 verification 发布一个 `ToKey` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
  67. msg, err := lib.Nats.Request("Cold_ReadDeviceDataListBy_T_snid", b, 3*time.Second)
  68. //fmt.Printf("Cold_ReadDeviceDataListBy_T_snid : %s\n", string(msg.Data))
  69. if err != nil {
  70. return r, cnt
  71. }
  72. type T_R struct {
  73. Code int16 `xml:"Code"`
  74. Msg string `xml:"Msg"`
  75. Count int64 `xml:"Count"`
  76. Data []Device.DeviceData_R `xml:"Data"` // 泛型
  77. }
  78. var t_R T_R
  79. err = msgpack.Unmarshal(msg.Data, &t_R)
  80. if err != nil {
  81. return r, cnt
  82. }
  83. //fmt.Printf("Cold_ReadDeviceDataListBy_T_snid : %+v\n", t_R)
  84. return t_R.Data, t_R.Count
  85. }