klink.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package klink
  2. import "github.com/gogf/gf/encoding/gjson"
  3. type PacketAction string
  4. const (
  5. DevSendAction PacketAction = "devSend" // 设备上报数据
  6. BatchDevSendAction PacketAction = "batchDevSend" // 网关批量上报设备数据
  7. CloudSendAction PacketAction = "cloudSend" // 云端给设备下发指令
  8. ReportFirmwareAction PacketAction = "reportFirmware" // 设备上报固件信息
  9. DevLoginAction PacketAction = "devLogin" // 子设备上线
  10. DevLogoutAction PacketAction = "devLogout" // 子设备下线
  11. DevNetConfigAction PacketAction = "devNetConfig" // 设备配网信息
  12. DevUpgradeAction PacketAction = "devUpgrade" // 设备升级
  13. )
  14. // DevLogin 子设备上线
  15. type DevLogin struct {
  16. Action string `json:"action"`
  17. MsgId int64 `json:"msgId"`
  18. DeviceCode string `json:"deviceCode"`
  19. Pk string `json:"pk"`
  20. SubDeviceId string `json:"subDeviceId"`
  21. Timestamp int64 `json:"timestamp"`
  22. }
  23. func (d *DevLogin) Marshal() ([]byte, error) {
  24. return gjson.New(d).ToJson()
  25. }
  26. func (d *DevLogin) UnMarshal(bytes []byte) error {
  27. j, err := gjson.DecodeToJson(bytes)
  28. if err != nil {
  29. return err
  30. }
  31. return j.Struct(d)
  32. }
  33. // DevLogout 子设备下线
  34. type DevLogout struct {
  35. Action string `json:"action"`
  36. Pk string `json:"pk"`
  37. MsgId int64 `json:"msgId"`
  38. DeviceCode string `json:"deviceCode"`
  39. SubDeviceId string `json:"subDeviceId"`
  40. Timestamp int64 `json:"timestamp"`
  41. }
  42. func (d *DevLogout) Marshal() ([]byte, error) {
  43. return gjson.New(d).ToJson()
  44. }
  45. func (d *DevLogout) UnMarshal(bytes []byte) error {
  46. j, err := gjson.DecodeToJson(bytes)
  47. if err != nil {
  48. return err
  49. }
  50. return j.Struct(d)
  51. }
  52. // DevSend 设备上报数据帧
  53. type DevSend struct {
  54. Action string `json:"action"`
  55. MsgId int64 `json:"msgId"`
  56. DeviceCode string `json:"deviceCode"`
  57. SubDeviceId string `json:"subDeviceId"`
  58. Data *DevSendData `json:"data"`
  59. Timestamp int64 `json:"timestamp"`
  60. Version string `json:"version"`
  61. }
  62. func (d *DevSend) Marshal() ([]byte, error) {
  63. return gjson.New(d).ToJson()
  64. }
  65. func (d *DevSend) UnMarshal(bytes []byte) error {
  66. j, err := gjson.DecodeToJson(bytes)
  67. if err != nil {
  68. return err
  69. }
  70. return j.Struct(d)
  71. }
  72. type DevSendData struct {
  73. Cmd string `json:"cmd"`
  74. Params *gjson.Json `json:"params"`
  75. }
  76. type CloudSendData struct {
  77. Cmd string `json:"cmd"`
  78. Params map[string]interface{} `json:"params"`
  79. }
  80. type CloudSend struct {
  81. Action string `json:"action"`
  82. MsgId int64 `json:"msgId"`
  83. DeviceCode string `json:"deviceCode"`
  84. Timestamp int64 `json:"timestamp"`
  85. Data *CloudSendData `json:"data"`
  86. SubDeviceId string `json:"subDeviceId"`
  87. }
  88. func (c *CloudSend) Marshal() ([]byte, error) {
  89. return gjson.New(c).ToJson()
  90. }
  91. func (c *CloudSend) UnMarshal(bytes []byte) error {
  92. j, err := gjson.DecodeToJson(bytes)
  93. if err != nil {
  94. return err
  95. }
  96. return j.Struct(c)
  97. }
  98. // DevUpgrade 设备升级
  99. type DevUpgrade struct {
  100. Action string `json:"action"`
  101. MsgId int `json:"msgId"`
  102. DeviceCode string `json:"deviceCode"`
  103. SubDeviceId string `json:"subDeviceId"`
  104. Timestamp int64 `json:"timestamp"`
  105. Url string `json:"url"`
  106. Md5 string `json:"md5"`
  107. Type string `json:"type"`
  108. Version string `json:"version"`
  109. }
  110. func (c *DevUpgrade) Marshal() ([]byte, error) {
  111. return gjson.New(c).ToJson()
  112. }
  113. func (c *DevUpgrade) UnMarshal(bytes []byte) error {
  114. j, err := gjson.DecodeToJson(bytes)
  115. if err != nil {
  116. return err
  117. }
  118. return j.Struct(c)
  119. }