From e6a2f6eac73f4b95c7c368476f3122ba97a7c97a Mon Sep 17 00:00:00 2001 From: ilia Date: Thu, 26 Oct 2023 17:28:48 +0300 Subject: [PATCH] fill methods --- turtle/equipment/turtle_device.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/turtle/equipment/turtle_device.py b/turtle/equipment/turtle_device.py index 8de0663..27ee251 100644 --- a/turtle/equipment/turtle_device.py +++ b/turtle/equipment/turtle_device.py @@ -3,11 +3,14 @@ from typing import Optional, Collection, Any from controls.device import SynchronyDevice from controls.device import TraitDescriptor from controls.device import ActionDescriptor +from controls.device import DeviceError import inspect + class TurtleDevice(SynchronyDevice): def open(self): self.turtle = Turtle() + self.descriptors = dict(traits=dict(), actions=dict()) super().open() def close(self): @@ -22,20 +25,36 @@ class TurtleDevice(SynchronyDevice): def __getitem__(self, trait_name: str) -> Optional[Any]: """Return logical state of trait `trait_name`.""" - pass + return self.descriptors[trait_name] + + def read_descriptors(self, arg): + self.descriptors = self.get_descriptors() + return self.descriptors def read(self, trait_name: str) -> Any: - pass + """Read physical state of trait `trait_name` from device.""" + self.read_descriptors() + return self.descriptors[trait_name] def write(self, trait_name: str, value: Any) -> bool: - self.turtle.write() + """Turtle traits are not writable""" + # trait_desc = self.trait_descriptors.get(trait_name) + # if trait_desc.writable: + # return getattr(self.turtle, trait_name)(value) + super().write() def execute(self, action_name: str, *args, **kwargs): """Execute action `action_name`, using `args` and `kwargs` as action argument.""" - pass + action = self.get_descriptors()["actions"].get(action_name) + if action: + return getattr(self.turtle, action_name)(*args, **kwargs) + super().execute() def invalidate(self, trait_name: str): - pass + """Invalidate logical state of trait `trait_name`""" + if self.trait_descriptors.get(trait_name): + self.trait_descriptors[trait_name] = None + raise DeviceError() def get_descriptors(self): descriptors = dict(actions=dict(), traits=dict())