From b10d4e32fed8597b6ac2c9634efbe3594de5b9b4 Mon Sep 17 00:00:00 2001 From: ototadana Date: Sat, 18 Jun 2022 18:04:34 +0900 Subject: [PATCH] add type hints --- dashboard.py | 7 +++++-- drone_command_requester.py | 7 +++++-- drone_state_receiver.py | 10 +++++++--- drone_video_receiver.py | 7 +++++-- info.py | 40 +++++++++++++++++++++++++------------- runnable.py | 6 ++++++ runner.py | 6 +++++- startable.py | 6 ++++++ 8 files changed, 65 insertions(+), 24 deletions(-) create mode 100644 runnable.py create mode 100644 startable.py diff --git a/dashboard.py b/dashboard.py index 5ebf302..87c8104 100644 --- a/dashboard.py +++ b/dashboard.py @@ -3,9 +3,12 @@ import PySimpleGUI as sg from PIL import Image, ImageTk +from info import Info +from runnable import Runnable -class Dashboard: - def run(self, info): + +class Dashboard(Runnable): + def run(self, info: Info) -> None: DISPLAY_SIZE = (800, 600) layout = [ [sg.Image(filename="", key="image", size=DISPLAY_SIZE)], diff --git a/drone_command_requester.py b/drone_command_requester.py index ad2f7f1..669f744 100644 --- a/drone_command_requester.py +++ b/drone_command_requester.py @@ -1,9 +1,12 @@ import socket import time +from info import Info +from startable import Startable -class DroneCommandRequester: - def start(self, info): + +class DroneCommandRequester(Startable): + def start(self, info: Info) -> None: DRONE_ADDRESS = ("192.168.10.1", 8889) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind(("", 8889)) diff --git a/drone_state_receiver.py b/drone_state_receiver.py index fa891f6..4950153 100644 --- a/drone_state_receiver.py +++ b/drone_state_receiver.py @@ -1,8 +1,12 @@ +from __future__ import annotations import socket +from info import Info +from startable import Startable -class DroneStateReceiver: - def start(self, info): + +class DroneStateReceiver(Startable): + def start(self, info: Info) -> None: state_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) state_sock.bind(("", 8890)) @@ -14,7 +18,7 @@ def start(self, info): print("\nExit . . .\n") break - def __get_drone_state(self, data): + def __get_drone_state(self, data: bytes) -> dict[str, float]: s = data.decode(errors="replace") values = s.split(";") state = {} diff --git a/drone_video_receiver.py b/drone_video_receiver.py index 3833451..3691004 100644 --- a/drone_video_receiver.py +++ b/drone_video_receiver.py @@ -1,8 +1,11 @@ import cv2 +from info import Info +from startable import Startable -class DroneVideoReceiver: - def start(self, info): + +class DroneVideoReceiver(Startable): + def start(self, info: Info) -> None: cap = cv2.VideoCapture("udp://0.0.0.0:11111?overrun_nonfatal=1") while info.is_active(): diff --git a/info.py b/info.py index b260c06..a7983c5 100644 --- a/info.py +++ b/info.py @@ -1,5 +1,17 @@ +from __future__ import annotations + +from cv2 import Mat + + class Info: - def __init__(self): + __state: dict[str, float] + __is_active: bool + __image: Mat + __command: str + __sent_command: str + __result: str + + def __init__(self) -> None: self.__state = {} self.__is_active = True self.__image = None @@ -7,43 +19,43 @@ def __init__(self): self.__sent_command = "" self.__result = "" - def set_states(self, states): + def set_states(self, states: dict[str, float]) -> None: self.__state = states - def get_states(self): + def get_states(self) -> dict[str, float]: return self.__state - def get_state(self, name): + def get_state(self, name: str) -> float: return self.__state.get(name, 0.0) - def is_active(self): + def is_active(self) -> bool: return self.__is_active - def stop(self): + def stop(self) -> None: self.__is_active = False - def set_image(self, image): + def set_image(self, image: Mat) -> None: self.__image = image - def get_image(self): + def get_image(self) -> Mat: return self.__image - def entry_command(self, command): + def entry_command(self, command: str) -> None: self.__command = command - def pick_command(self): + def pick_command(self) -> str: command = self.__command self.__command = "" return command - def set_sent_command(self, command): + def set_sent_command(self, command: str) -> None: self.__sent_command = command - def get_sent_command(self): + def get_sent_command(self) -> str: return self.__sent_command - def set_command_result(self, result): + def set_command_result(self, result: str) -> None: self.__result = result - def get_command_result(self): + def get_command_result(self) -> str: return self.__result diff --git a/runnable.py b/runnable.py new file mode 100644 index 0000000..02acca2 --- /dev/null +++ b/runnable.py @@ -0,0 +1,6 @@ +from info import Info + + +class Runnable: + def run(self, info: Info) -> None: + pass diff --git a/runner.py b/runner.py index 61715bf..9b3a73f 100644 --- a/runner.py +++ b/runner.py @@ -1,10 +1,14 @@ +from __future__ import annotations + from threading import Thread from info import Info +from runnable import Runnable +from startable import Startable class Runner: - def run(self, startables, runnable): + def run(self, startables: list[Startable], runnable: Runnable) -> None: info = Info() for startable in startables: diff --git a/startable.py b/startable.py new file mode 100644 index 0000000..bdd254a --- /dev/null +++ b/startable.py @@ -0,0 +1,6 @@ +from info import Info + + +class Startable: + def start(self, info: Info) -> None: + pass