BxResp.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package bx
  2. import (
  3. "encoding/binary"
  4. "encoding/hex"
  5. "fmt"
  6. )
  7. type BxResp struct {
  8. group, cmd, Err, r0, r1 byte
  9. Data []byte
  10. //StateInfo *StateInfo
  11. }
  12. func parse_(pack BxDataPack) *BxResp {
  13. var offset int
  14. resp := &BxResp{}
  15. resp.group = pack.data[offset]
  16. offset++
  17. resp.cmd = pack.data[offset]
  18. offset++
  19. resp.Err = pack.data[offset]
  20. offset++
  21. resp.r0 = pack.data[offset]
  22. offset++
  23. resp.r1 = pack.data[offset]
  24. offset++
  25. resp.Data = pack.data[offset:(offset + int(pack.dataLen) - 5)]
  26. //if !resp.IsInfo() {
  27. // return resp
  28. //}
  29. //offset = 0
  30. //resp.StateInfo = &StateInfo{}
  31. //resp.StateInfo.OnOff = resp.Data[offset]
  32. //offset++
  33. //resp.StateInfo.Brightness = resp.Data[offset]
  34. //offset++
  35. //t := resp.Data[offset : offset+8]
  36. //resp.StateInfo.SystemTime = hex.EncodeToString(t)
  37. //offset += 8
  38. //resp.StateInfo.ProgramNum = resp.Data[offset]
  39. //offset++
  40. //resp.StateInfo.CruFileName = string(resp.Data[offset : offset+4])
  41. //offset += 4
  42. //resp.StateInfo.SpecialDynaArea = resp.Data[offset]
  43. //offset++
  44. //resp.StateInfo.PageNum = resp.Data[offset]
  45. //offset++
  46. //resp.StateInfo.DynaAreaNum = resp.Data[offset]
  47. //offset++
  48. //for i := byte(0); i < resp.StateInfo.ProgramNum; i++ {
  49. // resp.StateInfo.DynaAreaIDs = append(resp.StateInfo.DynaAreaIDs, resp.Data[offset])
  50. // offset++
  51. //}
  52. //resp.StateInfo.BarCode = string(resp.Data[offset : offset+16])
  53. return resp
  54. }
  55. func (r BxResp) Parse(src []byte, len int) *BxResp {
  56. dp := dpParse(src, len)
  57. if dp == nil {
  58. return nil
  59. } else {
  60. return parse_(*dp)
  61. }
  62. }
  63. func (r BxResp) IsAck() bool {
  64. if r.group == CMD_ACK.group && r.cmd == CMD_ACK.code {
  65. return true
  66. } else {
  67. return false
  68. }
  69. }
  70. func (r BxResp) NoError() bool {
  71. return r.Err == 0
  72. }
  73. func (r BxResp) Error() BxError {
  74. return bxErrors[r.Err]
  75. }
  76. // IsInfo 是否是返回"控制器状态"信息
  77. func (r BxResp) IsInfo() bool {
  78. if r.group == CMD_SYSTEM_STATE.group && r.cmd == CMD_SYSTEM_STATE.code {
  79. return true
  80. } else {
  81. return false
  82. }
  83. }
  84. type StateInfo struct {
  85. OnOff byte
  86. Brightness byte
  87. SystemTime string //年(2)+月(1)+日(1)+星期(1)+时(1)+分(1)+秒(1)
  88. ProgramNum byte
  89. CruFileName string
  90. SpecialDynaArea byte
  91. PageNum byte
  92. DynaAreaNum byte
  93. DynaAreaIDs []byte
  94. BarCode string
  95. }
  96. func (s *StateInfo) Print(name string) {
  97. info :=
  98. `==== %s ====
  99. 电源状态: %s
  100. 系统时间: %s
  101. 节目数量: %d
  102. 当前播放: %s
  103. 动态区数: %d
  104. 动态ID: %v
  105. 条码: %s
  106. ===============
  107. `
  108. onoff := ""
  109. if s.OnOff == 1 {
  110. onoff = "开机"
  111. } else {
  112. onoff = "关机"
  113. }
  114. fmt.Printf(info, name, onoff, s.SystemTime, s.ProgramNum, s.CruFileName, s.DynaAreaNum, s.DynaAreaIDs,
  115. s.BarCode)
  116. }
  117. func (s *StateInfo) Parse(data []byte) {
  118. offset := 0
  119. s.OnOff = data[offset]
  120. offset++
  121. s.Brightness = data[offset]
  122. offset++
  123. t := data[offset : offset+8]
  124. s.SystemTime = hex.EncodeToString(t)
  125. offset += 8
  126. s.ProgramNum = data[offset]
  127. offset++
  128. s.CruFileName = string(data[offset : offset+4])
  129. offset += 4
  130. s.SpecialDynaArea = data[offset]
  131. offset++
  132. s.PageNum = data[offset]
  133. offset++
  134. s.DynaAreaNum = data[offset]
  135. offset++
  136. for i := byte(0); i < s.DynaAreaNum; i++ {
  137. s.DynaAreaIDs = append(s.DynaAreaIDs, data[offset])
  138. offset++
  139. }
  140. s.BarCode = string(data[offset : offset+16])
  141. }
  142. type Params struct {
  143. Address uint16
  144. DeviceType byte
  145. BaudRate byte
  146. ScreenWidth uint16
  147. ScreenHeight uint16
  148. Color byte
  149. DA byte
  150. OE byte
  151. FreqPar byte
  152. RowOrder byte
  153. MirrorMode byte
  154. OEAngle byte
  155. ScanMode byte
  156. ScanConfNum byte
  157. LatticeMode byte
  158. //Reserved []byte
  159. }
  160. func (p *Params) Parse(data []byte) {
  161. offset := 0
  162. addr, err := BIN2Uint64(data[offset:offset+2], binary.LittleEndian)
  163. if err == nil {
  164. p.Address = uint16(addr)
  165. }
  166. offset += 2
  167. p.DeviceType = data[offset]
  168. offset++
  169. p.BaudRate = data[offset]
  170. offset++
  171. w, err := BIN2Uint64(data[offset:offset+2], binary.LittleEndian)
  172. if err == nil {
  173. p.ScreenWidth = uint16(w)
  174. }
  175. offset += 2
  176. h, err := BIN2Uint64(data[offset:offset+2], binary.LittleEndian)
  177. if err == nil {
  178. p.ScreenHeight = uint16(h)
  179. }
  180. offset += 2
  181. p.Color = data[offset]
  182. offset++
  183. p.DA = data[offset]
  184. offset++
  185. p.OE = data[offset]
  186. offset++
  187. p.FreqPar = data[offset]
  188. offset++
  189. p.RowOrder = data[offset]
  190. offset++
  191. p.MirrorMode = data[offset]
  192. offset++
  193. p.OEAngle = data[offset]
  194. offset++
  195. p.ScanMode = data[offset]
  196. offset++
  197. p.ScanConfNum = data[offset]
  198. offset++
  199. //p.LatticeMode = data[offset]
  200. //offset++
  201. }