BxResp.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. //CustomId string
  96. }
  97. func (s *StateInfo) Print(name string) {
  98. info :=
  99. `==== %s ====
  100. 电源状态: %s
  101. 系统时间: %s
  102. 节目数量: %d
  103. 当前播放: %s
  104. 动态区数: %d
  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.BarCode)
  115. }
  116. func (s *StateInfo) Parse(data []byte) {
  117. offset := 0
  118. s.OnOff = data[offset]
  119. offset++
  120. s.Brightness = data[offset]
  121. offset++
  122. t := data[offset : offset+8]
  123. s.SystemTime = hex.EncodeToString(t)
  124. offset += 8
  125. s.ProgramNum = data[offset]
  126. offset++
  127. s.CruFileName = string(data[offset : offset+4])
  128. offset += 4
  129. s.SpecialDynaArea = data[offset]
  130. offset++
  131. s.PageNum = data[offset]
  132. offset++
  133. s.DynaAreaNum = data[offset]
  134. offset++
  135. for i := byte(0); i < s.DynaAreaNum; i++ {
  136. s.DynaAreaIDs = append(s.DynaAreaIDs, data[offset])
  137. offset++
  138. }
  139. s.BarCode = string(data[offset : offset+16])
  140. }
  141. type Params struct {
  142. Address uint16
  143. DeviceType byte
  144. BaudRate byte
  145. ScreenWidth uint16
  146. ScreenHeight uint16
  147. Color byte
  148. DA byte
  149. OE byte
  150. FreqPar byte
  151. RowOrder byte
  152. MirrorMode byte
  153. OEAngle byte
  154. ScanMode byte
  155. ScanConfNum byte
  156. LatticeMode byte
  157. //Reserved []byte
  158. }
  159. func (p *Params) Parse(data []byte) {
  160. offset := 0
  161. addr, err := BIN2Uint64(data[offset:offset+2], binary.LittleEndian)
  162. if err == nil {
  163. p.Address = uint16(addr)
  164. }
  165. offset += 2
  166. p.DeviceType = data[offset]
  167. offset++
  168. p.BaudRate = data[offset]
  169. offset++
  170. w, err := BIN2Uint64(data[offset:offset+2], binary.LittleEndian)
  171. if err == nil {
  172. p.ScreenWidth = uint16(w)
  173. }
  174. offset += 2
  175. h, err := BIN2Uint64(data[offset:offset+2], binary.LittleEndian)
  176. if err == nil {
  177. p.ScreenHeight = uint16(h)
  178. }
  179. offset += 2
  180. p.Color = data[offset]
  181. offset++
  182. p.DA = data[offset]
  183. offset++
  184. p.OE = data[offset]
  185. offset++
  186. p.FreqPar = data[offset]
  187. offset++
  188. p.RowOrder = data[offset]
  189. offset++
  190. p.MirrorMode = data[offset]
  191. offset++
  192. p.OEAngle = data[offset]
  193. offset++
  194. p.ScanMode = data[offset]
  195. offset++
  196. p.ScanConfNum = data[offset]
  197. offset++
  198. //p.LatticeMode = data[offset]
  199. //offset++
  200. }