Add interactive control interface

This commit is contained in:
clerie 2022-06-25 17:03:13 +02:00
parent 1046135e54
commit ae9d73659a
4 changed files with 73 additions and 0 deletions

View File

@ -1,5 +1,15 @@
#!/usr/bin/env python3
class ControllerConfig:
def __init__(self, c):
self._c = c
def __getattr__(self, name):
if name in self._c.keys():
return self._c.get(name)
else:
raise AttributeError()
class DectConfig:
def __init__(self, c):
self._c = c
@ -17,6 +27,7 @@ class DectConfig:
class Config:
def __init__(self, c):
self._c = c
self.controller = ControllerConfig(c.get("controller", {}))
self.dect = DectConfig(c.get("dect", {}))
def check(self):

51
fieldpoc/controller.py Normal file
View File

@ -0,0 +1,51 @@
#!/usr/bin/env python3
import socketserver
import time
import threading
class Controller:
def __init__(self, fp):
self.fp = fp
def get_handler(self):
class HandleRequest(socketserver.BaseRequestHandler):
fp = self.fp
def handle(self):
self.request.sendall("FieldPOC interactive controller\n".encode("utf-8"))
while True:
self.request.sendall("> ".encode("utf-8"))
data = self.request.recv(1024).decode("utf-8").strip()
if data == "help":
self.request.sendall("""Availiable commands:
help Show this info
stop Shutdown FieldPOC
exit Disconnect
""".encode("utf-8"))
elif data == "quit" or data == "exit":
break
elif data == "stop":
self.fp.stop.set()
break
else:
self.request.sendall("Unknown command, type 'help'\n".encode("utf-8"))
self.request.sendall("disconnecting\n".encode("utf-8"))
return HandleRequest
def run(self):
with socketserver.ThreadingTCPServer((self.fp.config.controller.host, self.fp.config.controller.port), self.get_handler()) as server:
threading.Thread(target=server.serve_forever).start()
self.fp.stop.wait()
print("stopping interactive controller")
server.shutdown()
print("stopped interactive controller")

View File

@ -5,6 +5,7 @@ import pathlib
import threading
from . import config
from . import controller
from . import dect
class FieldPOC:
@ -17,9 +18,15 @@ class FieldPOC:
self.extensions_file_path = pathlib.Path(extensions_file_path)
self._load_extensions()
self.stop = threading.Event()
self._controller = controller.Controller(self)
self._dect = dect.Dect(self)
def run(self):
self._controller_thread = threading.Thread(target=self._controller.run)
self._controller_thread.start()
self._dect_thread = threading.Thread(target=self._dect.run)
self._dect_thread.start()

View File

@ -1,4 +1,8 @@
{
"controller": {
"host": "127.0.0.1",
"port": 9437
},
"dect": {
"host": "192.168.0.100",
"username": "omm",