Commit c78dc38bf1038dc3bb88fd0f4137b31514e9de97

Authored by Quentin Durand
1 parent 54f89f9d
Exists in dev

Classe abstraite ctrl command + default + un peu de back sur les views et le simulateur

src/dashboard/views.py
... ... @@ -17,7 +17,7 @@ from django.http import Http404
17 17 import json
18 18 from random import randint
19 19 from devices.Telescope import TelescopeController
20   -from devices.TelescopeRemoteControl import TelescopeRemoteControl
  20 +from devices.TelescopeRemoteControlDefault import TelescopeRemoteControlDefault
21 21 from django.core.mail import send_mail
22 22 import time
23 23 import utils.celme as celme
... ... @@ -298,25 +298,32 @@ def send_command_to_telescope(request):
298 298 @level_required(3)
299 299 def submit_command_to_telescope(request):
300 300 if request.method == 'POST':
301   - commands = [request.POST.get("comande"), request.POST.get("param1")]
302   - try: #TODO faire un truc plus joli pour gérer les param
303   - param2 = request.POST.get("param2")
304   - if param2:
305   - commands.append(param2)
  301 + commands = [request.POST.get("first_command"), request.POST.get("first_param")]
  302 + try: #TODO faire un truc plus joli pour gérer les params queqlue soit leur nombre
  303 + input_0 = request.POST.get("input_number_0")
  304 + input_1 = request.POST.get("input_number_1")
  305 + input_2 = request.POST.get("input_number_2")
  306 + if input_0:
  307 + commands.append(input_0)
  308 + if input_1:
  309 + commands.append(input_1)
  310 + if input_2:
  311 + commands.append(input_2)
306 312 except Exception:
307 313 pass
308   - TelescopeRemoteControl(commands, False).exec_command()
  314 + response = TelescopeRemoteControlDefault(commands, False).exec_command()
  315 + #TODO passer en JS pour send les réponses en AJAX
309 316 return redirect('send_command_to_telescope')
310 317  
311 318 @login_required
312 319 @level_required(3)
313 320 def submit_command_to_telescope_expert(request):
314   - import os
  321 + #import os
315 322 if request.method == 'POST':
316 323 param = request.POST.get("commande_expert")
317 324 if param:
318   - response = TelescopeRemoteControl(param, expert_mode=True).exec_command()
319   - os.system("echo \"status :" + response + "\" >> /home/portos/IRAP/pyros/src/status")
  325 + response = TelescopeRemoteControlDefault(param, expert_mode=True).exec_command()
  326 + #os.system("echo \"status :" + response + "\" >> /home/portos/IRAP/pyros/src/status")
320 327 return HttpResponse(json.dumps({'message': "Command send OK", 'response': response}))
321 328 return HttpResponse(json.dumps({'message': "Missing command data"}))
322 329 return redirect('submit_command_to_telescope')
... ...
src/devices/Device.py
... ... @@ -103,7 +103,8 @@ class DeviceController():
103 103 try:
104 104 readable, writable, exceptional = select.select([self.sock], [], [self.sock], 0)
105 105 if not (readable or exceptional):
106   - raise (Exception("Socket not readable"))
  106 + ret = self.sock.recv(size).decode()
  107 + raise (Exception("KO: " + ret))
107 108 ret = self.sock.recv(size).decode()
108 109 if (not ret):
109 110 if (settings.DEBUG):
... ... @@ -113,7 +114,7 @@ class DeviceController():
113 114 except Exception as e:
114 115 if (settings.DEBUG):
115 116 self.log(self.name, "Socket not readable : " + str(e))
116   - return ("FAILED")
  117 + return (str(e))
117 118 return (ret)
118 119  
119 120 def blockAndReadBytes(self, size) -> str:
... ...
src/devices/Telescope.py
... ... @@ -28,6 +28,10 @@ class TelescopeController(DeviceController):
28 28 response = self.blockAndReadMessage()
29 29 return response
30 30  
  31 + def send_command(self, command):
  32 + self.sendMessage(command)
  33 + return self.blockAndReadMessage()
  34 +
31 35 def getStatus(self):
32 36 self.get("STATUS")
33 37 return "OK"
... ...
src/devices/TelescopeRemoteControl.py deleted
... ... @@ -1,49 +0,0 @@
1   -from django.conf import settings
2   -from common.models import Log
3   -from devices.Telescope import TelescopeController
4   -
5   -
6   -'''
7   - Class managing the input from the remote commands sent by the dashboard
8   - the command is parsed and sent to the Telescope via an instance of TelescopeController
9   -
10   -'''
11   -
12   -class TelescopeRemoteControl():
13   - _command = []
14   - _command_matcher = {}
15   - def __init__(self, command, expert_mode):
16   - self._command = command
17   - if expert_mode:
18   - self._command = str(command).split()
19   - self._telescope = TelescopeController()
20   - self._command_matcher = {
21   - "GET": self.get,
22   - "SET": self.set,
23   - "DO": self.do,
24   - }
25   -
26   - def exec_command(self):
27   - if self._command[0] in self._command_matcher:
28   - response = self._command_matcher[self._command[0]]()
29   - else:
30   - return "KO: Unknown command"
31   - return response
32   -
33   - def get(self):
34   - param_a = {"POSITION", "STATUS", "SETUP"}
35   - if self._command[1] in param_a:
36   - response = self._telescope.get(self._command[1])
37   - return response
38   - return "KO: Unknown command"
39   -
40   - def set(self):
41   - response = self._telescope.set(' '.join(self._command[1:]))
42   - return "OK Command sent"
43   -
44   - def do(self):
45   - response = self._telescope.do(' '.join(self._command[1:]))
46   - return "OK command sent"
47   -
48   - def expert_command(self):
49   - return True
src/devices/TelescopeRemoteControlAbstract.py 0 → 100644
... ... @@ -0,0 +1,54 @@
  1 +from django.conf import settings
  2 +from common.models import Log
  3 +from devices.Telescope import TelescopeController
  4 +
  5 +
  6 +'''
  7 + Class managing the input from the remote commands sent by the dashboard
  8 + the command is parsed and sent to the Telescope via an instance of TelescopeController
  9 +
  10 +'''
  11 +
  12 +class TelescopeRemoteControlAbstract():
  13 + _command = []
  14 + _command_matcher_set = {}
  15 + _command_matcher_get = {}
  16 + _command_matcher_do = {}
  17 + _telescope = None
  18 +
  19 + def __init__(self, command, expert_mode):
  20 + self._command = command
  21 + if expert_mode:
  22 + self._command = str(command).split()
  23 + self._telescope = TelescopeController()
  24 +
  25 + def exec_command(self):
  26 + raise NotImplementedError("You must implement %s method" % type(self).__name__)
  27 + # if self._command[0] in self._command_matcher:
  28 + # response = self._command_matcher[self._command[0]]()
  29 + # else:
  30 + # return "KO: Unknown command"
  31 + # return response
  32 +
  33 + def get(self):
  34 + # param_a = {"POSITION", "STATUS", "SETUP"}
  35 + # if self._command[1] in param_a:
  36 + # response = self._telescope.get(self._command[1])
  37 + # return response
  38 + # return "KO: Unknown command"
  39 + raise NotImplementedError("You must implement %s method" % type(self).__name__)
  40 +
  41 + def set(self):
  42 + # response = self._telescope.set(' '.join(self._command[1:]))
  43 + # return "OK Command sent"
  44 + raise NotImplementedError("You must implement %s method" % type(self).__name__)
  45 +
  46 + def do(self):
  47 + #response = self._telescope.do(' '.join(self._command[1:]))
  48 + #return "OK command sent"
  49 + raise NotImplementedError("You must implement %s method" % type(self).__name__)
  50 +
  51 +
  52 + def expert_command(self):
  53 + raise NotImplementedError("You must implement %s method" % type(self).__name__)
  54 + #return True
... ...
src/devices/TelescopeRemoteControlDefault.py 0 → 100644
... ... @@ -0,0 +1,64 @@
  1 +from django.conf import settings
  2 +from common.models import Log
  3 +from devices.TelescopeRemoteControlAbstract import TelescopeRemoteControlAbstract
  4 +from devices.Telescope import TelescopeController
  5 +import os
  6 +
  7 +class TelescopeRemoteControlDefault(TelescopeRemoteControlAbstract):
  8 + _command_matcher_set = {
  9 + "COORD_RADEC": "COORD_RADEC",
  10 + "COORD_HADEC": "COORD_HADEC",
  11 + "COORD_ALTAZ": "COORD_ALTAZ",
  12 + "DRIFT_HADEC": "DRIFT_HADEC",
  13 + "ROTATOR_ANGLE": "ROTATOR_ANGLE"
  14 + }
  15 + _command_matcher_get = {
  16 + "INFORMATIONS": "INFORMATIONS",
  17 + "COORD_RADEC": "COORD_RADEC",
  18 + "COORD_HADEC": "COORD_HADEC",
  19 + "COORD_ALTAZ": "COORD_ALTAZ"
  20 + }
  21 + _command_matcher_do = {
  22 + "EVAL": "EVAL",
  23 + "HOMING": "HOMING",
  24 + "ZENITH": "ZENITH",
  25 + "GOTO": "GOTO",
  26 + "STOP": "STOP",
  27 + "DRIFT_ON": "DRIFT_ON",
  28 + "DRIFT_OFF": "DRIFT_OFF",
  29 + "ROTATOR_GOTO": "ROTATOR_GOTO"
  30 + }
  31 + def __init__(self, command, expert_mode):
  32 + super().__init__(command, expert_mode)
  33 +
  34 + def exec_command(self):
  35 +
  36 + os.system("echo \"status :" + str(self._command) + "\" >> /home/portos/IRAP/pyros/src/commande_recu")
  37 + if self._command[0] == "GET": return self.get()
  38 + elif self._command[0] == "SET": return self.set()
  39 + elif self._command[0] == "DO": return self.do()
  40 + else: return "KO: Unknown command"
  41 +
  42 + def do(self):
  43 + if self._command[1] in self._command_matcher_do:
  44 + definitive_command = "DO " + self._command_matcher_do[self._command[1]] + ' ' + ' '.join(self._command[2:])
  45 + response = self._telescope.send_command(definitive_command)
  46 + else:
  47 + response = "KO: Unknown command"
  48 + return response
  49 +
  50 + def set(self):
  51 + if self._command[1] in self._command_matcher_set:
  52 + definitive_command = "SET " + self._command_matcher_set[self._command[1]] + ' ' + ' '.join(self._command[2:])
  53 + response = self._telescope.send_command(definitive_command)
  54 + else:
  55 + response = "KO: Unknown command"
  56 + return response
  57 +
  58 + def get(self):
  59 + if self._command[1] in self._command_matcher_get:
  60 + definitive_command = "GET " + self._command_matcher_get[self._command[1]] #+ " " + self._command[2:]
  61 + response = self._telescope.send_command(definitive_command)
  62 + else:
  63 + response = "KO: Unknown command"
  64 + return response
0 65 \ No newline at end of file
... ...
src/misc/static/js/command_control.js
... ... @@ -159,6 +159,8 @@ $("#command_form_expert").submit(function(event){
159 159 console.log(obj.response);
160 160 if (obj.response === "KO: Unknown command")
161 161 alert("Unknown command");
  162 + else if (obj.response.startsWith("KO"))
  163 + alert(obj.response);
162 164 })
163 165 .fail(function() {
164 166 alert( "An error occured" );
... ...