common.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package zigbee
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "time"
  6. "github.com/valyala/bytebufferpool"
  7. )
  8. var (
  9. HeadFlag2Down uint8 = 0xF2
  10. HeadFlag2Up uint8 = 0xF3
  11. )
  12. var (
  13. CmdSendData uint8 = 0x0B
  14. CmdSendDataAck uint8 = 0x8B
  15. CmdSetPid uint8 = 0x13
  16. CmdGetPid uint8 = 0x14
  17. CmdSetPidAck uint8 = 0x93
  18. CmdGetPidAck uint8 = 0x94
  19. )
  20. var ErrorProtocol = errors.New("协议错误")
  21. var ErrorInvalidCrc1 = errors.New("外层CRC校验错误")
  22. var ErrorInvalidCrc2 = errors.New("内层CRC校验错误")
  23. type Pack interface {
  24. SetData(Count, No, Cmd uint16, Data []byte)
  25. EnCode() (*bytebufferpool.ByteBuffer, error)
  26. DeCode(Data []byte) error
  27. }
  28. // MsgHeader2 消息格式
  29. type MsgHeader2 struct {
  30. Head byte //集中到单灯F2,单灯到集中F3
  31. PoleID uint32 //杆号
  32. Cmd2 uint8 //固定为F2
  33. Seq uint8 //流水号,循环利用
  34. Length2 uint8 //包体2长度
  35. Body2 Pack //包体内容,普通功能协议或升级功能协议
  36. Crc2 uint16 //内层CRC
  37. }
  38. func (o *MsgHeader2) SetDody(body Pack) {
  39. o.Body2 = body
  40. }
  41. func (o *MsgHeader2) SetData(PoleID uint32, Seq uint8) {
  42. o.PoleID = PoleID
  43. o.Seq = Seq
  44. }
  45. func (o *MsgHeader2) EnCode() (*bytebufferpool.ByteBuffer, error) {
  46. bytebuf, err := o.Body2.EnCode() //Body2 Pack //包体内容,普通功能协议或升级功能协议
  47. defer func() {
  48. if bytebuf != nil {
  49. bytebufferpool.Put(bytebuf)
  50. }
  51. }()
  52. if err != nil {
  53. return nil, err
  54. }
  55. buff := bytebufferpool.Get()
  56. buff.WriteByte(HeadFlag2Down) //Head byte //集中到单灯F2,单灯到集中F3
  57. tmp := make([]byte, 4)
  58. binary.BigEndian.PutUint32(tmp, o.PoleID)
  59. buff.Write(tmp) //LampID uint32 //杆号
  60. buff.WriteByte(0xF2) //Cmd2 uint8 //固定为F2
  61. buff.WriteByte(o.Seq) //Seq uint8 //流水号,循环利用
  62. buff.WriteByte(uint8(bytebuf.Len())) //Length2 uint8 //包体2长度
  63. buff.Write(bytebuf.B) //Body2 Pack //包体内容,普通功能协议或升级功能协议
  64. crc16 := CRC16(buff.B)
  65. tmp2 := make([]byte, 2)
  66. binary.BigEndian.PutUint16(tmp2, crc16)
  67. buff.Write(tmp2) //计算内层CRC16
  68. return buff, nil
  69. }
  70. func (o *MsgHeader2) DeCode(Data []byte) error {
  71. if len(Data) >= 10 {
  72. o.Head = Data[0] //集中到单灯F2,单灯到集中F3
  73. o.PoleID = binary.BigEndian.Uint32(Data[1:5]) //杆号
  74. o.Cmd2 = Data[5] //固定为F2
  75. o.Seq = Data[6] //流水号,循环利用
  76. o.Length2 = Data[7] //包体2长度
  77. o.Crc2 = binary.BigEndian.Uint16(Data[o.Length2+8 : o.Length2+10])
  78. crc := CRC16(Data[0 : o.Length2+8])
  79. if crc != o.Crc2 {
  80. return ErrorInvalidCrc2
  81. }
  82. return o.Body2.DeCode(Data[8 : o.Length2+8])
  83. }
  84. return ErrorProtocol
  85. }
  86. // GeneralFunc 普通功能协议
  87. type GeneralFunc struct {
  88. Len uint8 //此包长度
  89. Count uint8 //总包数
  90. No uint8 //当前升级包号
  91. Cmd uint8 //单杆命令
  92. Data []byte //数据内容
  93. }
  94. func (o *GeneralFunc) SetData(Count, No, Cmd uint16, Data []byte) {
  95. o.Cmd = uint8(Cmd)
  96. o.Data = Data
  97. o.Count = uint8(Count)
  98. o.No = uint8(No)
  99. }
  100. func (o *GeneralFunc) EnCode() (*bytebufferpool.ByteBuffer, error) {
  101. buff := bytebufferpool.Get()
  102. buff.WriteByte(uint8(4 + len(o.Data))) // Len uint8 //此包长度
  103. buff.WriteByte(0) //Count uint8 //总包数
  104. buff.WriteByte(0) //No uint8 //当前升级包号
  105. buff.WriteByte(o.Cmd) //M1Cmd uint8 //单杆命令
  106. buff.Write(o.Data) //mapData []byte //数据内容
  107. return buff, nil
  108. }
  109. func (o *GeneralFunc) DeCode(Data []byte) error {
  110. if len(Data) >= 4 {
  111. o.Len = Data[0]
  112. o.Count = Data[1]
  113. o.No = Data[2]
  114. o.Cmd = Data[3]
  115. o.Data = Data[4:]
  116. return nil
  117. }
  118. return ErrorProtocol
  119. }
  120. // UpgradeFunc 升级功能协议
  121. type UpgradeFunc struct {
  122. Len uint8 //此包长度
  123. Count uint16 //总包数
  124. No uint16 //当前升级包号
  125. Cmd uint8 //单杆命令
  126. Data []byte //数据内容
  127. }
  128. func (o *UpgradeFunc) SetData(Count, No, Cmd uint16, Data []byte) {
  129. o.Cmd = uint8(Cmd)
  130. o.Data = Data
  131. o.Count = Count
  132. o.No = No
  133. }
  134. func (o *UpgradeFunc) EnCode() (*bytebufferpool.ByteBuffer, error) {
  135. buff := bytebufferpool.Get()
  136. buff.WriteByte(uint8(6 + len(o.Data))) // Len uint8 //此包长度
  137. tmp := make([]byte, 2)
  138. binary.BigEndian.PutUint16(tmp, o.Count)
  139. buff.Write(tmp) //Count uint16 //总包数
  140. binary.BigEndian.PutUint16(tmp, o.No)
  141. buff.Write(tmp) //No uint16 //当前升级包号
  142. buff.WriteByte(o.Cmd) //M1Cmd uint8 //单杆命令
  143. buff.Write(o.Data) //mapData []byte //数据内容
  144. return buff, nil
  145. }
  146. func (o *UpgradeFunc) DeCode(Data []byte) error {
  147. if len(Data) >= 4 {
  148. o.Len = Data[0]
  149. o.Count = binary.BigEndian.Uint16(Data[1:3])
  150. o.No = binary.BigEndian.Uint16(Data[3:5])
  151. o.Cmd = Data[5]
  152. o.Data = Data[6:]
  153. return nil
  154. }
  155. return ErrorProtocol
  156. }
  157. type OnOffTime struct {
  158. OnHour uint8 `json:"onhour"`
  159. OnMinite uint8 `json:"onminite"`
  160. OffHour uint8 `json:"offhour"`
  161. OffMinite uint8 `json:"offminite"`
  162. Brightness uint8 `json:"brightness"` //亮度0~100
  163. }
  164. func (o *OnOffTime) SetData(onHour, onMinite, offHour, offMinite, brightness uint8) {
  165. o.OnHour = onHour
  166. o.OnMinite = onMinite
  167. o.OffHour = offHour
  168. o.OffMinite = offMinite
  169. o.Brightness = brightness
  170. }
  171. func (o *OnOffTime) EnCode() []byte {
  172. buff := make([]byte, 5)
  173. buff[0] = o.OnHour
  174. buff[1] = o.OnMinite
  175. buff[2] = o.OffHour
  176. buff[3] = o.OffMinite
  177. buff[4] = o.Brightness
  178. return buff
  179. }
  180. func (o *OnOffTime) DeCode(data []byte) error {
  181. if len(data) >= 5 {
  182. o.OnHour = data[0]
  183. o.OnMinite = data[1]
  184. o.OffHour = data[2]
  185. o.OffMinite = data[3]
  186. o.Brightness = data[4]
  187. return nil
  188. }
  189. return ErrorProtocol
  190. }
  191. func (o *OnOffTime) InTimeRange(t time.Time) bool {
  192. h := uint8(t.Hour())
  193. m := uint8(t.Minute())
  194. if o.OnHour > o.OffHour { //跨日情况,例如19:30~5:30,开灯时间大于关灯时间
  195. if h > o.OnHour || h < o.OffHour || (h == o.OnHour && m >= o.OnMinite) ||
  196. (h == o.OffHour && m <= o.OffMinite) {
  197. return true
  198. }
  199. } else if o.OnHour < o.OffHour { //开灯时间小于关灯时间
  200. if (h > o.OnHour && h < o.OffHour) ||
  201. (h == o.OnHour && m >= o.OnMinite) ||
  202. (h == o.OffHour && m <= o.OffMinite) {
  203. return true
  204. }
  205. } else { //开灯时间等于关灯时间
  206. if h == o.OnHour && m >= o.OnMinite && m <= o.OffMinite {
  207. return true
  208. }
  209. }
  210. return false
  211. }
  212. // CalculateChddjkZigbeeResponselength 长和单灯控制器zigbee协议,预估长度,非精确长度
  213. func CalculateChddjkZigbeeResponselength(adu []byte) int {
  214. return int(float32(len(adu)) * 1.5)
  215. }