diff --git a/fetapd.py b/fetapd.py index fb3c25f..1f7740f 100644 --- a/fetapd.py +++ b/fetapd.py @@ -1,6 +1,7 @@ import time from phoneinterface import PhoneInterface, PhoneEvent from apparatinterface import FeApPinConfiguration, FeApUserInterface +from webinterface import FeTapWeb import configreader import statemachine @@ -70,9 +71,10 @@ if __name__ == '__main__': cfg = configreader.ConfigurationReader() cfg.read('fetap.ini') - phone = PhoneInterface(cfg.phoneconfig, webinterface_enabled=True, api_enabled=True) + phone = PhoneInterface(cfg.phoneconfig) feap = FeApUserInterface(cfg.pinconfig) controller = statemachine.StateMachineController(phone, feap, cfg.dialconfig) + webinterface = FeTapWeb({}, controller) # TODO: Use real events from daemon controller.queue_event('registration_in_progress') @@ -84,6 +86,7 @@ if __name__ == '__main__': phone.add_event_cb(phone_cb) phone.start() + webinterface.start() try: while True: time.sleep(1) diff --git a/phoneinterface.py b/phoneinterface.py index a2fe56c..ec0204c 100644 --- a/phoneinterface.py +++ b/phoneinterface.py @@ -46,9 +46,7 @@ class PhoneEvent(object): class PhoneInterface(object): - def __init__(self, config, webinterface_enabled = False, api_enabled = False): - self.webinterface_enabled = webinterface_enabled - self.api_enabled = api_enabled + def __init__(self, config): self.__event_cbs = [] self.__config = config @@ -83,10 +81,6 @@ class PhoneInterface(object): #self.__core.video_capture_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): print(PhoneEvent.string(evt)) @@ -110,16 +104,10 @@ class PhoneInterface(object): self.__core.process_event() time.sleep(0.2) # Value for good measure - def __webthread(self): - self.webinterface.start("0.0.0.0", 8080) - def start(self): self.__running = True t = threading.Thread(target=self.__pollthread) t.start() - if self.webinterface_enabled: - w = threading.Thread(target=self.__webthread) - w.start() def stop(self): self.stop_playing() diff --git a/statemachine.py b/statemachine.py index 04825c4..9e30a25 100644 --- a/statemachine.py +++ b/statemachine.py @@ -277,7 +277,7 @@ class StateMachineController(object): self.feap = feap self.dialconfig = dialconfig - self.__state = InitState(self) + self.state = InitState(self) self.__timeout = None @@ -293,20 +293,20 @@ class StateMachineController(object): return print('!!! event: %s' % (evname)) - handler = getattr(self.__state, 'on_%s' % (evname)) + handler = getattr(self.state, 'on_%s' % (evname)) try: newstate = handler(*evargs, **evkwargs) except IllegalEventError: - print('illegal event occured!!!!!!!!!!!!!!!!!!!!', self.__state.__class__.__name__) + print('illegal event occured!!!!!!!!!!!!!!!!!!!!', self.state.__class__.__name__) if not newstate: continue - self.__state.leave() + self.state.leave() self.abort_timeout() - oldstate = self.__state.__class__ + oldstate = self.state.__class__ print('%s -> %s' % (oldstate.__name__, newstate.__name__)) - self.__state = newstate(self) + self.state = newstate(self) def queue_event(self, evname, *evargs, **evkwargs): if not hasattr(AbstractState, 'on_%s' % (evname)): diff --git a/webinterface.py b/webinterface.py index bafd3d7..9ea48e7 100644 --- a/webinterface.py +++ b/webinterface.py @@ -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(): - 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('/')(self.statics) pass - @bottle.route("/") - def root(): + def root(self): return "

Hello world!

" - def start(self, host, port): - bottle.run(host=host, port=port) + def publish_status(self): + 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) + diff --git a/webinterface/.index.html.swp b/webinterface/.index.html.swp new file mode 100644 index 0000000..e2c125d Binary files /dev/null and b/webinterface/.index.html.swp differ diff --git a/webinterface/css/style.css b/webinterface/css/style.css new file mode 100644 index 0000000..e69de29 diff --git a/webinterface/index.html b/webinterface/index.html new file mode 100644 index 0000000..32384e6 --- /dev/null +++ b/webinterface/index.html @@ -0,0 +1,12 @@ + + + + + FeTap Webinterface + + + + + + + diff --git a/webinterface/js/.scripts.js.swp b/webinterface/js/.scripts.js.swp new file mode 100644 index 0000000..4af748b Binary files /dev/null and b/webinterface/js/.scripts.js.swp differ diff --git a/webinterface/js/scripts.js b/webinterface/js/scripts.js new file mode 100644 index 0000000..5f0b70f --- /dev/null +++ b/webinterface/js/scripts.js @@ -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);