NatsServer.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package NatsServer
  2. import (
  3. "ColdVerify_local/conf"
  4. "ColdVerify_local/lib"
  5. "ColdVerify_local/logs"
  6. "ColdVerify_local/models/Account"
  7. "ColdVerify_local/models/Device"
  8. "ColdVerify_local/models/Task"
  9. "errors"
  10. "github.com/nats-io/nats.go"
  11. "github.com/vmihailenco/msgpack/v5"
  12. "time"
  13. )
  14. func Update_Task(v Task.Task) error {
  15. logs.Debug("Nats =>", lib.FuncName(), v)
  16. b, err := msgpack.Marshal(&v)
  17. if err != nil {
  18. return err
  19. }
  20. msg, err := lib.Nats.Request("ColdVerify_Server_Update_Task", b, 3*time.Second)
  21. if err != nil {
  22. return err
  23. }
  24. type T_R struct {
  25. Code int16 `xml:"Code"`
  26. Msg string `xml:"Msg"`
  27. Data Task.Task `xml:"Data"`
  28. }
  29. var t_R T_R
  30. err = msgpack.Unmarshal(msg.Data, &t_R)
  31. if err != nil {
  32. return err
  33. }
  34. if t_R.Code != 200 {
  35. return errors.New(t_R.Msg)
  36. }
  37. return nil
  38. }
  39. func Read_Task(T_task_id string) (task Task.Task, err error) {
  40. //logs.Println("Nats =>", lib.FuncName(), T_task_id)
  41. if lib.Nats == nil || !lib.Nats.IsConnected() {
  42. lib.Nats, err = nats.Connect("nats://" + conf.NatsServer_Url)
  43. if err != nil {
  44. logs.Println("无法重新连接到 NATS:", err)
  45. } else {
  46. logs.Println("成功重新连接到 NATS...")
  47. }
  48. }
  49. msg, err := lib.Nats.Request("ColdVerify_Server_Read_Task", []byte(T_task_id), 3*time.Second)
  50. if err != nil {
  51. logs.Error(lib.FuncName(), err.Error())
  52. return task, err
  53. }
  54. type T_R struct {
  55. Code int16 `xml:"Code"`
  56. Msg string `xml:"Msg"`
  57. Data Task.Task `xml:"Data"`
  58. }
  59. var t_R T_R
  60. err = msgpack.Unmarshal(msg.Data, &t_R)
  61. if err != nil {
  62. logs.Error(lib.FuncName(), err.Error())
  63. return task, err
  64. }
  65. if t_R.Code != 200 {
  66. err = errors.New(t_R.Msg)
  67. logs.Error(lib.FuncName(), err.Error())
  68. return task, err
  69. }
  70. return t_R.Data, nil
  71. }
  72. func Read_User(T_uuid string) (user Account.User, err error) {
  73. logs.Println("Nats =>", lib.FuncName(), T_uuid)
  74. msg, err := lib.Nats.Request("ColdVerify_Server_Read_User", []byte(T_uuid), 3*time.Second)
  75. if err != nil {
  76. logs.Error(lib.FuncName(), err.Error())
  77. return user, err
  78. }
  79. type T_R struct {
  80. Code int16 `xml:"Code"`
  81. Msg string `xml:"Msg"`
  82. Data Account.User `xml:"Data"`
  83. }
  84. var t_R T_R
  85. err = msgpack.Unmarshal(msg.Data, &t_R)
  86. if err != nil {
  87. logs.Error(lib.FuncName(), err.Error())
  88. return user, err
  89. }
  90. if t_R.Code != 200 {
  91. err = errors.New(t_R.Msg)
  92. logs.Error(lib.FuncName(), err.Error())
  93. return user, err
  94. }
  95. return t_R.Data, nil
  96. }
  97. func Device_Class_List(T_task_id string) (list []Device.DeviceClassList, err error) {
  98. //logs.Println("Nats =>", lib.FuncName(), T_task_id)
  99. if !lib.Nats.IsConnected() {
  100. lib.Nats, err = nats.Connect("nats://" + conf.NatsServer_Url)
  101. if err != nil {
  102. logs.Println("无法重新连接到 NATS:", err)
  103. }
  104. logs.Println("成功重新连接到 NATS...")
  105. }
  106. msg, err := lib.Nats.Request("ColdVerify_Server_Device_Class_List", []byte(T_task_id), 3*time.Second)
  107. if err != nil {
  108. logs.Error(lib.FuncName(), err.Error())
  109. return list, err
  110. }
  111. type T_R struct {
  112. Code int16 `xml:"Code"`
  113. Msg string `xml:"Msg"`
  114. Data []Device.DeviceClassList `xml:"Data"`
  115. }
  116. var t_R T_R
  117. err = msgpack.Unmarshal(msg.Data, &t_R)
  118. if err != nil {
  119. logs.Error(lib.FuncName(), err.Error())
  120. return list, err
  121. }
  122. if t_R.Code != 200 {
  123. err = errors.New(t_R.Msg)
  124. logs.Error(lib.FuncName(), err.Error())
  125. return list, err
  126. }
  127. return t_R.Data, nil
  128. }
  129. func Add_DeviceClassList(T_task_id, T_sn, T_id string) (err error) {
  130. logs.Debug("Nats =>", lib.FuncName(), T_task_id, T_sn, T_id)
  131. type T_R struct {
  132. T_task_id string `xml:"T_task_id"` // 任务主键id
  133. T_sn string `xml:"T_sn"`
  134. T_id string `xml:"T_id"`
  135. }
  136. t_R := T_R{
  137. T_task_id: T_task_id,
  138. T_sn: T_sn,
  139. T_id: T_id,
  140. }
  141. b, err := msgpack.Marshal(&t_R)
  142. if err != nil {
  143. return err
  144. }
  145. msg, err := lib.Nats.Request("ColdVerify_Server_Add_DeviceClassList", b, 3*time.Second)
  146. if err != nil {
  147. return err
  148. }
  149. type T_Res struct {
  150. Code int16 `xml:"Code"`
  151. Msg string `xml:"Msg"`
  152. }
  153. var t_Res T_Res
  154. err = msgpack.Unmarshal(msg.Data, &t_Res)
  155. if err != nil {
  156. return err
  157. }
  158. if t_Res.Code != 200 {
  159. return errors.New(t_Res.Msg)
  160. }
  161. return nil
  162. }
  163. func Edit_DeviceClassList(T_task_id, T_sn, T_id string) (err error) {
  164. logs.Debug("Nats =>", lib.FuncName(), T_task_id, T_sn, T_id)
  165. type T_R struct {
  166. T_task_id string `xml:"T_task_id"` // 任务主键id
  167. T_sn string `xml:"T_sn"`
  168. T_id string `xml:"T_id"`
  169. }
  170. t_R := T_R{
  171. T_task_id: T_task_id,
  172. T_sn: T_sn,
  173. T_id: T_id,
  174. }
  175. b, err := msgpack.Marshal(&t_R)
  176. if err != nil {
  177. return err
  178. }
  179. msg, err := lib.Nats.Request("ColdVerify_Server_Edit_DeviceClassList", b, 3*time.Second)
  180. if err != nil {
  181. return err
  182. }
  183. type T_Res struct {
  184. Code int16 `xml:"Code"`
  185. Msg string `xml:"Msg"`
  186. }
  187. var t_Res T_Res
  188. err = msgpack.Unmarshal(msg.Data, &t_Res)
  189. if err != nil {
  190. return err
  191. }
  192. if t_Res.Code != 200 {
  193. return errors.New(t_Res.Msg)
  194. }
  195. return nil
  196. }
  197. func Del_TaskData_ByT_BindDeviceDataTime(T_task_id string) {
  198. logs.Debug("Nats =>", lib.FuncName(), T_task_id)
  199. err := lib.Nats.Publish("ColdVerify_Server_Del_TaskData_ByT_BindDeviceDataTime", []byte(T_task_id))
  200. if err != nil {
  201. logs.Error("Nats =>", lib.FuncName(), err)
  202. }
  203. }