import threading from random import Random from time import time from FileIO import FileIO class ZoneManager: def __init__(self, systemSettings, fileIO): self.random = Random() self.systemSettings = systemSettings self.fileIO = FileIO self.zones = fileIO.loadZones() for zone in self.zones: zone.setZoneManager(self) self.pipeLine = [] #todo Thread Synchron #self.lock = threading.Lock() def getZone(self, number): for zone in self.zones: if(zone.number == number): return zone def addIrrigationJob(self, job): #with self.lock: self.pipeLine.append(job) def deleteIrrigationJobByID(self, id): #with self.lock: i = 0 while i < (len(self.pipeLine)): irrigationJob = self.pipeLine[i] if(irrigationJob.id == id): self.pipeLine.pop(i) else: i = i + 1 def deleteIrrigationJobsForZone(self, zone): #with self.lock: i = 0 while i < (len(self.pipeLine)): irrigationJob = self.pipeLine[i] if (irrigationJob.zone == zone): self.pipeLine.pop(i) else: i = i + 1 def isAnyZoneBusy(self): for zone in self.zones: if(zone.state): return True return False def switchZoneState(self, zone, state, duration, instant=False): if(instant or self.systemSettings.multiZoneIrrigation or state==False or (not self.isAnyZoneBusy())): zone.switchState(state=state, duration=duration, instant=True) else: self.addIrrigationJob(IrrigationJob(id=self.random.randint(a=100000000, b=999999999), zone=zone, duration=duration)) def switchZoneIndexState(self, zoneIndex, state, duration, instant=False): zone = self.getZone(zoneIndex) self.switchZoneState(zone, state, duration, instant) def getPlanedDurationForZone(self, zone): totalDuration = 0 #with self.lock: for irrigationJob in self.pipeLine: if(irrigationJob.zone == zone): totalDuration = totalDuration + irrigationJob.duration return totalDuration def refreshStates(self): for zone in self.zones: zone.refreshState() def processPipeline(self): #with self.lock: if(len(self.pipeLine) > 0 and (not self.isAnyZoneBusy())): irrigationJob = self.pipeLine[0] irrigationJob.process() self.pipeLine.pop(0) def cronJobs(self): self.refreshStates() self.processPipeline() print("Executed Cron Jobs of ZoneManager.\nactual Time: " + str(time())) class IrrigationJob: def __init__(self, id, zone, duration=0): self.id = id self.zone = zone self.duration = duration self.zone.switchState(state=True, duration=duration, instant=False) def process(self): self.zone.switchState(state=True, duration=self.duration, instant=True)