Skip to content

Events

Class buffers, deadbands, and the overflow they report. What changed, when, and which class it belongs to.

py1815.events

Event buffers: what changed, when, and which class it belongs to.

A static read answers "what is the value now". An event answers "it changed, and here is what it was at the time" -- which is the question a master asks when it wants a sequence rather than a snapshot, and it is why events are held rather than computed.

Three properties are the whole of this module, and each exists because of a way event reporting goes wrong in practice.

An event is generated by a deadband, not by a difference. Reporting every read of a measured value floods the buffer with noise no operator asked for. The deadband is the operator's statement of what counts as a change.

A quality change always generates an event, whatever the magnitude. A point that goes comm-lost while holding the same number has not changed value and has changed in the way that matters most. A deadband applied to quality would hide exactly the transitions someone is watching for.

A full buffer drops its oldest event and says so. The alternative -- dropping the newest -- keeps a master reading history while the present goes unreported, and silence about either is worse than the loss.

Copyright 2026 DER Security Corp. Licensed under the Apache License, Version 2.0.

EventClass

Bases: IntEnum

The class an event is reported in.

Class 0 is not here: it is the static data, which is a read rather than a buffer. An event assigned class 0 is an event that is never reported, so assigning one is refused rather than quietly honored.

Source code in src/py1815/events.py
class EventClass(IntEnum):
    """The class an event is reported in.

    Class 0 is not here: it is the static data, which is a read rather than a
    buffer. An event assigned class 0 is an event that is never reported, so
    assigning one is refused rather than quietly honored.
    """

    CLASS_1 = 1
    CLASS_2 = 2
    CLASS_3 = 3

AnalogEvent dataclass

One analog input change.

Source code in src/py1815/events.py
@dataclass(frozen=True)
class AnalogEvent:
    """One analog input change."""

    index: int
    point: AnalogPoint
    timestamp_ms: int
    #: Where this event falls among every event recorded, across classes.
    #: Not part of what the event says, so two events are still equal
    #: without it; it is what puts a response in the order things happened.
    order: int = field(default=0, compare=False)
    #: The value as the source gave it, for the floating-point variations.
    #: ``point`` carries the value after scaling and rounding to the integer
    #: that decides whether a change is an event. None means the floating-point
    #: variations carry ``point``'s value.
    unscaled: float | None = field(default=None, compare=False)

BinaryEvent dataclass

One binary input change.

Source code in src/py1815/events.py
@dataclass(frozen=True)
class BinaryEvent:
    """One binary input change."""

    index: int
    point: BinaryPoint
    timestamp_ms: int
    #: Where this event falls among every event recorded, across classes.
    #: Not part of what the event says, so two events are still equal
    #: without it; it is what puts a response in the order things happened.
    order: int = field(default=0, compare=False)
    #: Whether the clock that stamped it had been set. An event stamped
    #: by a clock that had not is reported with relative time, behind a
    #: common time that says so.
    synchronized: bool = field(default=True, compare=False)

DoubleBitEvent dataclass

One double-bit binary input change.

Source code in src/py1815/events.py
@dataclass(frozen=True)
class DoubleBitEvent:
    """One double-bit binary input change."""

    index: int
    point: DoubleBitPoint
    timestamp_ms: int
    #: Where this event falls among every event recorded, across classes.
    order: int = field(default=0, compare=False)
    #: Whether the clock that stamped it had been set. An event stamped
    #: by a clock that had not is reported with relative time, behind a
    #: common time that says so.
    synchronized: bool = field(default=True, compare=False)

FrozenCounterEvent dataclass

One counter, as it stood at one freeze.

Source code in src/py1815/events.py
@dataclass(frozen=True)
class FrozenCounterEvent:
    """One counter, as it stood at one freeze."""

    index: int
    point: CounterPoint
    timestamp_ms: int
    #: Where this event falls among every event recorded, across classes.
    #: Not part of what the event says, so two events are still equal
    #: without it; it is what puts a response in the order things happened.
    order: int = field(default=0, compare=False)

CounterEvent dataclass

One change in a counter's flags, with the count it held at the change.

Source code in src/py1815/events.py
@dataclass(frozen=True)
class CounterEvent:
    """One change in a counter's flags, with the count it held at the change."""

    index: int
    point: CounterPoint
    timestamp_ms: int
    #: Where this event falls among every event recorded, across classes.
    order: int = field(default=0, compare=False)

BinaryOutputEvent dataclass

One binary output change: its state or its flags.

Source code in src/py1815/events.py
@dataclass(frozen=True)
class BinaryOutputEvent:
    """One binary output change: its state or its flags."""

    index: int
    point: BinaryPoint
    timestamp_ms: int
    #: Where this event falls among every event recorded, across classes.
    order: int = field(default=0, compare=False)

AnalogOutputEvent dataclass

One analog output change: its value or its flags.

Source code in src/py1815/events.py
@dataclass(frozen=True)
class AnalogOutputEvent:
    """One analog output change: its value or its flags."""

    index: int
    #: The value as the integer variations carry it, with its flags.
    point: AnalogPoint
    #: The same value in engineering units, which the floating-point
    #: variations carry unscaled.
    value: float
    timestamp_ms: int
    #: Where this event falls among every event recorded, across classes.
    order: int = field(default=0, compare=False)

EventBuffers

Per-class event storage, and the deadband rules that fill it.

Holds the last reported value per point so a deadband can be applied against what the master was actually told, rather than against the previous reading. Comparing against the previous reading lets a value drift past the deadband in steps that individually fall inside it, and the master never hears about a change that has already happened.

Source code in src/py1815/events.py
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
class EventBuffers:  # pylint: disable=too-many-public-methods
    """Per-class event storage, and the deadband rules that fill it.

    Holds the last reported value per point so a deadband can be applied against
    what the master was actually told, rather than against the previous reading.
    Comparing against the previous reading lets a value drift past the deadband
    in steps that individually fall inside it, and the master never hears about
    a change that has already happened.
    """

    def __init__(
        self, *, capacity: int = DEFAULT_CAPACITY, analog_latest_only: bool = False
    ) -> None:
        """
        Args:
            capacity: Events each class holds before its oldest is dropped.
            analog_latest_only: Keep one event per analog point, the newest.
                The default keeps every change, which is the sequence of
                record. The alternative is what the IEEE 1815.2 profile
                selects for analog inputs: a measurement that moved five times
                between polls is reported once, at its latest value, and a
                slow master is not handed a backlog of readings it has no use
                for. Binary and frozen counter events are never collapsed,
                because for those the sequence is the information.
        """
        if capacity < 1:
            # Otherwise the first event drops one from an empty buffer, and the
            # IndexError names a deque rather than the configuration.
            raise ValueError(f"capacity is {capacity}; a buffer holds at least one event")
        self._buffers = {cls: _ClassBuffer(capacity) for cls in EventClass}
        self._last_analog: dict[int, AnalogPoint] = {}
        self._last_binary: dict[int, BinaryPoint] = {}
        self._last_double_bit: dict[int, DoubleBitPoint] = {}
        self._last_counter: dict[int, CounterPoint] = {}
        #: The count in each counter's last event, or its first reading.
        self._counter_reported: dict[int, int] = {}
        self._last_binary_output: dict[int, BinaryPoint] = {}
        self._last_analog_output: dict[int, AnalogPoint] = {}
        self._overflow_generation = 0
        self._recorded = 0
        self._analog_latest_only = analog_latest_only
        self._capacity = capacity
        #: The analog event each point currently has buffered, when only the
        #: newest is kept. What makes superseding one a lookup rather than a
        #: scan of every class on every reading.
        self._pending_analog: dict[int, AnalogEvent] = {}
        #: Whether the clock stamping events has been set. A session
        #: that asks its master for the time clears this until the
        #: time is written; binary events recorded meanwhile carry it.
        self.synchronized = True
        self._unsynchronized = 0

    @property
    def holds_unsynchronized(self) -> bool:
        """Whether any buffered binary event was stamped by a clock that had not been set."""
        return self._unsynchronized > 0

    def _forget(self, event: Event) -> None:
        """Stop counting an event that has left the buffers."""
        if isinstance(event, BinaryEvent | DoubleBitEvent) and not event.synchronized:
            self._unsynchronized -= 1

    def count(self, event_class: EventClass) -> int:
        return len(self._buffers[event_class].events)

    @property
    def total(self) -> int:
        return sum(len(buffer.events) for buffer in self._buffers.values())

    def overflowed(self) -> bool:
        """Whether any class has dropped an event since it was last reported."""
        return any(buffer.overflowed for buffer in self._buffers.values())

    @property
    def overflow_generation(self) -> int:
        """How many events have been lost, counted from the first.

        Monotonic, and deliberately not reset by ``clear_overflow``. A
        confirmation acknowledges the loss a particular response reported, and
        the flag alone cannot say whether more has been lost since that response
        was built -- it reads the same after one eviction and after a hundred.
        Clearing on the flag would retire a report of data loss the master has
        never been sent.
        """
        return self._overflow_generation

    @property
    def recorded(self) -> int:
        """How many events have been recorded, counted from the first.

        Monotonic, like :attr:`overflow_generation`, and for a similar reason:
        the count of events held cannot say whether anything new has happened,
        since one may have been retired or evicted for each one recorded. A
        session that has stopped retrying an unsolicited response reads this
        to learn that there is news worth starting again for.
        """
        return self._recorded

    def classes_with_events(self, excluding: Iterable[Event] = ()) -> set[EventClass]:
        """Which classes have something to report, for the indication bits.

        ``excluding`` names events a response is already carrying. A class
        whose every event is in the response being built has nothing *more*
        to report, and saying it has would send the master back for events
        it is holding.
        """
        carried = set(map(id, excluding))
        if not carried:
            return {cls for cls, buffer in self._buffers.items() if buffer.events}
        return {
            cls
            for cls, buffer in self._buffers.items()
            if any(id(event) not in carried for event in buffer.events)
        }

    def _next_order(self) -> int:
        self._recorded += 1
        return self._recorded

    def peek(self, event_class: EventClass, limit: int | None = None) -> list[Event]:
        """The oldest events of a class, without removing them.

        Reads do not consume. An event leaves the buffer when the master
        confirms the response carrying it, because a response that never arrives
        must not have taken the only copy with it.

        ``limit`` is taken from the front, and taken while the deque is walked
        rather than after. The difference is the whole point of the parameter:
        materializing the class and slicing it afterwards costs the buffer on
        every call, and ``capacity`` has no upper bound, so a caller asking for
        the few events that fit a response would still pay for all of them.
        """
        events = self._buffers[event_class].events
        if limit is None:
            return list(events)
        # Clamped to the length as well as to zero. ``islice`` refuses a stop
        # above ``sys.maxsize``, where a plain slice simply returns everything,
        # and a limit is an upper bound rather than a promise -- a caller that
        # names a number larger than the buffer is asking for the buffer.
        return list(islice(events, min(max(0, limit), len(events))))

    def drop(self, events: Iterable[Event]) -> None:
        """Remove events a master has confirmed.

        Matched on identity, not equality, and the difference matters. Events
        are frozen dataclasses, so two genuinely distinct events carrying the
        same index, value and timestamp compare equal -- a master confirming
        one of them would silently drop both. ``id()`` is what tells them
        apart, and tidying this into ``==`` would reintroduce that.

        It is load-bearing that the events passed in are still held by a
        buffer, since that is what keeps them alive and their ids stable for
        the length of this call. True of the ``peek`` then ``drop`` lifecycle
        above; not true of ids kept across a confirm cycle.
        """
        confirmed = set(map(id, events))
        for buffer in self._buffers.values():
            remaining: deque[Event] = deque()
            for event in buffer.events:
                if id(event) in confirmed:
                    self._forget(event)
                else:
                    remaining.append(event)
            buffer.events = remaining
        for index, pending in list(self._pending_analog.items()):
            if id(pending) in confirmed:
                del self._pending_analog[index]

    def _discard(self, event: Event) -> None:
        """Remove one event, found by identity, without rebuilding a buffer.

        ``drop`` rebuilds every class to retire a batch, which is the right
        cost once per confirmation and the wrong one once per reading.
        """
        for buffer in self._buffers.values():
            for position, held in enumerate(buffer.events):
                if held is event:
                    del buffer.events[position]
                    return

    def clear_overflow(self) -> None:
        """Forget the overflow, once it has been reported to a master."""
        for buffer in self._buffers.values():
            buffer.overflowed = False

    def _buffer(self, event_class: EventClass, event: Event) -> None:
        """Hold an event, counting it against the generation if one was lost."""
        buffer = self._buffers[event_class]
        evicted = buffer.events[0] if len(buffer.events) >= buffer.capacity else None
        if buffer.add(event):
            self._overflow_generation += 1
        if evicted is not None:
            self._forget(evicted)
        if isinstance(evicted, AnalogEvent) and self._pending_analog.get(evicted.index) is evicted:
            del self._pending_analog[evicted.index]

    @staticmethod
    def _checked_index(index: int) -> int:
        """An index an event block can actually carry.

        Checked when the event is recorded rather than when it is read. The
        encoder refuses a negative or oversized index, and that refusal arriving
        at read time would take out every read of the class -- a caller's
        mistake surfacing as a protocol failure, far from the line that made it
        and with the connection as collateral. Refusing here puts the error
        where the mistake is and leaves the buffer incapable of holding one.
        """
        if not 0 <= index <= MAX_INDEX:
            raise ValueError(
                f"index {index} is outside 0..{MAX_INDEX}, which is what an event block can carry"
            )
        return index

    def record_analog(  # pylint: disable=too-many-arguments
        self,
        index: int,
        point: AnalogPoint,
        *,
        event_class: EventClass,
        deadband: float = 0.0,
        timestamp_ms: int | None = None,
        unscaled: float | None = None,
    ) -> AnalogEvent | None:
        """Record an analog reading, returning the event it generated, if any.

        ``unscaled`` is the value a floating-point variation reports for the
        event. It plays no part in deciding whether the change is an event.
        """
        self._checked_index(index)
        previous = self._last_analog.get(index)
        if previous is not None and not self._analog_changed(previous, point, deadband):
            return None

        event = AnalogEvent(
            index,
            point,
            timestamp_ms if timestamp_ms is not None else now_ms(),
            order=self._next_order(),
            unscaled=unscaled,
        )
        self._last_analog[index] = point
        if self._analog_latest_only:
            superseded = self._pending_analog.get(index)
            if superseded is not None:
                # Removed rather than overwritten in place: the event moves to
                # the back of its class, because it now reports a change that
                # happened after everything buffered ahead of it.
                self._discard(superseded)
            self._pending_analog[index] = event
        self._buffer(event_class, event)
        return event

    @property
    def capacity(self) -> int:
        """Events each class holds before its oldest is dropped."""
        return self._capacity

    @property
    def analog_latest_only(self) -> bool:
        """Whether only the newest event per analog point is kept."""
        return self._analog_latest_only

    def prime_analog(self, index: int, point: AnalogPoint) -> None:
        """Note where an analog point stands, without reporting it.

        For the first reading after start. A change is measured against the
        last value a master was told, and at start it has been told nothing --
        it learns every point from its integrity poll, so an event per point
        here would report a few hundred changes that did not happen.
        """
        self._last_analog[self._checked_index(index)] = point

    def prime_binary(self, index: int, point: BinaryPoint) -> None:
        """Note where a binary point stands, without reporting it."""
        self._last_binary[self._checked_index(index)] = point

    def prime_double_bit(self, index: int, point: DoubleBitPoint) -> None:
        """Note where a double-bit point stands, without reporting it."""
        self._last_double_bit[self._checked_index(index)] = point

    def prime_counter(self, index: int, point: CounterPoint) -> None:
        """Note a counter's flags and count, without reporting them."""
        self._last_counter[self._checked_index(index)] = point
        self._counter_reported[index] = point.value

    def record_counter(
        self,
        index: int,
        point: CounterPoint,
        *,
        event_class: EventClass,
        value_deadband: float | None = None,
        timestamp_ms: int | None = None,
    ) -> CounterEvent | None:
        """Record a counter reading, returning the event it generated, if any.

        A change in the flags is always an event: IEEE 1815-2012 11.9.5.2 has
        a device that supports counter events generate one whenever a flag
        changes. A change in value is optional there, and is an event only
        when *value_deadband* is given and the count has moved further than
        it from the count in the counter's last event (or its first reading).
        Each event is kept, as binary changes are, because the sequence is
        the information.
        """
        self._checked_index(index)
        previous = self._last_counter.get(index)
        self._last_counter[index] = point
        if previous is None:
            self._counter_reported[index] = point.value
            return None
        reported = self._counter_reported.get(index, previous.value)
        moved = value_deadband is not None and abs(point.value - reported) > value_deadband
        if previous.flags == point.flags and not moved:
            return None
        self._counter_reported[index] = point.value
        event = CounterEvent(
            index,
            point,
            timestamp_ms if timestamp_ms is not None else now_ms(),
            order=self._next_order(),
        )
        self._buffer(event_class, event)
        return event

    def prime_binary_output(self, index: int, point: BinaryPoint) -> None:
        """Note where a binary output stands, without reporting it."""
        self._last_binary_output[self._checked_index(index)] = point

    def prime_analog_output(self, index: int, point: AnalogPoint) -> None:
        """Note where an analog output stands, without reporting it."""
        self._last_analog_output[self._checked_index(index)] = point

    def record_binary_output(
        self,
        index: int,
        point: BinaryPoint,
        *,
        event_class: EventClass,
        timestamp_ms: int | None = None,
    ) -> BinaryOutputEvent | None:
        """Record a binary output's status, returning the event it generated, if any.

        Any change of state or of a quality flag is an event, and a repeat of
        what the master was last told is not (IEEE 1815-2012, A.7.1.1).
        """
        self._checked_index(index)
        previous = self._last_binary_output.get(index)
        if previous is not None and not self._binary_changed(previous, point):
            return None
        event = BinaryOutputEvent(
            index,
            point,
            timestamp_ms if timestamp_ms is not None else now_ms(),
            order=self._next_order(),
        )
        self._last_binary_output[index] = point
        self._buffer(event_class, event)
        return event

    def record_analog_output(
        self,
        index: int,
        point: AnalogPoint,
        value: float,
        *,
        event_class: EventClass,
        timestamp_ms: int | None = None,
    ) -> AnalogOutputEvent | None:
        """Record an analog output's status, returning the event it generated, if any.

        Any change of the transmitted value or of a quality flag is an event.
        Every one is kept: each is a setpoint taking effect, and a master
        reading the sequence is owed all of them.
        """
        self._checked_index(index)
        previous = self._last_analog_output.get(index)
        if previous is not None and not self._analog_changed(previous, point, 0.0):
            return None
        event = AnalogOutputEvent(
            index,
            point,
            value,
            timestamp_ms if timestamp_ms is not None else now_ms(),
            order=self._next_order(),
        )
        self._last_analog_output[index] = point
        self._buffer(event_class, event)
        return event

    def record_frozen_counter(
        self,
        index: int,
        point: CounterPoint,
        *,
        event_class: EventClass,
        timestamp_ms: int | None = None,
    ) -> FrozenCounterEvent:
        """Record one counter's value at a freeze. Always an event.

        No comparison against the previous freeze, unlike the other two. A
        frozen counter event is an entry in a log -- the value at this moment
        -- and two freezes of a counter that did not move are two entries the
        master is owed, since their absence would read as freezes that never
        happened.
        """
        self._checked_index(index)
        event = FrozenCounterEvent(
            index,
            point,
            timestamp_ms if timestamp_ms is not None else now_ms(),
            order=self._next_order(),
        )
        self._buffer(event_class, event)
        return event

    def peek_kind(self, kind: type, limit: int | None = None) -> list[Event]:
        """The oldest events of one type, across every class, without removing them.

        For a master that reads an event group by name rather than a class.
        Oldest first whichever class each is in: a master that asks for the
        next three binary changes is asking for the three that happened first,
        and taking them a class at a time would hand it a later change ahead
        of an earlier one.

        Each class is already in order, so the oldest ``limit`` overall are
        among the oldest ``limit`` of each, and no class is walked further.
        """
        found: list[Event] = []
        for event_class in EventClass:
            taken = 0
            for event in self._buffers[event_class].events:
                if limit is not None and taken >= limit:
                    break
                if isinstance(event, kind):
                    found.append(event)
                    taken += 1
        found.sort(key=lambda event: event.order)
        return found if limit is None else found[: max(0, limit)]

    def record_binary(
        self,
        index: int,
        point: BinaryPoint,
        *,
        event_class: EventClass,
        timestamp_ms: int | None = None,
        synchronized: bool | None = None,
    ) -> BinaryEvent | None:
        """Record a binary reading, returning the event it generated, if any.

        No deadband: a binary point has two values, so any change is the whole
        of its range.

        ``synchronized`` says whether the clock behind the timestamp had
        been set, and defaults to what these buffers were last told.
        """
        self._checked_index(index)
        previous = self._last_binary.get(index)
        if previous is not None and not self._binary_changed(previous, point):
            return None

        event = BinaryEvent(
            index,
            point,
            timestamp_ms if timestamp_ms is not None else now_ms(),
            order=self._next_order(),
            synchronized=self.synchronized if synchronized is None else synchronized,
        )
        if not event.synchronized:
            self._unsynchronized += 1
        self._last_binary[index] = point
        self._buffer(event_class, event)
        return event

    def record_double_bit(
        self,
        index: int,
        point: DoubleBitPoint,
        *,
        event_class: EventClass,
        timestamp_ms: int | None = None,
        synchronized: bool | None = None,
    ) -> DoubleBitEvent | None:
        """Record a double-bit reading, returning the event it generated, if any.

        A change of state or of any quality flag is an event, and one event
        covers both bits of the state changing together (IEEE 1815-2012 11.9.6).
        """
        self._checked_index(index)
        previous = self._last_double_bit.get(index)
        if previous is not None and not self._double_bit_changed(previous, point):
            return None

        event = DoubleBitEvent(
            index,
            point,
            timestamp_ms if timestamp_ms is not None else now_ms(),
            order=self._next_order(),
            synchronized=self.synchronized if synchronized is None else synchronized,
        )
        if not event.synchronized:
            self._unsynchronized += 1
        self._last_double_bit[index] = point
        self._buffer(event_class, event)
        return event

    @staticmethod
    def _double_bit_changed(previous: DoubleBitPoint, current: DoubleBitPoint) -> bool:
        return previous.state != current.state or previous.flags != current.flags

    @staticmethod
    def _analog_changed(previous: AnalogPoint, current: AnalogPoint, deadband: float) -> bool:
        if _analog_quality_of(previous.flags) != _analog_quality_of(current.flags):
            # Quality first and unconditionally. A point that goes comm-lost
            # holding the same number has not moved and has changed in the way
            # that matters, and a deadband applied here would hide it.
            return True

        # NaN before the deadband, because every comparison involving it is
        # false -- so a reading that went NaN would look unchanged, the master
        # would never hear about it, and the encoder's REFERENCE_ERR would never
        # reach the wire. The recovery back to a real number would be lost the
        # same way. NaN to NaN is genuinely unchanged.
        was_nan, is_nan = math.isnan(previous.value), math.isnan(current.value)
        if was_nan or is_nan:
            return was_nan != is_nan

        return abs(current.value - previous.value) > deadband

    @staticmethod
    def _binary_changed(previous: BinaryPoint, current: BinaryPoint) -> bool:
        if previous.state != current.state:
            return True
        return _binary_quality_of(previous.flags) != _binary_quality_of(current.flags)
holds_unsynchronized property
holds_unsynchronized: bool

Whether any buffered binary event was stamped by a clock that had not been set.

overflow_generation property
overflow_generation: int

How many events have been lost, counted from the first.

Monotonic, and deliberately not reset by clear_overflow. A confirmation acknowledges the loss a particular response reported, and the flag alone cannot say whether more has been lost since that response was built -- it reads the same after one eviction and after a hundred. Clearing on the flag would retire a report of data loss the master has never been sent.

recorded property
recorded: int

How many events have been recorded, counted from the first.

Monotonic, like :attr:overflow_generation, and for a similar reason: the count of events held cannot say whether anything new has happened, since one may have been retired or evicted for each one recorded. A session that has stopped retrying an unsolicited response reads this to learn that there is news worth starting again for.

capacity property
capacity: int

Events each class holds before its oldest is dropped.

analog_latest_only property
analog_latest_only: bool

Whether only the newest event per analog point is kept.

overflowed
overflowed() -> bool

Whether any class has dropped an event since it was last reported.

Source code in src/py1815/events.py
def overflowed(self) -> bool:
    """Whether any class has dropped an event since it was last reported."""
    return any(buffer.overflowed for buffer in self._buffers.values())
classes_with_events
classes_with_events(
    excluding: Iterable[Event] = (),
) -> set[EventClass]

Which classes have something to report, for the indication bits.

excluding names events a response is already carrying. A class whose every event is in the response being built has nothing more to report, and saying it has would send the master back for events it is holding.

Source code in src/py1815/events.py
def classes_with_events(self, excluding: Iterable[Event] = ()) -> set[EventClass]:
    """Which classes have something to report, for the indication bits.

    ``excluding`` names events a response is already carrying. A class
    whose every event is in the response being built has nothing *more*
    to report, and saying it has would send the master back for events
    it is holding.
    """
    carried = set(map(id, excluding))
    if not carried:
        return {cls for cls, buffer in self._buffers.items() if buffer.events}
    return {
        cls
        for cls, buffer in self._buffers.items()
        if any(id(event) not in carried for event in buffer.events)
    }
peek
peek(
    event_class: EventClass, limit: int | None = None
) -> list[Event]

The oldest events of a class, without removing them.

Reads do not consume. An event leaves the buffer when the master confirms the response carrying it, because a response that never arrives must not have taken the only copy with it.

limit is taken from the front, and taken while the deque is walked rather than after. The difference is the whole point of the parameter: materializing the class and slicing it afterwards costs the buffer on every call, and capacity has no upper bound, so a caller asking for the few events that fit a response would still pay for all of them.

Source code in src/py1815/events.py
def peek(self, event_class: EventClass, limit: int | None = None) -> list[Event]:
    """The oldest events of a class, without removing them.

    Reads do not consume. An event leaves the buffer when the master
    confirms the response carrying it, because a response that never arrives
    must not have taken the only copy with it.

    ``limit`` is taken from the front, and taken while the deque is walked
    rather than after. The difference is the whole point of the parameter:
    materializing the class and slicing it afterwards costs the buffer on
    every call, and ``capacity`` has no upper bound, so a caller asking for
    the few events that fit a response would still pay for all of them.
    """
    events = self._buffers[event_class].events
    if limit is None:
        return list(events)
    # Clamped to the length as well as to zero. ``islice`` refuses a stop
    # above ``sys.maxsize``, where a plain slice simply returns everything,
    # and a limit is an upper bound rather than a promise -- a caller that
    # names a number larger than the buffer is asking for the buffer.
    return list(islice(events, min(max(0, limit), len(events))))
drop
drop(events: Iterable[Event]) -> None

Remove events a master has confirmed.

Matched on identity, not equality, and the difference matters. Events are frozen dataclasses, so two genuinely distinct events carrying the same index, value and timestamp compare equal -- a master confirming one of them would silently drop both. id() is what tells them apart, and tidying this into == would reintroduce that.

It is load-bearing that the events passed in are still held by a buffer, since that is what keeps them alive and their ids stable for the length of this call. True of the peek then drop lifecycle above; not true of ids kept across a confirm cycle.

Source code in src/py1815/events.py
def drop(self, events: Iterable[Event]) -> None:
    """Remove events a master has confirmed.

    Matched on identity, not equality, and the difference matters. Events
    are frozen dataclasses, so two genuinely distinct events carrying the
    same index, value and timestamp compare equal -- a master confirming
    one of them would silently drop both. ``id()`` is what tells them
    apart, and tidying this into ``==`` would reintroduce that.

    It is load-bearing that the events passed in are still held by a
    buffer, since that is what keeps them alive and their ids stable for
    the length of this call. True of the ``peek`` then ``drop`` lifecycle
    above; not true of ids kept across a confirm cycle.
    """
    confirmed = set(map(id, events))
    for buffer in self._buffers.values():
        remaining: deque[Event] = deque()
        for event in buffer.events:
            if id(event) in confirmed:
                self._forget(event)
            else:
                remaining.append(event)
        buffer.events = remaining
    for index, pending in list(self._pending_analog.items()):
        if id(pending) in confirmed:
            del self._pending_analog[index]
clear_overflow
clear_overflow() -> None

Forget the overflow, once it has been reported to a master.

Source code in src/py1815/events.py
def clear_overflow(self) -> None:
    """Forget the overflow, once it has been reported to a master."""
    for buffer in self._buffers.values():
        buffer.overflowed = False
record_analog
record_analog(
    index: int,
    point: AnalogPoint,
    *,
    event_class: EventClass,
    deadband: float = 0.0,
    timestamp_ms: int | None = None,
    unscaled: float | None = None,
) -> AnalogEvent | None

Record an analog reading, returning the event it generated, if any.

unscaled is the value a floating-point variation reports for the event. It plays no part in deciding whether the change is an event.

Source code in src/py1815/events.py
def record_analog(  # pylint: disable=too-many-arguments
    self,
    index: int,
    point: AnalogPoint,
    *,
    event_class: EventClass,
    deadband: float = 0.0,
    timestamp_ms: int | None = None,
    unscaled: float | None = None,
) -> AnalogEvent | None:
    """Record an analog reading, returning the event it generated, if any.

    ``unscaled`` is the value a floating-point variation reports for the
    event. It plays no part in deciding whether the change is an event.
    """
    self._checked_index(index)
    previous = self._last_analog.get(index)
    if previous is not None and not self._analog_changed(previous, point, deadband):
        return None

    event = AnalogEvent(
        index,
        point,
        timestamp_ms if timestamp_ms is not None else now_ms(),
        order=self._next_order(),
        unscaled=unscaled,
    )
    self._last_analog[index] = point
    if self._analog_latest_only:
        superseded = self._pending_analog.get(index)
        if superseded is not None:
            # Removed rather than overwritten in place: the event moves to
            # the back of its class, because it now reports a change that
            # happened after everything buffered ahead of it.
            self._discard(superseded)
        self._pending_analog[index] = event
    self._buffer(event_class, event)
    return event
prime_analog
prime_analog(index: int, point: AnalogPoint) -> None

Note where an analog point stands, without reporting it.

For the first reading after start. A change is measured against the last value a master was told, and at start it has been told nothing -- it learns every point from its integrity poll, so an event per point here would report a few hundred changes that did not happen.

Source code in src/py1815/events.py
def prime_analog(self, index: int, point: AnalogPoint) -> None:
    """Note where an analog point stands, without reporting it.

    For the first reading after start. A change is measured against the
    last value a master was told, and at start it has been told nothing --
    it learns every point from its integrity poll, so an event per point
    here would report a few hundred changes that did not happen.
    """
    self._last_analog[self._checked_index(index)] = point
prime_binary
prime_binary(index: int, point: BinaryPoint) -> None

Note where a binary point stands, without reporting it.

Source code in src/py1815/events.py
def prime_binary(self, index: int, point: BinaryPoint) -> None:
    """Note where a binary point stands, without reporting it."""
    self._last_binary[self._checked_index(index)] = point
prime_double_bit
prime_double_bit(index: int, point: DoubleBitPoint) -> None

Note where a double-bit point stands, without reporting it.

Source code in src/py1815/events.py
def prime_double_bit(self, index: int, point: DoubleBitPoint) -> None:
    """Note where a double-bit point stands, without reporting it."""
    self._last_double_bit[self._checked_index(index)] = point
prime_counter
prime_counter(index: int, point: CounterPoint) -> None

Note a counter's flags and count, without reporting them.

Source code in src/py1815/events.py
def prime_counter(self, index: int, point: CounterPoint) -> None:
    """Note a counter's flags and count, without reporting them."""
    self._last_counter[self._checked_index(index)] = point
    self._counter_reported[index] = point.value
record_counter
record_counter(
    index: int,
    point: CounterPoint,
    *,
    event_class: EventClass,
    value_deadband: float | None = None,
    timestamp_ms: int | None = None,
) -> CounterEvent | None

Record a counter reading, returning the event it generated, if any.

A change in the flags is always an event: IEEE 1815-2012 11.9.5.2 has a device that supports counter events generate one whenever a flag changes. A change in value is optional there, and is an event only when value_deadband is given and the count has moved further than it from the count in the counter's last event (or its first reading). Each event is kept, as binary changes are, because the sequence is the information.

Source code in src/py1815/events.py
def record_counter(
    self,
    index: int,
    point: CounterPoint,
    *,
    event_class: EventClass,
    value_deadband: float | None = None,
    timestamp_ms: int | None = None,
) -> CounterEvent | None:
    """Record a counter reading, returning the event it generated, if any.

    A change in the flags is always an event: IEEE 1815-2012 11.9.5.2 has
    a device that supports counter events generate one whenever a flag
    changes. A change in value is optional there, and is an event only
    when *value_deadband* is given and the count has moved further than
    it from the count in the counter's last event (or its first reading).
    Each event is kept, as binary changes are, because the sequence is
    the information.
    """
    self._checked_index(index)
    previous = self._last_counter.get(index)
    self._last_counter[index] = point
    if previous is None:
        self._counter_reported[index] = point.value
        return None
    reported = self._counter_reported.get(index, previous.value)
    moved = value_deadband is not None and abs(point.value - reported) > value_deadband
    if previous.flags == point.flags and not moved:
        return None
    self._counter_reported[index] = point.value
    event = CounterEvent(
        index,
        point,
        timestamp_ms if timestamp_ms is not None else now_ms(),
        order=self._next_order(),
    )
    self._buffer(event_class, event)
    return event
prime_binary_output
prime_binary_output(index: int, point: BinaryPoint) -> None

Note where a binary output stands, without reporting it.

Source code in src/py1815/events.py
def prime_binary_output(self, index: int, point: BinaryPoint) -> None:
    """Note where a binary output stands, without reporting it."""
    self._last_binary_output[self._checked_index(index)] = point
prime_analog_output
prime_analog_output(index: int, point: AnalogPoint) -> None

Note where an analog output stands, without reporting it.

Source code in src/py1815/events.py
def prime_analog_output(self, index: int, point: AnalogPoint) -> None:
    """Note where an analog output stands, without reporting it."""
    self._last_analog_output[self._checked_index(index)] = point
record_binary_output
record_binary_output(
    index: int,
    point: BinaryPoint,
    *,
    event_class: EventClass,
    timestamp_ms: int | None = None,
) -> BinaryOutputEvent | None

Record a binary output's status, returning the event it generated, if any.

Any change of state or of a quality flag is an event, and a repeat of what the master was last told is not (IEEE 1815-2012, A.7.1.1).

Source code in src/py1815/events.py
def record_binary_output(
    self,
    index: int,
    point: BinaryPoint,
    *,
    event_class: EventClass,
    timestamp_ms: int | None = None,
) -> BinaryOutputEvent | None:
    """Record a binary output's status, returning the event it generated, if any.

    Any change of state or of a quality flag is an event, and a repeat of
    what the master was last told is not (IEEE 1815-2012, A.7.1.1).
    """
    self._checked_index(index)
    previous = self._last_binary_output.get(index)
    if previous is not None and not self._binary_changed(previous, point):
        return None
    event = BinaryOutputEvent(
        index,
        point,
        timestamp_ms if timestamp_ms is not None else now_ms(),
        order=self._next_order(),
    )
    self._last_binary_output[index] = point
    self._buffer(event_class, event)
    return event
record_analog_output
record_analog_output(
    index: int,
    point: AnalogPoint,
    value: float,
    *,
    event_class: EventClass,
    timestamp_ms: int | None = None,
) -> AnalogOutputEvent | None

Record an analog output's status, returning the event it generated, if any.

Any change of the transmitted value or of a quality flag is an event. Every one is kept: each is a setpoint taking effect, and a master reading the sequence is owed all of them.

Source code in src/py1815/events.py
def record_analog_output(
    self,
    index: int,
    point: AnalogPoint,
    value: float,
    *,
    event_class: EventClass,
    timestamp_ms: int | None = None,
) -> AnalogOutputEvent | None:
    """Record an analog output's status, returning the event it generated, if any.

    Any change of the transmitted value or of a quality flag is an event.
    Every one is kept: each is a setpoint taking effect, and a master
    reading the sequence is owed all of them.
    """
    self._checked_index(index)
    previous = self._last_analog_output.get(index)
    if previous is not None and not self._analog_changed(previous, point, 0.0):
        return None
    event = AnalogOutputEvent(
        index,
        point,
        value,
        timestamp_ms if timestamp_ms is not None else now_ms(),
        order=self._next_order(),
    )
    self._last_analog_output[index] = point
    self._buffer(event_class, event)
    return event
record_frozen_counter
record_frozen_counter(
    index: int,
    point: CounterPoint,
    *,
    event_class: EventClass,
    timestamp_ms: int | None = None,
) -> FrozenCounterEvent

Record one counter's value at a freeze. Always an event.

No comparison against the previous freeze, unlike the other two. A frozen counter event is an entry in a log -- the value at this moment -- and two freezes of a counter that did not move are two entries the master is owed, since their absence would read as freezes that never happened.

Source code in src/py1815/events.py
def record_frozen_counter(
    self,
    index: int,
    point: CounterPoint,
    *,
    event_class: EventClass,
    timestamp_ms: int | None = None,
) -> FrozenCounterEvent:
    """Record one counter's value at a freeze. Always an event.

    No comparison against the previous freeze, unlike the other two. A
    frozen counter event is an entry in a log -- the value at this moment
    -- and two freezes of a counter that did not move are two entries the
    master is owed, since their absence would read as freezes that never
    happened.
    """
    self._checked_index(index)
    event = FrozenCounterEvent(
        index,
        point,
        timestamp_ms if timestamp_ms is not None else now_ms(),
        order=self._next_order(),
    )
    self._buffer(event_class, event)
    return event
peek_kind
peek_kind(
    kind: type, limit: int | None = None
) -> list[Event]

The oldest events of one type, across every class, without removing them.

For a master that reads an event group by name rather than a class. Oldest first whichever class each is in: a master that asks for the next three binary changes is asking for the three that happened first, and taking them a class at a time would hand it a later change ahead of an earlier one.

Each class is already in order, so the oldest limit overall are among the oldest limit of each, and no class is walked further.

Source code in src/py1815/events.py
def peek_kind(self, kind: type, limit: int | None = None) -> list[Event]:
    """The oldest events of one type, across every class, without removing them.

    For a master that reads an event group by name rather than a class.
    Oldest first whichever class each is in: a master that asks for the
    next three binary changes is asking for the three that happened first,
    and taking them a class at a time would hand it a later change ahead
    of an earlier one.

    Each class is already in order, so the oldest ``limit`` overall are
    among the oldest ``limit`` of each, and no class is walked further.
    """
    found: list[Event] = []
    for event_class in EventClass:
        taken = 0
        for event in self._buffers[event_class].events:
            if limit is not None and taken >= limit:
                break
            if isinstance(event, kind):
                found.append(event)
                taken += 1
    found.sort(key=lambda event: event.order)
    return found if limit is None else found[: max(0, limit)]
record_binary
record_binary(
    index: int,
    point: BinaryPoint,
    *,
    event_class: EventClass,
    timestamp_ms: int | None = None,
    synchronized: bool | None = None,
) -> BinaryEvent | None

Record a binary reading, returning the event it generated, if any.

No deadband: a binary point has two values, so any change is the whole of its range.

synchronized says whether the clock behind the timestamp had been set, and defaults to what these buffers were last told.

Source code in src/py1815/events.py
def record_binary(
    self,
    index: int,
    point: BinaryPoint,
    *,
    event_class: EventClass,
    timestamp_ms: int | None = None,
    synchronized: bool | None = None,
) -> BinaryEvent | None:
    """Record a binary reading, returning the event it generated, if any.

    No deadband: a binary point has two values, so any change is the whole
    of its range.

    ``synchronized`` says whether the clock behind the timestamp had
    been set, and defaults to what these buffers were last told.
    """
    self._checked_index(index)
    previous = self._last_binary.get(index)
    if previous is not None and not self._binary_changed(previous, point):
        return None

    event = BinaryEvent(
        index,
        point,
        timestamp_ms if timestamp_ms is not None else now_ms(),
        order=self._next_order(),
        synchronized=self.synchronized if synchronized is None else synchronized,
    )
    if not event.synchronized:
        self._unsynchronized += 1
    self._last_binary[index] = point
    self._buffer(event_class, event)
    return event
record_double_bit
record_double_bit(
    index: int,
    point: DoubleBitPoint,
    *,
    event_class: EventClass,
    timestamp_ms: int | None = None,
    synchronized: bool | None = None,
) -> DoubleBitEvent | None

Record a double-bit reading, returning the event it generated, if any.

A change of state or of any quality flag is an event, and one event covers both bits of the state changing together (IEEE 1815-2012 11.9.6).

Source code in src/py1815/events.py
def record_double_bit(
    self,
    index: int,
    point: DoubleBitPoint,
    *,
    event_class: EventClass,
    timestamp_ms: int | None = None,
    synchronized: bool | None = None,
) -> DoubleBitEvent | None:
    """Record a double-bit reading, returning the event it generated, if any.

    A change of state or of any quality flag is an event, and one event
    covers both bits of the state changing together (IEEE 1815-2012 11.9.6).
    """
    self._checked_index(index)
    previous = self._last_double_bit.get(index)
    if previous is not None and not self._double_bit_changed(previous, point):
        return None

    event = DoubleBitEvent(
        index,
        point,
        timestamp_ms if timestamp_ms is not None else now_ms(),
        order=self._next_order(),
        synchronized=self.synchronized if synchronized is None else synchronized,
    )
    if not event.synchronized:
        self._unsynchronized += 1
    self._last_double_bit[index] = point
    self._buffer(event_class, event)
    return event

now_ms

now_ms() -> int

Wall-clock milliseconds, which is what a DNP3 timestamp carries.

Wall clock rather than monotonic, deliberately: the timestamp is for the master's sequence of record, and a monotonic reading means nothing to it. A clock that steps backwards produces events out of order, which is a real hazard and the operator's to resolve -- not one to be hidden here by inventing times that look plausible.

Source code in src/py1815/events.py
def now_ms() -> int:
    """Wall-clock milliseconds, which is what a DNP3 timestamp carries.

    Wall clock rather than monotonic, deliberately: the timestamp is for the
    master's sequence of record, and a monotonic reading means nothing to it. A
    clock that steps backwards produces events out of order, which is a real
    hazard and the operator's to resolve -- not one to be hidden here by
    inventing times that look plausible.
    """
    return int(time.time() * 1000)