import enum import logging from datetime import datetime from eive_tmtc.config.definitions import CustomServiceList from spacepackets.ecss import PusTelecommand, PusService from tmtccmd.config.tmtc import ( tmtc_definitions_provider, TmtcDefinitionWrapper, OpCodeEntry, ) from tmtccmd.tc import service_provider from tmtccmd.tc.decorator import ServiceProviderParams class Subservice(enum.IntEnum): SET_TIME = 128 DUMP_TIME = 129 class OpCode: SET_CURRENT_TIME = ["set_curr_time"] DUMP_TIME = ["dump_time"] class Info: SET_CURRENT_TIME = "Setting current time in ASCII format" DUMP_TIME = "Dump system time as event" @service_provider(CustomServiceList.TIME.value) def pack_set_current_time_ascii_command(p: ServiceProviderParams): q = p.queue_helper o = p.op_code if o in OpCode.SET_CURRENT_TIME: current_time = datetime.utcnow().isoformat() + "Z" + "\0" current_time_ascii = current_time.encode("ascii") logging.getLogger(__name__).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 o in OpCode.DUMP_TIME: q.add_log_cmd(Info.DUMP_TIME) q.add_pus_tc( PusTelecommand( service=PusService.S9_TIME_MGMT, subservice=Subservice.DUMP_TIME ) ) @tmtc_definitions_provider def add_time_cmds(defs: TmtcDefinitionWrapper): oce = OpCodeEntry() oce.add( keys=OpCode.SET_CURRENT_TIME, info=Info.SET_CURRENT_TIME, ) oce.add(keys=OpCode.DUMP_TIME, info=Info.DUMP_TIME) defs.add_service( name=CustomServiceList.TIME.value, info="Time Service", op_code_entry=oce, )