summaryrefslogtreecommitdiff
path: root/include/py/homekit/relay/sunxi_h3_server.py
blob: 1f3396998a70d6a2e113244c9140dad96d8c2625 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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()