Skip to content

Commit

Permalink
Report includes machine states
Browse files Browse the repository at this point in the history
 - ReportEvent updated to version 002 (if Report.Version is 002)
 - Add enum pico.cycler.statey
  • Loading branch information
jessicamillar committed Nov 9, 2024
1 parent d60d989 commit f57e871
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/gwproto/enums/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from gwproto.enums.hz_calc_method import HzCalcMethod
from gwproto.enums.kind_of_param import KindOfParam
from gwproto.enums.make_model import MakeModel
from gwproto.enums.pico_cycler_state import PicoCyclerState
from gwproto.enums.relay_closed_or_open import RelayClosedOrOpen
from gwproto.enums.relay_energization_state import RelayEnergizationState
from gwproto.enums.relay_pin_set import RelayPinSet
Expand Down Expand Up @@ -77,6 +78,7 @@
"HzCalcMethod", # [hz.calc.method.000](https://gridworks-type-registry.readthedocs.io/en/latest/enums.html#hzcalcmethod)
"KindOfParam", # [spaceheat.kind.of.param.000](https://gridworks-type-registry.readthedocs.io/en/latest/enums.html#spaceheatkindofparam)
"MakeModel", # [spaceheat.make.model.003](https://gridworks-type-registry.readthedocs.io/en/latest/enums.html#spaceheatmakemodel)
"PicoCyclerState", # [pico.cycler.state.000](https://gridworks-type-registry.readthedocs.io/en/latest/enums.html#picocyclerstate)
"RelayClosedOrOpen", # [relay.closed.or.open.000](https://gridworks-type-registry.readthedocs.io/en/latest/enums.html#relayclosedoropen)
"RelayEnergizationState", # [relay.energization.state.000](https://gridworks-type-registry.readthedocs.io/en/latest/enums.html#relayenergizationstate)
"RelayPinSet", # [relay.pin.set.000](https://gridworks-type-registry.readthedocs.io/en/latest/enums.html#relaypinset)
Expand Down
42 changes: 42 additions & 0 deletions src/gwproto/enums/pico_cycler_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from enum import auto
from typing import List

from gw.enums import GwStrEnum


class PicoCyclerState(GwStrEnum):
"""
Values:
- PicosLive
- RelayOpening
- RelayOpen
- RelayClosing
- PicosRebooting
For more information:
- [ASLs](https://gridworks-type-registry.readthedocs.io/en/latest/)
- [Global Authority](https://gridworks-type-registry.readthedocs.io/en/latest/enums.html#picocyclerstate)
"""

PicosLive = auto()
RelayOpening = auto()
RelayOpen = auto()
RelayClosing = auto()
PicosRebooting = auto()

@classmethod
def default(cls) -> "PicoCyclerState":
return cls.PicosLive

@classmethod
def values(cls) -> List[str]:
return [elt.value for elt in cls]

@classmethod
def enum_name(cls) -> str:
return "pico.cycler.state"

@classmethod
def enum_version(cls) -> str:
return "000"
6 changes: 5 additions & 1 deletion src/gwproto/messages/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,14 @@ class PeerActiveEvent(CommEvent):
class ReportEvent(EventBase):
Report: Report
TypeName: Literal["report.event"] = "report.event"
Version: Literal["000"] = "000"
Version: Literal["000", "002"] = "002"

def __init__(self, **data: dict[str, Any]) -> None:
super().__init__(**data)
if self.Report.Version == "001":
self.Version = "000"
elif self.Report.Version == "002":
self.Version = "002"
self.MessageId = self.Report.Id
self.TimeCreatedMs = self.Report.MessageCreatedMs

Expand Down
2 changes: 2 additions & 0 deletions src/gwproto/named_types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
)
from gwproto.named_types.keyparam_change_log import KeyparamChangeLog
from gwproto.named_types.layout_lite import LayoutLite
from gwproto.named_types.machine_states import MachineStates
from gwproto.named_types.pico_flow_module_component_gt import PicoFlowModuleComponentGt
from gwproto.named_types.pico_missing import PicoMissing
from gwproto.named_types.pico_tank_module_component_gt import PicoTankModuleComponentGt
Expand Down Expand Up @@ -75,6 +76,7 @@
"I2cMultichannelDtRelayComponentGt",
"KeyparamChangeLog",
"LayoutLite",
"MachineStates",
"PicoFlowModuleComponentGt",
"PicoMissing",
"PicoTankModuleComponentGt",
Expand Down
41 changes: 41 additions & 0 deletions src/gwproto/named_types/machine_states.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Type machine.states, version 000"""

from typing import List, Literal

from pydantic import BaseModel, model_validator
from typing_extensions import Self

from gwproto.property_format import (
HandleName,
LeftRightDotStr,
UTCMilliseconds,
)


class MachineStates(BaseModel):
""" """

MachineHandle: HandleName
StateEnum: LeftRightDotStr
StateList: List[str]
UnixMsList: List[UTCMilliseconds]
TypeName: Literal["machine.states"] = "machine.states"
Version: Literal["000"] = "000"

@model_validator(mode="after")
def check_axiom_1(self) -> Self:
"""
Axiom 1: List Length Consistency.
StateList and UnixMsList must have the same length
"""
# Implement check for axiom 1"
return self

@model_validator(mode="after")
def check_axiom_2(self) -> Self:
"""
Axiom 2: If StateEnum is a recognized GridWorks enum, then the StateList elements are all values of that enum..
"""
# Implement check for axiom 2"
return self
12 changes: 5 additions & 7 deletions src/gwproto/named_types/report.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Type report, version 001"""
"""Type report, version 002"""

from typing import List, Literal

from pydantic import BaseModel, PositiveInt, field_validator # Count:true
from pydantic import BaseModel, PositiveInt, field_validator

from gwproto.named_types.channel_readings import ChannelReadings
from gwproto.named_types.fsm_atomic_report import FsmAtomicReport
from gwproto.named_types.fsm_full_report import FsmFullReport
from gwproto.named_types.machine_states import MachineStates
from gwproto.property_format import (
LeftRightDotStr,
UTCMilliseconds,
Expand All @@ -16,20 +16,18 @@


class Report(BaseModel):
""" """

FromGNodeAlias: LeftRightDotStr
FromGNodeInstanceId: UUID4Str
AboutGNodeAlias: LeftRightDotStr
SlotStartUnixS: UTCSeconds
SlotDurationS: PositiveInt
ChannelReadingList: List[ChannelReadings]
FsmActionList: List[FsmAtomicReport]
StateList: List[MachineStates]
FsmReportList: List[FsmFullReport]
MessageCreatedMs: UTCMilliseconds
Id: UUID4Str
TypeName: Literal["report"] = "report"
Version: Literal["001"] = "001"
Version: Literal["002"] = "002"

@field_validator("ChannelReadingList")
@classmethod
Expand Down
4 changes: 2 additions & 2 deletions tests/data/report_message.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
"Version":"002"
}
],
"FsmActionList": [],
"StateList": [],
"FsmReportList": [],
"Id":"4dab57dd-8b4e-4ea4-90a3-d63df9eeb061",
"TypeName":"report",
"Version":"001"
"Version":"002"
}
},
"TypeName": "gw"
Expand Down
19 changes: 19 additions & 0 deletions tests/enums/pico_cycler_state_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
Tests for enum pico.cycler.state.000 from the GridWorks Type Registry.
"""

from gwproto.enums import PicoCyclerState


def test_pico_cycler_state() -> None:
assert set(PicoCyclerState.values()) == {
"PicosLive",
"RelayOpening",
"RelayOpen",
"RelayClosing",
"PicosRebooting",
}

assert PicoCyclerState.default() == PicoCyclerState.PicosLive
assert PicoCyclerState.enum_name() == "pico.cycler.state"
assert PicoCyclerState.enum_version() == "000"
18 changes: 18 additions & 0 deletions tests/named_types/test_machine_states.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Tests machine.states type, version 000"""

from gwproto.named_types import MachineStates


def test_machine_states_generated() -> None:
d = {
"MachineHandle": "h.pico-cycler",
"StateEnum": "pico.cycler.state",
"StateList": ["PicosLive"],
"UnixMsList": [1731168353695],
"TypeName": "machine.states",
"Version": "000",
}

d2 = MachineStates.model_validate(d).model_dump(exclude_none=True)

assert d2 == d
13 changes: 11 additions & 2 deletions tests/named_types/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,21 @@ def test_report_generated() -> None:
"Version": "002",
},
],
"FsmActionList": [],
"StateList": [
{
"MachineHandle": "h.pico-cycler",
"StateEnum": "pico.cycler.state",
"StateList": ["PicosLive"],
"UnixMsList": [1731168353695],
"TypeName": "machine.states",
"Version": "000",
}
],
"FsmReportList": [],
"MessageCreatedMs": 1656945600044,
"Id": "4dab57dd-8b4e-4ea4-90a3-d63df9eeb061",
"TypeName": "report",
"Version": "001",
"Version": "002",
}

d2 = Report.model_validate(d).model_dump(exclude_none=True)
Expand Down

0 comments on commit f57e871

Please sign in to comment.