YunIot.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package SDK
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/nats-io/nats.go"
  6. "github.com/vmihailenco/msgpack/v5"
  7. "time"
  8. )
  9. const Success = 200
  10. const Error = 201
  11. type Client struct {
  12. Host string
  13. NatsConn *nats.Conn
  14. }
  15. func NewYunIot(Host string) (*Client, error) {
  16. if len(Host) == 0 {
  17. Host = "127.0.0.1"
  18. }
  19. Client_ := &Client{Host: Host}
  20. var err error
  21. // 连接Nats服务器
  22. Client_.NatsConn, err = nats.Connect("nats://"+Host+":6503", nats.MaxReconnects(10), nats.ReconnectWait(10*time.Second),
  23. nats.DisconnectErrHandler(func(nc *nats.Conn, err error) {
  24. // handle disconnect error event
  25. fmt.Println("natsS.DisconnectErrHandler 断开 ", err)
  26. }),
  27. nats.ReconnectHandler(func(nc *nats.Conn) {
  28. // handle reconnect event
  29. fmt.Println("natsS.ReconnectHandler, 已经重新连接 ")
  30. }))
  31. if err != nil {
  32. fmt.Println("YunIot 连接失败!")
  33. return Client_, err
  34. }
  35. if err != nil {
  36. fmt.Println("YunIot 连接失败!")
  37. return Client_, err
  38. }
  39. fmt.Println("YunIot OK!")
  40. return Client_, err
  41. }
  42. func (c *Client) GetNameByGet(name string) (data interface{}, err error) {
  43. return "名字是" + name, nil
  44. }
  45. func (c *Client) GetDevice(T_sn string) (Device, error) {
  46. Device_r := Device{T_sn: T_sn}
  47. b, err := msgpack.Marshal(&Device_r)
  48. if err != nil {
  49. return Device{}, err
  50. }
  51. // 请求-响应
  52. msg, err := c.NatsConn.Request("/Device/Device/Get", b, 3*time.Second)
  53. if err != nil {
  54. return Device{}, err
  55. }
  56. JSONR_R := JSONR{}
  57. err = msgpack.Unmarshal(msg.Data, &JSONR_R)
  58. if err != nil {
  59. return Device{}, err
  60. }
  61. if JSONR_R.Code != 200 {
  62. return Device{}, errors.New(JSONR_R.Msg)
  63. }
  64. b, err = msgpack.Marshal(JSONR_R.Data)
  65. if err != nil {
  66. return Device{}, err
  67. }
  68. err = msgpack.Unmarshal(b, &Device_r)
  69. if err != nil {
  70. return Device{}, err
  71. }
  72. return Device_r, nil
  73. }
  74. func (c *Client) GetDeviceList(T_ProductID, T_sn string, T_online, PageIndex, PageSize int) (JSONS_Device, error) {
  75. Device_r := Device_Form{T_ProductID: T_ProductID, T_sn: T_sn, T_online: T_online, PageIndex: PageIndex, PageSize: PageSize}
  76. b, err := msgpack.Marshal(&Device_r)
  77. if err != nil {
  78. return JSONS_Device{}, err
  79. }
  80. // 请求-响应
  81. msg, err := c.NatsConn.Request("/Device/Device/List", b, 3*time.Second)
  82. if err != nil {
  83. return JSONS_Device{}, err
  84. }
  85. JSONR_R := JSONS_Device{}
  86. err = msgpack.Unmarshal(msg.Data, &JSONR_R)
  87. if err != nil {
  88. return JSONS_Device{}, err
  89. }
  90. if JSONR_R.Code != 200 {
  91. return JSONS_Device{Code: Error, Msg: JSONR_R.Msg}, errors.New(JSONR_R.Msg)
  92. }
  93. return JSONR_R, nil
  94. }
  95. func (c *Client) GetDeviceData(T_sn, T_jointTab, T_jsonFind, T_jsonSort string, PageIndex, PageSize int) (JSONS_Data, error) {
  96. DeviceData_Form_r := DeviceData_Form{T_sn: T_sn, T_jointTab: T_jointTab, T_jsonFind: T_jsonFind, T_jsonSort: T_jsonSort, PageIndex: PageIndex, PageSize: PageSize}
  97. b, err := msgpack.Marshal(&DeviceData_Form_r)
  98. if err != nil {
  99. return JSONS_Data{}, err
  100. }
  101. if len(DeviceData_Form_r.T_sn) < 1 {
  102. return JSONS_Data{Code: Error, Msg: "T_sn !"}, errors.New("T_sn !")
  103. }
  104. if len(DeviceData_Form_r.T_jointTab) < 1 {
  105. return JSONS_Data{Code: Error, Msg: "T_jointTab !"}, errors.New("T_jointTab !")
  106. }
  107. // 请求-响应
  108. msg, err := c.NatsConn.Request("/Device/Device/Data", b, 3*time.Second)
  109. if err != nil {
  110. return JSONS_Data{Code: Error, Msg: "请求异常 !"}, err
  111. }
  112. JSONR_R := JSONS_Data{}
  113. err = msgpack.Unmarshal(msg.Data, &JSONR_R)
  114. if err != nil {
  115. return JSONS_Data{Code: Error, Msg: "解析异常 !"}, err
  116. }
  117. if JSONR_R.Code != 200 {
  118. return JSONS_Data{Code: Error, Msg: JSONR_R.Msg}, errors.New(JSONR_R.Msg)
  119. }
  120. return JSONR_R, nil
  121. }
  122. func (c *Client) GetDevicePush(T_sn, T_data string) error {
  123. Device_r := Device{T_sn: T_sn, T_data: T_data}
  124. b, err := msgpack.Marshal(&Device_r)
  125. if err != nil {
  126. return err
  127. }
  128. // 请求-响应
  129. msg, err := c.NatsConn.Request("/Device/Device/Push", b, 3*time.Second)
  130. if err != nil {
  131. return err
  132. }
  133. JSONR_R := JSONR{}
  134. err = msgpack.Unmarshal(msg.Data, &JSONR_R)
  135. if err != nil {
  136. return err
  137. }
  138. if JSONR_R.Code != 200 {
  139. return errors.New(JSONR_R.Msg)
  140. }
  141. return nil
  142. }
  143. func (c *Client) GetProductType(T_ProductID string) (ProductType, error) {
  144. ProductType_r := ProductType{T_ProductID: T_ProductID}
  145. b, err := msgpack.Marshal(&ProductType_r)
  146. if err != nil {
  147. return ProductType{}, err
  148. }
  149. // 请求-响应
  150. msg, err := c.NatsConn.Request("/Product/ProductType/Get", b, 3*time.Second)
  151. if err != nil {
  152. return ProductType{}, err
  153. }
  154. JSONR_R := JSONR{}
  155. err = msgpack.Unmarshal(msg.Data, &JSONR_R)
  156. if err != nil {
  157. return ProductType{}, err
  158. }
  159. if JSONR_R.Code != 200 {
  160. return ProductType{}, errors.New(JSONR_R.Msg)
  161. }
  162. b, err = msgpack.Marshal(JSONR_R.Data)
  163. if err != nil {
  164. return ProductType{}, err
  165. }
  166. err = msgpack.Unmarshal(b, &ProductType_r)
  167. if err != nil {
  168. return ProductType{}, err
  169. }
  170. return ProductType_r, nil
  171. }
  172. func (c *Client) GetProductTypeList(T_uuid, T_name string) ([]ProductType, error) {
  173. ProductType_r := ProductType{T_uuid: T_uuid, T_name: T_name}
  174. b, err := msgpack.Marshal(&ProductType_r)
  175. if err != nil {
  176. return []ProductType{}, err
  177. }
  178. // 请求-响应
  179. msg, err := c.NatsConn.Request("/Product/ProductType/List", b, 3*time.Second)
  180. if err != nil {
  181. return []ProductType{}, err
  182. }
  183. JSONR_R := []ProductType{}
  184. err = msgpack.Unmarshal(msg.Data, &JSONR_R)
  185. if err != nil {
  186. return []ProductType{}, err
  187. }
  188. return JSONR_R, nil
  189. }
  190. func (c *Client) GetProductProt(Id int) (ProductProt, error) {
  191. ProductProt_r := ProductProt{Id: Id}
  192. b, err := msgpack.Marshal(&ProductProt_r)
  193. if err != nil {
  194. return ProductProt{}, err
  195. }
  196. // 请求-响应
  197. msg, err := c.NatsConn.Request("/Product/ProductProt/Get", b, 3*time.Second)
  198. if err != nil {
  199. return ProductProt{}, err
  200. }
  201. JSONR_R := JSONR{}
  202. err = msgpack.Unmarshal(msg.Data, &JSONR_R)
  203. if err != nil {
  204. return ProductProt{}, err
  205. }
  206. if JSONR_R.Code != 200 {
  207. return ProductProt{}, errors.New(JSONR_R.Msg)
  208. }
  209. b, err = msgpack.Marshal(JSONR_R.Data)
  210. if err != nil {
  211. return ProductProt{}, err
  212. }
  213. err = msgpack.Unmarshal(b, &ProductProt_r)
  214. if err != nil {
  215. return ProductProt{}, err
  216. }
  217. return ProductProt_r, nil
  218. }
  219. func (c *Client) GetProductProtList(T_uuid string, T_mode int, T_name string) ([]ProductProt, error) {
  220. ProductProt_r := ProductProt{T_uuid: T_uuid, T_mode: T_mode, T_name: T_name}
  221. b, err := msgpack.Marshal(&ProductProt_r)
  222. if err != nil {
  223. return []ProductProt{}, err
  224. }
  225. // 请求-响应
  226. msg, err := c.NatsConn.Request("/Product/ProductProt/List", b, 3*time.Second)
  227. if err != nil {
  228. return []ProductProt{}, err
  229. }
  230. JSONR_R := []ProductProt{}
  231. err = msgpack.Unmarshal(msg.Data, &JSONR_R)
  232. if err != nil {
  233. return []ProductProt{}, err
  234. }
  235. return JSONR_R, nil
  236. }
  237. func (c *Client) GetProductModeList() ([]ProductMode, error) {
  238. // 请求-响应
  239. msg, err := c.NatsConn.Request("/Product/ProductModeLists/List", []byte(""), 3*time.Second)
  240. if err != nil {
  241. return []ProductMode{}, err
  242. }
  243. JSONR_R := []ProductMode{}
  244. err = msgpack.Unmarshal(msg.Data, &JSONR_R)
  245. if err != nil {
  246. return []ProductMode{}, err
  247. }
  248. return JSONR_R, nil
  249. }