Blame view

src/monitoring/TelescopeMonitoring.py 1.77 KB
e6eb923f   Quentin Durand   Prototype agent t...
1
2
3
4
5
6
7
8
9
10
11
12
13
14

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
01c4bb7b   Quentin Durand   add coments
15
    retrieving the status of the device 
e6eb923f   Quentin Durand   Prototype agent t...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'''
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
01c4bb7b   Quentin Durand   add coments
36
        Pour utiliser la traduction de grammaires, instancier ici un TelescopeRemoteControlXXXX et executer sa méthode exec_command 
e6eb923f   Quentin Durand   Prototype agent t...
37
38
39
    '''

    def execute_request(self, request):
01c4bb7b   Quentin Durand   add coments
40
        #remplacer cette ligne par l'utilisation de TelescopeRemoteControl
e6eb923f   Quentin Durand   Prototype agent t...
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
        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)