Commit 7e40048fd5c4411b6a9d78211cd3c91fa51b157c
1 parent
7663cf3d
Exists in
dev
Implementation backend demo remote control command
Showing
6 changed files
with
118 additions
and
30 deletions
Show diff stats
simulators/telescope/telescopeSimulator.py
... | ... | @@ -11,14 +11,18 @@ from utils.StatusManager import StatusManager |
11 | 11 | |
12 | 12 | |
13 | 13 | class TelescopeSimulator(DeviceSim, StatusManager): |
14 | - status = {"status" : "VALID"} | |
14 | + status = {"status": "VALID"} | |
15 | 15 | doors_timer = 5 |
16 | 16 | move_time = 5 |
17 | + _loop_mode = False | |
17 | 18 | |
18 | 19 | def __init__(self, argv): |
19 | 20 | super().__init__(argv) |
20 | 21 | self.setDevice("Telescope") |
21 | - self.setStatusManager("telescopeSimulator", argv) | |
22 | + if len(argv) == 1: | |
23 | + self.loop_mode = True | |
24 | + if (len(argv) > 1): | |
25 | + self.setStatusManager("telescopeSimulator", argv) | |
22 | 26 | |
23 | 27 | def telescopePrint(self, string: str): |
24 | 28 | if DEBUG_FILE: |
... | ... | @@ -37,16 +41,21 @@ class TelescopeSimulator(DeviceSim, StatusManager): |
37 | 41 | cmd_type = cut_data[0] |
38 | 42 | cmd = cut_data[1] |
39 | 43 | args = cut_data[2:] |
40 | - if (cmd_type == "GET"): | |
41 | - if (cmd == "STATUS"): | |
44 | + if cmd_type == "GET": | |
45 | + if cmd == "STATUS": | |
42 | 46 | answer = self.create_status() |
43 | 47 | else: |
44 | - answer = "Invalid cmd for GET: " + cmd | |
48 | + answer = "Invalid or not implemented cmd for GET: " + cmd | |
45 | 49 | self.telescopePrint(answer) |
46 | - elif (cmd_type == "DO"): | |
47 | - if (cmd == "GOTO"): | |
50 | + elif cmd_type == "DO": | |
51 | + if cmd == "GOTO": | |
48 | 52 | answer = "NEW POSITION SET TO " + ' '.join(args) |
53 | + else: | |
54 | + answer = "Invalid or not implemented cmd for DO: " + cmd | |
55 | + elif cmd_type == "SET": | |
56 | + answer = "Invalid or not implemented cmd for SET: " + cmd | |
49 | 57 | else: |
58 | + answer = "Unknown command received : " + data | |
50 | 59 | self.telescopePrint("Ignored message " + data) |
51 | 60 | |
52 | 61 | if (len(answer) > 0): |
... | ... | @@ -56,17 +65,17 @@ class TelescopeSimulator(DeviceSim, StatusManager): |
56 | 65 | def loop(self): |
57 | 66 | i = 0 |
58 | 67 | |
59 | - if (self.ended == 0): | |
68 | + if not self.loop_mode and self.ended == 0: | |
60 | 69 | self.telescopePrint("Not entry for telescope found in config file : " + self.config_file) |
61 | 70 | return (0) |
62 | - while (True): | |
71 | + while True: | |
63 | 72 | self.updateStatus(i) |
64 | - if (self.isConnected()): | |
73 | + if self.isConnected(): | |
65 | 74 | self.handleConnection() |
66 | 75 | i += 1 |
67 | - if (i > self.ended): | |
68 | - return (0) | |
69 | - return (1) | |
76 | + if not self.loop_mode and i > self.ended: | |
77 | + return 0 | |
78 | + return 1 | |
70 | 79 | |
71 | 80 | def run(self): |
72 | 81 | if DEBUG_FILE: |
... | ... | @@ -74,13 +83,13 @@ class TelescopeSimulator(DeviceSim, StatusManager): |
74 | 83 | self.parse() |
75 | 84 | self.configSocket() |
76 | 85 | self.loop() |
77 | - return (0) | |
86 | + return 0 | |
78 | 87 | |
79 | -if (__name__ == "__main__"): | |
88 | + | |
89 | +if __name__ == "__main__": | |
80 | 90 | sim = TelescopeSimulator(sys.argv) |
81 | 91 | sim.run() |
82 | 92 | |
83 | - | |
84 | 93 | # class Telescope(Device): |
85 | 94 | # |
86 | 95 | # def __init__(self): | ... | ... |
src/dashboard/views.py
... | ... | @@ -13,7 +13,8 @@ from django.utils.decorators import method_decorator |
13 | 13 | from django.urls import reverse_lazy, reverse |
14 | 14 | from django.http import Http404 |
15 | 15 | import json |
16 | -from django.views.decorators.csrf import requires_csrf_token | |
16 | +from devices.Telescope import TelescopeController | |
17 | +from devices.TelescopeRemoteControl import TelescopeRemoteControl | |
17 | 18 | |
18 | 19 | |
19 | 20 | log = l.setupLogger("dashboard", "dashboard") |
... | ... | @@ -174,10 +175,16 @@ def send_command_to_telescope(request): |
174 | 175 | |
175 | 176 | @login_required |
176 | 177 | def submit_command_to_telescope(request): |
177 | - import json | |
178 | 178 | if request.method == 'POST': |
179 | - param = request.POST.get("comande") | |
180 | - return HttpResponse(json.dumps({'message': "Command send OK"})) | |
179 | + commands = [request.POST.get("comande"), request.POST.get("param1")] | |
180 | + try: #TODO faire un truc plus joli pour gรฉrer les param | |
181 | + param2 = request.POST.get("param2") | |
182 | + if param2: | |
183 | + commands.append(param2) | |
184 | + except Exception: | |
185 | + pass | |
186 | + TelescopeRemoteControl(commands, False).exec_command() | |
187 | + return redirect('send_command_to_telescope') | |
181 | 188 | |
182 | 189 | @login_required |
183 | 190 | def submit_command_to_telescope_expert(request): |
... | ... | @@ -185,7 +192,8 @@ def submit_command_to_telescope_expert(request): |
185 | 192 | if request.method == 'POST': |
186 | 193 | param = request.POST.get("commande_expert") |
187 | 194 | if param: |
188 | - os.system("echo " + param + " >> contenu") | |
189 | - return HttpResponse(json.dumps({'message': "Command send OK"})) | |
195 | + response = TelescopeRemoteControl(param, expert_mode=True).exec_command() | |
196 | + os.system("echo \"status :" + response + "\" >> /home/portos/IRAP/pyros/src/status") | |
197 | + return HttpResponse(json.dumps({'message': "Command send OK", 'response': response})) | |
190 | 198 | return HttpResponse(json.dumps({'message': "Missing command data"})) |
191 | 199 | return redirect('submit_command_to_telescope') |
192 | 200 | \ No newline at end of file | ... | ... |
src/devices/Device.py
... | ... | @@ -77,7 +77,7 @@ class DeviceController(): |
77 | 77 | return False |
78 | 78 | # (EP) NON, 0 = false !!! |
79 | 79 | #return (0) |
80 | - Log.objects.create(agent=self.name, message='Message send : ' + message) | |
80 | + Log.objects.create(agent=self.name, message='Message sent : ' + message) | |
81 | 81 | return True |
82 | 82 | |
83 | 83 | def isError(self, message: str): |
... | ... | @@ -118,7 +118,9 @@ class DeviceController(): |
118 | 118 | |
119 | 119 | def blockAndReadBytes(self, size) -> str: |
120 | 120 | self.sock.setblocking(1) |
121 | - return (self.readBytes(size)) | |
121 | + message = self.readBytes(size) | |
122 | + Log.objects.create(agent=self.name, message='Message received : ' + message) | |
123 | + return (message) | |
122 | 124 | |
123 | 125 | def blockAndReadMessage(self) -> str: |
124 | 126 | return (self.blockAndReadBytes(1024)) |
... | ... | @@ -131,7 +133,10 @@ class DeviceController(): |
131 | 133 | |
132 | 134 | # TODO maybe read more than 1024 bytes ????? |
133 | 135 | def readMessage(self) -> str: |
134 | - return self.readBytes(1024) | |
136 | + message = str(self.readBytes(1024)) | |
137 | + #os.system("echo lol >> /home/portos/IRAP/pyros/src/POURKOICAMARCHPA") | |
138 | + Log.objects.create(agent=self.name, message='Message received : ' + message) | |
139 | + return message | |
135 | 140 | |
136 | 141 | def getConnection(self, device_name): |
137 | 142 | self.ip = self.config.get(device_name, "ip") | ... | ... |
src/devices/Telescope.py
... | ... | @@ -18,11 +18,15 @@ class TelescopeController(DeviceController): |
18 | 18 | |
19 | 19 | def set(self, command: str, *args): |
20 | 20 | message = "SET " + command + " " + " ".join([str(arg) for arg in args]) |
21 | - return (self.sendMessage(message)) | |
21 | + self.sendMessage(message) | |
22 | + response = self.blockAndReadMessage() | |
23 | + return response | |
22 | 24 | |
23 | 25 | def do(self, command: str, *args): |
24 | 26 | message = "DO " + command + " " + " ".join([str(arg) for arg in args]) |
25 | - return (self.sendMessage(message)) | |
27 | + self.sendMessage(message) | |
28 | + response = self.blockAndReadMessage() | |
29 | + return response | |
26 | 30 | |
27 | 31 | def getStatus(self): |
28 | 32 | self.get("STATUS") | ... | ... |
... | ... | @@ -0,0 +1,49 @@ |
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/misc/static/js/command_control.js
... | ... | @@ -98,7 +98,10 @@ jQuery(document).ready(function(){ |
98 | 98 | {val: 'ABORT', text: 'ABORT'}, |
99 | 99 | {val: 'HOMING', text: 'HOMING'}, |
100 | 100 | {val: 'DOORS', text: 'DOORS'}, |
101 | - {val: 'STOP', text: 'STOP'} | |
101 | + {val: 'GOTO', text: 'GOTO'}, | |
102 | + {val: 'STOP', text: 'STOP'}, | |
103 | + {val: 'OPEN DOME SHUTTER', text: 'OPEN DOME SHUTTER'}, | |
104 | + {val: 'CLOSE DOME SHUTTER', text: 'CLOSE DOME SHUTTER'} | |
102 | 105 | ]; |
103 | 106 | |
104 | 107 | var sel = $('<select>', {id:"param1", name:"param1"}).appendTo('#command_form'); |
... | ... | @@ -119,6 +122,12 @@ jQuery(document).ready(function(){ |
119 | 122 | selected.append($("<option>").attr('value','CLOSE').text('CLOSE')); |
120 | 123 | selected.css("font-size", "25px"); |
121 | 124 | } |
125 | + if ($("#param1").find(":selected").text() === "GOTO") | |
126 | + { | |
127 | + var selected = $('<input size="10">', {id:"param2", name:"param2", placeholder:"Coords"}).appendTo('#command_form'); | |
128 | + selected.css("font-size", "25px"); | |
129 | + } | |
130 | + | |
122 | 131 | |
123 | 132 | $('<br><br>').appendTo("#command_form"); |
124 | 133 | $('<input>', {id:"submit_button", type:"submit", value:"SEND"}).appendTo('#command_form'); |
... | ... | @@ -168,8 +177,12 @@ $("#command_form_expert").submit(function(event){ |
168 | 177 | |
169 | 178 | event.preventDefault(); |
170 | 179 | $.post('/dashboard/observation_status/send_command_to_telescope/submit_expert', $(this).serialize(), function(data){ |
171 | - var obj = JSON.parse(data); | |
172 | - console.log(obj.message); }) | |
180 | + var obj = JSON.parse(data); | |
181 | + console.log(obj.message); | |
182 | + console.log(obj.response); | |
183 | + if (obj.response === "KO: Unknown command") | |
184 | + alert("Unknown command"); | |
185 | + }) | |
173 | 186 | .fail(function() { |
174 | 187 | alert( "An error occured" ); |
175 | 188 | }) | ... | ... |