TelescopeMonitoring.py
1.77 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
from common.models import TelescopeCommand
from devices import Telescope
#from time import sleep
import time
import os
import sys
'''
THIS IS A PROTOTYPE ! Its does not use the RemoteControlClass which convert the command to specific grammar,
it need to be implemented
Agent which turn in an infinite loop and watch the database looking for requests to execute, it will be also used for
retrieving the status of the device
'''
class TelescopeMonitoring():
_telescope_controller = Telescope.TelescopeController()
def __init__(self):
self.loop()
'''
Method looking for commands to execute in the database, if there are some, return them
'''
def get_requests(self):
try:
requests = TelescopeCommand.objects.filter(answer=None)
except TelescopeCommand.DoesNotExist:
requests = None
return requests
'''
Method that executes the requests found in db by get_requests, then place the corresponding answer in the db for the request
Pour utiliser la traduction de grammaires, instancier ici un TelescopeRemoteControlXXXX et executer sa méthode exec_command
'''
def execute_request(self, request):
#remplacer cette ligne par l'utilisation de TelescopeRemoteControl
answer = self._telescope_controller.send_command(request.request)
request.answer = answer
request.save()
'''
Main loop, added sleep to save the use of the CPU
'''
def loop(self):
while True:
requests = self.get_requests()
if requests:
for request in requests:
self.execute_request(request)
#TODO: rajouter ici la gestion des status et le stockage en db
time.sleep(0.03)