From ba6c3c737a954fa76cd92db24785f2f96c07d3b9 Mon Sep 17 00:00:00 2001 From: "Sergey V. DUDANOV" Date: Tue, 22 Oct 2024 13:07:36 +0400 Subject: [PATCH] meta model changes --- pyftms/models/realtime_data/common.py | 3 ++- pyftms/models/realtime_data/cross_trainer.py | 27 ++++++++++---------- pyftms/models/realtime_data/indoor_bike.py | 24 ++++++++--------- pyftms/models/realtime_data/rower.py | 26 +++++++++---------- pyftms/models/realtime_data/treadmill.py | 24 +++++++++-------- pyftms/serializer/model.py | 13 +++++----- 6 files changed, 61 insertions(+), 56 deletions(-) diff --git a/pyftms/models/realtime_data/common.py b/pyftms/models/realtime_data/common.py index 7230292078..a261dcda68 100644 --- a/pyftms/models/realtime_data/common.py +++ b/pyftms/models/realtime_data/common.py @@ -5,6 +5,7 @@ import io from typing import Any, cast, override +from ...client.properties import MachineFeatures from ...serializer import BaseModel, get_serializer, model_meta @@ -52,7 +53,7 @@ class RealtimeSpeedData(RealtimeData): default=None, metadata=model_meta( format="u2.01", - features_bit=0, + feature=MachineFeatures.AVERAGE_SPEED, ), ) """Average Speed""" diff --git a/pyftms/models/realtime_data/cross_trainer.py b/pyftms/models/realtime_data/cross_trainer.py index b82eefe5d9..f5dd12291d 100644 --- a/pyftms/models/realtime_data/cross_trainer.py +++ b/pyftms/models/realtime_data/cross_trainer.py @@ -8,6 +8,7 @@ BaseModel, EnergyData, InclinationData, + MachineFeatures, RealtimeSpeedData, model_meta, ) @@ -55,7 +56,7 @@ class CrossTrainerData(RealtimeSpeedData): default=None, metadata=model_meta( format="u3", - features_bit=2, + feature=MachineFeatures.DISTANCE, ), ) """Total Distance""" @@ -63,7 +64,7 @@ class CrossTrainerData(RealtimeSpeedData): step_rate: StepRateData | None = dc.field( default=None, metadata=model_meta( - features_bit=6, + feature=MachineFeatures.STEP_COUNT, ), ) """Step Rate Data""" @@ -72,7 +73,7 @@ class CrossTrainerData(RealtimeSpeedData): default=None, metadata=model_meta( format="u2", - features_bit=8, + feature=MachineFeatures.STRIDE_COUNT, ), ) """Stride Count""" @@ -80,7 +81,7 @@ class CrossTrainerData(RealtimeSpeedData): elevation_gain: ElevationGainData | None = dc.field( default=None, metadata=model_meta( - features_bit=4, + feature=MachineFeatures.ELEVATION_GAIN, ), ) """Elevation Gain Data""" @@ -88,7 +89,7 @@ class CrossTrainerData(RealtimeSpeedData): inclination: InclinationData | None = dc.field( default=None, metadata=model_meta( - features_bit=3, + feature=MachineFeatures.INCLINATION, ), ) """Inclination and Ramp Angle Data""" @@ -97,7 +98,7 @@ class CrossTrainerData(RealtimeSpeedData): default=None, metadata=model_meta( format="s2.1", - features_bit=7, + feature=MachineFeatures.RESISTANCE, ), ) """Resistance Level""" @@ -106,7 +107,7 @@ class CrossTrainerData(RealtimeSpeedData): default=None, metadata=model_meta( format="s2", - features_bit=14, + feature=MachineFeatures.POWER_MEASUREMENT, ), ) """Instantaneous Power""" @@ -115,7 +116,7 @@ class CrossTrainerData(RealtimeSpeedData): default=None, metadata=model_meta( format="s2", - features_bit=14, + feature=MachineFeatures.POWER_MEASUREMENT, ), ) """Average Power""" @@ -123,7 +124,7 @@ class CrossTrainerData(RealtimeSpeedData): energy: EnergyData | None = dc.field( default=None, metadata=model_meta( - features_bit=9, + feature=MachineFeatures.EXPENDED_ENERGY, ), ) """Energy Data""" @@ -132,7 +133,7 @@ class CrossTrainerData(RealtimeSpeedData): default=None, metadata=model_meta( format="u1", - features_bit=10, + feature=MachineFeatures.HEART_RATE, ), ) """Heart Rate""" @@ -141,7 +142,7 @@ class CrossTrainerData(RealtimeSpeedData): default=None, metadata=model_meta( format="u1.1", - features_bit=11, + feature=MachineFeatures.METABOLIC_EQUIVALENT, ), ) """Metabolic Equivalent""" @@ -150,7 +151,7 @@ class CrossTrainerData(RealtimeSpeedData): default=None, metadata=model_meta( format="u2", - features_bit=12, + feature=MachineFeatures.ELAPSED_TIME, ), ) """Elapsed Time""" @@ -159,7 +160,7 @@ class CrossTrainerData(RealtimeSpeedData): default=None, metadata=model_meta( format="u2", - features_bit=13, + feature=MachineFeatures.REMAINING_TIME, ), ) """Remaining Time""" diff --git a/pyftms/models/realtime_data/indoor_bike.py b/pyftms/models/realtime_data/indoor_bike.py index 579d0deba8..11a0f0ff6f 100644 --- a/pyftms/models/realtime_data/indoor_bike.py +++ b/pyftms/models/realtime_data/indoor_bike.py @@ -3,7 +3,7 @@ import dataclasses as dc -from .common import EnergyData, RealtimeSpeedData, model_meta +from .common import EnergyData, MachineFeatures, RealtimeSpeedData, model_meta @dc.dataclass(frozen=True) @@ -12,7 +12,7 @@ class IndoorBikeData(RealtimeSpeedData): default=None, metadata=model_meta( format="u2.5", - features_bit=1, + feature=MachineFeatures.CADENCE, ), ) """Instantaneous Cadence""" @@ -21,7 +21,7 @@ class IndoorBikeData(RealtimeSpeedData): default=None, metadata=model_meta( format="u2.5", - features_bit=1, + feature=MachineFeatures.CADENCE, ), ) """Average Cadence""" @@ -30,7 +30,7 @@ class IndoorBikeData(RealtimeSpeedData): default=None, metadata=model_meta( format="u3", - features_bit=2, + feature=MachineFeatures.DISTANCE, ), ) """Total Distance""" @@ -39,7 +39,7 @@ class IndoorBikeData(RealtimeSpeedData): default=None, metadata=model_meta( format="s2", - features_bit=7, + feature=MachineFeatures.RESISTANCE, ), ) """Resistance Level""" @@ -48,7 +48,7 @@ class IndoorBikeData(RealtimeSpeedData): default=None, metadata=model_meta( format="s2", - features_bit=14, + feature=MachineFeatures.POWER_MEASUREMENT, ), ) """Instantaneous Power""" @@ -57,7 +57,7 @@ class IndoorBikeData(RealtimeSpeedData): default=None, metadata=model_meta( format="s2", - features_bit=14, + feature=MachineFeatures.POWER_MEASUREMENT, ), ) """Average Power""" @@ -65,7 +65,7 @@ class IndoorBikeData(RealtimeSpeedData): energy: EnergyData | None = dc.field( default=None, metadata=model_meta( - features_bit=9, + feature=MachineFeatures.EXPENDED_ENERGY, ), ) """Energy Data""" @@ -74,7 +74,7 @@ class IndoorBikeData(RealtimeSpeedData): default=None, metadata=model_meta( format="u1", - features_bit=10, + feature=MachineFeatures.HEART_RATE, ), ) """Heart Rate""" @@ -83,7 +83,7 @@ class IndoorBikeData(RealtimeSpeedData): default=None, metadata=model_meta( format="u1.1", - features_bit=11, + feature=MachineFeatures.METABOLIC_EQUIVALENT, ), ) """Metabolic Equivalent""" @@ -92,7 +92,7 @@ class IndoorBikeData(RealtimeSpeedData): default=None, metadata=model_meta( format="u2", - features_bit=12, + feature=MachineFeatures.ELAPSED_TIME, ), ) """Elapsed Time""" @@ -101,7 +101,7 @@ class IndoorBikeData(RealtimeSpeedData): default=None, metadata=model_meta( format="u2", - features_bit=13, + feature=MachineFeatures.REMAINING_TIME, ), ) """Remaining Time""" diff --git a/pyftms/models/realtime_data/rower.py b/pyftms/models/realtime_data/rower.py index e142968675..9f96223f2f 100644 --- a/pyftms/models/realtime_data/rower.py +++ b/pyftms/models/realtime_data/rower.py @@ -3,7 +3,7 @@ import dataclasses as dc -from .common import BaseModel, EnergyData, RealtimeData, model_meta +from .common import BaseModel, EnergyData, MachineFeatures, RealtimeData, model_meta @dc.dataclass(frozen=True) @@ -35,7 +35,7 @@ class RowerData(RealtimeData): default=None, metadata=model_meta( format="u1.5", - features_bit=1, + feature=MachineFeatures.CADENCE, ), ) """Average Stroke Rate""" @@ -44,7 +44,7 @@ class RowerData(RealtimeData): default=None, metadata=model_meta( format="u3", - features_bit=2, + feature=MachineFeatures.DISTANCE, ), ) """Total Distance""" @@ -53,7 +53,7 @@ class RowerData(RealtimeData): default=None, metadata=model_meta( format="u2", - features_bit=5, + feature=MachineFeatures.PACE, ), ) """Instantaneous Split Time""" @@ -62,7 +62,7 @@ class RowerData(RealtimeData): default=None, metadata=model_meta( format="u2", - features_bit=5, + feature=MachineFeatures.PACE, ), ) """Average Split Time""" @@ -71,7 +71,7 @@ class RowerData(RealtimeData): default=None, metadata=model_meta( format="s2", - features_bit=14, + feature=MachineFeatures.POWER_MEASUREMENT, ), ) """Instantaneous Power""" @@ -80,7 +80,7 @@ class RowerData(RealtimeData): default=None, metadata=model_meta( format="s2", - features_bit=14, + feature=MachineFeatures.POWER_MEASUREMENT, ), ) """Average Power""" @@ -89,7 +89,7 @@ class RowerData(RealtimeData): default=None, metadata=model_meta( format="s2", - features_bit=7, + feature=MachineFeatures.RESISTANCE, ), ) """Resistance Level""" @@ -97,7 +97,7 @@ class RowerData(RealtimeData): energy: EnergyData | None = dc.field( default=None, metadata=model_meta( - features_bit=9, + feature=MachineFeatures.EXPENDED_ENERGY, ), ) """Energy Data""" @@ -106,7 +106,7 @@ class RowerData(RealtimeData): default=None, metadata=model_meta( format="u1", - features_bit=10, + feature=MachineFeatures.HEART_RATE, ), ) """Heart Rate""" @@ -115,7 +115,7 @@ class RowerData(RealtimeData): default=None, metadata=model_meta( format="u1.1", - features_bit=11, + feature=MachineFeatures.METABOLIC_EQUIVALENT, ), ) """Metabolic Equivalent""" @@ -124,7 +124,7 @@ class RowerData(RealtimeData): default=None, metadata=model_meta( format="u2", - features_bit=12, + feature=MachineFeatures.ELAPSED_TIME, ), ) """Elapsed Time""" @@ -133,7 +133,7 @@ class RowerData(RealtimeData): default=None, metadata=model_meta( format="u2", - features_bit=13, + feature=MachineFeatures.REMAINING_TIME, ), ) """Remaining Time""" diff --git a/pyftms/models/realtime_data/treadmill.py b/pyftms/models/realtime_data/treadmill.py index 6b2dfa426a..b30016a81f 100644 --- a/pyftms/models/realtime_data/treadmill.py +++ b/pyftms/models/realtime_data/treadmill.py @@ -7,6 +7,7 @@ BaseModel, EnergyData, InclinationData, + MachineFeatures, RealtimeSpeedData, model_meta, ) @@ -54,7 +55,7 @@ class TreadmillData(RealtimeSpeedData): default=None, metadata=model_meta( format="u3", - features_bit=2, + feature=MachineFeatures.DISTANCE, ), ) """Total Distance""" @@ -62,7 +63,7 @@ class TreadmillData(RealtimeSpeedData): inclination: InclinationData | None = dc.field( default=None, metadata=model_meta( - features_bit=3, + feature=MachineFeatures.INCLINATION, ), ) """Inclination and Ramp Angle Data""" @@ -70,7 +71,7 @@ class TreadmillData(RealtimeSpeedData): elevation_gain: ElevationGainData | None = dc.field( default=None, metadata=model_meta( - features_bit=4, + feature=MachineFeatures.ELEVATION_GAIN, ), ) """Elevation Gain Data""" @@ -79,7 +80,7 @@ class TreadmillData(RealtimeSpeedData): default=None, metadata=model_meta( format="u1.1", - features_bit=5, + feature=MachineFeatures.PACE, ), ) """Instantaneous Speed""" @@ -88,7 +89,7 @@ class TreadmillData(RealtimeSpeedData): default=None, metadata=model_meta( format="u1.1", - features_bit=5, + feature=MachineFeatures.PACE, ), ) """Average Speed""" @@ -96,7 +97,7 @@ class TreadmillData(RealtimeSpeedData): energy: EnergyData | None = dc.field( default=None, metadata=model_meta( - features_bit=9, + feature=MachineFeatures.EXPENDED_ENERGY, ), ) """Energy Data""" @@ -105,7 +106,7 @@ class TreadmillData(RealtimeSpeedData): default=None, metadata=model_meta( format="u1", - features_bit=10, + feature=MachineFeatures.HEART_RATE, ), ) """Heart Rate""" @@ -114,7 +115,7 @@ class TreadmillData(RealtimeSpeedData): default=None, metadata=model_meta( format="u1.1", - features_bit=11, + feature=MachineFeatures.METABOLIC_EQUIVALENT, ), ) """Metabolic Equivalent""" @@ -123,7 +124,7 @@ class TreadmillData(RealtimeSpeedData): default=None, metadata=model_meta( format="u2", - features_bit=12, + feature=MachineFeatures.ELAPSED_TIME, ), ) """Elapsed Time""" @@ -132,7 +133,7 @@ class TreadmillData(RealtimeSpeedData): default=None, metadata=model_meta( format="u2", - features_bit=13, + feature=MachineFeatures.REMAINING_TIME, ), ) """Remaining Time""" @@ -140,7 +141,7 @@ class TreadmillData(RealtimeSpeedData): force_on_belt: ForceOnBeltData | None = dc.field( default=None, metadata=model_meta( - features_bit=15, + feature=MachineFeatures.FORCE_ON_BELT_AND_POWER_OUTPUT, ), ) """Force On Belt and Power Output""" @@ -149,6 +150,7 @@ class TreadmillData(RealtimeSpeedData): default=None, metadata=model_meta( format="u3", + feature=MachineFeatures.STEP_COUNT, ), ) """Steps Count""" diff --git a/pyftms/serializer/model.py b/pyftms/serializer/model.py index 8985328ff6..425bf22e1a 100644 --- a/pyftms/serializer/model.py +++ b/pyftms/serializer/model.py @@ -21,6 +21,7 @@ override, ) +from ..client.properties import MachineFeatures from .list import ListSerializer from .num import NumSerializer from .serializer import Serializer @@ -28,7 +29,7 @@ class ModelMeta(TypedDict, total=False): format: str - features_bit: int + feature: MachineFeatures code: int num: int @@ -36,14 +37,14 @@ class ModelMeta(TypedDict, total=False): def model_meta( *, format: str = "", - features_bit: int | None = None, + feature: MachineFeatures | None = None, code: int | None = None, num: int | None = None, ) -> ModelMeta: result = ModelMeta(format=format) - if features_bit is not None: - result["features_bit"] = features_bit + if feature: + result["feature"] = feature if code is not None: result["code"] = code @@ -171,7 +172,7 @@ def _transform(input: dict): return result @classmethod - def _get_features(cls, features: int) -> tuple[str, ...]: + def _get_features(cls, features: MachineFeatures) -> tuple[str, ...]: result = [] def _get_cls_features(cls): @@ -185,7 +186,7 @@ def _get_cls_features(cls): if (tp := get_args(tp)[0]) is None: raise TypeError("Failed to get first type.") - if (bit := meta.get("features_bit")) is None or features & (1 << bit): + if (bit := meta.get("feature")) is None or bit in features: if isinstance(tp, type) and issubclass(tp, BaseModel): _get_cls_features(tp) continue