devmgr.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package service
  2. import (
  3. "smart_tunnel_edge/protocol/envSensor"
  4. "smart_tunnel_edge/protocol/lampControl"
  5. "smart_tunnel_edge/protocol/switchRelay"
  6. "smart_tunnel_edge/util/config"
  7. "smart_tunnel_edge/util/logger"
  8. "sync"
  9. "time"
  10. )
  11. var mu sync.Mutex
  12. const (
  13. RegulateLight = "regulateLight"
  14. RegulateSwitch = "regulateSwitch"
  15. LowLightLevel = 30
  16. HighLightLevel = 60
  17. )
  18. // 初始化时单灯或者开关继电器
  19. func InitControl() {
  20. if config.Instance().Policy.Control == RegulateLight {
  21. go SetLampBright(1, LowLightLevel)
  22. go SetLampBright(2, LowLightLevel)
  23. } else {
  24. go SetSwitchRelay(1, 1, 1)
  25. go SetSwitchRelay(2, 1, 1)
  26. }
  27. }
  28. func StartDeviceCollection(dev *config.EnvDev) {
  29. coverTime := dev.TaskTime
  30. ticker := time.NewTicker(time.Duration(coverTime) * time.Minute)
  31. defer ticker.Stop()
  32. for {
  33. select {
  34. case <-ticker.C:
  35. if dev.WaitTime > 0 {
  36. time.Sleep(time.Duration(dev.WaitTime) * time.Second)
  37. }
  38. //获取指令
  39. dataPack := envSensor.DataPack{Address: dev.Address, Start: []byte{0x01, 0xF8}, DataLen: []byte{0x00, 0x08}}
  40. if dev.Name == "光传感器" {
  41. dataPack = envSensor.DataPack{Address: dev.Address, Start: []byte{0x00, 0x06}, DataLen: []byte{0x00, 0x01}}
  42. }
  43. buffer := dataPack.GetEnvGatherCommand()
  44. mu.Lock()
  45. port, err := GetSerialPort(dev.SerialId)
  46. if err != nil {
  47. logger.Logger.Infof(err.Error())
  48. continue
  49. }
  50. write, err := port.Write(buffer.Bytes())
  51. mu.Unlock()
  52. if write < 0 || err != nil {
  53. logger.Logger.Errorf("设备:%v采集数据失败,错误:%v", dev.Sn, err)
  54. continue
  55. }
  56. }
  57. }
  58. }
  59. func OperationLampSwitchJudge(radarId int8, brightness, way, turnOf int) {
  60. if config.Instance().Policy.Control == RegulateLight {
  61. SetLampBright(radarId, brightness)
  62. } else {
  63. SetSwitchRelay(radarId, way, turnOf)
  64. }
  65. }
  66. // 单灯控制器调光方法
  67. func SetLampBright(radarId int8, brightness int) {
  68. var wg sync.WaitGroup
  69. for _, dev := range config.DevConfig.LampDevs {
  70. if dev.RadarId != radarId {
  71. continue
  72. }
  73. wg.Add(1)
  74. newDev := dev
  75. go func() {
  76. defer wg.Done()
  77. // 获取对应的串口
  78. port, err := GetSerialPort(newDev.SerialId)
  79. if err != nil {
  80. logger.Logger.Infof("获取串口失败,设备编号:%v,错误:%v", newDev.Sn, err)
  81. return
  82. }
  83. buffer := lampControl.DataPack{Addr: newDev.Address}.GetLampSetBrightCommand(brightness)
  84. mu.Lock() // 锁定串口操作
  85. write, err := port.Write(buffer.Bytes())
  86. mu.Unlock()
  87. if write < 0 || err != nil {
  88. logger.Logger.Errorf("单灯:%v操作失败,错误:%v", newDev.Sn, err)
  89. return
  90. }
  91. }()
  92. time.Sleep(time.Millisecond * 150)
  93. }
  94. wg.Wait()
  95. }
  96. // 开关继电器开关方法(单路)
  97. func SetSwitchRelay(radarId int8, way, turnOf int) {
  98. for _, dev := range config.DevConfig.SwitchDevs {
  99. if dev.RadarId != radarId {
  100. continue
  101. }
  102. port, err := GetSerialPort(dev.SerialId)
  103. if err != nil {
  104. logger.Logger.Infof("获取串口失败,设备编号:%v,错误:%v", dev.Sn, err)
  105. return
  106. }
  107. command := switchRelay.DataPack{Address: dev.Address, Way: way, Command: turnOf}.GetSwitchRelayCommand()
  108. mu.Lock() // 锁定串口操作
  109. write, err := port.Write(command.Bytes())
  110. mu.Unlock() // 锁定串口操作
  111. if write < 0 || err != nil {
  112. logger.Logger.Errorf("控制单路开关继电器:%v操作失败,错误:%v", dev.Sn, err)
  113. continue
  114. }
  115. time.Sleep(time.Millisecond * 150)
  116. }
  117. }
  118. // 开关继电器开关方法(多路)
  119. func SetSwitchMultiRelay(radarId int8, turnOf int, ways []int) {
  120. for _, dev := range config.DevConfig.SwitchDevs {
  121. if dev.RadarId != radarId {
  122. continue
  123. }
  124. port, err := GetSerialPort(dev.SerialId)
  125. if err != nil {
  126. logger.Logger.Infof("获取串口失败,设备编号:%v,错误:%v", dev.Sn, err)
  127. return
  128. }
  129. command := switchRelay.MultiLoopDataPack{Address: dev.Address, TurnOff: turnOf, State: ways}.GetControlMuchSwitchRelayCommand()
  130. mu.Lock() // 锁定串口操作
  131. write, err := port.Write(command.Bytes())
  132. mu.Unlock() // 锁定串口操作
  133. if write < 0 || err != nil {
  134. logger.Logger.Errorf("控制多路开关继电器:%v操作失败,错误:%v", dev.Sn, err)
  135. continue
  136. }
  137. time.Sleep(time.Millisecond * 150)
  138. }
  139. }