Skip to content

Commit

Permalink
add type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
ototadana committed Jun 18, 2022
1 parent 4d67283 commit b10d4e3
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 24 deletions.
7 changes: 5 additions & 2 deletions dashboard.py
Expand Up @@ -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)],
Expand Down
7 changes: 5 additions & 2 deletions 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))
Expand Down
10 changes: 7 additions & 3 deletions 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))

Expand All @@ -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 = {}
Expand Down
7 changes: 5 additions & 2 deletions 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():
Expand Down
40 changes: 26 additions & 14 deletions info.py
@@ -1,49 +1,61 @@
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
self.__command = ""
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
6 changes: 6 additions & 0 deletions runnable.py
@@ -0,0 +1,6 @@
from info import Info


class Runnable:
def run(self, info: Info) -> None:
pass
6 changes: 5 additions & 1 deletion 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:
Expand Down
6 changes: 6 additions & 0 deletions startable.py
@@ -0,0 +1,6 @@
from info import Info


class Startable:
def start(self, info: Info) -> None:
pass

0 comments on commit b10d4e3

Please sign in to comment.