Blame view

src/monitoring/tasks.py 2.4 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
from utils.JDManipulator import *
bca9a283   Jeremy   Reworked the sche...
7
import utils.Logger as L
4cb0ff36   Jeremy   Update
8
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

bca9a283   Jeremy   Reworked the sche...
34
35
36
    def logDB(self, message: str):
        Log.objects.create(agent='Monitoring', message=message)

4cb0ff36   Jeremy   Update
37
38
39
    def handleTimerStatus(self):
        self.timers["timer_status"] = self.timer_status
        self.status_plc = self.plc.get("STATUS")
f7dd3df1   Jeremy   Update simulators...
40
        log.info("Received status : " + str(self.status_plc))
bca9a283   Jeremy   Reworked the sche...
41
        self.logDB("Received status from plc")
e8e6f017   Jeremy   Reworked devices ...
42
43
44
        if (self.plc.isError(self.status_plc)):
            if (settings.DEBUG):
                log.info("Invalid status returned : " + str(self.status_plc))
bca9a283   Jeremy   Reworked the sche...
45
            self.logDB("Status considered as INVALID")
e8e6f017   Jeremy   Reworked devices ...
46
            return (1)
4cb0ff36   Jeremy   Update
47
48
        # TODO: parse, analyse, store
        return (0)
21598bc6   haribo   Date: 01/08/2016
49

4cb0ff36   Jeremy   Update
50
51
52
53
54
55
    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
56
                if (timer_value <= 0):
ef60c3ec   Jeremy   Majordome and mon...
57
                    if (timer_name in self.functions):
4cb0ff36   Jeremy   Update
58
59
60
61
                        self.functions[timer_name]()
                    else:
                        if (settings.DEBUG):
                            log.info("Timer : " + str(timer_name) + "is not known by the monitoring")
bca9a283   Jeremy   Reworked the sche...
62
                        self.logDB("Timer " + str(timer_name) + " unknown")
4cb0ff36   Jeremy   Update
63
64
                if (settings.DEBUG):
                    log.info("Timer : " + str(timer_name) + " executed by monitoring")
ef60c3ec   Jeremy   Majordome and mon...
65
66
67
68
69
        return (0)

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