Skip to content

Commit

Permalink
Report timeout of the engineering mode
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Dec 26, 2024
1 parent 16b22ff commit ba1bfa9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
### ✨ Improved

* Report last time the heartbeat was set in the PLC in the `status` command.

* Report timeout of the engineering mode.

## 1.0.1 - December 26, 2024

Expand Down
5 changes: 3 additions & 2 deletions python/lvmecp/actor/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,13 @@ async def _run_eng_mode(self, timeout: float | None = None):
"""

started_at: float = time.time()
timeout = timeout or self._engineering_mode_timeout
if timeout is not None:
self._engineering_mode_timeout = timeout

while True:
await self.emit_heartbeat()

if time.time() - started_at > timeout:
if time.time() - started_at > self._engineering_mode_timeout:
self.write("w", text="Engineering mode timed out and was disabled.")
await self.engineering_mode(False)
return
Expand Down
10 changes: 9 additions & 1 deletion python/lvmecp/actor/commands/engineering.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import click

from lvmecp.tools import timestamp_to_iso

from . import parser


Expand Down Expand Up @@ -55,4 +57,10 @@ async def disable(command: ECPCommand):
async def status(command: ECPCommand):
"""Returns the status of the engineering mode."""

return command.finish(engineering_mode=command.actor.is_engineering_mode_enabled())
enabled = command.actor.is_engineering_mode_enabled()
timeout = command.actor._engineering_mode_timeout

return command.finish(
engineering_mode_enabled=enabled,
engineering_mode_timeout=timestamp_to_iso(timeout),
)
5 changes: 4 additions & 1 deletion python/lvmecp/etc/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
"last_heartbeat_set": {
"oneOf": [{ "type": "number" }, { "type": "null" }]
},
"engineering_mode": { "type": "boolean" }
"engineering_mode_enabled": { "type": "boolean" },
"engineering_mode_timeout": {
"oneOf": [{ "type": "string" }, { "type": "null" }]
}
},
"additionalProperties": true
}
16 changes: 15 additions & 1 deletion python/lvmecp/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@

import asyncio
from contextlib import suppress
from datetime import datetime, timezone

from typing import Any, Callable, Coroutine


__all__ = ["loop_coro", "cancel_tasks_by_name"]
__all__ = ["loop_coro", "cancel_tasks_by_name", "timestamp_to_iso"]


def loop_coro(
Expand Down Expand Up @@ -46,3 +47,16 @@ async def cancel_tasks_by_name(name: str):
task.cancel()
with suppress(asyncio.CancelledError):
await task


def timestamp_to_iso(ts: float | None, timespec: str = "seconds") -> str | None:
"""Converts a timestamp to an ISO string."""

if ts is None:
return None

return (
datetime.fromtimestamp(ts, timezone.utc)
.isoformat(timespec=timespec)
.replace("+00:00", "Z")
)

0 comments on commit ba1bfa9

Please sign in to comment.