Blame view

src/monitoring/tasks.py 3.33 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 *
ce470283   Jeremy   Plc simulator fin...
7
import json
bca9a283   Jeremy   Reworked the sche...
8
import utils.Logger as L
4cb0ff36   Jeremy   Update
9
log = L.setupLogger("MonitoringTaskLogger", "Monitoring")
c5a3b4a0   haribo   Date: 05/07/2016
10

ce470283   Jeremy   Plc simulator fin...
11
12
13
DEBUG_FILE = False


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

    def run(self):
65149de7   Jeremy   Update
23
        self.setContext()
4cb0ff36   Jeremy   Update
24
25
        self.setTime()
        self.loop()
a190892c   haribo   Date: 05/07/2016
26

65149de7   Jeremy   Update
27
    def setContext(self):
ddf59dd4   haribo   Remaniement :
28
        self.plc = PLCController()
4cb0ff36   Jeremy   Update
29
        self.state = "RUNNING"
65149de7   Jeremy   Update
30
        return (0)
c5a3b4a0   haribo   Date: 05/07/2016
31

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

bca9a283   Jeremy   Reworked the sche...
38
39
40
    def logDB(self, message: str):
        Log.objects.create(agent='Monitoring', message=message)

ce470283   Jeremy   Plc simulator fin...
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    def parseStatus(self, status_plc):
        try:
            status = {}
            string_list = status_plc.splitlines()
            for item in string_list:
                res = item.split('=')
                status[res[0]] = res[1]
        except Exception as e:
            if DEBUG_FILE and settings.DEBUG:
                log.info(str(e))
            self.status_plc = {}
            return 1
        self.status_plc = status
        return 0

    def saveStatus(self):
        outside = WeatherWatch()
        inside = SiteWatch()
        for key, value in self.status_plc.items():
            if key.find("Weather") != -1:
                outside.setAttribute(key, value)
            else:
                inside.setAttribute(key, value)
        outside.setGlobalStatus()
        inside.setGlobalStatus()
        outside.save()
        inside.save()
        return 0

4cb0ff36   Jeremy   Update
70
71
    def handleTimerStatus(self):
        self.timers["timer_status"] = self.timer_status
ce470283   Jeremy   Plc simulator fin...
72
73
74
        status_plc = self.plc.get("STATUS")
        if (self.plc.isError(status_plc)):
            if (settings.DEBUG and DEBUG_FILE):
e8e6f017   Jeremy   Reworked devices ...
75
                log.info("Invalid status returned : " + str(self.status_plc))
e8e6f017   Jeremy   Reworked devices ...
76
            return (1)
ce470283   Jeremy   Plc simulator fin...
77
78
79
80
81
        if self.parseStatus(status_plc):
            if (settings.DEBUG and DEBUG_FILE):
                log.info("Invalid status returned : " + str(self.status_plc))
            return 1
        return self.saveStatus()
21598bc6   haribo   Date: 01/08/2016
82

4cb0ff36   Jeremy   Update
83
84
85
86
87
88
    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
89
                if (timer_value <= 0):
ef60c3ec   Jeremy   Majordome and mon...
90
                    if (timer_name in self.functions):
4cb0ff36   Jeremy   Update
91
92
                        self.functions[timer_name]()
                    else:
ce470283   Jeremy   Plc simulator fin...
93
                        if (settings.DEBUG and DEBUG_FILE):
4cb0ff36   Jeremy   Update
94
                            log.info("Timer : " + str(timer_name) + "is not known by the monitoring")
bca9a283   Jeremy   Reworked the sche...
95
                        self.logDB("Timer " + str(timer_name) + " unknown")
ce470283   Jeremy   Plc simulator fin...
96
                if (settings.DEBUG and DEBUG_FILE):
4cb0ff36   Jeremy   Update
97
                    log.info("Timer : " + str(timer_name) + " executed by monitoring")
ef60c3ec   Jeremy   Majordome and mon...
98
99
100
101
102
        return (0)

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