aboutsummaryrefslogtreecommitdiffstats
path: root/simulator/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'simulator/__init__.py')
-rw-r--r--simulator/__init__.py38
1 files changed, 33 insertions, 5 deletions
diff --git a/simulator/__init__.py b/simulator/__init__.py
index e81ae9a..5fc1366 100644
--- a/simulator/__init__.py
+++ b/simulator/__init__.py
@@ -1,5 +1,4 @@
import random
-import serial
import time
import sys
@@ -178,8 +177,37 @@ def run(port):
print("unexpected message", msg)
+class WSPort:
+ def __init__(self, ws):
+ self.ws = ws
+ self.in_waiting = 0
+
+ def write(self, data):
+ self.ws.send(data)
+
+ def read(self, _):
+ try:
+ return self.ws.recv(0)
+ except TimeoutError:
+ pass
+
+ @staticmethod
+ def wrap(fn):
+ return lambda ws: fn(WSPort(ws))
+
+
if __name__ == "__main__":
- port = sys.argv[-1]
- print(f"starting Dummy Oven on port {port}")
- with serial.Serial(port, baudrate=38400, timeout=1) as ser:
- run(ser)
+ if len(sys.argv) < 2:
+ import websockets.sync.server as ws
+
+ print("starting Dummy Oven over WebSocket on port :8001")
+ with ws.serve(WSPort.wrap(run), host="0.0.0.0", port=8001) as server:
+ server.serve_forever()
+
+ else:
+ import serial
+
+ port = sys.argv[-1]
+ print(f"starting Dummy Oven on port {port}")
+ with serial.Serial(port, baudrate=38400, timeout=1) as ser:
+ run(ser)