prod2u.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """Prod2U profile helpers. Not the public device-agnostic contract."""
  2. from __future__ import annotations
  3. from datetime import timedelta
  4. from typing import Optional
  5. from hth_sdk.batch import ActionBatchBuilder
  6. from hth_sdk.exceptions import ActionRejected
  7. from hth_sdk.types import Capabilities
  8. PROFILE_ID = "prod2u-normal-multiproject-115200-v1"
  9. PROJECT_ID = "prod2u-door-temp"
  10. FAN_IDS = tuple(f"fan.chamber.{i}" for i in range(1, 5))
  11. PTC_IDS = tuple(f"ptc.chamber.{i}" for i in range(1, 5))
  12. DOOR_IDS = ("door.x.close_percent", "door.y.close_percent", "door.z.close_percent")
  13. KNOWN_ACTUATOR_IDS = FAN_IDS + PTC_IDS + DOOR_IDS
  14. TEMPERATURE_IDS = tuple(f"temperature.channel.{i:02d}" for i in range(27))
  15. LIMIT_IDS = (
  16. "door.x.open_limit",
  17. "door.x.close_limit",
  18. "door.y.open_limit",
  19. "door.y.close_limit",
  20. "door.z.open_limit",
  21. "door.z.close_limit",
  22. )
  23. ESTIMATED_POSITION_IDS = (
  24. "door.x.estimated_close_percent",
  25. "door.y.estimated_close_percent",
  26. "door.z.estimated_close_percent",
  27. )
  28. KNOWN_SENSOR_IDS = TEMPERATURE_IDS + LIMIT_IDS + ESTIMATED_POSITION_IDS
  29. DOOR_FULL_OPEN = 0
  30. DOOR_FULL_CLOSE = 100
  31. DUTY_MIN = 0
  32. DUTY_MAX = 100
  33. # Daemon safety set-points for this profile's offline fixtures only.
  34. # Live clients must call EnterSafeMode() instead of copying these.
  35. SAFETY_FAN_PERCENT = 10
  36. SAFETY_PTC_PERCENT = 0
  37. SAFETY_DOOR_CLOSE_PERCENT = 0
  38. class Prod2UBuilder(ActionBatchBuilder):
  39. """Profile-specific helpers. Not the public device-agnostic contract."""
  40. def set_fan(self, chamber: int, percent: float) -> ActionBatchBuilder:
  41. return self.set(f"fan.chamber.{int(chamber)}", percent)
  42. def set_ptc(self, chamber: int, percent: float) -> ActionBatchBuilder:
  43. return self.set(f"ptc.chamber.{int(chamber)}", percent)
  44. def set_door_close_percent(self, axis: str, percent: float) -> ActionBatchBuilder:
  45. axis = axis.lower()
  46. if axis not in ("x", "y", "z"):
  47. raise ActionRejected("UNKNOWN_ID", f"unknown door axis {axis!r}")
  48. return self.set(f"door.{axis}.close_percent", percent)
  49. def _check_id(self, actuator_id: str) -> None:
  50. if not actuator_id:
  51. raise ActionRejected("INVALID_ARGUMENT", "actuator id is empty")
  52. if self._caps is not None:
  53. super()._check_id(actuator_id)
  54. return
  55. if actuator_id not in KNOWN_ACTUATOR_IDS:
  56. raise ActionRejected("UNKNOWN_ID", f"unknown actuator id {actuator_id!r}")
  57. def _check_range(self, actuator_id: str, value: float) -> None:
  58. if self._caps is not None:
  59. super()._check_range(actuator_id, value)
  60. return
  61. if value != value:
  62. raise ActionRejected("OUT_OF_RANGE", f"{actuator_id} is NaN")
  63. if value < DUTY_MIN or value > DUTY_MAX:
  64. raise ActionRejected(
  65. "OUT_OF_RANGE",
  66. f"{actuator_id}={value} outside {DUTY_MIN}..{DUTY_MAX}",
  67. )
  68. def builder(
  69. device_id: str,
  70. *,
  71. capabilities: Optional[Capabilities] = None,
  72. allow_unverified: bool = False,
  73. client_id: str = "hth-sdk",
  74. ) -> Prod2UBuilder:
  75. b = Prod2UBuilder(
  76. device_id,
  77. client_id=client_id,
  78. capabilities=capabilities,
  79. allow_unverified=allow_unverified or capabilities is None,
  80. )
  81. if capabilities is None:
  82. # Offline tests only. Production callers must pass GetCapabilities.
  83. b.valid_for(timedelta(seconds=15))
  84. b.strict_known_ids(True)
  85. return b
  86. def set_fan(b: ActionBatchBuilder, chamber: int, percent: float) -> ActionBatchBuilder:
  87. return b.set(f"fan.chamber.{int(chamber)}", percent)
  88. def set_ptc(b: ActionBatchBuilder, chamber: int, percent: float) -> ActionBatchBuilder:
  89. return b.set(f"ptc.chamber.{int(chamber)}", percent)
  90. def set_door_close_percent(b: ActionBatchBuilder, axis: str, percent: float) -> ActionBatchBuilder:
  91. axis = axis.lower()
  92. if axis not in ("x", "y", "z"):
  93. raise ActionRejected("UNKNOWN_ID", f"unknown door axis {axis!r}")
  94. return b.set(f"door.{axis}.close_percent", percent)