NatsColdApi.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package nats_server
  2. import (
  3. "cold-delivery/common/nats"
  4. "errors"
  5. "github.com/vmihailenco/msgpack/v5"
  6. "sort"
  7. "time"
  8. )
  9. func Cold_CompanyDeviceSensor_List_ByKey(T_sn, T_key string) (data []DeviceSensor_R, count int64, err error) {
  10. if len(T_sn) == 0 {
  11. err = errors.New("sn不能为空!")
  12. return
  13. }
  14. if len(T_key) == 0 {
  15. err = errors.New("公司秘钥不能为空!")
  16. return
  17. }
  18. type T_Req struct {
  19. T_sn string `xml:"T_sn"`
  20. T_key string `xml:"T_key"`
  21. }
  22. t_Req := T_Req{
  23. T_sn: T_sn,
  24. T_key: T_key,
  25. }
  26. b, _ := msgpack.Marshal(&t_Req)
  27. msg, err := nats.Nats.Request("Cold_CompanyDeviceSensor_List_ByKey", b, 3*time.Second)
  28. if err != nil {
  29. return
  30. }
  31. type T_R struct {
  32. Code int16 `xml:"Code"`
  33. Msg string `xml:"Msg"`
  34. Count int64 `xml:"Count"`
  35. Data []DeviceSensor_R `xml:"Data"` // 泛型
  36. }
  37. var t_R T_R
  38. err = msgpack.Unmarshal(msg.Data, &t_R)
  39. if err != nil {
  40. return
  41. }
  42. if t_R.Code != 200 {
  43. err = errors.New(t_R.Msg)
  44. return
  45. }
  46. return t_R.Data, t_R.Count, nil
  47. }
  48. func Cold_ReadDeviceDataListBy_T_snid(T_snid, startTime, endTime string, page, page_z int) (data []DeviceData_R, count int64, err error) {
  49. type T_Req struct {
  50. T_snid string `xml:"T_snid"`
  51. Time_start string `xml:"Time_start"`
  52. Time_end string `xml:"Time_end"`
  53. Page int `xml:"Page"`
  54. Page_z int `xml:"Page_z"`
  55. }
  56. t_Req := T_Req{
  57. T_snid: T_snid,
  58. Time_start: startTime,
  59. Time_end: endTime,
  60. Page: page,
  61. Page_z: page_z,
  62. }
  63. b, _ := msgpack.Marshal(&t_Req)
  64. msg, err := nats.Nats.Request("Cold_ReadDeviceDataListBy_T_snid", b, 3*time.Second)
  65. if err != nil {
  66. return
  67. }
  68. type T_R struct {
  69. Code int16 `xml:"Code"`
  70. Msg string `xml:"Msg"`
  71. Count int64 `xml:"Count"`
  72. Data []DeviceData_R `xml:"Data"` // 泛型
  73. }
  74. var t_R T_R
  75. err = msgpack.Unmarshal(msg.Data, &t_R)
  76. if err != nil {
  77. return
  78. }
  79. if t_R.Code != 200 {
  80. err = errors.New(t_R.Msg)
  81. return
  82. }
  83. list := t_R.Data
  84. sort.Slice(list, func(i, j int) bool {
  85. // 先按 T_time 字段排序,如果 T_time 相同则按 T_name 字段排序
  86. if list[i].T_time == list[j].T_time {
  87. return list[i].T_name < list[j].T_name
  88. }
  89. return list[i].T_time > list[j].T_time
  90. })
  91. return list, t_R.Count, nil
  92. }
  93. func Cold_ReadDeviceDataBy_T_snid_T_time(T_sn string, T_id int, Time string) (data DeviceData_, err error) {
  94. type T_Req struct {
  95. T_sn string `xml:"T_sn"`
  96. T_id int `xml:"T_id"`
  97. Time string `xml:"Time"`
  98. }
  99. t_Req := T_Req{
  100. T_sn: T_sn,
  101. T_id: T_id,
  102. Time: Time,
  103. }
  104. b, _ := msgpack.Marshal(&t_Req)
  105. msg, err := nats.Nats.Request("Cold_ReadDeviceDataBy_T_snid_T_time", b, 3*time.Second)
  106. if err != nil {
  107. return
  108. }
  109. type T_R struct {
  110. Code int16 `xml:"Code"`
  111. Msg string `xml:"Msg"`
  112. Data DeviceData_ `xml:"Data"` // 泛型
  113. }
  114. var t_R T_R
  115. err = msgpack.Unmarshal(msg.Data, &t_R)
  116. if err != nil {
  117. return
  118. }
  119. if t_R.Code != 200 {
  120. err = errors.New(t_R.Msg)
  121. return
  122. }
  123. return t_R.Data, nil
  124. }
  125. // 查询轨迹信息
  126. func Cold_ReadDeviceDataListBy_T_snidForLocus(T_snid, startTime, endTime string, page, page_z int) (data []DeviceData_R2, count int64, err error) {
  127. type T_Req struct {
  128. T_snid string `xml:"T_snid"`
  129. Time_start string `xml:"Time_start"`
  130. Time_end string `xml:"Time_end"`
  131. Page int `xml:"Page"`
  132. Page_z int `xml:"Page_z"`
  133. }
  134. t_Req := T_Req{
  135. T_snid: T_snid,
  136. Time_start: startTime,
  137. Time_end: endTime,
  138. Page: page,
  139. Page_z: page_z,
  140. }
  141. b, _ := msgpack.Marshal(&t_Req)
  142. msg, err := nats.Nats.Request("Cold_ReadDeviceDataListBy_T_snid", b, 3*time.Second)
  143. if err != nil {
  144. return
  145. }
  146. type T_R struct {
  147. Code int16 `xml:"Code"`
  148. Msg string `xml:"Msg"`
  149. Count int64 `xml:"Count"`
  150. Data []DeviceData_R2 `xml:"Data"` // 泛型
  151. }
  152. var t_R T_R
  153. err = msgpack.Unmarshal(msg.Data, &t_R)
  154. if err != nil {
  155. return
  156. }
  157. if t_R.Code != 200 {
  158. err = errors.New(t_R.Msg)
  159. return
  160. }
  161. return t_R.Data, t_R.Count, nil
  162. }
  163. func Cold_ReadDevice_List(name, T_key string, page, page_z int) (data []Device_R, count int64, err error) {
  164. type T_Req struct {
  165. Key string `xml:"Key"`
  166. Name string `xml:"Code"`
  167. Page int `xml:"Page"`
  168. Page_z int `xml:"Page_z"`
  169. }
  170. t_Req := T_Req{
  171. Key: T_key,
  172. Name: name,
  173. Page: page,
  174. Page_z: page_z,
  175. }
  176. b, _ := msgpack.Marshal(&t_Req)
  177. msg, err := nats.Nats.Request("Cold_ReadDevice_List", b, 3*time.Second)
  178. if err != nil {
  179. return
  180. }
  181. type T_R struct {
  182. Code int16 `xml:"Code"`
  183. Msg string `xml:"Msg"`
  184. Count int64 `xml:"Count"`
  185. Data []Device_R `xml:"Data"` // 泛型
  186. }
  187. var t_R T_R
  188. err = msgpack.Unmarshal(msg.Data, &t_R)
  189. if err != nil {
  190. return
  191. }
  192. if t_R.Code != 200 {
  193. err = errors.New(t_R.Msg)
  194. return
  195. }
  196. list := t_R.Data
  197. return list, t_R.Count, nil
  198. }
  199. func Cold_ReadCompanyByT_key(T_key string) (data Company_R, err error) {
  200. msg, err := nats.Nats.Request("Cold_ReadCompanyByT_key", []byte(T_key), 3*time.Second)
  201. if err != nil {
  202. return
  203. }
  204. type T_R struct {
  205. Code int16 `xml:"Code"`
  206. Msg string `xml:"Msg"`
  207. Data Company_R `xml:"Data"` // 泛型
  208. }
  209. var t_R T_R
  210. err = msgpack.Unmarshal(msg.Data, &t_R)
  211. if err != nil {
  212. return
  213. }
  214. if t_R.Code != 200 {
  215. err = errors.New(t_R.Msg)
  216. return
  217. }
  218. return t_R.Data, nil
  219. }
  220. func Cold_ReadDeviceByT_sn(T_sn string) (data Device, err error) {
  221. msg, err := nats.Nats.Request("Cold_ReadDeviceByT_sn", []byte(T_sn), 3*time.Second)
  222. if err != nil {
  223. return
  224. }
  225. type T_R struct {
  226. Code int16 `xml:"Code"`
  227. Msg string `xml:"Msg"`
  228. Data Device `xml:"Data"` // 泛型
  229. }
  230. var t_R T_R
  231. err = msgpack.Unmarshal(msg.Data, &t_R)
  232. if err != nil {
  233. return
  234. }
  235. if t_R.Code != 200 {
  236. err = errors.New(t_R.Msg)
  237. return
  238. }
  239. data = t_R.Data
  240. return data, nil
  241. }