123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- package SDK
- import (
- "errors"
- "fmt"
- "github.com/nats-io/nats.go"
- "github.com/vmihailenco/msgpack/v5"
- "time"
- )
- const Success = 200
- const Error = 201
- type Client struct {
- Host string
- NatsConn *nats.Conn
- }
- func NewYunIot(Host string) (*Client, error) {
- if len(Host) == 0 {
- Host = "127.0.0.1"
- }
- Client_ := &Client{Host: Host}
- var err error
- // 连接Nats服务器
- Client_.NatsConn, err = nats.Connect("nats://"+Host+":6503", nats.MaxReconnects(10), nats.ReconnectWait(10*time.Second),
- nats.DisconnectErrHandler(func(nc *nats.Conn, err error) {
- // handle disconnect error event
- fmt.Println("natsS.DisconnectErrHandler 断开 ", err)
- }),
- nats.ReconnectHandler(func(nc *nats.Conn) {
- // handle reconnect event
- fmt.Println("natsS.ReconnectHandler, 已经重新连接 ")
- }))
- if err != nil {
- fmt.Println("YunIot 连接失败!")
- return Client_, err
- }
- if err != nil {
- fmt.Println("YunIot 连接失败!")
- return Client_, err
- }
- fmt.Println("YunIot OK!")
- return Client_, err
- }
- func (c *Client) GetNameByGet(name string) (data interface{}, err error) {
- return "名字是" + name, nil
- }
- func (c *Client) GetDevice(T_sn string) (Device, error) {
- Device_r := Device{T_sn: T_sn}
- b, err := msgpack.Marshal(&Device_r)
- if err != nil {
- return Device{}, err
- }
- // 请求-响应
- msg, err := c.NatsConn.Request("/Device/Device/Get", b, 3*time.Second)
- if err != nil {
- return Device{}, err
- }
- JSONR_R := JSONR{}
- err = msgpack.Unmarshal(msg.Data, &JSONR_R)
- if err != nil {
- return Device{}, err
- }
- if JSONR_R.Code != 200 {
- return Device{}, errors.New(JSONR_R.Msg)
- }
- b, err = msgpack.Marshal(JSONR_R.Data)
- if err != nil {
- return Device{}, err
- }
- err = msgpack.Unmarshal(b, &Device_r)
- if err != nil {
- return Device{}, err
- }
- return Device_r, nil
- }
- func (c *Client) GetDeviceList(T_ProductID, T_sn string, T_online, PageIndex, PageSize int) (JSONS_Device, error) {
- Device_r := Device_Form{T_ProductID: T_ProductID, T_sn: T_sn, T_online: T_online, PageIndex: PageIndex, PageSize: PageSize}
- b, err := msgpack.Marshal(&Device_r)
- if err != nil {
- return JSONS_Device{}, err
- }
- // 请求-响应
- msg, err := c.NatsConn.Request("/Device/Device/List", b, 3*time.Second)
- if err != nil {
- return JSONS_Device{}, err
- }
- JSONR_R := JSONS_Device{}
- err = msgpack.Unmarshal(msg.Data, &JSONR_R)
- if err != nil {
- return JSONS_Device{}, err
- }
- if JSONR_R.Code != 200 {
- return JSONS_Device{Code: Error, Msg: JSONR_R.Msg}, errors.New(JSONR_R.Msg)
- }
- return JSONR_R, nil
- }
- func (c *Client) GetDeviceData(T_sn, T_jointTab, T_jsonFind, T_jsonSort string, PageIndex, PageSize int) (JSONS_Data, error) {
- 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}
- b, err := msgpack.Marshal(&DeviceData_Form_r)
- if err != nil {
- return JSONS_Data{}, err
- }
- if len(DeviceData_Form_r.T_sn) < 1 {
- return JSONS_Data{Code: Error, Msg: "T_sn !"}, errors.New("T_sn !")
- }
- if len(DeviceData_Form_r.T_jointTab) < 1 {
- return JSONS_Data{Code: Error, Msg: "T_jointTab !"}, errors.New("T_jointTab !")
- }
- // 请求-响应
- msg, err := c.NatsConn.Request("/Device/Device/Data", b, 3*time.Second)
- if err != nil {
- return JSONS_Data{Code: Error, Msg: "请求异常 !"}, err
- }
- JSONR_R := JSONS_Data{}
- err = msgpack.Unmarshal(msg.Data, &JSONR_R)
- if err != nil {
- return JSONS_Data{Code: Error, Msg: "解析异常 !"}, err
- }
- if JSONR_R.Code != 200 {
- return JSONS_Data{Code: Error, Msg: JSONR_R.Msg}, errors.New(JSONR_R.Msg)
- }
- return JSONR_R, nil
- }
- func (c *Client) GetDevicePush(T_sn, T_data string) error {
- Device_r := Device{T_sn: T_sn, T_data: T_data}
- b, err := msgpack.Marshal(&Device_r)
- if err != nil {
- return err
- }
- // 请求-响应
- msg, err := c.NatsConn.Request("/Device/Device/Push", b, 3*time.Second)
- if err != nil {
- return err
- }
- JSONR_R := JSONR{}
- err = msgpack.Unmarshal(msg.Data, &JSONR_R)
- if err != nil {
- return err
- }
- if JSONR_R.Code != 200 {
- return errors.New(JSONR_R.Msg)
- }
- return nil
- }
- func (c *Client) GetProductType(T_ProductID string) (ProductType, error) {
- ProductType_r := ProductType{T_ProductID: T_ProductID}
- b, err := msgpack.Marshal(&ProductType_r)
- if err != nil {
- return ProductType{}, err
- }
- // 请求-响应
- msg, err := c.NatsConn.Request("/Product/ProductType/Get", b, 3*time.Second)
- if err != nil {
- return ProductType{}, err
- }
- JSONR_R := JSONR{}
- err = msgpack.Unmarshal(msg.Data, &JSONR_R)
- if err != nil {
- return ProductType{}, err
- }
- if JSONR_R.Code != 200 {
- return ProductType{}, errors.New(JSONR_R.Msg)
- }
- b, err = msgpack.Marshal(JSONR_R.Data)
- if err != nil {
- return ProductType{}, err
- }
- err = msgpack.Unmarshal(b, &ProductType_r)
- if err != nil {
- return ProductType{}, err
- }
- return ProductType_r, nil
- }
- func (c *Client) GetProductTypeList(T_uuid, T_name string) ([]ProductType, error) {
- ProductType_r := ProductType{T_uuid: T_uuid, T_name: T_name}
- b, err := msgpack.Marshal(&ProductType_r)
- if err != nil {
- return []ProductType{}, err
- }
- // 请求-响应
- msg, err := c.NatsConn.Request("/Product/ProductType/List", b, 3*time.Second)
- if err != nil {
- return []ProductType{}, err
- }
- JSONR_R := []ProductType{}
- err = msgpack.Unmarshal(msg.Data, &JSONR_R)
- if err != nil {
- return []ProductType{}, err
- }
- return JSONR_R, nil
- }
- func (c *Client) GetProductProt(Id int) (ProductProt, error) {
- ProductProt_r := ProductProt{Id: Id}
- b, err := msgpack.Marshal(&ProductProt_r)
- if err != nil {
- return ProductProt{}, err
- }
- // 请求-响应
- msg, err := c.NatsConn.Request("/Product/ProductProt/Get", b, 3*time.Second)
- if err != nil {
- return ProductProt{}, err
- }
- JSONR_R := JSONR{}
- err = msgpack.Unmarshal(msg.Data, &JSONR_R)
- if err != nil {
- return ProductProt{}, err
- }
- if JSONR_R.Code != 200 {
- return ProductProt{}, errors.New(JSONR_R.Msg)
- }
- b, err = msgpack.Marshal(JSONR_R.Data)
- if err != nil {
- return ProductProt{}, err
- }
- err = msgpack.Unmarshal(b, &ProductProt_r)
- if err != nil {
- return ProductProt{}, err
- }
- return ProductProt_r, nil
- }
- func (c *Client) GetProductProtList(T_uuid string, T_mode int, T_name string) ([]ProductProt, error) {
- ProductProt_r := ProductProt{T_uuid: T_uuid, T_mode: T_mode, T_name: T_name}
- b, err := msgpack.Marshal(&ProductProt_r)
- if err != nil {
- return []ProductProt{}, err
- }
- // 请求-响应
- msg, err := c.NatsConn.Request("/Product/ProductProt/List", b, 3*time.Second)
- if err != nil {
- return []ProductProt{}, err
- }
- JSONR_R := []ProductProt{}
- err = msgpack.Unmarshal(msg.Data, &JSONR_R)
- if err != nil {
- return []ProductProt{}, err
- }
- return JSONR_R, nil
- }
- func (c *Client) GetProductModeList() ([]ProductMode, error) {
- // 请求-响应
- msg, err := c.NatsConn.Request("/Product/ProductModeLists/List", []byte(""), 3*time.Second)
- if err != nil {
- return []ProductMode{}, err
- }
- JSONR_R := []ProductMode{}
- err = msgpack.Unmarshal(msg.Data, &JSONR_R)
- if err != nil {
- return []ProductMode{}, err
- }
- return JSONR_R, nil
- }
|