fetapi/webinterface.py

66 lines
2.2 KiB
Python
Raw Normal View History

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
2021-02-24 22:29:41 +01:00
from statemachine import InitState, RegisteringState, IdleState, SchelltState, AcceptingState, CallTerminatingState, ForgottenState, BusyBeepingState, CallRunningState, WecktState, ConnectingState, DialingState
import threading
2021-02-24 23:34:26 +01:00
import json
2021-02-22 15:39:57 +01:00
class FeTapWeb():
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)
2021-02-22 15:39:57 +01:00
pass
def root(self):
2021-02-22 15:39:57 +01:00
return "<h1>Hello world!</h1>"
2021-02-25 17:19:59 +01:00
def on_event(self, state, event, evargs, evkwargs):
if event == "incoming_call":
remote_number = self.controller.phone.get_remote_number()
self.send_event({"event": "incoming_call", "data": {"from": remote_number}})
elif event == "nummernschalter_input":
self.send_event({"event": "num_entered", "data": {"number": int(evargs[0])}})
else:
pass
print(state, event, evargs, evkwargs)
def on_transition(self, ostate, nstate, event, evargs, evkwargs):
state_name = nstate.__class__.__name__
2021-02-24 23:47:38 +01:00
data = {"state": state_name}
2021-02-25 17:19:59 +01:00
self.send_event(data)
print(ostate, nstate, event, evargs, evkwargs)
2021-02-24 23:34:26 +01:00
def send_event(self, js_event):
data = json.dumps(js_event)
for ws in self.websockets:
ws.send(data)
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)