forked from yamcs/quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.py
executable file
·81 lines (64 loc) · 2.14 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
#!/usr/bin/env python3
import binascii
import io
import socket
import sys
from struct import unpack_from
from threading import Thread
from time import sleep
def send_tm(simulator):
tm_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
with io.open('testdata.ccsds', '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, ('127.0.0.1', 10015))
simulator.tm_counter += 1
sleep(1)
def receive_tc(simulator):
tc_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
tc_socket.bind(('127.0.0.1', 10025))
while True:
data, _ = tc_socket.recvfrom(4096)
simulator.last_tc = data
simulator.tc_counter += 1
class Simulator():
def __init__(self):
self.tm_counter = 0
self.tc_counter = 0
self.tm_thread = None
self.tc_thread = None
self.last_tc = None
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()
simulator.start()
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()