Blame view

src/monitoring/tasks.py 4.86 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
c72eb17a   Jeremy   Update celery task
8
9
10
import majordome.tasks
import alert_manager.tasks

4cb0ff36   Jeremy   Update
11
log = L.setupLogger("MonitoringTaskLogger", "Monitoring")
c5a3b4a0   haribo   Date: 05/07/2016
12

ce470283   Jeremy   Plc simulator fin...
13
14
15
DEBUG_FILE = False


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

    def run(self):
c72eb17a   Jeremy   Update celery task
25
        self.createTask()
65149de7   Jeremy   Update
26
        self.setContext()
4cb0ff36   Jeremy   Update
27
        self.setTime()
c72eb17a   Jeremy   Update celery task
28
        self.setTasks()
4cb0ff36   Jeremy   Update
29
        self.loop()
a190892c   haribo   Date: 05/07/2016
30

c72eb17a   Jeremy   Update celery task
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    def createTask(self):
        try:
            TaskId.objects.find(task="monitoring").delete()
        except Exception as e:
            log.info(str(e))
            return 1
        TaskId.objects.create(task_id=self.request.id, task="monitoring")
        return 0

    def setTasks(self):
        try:
            self.majordome_task = TaskId.objects.get(task="majordome")
            self.alert_task = TaskId.objects.get(task="alert_manager")
        except Exception as e:
            self.majordome_task = None
            self.alert_task = None
        return 0

65149de7   Jeremy   Update
49
    def setContext(self):
ddf59dd4   haribo   Remaniement :
50
        self.plc = PLCController()
4cb0ff36   Jeremy   Update
51
        self.state = "RUNNING"
65149de7   Jeremy   Update
52
        return (0)
c5a3b4a0   haribo   Date: 05/07/2016
53

4cb0ff36   Jeremy   Update
54
    def setTime(self):
ef60c3ec   Jeremy   Majordome and mon...
55
        self.timer_status = 2
c72eb17a   Jeremy   Update celery task
56
57
58
59
60
        self.timer_tasks = 5
        self.timers = {"timer_status": self.timer_status,
                       "tasks": self.timer_tasks}
        self.functions = {"timer_status": self.handleTimerStatus,
                          "tasks": self.handleTasks}
4cb0ff36   Jeremy   Update
61
        return (0)
21598bc6   haribo   Date: 01/08/2016
62

bca9a283   Jeremy   Reworked the sche...
63
64
65
    def logDB(self, message: str):
        Log.objects.create(agent='Monitoring', message=message)

c72eb17a   Jeremy   Update celery task
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    def handleTasks(self):
        self.timers["tasks"] = self.timer_tasks
        # TODO check majordome and alert_manager status
        if self.majordome_task is None:
            try:
                self.monitoring_task = TaskId.objects.get(task="majordome")
            except Exception as e:
                majordome.tasks.Majordome.apply_async()
                if settings.DEBUG and DEBUG_FILE:
                    log.info(str(e))
        if self.alert_task is None:
            try:
                self.alert_task = TaskId.objects.get(task="alert_manager")
            except Exception as e:
                alert_manager.tasks.AlertListener.apply_async()
                if settings.DEBUG and DEBUG_FILE:
                    log.info(str(e))
        return 0

ce470283   Jeremy   Plc simulator fin...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    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
114
115
    def handleTimerStatus(self):
        self.timers["timer_status"] = self.timer_status
678838ed   Jeremy   Weather ans insid...
116
        status_plc = self.plc.getStatus()
ce470283   Jeremy   Plc simulator fin...
117
118
        if (self.plc.isError(status_plc)):
            if (settings.DEBUG and DEBUG_FILE):
e8e6f017   Jeremy   Reworked devices ...
119
                log.info("Invalid status returned : " + str(self.status_plc))
e8e6f017   Jeremy   Reworked devices ...
120
            return (1)
ce470283   Jeremy   Plc simulator fin...
121
122
123
124
125
        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
126

4cb0ff36   Jeremy   Update
127
128
129
130
131
132
    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
133
                if (timer_value <= 0):
ef60c3ec   Jeremy   Majordome and mon...
134
                    if (timer_name in self.functions):
4cb0ff36   Jeremy   Update
135
136
                        self.functions[timer_name]()
                    else:
ce470283   Jeremy   Plc simulator fin...
137
                        if (settings.DEBUG and DEBUG_FILE):
4cb0ff36   Jeremy   Update
138
                            log.info("Timer : " + str(timer_name) + "is not known by the monitoring")
bca9a283   Jeremy   Reworked the sche...
139
                        self.logDB("Timer " + str(timer_name) + " unknown")
ce470283   Jeremy   Plc simulator fin...
140
                if (settings.DEBUG and DEBUG_FILE):
4cb0ff36   Jeremy   Update
141
                    log.info("Timer : " + str(timer_name) + " executed by monitoring")
ef60c3ec   Jeremy   Majordome and mon...
142
143
144
145
146
        return (0)

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