diff options
Diffstat (limited to 'simulator')
| -rw-r--r-- | simulator/README.md | 9 | ||||
| -rw-r--r-- | simulator/__init__.py | 38 |
2 files changed, 42 insertions, 5 deletions
diff --git a/simulator/README.md b/simulator/README.md index e3980da..df091d7 100644 --- a/simulator/README.md +++ b/simulator/README.md @@ -4,6 +4,7 @@ This directory contains a 'dummy' oven implemented in Python. It exists mainy to facilitate development of the web client without physical access to an oven (and without wasting energy). The code has been written and tested with Python 3.11 but should work with most if not all Python 3 versions. +Either the `pyserial` or `websockets` python library is required, depending on the operating mode: ## Serial loopback on Linux @@ -16,4 +17,12 @@ You can use tty0tty like this: python simulator/__init__.py /dev/tnt1 # /dev/tnt0 now behaves as if it were a reflow oven +## WebSocket implementation + +Alternatively, WebSockets can be used as a transport for developopment. +To run the simulator in websocket mode, omit the serial port argument: + + python simulator/__init__.py + # a WebSocket server is now accepting connections at 0.0.0.0:8001 + [tty0tty]: https://github.com/freemed/tty0tty 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) |
