screen.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package lc
  2. import (
  3. "fmt"
  4. "github.com/sirupsen/logrus"
  5. "golang.org/x/text/encoding/simplifiedchinese"
  6. "lc-smartX/bx"
  7. "net"
  8. "strconv"
  9. "time"
  10. )
  11. type Screener interface {
  12. Display()
  13. }
  14. type Screen struct {
  15. Id byte
  16. Sn string //B006K12311020021
  17. Name string
  18. Type byte
  19. FileNum byte
  20. Files []string
  21. Conn net.Conn
  22. IsLive bool
  23. StateInfo *bx.StateInfo //状态信息
  24. Params *bx.Params //屏参
  25. }
  26. func NewScreen(name string, conn net.Conn) Screen {
  27. s := Screen{
  28. Name: name,
  29. Conn: conn,
  30. IsLive: true,
  31. StateInfo: &bx.StateInfo{},
  32. Params: &bx.Params{},
  33. }
  34. state := s.State()
  35. s.StateInfo.Parse(state.Data)
  36. params := s.Param()
  37. s.Params.Parse(params.Data)
  38. return s
  39. }
  40. func (s *Screen) Display() {
  41. s.Lock(1, "P001")
  42. }
  43. func (s *Screen) send(data []byte) {
  44. if !s.IsLive {
  45. fmt.Println("连接已断开!")
  46. return
  47. }
  48. _, err := s.Conn.Write(data)
  49. if err != nil {
  50. logrus.Error("tcp write error:", err)
  51. s.IsLive = false
  52. }
  53. }
  54. // CorrectTime 校正时间
  55. func (s *Screen) CorrectTime() (ok bool) {
  56. if !s.IsLive {
  57. return false
  58. }
  59. cmd := bx.NewBxCmdSystemClockCorrect(time.Now())
  60. data := bx.NewBxDataPackCmd(cmd)
  61. _, err := s.Conn.Write(data.Pack())
  62. if err != nil {
  63. logrus.WithFields(map[string]interface{}{"Id": s.Id, "Name": s.Name}).Errorf("Conn写数据失败:%v", err)
  64. return false
  65. }
  66. return true
  67. }
  68. type Color byte
  69. const (
  70. Default Color = iota
  71. Red
  72. Green
  73. Yellow
  74. Blue
  75. LightBlue
  76. LightPurple
  77. White
  78. None
  79. )
  80. // TextRam
  81. // 0x01——静止显示
  82. // 0x02——快速打出
  83. // 0x03——向左移动
  84. // 0x04——向右移动
  85. // 0x05——向上移动
  86. // 0x06——向下移动
  87. func (s *Screen) TextRam(str string, color Color, playMode byte) {
  88. if !s.IsLive {
  89. return
  90. }
  91. var areas []bx.BxArea
  92. //var id byte
  93. //var x, y int16
  94. //var w, h int16 = 64, 16
  95. encoder := simplifiedchinese.GB18030.NewEncoder()
  96. bytes, err := encoder.Bytes([]byte("\\C" + strconv.Itoa(int(color)) + str))
  97. if err != nil {
  98. logrus.Error("编码转换失败:", err)
  99. return
  100. }
  101. x := 0x8000
  102. w := 0x8040
  103. area := bx.NewBxAreaDynamic(s.StateInfo.DynaAreaNum, playMode, int16(x), 0, int16(w), 16, bytes, false)
  104. areas = append(areas, area)
  105. //
  106. cmd := bx.NewBxCmdSendDynamicArea(areas)
  107. pack := bx.NewBxDataPackCmd(cmd)
  108. pack.SetDispType(2) //动态显示模式
  109. d := pack.Pack()
  110. s.send(d)
  111. s.ReadResp()
  112. s.StateInfo.DynaAreaNum++
  113. }
  114. // DelRamText 删除动态区,不传删除所有
  115. func (s *Screen) DelRamText(numbers ...byte) {
  116. if !s.IsLive {
  117. return
  118. }
  119. cmd := bx.NewCmdDelDynamicArea(numbers)
  120. pack := bx.NewBxDataPackCmd(cmd)
  121. s.send(pack.Pack())
  122. s.ReadResp()
  123. if len(numbers) == 0 {
  124. s.StateInfo.DynaAreaNum = 0
  125. } else {
  126. s.StateInfo.DynaAreaNum--
  127. }
  128. }
  129. func (s *Screen) TextFlash(msg string, color Color) {
  130. if !s.IsLive {
  131. return
  132. }
  133. encoder := simplifiedchinese.GB18030.NewEncoder()
  134. var bytes []byte
  135. //
  136. if color == None {
  137. bytes = []byte(msg)
  138. } else {
  139. bytes, _ = encoder.Bytes([]byte("\\C" + strconv.Itoa(int(color)) + msg))
  140. }
  141. x := 0x8000
  142. w := 0x8040
  143. area := bx.NewBxAreaDynamic(0xff, 2, int16(x), 0, int16(w), 16, bytes, false)
  144. //if s.FileNum
  145. name := fmt.Sprintf("P%03d", s.StateInfo.ProgramNum)
  146. fmt.Println("文件名:", name)
  147. file := bx.NewBxFile(name, "", []bx.BxArea{area})
  148. cmd := file.NewCmdWriteFile()
  149. pack := bx.NewBxDataPackCmd(cmd)
  150. data := pack.Pack()
  151. fmt.Println("数据长度:", len(data))
  152. fmt.Printf("数据:% 02x\n", data)
  153. s.send(data)
  154. resp := s.ReadResp()
  155. if !resp.IsAck() {
  156. logrus.Error("设备拒绝写文件! error:", resp.Error().Description)
  157. return
  158. }
  159. pack1 := bx.NewBxDataPackCmd(cmd)
  160. data1 := pack1.Pack()
  161. fmt.Println("数据长度:", len(data))
  162. s.send(data1)
  163. fmt.Printf("数据1:% 02x\n", data1)
  164. resp1 := s.ReadResp()
  165. if resp1.NoError() {
  166. s.StateInfo.ProgramNum++
  167. }
  168. }
  169. func (s *Screen) Bitmap(name string, bitmap []byte) {
  170. file := bx.NewBitmapFile(name, bitmap)
  171. cmd := file.NewCmd()
  172. pack := bx.NewBxDataPackCmd(cmd)
  173. data := pack.Pack()
  174. fmt.Println("数据长度:", len(data))
  175. fmt.Printf("数据:% 02x\n", data)
  176. s.send(data)
  177. resp := s.ReadResp()
  178. if !resp.IsAck() {
  179. logrus.Error("设备拒绝写文件! error:", resp.Error().Description)
  180. return
  181. }
  182. pack1 := bx.NewBxDataPackCmd(cmd)
  183. data1 := pack1.Pack()
  184. fmt.Println("数据长度1:", len(data1))
  185. s.send(data1)
  186. fmt.Printf("写图文件数据:% 02x\n", data1)
  187. s.ReadResp()
  188. }
  189. // Lock 锁定状态:0x00——解锁状态,0x01——锁定状态
  190. func (s *Screen) Lock(flag byte, name string) {
  191. cmd := bx.NewCmdLock(flag, name)
  192. pack := bx.NewBxDataPackCmd(&cmd)
  193. s.send(pack.Pack())
  194. }
  195. func (s *Screen) QueryFile() {
  196. }
  197. func (s *Screen) DelFile(delFiles ...string) {
  198. cmd := bx.NewCmdDeleteFile(delFiles)
  199. pack := bx.NewBxDataPackCmd(cmd)
  200. s.send(pack.Pack())
  201. s.ReadResp()
  202. if len(delFiles) == 0 {
  203. s.StateInfo.ProgramNum = 0
  204. } else {
  205. s.StateInfo.ProgramNum--
  206. }
  207. }
  208. func (s *Screen) DelText(delIds []byte) {
  209. cmd := bx.NewBxCmdSendDynamicArea(nil)
  210. cmd.SetDelAreaIds(delIds)
  211. pack := bx.NewBxDataPackCmd(cmd)
  212. s.send(pack.Pack())
  213. }
  214. func (s *Screen) TurnOnOff(onOff bool) {
  215. if !s.IsLive {
  216. return
  217. }
  218. cmd := bx.NewBxCmdTurnOnOff(onOff)
  219. pack := bx.NewBxDataPackCmd(cmd)
  220. s.send(pack.Pack())
  221. }
  222. // TimingSwitch 定时开关屏
  223. // 13:49 开, 13:55 关,最多设置3组
  224. //
  225. // onOffSet := [][2]uint64{
  226. // {1349, 1355},
  227. // }
  228. func (s *Screen) TimingSwitch(onOffSet [][2]uint64) {
  229. if !s.IsLive {
  230. return
  231. }
  232. cmd := bx.NewCmdTimingSwitch(onOffSet)
  233. pack := bx.NewBxDataPackCmd(cmd)
  234. s.send(pack.Pack())
  235. }
  236. func (s *Screen) CancelTimingSwitch() {
  237. if !s.IsLive {
  238. return
  239. }
  240. cmd := bx.NewCmdCancelTimingSwitch()
  241. pack := bx.NewBxDataPackCmd(cmd)
  242. s.send(pack.Pack())
  243. }
  244. func (s *Screen) State() *bx.BxResp {
  245. if !s.IsLive {
  246. return nil
  247. }
  248. cmd := bx.NewCmdState()
  249. pack := bx.NewBxDataPackCmd(cmd)
  250. s.send(pack.Pack())
  251. r := s.ReadResp()
  252. return &r
  253. }
  254. func (s *Screen) Param() *bx.BxResp {
  255. if !s.IsLive {
  256. return nil
  257. }
  258. cmd := bx.NewCmdReadParams()
  259. pack := bx.NewBxDataPackCmd(cmd)
  260. s.send(pack.Pack())
  261. r := s.ReadResp()
  262. return &r
  263. }
  264. func (s *Screen) Info() {
  265. s.StateInfo.Print(s.Name)
  266. }
  267. func (s *Screen) ReadResp() bx.BxResp {
  268. var resp = make([]byte, 1024)
  269. read, err := s.Conn.Read(resp)
  270. if err != nil {
  271. logrus.Error("读数据错误:", err)
  272. s.IsLive = false
  273. return bx.BxResp{}
  274. }
  275. var bxResp = bx.BxResp{}
  276. parse := bxResp.Parse(resp, read)
  277. if parse.IsAck() {
  278. fmt.Println("response ACK")
  279. fmt.Printf("原始响应数据:% 0x\n", resp[:read])
  280. fmt.Println("解析响应数据:", parse)
  281. } else if parse.IsInfo() {
  282. fmt.Println("state ACK")
  283. fmt.Printf("原始响应数据:% 0x\n", resp[:read])
  284. fmt.Println("解析响应数据:", parse)
  285. } else {
  286. fmt.Println("response")
  287. fmt.Printf("原始响应数据:% 0x\n", resp[:read])
  288. fmt.Println("解析响应数据:", parse)
  289. }
  290. return *parse
  291. }