package service import ( "fmt" "log" "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 { _ = fmt.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 if err != nil { fmt.Errorf(err.Error()) continue } rcTime, rlTime, err := utils.SunriseSunsetForChina(28.23, 113.05) if err != nil { log.Printf("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 { fmt.Printf("GetDataByDeviceId err = %s\n", err.Error()) } for i, i2 := range value { data := modbus.DeviceLoopSwitch(i, i2) if model.ConnectionMap[key] == nil { fmt.Errorf("设备连接丢失") } err := utils.WriteDevice(data, model.ConnectionMap[key]) time.Sleep(100 * time.Millisecond) if err != nil { fmt.Printf("WriteDevice err = %s\n", err.Error()) } for j, loop := range dev.DeviceLoops { if loop.ID == i { dev.DeviceLoops[j].State = i2 } } } for i, device := range reg.Devices { if device.Sn == dev.Sn { reg.Devices[i] = dev } } data, err := utils.SaveRegionOnData(reg) if err != nil { fmt.Printf("SaveRegionOnData err = %s\n", err.Error()) } err = SaveData(data) if err != nil { fmt.Printf("SaveData err = %s\n", err.Error()) } } } // UpdateDeviceRelayStatus 更新设备状态 //func (c Cron) UpdateDeviceRelayStatus() { // service := DeviceService{} // devices := service.GetDevicesByModule1AndModule3() // for _, d := range devices { // //新2路开关,因固件与8路相同 这里要特殊处理下这里 // if *d.Module == 3 { // d.CircuitNum = 8 // } // Query(d.DeviceSn, d.CircuitNum) // } //} // //// StatisticsDevice 设备统计 //func (c Cron) StatisticsDevice() { // statisticsService := DeviceOnlineDailyStatisticsService{} // onlines := statisticsService.GetTotalDeviceOnline() // for _, online := range onlines { // err := statisticsService.UpdateStatisticsDevice(online) // if err != nil { // logger.Logger.Errorf("StatisticsDevice err = %s", err.Error()) // } // } //} //