Move webinterface, add websockets, more PoC code

This commit is contained in:
sqozz 2021-02-24 18:30:15 +01:00
parent 9878acd9b6
commit ccb76fd306
9 changed files with 83 additions and 26 deletions

View file

@ -1,6 +1,7 @@
import time import time
from phoneinterface import PhoneInterface, PhoneEvent from phoneinterface import PhoneInterface, PhoneEvent
from apparatinterface import FeApPinConfiguration, FeApUserInterface from apparatinterface import FeApPinConfiguration, FeApUserInterface
from webinterface import FeTapWeb
import configreader import configreader
import statemachine import statemachine
@ -70,9 +71,10 @@ if __name__ == '__main__':
cfg = configreader.ConfigurationReader() cfg = configreader.ConfigurationReader()
cfg.read('fetap.ini') cfg.read('fetap.ini')
phone = PhoneInterface(cfg.phoneconfig, webinterface_enabled=True, api_enabled=True) phone = PhoneInterface(cfg.phoneconfig)
feap = FeApUserInterface(cfg.pinconfig) feap = FeApUserInterface(cfg.pinconfig)
controller = statemachine.StateMachineController(phone, feap, cfg.dialconfig) controller = statemachine.StateMachineController(phone, feap, cfg.dialconfig)
webinterface = FeTapWeb({}, controller)
# TODO: Use real events from daemon # TODO: Use real events from daemon
controller.queue_event('registration_in_progress') controller.queue_event('registration_in_progress')
@ -84,6 +86,7 @@ if __name__ == '__main__':
phone.add_event_cb(phone_cb) phone.add_event_cb(phone_cb)
phone.start() phone.start()
webinterface.start()
try: try:
while True: while True:
time.sleep(1) time.sleep(1)

View file

@ -46,9 +46,7 @@ class PhoneEvent(object):
class PhoneInterface(object): class PhoneInterface(object):
def __init__(self, config, webinterface_enabled = False, api_enabled = False): def __init__(self, config):
self.webinterface_enabled = webinterface_enabled
self.api_enabled = api_enabled
self.__event_cbs = [] self.__event_cbs = []
self.__config = config self.__config = config
@ -83,10 +81,6 @@ class PhoneInterface(object):
#self.__core.video_capture_enabled = False #self.__core.video_capture_enabled = False
#self.__core.video_display_enabled = False #self.__core.video_display_enabled = False
if self.webinterface_enabled:
from webinterface import FeTapWeb
self.webinterface = FeTapWeb(api_enabled=api_enabled)
def run_callbacks(self, evt): def run_callbacks(self, evt):
print(PhoneEvent.string(evt)) print(PhoneEvent.string(evt))
@ -110,16 +104,10 @@ class PhoneInterface(object):
self.__core.process_event() self.__core.process_event()
time.sleep(0.2) # Value for good measure time.sleep(0.2) # Value for good measure
def __webthread(self):
self.webinterface.start("0.0.0.0", 8080)
def start(self): def start(self):
self.__running = True self.__running = True
t = threading.Thread(target=self.__pollthread) t = threading.Thread(target=self.__pollthread)
t.start() t.start()
if self.webinterface_enabled:
w = threading.Thread(target=self.__webthread)
w.start()
def stop(self): def stop(self):
self.stop_playing() self.stop_playing()

View file

@ -277,7 +277,7 @@ class StateMachineController(object):
self.feap = feap self.feap = feap
self.dialconfig = dialconfig self.dialconfig = dialconfig
self.__state = InitState(self) self.state = InitState(self)
self.__timeout = None self.__timeout = None
@ -293,20 +293,20 @@ class StateMachineController(object):
return return
print('!!! event: %s' % (evname)) print('!!! event: %s' % (evname))
handler = getattr(self.__state, 'on_%s' % (evname)) handler = getattr(self.state, 'on_%s' % (evname))
try: try:
newstate = handler(*evargs, **evkwargs) newstate = handler(*evargs, **evkwargs)
except IllegalEventError: except IllegalEventError:
print('illegal event occured!!!!!!!!!!!!!!!!!!!!', self.__state.__class__.__name__) print('illegal event occured!!!!!!!!!!!!!!!!!!!!', self.state.__class__.__name__)
if not newstate: if not newstate:
continue continue
self.__state.leave() self.state.leave()
self.abort_timeout() self.abort_timeout()
oldstate = self.__state.__class__ oldstate = self.state.__class__
print('%s -> %s' % (oldstate.__name__, newstate.__name__)) print('%s -> %s' % (oldstate.__name__, newstate.__name__))
self.__state = newstate(self) self.state = newstate(self)
def queue_event(self, evname, *evargs, **evkwargs): def queue_event(self, evname, *evargs, **evkwargs):
if not hasattr(AbstractState, 'on_%s' % (evname)): if not hasattr(AbstractState, 'on_%s' % (evname)):

View file

@ -1,12 +1,46 @@
import bottle from bottle import route, get, static_file, run
from bottle.ext.websocket import GeventWebSocketServer
from bottle.ext.websocket import websocket
from os.path import join as pathjoin
import threading
class FeTapWeb(): class FeTapWeb():
def __init__(self, api_enabled=False): def __init__(self, config, statemachine_controller):
self.config = config
self.controller = statemachine_controller
self.config = {"host": "0.0.0.0", "port": 8080}
self.websockets = []
route("/")(self.root)
get("/ws", apply=[websocket])(self.ws_connect)
route('/<path:path>')(self.statics)
pass pass
@bottle.route("/") def root(self):
def root():
return "<h1>Hello world!</h1>" return "<h1>Hello world!</h1>"
def start(self, host, port): def publish_status(self):
bottle.run(host=host, port=port) for ws in self.websockets:
ws.send("this is a message triggered from the server")
return "ok"
def ws_connect(self, ws):
self.websockets.append(ws)
while True:
data = ws.receive()
if data != None:
print(data)
else:
break
self.websockets.remove(ws)
def statics(self, path):
return static_file(path, root="./webinterface")
def start(self):
self.__running = True
w = threading.Thread(target=self.__webthread)
w.start()
def __webthread(self):
run(host=self.config["host"], port=self.config["port"], server=GeventWebSocketServer)

Binary file not shown.

View file

12
webinterface/index.html Normal file
View file

@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>FeTap Webinterface</title>
<meta name="description" content="The official FeTap Webinterface">
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<script src="js/scripts.js"></script>
</body>
</html>

Binary file not shown.

View file

@ -0,0 +1,20 @@
function status_update(ev) {
console.log("Got message from server: " + ev.data);
}
function ws_connected(ev) {
console.log("Connection to fetapi now active!");
ev.target.onmessage = status_update;
ev.target.send("test");
}
function connect_ws() {
webSocket = new WebSocket("ws://" + window.location.hostname + ":" + window.location.port + "/ws");
webSocket.onopen = ws_connected;
}
function init() {
connect_ws();
}
document.addEventListener("DOMContentLoaded", init);