jmri_server.wire_states

Wire-format state translation.

JMRI's JSON API encodes UNKNOWN as 0 on the wire, while the internal NamedBean.UNKNOWN = 1 matches JMRI's Java integer constants. This module isolates the translation so service code can deal in the wire form unambiguously.

JSON wire format (per the Stage 0 schemas): 0 = UNKNOWN 2 = ACTIVE / CLOSED / ON 4 = INACTIVE / THROWN / OFF 8 = INCONSISTENT / INTERMEDIATE

 1"""Wire-format state translation.
 2
 3JMRI's JSON API encodes ``UNKNOWN`` as ``0`` on the wire, while the
 4internal ``NamedBean.UNKNOWN = 1`` matches JMRI's Java integer constants.
 5This module isolates the translation so service code can deal in the wire
 6form unambiguously.
 7
 8JSON wire format (per the Stage 0 schemas):
 9  0 = UNKNOWN
10  2 = ACTIVE   / CLOSED  / ON
11  4 = INACTIVE / THROWN  / OFF
12  8 = INCONSISTENT / INTERMEDIATE
13"""
14from __future__ import annotations
15
16from jmri_core.state import (
17    BlockState,
18    LightState,
19    PowerState,
20    SensorState,
21    TurnoutState,
22)
23
24WIRE_UNKNOWN = 0
25
26
27def to_wire(internal: int) -> int:
28    """Translate an internal IntEnum value to its JMRI JSON wire form."""
29    return WIRE_UNKNOWN if internal == 1 else internal
30
31
32def turnout_from_wire(wire: int) -> TurnoutState:
33    """Translate a wire-format turnout state back to the internal enum."""
34    if wire == WIRE_UNKNOWN:
35        return TurnoutState.UNKNOWN
36    return TurnoutState(wire)
37
38
39def sensor_from_wire(wire: int) -> SensorState:
40    if wire == WIRE_UNKNOWN:
41        return SensorState.UNKNOWN
42    return SensorState(wire)
43
44
45def light_from_wire(wire: int) -> LightState:
46    if wire == WIRE_UNKNOWN:
47        return LightState.UNKNOWN
48    return LightState(wire)
49
50
51def power_from_wire(wire: int) -> PowerState:
52    if wire == WIRE_UNKNOWN:
53        return PowerState.UNKNOWN
54    return PowerState(wire)
55
56
57def block_from_wire(wire: int) -> BlockState:
58    if wire == WIRE_UNKNOWN:
59        return BlockState.UNKNOWN
60    return BlockState(wire)
WIRE_UNKNOWN = 0
def to_wire(internal: int) -> int:
28def to_wire(internal: int) -> int:
29    """Translate an internal IntEnum value to its JMRI JSON wire form."""
30    return WIRE_UNKNOWN if internal == 1 else internal

Translate an internal IntEnum value to its JMRI JSON wire form.

def turnout_from_wire(wire: int) -> jmri_core.state.TurnoutState:
33def turnout_from_wire(wire: int) -> TurnoutState:
34    """Translate a wire-format turnout state back to the internal enum."""
35    if wire == WIRE_UNKNOWN:
36        return TurnoutState.UNKNOWN
37    return TurnoutState(wire)

Translate a wire-format turnout state back to the internal enum.

def sensor_from_wire(wire: int) -> jmri_core.state.SensorState:
40def sensor_from_wire(wire: int) -> SensorState:
41    if wire == WIRE_UNKNOWN:
42        return SensorState.UNKNOWN
43    return SensorState(wire)
def light_from_wire(wire: int) -> jmri_core.state.LightState:
46def light_from_wire(wire: int) -> LightState:
47    if wire == WIRE_UNKNOWN:
48        return LightState.UNKNOWN
49    return LightState(wire)
def power_from_wire(wire: int) -> jmri_core.state.PowerState:
52def power_from_wire(wire: int) -> PowerState:
53    if wire == WIRE_UNKNOWN:
54        return PowerState.UNKNOWN
55    return PowerState(wire)
def block_from_wire(wire: int) -> jmri_core.state.BlockState:
58def block_from_wire(wire: int) -> BlockState:
59    if wire == WIRE_UNKNOWN:
60        return BlockState.UNKNOWN
61    return BlockState(wire)