From 7e40048fd5c4411b6a9d78211cd3c91fa51b157c Mon Sep 17 00:00:00 2001 From: Quentin Durand Date: Wed, 13 Jun 2018 17:02:03 +0200 Subject: [PATCH] Implementation backend demo remote control command --- simulators/telescope/telescopeSimulator.py | 41 +++++++++++++++++++++++++---------------- src/dashboard/views.py | 20 ++++++++++++++------ src/devices/Device.py | 11 ++++++++--- src/devices/Telescope.py | 8 ++++++-- src/devices/TelescopeRemoteControl.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/misc/static/js/command_control.js | 19 ++++++++++++++++--- 6 files changed, 118 insertions(+), 30 deletions(-) create mode 100644 src/devices/TelescopeRemoteControl.py diff --git a/simulators/telescope/telescopeSimulator.py b/simulators/telescope/telescopeSimulator.py index 8b57682..982c860 100644 --- a/simulators/telescope/telescopeSimulator.py +++ b/simulators/telescope/telescopeSimulator.py @@ -11,14 +11,18 @@ from utils.StatusManager import StatusManager class TelescopeSimulator(DeviceSim, StatusManager): - status = {"status" : "VALID"} + status = {"status": "VALID"} doors_timer = 5 move_time = 5 + _loop_mode = False def __init__(self, argv): super().__init__(argv) self.setDevice("Telescope") - self.setStatusManager("telescopeSimulator", argv) + if len(argv) == 1: + self.loop_mode = True + if (len(argv) > 1): + self.setStatusManager("telescopeSimulator", argv) def telescopePrint(self, string: str): if DEBUG_FILE: @@ -37,16 +41,21 @@ class TelescopeSimulator(DeviceSim, StatusManager): cmd_type = cut_data[0] cmd = cut_data[1] args = cut_data[2:] - if (cmd_type == "GET"): - if (cmd == "STATUS"): + if cmd_type == "GET": + if cmd == "STATUS": answer = self.create_status() else: - answer = "Invalid cmd for GET: " + cmd + answer = "Invalid or not implemented cmd for GET: " + cmd self.telescopePrint(answer) - elif (cmd_type == "DO"): - if (cmd == "GOTO"): + elif cmd_type == "DO": + if cmd == "GOTO": answer = "NEW POSITION SET TO " + ' '.join(args) + else: + answer = "Invalid or not implemented cmd for DO: " + cmd + elif cmd_type == "SET": + answer = "Invalid or not implemented cmd for SET: " + cmd else: + answer = "Unknown command received : " + data self.telescopePrint("Ignored message " + data) if (len(answer) > 0): @@ -56,17 +65,17 @@ class TelescopeSimulator(DeviceSim, StatusManager): def loop(self): i = 0 - if (self.ended == 0): + if not self.loop_mode and self.ended == 0: self.telescopePrint("Not entry for telescope found in config file : " + self.config_file) return (0) - while (True): + while True: self.updateStatus(i) - if (self.isConnected()): + if self.isConnected(): self.handleConnection() i += 1 - if (i > self.ended): - return (0) - return (1) + if not self.loop_mode and i > self.ended: + return 0 + return 1 def run(self): if DEBUG_FILE: @@ -74,13 +83,13 @@ class TelescopeSimulator(DeviceSim, StatusManager): self.parse() self.configSocket() self.loop() - return (0) + return 0 -if (__name__ == "__main__"): + +if __name__ == "__main__": sim = TelescopeSimulator(sys.argv) sim.run() - # class Telescope(Device): # # def __init__(self): diff --git a/src/dashboard/views.py b/src/dashboard/views.py index 0accc45..432cd72 100644 --- a/src/dashboard/views.py +++ b/src/dashboard/views.py @@ -13,7 +13,8 @@ from django.utils.decorators import method_decorator from django.urls import reverse_lazy, reverse from django.http import Http404 import json -from django.views.decorators.csrf import requires_csrf_token +from devices.Telescope import TelescopeController +from devices.TelescopeRemoteControl import TelescopeRemoteControl log = l.setupLogger("dashboard", "dashboard") @@ -174,10 +175,16 @@ def send_command_to_telescope(request): @login_required def submit_command_to_telescope(request): - import json if request.method == 'POST': - param = request.POST.get("comande") - return HttpResponse(json.dumps({'message': "Command send OK"})) + commands = [request.POST.get("comande"), request.POST.get("param1")] + try: #TODO faire un truc plus joli pour gérer les param + param2 = request.POST.get("param2") + if param2: + commands.append(param2) + except Exception: + pass + TelescopeRemoteControl(commands, False).exec_command() + return redirect('send_command_to_telescope') @login_required def submit_command_to_telescope_expert(request): @@ -185,7 +192,8 @@ def submit_command_to_telescope_expert(request): if request.method == 'POST': param = request.POST.get("commande_expert") if param: - os.system("echo " + param + " >> contenu") - return HttpResponse(json.dumps({'message': "Command send OK"})) + response = TelescopeRemoteControl(param, expert_mode=True).exec_command() + os.system("echo \"status :" + response + "\" >> /home/portos/IRAP/pyros/src/status") + return HttpResponse(json.dumps({'message': "Command send OK", 'response': response})) return HttpResponse(json.dumps({'message': "Missing command data"})) return redirect('submit_command_to_telescope') \ No newline at end of file diff --git a/src/devices/Device.py b/src/devices/Device.py index 7a67a6d..2ebd6ef 100644 --- a/src/devices/Device.py +++ b/src/devices/Device.py @@ -77,7 +77,7 @@ class DeviceController(): return False # (EP) NON, 0 = false !!! #return (0) - Log.objects.create(agent=self.name, message='Message send : ' + message) + Log.objects.create(agent=self.name, message='Message sent : ' + message) return True def isError(self, message: str): @@ -118,7 +118,9 @@ class DeviceController(): def blockAndReadBytes(self, size) -> str: self.sock.setblocking(1) - return (self.readBytes(size)) + message = self.readBytes(size) + Log.objects.create(agent=self.name, message='Message received : ' + message) + return (message) def blockAndReadMessage(self) -> str: return (self.blockAndReadBytes(1024)) @@ -131,7 +133,10 @@ class DeviceController(): # TODO maybe read more than 1024 bytes ????? def readMessage(self) -> str: - return self.readBytes(1024) + message = str(self.readBytes(1024)) + #os.system("echo lol >> /home/portos/IRAP/pyros/src/POURKOICAMARCHPA") + Log.objects.create(agent=self.name, message='Message received : ' + message) + return message def getConnection(self, device_name): self.ip = self.config.get(device_name, "ip") diff --git a/src/devices/Telescope.py b/src/devices/Telescope.py index ddad20f..30679dd 100644 --- a/src/devices/Telescope.py +++ b/src/devices/Telescope.py @@ -18,11 +18,15 @@ class TelescopeController(DeviceController): def set(self, command: str, *args): message = "SET " + command + " " + " ".join([str(arg) for arg in args]) - return (self.sendMessage(message)) + self.sendMessage(message) + response = self.blockAndReadMessage() + return response def do(self, command: str, *args): message = "DO " + command + " " + " ".join([str(arg) for arg in args]) - return (self.sendMessage(message)) + self.sendMessage(message) + response = self.blockAndReadMessage() + return response def getStatus(self): self.get("STATUS") diff --git a/src/devices/TelescopeRemoteControl.py b/src/devices/TelescopeRemoteControl.py new file mode 100644 index 0000000..9dd04c1 --- /dev/null +++ b/src/devices/TelescopeRemoteControl.py @@ -0,0 +1,49 @@ +from django.conf import settings +from common.models import Log +from devices.Telescope import TelescopeController + + +''' + Class managing the input from the remote commands sent by the dashboard + the command is parsed and sent to the Telescope via an instance of TelescopeController + +''' + +class TelescopeRemoteControl(): + _command = [] + _command_matcher = {} + def __init__(self, command, expert_mode): + self._command = command + if expert_mode: + self._command = str(command).split() + self._telescope = TelescopeController() + self._command_matcher = { + "GET": self.get, + "SET": self.set, + "DO": self.do, + } + + def exec_command(self): + if self._command[0] in self._command_matcher: + response = self._command_matcher[self._command[0]]() + else: + return "KO: Unknown command" + return response + + def get(self): + param_a = {"POSITION", "STATUS", "SETUP"} + if self._command[1] in param_a: + response = self._telescope.get(self._command[1]) + return response + return "KO: Unknown command" + + def set(self): + response = self._telescope.set(' '.join(self._command[1:])) + return "OK Command sent" + + def do(self): + response = self._telescope.do(' '.join(self._command[1:])) + return "OK command sent" + + def expert_command(self): + return True diff --git a/src/misc/static/js/command_control.js b/src/misc/static/js/command_control.js index ec2b5f7..4cc3e3f 100644 --- a/src/misc/static/js/command_control.js +++ b/src/misc/static/js/command_control.js @@ -98,7 +98,10 @@ jQuery(document).ready(function(){ {val: 'ABORT', text: 'ABORT'}, {val: 'HOMING', text: 'HOMING'}, {val: 'DOORS', text: 'DOORS'}, - {val: 'STOP', text: 'STOP'} + {val: 'GOTO', text: 'GOTO'}, + {val: 'STOP', text: 'STOP'}, + {val: 'OPEN DOME SHUTTER', text: 'OPEN DOME SHUTTER'}, + {val: 'CLOSE DOME SHUTTER', text: 'CLOSE DOME SHUTTER'} ]; var sel = $('', {id:"param2", name:"param2", placeholder:"Coords"}).appendTo('#command_form'); + selected.css("font-size", "25px"); + } + $('

').appendTo("#command_form"); $('', {id:"submit_button", type:"submit", value:"SEND"}).appendTo('#command_form'); @@ -168,8 +177,12 @@ $("#command_form_expert").submit(function(event){ event.preventDefault(); $.post('/dashboard/observation_status/send_command_to_telescope/submit_expert', $(this).serialize(), function(data){ - var obj = JSON.parse(data); - console.log(obj.message); }) + var obj = JSON.parse(data); + console.log(obj.message); + console.log(obj.response); + if (obj.response === "KO: Unknown command") + alert("Unknown command"); + }) .fail(function() { alert( "An error occured" ); }) -- libgit2 0.21.2