Commit 1b4dae11892163776d428f70a34aa962c1d89c52
1 parent
e0098e4f
Exists in
dev
Nouvelle classe (dataclass) "Cmd" pour les commandes
Showing
2 changed files
with
74 additions
and
9 deletions
Show diff stats
src/device_controller/abstract_component/device_controller.py
... | ... | @@ -250,6 +250,17 @@ class UnexpectedCommandReturnCode(Exception): |
250 | 250 | class TimeoutException(Exception): |
251 | 251 | pass |
252 | 252 | |
253 | +from dataclasses import dataclass, field | |
254 | +from typing import Dict | |
255 | +@dataclass | |
256 | +class Cmd: | |
257 | + generic_name: str = 'generic name' | |
258 | + native_name: str = 'native name' | |
259 | + params: Dict[str, str] = field(default_factory=dict) | |
260 | + final_device_responses: Dict[str, str] = field(default_factory=dict) | |
261 | + final_simul_response: str = 'simulator response' | |
262 | + immediate_responses: Dict[str, str] = field(default_factory=dict) | |
263 | + errors: Dict[str, str] = field(default_factory=dict) | |
253 | 264 | |
254 | 265 | class Gen2NatCmds: |
255 | 266 | # To be set by constructor |
... | ... | @@ -262,13 +273,14 @@ class Gen2NatCmds: |
262 | 273 | |
263 | 274 | def __str__(self)->str: return str(self.GEN2NAT_CMDS) |
264 | 275 | |
265 | - | |
266 | - def build_cmd(self, generic_name:str, native_name:str, | |
267 | - params:dict = {}, | |
268 | - final_device_responses:dict = {}, | |
269 | - final_simul_response:str = '', | |
270 | - immediate_responses:dict = {}, | |
271 | - errors:dict = {}, | |
276 | + # build cmd as dict | |
277 | + def build_cmd(self, | |
278 | + generic_name: str, native_name: str, | |
279 | + params: dict = {}, | |
280 | + final_device_responses: dict = {}, | |
281 | + final_simul_response: str = '', | |
282 | + immediate_responses: dict = {}, | |
283 | + errors: dict = {}, | |
272 | 284 | )->dict: |
273 | 285 | cmd = { |
274 | 286 | 'generic_name': generic_name, |
... | ... | @@ -282,6 +294,8 @@ class Gen2NatCmds: |
282 | 294 | 'errors': errors |
283 | 295 | } |
284 | 296 | return cmd |
297 | + | |
298 | + | |
285 | 299 | # TODO: |
286 | 300 | def build_cmd_get_set(self, generic_name:str, native_get_name:str, native_set_name:str)->(dict,dict): |
287 | 301 | get_cmd = set_cmd = {} |
... | ... | @@ -292,6 +306,8 @@ class Gen2NatCmds: |
292 | 306 | |
293 | 307 | def add_cmd(self, cmd:dict): |
294 | 308 | self.cmds[cmd['generic_name']] = cmd |
309 | + def add_cmd2(self, cmd:Cmd): | |
310 | + self.cmds[cmd.generic_name] = cmd | |
295 | 311 | |
296 | 312 | def print_mes_commandes(self): |
297 | 313 | pprint.sorted = lambda x, key=None: x | ... | ... |
src/device_controller/concrete_component/gemini/gemini_controller.py
... | ... | @@ -18,7 +18,12 @@ import time |
18 | 18 | sys.path.append('../../..') |
19 | 19 | |
20 | 20 | # My parent class and exceptions |
21 | -from device_controller.abstract_component.device_controller import printd, DeviceController, Gen2NatCmds, UnknownNativeCmdException, UnknownGenericCmdArgException | |
21 | +from device_controller.abstract_component.device_controller import ( | |
22 | + printd, | |
23 | + DeviceController, | |
24 | + Gen2NatCmds, Cmd, | |
25 | + UnknownNativeCmdException, UnknownGenericCmdArgException | |
26 | +) | |
22 | 27 | |
23 | 28 | #from src.client.socket_client_telescope_abstract import Position, UnknownNativeCmdException, TimeoutException, SocketClientTelescopeAbstract |
24 | 29 | ##from src_socket.client.socket_client_telescope_abstract import * |
... | ... | @@ -224,7 +229,11 @@ class DC_Gemini(DeviceController): |
224 | 229 | ''' Commands dictionary |
225 | 230 | ''' |
226 | 231 | |
232 | + # | |
227 | 233 | # VERSION 1 : 1 gros dictionnaire par commande |
234 | + # CON : long et error prone | |
235 | + # | |
236 | + | |
228 | 237 | get_ack = { |
229 | 238 | 'generic_name': 'get_ack', |
230 | 239 | 'native_name': Protocol.COMMAND6, |
... | ... | @@ -267,7 +276,11 @@ class DC_Gemini(DeviceController): |
267 | 276 | } |
268 | 277 | } |
269 | 278 | |
279 | + # | |
270 | 280 | # VERSION 2 : on utilise la methode build_cmd() en précisant key=val à chaque paramètre (sauf les paramètres vides, par défaut) |
281 | + # PRO: court, pas d'erreur possible | |
282 | + # CON: appel de fonction | |
283 | + | |
271 | 284 | mes_commandes = Gen2NatCmds({}) |
272 | 285 | get_ack = mes_commandes.build_cmd( |
273 | 286 | generic_name = 'get_ack', |
... | ... | @@ -292,7 +305,10 @@ class DC_Gemini(DeviceController): |
292 | 305 | ) |
293 | 306 | mes_commandes.add_cmd(get_ack) |
294 | 307 | |
308 | + # | |
295 | 309 | # VERSION 3 (+court): on utilise la methode build_cmd() SANS préciser le nom des param => il faut alors les rentrer TOUS |
310 | + # | |
311 | + | |
296 | 312 | get_ack = mes_commandes.build_cmd( |
297 | 313 | 'get_ack', |
298 | 314 | Protocol.COMMAND6, |
... | ... | @@ -314,16 +330,49 @@ class DC_Gemini(DeviceController): |
314 | 330 | '1' : 'pb 1 ...', |
315 | 331 | } |
316 | 332 | ) |
333 | + | |
317 | 334 | # Commande vide |
318 | 335 | cmd2 = mes_commandes.build_cmd('cmd2_generic', 'cmd2_native') |
319 | 336 | |
320 | - # VERSION 4 : OPTIMISATION des commandes get et set : on construit les 2 en même temps, et ca génère 2 commandes différentes (un get et un set) | |
337 | + # | |
338 | + # OPTIMISATION possible pour les commandes get et set : | |
339 | + # on construit les 2 en même temps, et ca génère 2 commandes différentes (un get et un set) | |
340 | + # | |
321 | 341 | get_dec, set_dec = mes_commandes.build_cmd_get_set(generic_name='dec', native_get_name='GD', native_set_name='Sd') |
322 | 342 | do_init = mes_commandes.build_cmd_do('do_init', 'titi') |
323 | 343 | |
324 | 344 | |
345 | + # | |
346 | + # VERSION 4 : Command class (Cmd) | |
347 | + # | |
348 | + get_ack2 = Cmd( | |
349 | + #generic_name = | |
350 | + 'get_ack2', | |
351 | + #native_name = | |
352 | + Protocol.COMMAND6, | |
353 | + {}, | |
354 | + { | |
355 | + 'G' : 'after completed startup', | |
356 | + 'B' : 'while the initial startup message is being displayed (new in L4)', | |
357 | + 'b' : 'while waiting for the selection of the Startup Mode', | |
358 | + 'S' : 'during a Cold Start (new in L4)', | |
359 | + }, | |
360 | + 'G', | |
361 | + { | |
362 | + 'ready' : 'gg', | |
363 | + 'wait' : 10, | |
364 | + 'error' : '', | |
365 | + }, | |
366 | + { | |
367 | + '0' : 'pb 0 ...', | |
368 | + '1' : 'pb 1 ...', | |
369 | + } | |
370 | + ) | |
371 | + | |
325 | 372 | |
373 | + # Utilisation, affichage | |
326 | 374 | mes_commandes.add_cmd(get_ack) |
375 | + mes_commandes.add_cmd2(get_ack2) | |
327 | 376 | mes_commandes.add_cmd(cmd2) |
328 | 377 | mes_commandes.add_cmd(set_ra) |
329 | 378 | ... | ... |