packing.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package zigbee
  2. import (
  3. "github.com/valyala/bytebufferpool"
  4. )
  5. var (
  6. CmdReadDldy uint8 = 0x01
  7. CmdReadBrightness uint8 = 0x02
  8. CmdSetAuto uint8 = 0x08
  9. CmdSetBrightness uint8 = 0x12
  10. CmdReadGroup uint8 = 0x05
  11. CmdSetGroup uint8 = 0x15
  12. CmdSetEnabled uint8 = 0x16
  13. CmdSetCorrectiontime uint8 = 0x23
  14. CmdSetOnofftime uint8 = 0x26
  15. CmdReadOnofftime uint8 = 0x27
  16. CmdSetOnofftime2 uint8 = 0x28
  17. CmdReadOnofftime2 uint8 = 0x29
  18. CmdReadTime uint8 = 0x30
  19. CmdSetBroadcastCorrectiontime uint8 = 0x70
  20. CmdReadOnofftime12 uint8 = 0x2F
  21. CmdSetBroadcastOn uint8 = 0x84
  22. CmdSetBroadcastOff uint8 = 0x85
  23. CmdSetBroadcastAuto uint8 = 0x86
  24. )
  25. // PackGeneralFuncCommand /////////////////////////////////////////////////////////////////////////////////////////////
  26. //公共方法
  27. ///////////////////////////////////////////////////////////////////////////////////////////////
  28. type PackGeneralFuncCommand struct {
  29. Cmd uint8
  30. Seq uint8
  31. PoleID uint32
  32. Data []byte
  33. }
  34. func (o *PackGeneralFuncCommand) SetData(PoleID uint32, Cmd, Seq uint8, Data []byte) {
  35. o.Cmd = Cmd
  36. o.PoleID = PoleID
  37. o.Data = Data
  38. o.Seq = Seq
  39. }
  40. func (o *PackGeneralFuncCommand) EnCode() (*bytebufferpool.ByteBuffer, error) {
  41. var psd PackSendData
  42. var gf GeneralFunc
  43. psd.Body1.Data.SetDody(&gf)
  44. psd.Body1.Data.SetData(o.PoleID, o.Seq)
  45. psd.Body1.Data.Body2.SetData(0, 0, uint16(o.Cmd), o.Data)
  46. return psd.EnCode()
  47. }
  48. func (o *PackGeneralFuncCommand) DeCode(data []byte) error {
  49. var psd PackSendData
  50. var gf GeneralFunc
  51. psd.Body1.Data.SetDody(&gf)
  52. err := psd.DeCode(data)
  53. if err == nil {
  54. o.Cmd = gf.Cmd
  55. o.Seq = psd.Body1.Data.Seq
  56. o.PoleID = psd.Body1.Data.PoleID
  57. o.Data = make([]byte, len(gf.Data))
  58. copy(o.Data, gf.Data)
  59. }
  60. return err
  61. }
  62. type PackUpgradeFuncCommand struct {
  63. Cmd uint8
  64. Seq uint8
  65. PoleID uint32
  66. Data []byte
  67. }
  68. func (o *PackUpgradeFuncCommand) SetData(PoleID uint32, Cmd, Seq uint8, Data []byte) {
  69. o.Cmd = Cmd
  70. o.PoleID = PoleID
  71. o.Data = Data
  72. o.Seq = Seq
  73. }
  74. func (o *PackUpgradeFuncCommand) EnCode() (*bytebufferpool.ByteBuffer, error) {
  75. var psd PackSendData
  76. var up UpgradeFunc
  77. psd.Body1.Data.SetDody(&up)
  78. psd.Body1.Data.SetData(o.PoleID, o.Seq)
  79. psd.Body1.Data.Body2.SetData(0, 0, uint16(o.Cmd), o.Data)
  80. return psd.EnCode()
  81. }
  82. func (o *PackUpgradeFuncCommand) DeCode(data []byte) error {
  83. var psd PackSendData
  84. var uf UpgradeFunc
  85. psd.Body1.Data.SetDody(&uf)
  86. err := psd.DeCode(data)
  87. if err == nil {
  88. o.Cmd = uf.Cmd
  89. o.Seq = psd.Body1.Data.Seq
  90. o.PoleID = psd.Body1.Data.PoleID
  91. o.Data = make([]byte, len(uf.Data))
  92. copy(o.Data, uf.Data)
  93. }
  94. return err
  95. }