screen.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. Name string
  16. Conn net.Conn
  17. IsLive bool
  18. StateInfo *bx.StateInfo //状态信息
  19. Params *bx.Params //屏参
  20. }
  21. func NewScreen(name string, conn net.Conn) Screen {
  22. s := Screen{
  23. Name: name,
  24. Conn: conn,
  25. IsLive: true,
  26. StateInfo: &bx.StateInfo{},
  27. Params: &bx.Params{},
  28. }
  29. state := s.State()
  30. s.StateInfo.Parse(state.Data)
  31. params := s.Param()
  32. s.Params.Parse(params.Data)
  33. return s
  34. }
  35. func (s *Screen) Display() {
  36. if !s.IsLive {
  37. return
  38. }
  39. s.Lock(1, "P001")
  40. }
  41. func (s *Screen) Back() {
  42. s.Lock(1, "P000")
  43. }
  44. // Correct 校正时间
  45. func (s *Screen) Correct() {
  46. now := time.Now()
  47. if !s.IsLive {
  48. return
  49. }
  50. cmd := bx.NewBxCmdSystemClockCorrect(now)
  51. data := bx.NewBxDataPackCmd(cmd)
  52. s.send(data.Pack())
  53. }
  54. func (s *Screen) send(data []byte) {
  55. if !s.IsLive {
  56. return
  57. }
  58. _, err := s.Conn.Write(data)
  59. if err != nil {
  60. logrus.WithFields(map[string]interface{}{"设备名": s.Name}).Error("tcp write error:", err)
  61. s.IsLive = false
  62. }
  63. }
  64. type Color byte
  65. const (
  66. Default Color = iota
  67. Red
  68. Green
  69. Yellow
  70. Blue
  71. LightBlue
  72. LightPurple
  73. White
  74. None
  75. )
  76. // TextRam dispMode显示方式
  77. // 0x01——静止显示
  78. // 0x02——快速打出
  79. // 0x03——向左移动
  80. // 0x04——向右移动
  81. // 0x05——向上移动
  82. // 0x06——向下移动
  83. func (s *Screen) TextRam(ff FlashFile) {
  84. if !s.IsLive {
  85. return
  86. }
  87. var areas []bx.BxArea
  88. encoder := simplifiedchinese.GB18030.NewEncoder()
  89. var bytes []byte
  90. if ff.color == None || ff.color == Default {
  91. bytes, _ = encoder.Bytes([]byte(ff.msg))
  92. } else {
  93. bytes, _ = encoder.Bytes([]byte("\\C" + strconv.Itoa(int(ff.color)) + ff.msg))
  94. }
  95. area := bx.NewBxAreaDynamic(s.StateInfo.DynaAreaNum, 0, byte(ff.dispMode), ff.originX, ff.originY, ff.width, ff.height, bytes, false)
  96. areas = append(areas, area)
  97. //
  98. cmd := bx.NewBxCmdSendDynamicArea(areas)
  99. pack := bx.NewBxDataPackCmd(cmd)
  100. pack.SetDispType(1) //动态显示模式
  101. d := pack.Pack()
  102. s.send(d)
  103. s.ReadResp()
  104. s.StateInfo.DynaAreaNum++
  105. }
  106. // DelRamText 删除动态区,不传删除所有
  107. func (s *Screen) DelRamText(numbers ...byte) {
  108. if !s.IsLive {
  109. return
  110. }
  111. cmd := bx.NewCmdDelDynamicArea(numbers)
  112. pack := bx.NewBxDataPackCmd(cmd)
  113. s.send(pack.Pack())
  114. s.ReadResp()
  115. if len(numbers) == 0 {
  116. s.StateInfo.DynaAreaNum = 0
  117. } else {
  118. s.StateInfo.DynaAreaNum--
  119. }
  120. }
  121. type RunMode byte
  122. const (
  123. Loop RunMode = iota //循环
  124. LoopAndStayAtEnd //循环直到最后,停留在最后一个动态区
  125. LoopAndTimeoutOff //循环直到超时,超时后未更新不在显示
  126. LoopAndStayAtLogo //循环完后,停留显示LOGO
  127. LoopAndOff //循环完后不在显示
  128. LoopAndCountOff //循环设定次数后不在显示
  129. DefaultRunMode RunMode = Loop //默认
  130. )
  131. type DispMode byte
  132. const (
  133. _ DispMode = iota
  134. Static //0x01——静止显示
  135. QuickPunch //0x02——快速打出
  136. MoveLeft //0x03——向左移动
  137. MoveRight //0x04——向右移动
  138. MoveUp //0x05——向上移动
  139. MoveDown //0x06——向下移动
  140. DefaultDispMode DispMode = Static
  141. )
  142. type FlashFile struct {
  143. msg string
  144. color Color
  145. runMode RunMode
  146. dispMode DispMode
  147. originX uint16
  148. originY uint16
  149. width uint16
  150. height uint16
  151. }
  152. func (ft *FlashFile) SetMsg(msg string, color Color) {
  153. ft.msg = msg
  154. ft.color = color
  155. }
  156. func (ft *FlashFile) SetMode(runMode RunMode, dispMode DispMode) {
  157. ft.runMode = runMode
  158. ft.dispMode = dispMode
  159. }
  160. // SetOrigin xIsPixel=true表示x坐标为像素单位, =false表示以字节(8像素)为单位;y只有像素单位
  161. func (ft *FlashFile) SetOrigin(x uint16, xIsPixel bool, y uint16) {
  162. ft.originY = y
  163. if xIsPixel {
  164. ft.originX = 0x8000 | x
  165. return
  166. }
  167. ft.originX = x
  168. }
  169. // SetArea yIsPixel=true表示像素单位, =false表示以字节(8像素)为单位
  170. func (ft *FlashFile) SetArea(w uint16, yIsPixel bool, h uint16) {
  171. ft.height = h
  172. if yIsPixel {
  173. ft.width = 0x8000 | w
  174. return
  175. }
  176. ft.width = w
  177. }
  178. func (s *Screen) TextFlash(ft FlashFile, isLogo bool) {
  179. if !s.IsLive {
  180. return
  181. }
  182. encoder := simplifiedchinese.GB18030.NewEncoder()
  183. var bytes []byte
  184. if ft.color == None || ft.color == Default {
  185. bytes, _ = encoder.Bytes([]byte(ft.msg))
  186. } else {
  187. bytes, _ = encoder.Bytes([]byte("\\C" + strconv.Itoa(int(ft.color)) + ft.msg))
  188. }
  189. area := bx.NewBxAreaDynamic(0xff, byte(ft.runMode), byte(ft.dispMode), ft.originX, ft.originY, ft.width, ft.height, bytes, false)
  190. //if s.FileNum
  191. name := fmt.Sprintf("P%03d", s.StateInfo.ProgramNum)
  192. if isLogo {
  193. name = "LOGO"
  194. }
  195. file := bx.NewBxFile(name, "", []bx.BxArea{area})
  196. cmd := file.NewCmdWriteFile()
  197. pack := bx.NewBxDataPackCmd(cmd)
  198. data := pack.Pack()
  199. s.send(data)
  200. resp := s.ReadResp()
  201. if !resp.IsAck() {
  202. logrus.Error("设备拒绝写文件! error:", resp.Error().Description)
  203. return
  204. }
  205. pack1 := bx.NewBxDataPackCmd(cmd)
  206. data1 := pack1.Pack()
  207. s.send(data1)
  208. resp1 := s.ReadResp()
  209. if resp1.NoError() {
  210. s.StateInfo.ProgramNum++
  211. }
  212. }
  213. func (s *Screen) Bitmap(name string, bitmap []byte) {
  214. file := bx.NewBitmapFile(name, bitmap)
  215. cmd := file.NewCmd()
  216. pack := bx.NewBxDataPackCmd(cmd)
  217. data := pack.Pack()
  218. fmt.Println("数据长度:", len(data))
  219. fmt.Printf("数据:% 02x\n", data)
  220. s.send(data)
  221. resp := s.ReadResp()
  222. if !resp.IsAck() {
  223. logrus.Error("设备拒绝写文件! error:", resp.Error().Description)
  224. return
  225. }
  226. pack1 := bx.NewBxDataPackCmd(cmd)
  227. data1 := pack1.Pack()
  228. fmt.Println("数据长度1:", len(data1))
  229. s.send(data1)
  230. fmt.Printf("写图文件数据:% 02x\n", data1)
  231. s.ReadResp()
  232. }
  233. // Lock 锁定状态:0x00——解锁状态,0x01——锁定状态
  234. func (s *Screen) Lock(flag byte, name string) {
  235. cmd := bx.NewCmdLock(flag, name)
  236. pack := bx.NewBxDataPackCmd(&cmd)
  237. s.send(pack.Pack())
  238. }
  239. func (s *Screen) QueryFile() {
  240. }
  241. func (s *Screen) DelFile(delFiles ...string) {
  242. cmd := bx.NewCmdDeleteFile(delFiles)
  243. pack := bx.NewBxDataPackCmd(cmd)
  244. s.send(pack.Pack())
  245. s.ReadResp()
  246. if len(delFiles) == 0 {
  247. s.StateInfo.ProgramNum = 0
  248. } else {
  249. s.StateInfo.ProgramNum--
  250. }
  251. }
  252. func (s *Screen) DelText(delIds []byte) {
  253. cmd := bx.NewBxCmdSendDynamicArea(nil)
  254. cmd.SetDelAreaIds(delIds)
  255. pack := bx.NewBxDataPackCmd(cmd)
  256. s.send(pack.Pack())
  257. }
  258. func (s *Screen) TurnOnOff(onOff bool) {
  259. if !s.IsLive {
  260. return
  261. }
  262. cmd := bx.NewBxCmdTurnOnOff(onOff)
  263. pack := bx.NewBxDataPackCmd(cmd)
  264. s.send(pack.Pack())
  265. }
  266. // TimingSwitch 定时开关屏
  267. // 13:49 开, 13:55 关,最多设置3组
  268. //
  269. // onOffSet := [][2]uint64{
  270. // {1349, 1355},
  271. // }
  272. func (s *Screen) TimingSwitch(onOffSet [][2]uint64) {
  273. if !s.IsLive {
  274. return
  275. }
  276. cmd := bx.NewCmdTimingSwitch(onOffSet)
  277. pack := bx.NewBxDataPackCmd(cmd)
  278. s.send(pack.Pack())
  279. }
  280. func (s *Screen) CancelTimingSwitch() {
  281. if !s.IsLive {
  282. return
  283. }
  284. cmd := bx.NewCmdCancelTimingSwitch()
  285. pack := bx.NewBxDataPackCmd(cmd)
  286. s.send(pack.Pack())
  287. }
  288. func (s *Screen) State() *bx.BxResp {
  289. if !s.IsLive {
  290. return nil
  291. }
  292. cmd := bx.NewCmdState()
  293. pack := bx.NewBxDataPackCmd(cmd)
  294. s.send(pack.Pack())
  295. r := s.ReadResp()
  296. return &r
  297. }
  298. func (s *Screen) Param() *bx.BxResp {
  299. if !s.IsLive {
  300. return nil
  301. }
  302. cmd := bx.NewCmdReadParams()
  303. pack := bx.NewBxDataPackCmd(cmd)
  304. s.send(pack.Pack())
  305. r := s.ReadResp()
  306. return &r
  307. }
  308. func (s *Screen) Info() {
  309. s.StateInfo.Print(s.Name)
  310. }
  311. func (s *Screen) ReadResp() bx.BxResp {
  312. var resp = make([]byte, 1024)
  313. read, err := s.Conn.Read(resp)
  314. if err != nil {
  315. logrus.Error("读数据错误:", err)
  316. s.IsLive = false
  317. return bx.BxResp{}
  318. }
  319. var bxResp = bx.BxResp{}
  320. parse := bxResp.Parse(resp, read)
  321. if parse.IsAck() {
  322. fmt.Println("response ACK")
  323. fmt.Printf("原始响应数据:% 0x\n", resp[:read])
  324. fmt.Println("解析响应数据:", parse)
  325. } else if parse.IsInfo() {
  326. fmt.Println("state ACK")
  327. fmt.Printf("原始响应数据:% 0x\n", resp[:read])
  328. fmt.Println("解析响应数据:", parse)
  329. } else {
  330. fmt.Println("response")
  331. fmt.Printf("原始响应数据:% 0x\n", resp[:read])
  332. fmt.Println("解析响应数据:", parse)
  333. }
  334. return *parse
  335. }