2023-02-01 19:13:59 +01:00
|
|
|
import enum
|
2024-04-08 11:35:05 +02:00
|
|
|
import struct
|
2023-02-01 19:13:59 +01:00
|
|
|
import logging
|
2024-04-08 11:35:05 +02:00
|
|
|
import datetime
|
2023-02-01 19:13:59 +01:00
|
|
|
|
|
|
|
from spacepackets.ecss import PusTelecommand, PusService
|
|
|
|
|
2023-11-22 10:17:05 +01:00
|
|
|
from tmtccmd.tmtc import DefaultPusQueueHelper
|
|
|
|
from tmtccmd.config import CmdTreeNode
|
2023-02-01 19:13:59 +01:00
|
|
|
|
|
|
|
|
2024-04-08 11:35:05 +02:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2024-04-08 13:24:40 +02:00
|
|
|
|
2023-02-01 19:13:59 +01:00
|
|
|
class Subservice(enum.IntEnum):
|
|
|
|
SET_TIME = 128
|
|
|
|
DUMP_TIME = 129
|
2024-04-08 11:35:05 +02:00
|
|
|
RELATIVE_TIMESHIFT = 130
|
2023-02-01 19:13:59 +01:00
|
|
|
|
|
|
|
|
2023-11-22 10:17:05 +01:00
|
|
|
class CmdStr:
|
|
|
|
SET_CURRENT_TIME = "set_curr_time"
|
2024-04-08 11:35:05 +02:00
|
|
|
RELATIVE_TIMESHIFT = "relative_timeshift"
|
2023-11-22 10:17:05 +01:00
|
|
|
DUMP_TIME = "dump_time"
|
2023-02-01 19:13:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Info:
|
|
|
|
SET_CURRENT_TIME = "Setting current time in ASCII format"
|
2024-04-08 11:35:05 +02:00
|
|
|
RELATIVE_TIMESHIFT = "Shift time with a relative offset"
|
2023-02-01 19:13:59 +01:00
|
|
|
DUMP_TIME = "Dump system time as event"
|
|
|
|
|
|
|
|
|
2023-11-22 10:17:05 +01:00
|
|
|
def pack_time_management_cmd(q: DefaultPusQueueHelper, cmd_str: str):
|
|
|
|
if cmd_str == CmdStr.SET_CURRENT_TIME:
|
2024-04-08 13:24:40 +02:00
|
|
|
current_time = (
|
|
|
|
datetime.datetime.now(datetime.timezone.utc).isoformat() + "Z" + "\0"
|
2023-02-01 19:13:59 +01:00
|
|
|
)
|
2024-04-08 13:24:40 +02:00
|
|
|
current_time_ascii = current_time.encode("ascii")
|
|
|
|
_LOGGER.info(f"Current time in ASCII format: {current_time_ascii}")
|
2023-02-01 19:13:59 +01:00
|
|
|
q.add_log_cmd(Info.SET_CURRENT_TIME)
|
|
|
|
q.add_pus_tc(
|
|
|
|
PusTelecommand(
|
|
|
|
service=PusService.S9_TIME_MGMT,
|
|
|
|
subservice=Subservice.SET_TIME,
|
|
|
|
app_data=current_time_ascii,
|
|
|
|
)
|
|
|
|
)
|
2024-04-08 11:35:05 +02:00
|
|
|
elif cmd_str == CmdStr.RELATIVE_TIMESHIFT:
|
2024-04-08 13:24:40 +02:00
|
|
|
nanos = int(input("Specify relative timeshift in nanoseconds: "))
|
|
|
|
nanos_packed = struct.pack("!q", nanos)
|
2024-04-08 11:35:05 +02:00
|
|
|
q.add_log_cmd(Info.RELATIVE_TIMESHIFT)
|
|
|
|
q.add_pus_tc(
|
|
|
|
PusTelecommand(
|
|
|
|
service=PusService.S9_TIME_MGMT,
|
|
|
|
subservice=Subservice.RELATIVE_TIMESHIFT,
|
2024-04-08 13:24:40 +02:00
|
|
|
app_data=nanos_packed,
|
2024-04-08 11:35:05 +02:00
|
|
|
)
|
|
|
|
)
|
2023-11-22 10:17:05 +01:00
|
|
|
elif cmd_str == CmdStr.DUMP_TIME:
|
2023-02-01 19:13:59 +01:00
|
|
|
q.add_log_cmd(Info.DUMP_TIME)
|
|
|
|
q.add_pus_tc(
|
|
|
|
PusTelecommand(
|
|
|
|
service=PusService.S9_TIME_MGMT, subservice=Subservice.DUMP_TIME
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-11-22 10:51:26 +01:00
|
|
|
def create_time_node() -> CmdTreeNode:
|
2023-11-22 10:17:05 +01:00
|
|
|
time_node = CmdTreeNode("time", "Time Management")
|
|
|
|
time_node.add_child(CmdTreeNode(CmdStr.SET_CURRENT_TIME, "Set current time"))
|
|
|
|
time_node.add_child(CmdTreeNode(CmdStr.DUMP_TIME, "Dumpy current time"))
|
2024-04-08 13:24:40 +02:00
|
|
|
time_node.add_child(CmdTreeNode(CmdStr.RELATIVE_TIMESHIFT, Info.RELATIVE_TIMESHIFT))
|
2023-11-22 10:17:05 +01:00
|
|
|
return time_node
|