NatsColdApi.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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, 120*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. }
  242. func Read_DeviceTask_List_By_Condition(T_sn string) (data DeviceTask, err error) {
  243. type T_R struct {
  244. Code int16 `xml:"Code"`
  245. Msg string `xml:"Msg"`
  246. Data DeviceTask `xml:"Data"` // 泛型
  247. }
  248. type T_Req struct {
  249. T_sn string `xml:"T_sn"`
  250. }
  251. if len(T_sn) == 0 {
  252. err = errors.New("sn不能为空!")
  253. return
  254. }
  255. t_req := T_Req{
  256. T_sn: T_sn,
  257. }
  258. b, _ := msgpack.Marshal(&t_req)
  259. msg, err := nats.Nats.Request("Read_DeviceTask_List_By_Condition", b, 3*time.Second)
  260. if err != nil {
  261. return
  262. }
  263. var t_R T_R
  264. err = msgpack.Unmarshal(msg.Data, &t_R)
  265. if err != nil {
  266. return
  267. }
  268. if t_R.Code != 200 {
  269. err = errors.New(t_R.Msg)
  270. return
  271. }
  272. data = t_R.Data
  273. return data, nil
  274. }
  275. func Read_New_DeviceData(T_sn string, T_id int) (data DeviceSensor, err error) {
  276. type T_R struct {
  277. Code int16 `xml:"Code"`
  278. Msg string `xml:"Msg"`
  279. Data DeviceSensor `xml:"Data"` // 泛型
  280. }
  281. type T_Req struct {
  282. T_sn string `xml:"T_sn"`
  283. T_id int `xml:"T_id"`
  284. }
  285. if len(T_sn) == 0 {
  286. err = errors.New("sn不能为空!")
  287. return
  288. }
  289. t_req := T_Req{
  290. T_sn: T_sn,
  291. T_id: T_id,
  292. }
  293. b, _ := msgpack.Marshal(&t_req)
  294. msg, err := nats.Nats.Request("Read_New_DeviceData", b, 3*time.Second)
  295. if err != nil {
  296. return
  297. }
  298. var t_R T_R
  299. err = msgpack.Unmarshal(msg.Data, &t_R)
  300. if err != nil {
  301. return
  302. }
  303. if t_R.Code != 200 {
  304. err = errors.New(t_R.Msg)
  305. return
  306. }
  307. data = t_R.Data
  308. return data, nil
  309. }
  310. func Read_Start_Time_DeviceData(T_sn string, T_id int, StartTime, EndTime string) (data []DeviceData_, err error) {
  311. type T_R struct {
  312. Code int16 `xml:"Code"`
  313. Msg string `xml:"Msg"`
  314. Data []DeviceData_ `xml:"Data"` // 泛型
  315. }
  316. type T_Req struct {
  317. T_sn string `xml:"T_sn"`
  318. T_id int `xml:"T_id"`
  319. StartTime string `xml:"StartTime"`
  320. EndTime string `xml:"EndTime"`
  321. }
  322. if len(T_sn) == 0 {
  323. err = errors.New("sn不能为空!")
  324. return
  325. }
  326. t_req := T_Req{
  327. T_sn: T_sn,
  328. T_id: T_id,
  329. StartTime: StartTime,
  330. EndTime: EndTime,
  331. }
  332. b, _ := msgpack.Marshal(&t_req)
  333. msg, err := nats.Nats.Request("Read_Start_Time_DeviceData", b, 3*time.Second)
  334. if err != nil {
  335. return
  336. }
  337. var t_R T_R
  338. err = msgpack.Unmarshal(msg.Data, &t_R)
  339. if err != nil {
  340. return
  341. }
  342. if t_R.Code != 200 {
  343. err = errors.New(t_R.Msg)
  344. return
  345. }
  346. data = t_R.Data
  347. return data, nil
  348. }