pyvisionect 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyvisionect/__init__.py +120 -0
- pyvisionect/devices/__init__.py +92 -0
- pyvisionect/devices/enums.py +403 -0
- pyvisionect/devices/panels.py +242 -0
- pyvisionect/devices/tclv.py +460 -0
- pyvisionect/imaging/README.md +175 -0
- pyvisionect/imaging/__init__.py +181 -0
- pyvisionect/imaging/checksum.py +114 -0
- pyvisionect/imaging/constants.py +131 -0
- pyvisionect/imaging/data/blue_noise_64.npy +0 -0
- pyvisionect/imaging/decode.py +172 -0
- pyvisionect/imaging/dither.py +357 -0
- pyvisionect/imaging/encoder.py +594 -0
- pyvisionect/imaging/geometry.py +129 -0
- pyvisionect/imaging/grey.py +104 -0
- pyvisionect/imaging/interlace.py +244 -0
- pyvisionect/imaging/pack.py +142 -0
- pyvisionect/imaging/panel.py +339 -0
- pyvisionect/imaging/partial.py +931 -0
- pyvisionect/imaging/rects.py +466 -0
- pyvisionect/io/__init__.py +33 -0
- pyvisionect/io/tcp.py +795 -0
- pyvisionect/io/usb/__init__.py +237 -0
- pyvisionect/io/usb/cli.py +278 -0
- pyvisionect/io/usb/commands.py +781 -0
- pyvisionect/io/usb/console.py +730 -0
- pyvisionect/io/usb/device.py +914 -0
- pyvisionect/io/usb/discovery.py +260 -0
- pyvisionect/io/usb/encryption.py +262 -0
- pyvisionect/io/usb/errors.py +175 -0
- pyvisionect/io/usb/fake.py +292 -0
- pyvisionect/io/usb/parsers.py +1464 -0
- pyvisionect/io/usb/provisioning.py +502 -0
- pyvisionect/packets/__init__.py +135 -0
- pyvisionect/packets/control.py +95 -0
- pyvisionect/packets/file.py +325 -0
- pyvisionect/packets/image.py +234 -0
- pyvisionect/packets/misc.py +202 -0
- pyvisionect/packets/param.py +284 -0
- pyvisionect/packets/status.py +518 -0
- pyvisionect/packets/stored.py +217 -0
- pyvisionect/py.typed +0 -0
- pyvisionect/session/__init__.py +100 -0
- pyvisionect/session/connection.py +1043 -0
- pyvisionect/session/device.py +587 -0
- pyvisionect/session/events.py +196 -0
- pyvisionect/session/filetransfer.py +251 -0
- pyvisionect/session/pending.py +425 -0
- pyvisionect/wire/__init__.py +91 -0
- pyvisionect/wire/blocks.py +350 -0
- pyvisionect/wire/checksum.py +115 -0
- pyvisionect/wire/errors.py +176 -0
- pyvisionect/wire/framing.py +334 -0
- pyvisionect-0.1.0.dist-info/METADATA +1030 -0
- pyvisionect-0.1.0.dist-info/RECORD +58 -0
- pyvisionect-0.1.0.dist-info/WHEEL +4 -0
- pyvisionect-0.1.0.dist-info/entry_points.txt +3 -0
- pyvisionect-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
"""Durable, coalescing per-device work, applied in the vendor's order on connect.
|
|
2
|
+
|
|
3
|
+
Why this is in the library
|
|
4
|
+
--------------------------
|
|
5
|
+
|
|
6
|
+
:meth:`DeviceConnection.send_image` and friends queue onto a **connection**, and
|
|
7
|
+
a sign is absent most of the time -- a battery device wakes for a minute a day.
|
|
8
|
+
So ``connection_for(uuid)`` returns ``None`` in the common case, and every
|
|
9
|
+
consumer would otherwise build its own deferral layer. Worse, the *coalescing*
|
|
10
|
+
rules are correctness, not style: a buffer that grows while the device sleeps is
|
|
11
|
+
a bug, and pushing ten images to a sleeping sign must show the tenth, not replay
|
|
12
|
+
all ten.
|
|
13
|
+
|
|
14
|
+
So the model here is a **desired-state register**, not a queue. Depth for
|
|
15
|
+
content is exactly one.
|
|
16
|
+
|
|
17
|
+
Coalescing rules
|
|
18
|
+
----------------
|
|
19
|
+
|
|
20
|
+
=================== ========================================== ===============================
|
|
21
|
+
work rule why
|
|
22
|
+
=================== ========================================== ===============================
|
|
23
|
+
content revision counter, **last-write-wins** ten pushes to a sleeping sign
|
|
24
|
+
must show the tenth
|
|
25
|
+
TCLV write per-id last-write-wins heartbeat 5 then 30 means 30
|
|
26
|
+
TCLV read set union reads are free and idempotent
|
|
27
|
+
ghost clear, reboot boolean, set semantics two reboots is one reboot
|
|
28
|
+
sleep **replaces** any prior sleep two sleeps is a logic error
|
|
29
|
+
framebuffer read single slot, later replaces earlier a diagnostic, not a log
|
|
30
|
+
=================== ========================================== ===============================
|
|
31
|
+
|
|
32
|
+
Ordering on connect
|
|
33
|
+
-------------------
|
|
34
|
+
|
|
35
|
+
:data:`PENDING_ORDER` is not arbitrary and is copied from the vendor's
|
|
36
|
+
reconciliation:
|
|
37
|
+
|
|
38
|
+
1. param **reads** -- correct our view from the device before anything acts on
|
|
39
|
+
it, and before a write can race them;
|
|
40
|
+
2. param **writes** -- cheap, small, and may change heartbeat or power behaviour;
|
|
41
|
+
3. ``flash_save`` (TCLV 53) -- persist step 2 before anything can reboot;
|
|
42
|
+
4. framebuffer read -- expensive, so do it while the link definitely exists, but
|
|
43
|
+
after the cheap work has landed;
|
|
44
|
+
5. **image push** -- the big one; it must land before the device leaves;
|
|
45
|
+
6. ghost clear -- a second full-screen push, only after 5;
|
|
46
|
+
7. **sleep** -- the device leaves, so everything above must already be on the
|
|
47
|
+
wire. The vendor waits for its renderer before publishing its sleep packet;
|
|
48
|
+
get this wrong and the user stares at yesterday's dashboard for a day;
|
|
49
|
+
8. **reboot** -- last, unconditionally. Nothing survives it.
|
|
50
|
+
|
|
51
|
+
:meth:`PendingWork.apply` enqueues in exactly that order, and
|
|
52
|
+
:meth:`DeviceConnection.apply_pending` is the normal entry point. Because
|
|
53
|
+
``drain()`` returns bytes in queue order and TCP preserves order, queueing sleep
|
|
54
|
+
after the image bytes *is* "the image lands first". A caller that additionally
|
|
55
|
+
wants each phase **acked** before the next can drive it in stages with the
|
|
56
|
+
``phases=`` argument.
|
|
57
|
+
|
|
58
|
+
Slots clear on ack, not on send
|
|
59
|
+
-------------------------------
|
|
60
|
+
|
|
61
|
+
:meth:`PendingWork.on_acked` / :meth:`PendingWork.on_nacked` take the packet id
|
|
62
|
+
from the library's own ``Acked``/``Nacked`` events, so a connection that drops
|
|
63
|
+
mid-reconcile leaves the remainder queued for next time.
|
|
64
|
+
|
|
65
|
+
``Nacked.charging`` (control flag bit 25) deserves its own branch and gets one
|
|
66
|
+
here: it is a **retry-later**, not a failure. Treating it as a hard failure
|
|
67
|
+
would make an integration appear to break whenever someone plugs the sign in.
|
|
68
|
+
"""
|
|
69
|
+
|
|
70
|
+
from __future__ import annotations
|
|
71
|
+
|
|
72
|
+
from dataclasses import dataclass, field
|
|
73
|
+
from typing import Any, Iterable, Sequence
|
|
74
|
+
|
|
75
|
+
__all__ = [
|
|
76
|
+
"PENDING_ORDER",
|
|
77
|
+
"SLOT_PARAM_READS",
|
|
78
|
+
"SLOT_PARAM_WRITES",
|
|
79
|
+
"SLOT_FLASH_SAVE",
|
|
80
|
+
"SLOT_FRAMEBUFFER_READ",
|
|
81
|
+
"SLOT_IMAGE",
|
|
82
|
+
"SLOT_GHOST_CLEAR",
|
|
83
|
+
"SLOT_SLEEP",
|
|
84
|
+
"SLOT_REBOOT",
|
|
85
|
+
"PendingWork",
|
|
86
|
+
]
|
|
87
|
+
|
|
88
|
+
SLOT_PARAM_READS = "param_reads"
|
|
89
|
+
SLOT_PARAM_WRITES = "param_writes"
|
|
90
|
+
SLOT_FLASH_SAVE = "flash_save"
|
|
91
|
+
SLOT_FRAMEBUFFER_READ = "framebuffer_read"
|
|
92
|
+
SLOT_IMAGE = "image"
|
|
93
|
+
SLOT_GHOST_CLEAR = "ghost_clear"
|
|
94
|
+
SLOT_SLEEP = "sleep"
|
|
95
|
+
SLOT_REBOOT = "reboot"
|
|
96
|
+
|
|
97
|
+
PENDING_ORDER: tuple[str, ...] = (
|
|
98
|
+
SLOT_PARAM_READS,
|
|
99
|
+
SLOT_PARAM_WRITES,
|
|
100
|
+
SLOT_FLASH_SAVE,
|
|
101
|
+
SLOT_FRAMEBUFFER_READ,
|
|
102
|
+
SLOT_IMAGE,
|
|
103
|
+
SLOT_GHOST_CLEAR,
|
|
104
|
+
SLOT_SLEEP,
|
|
105
|
+
SLOT_REBOOT,
|
|
106
|
+
)
|
|
107
|
+
"""The order work is put on the wire. See the module docstring; do not reorder.
|
|
108
|
+
|
|
109
|
+
In particular :data:`SLOT_SLEEP` must come after :data:`SLOT_IMAGE` and
|
|
110
|
+
:data:`SLOT_REBOOT` must be last.
|
|
111
|
+
"""
|
|
112
|
+
|
|
113
|
+
TCLV_CMD_FLASH_SAVE = 53
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@dataclass(slots=True)
|
|
117
|
+
class PendingWork:
|
|
118
|
+
"""Desired state for one device UUID. Serialisable, coalescing, durable.
|
|
119
|
+
|
|
120
|
+
Obtain one with :meth:`DeviceStateStore.queue`; persist it with
|
|
121
|
+
:meth:`to_dict` and restore with :meth:`from_dict` so that a consumer
|
|
122
|
+
restart does not silently drop a push the user queued ten minutes ago.
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
content_revision: int = 0
|
|
126
|
+
"""Bumped by :meth:`set_image`. Compared against :attr:`pushed_revision`."""
|
|
127
|
+
|
|
128
|
+
pushed_revision: int = 0
|
|
129
|
+
"""The revision last acked by the device."""
|
|
130
|
+
|
|
131
|
+
force_push: bool = False
|
|
132
|
+
"""Push even when the device reports it is already in sync."""
|
|
133
|
+
|
|
134
|
+
frame: Any = None
|
|
135
|
+
"""The single pending :class:`~pyvisionect.imaging.EncodedFrame`.
|
|
136
|
+
|
|
137
|
+
Depth exactly one: :meth:`set_image` replaces it. Not included in
|
|
138
|
+
:meth:`to_dict` -- it is megabytes of pixels and cheaper to re-encode than
|
|
139
|
+
to persist. Persist the *content source* instead and re-encode on restore;
|
|
140
|
+
:attr:`content_revision` survives so the intent is not lost.
|
|
141
|
+
"""
|
|
142
|
+
|
|
143
|
+
params: dict[int, Any] = field(default_factory=dict)
|
|
144
|
+
"""TCLV writes, last-write-wins per id."""
|
|
145
|
+
|
|
146
|
+
param_reads: set[int] = field(default_factory=set)
|
|
147
|
+
"""TCLV reads, set union."""
|
|
148
|
+
|
|
149
|
+
persist_params: bool = True
|
|
150
|
+
"""Issue ``flash_save`` (TCLV 53) after the writes.
|
|
151
|
+
|
|
152
|
+
On by default because without it every write is RAM-only and is lost at the
|
|
153
|
+
next reboot -- the same trap the USB CLI has.
|
|
154
|
+
"""
|
|
155
|
+
|
|
156
|
+
want_ghost_clear: bool = False
|
|
157
|
+
want_reboot: bool = False
|
|
158
|
+
|
|
159
|
+
sleep_minutes: int | None = None
|
|
160
|
+
"""Replaces any prior sleep. ``None`` means "do not sleep"."""
|
|
161
|
+
|
|
162
|
+
framebuffer_read: str | None = None
|
|
163
|
+
"""A single pending framebuffer read request; a later one replaces it."""
|
|
164
|
+
|
|
165
|
+
queued_at: dict[str, float] = field(default_factory=dict)
|
|
166
|
+
"""Slot -> the clock value at which it was queued. Purely informational."""
|
|
167
|
+
|
|
168
|
+
attempts: dict[str, int] = field(default_factory=dict)
|
|
169
|
+
"""Slot -> how many times we have put it on the wire."""
|
|
170
|
+
|
|
171
|
+
inflight: dict[int, str] = field(default_factory=dict)
|
|
172
|
+
"""Packet id -> slot, for the work currently awaiting an ack."""
|
|
173
|
+
|
|
174
|
+
last_nack_charging: bool = False
|
|
175
|
+
"""Whether the most recent NACK was "refused because charging"."""
|
|
176
|
+
|
|
177
|
+
# ------------------------------------------------------------ queueing
|
|
178
|
+
|
|
179
|
+
def _touch(self, slot: str, now: float | None) -> None:
|
|
180
|
+
if now is not None:
|
|
181
|
+
self.queued_at[slot] = now
|
|
182
|
+
|
|
183
|
+
def set_image(self, frame: Any, *, force: bool = False,
|
|
184
|
+
now: float | None = None) -> int:
|
|
185
|
+
"""Queue *frame* as the content to display. Last-write-wins.
|
|
186
|
+
|
|
187
|
+
Returns:
|
|
188
|
+
The new :attr:`content_revision`.
|
|
189
|
+
"""
|
|
190
|
+
self.frame = frame
|
|
191
|
+
self.content_revision += 1
|
|
192
|
+
if force:
|
|
193
|
+
self.force_push = True
|
|
194
|
+
self._touch(SLOT_IMAGE, now)
|
|
195
|
+
return self.content_revision
|
|
196
|
+
|
|
197
|
+
def write_param(self, param_id: int, value: Any, *,
|
|
198
|
+
now: float | None = None) -> None:
|
|
199
|
+
"""Queue a TCLV write. Last-write-wins per id.
|
|
200
|
+
|
|
201
|
+
Raises:
|
|
202
|
+
ReadOnlyParameter: for the eight ids the network cannot write, with
|
|
203
|
+
the USB command that can. Raised here rather than at send time
|
|
204
|
+
so the caller learns immediately instead of in an hour.
|
|
205
|
+
"""
|
|
206
|
+
from ..devices.tclv import check_writable
|
|
207
|
+
|
|
208
|
+
check_writable(param_id)
|
|
209
|
+
self.params[param_id] = value
|
|
210
|
+
self._touch(SLOT_PARAM_WRITES, now)
|
|
211
|
+
|
|
212
|
+
def write_params(self, values: dict[int, Any], *,
|
|
213
|
+
now: float | None = None) -> None:
|
|
214
|
+
for param_id, value in values.items():
|
|
215
|
+
self.write_param(param_id, value, now=now)
|
|
216
|
+
|
|
217
|
+
def read_params(self, ids: Iterable[int], *, now: float | None = None) -> None:
|
|
218
|
+
"""Queue TCLV reads. Set union; reads are idempotent and nearly free."""
|
|
219
|
+
self.param_reads.update(int(i) for i in ids)
|
|
220
|
+
self._touch(SLOT_PARAM_READS, now)
|
|
221
|
+
|
|
222
|
+
def read_framebuffer(self, what: str = "auto", *,
|
|
223
|
+
now: float | None = None) -> None:
|
|
224
|
+
"""Queue a framebuffer/file read. Single slot; a later call replaces it."""
|
|
225
|
+
self.framebuffer_read = what
|
|
226
|
+
self._touch(SLOT_FRAMEBUFFER_READ, now)
|
|
227
|
+
|
|
228
|
+
def ghost_clear(self, *, now: float | None = None) -> None:
|
|
229
|
+
"""Queue an inverse / anti-ghosting full-screen refresh. Set semantics."""
|
|
230
|
+
self.want_ghost_clear = True
|
|
231
|
+
self._touch(SLOT_GHOST_CLEAR, now)
|
|
232
|
+
|
|
233
|
+
def reboot(self, *, now: float | None = None) -> None:
|
|
234
|
+
"""Queue a reboot. Set semantics -- pressing it twice is one reboot.
|
|
235
|
+
|
|
236
|
+
Note this sends a ``packet.Type 2`` command, which has never been
|
|
237
|
+
observed on the wire; see :mod:`pyvisionect.packets.misc`.
|
|
238
|
+
"""
|
|
239
|
+
self.want_reboot = True
|
|
240
|
+
self._touch(SLOT_REBOOT, now)
|
|
241
|
+
|
|
242
|
+
def sleep(self, minutes: int, *, now: float | None = None) -> None:
|
|
243
|
+
"""Queue a sleep. **Replaces** any prior sleep.
|
|
244
|
+
|
|
245
|
+
Raises:
|
|
246
|
+
ValueError: for ``minutes <= 0``; the vendor rejects a zero
|
|
247
|
+
duration with ``errSleepTime``.
|
|
248
|
+
"""
|
|
249
|
+
if int(minutes) <= 0:
|
|
250
|
+
raise ValueError(
|
|
251
|
+
f"sleep duration must be a positive number of minutes, got "
|
|
252
|
+
f"{minutes!r}; the vendor's stdcmd.Sleep.Done rejects 0 with "
|
|
253
|
+
"errSleepTime"
|
|
254
|
+
)
|
|
255
|
+
self.sleep_minutes = int(minutes)
|
|
256
|
+
self._touch(SLOT_SLEEP, now)
|
|
257
|
+
|
|
258
|
+
def command(self, kind: str, **kwargs: Any) -> None:
|
|
259
|
+
"""Generic entry point: ``"ghost_clear"``, ``"reboot"`` or ``"sleep"``."""
|
|
260
|
+
if kind == "ghost_clear":
|
|
261
|
+
self.ghost_clear(**kwargs)
|
|
262
|
+
elif kind == "reboot":
|
|
263
|
+
self.reboot(**kwargs)
|
|
264
|
+
elif kind == "sleep":
|
|
265
|
+
self.sleep(**kwargs)
|
|
266
|
+
else:
|
|
267
|
+
raise ValueError(
|
|
268
|
+
f"unknown command kind {kind!r}; known kinds are "
|
|
269
|
+
"'ghost_clear', 'reboot', 'sleep'"
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
# -------------------------------------------------------------- state
|
|
273
|
+
|
|
274
|
+
@property
|
|
275
|
+
def content_pending(self) -> bool:
|
|
276
|
+
"""Whether there is content to push."""
|
|
277
|
+
if self.frame is None:
|
|
278
|
+
return False
|
|
279
|
+
return self.force_push or self.content_revision != self.pushed_revision
|
|
280
|
+
|
|
281
|
+
@property
|
|
282
|
+
def is_empty(self) -> bool:
|
|
283
|
+
"""Whether there is nothing at all left to do."""
|
|
284
|
+
return not any(
|
|
285
|
+
(
|
|
286
|
+
self.param_reads,
|
|
287
|
+
self.params,
|
|
288
|
+
self.framebuffer_read,
|
|
289
|
+
self.content_pending,
|
|
290
|
+
self.want_ghost_clear,
|
|
291
|
+
self.sleep_minutes is not None,
|
|
292
|
+
self.want_reboot,
|
|
293
|
+
)
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
def slots(self) -> list[str]:
|
|
297
|
+
"""The pending slots, in :data:`PENDING_ORDER`."""
|
|
298
|
+
present = {
|
|
299
|
+
SLOT_PARAM_READS: bool(self.param_reads),
|
|
300
|
+
SLOT_PARAM_WRITES: bool(self.params),
|
|
301
|
+
SLOT_FLASH_SAVE: bool(self.params) and self.persist_params,
|
|
302
|
+
SLOT_FRAMEBUFFER_READ: self.framebuffer_read is not None,
|
|
303
|
+
SLOT_IMAGE: self.content_pending,
|
|
304
|
+
SLOT_GHOST_CLEAR: self.want_ghost_clear,
|
|
305
|
+
SLOT_SLEEP: self.sleep_minutes is not None,
|
|
306
|
+
SLOT_REBOOT: self.want_reboot,
|
|
307
|
+
}
|
|
308
|
+
return [slot for slot in PENDING_ORDER if present[slot]]
|
|
309
|
+
|
|
310
|
+
def clear_slot(self, slot: str) -> None:
|
|
311
|
+
"""Mark one slot done. Called for you by :meth:`on_acked`."""
|
|
312
|
+
if slot == SLOT_PARAM_READS:
|
|
313
|
+
self.param_reads.clear()
|
|
314
|
+
elif slot == SLOT_PARAM_WRITES:
|
|
315
|
+
self.params.clear()
|
|
316
|
+
elif slot == SLOT_FLASH_SAVE:
|
|
317
|
+
pass # derived from params; cleared with them
|
|
318
|
+
elif slot == SLOT_FRAMEBUFFER_READ:
|
|
319
|
+
self.framebuffer_read = None
|
|
320
|
+
elif slot == SLOT_IMAGE:
|
|
321
|
+
self.pushed_revision = self.content_revision
|
|
322
|
+
self.force_push = False
|
|
323
|
+
elif slot == SLOT_GHOST_CLEAR:
|
|
324
|
+
self.want_ghost_clear = False
|
|
325
|
+
elif slot == SLOT_SLEEP:
|
|
326
|
+
self.sleep_minutes = None
|
|
327
|
+
elif slot == SLOT_REBOOT:
|
|
328
|
+
self.want_reboot = False
|
|
329
|
+
else:
|
|
330
|
+
raise ValueError(f"unknown slot {slot!r}")
|
|
331
|
+
|
|
332
|
+
def note_sent(self, slot: str, packet_id: int, *,
|
|
333
|
+
now: float | None = None) -> None:
|
|
334
|
+
"""Record that *slot* went out as *packet_id*, awaiting an ack."""
|
|
335
|
+
self.inflight[packet_id] = slot
|
|
336
|
+
self.attempts[slot] = self.attempts.get(slot, 0) + 1
|
|
337
|
+
self._touch(slot, now)
|
|
338
|
+
|
|
339
|
+
def on_acked(self, packet_id: int) -> str | None:
|
|
340
|
+
"""Clear whatever slot *packet_id* belonged to. Returns the slot, if any."""
|
|
341
|
+
slot = self.inflight.pop(packet_id, None)
|
|
342
|
+
if slot is None:
|
|
343
|
+
return None
|
|
344
|
+
self.last_nack_charging = False
|
|
345
|
+
self.clear_slot(slot)
|
|
346
|
+
return slot
|
|
347
|
+
|
|
348
|
+
def on_nacked(self, packet_id: int, *, charging: bool = False) -> str | None:
|
|
349
|
+
"""Leave the slot queued for the next connection. Returns the slot.
|
|
350
|
+
|
|
351
|
+
A charging NACK is a retry-later and is recorded as such; the work stays
|
|
352
|
+
queued either way, because a NACK means the device did not do it.
|
|
353
|
+
"""
|
|
354
|
+
slot = self.inflight.pop(packet_id, None)
|
|
355
|
+
self.last_nack_charging = charging
|
|
356
|
+
return slot
|
|
357
|
+
|
|
358
|
+
def reset_inflight(self) -> None:
|
|
359
|
+
"""Forget what was in flight. Call when a connection drops.
|
|
360
|
+
|
|
361
|
+
Nothing is cleared: a packet that was never acked was never done.
|
|
362
|
+
"""
|
|
363
|
+
self.inflight.clear()
|
|
364
|
+
|
|
365
|
+
# ------------------------------------------------------- persistence
|
|
366
|
+
|
|
367
|
+
VERSION = 1
|
|
368
|
+
|
|
369
|
+
def to_dict(self) -> dict[str, Any]:
|
|
370
|
+
"""A JSON-safe snapshot. :attr:`frame` is deliberately **not** included."""
|
|
371
|
+
return {
|
|
372
|
+
"v": self.VERSION,
|
|
373
|
+
"content_revision": self.content_revision,
|
|
374
|
+
"pushed_revision": self.pushed_revision,
|
|
375
|
+
"force_push": self.force_push,
|
|
376
|
+
"params": {str(k): _jsonable(v) for k, v in self.params.items()},
|
|
377
|
+
"param_reads": sorted(self.param_reads),
|
|
378
|
+
"persist_params": self.persist_params,
|
|
379
|
+
"want_ghost_clear": self.want_ghost_clear,
|
|
380
|
+
"want_reboot": self.want_reboot,
|
|
381
|
+
"sleep_minutes": self.sleep_minutes,
|
|
382
|
+
"framebuffer_read": self.framebuffer_read,
|
|
383
|
+
"queued_at": dict(self.queued_at),
|
|
384
|
+
"attempts": dict(self.attempts),
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
@classmethod
|
|
388
|
+
def from_dict(cls, raw: dict[str, Any]) -> "PendingWork":
|
|
389
|
+
"""Restore from :meth:`to_dict`.
|
|
390
|
+
|
|
391
|
+
Unknown future versions raise rather than silently mis-restoring.
|
|
392
|
+
"""
|
|
393
|
+
version = raw.get("v", 1)
|
|
394
|
+
if version != cls.VERSION:
|
|
395
|
+
raise ValueError(
|
|
396
|
+
f"PendingWork snapshot version {version} is not {cls.VERSION}; "
|
|
397
|
+
"discard it and start fresh rather than guessing"
|
|
398
|
+
)
|
|
399
|
+
work = cls(
|
|
400
|
+
content_revision=int(raw.get("content_revision", 0)),
|
|
401
|
+
pushed_revision=int(raw.get("pushed_revision", 0)),
|
|
402
|
+
force_push=bool(raw.get("force_push", False)),
|
|
403
|
+
params={int(k): _unjsonable(v) for k, v in (raw.get("params") or {}).items()},
|
|
404
|
+
param_reads={int(i) for i in (raw.get("param_reads") or [])},
|
|
405
|
+
persist_params=bool(raw.get("persist_params", True)),
|
|
406
|
+
want_ghost_clear=bool(raw.get("want_ghost_clear", False)),
|
|
407
|
+
want_reboot=bool(raw.get("want_reboot", False)),
|
|
408
|
+
sleep_minutes=raw.get("sleep_minutes"),
|
|
409
|
+
framebuffer_read=raw.get("framebuffer_read"),
|
|
410
|
+
queued_at=dict(raw.get("queued_at") or {}),
|
|
411
|
+
attempts=dict(raw.get("attempts") or {}),
|
|
412
|
+
)
|
|
413
|
+
return work
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
def _jsonable(value: Any) -> Any:
|
|
417
|
+
if isinstance(value, bytes):
|
|
418
|
+
return {"__bytes__": value.hex()}
|
|
419
|
+
return value
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
def _unjsonable(value: Any) -> Any:
|
|
423
|
+
if isinstance(value, dict) and "__bytes__" in value:
|
|
424
|
+
return bytes.fromhex(value["__bytes__"])
|
|
425
|
+
return value
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"""Wire layer: framing, the LZ4 block chain, and the direction-dependent CRC.
|
|
2
|
+
|
|
3
|
+
Pure. No sockets, no clock.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from .blocks import (
|
|
7
|
+
BLOCK_HEADER_SIZE,
|
|
8
|
+
BLOCK_PLAINTEXT_SIZE,
|
|
9
|
+
STORED_ONLY,
|
|
10
|
+
BlockHeader,
|
|
11
|
+
decode_blocks,
|
|
12
|
+
encode_blocks,
|
|
13
|
+
have_lz4,
|
|
14
|
+
iter_block_headers,
|
|
15
|
+
lz4_compress_block,
|
|
16
|
+
lz4_decompress_block,
|
|
17
|
+
stored_only,
|
|
18
|
+
)
|
|
19
|
+
from .checksum import Direction, crc32_ieee, frame_checksum, verify_checksum
|
|
20
|
+
from .errors import (
|
|
21
|
+
BlockError,
|
|
22
|
+
ChecksumMismatch,
|
|
23
|
+
CommandPacketsDisabled,
|
|
24
|
+
ListenError,
|
|
25
|
+
MissingLz4,
|
|
26
|
+
FrameTooLarge,
|
|
27
|
+
PayloadError,
|
|
28
|
+
ProtocolError,
|
|
29
|
+
ReadOnlyParameter,
|
|
30
|
+
ShortBuffer,
|
|
31
|
+
UnsupportedCompression,
|
|
32
|
+
UnsupportedSecurity,
|
|
33
|
+
UnsupportedVersion,
|
|
34
|
+
VisionectError,
|
|
35
|
+
)
|
|
36
|
+
from .framing import (
|
|
37
|
+
DATA_HEADER_SIZE,
|
|
38
|
+
MAX_FRAME_LENGTH,
|
|
39
|
+
PROTOCOL_HEADER_SIZE,
|
|
40
|
+
PROTOCOL_VERSION,
|
|
41
|
+
Compression,
|
|
42
|
+
DataHeader,
|
|
43
|
+
Frame,
|
|
44
|
+
FrameDecoder,
|
|
45
|
+
ProtocolHeader,
|
|
46
|
+
Security,
|
|
47
|
+
encode_frame,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
__all__ = [
|
|
51
|
+
"BLOCK_HEADER_SIZE",
|
|
52
|
+
"BLOCK_PLAINTEXT_SIZE",
|
|
53
|
+
"BlockError",
|
|
54
|
+
"BlockHeader",
|
|
55
|
+
"ChecksumMismatch",
|
|
56
|
+
"CommandPacketsDisabled",
|
|
57
|
+
"Compression",
|
|
58
|
+
"DATA_HEADER_SIZE",
|
|
59
|
+
"DataHeader",
|
|
60
|
+
"Direction",
|
|
61
|
+
"Frame",
|
|
62
|
+
"FrameDecoder",
|
|
63
|
+
"FrameTooLarge",
|
|
64
|
+
"MAX_FRAME_LENGTH",
|
|
65
|
+
"PROTOCOL_HEADER_SIZE",
|
|
66
|
+
"PROTOCOL_VERSION",
|
|
67
|
+
"PayloadError",
|
|
68
|
+
"ProtocolError",
|
|
69
|
+
"ListenError",
|
|
70
|
+
"MissingLz4",
|
|
71
|
+
"ProtocolHeader",
|
|
72
|
+
"ReadOnlyParameter",
|
|
73
|
+
"STORED_ONLY",
|
|
74
|
+
"Security",
|
|
75
|
+
"ShortBuffer",
|
|
76
|
+
"UnsupportedCompression",
|
|
77
|
+
"UnsupportedSecurity",
|
|
78
|
+
"UnsupportedVersion",
|
|
79
|
+
"VisionectError",
|
|
80
|
+
"crc32_ieee",
|
|
81
|
+
"decode_blocks",
|
|
82
|
+
"encode_blocks",
|
|
83
|
+
"encode_frame",
|
|
84
|
+
"frame_checksum",
|
|
85
|
+
"have_lz4",
|
|
86
|
+
"iter_block_headers",
|
|
87
|
+
"lz4_compress_block",
|
|
88
|
+
"lz4_decompress_block",
|
|
89
|
+
"stored_only",
|
|
90
|
+
"verify_checksum",
|
|
91
|
+
]
|