klink.go 2.8 KB

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