Logo
Telescience Resource Kit
TReK Python  5.4.0 ART
ex_command.py
1"""
2Example TReK Command Demonstration
3"""
4
5import trek
6
7
8def main() -> None:
9 """
10 Main Routine
11 """
12
13 try:
14 api = trek.CommandApi()
15
16 # Connect to a destination. The name passed into the Connect() call is
17 # the name used when configuring the destination in the TReK Command
18 # application.
19 api.connect("POIC")
20
21 # Calling this method allows the API to clean up resources that may be
22 # left over from a user program that crashed. Not checking the return
23 # value as it is safe to continue if this method fails.
24 api.cleanse()
25
26 # Once connected to the API, you can make calls to the API to see what is
27 # available for the interface. Call to see if there are any status
28 # messages. If there are, print out all of the information in each
29 # message.
30
31 status_list = api.get_status_message_list()
32 print(f"There are {len(status_list)} status messages for this destination.\n")
33
34 for msg in status_list:
35 print(f"Status Message: {msg}\n")
37
38 _ = api.get_status_message(msg, pc)
39
40 # get all of the parameters that make up the status message
41 param_list = pc.get_sorted_parameter_list(False)
42
43 for param_name in param_list:
44 # print each name/value in status mesesage
45 param = pc.find_parameter(param_name)
46 print(f"{param_name}: {param.get_value_as_string()}\n")
47
48 # Check to see if there are command responses associated with this
49 # destination. If there is, get a copy of the responses so we can
50 # check any errors that are received when sending commands. Store
51 # the responses in a map for quick lookup.
52 resp_list = api.get_command_response_list()
53
54 response_dictionary = {}
55 for cr_name in resp_list:
56 response = trek.CommandResponse()
57 try:
58 api.get_command_response(cr_name, response)
59 except trek.TrekError as err:
60 print(f"Error {err.code}: Could not retrieve response {cr_name}.\n")
61 # Not fatal! Should never occur unless API has coding error.
62 continue
63 response_dictionary[cr_name] = response
64
65 # Send a command by initiating an uplink from the destination. This method
66 # of sending commands is not avaiable for all destinations, but will fail
67 # gracefully. The token will be used to get command track information.
68 token = api.initiate_command("ACCELERATE")
69
70 track_item = trek.TrackItem()
71 api.get_track(token, track_item)
72
73 for idx in range(track_item.get_number_of_responses()):
74 resp_item = track_item.get_response(idx)
75
76 if resp_item.name in response_dictionary:
77 my_command_res: trek.CommandResponse = response_dictionary[resp_item.name]
78 resp_info = my_command_res.get_error_details(resp_item.value)
79 print(f"Response {resp_item.name}: {resp_info.info}.\n")
80
81 #
82 # It is always best to call Disconnect when you are finished with the
83 # API. There are system resources that can be freed when you no
84 # longer need the API.
85 #
86 api.disconnect()
87
88 except trek.TrekError as err:
89 # Locate friendly message why it failed.
90 print(err.find_error_code())
91 print(err)
92
93
94if __name__ == "__main__":
95 main()
Provides access to command features of TReK.
Definition: trek.py:850
This class holds all the responses associated with a single command uplink.
Definition: trek.py:1872
This class describes a parameter collection.
Definition: trek.py:11554
This class holds all of the responses associated with a single command uplink.
Definition: trek.py:15089
The TReK C++ was designed to return error codes.
Definition: trek.py:15453