klink.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. )
  13. // DevLogin 子设备上线
  14. type DevLogin struct {
  15. Action string `json:"action"`
  16. MsgId int64 `json:"msgId"`
  17. DeviceCode string `json:"deviceCode"`
  18. Pk string `json:"pk"`
  19. SubDeviceId string `json:"subDeviceId"`
  20. Timestamp int64 `json:"timestamp"`
  21. }
  22. func (d *DevLogin) Marshal() ([]byte, error) {
  23. return gjson.New(d).ToJson()
  24. }
  25. func (d *DevLogin) UnMarshal(bytes []byte) error {
  26. j, err := gjson.DecodeToJson(bytes)
  27. if err != nil {
  28. return err
  29. }
  30. return j.Struct(d)
  31. }
  32. // DevLogout 子设备下线
  33. type DevLogout struct {
  34. Action string `json:"action"`
  35. Pk string `json:"pk"`
  36. MsgId int64 `json:"msgId"`
  37. DeviceCode string `json:"deviceCode"`
  38. SubDeviceId string `json:"subDeviceId"`
  39. Timestamp int64 `json:"timestamp"`
  40. }
  41. func (d *DevLogout) Marshal() ([]byte, error) {
  42. return gjson.New(d).ToJson()
  43. }
  44. func (d *DevLogout) UnMarshal(bytes []byte) error {
  45. j, err := gjson.DecodeToJson(bytes)
  46. if err != nil {
  47. return err
  48. }
  49. return j.Struct(d)
  50. }
  51. // DevSend 设备上报数据帧
  52. type DevSend struct {
  53. Action string `json:"action"`
  54. MsgId int64 `json:"msgId"`
  55. DeviceCode string `json:"deviceCode"`
  56. SubDeviceId string `json:"subDeviceId"`
  57. Data *DevSendData `json:"data"`
  58. Timestamp int64 `json:"timestamp"`
  59. }
  60. func (d *DevSend) Marshal() ([]byte, error) {
  61. return gjson.New(d).ToJson()
  62. }
  63. func (d *DevSend) UnMarshal(bytes []byte) error {
  64. j, err := gjson.DecodeToJson(bytes)
  65. if err != nil {
  66. return err
  67. }
  68. return j.Struct(d)
  69. }
  70. type DevSendData struct {
  71. Cmd string `json:"cmd"`
  72. Params *gjson.Json `json:"params"`
  73. }
  74. type CloudSendData struct {
  75. Cmd string `json:"cmd"`
  76. Params *gjson.Json `json:"params"`
  77. }
  78. type CloudSend struct {
  79. Action string `json:"action"`
  80. MsgId int64 `json:"msgId"`
  81. DeviceCode string `json:"deviceCode"`
  82. Timestamp int64 `json:"timestamp"`
  83. Data *CloudSendData `json:"data"`
  84. SubDeviceId string `json:"subDeviceId"`
  85. }
  86. func (c *CloudSend) Marshal() ([]byte, error) {
  87. return gjson.New(c).ToJson()
  88. }
  89. func (c *CloudSend) UnMarshal(bytes []byte) error {
  90. j, err := gjson.DecodeToJson(bytes)
  91. if err != nil {
  92. return err
  93. }
  94. return j.Struct(c)
  95. }