package service import ( "net" "server/logger" "server/modbus" "server/model" "server/utils" "time" ) type Cron struct { } // RelayOnOffTimeTask 定时开关 前提是 设备在线 否则失效 func (c Cron) RelayOnOffTimeTask() { current := time.Now().Format("15:04") devices, err := utils.GetOnlineDevices() if err != nil { logger.Get().Errorf("LoadData err = %s", err.Error()) return } relayMap := make(map[string]map[int]int) for _, d := range devices { //if d.State == 0 { // continue //} tmp, isExist := relayMap[d.Sn] if !isExist { // 回路id 回路状态 tmp = make(map[int]int) } relays := d.DeviceLoops rcTime, rlTime, err := utils.SunriseSunsetForChina(28.23, 113.05) if err != nil { logger.Get().Errorf("SunriseSunsetForChina err = %s", err.Error()) rcTime = "06:00" rlTime = "18:00" } for _, r := range relays { //日出日落时间 if r.TimeCondition1OnTime == "日出" { r.TimeCondition1OnTime = rcTime } else if r.TimeCondition1OnTime == "日落" { r.TimeCondition1OnTime = rlTime } if r.TimeCondition1OffTime == "日出" { r.TimeCondition1OffTime = rcTime } else if r.TimeCondition1OffTime == "日落" { r.TimeCondition1OffTime = rlTime } if r.TimeCondition2OnTime == "日出" { r.TimeCondition2OnTime = rcTime } else if r.TimeCondition2OnTime == "日落" { r.TimeCondition2OnTime = rlTime } if r.TimeCondition2OffTime == "日出" { r.TimeCondition2OffTime = rcTime } else if r.TimeCondition2OffTime == "日落" { r.TimeCondition2OffTime = rlTime } isTiming := 0 // 用来判断该设备的回路 是否定时 //控制回路 时间判断 是否关闭 当前时间是否等与规定时间 规定时间到规定时间+10分钟 //因为1分钟执行一次,所以会多次执行,则新加判断 如果数据库中状态和回路状态相同则不执行下去 layout := "15:04" rTimeCondition1OnTime, _ := time.Parse(layout, r.TimeCondition1OnTime) rTimeCondition1OffTime, _ := time.Parse(layout, r.TimeCondition1OffTime) rTimeCondition2OnTime, _ := time.Parse(layout, r.TimeCondition2OnTime) rTimeCondition2OffTime, _ := time.Parse(layout, r.TimeCondition2OffTime) rCurrent, _ := time.Parse(layout, current) if r.TimeCondition1OnTime != "关闭" && (r.TimeCondition1OnTime == current || (rCurrent.Before(rTimeCondition1OnTime.Add(11*time.Minute)) && rCurrent.After(rTimeCondition1OnTime))) { tmp[r.ID] = 1 isTiming = 1 } if r.TimeCondition1OffTime != "关闭" && (r.TimeCondition1OffTime == current || (rCurrent.Before(rTimeCondition1OffTime.Add(11*time.Minute)) && rCurrent.After(rTimeCondition1OffTime))) { tmp[r.ID] = 0 isTiming = 1 } if r.TimeCondition2OnTime != "关闭" && (r.TimeCondition2OnTime == current || (rCurrent.Before(rTimeCondition2OnTime.Add(11*time.Minute)) && rCurrent.After(rTimeCondition2OnTime))) { tmp[r.ID] = 1 isTiming = 1 } if r.TimeCondition2OffTime != "关闭" && (r.TimeCondition2OffTime == current || (rCurrent.Before(rTimeCondition2OffTime.Add(11*time.Minute)) && rCurrent.After(rTimeCondition2OffTime))) { tmp[r.ID] = 0 isTiming = 1 } if isTiming == 1 { relayMap[d.Sn] = tmp } } } for key, value := range relayMap { reg, dev, err := utils.GetDataByDeviceId(key) if err != nil { logger.Get().Errorf("GetDataByDeviceId err = %s\n", err.Error()) } for i, i2 := range value { data := modbus.DeviceLoopSwitch(i, i2) if conn1, ok := model.ConnectionMap1.Load(key); ok { // 成功找到连接 netConn := conn1.(net.Conn) err := utils.WriteDevice(data, netConn) time.Sleep(100 * time.Millisecond) if err != nil { logger.Get().Errorf("WriteDevice err = %s\n", err.Error()) } for j, loop := range dev.DeviceLoops { if loop.ID == i { dev.DeviceLoops[j].State = i2 } } } else { // 没有找到对应的连接 logger.Get().Printf("Connection for key %s not found", key) } } for i, device := range reg.Devices { if device.Sn == dev.Sn { reg.Devices[i] = dev } } data, err := utils.SaveRegionOnData(reg) if err != nil { logger.Get().Errorf("SaveRegionOnData err = %s\n", err.Error()) } err = SaveData(data) if err != nil { logger.Get().Errorf("SaveData err = %s\n", err.Error()) } } }