NatsServer.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. }
  46. logs.Println("成功重新连接到 NATS...")
  47. }
  48. msg, err := lib.Nats.Request("ColdVerify_Server_Read_Task", []byte(T_task_id), 3*time.Second)
  49. if err != nil {
  50. logs.Error(lib.FuncName(), err.Error())
  51. return task, err
  52. }
  53. type T_R struct {
  54. Code int16 `xml:"Code"`
  55. Msg string `xml:"Msg"`
  56. Data Task.Task `xml:"Data"`
  57. }
  58. var t_R T_R
  59. err = msgpack.Unmarshal(msg.Data, &t_R)
  60. if err != nil {
  61. logs.Error(lib.FuncName(), err.Error())
  62. return task, err
  63. }
  64. if t_R.Code != 200 {
  65. err = errors.New(t_R.Msg)
  66. logs.Error(lib.FuncName(), err.Error())
  67. return task, err
  68. }
  69. return t_R.Data, nil
  70. }
  71. func Read_User(T_uuid string) (user Account.User, err error) {
  72. logs.Println("Nats =>", lib.FuncName(), T_uuid)
  73. msg, err := lib.Nats.Request("ColdVerify_Server_Read_User", []byte(T_uuid), 3*time.Second)
  74. if err != nil {
  75. logs.Error(lib.FuncName(), err.Error())
  76. return user, err
  77. }
  78. type T_R struct {
  79. Code int16 `xml:"Code"`
  80. Msg string `xml:"Msg"`
  81. Data Account.User `xml:"Data"`
  82. }
  83. var t_R T_R
  84. err = msgpack.Unmarshal(msg.Data, &t_R)
  85. if err != nil {
  86. logs.Error(lib.FuncName(), err.Error())
  87. return user, err
  88. }
  89. if t_R.Code != 200 {
  90. err = errors.New(t_R.Msg)
  91. logs.Error(lib.FuncName(), err.Error())
  92. return user, err
  93. }
  94. return t_R.Data, nil
  95. }
  96. func Device_Class_List(T_task_id string) (list []Device.DeviceClassList, err error) {
  97. logs.Println("Nats =>", lib.FuncName(), T_task_id)
  98. if !lib.Nats.IsConnected() {
  99. lib.Nats, err = nats.Connect("nats://" + conf.NatsServer_Url)
  100. if err != nil {
  101. logs.Println("无法重新连接到 NATS:", err)
  102. }
  103. logs.Println("成功重新连接到 NATS...")
  104. }
  105. msg, err := lib.Nats.Request("ColdVerify_Server_Device_Class_List", []byte(T_task_id), 3*time.Second)
  106. if err != nil {
  107. logs.Error(lib.FuncName(), err.Error())
  108. return list, err
  109. }
  110. type T_R struct {
  111. Code int16 `xml:"Code"`
  112. Msg string `xml:"Msg"`
  113. Data []Device.DeviceClassList `xml:"Data"`
  114. }
  115. var t_R T_R
  116. err = msgpack.Unmarshal(msg.Data, &t_R)
  117. if err != nil {
  118. logs.Error(lib.FuncName(), err.Error())
  119. return list, err
  120. }
  121. if t_R.Code != 200 {
  122. err = errors.New(t_R.Msg)
  123. logs.Error(lib.FuncName(), err.Error())
  124. return list, err
  125. }
  126. return t_R.Data, nil
  127. }
  128. func Add_DeviceClassList(T_task_id, T_sn, T_id string) (err error) {
  129. logs.Debug("Nats =>", lib.FuncName(), T_task_id, T_sn, T_id)
  130. type T_R struct {
  131. T_task_id string `xml:"T_task_id"` // 任务主键id
  132. T_sn string `xml:"T_sn"`
  133. T_id string `xml:"T_id"`
  134. }
  135. t_R := T_R{
  136. T_task_id: T_task_id,
  137. T_sn: T_sn,
  138. T_id: T_id,
  139. }
  140. b, err := msgpack.Marshal(&t_R)
  141. if err != nil {
  142. return err
  143. }
  144. msg, err := lib.Nats.Request("ColdVerify_Server_Add_DeviceClassList", b, 3*time.Second)
  145. if err != nil {
  146. return err
  147. }
  148. type T_Res struct {
  149. Code int16 `xml:"Code"`
  150. Msg string `xml:"Msg"`
  151. }
  152. var t_Res T_Res
  153. err = msgpack.Unmarshal(msg.Data, &t_Res)
  154. if err != nil {
  155. return err
  156. }
  157. if t_Res.Code != 200 {
  158. return errors.New(t_Res.Msg)
  159. }
  160. return nil
  161. }
  162. func Edit_DeviceClassList(T_task_id, T_sn, T_id string) (err error) {
  163. logs.Debug("Nats =>", lib.FuncName(), T_task_id, T_sn, T_id)
  164. type T_R struct {
  165. T_task_id string `xml:"T_task_id"` // 任务主键id
  166. T_sn string `xml:"T_sn"`
  167. T_id string `xml:"T_id"`
  168. }
  169. t_R := T_R{
  170. T_task_id: T_task_id,
  171. T_sn: T_sn,
  172. T_id: T_id,
  173. }
  174. b, err := msgpack.Marshal(&t_R)
  175. if err != nil {
  176. return err
  177. }
  178. msg, err := lib.Nats.Request("ColdVerify_Server_Edit_DeviceClassList", b, 3*time.Second)
  179. if err != nil {
  180. return err
  181. }
  182. type T_Res struct {
  183. Code int16 `xml:"Code"`
  184. Msg string `xml:"Msg"`
  185. }
  186. var t_Res T_Res
  187. err = msgpack.Unmarshal(msg.Data, &t_Res)
  188. if err != nil {
  189. return err
  190. }
  191. if t_Res.Code != 200 {
  192. return errors.New(t_Res.Msg)
  193. }
  194. return nil
  195. }