From 0626f26fea5316b632d0a84187508baed779aad6 Mon Sep 17 00:00:00 2001 From: MarieMih Date: Mon, 20 Nov 2023 16:17:49 +0300 Subject: [PATCH] upgrade_both_HW3_HW4 --- controls/__init__.py | 4 ++-- equipment/device.py | 3 +-- equipment/turtle_device.py | 37 ++++++++++++++++++++++++++----------- noblocking_turtle_shell.py | 13 +++++++++---- turtle_shell.py | 1 + 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/controls/__init__.py b/controls/__init__.py index 35e48d4..5458f05 100644 --- a/controls/__init__.py +++ b/controls/__init__.py @@ -1,4 +1,4 @@ -*** +""" SCADA (Supervisory Control And Data Acquisition) SCADA-системы нужны для организации сбора данных в реальном времени, @@ -11,4 +11,4 @@ SCADA-системы решают следующие задачи: Ведение базы данных с технологической информацией. Аварийная сигнализация и управление служебными сообщениями о неполадках. Подготовка и генерирование отчетов о ходе технологического процесса. -*** +""" diff --git a/equipment/device.py b/equipment/device.py index ea4739a..26f4820 100644 --- a/equipment/device.py +++ b/equipment/device.py @@ -1,7 +1,6 @@ from dataclasses import dataclass from typing import Optional, Collection, Any from abc import ABC, abstractmethod - from enum import IntEnum class DeviceLifecycleState(IntEnum): @@ -58,7 +57,7 @@ class Device(ABC): def action_descriptors(self) -> Collection[ActionDescriptor]: pass - #@abstractmethod + @abstractmethod def __getitem__(self, trait_name: str) -> Optional[Any]: """Return logical state of trait `trait_name`.""" pass diff --git a/equipment/turtle_device.py b/equipment/turtle_device.py index b25aef7..8870ec8 100644 --- a/equipment/turtle_device.py +++ b/equipment/turtle_device.py @@ -1,18 +1,18 @@ +import time from turtle import Turtle -from typing import Any from equipment.device import SynchronyDevice +from equipment.device import Device import inspect -import types +from typing import Optional, Collection, Any class TurtleDevice(SynchronyDevice): def open(self): super().open() - return Turtle() + self.turtle = Turtle() - @staticmethod - def close (exam): + def close (self): super().close() - del exam + del self.turtle @property def trait_descriptors(self): @@ -43,20 +43,35 @@ class TurtleDevice(SynchronyDevice): def execute(self, action_name: str, *args, **kwargs): """Execute action `action_name`, using `args` and `kwargs` as action argument.""" - pass + if self.state == 1: + f = getattr(self.turtle, action_name) + f(int(*args), **kwargs) + else: + print("It's not opened") + self.open() + #time.sleep(30) + f = getattr(self.turtle, action_name) + f(int(*args), **kwargs) def read(self, trait_name: str): """Read physical state of trait `trait_name` from device.""" - pass + return getattr(self.turtle, trait_name) def write(self, trait_name: str, value: Any) -> bool: """Pass `value` to trait `trait_name` of device.""" - pass + setattr(self.turtle, trait_name, value) def invalidate(self, trait_name: str): """Invalidate logical state of trait `trait_name`""" - pass + if self.read(trait_name) == self.__getitem__(trait_name): + print("Everything's okey") + else: + print(f"Something went wrong with {trait_name}.") + def __getitem__(self, trait_name: str) -> Optional[Any]: + """Return logical state of trait `trait_name`.""" + return getattr(self, trait_name) +#print(inspect.getmembers(Turtle)) #t = TurtleDevice() -#print(t.trait_descriptors) +#print(t.execute("forward", 3)) diff --git a/noblocking_turtle_shell.py b/noblocking_turtle_shell.py index 34e2302..1e13129 100644 --- a/noblocking_turtle_shell.py +++ b/noblocking_turtle_shell.py @@ -2,6 +2,7 @@ import cmd import threading from threading import Thread from threading import Event +import argparse import time from queue import Queue @@ -15,8 +16,10 @@ class TurtleDeviceThread(threading.Thread): def __message_processing__(self): while True: new_message = self.queue.get() - print("Name and args of action are {}".format(new_message)) - #self.device. + #print(type(new_message)) + #print(new_message) + #print("Name and args of action are {}".format(new_message)) + self.device.execute(*new_message) #time.sleep(30) self.queue.task_done() if self.event.is_set(): @@ -46,7 +49,7 @@ class NoBlockingTurtleShell(cmd.Cmd): def do_execute(self, arg): # TODO(Homework 4) \begin - turtle_thread.queue.put(arg) + turtle_thread.queue.put(parse(arg)) # TODO(Homework 4) \end def do_exit(self, arg): @@ -54,7 +57,9 @@ class NoBlockingTurtleShell(cmd.Cmd): turtle_thread.event.set() # TODO(Homework 4) \end - +def parse(arg): + 'Convert a series of zero or more numbers to an argument tuple' + return tuple(arg.split()) if __name__ == '__main__': turtle_thread = TurtleDeviceThread() diff --git a/turtle_shell.py b/turtle_shell.py index ac22aab..20464cf 100644 --- a/turtle_shell.py +++ b/turtle_shell.py @@ -10,6 +10,7 @@ class TurtleShell(cmd.Cmd): # ----- basic turtle commands ----- def do_forward(self, arg): 'Move the turtle forward by the specified distance: FORWARD 10' + forward(*parse(arg)) def do_right(self, arg): 'Turn turtle right by given number of degrees: RIGHT 20'