Add interactive control interface
parent
1046135e54
commit
ae9d73659a
@ -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")
|
Loading…
Reference in New Issue