Mercurial > code > home > repos > light-bridge
annotate light.py @ 12:7cc004eafb82
refactor (approx)
author | drewp@bigasterisk.com |
---|---|
date | Sun, 28 Jan 2024 20:02:34 -0800 |
parents | 028ed39aa78f |
children | 1c865af058e7 |
rev | line source |
---|---|
2 | 1 import asyncio |
2 import logging | |
9 | 3 from dataclasses import dataclass, field |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
4 from typing import Callable |
2 | 5 |
5
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
6 from color import Color |
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
7 |
2 | 8 log = logging.getLogger('light') |
9 | |
10 | |
9 | 11 @dataclass(frozen=True) |
12 class DeviceColor: | |
13 """neutral representation of the adjusted color that we send to a device""" | |
14 r: float = 0 | |
15 g: float = 0 | |
16 b: float = 0 | |
17 w: float = 0 | |
18 x: float = 0 | |
19 y: float = 0 | |
20 | |
21 def summary(self) -> dict: | |
22 return dict([(k, round(v, 3)) for k, v in self.__dict__.items() if v > 0]) | |
23 | |
24 | |
2 | 25 @dataclass |
26 class Light: | |
27 name: str | |
28 address: str | |
9 | 29 |
30 requestingColor: Color = Color.fromHex('#000000') | |
31 requestingDeviceColor: DeviceColor = DeviceColor() | |
32 | |
33 emittingColor: Color = Color.fromHex('#000000') | |
34 online: bool | None = None | |
35 latencyMs: float | None = None | |
36 | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
37 notifyChanged: Callable | None = None |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
38 |
9 | 39 def __post_init__(self): |
40 self.requestingDeviceColor = self.deviceColor(self.requestingColor) | |
2 | 41 |
5
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
42 def to_dict(self): |
9 | 43 d = { |
44 'name': self.name, | |
45 'address': self.address, | |
46 'requestingColor': self.requestingColor.hex(), | |
47 'requestingDeviceColor': self.requestingDeviceColor.summary(), | |
48 'emittingColor': self.emittingColor.hex(), | |
49 'online': self.online, | |
50 'latencyMs': self.latencyMs, | |
2 | 51 } |
52 | |
9 | 53 return {'light': d} |
54 | |
55 def deviceColor(self, c: Color) -> DeviceColor: | |
56 # do LUT here | |
57 return DeviceColor(r=c.r, g=c.g, b=c.b) | |
58 | |
59 async def setColor(self, c: Color): | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
60 log.info(f'setColor from {self.requestingColor} to {c}') |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
61 if c == self.requestingColor: |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
62 return |
9 | 63 self.requestingColor = c |
64 self.requestingDeviceColor = self.deviceColor(self.requestingColor) | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
65 if self.notifyChanged: |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
66 self.notifyChanged() |
9 | 67 |
2 | 68 |
69 class Lights: | |
70 _d: dict[str, Light] = {} | |
71 | |
6 | 72 def __init__(self): |
9 | 73 self.add(Light('do-desk', 'topic1')) |
74 self.add(Light('do-desk2', 'topic2')) | |
6 | 75 |
2 | 76 def add(self, d: Light): |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
77 d.notifyChanged = self.notifyChanged |
2 | 78 self._d[d.name] = d |
79 | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
80 self.notifyChanged() |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
81 |
2 | 82 def byName(self, name: str) -> Light: |
83 return self._d[name] | |
84 | |
12 | 85 def to_dict(self): |
86 return {'lights': [d.to_dict() for d in sorted(self._d.values(), key=lambda r: r.name)]} | |
87 | |
88 # the following is bad. Get a better events lib. | |
89 _changeSleepTask: asyncio.Task | None = None | |
90 | |
2 | 91 async def changes(self): # yields None on any data change |
92 while True: | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
93 log.info('Lights has a change') |
2 | 94 yield None |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
95 self._changeSleepTask = asyncio.create_task(self._sleep()) |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
96 try: |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
97 await self._changeSleepTask |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
98 except asyncio.CancelledError: |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
99 pass |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
100 self._changeSleepTask = None |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
101 |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
102 async def _sleep(self): |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
103 await asyncio.sleep(100) |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
104 |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
105 def notifyChanged(self): |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
106 log.info('Lights.notifyChanged()') |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
107 if self._changeSleepTask is not None: |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
108 self._changeSleepTask.cancel() |