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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
# ========================================================================== #
# #
# KVMD - The main Pi-KVM daemon. #
# #
# Copyright (C) 2018 Maxim Devaev <[email protected]> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
# ========================================================================== #
import asyncio
import operator
from typing import List
from typing import Dict
from typing import AsyncGenerator
from typing import Optional
from ...logging import get_logger
from ... import aiotools
from ... import gpio
from ...yamlconf import Section
from ...errors import OperationError
from ...errors import IsBusyError
# =====
class GpioChannelNotFoundError(OperationError):
def __init__(self) -> None:
super().__init__("GPIO channel is not found")
class GpioSwitchNotSupported(OperationError):
def __init__(self) -> None:
super().__init__("This GPIO channel does not support switching")
class GpioPulseNotSupported(OperationError):
def __init__(self) -> None:
super().__init__("This GPIO channel does not support pulsing")
class GpioChannelIsBusyError(IsBusyError):
def __init__(self) -> None:
super().__init__("Performing another GPIO operation on this channel, please try again later")
# =====
class _GpioInput:
def __init__(self, channel: str, config: Section, reader: gpio.BatchReader) -> None:
self.__channel = channel
self.__pin: int = config.pin # gpio.set_input(config.pin) # Configured in UserGpio/BatchReader
self.__inverted: bool = config.inverted
self.__reader = reader
def get_scheme(self) -> Dict:
return {}
def get_state(self) -> Dict:
return {"state": (self.__reader.get(self.__pin) ^ self.__inverted)}
def __str__(self) -> str:
return f"Input({self.__channel}, pin={self.__pin})"
__repr__ = __str__
class _GpioOutput: # pylint: disable=too-many-instance-attributes
def __init__(self, channel: str, config: Section, notifier: aiotools.AioNotifier) -> None:
self.__channel = channel
self.__pin: int = gpio.set_output(config.pin, (config.initial ^ config.inverted))
self.__inverted: bool = config.inverted
self.__switch: bool = config.switch
self.__pulse_delay: float = config.pulse.delay
self.__min_pulse_delay: float = config.pulse.min_delay
self.__max_pulse_delay: float = config.pulse.max_delay
self.__busy_delay: float = config.busy_delay
self.__state = config.initial
self.__region = aiotools.AioExclusiveRegion(GpioChannelIsBusyError, notifier)
def get_scheme(self) -> Dict:
return {
"switch": self.__switch,
"pulse": {
"delay": self.__pulse_delay,
"min_delay": (self.__min_pulse_delay if self.__pulse_delay else 0),
"max_delay": (self.__max_pulse_delay if self.__pulse_delay else 0),
},
}
def get_state(self) -> Dict:
busy = self.__region.is_busy()
return {
"state": (self.__state if not busy else False),
"busy": busy,
}
def cleanup(self) -> None:
try:
gpio.write(self.__pin, False)
except Exception:
get_logger().exception("Can't cleanup GPIO %s", self)
async def switch(self, state: bool) -> bool:
if not self.__switch:
raise GpioSwitchNotSupported()
async with self.__region:
# Состояние проверяется только при изменении
real_state = self.__read()
if state != real_state:
self.__write(state)
self.__state = state
get_logger(0).info("Switched %s to %d", self, state)
await asyncio.sleep(self.__busy_delay)
return True
self.__state = real_state
await asyncio.sleep(self.__busy_delay)
return False
@aiotools.atomic
async def pulse(self, delay: float) -> None:
if not self.__pulse_delay:
raise GpioPulseNotSupported()
delay = min(max((delay or self.__pulse_delay), self.__min_pulse_delay), self.__max_pulse_delay)
await aiotools.run_region_task(
f"Can't perform pulse of {self} or operation was not completed",
self.__region, self.__inner_pulse, delay,
)
@aiotools.atomic
async def __inner_pulse(self, delay: float) -> None:
try:
self.__write(True)
await asyncio.sleep(delay)
finally:
self.__write(False)
await asyncio.sleep(self.__busy_delay)
get_logger(0).info("Pulsed %s with delay=%.2f", self, delay)
def __read(self) -> bool:
return (gpio.read(self.__pin) ^ self.__inverted)
def __write(self, state: bool) -> None:
gpio.write(self.__pin, (state ^ self.__inverted))
def __str__(self) -> str:
return f"Output({self.__channel}, pin={self.__pin})"
__repr__ = __str__
# =====
class UserGpio:
def __init__(self, config: Section) -> None:
self.__view = config.view
self.__state_notifier = aiotools.AioNotifier()
self.__reader = gpio.BatchReader(
pins=[gpio.set_input(ch_config.pin) for ch_config in config.scheme.values()],
interval=config.state_poll,
notifier=self.__state_notifier,
)
self.__inputs: Dict[str, _GpioInput] = {}
self.__outputs: Dict[str, _GpioOutput] = {}
for (channel, ch_config) in sorted(config.scheme.items(), key=operator.itemgetter(0)):
if ch_config.mode == "input":
self.__inputs[channel] = _GpioInput(channel, ch_config, self.__reader)
else: # output:
self.__outputs[channel] = _GpioOutput(channel, ch_config, self.__state_notifier)
async def get_model(self) -> Dict:
return {
"scheme": {
"inputs": {channel: gin.get_scheme() for (channel, gin) in self.__inputs.items()},
"outputs": {channel: gout.get_scheme() for (channel, gout) in self.__outputs.items()},
},
"view": self.__make_view(),
}
async def get_state(self) -> Dict:
return {
"inputs": {channel: gin.get_state() for (channel, gin) in self.__inputs.items()},
"outputs": {channel: gout.get_state() for (channel, gout) in self.__outputs.items()},
}
async def poll_state(self) -> AsyncGenerator[Dict, None]:
reader_task = asyncio.create_task(self.__reader.poll())
waiter_task: Optional[asyncio.Task] = None
prev_state: Dict = {}
try:
while True:
if reader_task.cancelled():
break
if reader_task.done():
RuntimeError("BatchReader task is dead")
state = await self.get_state()
if state != prev_state:
yield state
prev_state = state
if waiter_task is None:
waiter_task = asyncio.create_task(self.__state_notifier.wait())
if waiter_task in (await aiotools.wait_first(reader_task, waiter_task))[0]:
waiter_task = None
finally:
if not reader_task.done():
reader_task.cancel()
await reader_task
async def cleanup(self) -> None:
for gout in self.__outputs.values():
gout.cleanup()
async def switch(self, channel: str, state: bool) -> bool:
gout = self.__outputs.get(channel)
if gout is None:
raise GpioChannelNotFoundError()
return (await gout.switch(state))
async def pulse(self, channel: str, delay: float) -> None:
gout = self.__outputs.get(channel)
if gout is None:
raise GpioChannelNotFoundError()
await gout.pulse(delay)
# =====
def __make_view(self) -> Dict:
table: List[Optional[List[Dict]]] = []
for row in self.__view["table"]:
if len(row) == 0:
table.append(None)
continue
items: List[Dict] = []
for item in map(str.strip, row):
if item.startswith("#") or len(item) == 0:
items.append({
"type": "label",
"text": item[1:].strip(),
})
elif (parts := list(map(str.strip, item.split(",", 1)))):
if parts[0] in self.__inputs:
items.append({
"type": "input",
"channel": parts[0],
})
elif parts[0] in self.__outputs:
items.append({
"type": "output",
"channel": parts[0],
"text": (parts[1] if len(parts) > 1 else "Click"),
})
table.append(items)
return {
"header": self.__view["header"],
"table": table,
}
|