eive-tmtc/eive_tmtc/tmtc/time.py

74 lines
2.3 KiB
Python

import enum
import struct
import logging
import datetime
from spacepackets.ecss import PusTelecommand, PusService
from tmtccmd.tmtc import DefaultPusQueueHelper
from tmtccmd.config import CmdTreeNode
_LOGGER = logging.getLogger(__name__)
class Subservice(enum.IntEnum):
SET_TIME = 128
DUMP_TIME = 129
RELATIVE_TIMESHIFT = 130
class CmdStr:
SET_CURRENT_TIME = "set_curr_time"
RELATIVE_TIMESHIFT = "relative_timeshift"
DUMP_TIME = "dump_time"
class Info:
SET_CURRENT_TIME = "Setting current time in ASCII format"
RELATIVE_TIMESHIFT = "Shift time with a relative offset"
DUMP_TIME = "Dump system time as event"
def pack_time_management_cmd(q: DefaultPusQueueHelper, cmd_str: str):
if cmd_str == CmdStr.SET_CURRENT_TIME:
current_time = (
datetime.datetime.now(datetime.timezone.utc).isoformat() + "Z" + "\0"
)
current_time_ascii = current_time.encode("ascii")
_LOGGER.info(f"Current time in ASCII format: {current_time_ascii}")
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,
)
)
elif cmd_str == CmdStr.RELATIVE_TIMESHIFT:
nanos = int(input("Specify relative timeshift in nanoseconds: "))
nanos_packed = struct.pack("!q", nanos)
q.add_log_cmd(Info.RELATIVE_TIMESHIFT)
q.add_pus_tc(
PusTelecommand(
service=PusService.S9_TIME_MGMT,
subservice=Subservice.RELATIVE_TIMESHIFT,
app_data=nanos_packed,
)
)
elif cmd_str == CmdStr.DUMP_TIME:
q.add_log_cmd(Info.DUMP_TIME)
q.add_pus_tc(
PusTelecommand(
service=PusService.S9_TIME_MGMT, subservice=Subservice.DUMP_TIME
)
)
def create_time_node() -> CmdTreeNode:
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"))
time_node.add_child(CmdTreeNode(CmdStr.RELATIVE_TIMESHIFT, Info.RELATIVE_TIMESHIFT))
return time_node