From 5f34b77581355ed2e0f03a6eb7607e4c6d508e7e Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sun, 18 Dec 2022 05:35:54 +0300 Subject: pump_bot: rename modules, make it clear it works locally on sunxi h3 board --- src/gpiorelayd.py | 2 +- src/home/relay/__init__.py | 4 +- src/home/relay/__init__.pyi | 4 +- src/home/relay/client.py | 39 ------------------- src/home/relay/server.py | 82 --------------------------------------- src/home/relay/sunxi_h3_client.py | 39 +++++++++++++++++++ src/home/relay/sunxi_h3_server.py | 82 +++++++++++++++++++++++++++++++++++++++ src/pump_bot.py | 2 +- 8 files changed, 127 insertions(+), 127 deletions(-) delete mode 100644 src/home/relay/client.py delete mode 100644 src/home/relay/server.py create mode 100644 src/home/relay/sunxi_h3_client.py create mode 100644 src/home/relay/sunxi_h3_server.py (limited to 'src') diff --git a/src/gpiorelayd.py b/src/gpiorelayd.py index f39a86a..cd2941f 100755 --- a/src/gpiorelayd.py +++ b/src/gpiorelayd.py @@ -5,7 +5,7 @@ import sys from home.config import config from home.util import parse_addr -from home.relay.server import RelayServer +from home.relay.sunxi_h3_server import RelayServer logger = logging.getLogger(__name__) diff --git a/src/home/relay/__init__.py b/src/home/relay/__init__.py index f1568be..406403d 100644 --- a/src/home/relay/__init__.py +++ b/src/home/relay/__init__.py @@ -5,8 +5,8 @@ __all__ = ['RelayClient', 'RelayServer'] def __getattr__(name): _map = { - 'RelayClient': '.client', - 'RelayServer': '.server' + 'RelayClient': '.sunxi_h3_client', + 'RelayServer': '.sunxi_h3_server' } if name in __all__: diff --git a/src/home/relay/__init__.pyi b/src/home/relay/__init__.pyi index 94341f6..7a4a2f4 100644 --- a/src/home/relay/__init__.pyi +++ b/src/home/relay/__init__.pyi @@ -1,2 +1,2 @@ -from .client import RelayClient as RelayClient -from .server import RelayServer as RelayServer +from .sunxi_h3_client import RelayClient as RelayClient +from .sunxi_h3_server import RelayServer as RelayServer diff --git a/src/home/relay/client.py b/src/home/relay/client.py deleted file mode 100644 index 8c8d6c4..0000000 --- a/src/home/relay/client.py +++ /dev/null @@ -1,39 +0,0 @@ -import socket - - -class RelayClient: - def __init__(self, port=8307, host='127.0.0.1'): - self._host = host - self._port = port - self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - - def __del__(self): - self.sock.close() - - def connect(self): - self.sock.connect((self._host, self._port)) - - def _write(self, line): - self.sock.sendall((line+'\r\n').encode()) - - def _read(self): - buf = bytearray() - while True: - buf.extend(self.sock.recv(256)) - if b'\r\n' in buf: - break - - response = buf.decode().strip() - return response - - def on(self): - self._write('on') - return self._read() - - def off(self): - self._write('off') - return self._read() - - def status(self): - self._write('get') - return self._read() diff --git a/src/home/relay/server.py b/src/home/relay/server.py deleted file mode 100644 index 1f33969..0000000 --- a/src/home/relay/server.py +++ /dev/null @@ -1,82 +0,0 @@ -import asyncio -import logging - -from pyA20.gpio import gpio -from pyA20.gpio import port as gpioport -from ..util import Addr - -logger = logging.getLogger(__name__) - - -class RelayServer: - OFF = 1 - ON = 0 - - def __init__(self, - pinname: str, - addr: Addr): - if not hasattr(gpioport, pinname): - raise ValueError(f'invalid pin {pinname}') - - self.pin = getattr(gpioport, pinname) - self.addr = addr - - gpio.init() - gpio.setcfg(self.pin, gpio.OUTPUT) - - self.lock = asyncio.Lock() - - def run(self): - asyncio.run(self.run_server()) - - async def relay_set(self, value): - async with self.lock: - gpio.output(self.pin, value) - - async def relay_get(self): - async with self.lock: - return int(gpio.input(self.pin)) == RelayServer.ON - - async def handle_client(self, reader, writer): - request = None - while request != 'quit': - try: - request = await reader.read(255) - if request == b'\x04': - break - request = request.decode('utf-8').strip() - except Exception: - break - - data = 'unknown' - if request == 'on': - await self.relay_set(RelayServer.ON) - logger.debug('set on') - data = 'ok' - - elif request == 'off': - await self.relay_set(RelayServer.OFF) - logger.debug('set off') - data = 'ok' - - elif request == 'get': - status = await self.relay_get() - data = 'on' if status is True else 'off' - - writer.write((data + '\r\n').encode('utf-8')) - try: - await writer.drain() - except ConnectionError: - break - - try: - writer.close() - except ConnectionError: - pass - - async def run_server(self): - host, port = self.addr - server = await asyncio.start_server(self.handle_client, host, port) - async with server: - logger.info('Server started.') - await server.serve_forever() diff --git a/src/home/relay/sunxi_h3_client.py b/src/home/relay/sunxi_h3_client.py new file mode 100644 index 0000000..8c8d6c4 --- /dev/null +++ b/src/home/relay/sunxi_h3_client.py @@ -0,0 +1,39 @@ +import socket + + +class RelayClient: + def __init__(self, port=8307, host='127.0.0.1'): + self._host = host + self._port = port + self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + + def __del__(self): + self.sock.close() + + def connect(self): + self.sock.connect((self._host, self._port)) + + def _write(self, line): + self.sock.sendall((line+'\r\n').encode()) + + def _read(self): + buf = bytearray() + while True: + buf.extend(self.sock.recv(256)) + if b'\r\n' in buf: + break + + response = buf.decode().strip() + return response + + def on(self): + self._write('on') + return self._read() + + def off(self): + self._write('off') + return self._read() + + def status(self): + self._write('get') + return self._read() diff --git a/src/home/relay/sunxi_h3_server.py b/src/home/relay/sunxi_h3_server.py new file mode 100644 index 0000000..1f33969 --- /dev/null +++ b/src/home/relay/sunxi_h3_server.py @@ -0,0 +1,82 @@ +import asyncio +import logging + +from pyA20.gpio import gpio +from pyA20.gpio import port as gpioport +from ..util import Addr + +logger = logging.getLogger(__name__) + + +class RelayServer: + OFF = 1 + ON = 0 + + def __init__(self, + pinname: str, + addr: Addr): + if not hasattr(gpioport, pinname): + raise ValueError(f'invalid pin {pinname}') + + self.pin = getattr(gpioport, pinname) + self.addr = addr + + gpio.init() + gpio.setcfg(self.pin, gpio.OUTPUT) + + self.lock = asyncio.Lock() + + def run(self): + asyncio.run(self.run_server()) + + async def relay_set(self, value): + async with self.lock: + gpio.output(self.pin, value) + + async def relay_get(self): + async with self.lock: + return int(gpio.input(self.pin)) == RelayServer.ON + + async def handle_client(self, reader, writer): + request = None + while request != 'quit': + try: + request = await reader.read(255) + if request == b'\x04': + break + request = request.decode('utf-8').strip() + except Exception: + break + + data = 'unknown' + if request == 'on': + await self.relay_set(RelayServer.ON) + logger.debug('set on') + data = 'ok' + + elif request == 'off': + await self.relay_set(RelayServer.OFF) + logger.debug('set off') + data = 'ok' + + elif request == 'get': + status = await self.relay_get() + data = 'on' if status is True else 'off' + + writer.write((data + '\r\n').encode('utf-8')) + try: + await writer.drain() + except ConnectionError: + break + + try: + writer.close() + except ConnectionError: + pass + + async def run_server(self): + host, port = self.addr + server = await asyncio.start_server(self.handle_client, host, port) + async with server: + logger.info('Server started.') + await server.serve_forever() diff --git a/src/pump_bot.py b/src/pump_bot.py index bf791d3..de925db 100755 --- a/src/pump_bot.py +++ b/src/pump_bot.py @@ -6,7 +6,7 @@ from telegram import ReplyKeyboardMarkup, User from home.config import config from home.telegram import bot from home.telegram._botutil import user_any_name -from home.relay import RelayClient +from home.relay.sunxi_h3_client import RelayClient from home.api.types import BotType config.load('pump_bot') -- cgit v1.2.3