-
Notifications
You must be signed in to change notification settings - Fork 22
/
simulator.py
executable file
·111 lines (88 loc) · 3.4 KB
/
simulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
import binascii
import io
import socket
import sys
import argparse
from struct import unpack_from
from threading import Thread
from time import sleep
parser = argparse.ArgumentParser(description='Yamcs Simulator')
parser.add_argument('--testdata', type=str, default='testdata.ccsds', help='simulated testdata.ccsds data')
# telemetry
parser.add_argument('--tm_host', type=str, default='127.0.0.1', help='TM host')
parser.add_argument('--tm_port', type=int, default=10015, help='TM port')
parser.add_argument('-r', '--rate', type=int, default=1, help='TM playback rate. 1 = 1Hz, 10 = 10Hz, etc.')
# telecommand
parser.add_argument('--tc_host', type=str, default='127.0.0.1', help='TC host')
parser.add_argument('--tc_port', type=int, default=10025 , help='TC port')
args = vars(parser.parse_args())
# test data
TEST_DATA = args['testdata']
# telemetry
TM_SEND_ADDRESS = args['tm_host']
TM_SEND_PORT = args['tm_port']
RATE = args['rate']
# telecommand
TC_RECEIVE_ADDRESS = args['tc_host']
TC_RECEIVE_PORT = args['tc_port']
def send_tm(simulator):
tm_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
with io.open(TEST_DATA, 'rb') as f:
simulator.tm_counter = 1
header = bytearray(6)
while f.readinto(header) == 6:
(len,) = unpack_from('>H', header, 4)
packet = bytearray(len + 7)
f.seek(-6, io.SEEK_CUR)
f.readinto(packet)
tm_socket.sendto(packet, (TM_SEND_ADDRESS, TM_SEND_PORT))
simulator.tm_counter += 1
sleep(1 / simulator.rate)
def receive_tc(simulator):
tc_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
tc_socket.bind((TC_RECEIVE_ADDRESS, TC_RECEIVE_PORT ))
while True:
data, _ = tc_socket.recvfrom(4096)
simulator.last_tc = data
simulator.tc_counter += 1
class Simulator():
def __init__(self, rate):
self.tm_counter = 0
self.tc_counter = 0
self.tm_thread = None
self.tc_thread = None
self.last_tc = None
self.rate = rate
def start(self):
self.tm_thread = Thread(target=send_tm, args=(self,))
self.tm_thread.daemon = True
self.tm_thread.start()
self.tc_thread = Thread(target=receive_tc, args=(self,))
self.tc_thread.daemon = True
self.tc_thread.start()
def print_status(self):
cmdhex = None
if self.last_tc:
cmdhex = binascii.hexlify(self.last_tc).decode('ascii')
return 'Sent: {} packets. Received: {} commands. Last command: {}'.format(
self.tm_counter, self.tc_counter, cmdhex)
if __name__ == '__main__':
simulator = Simulator(RATE)
simulator.start()
sys.stdout.write('Using playback rate of ' + str(RATE) + 'Hz, ');
sys.stdout.write('TM host=' + str(TM_SEND_ADDRESS) + ', TM port=' + str(TM_SEND_PORT) + ', ');
sys.stdout.write('TC host=' + str(TC_RECEIVE_ADDRESS) + ', TC port=' + str(TC_RECEIVE_PORT) + '\r\n');
try:
prev_status = None
while True:
status = simulator.print_status()
if status != prev_status:
sys.stdout.write('\r')
sys.stdout.write(status)
sys.stdout.flush()
prev_status = status
sleep(0.5)
except KeyboardInterrupt:
sys.stdout.write('\n')
sys.stdout.flush()