klink.go 2.3 KB

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