|
@@ -2,10 +2,12 @@ package util
|
|
|
|
|
|
import (
|
|
|
"encoding/hex"
|
|
|
+ "encoding/json"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"github.com/tarm/serial"
|
|
|
"log"
|
|
|
+ "smart_tunnel_edge/mqtt"
|
|
|
"smart_tunnel_edge/util/config"
|
|
|
"smart_tunnel_edge/util/logger"
|
|
|
"strconv"
|
|
@@ -32,11 +34,6 @@ var (
|
|
|
lightDuration = 2 * time.Minute // 灯亮的持续时间(2分钟)
|
|
|
)
|
|
|
|
|
|
-const (
|
|
|
- RegulateLight = "regulateLight"
|
|
|
- RegulateSwitch = "regulateSwitch"
|
|
|
-)
|
|
|
-
|
|
|
func GetSerialPort(serialID int8) (*serial.Port, error) {
|
|
|
port, exists := serialPort[serialID]
|
|
|
if !exists {
|
|
@@ -96,52 +93,80 @@ func listenPort(portCode int8, port *serial.Port) {
|
|
|
shiDu, _ := strconv.ParseInt(dataString[6:10], 16, 0)
|
|
|
wenDu, _ := strconv.ParseInt(dataString[10:14], 16, 0)
|
|
|
gz, _ := strconv.ParseInt(dataString[30:38], 16, 0)
|
|
|
- fmt.Printf("湿度 :%v %%RH", float64(shiDu)/10.0)
|
|
|
- fmt.Printf("温度 :%v ℃", float64(wenDu)/10.0)
|
|
|
- fmt.Printf("光照度 :%v Lux \n", gz)
|
|
|
-
|
|
|
if _, exists := env[dataString[0:2]]; !exists {
|
|
|
env[dataString[0:2]] = gz
|
|
|
}
|
|
|
|
|
|
- //TODO 将采集数据上报
|
|
|
+ data := EnvData{Temperature: float64(wenDu) / 10.0, Humidity: float64(shiDu) / 10.0, Illuminance: gz}
|
|
|
+ for _, dev := range config.DevConfig.EnvDevs {
|
|
|
+ if dev.Address == dataString[0:2] {
|
|
|
+ data.Sn = dev.Sn
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ jsonData, _ := json.Marshal(data)
|
|
|
+ topic := mqtt.MqttService.GetTopic(mqtt.TopicGatherDataEnv)
|
|
|
+ err := mqtt.MqttService.Publish(topic, jsonData)
|
|
|
+ if err != nil {
|
|
|
+ logger.Logger.Errorf("MQTT Publish err = %s", err.Error())
|
|
|
+ continue
|
|
|
+ }
|
|
|
|
|
|
case len(dataString) == 14 && dataString[2:6] == "0302": //光照传感器数据
|
|
|
fmt.Printf("光传感:")
|
|
|
gz, _ := strconv.ParseInt(dataString[6:10], 16, 0)
|
|
|
- fmt.Printf("光照度 :%v Lux \n", gz)
|
|
|
|
|
|
if _, exists := env[dataString[0:2]]; !exists {
|
|
|
opt[dataString[0:2]] = gz
|
|
|
}
|
|
|
|
|
|
- //TODO 将采集数据上报
|
|
|
+ data := OpticalData{Illuminance: gz}
|
|
|
+ for _, dev := range config.DevConfig.EnvDevs {
|
|
|
+ if dev.Address == dataString[0:2] {
|
|
|
+ data.Sn = dev.Sn
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ jsonData, _ := json.Marshal(data)
|
|
|
+ topic := mqtt.MqttService.GetTopic(mqtt.TopicGatherDataOpt)
|
|
|
+ err := mqtt.MqttService.Publish(topic, jsonData)
|
|
|
+ if err != nil {
|
|
|
+ logger.Logger.Errorf("MQTT Publish err = %s", err.Error())
|
|
|
+ continue
|
|
|
+ }
|
|
|
|
|
|
case len(dataString) == 16 && dataString[0:2] == "78" && dataString[8:16] == "40790d0a": //雷达数据
|
|
|
dataBytes, _ := hex.DecodeString(dataString)
|
|
|
speed, _ := strconv.Atoi(string(dataBytes)[1:4])
|
|
|
- if speed > 5 && config.Instance().Policy.Id == 2 { //速度大于5且策略为根据来车调光(2代表根据来车改变光照)
|
|
|
- now := time.Now()
|
|
|
- if config.Instance().Policy.Control == RegulateLight { //判断操作策略
|
|
|
- //调光
|
|
|
- if isLightOperate { //如果已经操作
|
|
|
- if now.Sub(lastCarTime) <= lightDuration { // 并且两分钟内再来车,延长计时
|
|
|
- timer.Reset(lightDuration)
|
|
|
- }
|
|
|
- } else { //没操作,就去操作
|
|
|
-
|
|
|
+ // 配置实例
|
|
|
+ policy := config.Instance().Policy
|
|
|
+ if speed > 5 && policy.Id == 2 { // 速度大于5且策略为根据来车调光(2代表根据来车改变光照)
|
|
|
+ now := time.Now() // 记录来车时间
|
|
|
+ // 如果已经操作过灯
|
|
|
+ if isLightOperate {
|
|
|
+ // 如果在两分钟内再来车,延长计时
|
|
|
+ if now.Sub(lastCarTime) <= lightDuration {
|
|
|
+ timer.Reset(lightDuration)
|
|
|
}
|
|
|
} else {
|
|
|
- //开关模组
|
|
|
+ OperationLampSwitchJudge(portCode, 100)
|
|
|
+ isLightOperate = true
|
|
|
+ timer = time.AfterFunc(lightDuration, func() {
|
|
|
+ OperationLampSwitchJudge(portCode, 20)
|
|
|
+ isLightOperate = false
|
|
|
+ })
|
|
|
}
|
|
|
- lastCarTime = now //更新最后来车时间
|
|
|
+ // 更新最后来车时间
|
|
|
+ lastCarTime = now
|
|
|
}
|
|
|
continue
|
|
|
//default:
|
|
|
// // 其他情况可以处理默认数据
|
|
|
// fmt.Printf("错误数据: %s \n", dataString)
|
|
|
}
|
|
|
- if config.Instance().Nums.EnvNum == len(env) && config.Instance().Nums.OptNum == len(opt) {
|
|
|
+ if config.Instance().Policy.Id == 1 && config.Instance().Nums.EnvNum == len(env) && config.Instance().Nums.OptNum == len(opt) {
|
|
|
|
|
|
envAverage := calculateAverage(env)
|
|
|
optAverage := calculateAverage(opt)
|
|
@@ -150,6 +175,11 @@ func listenPort(portCode int8, port *serial.Port) {
|
|
|
fmt.Println("光照平均值:", optAverage)
|
|
|
|
|
|
//TODO 作比较,进行灯控操作并清空切片
|
|
|
+ if envAverage-6 >= 0 {
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
env = make(map[string]int64)
|
|
|
opt = make(map[string]int64)
|