tasks.py
1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from __future__ import absolute_import
from django.conf import settings
from common.models import *
from celery.task import Task
from devices.PLC import PLCController
from utils.JDManipulator import *
import utils.config as L
log = L.setupLogger("MonitoringTaskLogger", "Monitoring")
'''
Infinite task created at the program's start.
Checks the plc status, parse it, analyse it, store it in db
'''
class Monitoring(Task):
timers = {}
functions = {}
def run(self):
self.setContext()
self.setTime()
self.loop()
def setContext(self):
self.plc = PLCController()
self.state = "RUNNING"
return (0)
def setTime(self):
self.timer_status = 2
self.timers = {"timer_status": self.timer_status}
self.functions = {"timer_status": self.handleTimerStatus}
return (0)
def handleTimerStatus(self):
self.timers["timer_status"] = self.timer_status
self.status_plc = self.plc.get("STATUS")
print("handle Timer")
# TODO: parse, analyse, store
return (0)
def loop(self):
while (self.state != "SHUTDOWN"):
minimal_timer = min(self.timers, key=self.timers.get)
time.sleep(self.timers[minimal_timer])
self.timers = {key: value - self.timers[minimal_timer] for key, value in self.timers.items()}
for timer_name, timer_value in self.timers.items():
print("Loop")
if (timer_value <= 0):
if (timer_name in self.functions):
self.functions[timer_name]()
else:
if (settings.DEBUG):
log.info("Timer : " + str(timer_name) + "is not known by the monitoring")
if (settings.DEBUG):
log.info("Timer : " + str(timer_name) + " executed by monitoring")
return (0)
if (__name__ == "__main__"):
m = Monitoring()
m.run()