Mercurial > code > home > repos > light-bridge
annotate light.py @ 11:028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
author | drewp@bigasterisk.com |
---|---|
date | Sun, 28 Jan 2024 17:31:06 -0800 |
parents | 9f427d8073c3 |
children | 7cc004eafb82 |
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] = {} | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
71 _changeSleepTask: asyncio.Task | None = None |
2 | 72 |
6 | 73 def __init__(self): |
9 | 74 self.add(Light('do-desk', 'topic1')) |
75 self.add(Light('do-desk2', 'topic2')) | |
6 | 76 |
2 | 77 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
|
78 d.notifyChanged = self.notifyChanged |
2 | 79 self._d[d.name] = d |
80 | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
81 self.notifyChanged() |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
82 |
2 | 83 def byName(self, name: str) -> Light: |
84 return self._d[name] | |
85 | |
86 async def changes(self): # yields None on any data change | |
87 while True: | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
88 log.info('Lights has a change') |
2 | 89 yield None |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
90 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
|
91 try: |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
92 await self._changeSleepTask |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
93 except asyncio.CancelledError: |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
94 pass |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
95 self._changeSleepTask = None |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
96 |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
97 async def _sleep(self): |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
98 await asyncio.sleep(100) |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
99 |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
100 def notifyChanged(self): |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
101 log.info('Lights.notifyChanged()') |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
102 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
|
103 self._changeSleepTask.cancel() |
2 | 104 |
5
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
105 def to_dict(self): |
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
106 return {'lights': [d.to_dict() for d in sorted(self._d.values(), key=lambda r: r.name)]} |