diff --git a/ZoneManager.py b/ZoneManager.py index 84a0b0a..2ef1d91 100644 --- a/ZoneManager.py +++ b/ZoneManager.py @@ -12,8 +12,8 @@ class ZoneManager: self.zones = fileIO.loadZones() for zone in self.zones: zone.setZoneManager(self) - self.pipeLine = [] #todo Thread Synchron - #self.lock = threading.Lock() + self.pipeLine = [] + self.piplineMutexLock = threading.Lock() def getZone(self, number): for zone in self.zones: @@ -21,22 +21,23 @@ class ZoneManager: return zone def addIrrigationJob(self, job): - #with self.lock: + with self.piplineMutexLock: self.pipeLine.append(job) def deleteIrrigationJobByID(self, id): - #with self.lock: - i = 0 + i = 0 + with self.piplineMutexLock: while i < (len(self.pipeLine)): irrigationJob = self.pipeLine[i] if(irrigationJob.id == id): self.pipeLine.pop(i) + break else: i = i + 1 def deleteIrrigationJobsForZone(self, zone): - #with self.lock: - i = 0 + i = 0 + with self.piplineMutexLock: while i < (len(self.pipeLine)): irrigationJob = self.pipeLine[i] if (irrigationJob.zone == zone): @@ -62,10 +63,10 @@ class ZoneManager: def getPlanedDurationForZone(self, zone): totalDuration = 0 - #with self.lock: - for irrigationJob in self.pipeLine: - if(irrigationJob.zone == zone): - totalDuration = totalDuration + irrigationJob.duration + with self.piplineMutexLock: + for irrigationJob in self.pipeLine: + if(irrigationJob.zone == zone): + totalDuration = totalDuration + irrigationJob.duration return totalDuration def refreshStates(self): @@ -73,16 +74,21 @@ class ZoneManager: 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) + self.piplineMutexLock.acquire() + if(len(self.pipeLine) > 0 and (not self.isAnyZoneBusy())): + irrigationJob = self.pipeLine[0] + self.pipeLine.pop(0) + self.piplineMutexLock.release() + irrigationJob.process() + else: + self.piplineMutexLock.release() + + def cronJobs(self): self.refreshStates() self.processPipeline() - print("Executed Cron Jobs of ZoneManager.\nactual Time: " + str(time())) + print("Executed Cron Jobs of ZoneManager.") diff --git a/main.py b/main.py index 419c506..8645ade 100644 --- a/main.py +++ b/main.py @@ -13,8 +13,9 @@ webserver = Webserver(zoneManager=zoneManager, port=80) def cronJobs(): while True: - time.sleep(0.5) zoneManager.cronJobs() + print("Cronjobs done\nactual Time: " + str(time.time())) + time.sleep(0.5) if __name__ == "__main__": cronjob_Thread = threading.Thread(target=cronJobs)