From 72ab73ab0f7ec489acc1360347e9206630efde1b Mon Sep 17 00:00:00 2001 From: Andrey Lebedev Date: Thu, 21 May 2020 00:31:09 +0300 Subject: [PATCH] Add pyserial into the equation --- Pipfile | 2 ++ Pipfile.lock | 24 ++++++++++++++++++++---- src/ovshell_core/ext.py | 12 +++++------- src/ovshell_core/serial.py | 26 ++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 src/ovshell_core/serial.py diff --git a/Pipfile b/Pipfile index 4c31ca7..f3ddbee 100644 --- a/Pipfile +++ b/Pipfile @@ -15,3 +15,5 @@ openvario-shell = {editable = true,path = "."} urwid = "*" typing-extensions = "*" importlib-metadata = "*" +pyserial-asyncio = "*" +pyserial = "*" diff --git a/Pipfile.lock b/Pipfile.lock index df97b30..f8af157 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "0421984609e566b01905bcbf856c2967a3bf3b87bae2e12ec63fe7f915b2fb3a" + "sha256": "13a4e7677d12f9fd5bc688353215fd781a63661b32674c0e44cb68691cb1b8ab" }, "pipfile-spec": 6, "requires": {}, @@ -26,6 +26,22 @@ "editable": true, "path": "." }, + "pyserial": { + "hashes": [ + "sha256:6e2d401fdee0eab996cf734e67773a0143b932772ca8b42451440cfed942c627", + "sha256:e0770fadba80c31013896c7e6ef703f72e7834965954a78e71a3049488d4d7d8" + ], + "index": "pypi", + "version": "==3.4" + }, + "pyserial-asyncio": { + "hashes": [ + "sha256:861d975029498e777a67c6d6d392e20b84a79c01608f3778c978d0106467c8f8", + "sha256:c40677a8874d8c24d4423a97498746de776f6dbcd0efbb8fa43dcf011a589aee" + ], + "index": "pypi", + "version": "==0.4" + }, "typing-extensions": { "hashes": [ "sha256:6e95524d8a547a91e08f404ae485bbb71962de46967e1b71a0cb89af24e761c5", @@ -152,10 +168,10 @@ }, "packaging": { "hashes": [ - "sha256:3c292b474fda1671ec57d46d739d072bfd495a4f51ad01a055121d81e952b7a3", - "sha256:82f77b9bee21c1bafbf35a84905d604d5d1223801d639cf3ed140bd651c08752" + "sha256:4357f74f47b9c12db93624a82154e9b120fa8293699949152b22065d556079f8", + "sha256:998416ba6962ae7fbd6596850b80e17859a5753ba17c32284f67bfff33784181" ], - "version": "==20.3" + "version": "==20.4" }, "pathspec": { "hashes": [ diff --git a/src/ovshell_core/ext.py b/src/ovshell_core/ext.py index 26c7f52..fe9a36a 100644 --- a/src/ovshell_core/ext.py +++ b/src/ovshell_core/ext.py @@ -1,9 +1,9 @@ from typing import Sequence -import asyncio from ovshell import protocol from ovshell_core import settings +from ovshell_core import serial class CoreExtension(protocol.Extension): @@ -26,7 +26,10 @@ def list_settings(self) -> Sequence[protocol.Setting]: ] def start(self) -> None: - self.shell.processes.start(maintain_serial_devices()) + devlookup = serial.SerialDeviceLookupImpl() + self.shell.processes.start( + serial.maintain_serial_devices(self.shell, devlookup) + ) def _init_settings(self) -> None: config = self.shell.settings @@ -38,8 +41,3 @@ def _apply_font(self) -> None: font = config.get("core.font", str) if font is not None: settings.apply_font(self.shell.os, font) - - -async def maintain_serial_devices(): - while True: - await asyncio.sleep(1) diff --git a/src/ovshell_core/serial.py b/src/ovshell_core/serial.py new file mode 100644 index 0000000..d9bcd46 --- /dev/null +++ b/src/ovshell_core/serial.py @@ -0,0 +1,26 @@ +from typing import Sequence +from typing_extensions import Protocol +import asyncio + +from serial.tools import list_ports + +from ovshell import protocol + + +class SerialDeviceLookup(Protocol): + def enumerate(self) -> Sequence[str]: + pass + + +class SerialDeviceLookupImpl(SerialDeviceLookup): + def enumerate(self) -> Sequence[str]: + devs = list_ports.comports(include_links=False) + return [d.device for d in devs] + + +async def maintain_serial_devices( + shell: protocol.OpenVarioShell, devlookup: SerialDeviceLookup +) -> None: + while True: + devs = devlookup.enumerate() + await asyncio.sleep(1)