led_device.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package main
  2. //screen操作相关
  3. import (
  4. "context"
  5. "encoding/json"
  6. "github.com/sirupsen/logrus"
  7. "lc/common/mqtt"
  8. "lc/common/protocol"
  9. "lc/common/util"
  10. "lc/edge/led_screen/clt_client"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. type LedDevice struct {
  16. LedInfo
  17. playing string //当前播放节目 没有用到
  18. ctx context.Context
  19. cancel context.CancelFunc
  20. downQueue *util.MlQueue
  21. //upQueue *util.MlQueue
  22. mapTopicHandle map[string]func(m mqtt.Message)
  23. }
  24. func (l *LedDevice) GetCltUrl(ctlurl string) string {
  25. return l.BaseURL + ctlurl
  26. }
  27. func NewLedDevice(info LedInfo) *LedDevice {
  28. ctx, cancel := context.WithCancel(context.Background())
  29. dev := LedDevice{
  30. info,
  31. "",
  32. ctx,
  33. cancel,
  34. util.NewQueue(100),
  35. //util.NewQueue(50),
  36. make(map[string]func(m mqtt.Message)),
  37. }
  38. dev.SetTopicHandle()
  39. dev.MQTTSubscribe()
  40. go dev.Handle()
  41. return &dev
  42. }
  43. func (l *LedDevice) SetTopicHandle() {
  44. l.mapTopicHandle[protocol.TP_LED_SET_PGMS] = l.PublishProgram //发布节目
  45. l.mapTopicHandle[protocol.TP_LED_QUERY_PGMS] = l.ListPrograms //查询节目
  46. l.mapTopicHandle[protocol.TP_LED_DELETE] = l.DeleteProgram //删除节目
  47. l.mapTopicHandle[protocol.TP_LED_CLEAN] = l.CleanAll //清除所有节目
  48. l.mapTopicHandle[protocol.TP_LED_QUERY_INFO] = l.QueryInfo //查询屏信息
  49. l.mapTopicHandle[protocol.TP_LED_SWITCH] = l.SwitchProgram //切换节目
  50. l.mapTopicHandle[protocol.TP_LED_SNAPSHOT] = l.SnapShot //快照
  51. l.mapTopicHandle[protocol.TP_LED_QUERY_SCHEDULE] = l.GetSchedule //获取排程
  52. l.mapTopicHandle[protocol.TP_LED_SET_SCHEDULE] = l.SetSchedule //发布排程
  53. l.mapTopicHandle[protocol.TP_LED_QUERY_POWERSTATUS] = l.PowerStatus //电源状态
  54. l.mapTopicHandle[protocol.TP_LED_SET_CMD] = l.ControlPower //控制电源
  55. l.mapTopicHandle[protocol.TP_LED_QUERY_RESOLUTION] = l.Resolution //屏幕参数
  56. l.mapTopicHandle[protocol.TP_LED_QUERY_VOLUME] = l.GetVolume //查询音量
  57. l.mapTopicHandle[protocol.TP_LED_SET_VOLUME] = l.SetVolume //设置音量
  58. l.mapTopicHandle[protocol.TP_LED_QUERY_BRIGHTCOLOR] = l.GetBrightColor //查询亮度和色温
  59. l.mapTopicHandle[protocol.TP_LED_SET_BRIGHTNESS] = l.SetBright //设置亮度和色温
  60. }
  61. func (l *LedDevice) MQTTSubscribe() {
  62. GetMQTTMgr().Subscribe(GetTopic(l.SN, "down"), mqtt.AtMostOnce, l.HandleMessage, ToCloud)
  63. }
  64. func (l *LedDevice) HandleMessage(m mqtt.Message) {
  65. l.downQueue.Put(m)
  66. }
  67. func (l *LedDevice) Handle() {
  68. //timer := time.NewTicker(1 * time.Minute)
  69. timer := time.NewTicker(10 * time.Second)
  70. for {
  71. select {
  72. case <-l.ctx.Done():
  73. logrus.Errorf("设备[%s]的LedDevice.Handle退出,原因:%v", l.LedInfo.SN, l.ctx.Err())
  74. return
  75. case <-timer.C: //心跳,检查在线状态, 每隔1分钟执行一次
  76. url := l.GetCltUrl(clt_client.POWERS_TATUS)
  77. _, err := clt_client.GetClient().CommonGet(url)
  78. if err != nil {
  79. l.CheckOnline(l, uint8(1))
  80. logrus.Errorf(" error: %v", err)
  81. continue
  82. }
  83. l.CheckOnline(l, uint8(0))
  84. l.LedData(l)
  85. default:
  86. if m, ok, _ := l.downQueue.Get(); ok {
  87. if mm, ok := m.(mqtt.Message); ok {
  88. action := strings.Split(mm.Topic(), "/")[4]
  89. if fn, ok := l.mapTopicHandle[action]; ok {
  90. fn(mm)
  91. } else {
  92. logrus.Errorf("LcDevice.Handle:不支持的主题:%s", mm.Topic())
  93. }
  94. }
  95. } else {
  96. time.Sleep(50 * time.Millisecond)
  97. }
  98. }
  99. }
  100. }
  101. func (o *LedDevice) CheckOnline(dev *LedDevice, online uint8) {
  102. var obj protocol.Pack_LedState
  103. if str, err := obj.EnCode(dev.SN, appConfig.GID, GetNextUint64(), util.MlNow(), online); err == nil {
  104. topic := appConfig.Tenant + "/cltled/" + dev.SN + "/up/" + protocol.TP_LED_STATE + "/" + strconv.Itoa(int(GetNextUint64()))
  105. //fmt.Printf("topic = %v str=%v \n", topic, str)
  106. GetMQTTMgr().Publish(topic, []byte(str), mqtt.AtMostOnce, ToCloud)
  107. }
  108. }
  109. // LedData 上报信息屏详细信息
  110. func (o *LedDevice) LedData(dev *LedDevice) {
  111. var data protocol.CltledData
  112. //电源状态
  113. res, _ := clt_client.GetClient().CommonGet(o.GetCltUrl(clt_client.POWERS_TATUS))
  114. json.Unmarshal(res, &data)
  115. //音量
  116. res, _ = clt_client.GetClient().CommonGet(o.GetCltUrl(clt_client.GET_VOLUME))
  117. json.Unmarshal(res, &data)
  118. //亮度色温
  119. res, _ = clt_client.GetClient().CommonGet(o.GetCltUrl(clt_client.GET_BRIGHTCOLOR))
  120. json.Unmarshal(res, &data)
  121. //开关机时间
  122. res, _ = clt_client.GetClient().CommonGet(o.GetCltUrl(clt_client.GET_SCHEDULE))
  123. json.Unmarshal(res, &data)
  124. //分辨率
  125. res, _ = clt_client.GetClient().CommonGet(o.GetCltUrl(clt_client.RESOLUTION))
  126. json.Unmarshal(res, &data)
  127. //当前节目
  128. res, _ = clt_client.GetClient().CommonGet(o.GetCltUrl(clt_client.LIST_PROGRAMS))
  129. resMap := make(map[string]map[string]interface{})
  130. json.Unmarshal(res, &resMap)
  131. payingName, ok := resMap["playing"]["name"]
  132. if ok {
  133. if s, ok := payingName.(string); ok {
  134. data.Playing = s
  135. }
  136. }
  137. var obj protocol.Pack_LedCltledData
  138. if str, err := obj.EnCode(dev.SN, appConfig.GID, GetNextUint64(), &data); err == nil {
  139. topic := appConfig.Tenant + "/cltled/" + dev.SN + "/up/" + protocol.TP_LED_DATA + "/" + strconv.Itoa(int(GetNextUint64()))
  140. GetMQTTMgr().Publish(topic, []byte(str), mqtt.AtMostOnce, ToCloud)
  141. }
  142. }
  143. func GetNextUint64() uint64 {
  144. u64, _ := IDGen.NextId()
  145. return uint64(u64)
  146. }
  147. //===============POST=====
  148. // PublishProgram 平台发布节目
  149. func (l *LedDevice) PublishProgram(m mqtt.Message) {
  150. clt_client.GetClient().PublishProgram(m, l.BaseURL)
  151. //获取结果 并 响应云端
  152. l.ListPrograms(m)
  153. //发布成功
  154. //if resp.Playing.Name == pro.Name+".vsn" {
  155. // l.playing = resp.Playing.Name
  156. // logrus.Infof("%s发布成功!耗时:%v", pro.Name, (later - now))
  157. //} else {
  158. // logrus.Infof("%s发布失败!耗时:%v", pro.Name, (later - now))
  159. //}
  160. }
  161. func (l *LedDevice) SetSchedule(m mqtt.Message) {
  162. url := l.GetCltUrl(clt_client.SET_SCHEDULE)
  163. l.DoCommonPost(url, "func SetSchedule", m)
  164. l.GetSchedule(m)
  165. }
  166. // ControlPower 控制电源
  167. func (l *LedDevice) ControlPower(m mqtt.Message) {
  168. url := l.GetCltUrl(clt_client.CONTROL_POWER)
  169. l.DoCommonPost(url, "func ControlPower", m)
  170. l.PowerStatus(m)
  171. }
  172. func (l *LedDevice) DoCommonPost(url, funcname string, m mqtt.Message) {
  173. err := clt_client.GetClient().CommonPost(m.Payload(), url)
  174. if err != nil {
  175. logrus.Errorf(funcname+" error: %v", err)
  176. }
  177. }
  178. //==========================GET==================================GET
  179. // ListPrograms 查询所有节目以及当前播放的节目
  180. func (l *LedDevice) ListPrograms(m mqtt.Message) {
  181. url := l.GetCltUrl(clt_client.LIST_PROGRAMS)
  182. l.DoCommonGet(url, "func ListPrograms", m)
  183. }
  184. // QueryInfo 查询屏通用信息
  185. func (l *LedDevice) QueryInfo(m mqtt.Message) {
  186. url := l.GetCltUrl(clt_client.QUERY_INFO)
  187. l.DoCommonGet(url, "func QueryInfo", m)
  188. }
  189. // PowerStatus 获取电源状态
  190. func (l *LedDevice) PowerStatus(m mqtt.Message) {
  191. url := l.GetCltUrl(clt_client.POWERS_TATUS)
  192. l.DoCommonGet(url, "get power status", m)
  193. }
  194. // Resolution 查询屏幕参数
  195. func (l *LedDevice) Resolution(m mqtt.Message) {
  196. url := l.GetCltUrl(clt_client.RESOLUTION)
  197. l.DoCommonGet(url, "func Resolution", m)
  198. }
  199. // GetSchedule 查询电源排程
  200. func (l *LedDevice) GetSchedule(m mqtt.Message) {
  201. url := l.GetCltUrl(clt_client.GET_SCHEDULE)
  202. l.DoCommonGet(url, "func GetSchedule", m)
  203. }
  204. // GetBrightColor GetBrightnessColorTemperature 查询亮度和色温
  205. func (l *LedDevice) GetBrightColor(m mqtt.Message) {
  206. url := l.GetCltUrl(clt_client.GET_BRIGHTCOLOR)
  207. l.DoCommonGet(url, "func GetBrightnessColorTemperature", m)
  208. }
  209. // GetVolume 查询音量
  210. func (l *LedDevice) GetVolume(m mqtt.Message) {
  211. url := l.GetCltUrl(clt_client.GET_VOLUME)
  212. l.DoCommonGet(url, "func GetVolume", m)
  213. }
  214. // SnapShot 截屏
  215. func (l *LedDevice) SnapShot(m mqtt.Message) {
  216. url := l.GetCltUrl(clt_client.SNAPSHOT)
  217. shot := clt_client.GetClient().SnapShot(url)
  218. GetMQTTMgr().Publish(getUpTopic(m.Topic()), shot, mqtt.AtMostOnce, ToCloud)
  219. }
  220. func (l *LedDevice) DoCommonGet(url, funcname string, m mqtt.Message) {
  221. resp, err := clt_client.GetClient().CommonGet(url)
  222. if err != nil {
  223. logrus.Errorf(funcname+" error: %v", err)
  224. }
  225. GetMQTTMgr().Publish(getUpTopic(m.Topic()), resp, mqtt.AtMostOnce, ToCloud)
  226. }
  227. //================================PUT
  228. // SwitchProgram 切换节目
  229. func (l *LedDevice) SwitchProgram(m mqtt.Message) {
  230. clt_client.GetClient().SwitchProgram(m, l.BaseURL)
  231. l.SnapShot(m)
  232. }
  233. // SetBright 调节屏幕亮度
  234. func (l *LedDevice) SetBright(m mqtt.Message) {
  235. url := l.GetCltUrl(clt_client.SET_BRIGHT)
  236. err := clt_client.GetClient().CommonPut(m.Payload(), url)
  237. if err != nil {
  238. logrus.Errorf("set bright error: %v", err)
  239. }
  240. l.GetBrightColor(m)
  241. }
  242. // SetVolume 设置音量
  243. func (l *LedDevice) SetVolume(m mqtt.Message) {
  244. url := l.GetCltUrl(clt_client.SET_VOLUME)
  245. err := clt_client.GetClient().CommonPut(m.Payload(), url)
  246. if err != nil {
  247. logrus.Errorf("set volume error: %v", err)
  248. }
  249. l.GetVolume(m)
  250. }
  251. //=============DELETE=====
  252. // DeleteProgram 删除节目
  253. func (l *LedDevice) DeleteProgram(m mqtt.Message) {
  254. clt_client.GetClient().DelPrograms(m, l.BaseURL)
  255. //获取结果 并 响应云端
  256. l.ListPrograms(m)
  257. }
  258. // CleanAll 清除所有节目
  259. func (l *LedDevice) CleanAll(m mqtt.Message) {
  260. clt_client.GetClient().CleanAll(l.BaseURL)
  261. //获取结果 并 响应云端
  262. l.ListPrograms(m)
  263. }