package service import ( "smart_tunnel_edge/protocol/envSensor" "smart_tunnel_edge/protocol/lampControl" "smart_tunnel_edge/protocol/switchRelay" "smart_tunnel_edge/util/config" "smart_tunnel_edge/util/logger" "sync" "time" ) var mu sync.Mutex const ( RegulateLight = "regulateLight" RegulateSwitch = "regulateSwitch" LowLightLevel = 30 HighLightLevel = 60 ) // 初始化时单灯或者开关继电器 func InitControl() { if config.Instance().Policy.Control == RegulateLight { go SetLampBright(1, LowLightLevel) go SetLampBright(2, LowLightLevel) } else { go SetSwitchRelay(1, 1, 1) go SetSwitchRelay(2, 1, 1) } } func StartDeviceCollection(dev *config.EnvDev) { coverTime := dev.TaskTime ticker := time.NewTicker(time.Duration(coverTime) * time.Minute) defer ticker.Stop() for { select { case <-ticker.C: if dev.WaitTime > 0 { time.Sleep(time.Duration(dev.WaitTime) * time.Second) } //获取指令 dataPack := envSensor.DataPack{Address: dev.Address, Start: []byte{0x01, 0xF8}, DataLen: []byte{0x00, 0x08}} if dev.Name == "光传感器" { dataPack = envSensor.DataPack{Address: dev.Address, Start: []byte{0x00, 0x06}, DataLen: []byte{0x00, 0x01}} } buffer := dataPack.GetEnvGatherCommand() mu.Lock() port, err := GetSerialPort(dev.SerialId) if err != nil { logger.Logger.Infof(err.Error()) continue } write, err := port.Write(buffer.Bytes()) mu.Unlock() if write < 0 || err != nil { logger.Logger.Errorf("设备:%v采集数据失败,错误:%v", dev.Sn, err) continue } } } } func OperationLampSwitchJudge(radarId int8, brightness, way, turnOf int) { if config.Instance().Policy.Control == RegulateLight { SetLampBright(radarId, brightness) } else { SetSwitchRelay(radarId, way, turnOf) } } // 单灯控制器调光方法 func SetLampBright(radarId int8, brightness int) { var wg sync.WaitGroup for _, dev := range config.DevConfig.LampDevs { if dev.RadarId != radarId { continue } wg.Add(1) newDev := dev go func() { defer wg.Done() // 获取对应的串口 port, err := GetSerialPort(newDev.SerialId) if err != nil { logger.Logger.Infof("获取串口失败,设备编号:%v,错误:%v", newDev.Sn, err) return } buffer := lampControl.DataPack{Addr: newDev.Address}.GetLampSetBrightCommand(brightness) mu.Lock() // 锁定串口操作 write, err := port.Write(buffer.Bytes()) mu.Unlock() if write < 0 || err != nil { logger.Logger.Errorf("单灯:%v操作失败,错误:%v", newDev.Sn, err) return } }() time.Sleep(time.Millisecond * 150) } wg.Wait() } // 开关继电器开关方法(单路) func SetSwitchRelay(radarId int8, way, turnOf int) { for _, dev := range config.DevConfig.SwitchDevs { if dev.RadarId != radarId { continue } port, err := GetSerialPort(dev.SerialId) if err != nil { logger.Logger.Infof("获取串口失败,设备编号:%v,错误:%v", dev.Sn, err) return } command := switchRelay.DataPack{Address: dev.Address, Way: way, Command: turnOf}.GetSwitchRelayCommand() mu.Lock() // 锁定串口操作 write, err := port.Write(command.Bytes()) mu.Unlock() // 锁定串口操作 if write < 0 || err != nil { logger.Logger.Errorf("控制单路开关继电器:%v操作失败,错误:%v", dev.Sn, err) continue } time.Sleep(time.Millisecond * 150) } } // 开关继电器开关方法(多路) func SetSwitchMultiRelay(radarId int8, turnOf int, ways []int) { for _, dev := range config.DevConfig.SwitchDevs { if dev.RadarId != radarId { continue } port, err := GetSerialPort(dev.SerialId) if err != nil { logger.Logger.Infof("获取串口失败,设备编号:%v,错误:%v", dev.Sn, err) return } command := switchRelay.MultiLoopDataPack{Address: dev.Address, TurnOff: turnOf, State: ways}.GetControlMuchSwitchRelayCommand() mu.Lock() // 锁定串口操作 write, err := port.Write(command.Bytes()) mu.Unlock() // 锁定串口操作 if write < 0 || err != nil { logger.Logger.Errorf("控制多路开关继电器:%v操作失败,错误:%v", dev.Sn, err) continue } time.Sleep(time.Millisecond * 150) } }