Blame view

src/monitoring/tasks.py 2.07 KB
5b5566ab   haribo   added celery
1
from __future__ import absolute_import
a190892c   haribo   Date: 05/07/2016
2
from django.conf import settings
ddf59dd4   haribo   Remaniement :
3
from common.models import *
5b5566ab   haribo   added celery
4
from celery.task import Task
ddf59dd4   haribo   Remaniement :
5
from devices.PLC import PLCController
4cb0ff36   Jeremy   Update
6
7
8
from utils.JDManipulator import *
import utils.config as L
log = L.setupLogger("MonitoringTaskLogger", "Monitoring")
c5a3b4a0   haribo   Date: 05/07/2016
9

65149de7   Jeremy   Update
10
11
'''
    Infinite task created at the program's start.
4cb0ff36   Jeremy   Update
12
    Checks the plc status, parse it, analyse it, store it in db
65149de7   Jeremy   Update
13
'''
ef60c3ec   Jeremy   Majordome and mon...
14
15
16
class Monitoring(Task):
    timers = {}
    functions = {}
5b5566ab   haribo   added celery
17
18

    def run(self):
65149de7   Jeremy   Update
19
        self.setContext()
4cb0ff36   Jeremy   Update
20
21
        self.setTime()
        self.loop()
a190892c   haribo   Date: 05/07/2016
22

65149de7   Jeremy   Update
23
    def setContext(self):
ddf59dd4   haribo   Remaniement :
24
        self.plc = PLCController()
4cb0ff36   Jeremy   Update
25
        self.state = "RUNNING"
65149de7   Jeremy   Update
26
        return (0)
c5a3b4a0   haribo   Date: 05/07/2016
27

4cb0ff36   Jeremy   Update
28
    def setTime(self):
ef60c3ec   Jeremy   Majordome and mon...
29
30
31
        self.timer_status = 2
        self.timers = {"timer_status": self.timer_status}
        self.functions = {"timer_status": self.handleTimerStatus}
4cb0ff36   Jeremy   Update
32
        return (0)
21598bc6   haribo   Date: 01/08/2016
33

4cb0ff36   Jeremy   Update
34
35
36
    def handleTimerStatus(self):
        self.timers["timer_status"] = self.timer_status
        self.status_plc = self.plc.get("STATUS")
e8e6f017   Jeremy   Reworked devices ...
37
38
39
40
        if (self.plc.isError(self.status_plc)):
            if (settings.DEBUG):
                log.info("Invalid status returned : " + str(self.status_plc))
            return (1)
4cb0ff36   Jeremy   Update
41
42
        # TODO: parse, analyse, store
        return (0)
21598bc6   haribo   Date: 01/08/2016
43

4cb0ff36   Jeremy   Update
44
45
46
47
48
49
    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():
4cb0ff36   Jeremy   Update
50
                if (timer_value <= 0):
ef60c3ec   Jeremy   Majordome and mon...
51
                    if (timer_name in self.functions):
4cb0ff36   Jeremy   Update
52
53
54
55
56
57
                        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")
ef60c3ec   Jeremy   Majordome and mon...
58
59
60
61
62
        return (0)

if (__name__ == "__main__"):
    m = Monitoring()
    m.run()