forked from muccc/iridium-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiridium-acars-to-airframes.py
executable file
·134 lines (121 loc) · 4.73 KB
/
iridium-acars-to-airframes.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
#
# iridium-acars-to-airframes.py
#
# Send the ACARS JSON output from iridium-toolkit to Airframes.io. You may send to additional destinations
# by specifying the --output option multiple times.
#
# INSTRUCTIONS:
#
# 1. Ensure that your iridium-toolkit setup is properly decoding acars first.
# 2. Pipe the output of the "-m acars -a json" to this script. Make sure you set your station ident!
# 3. Optionally, specify additional outputs with the --output option.
#
# EXAMPLE:
#
# $ export PYTHONUNBUFFERED=x
# $ reassembler.py -i zmq: -m acars -a json --station YOUR_STATION_IDENT | iridium-acars-to-airframes.py
#
# USAGE:
#
# iridium-acars-to-airframes.py [-h] [--station STATION] [--debug] [--output OUTPUT]
#
# Feed Iridium ACARS to Airframes.io
#
# options:
# -h, --help show this help message and exit
# --station STATION, -s STATION
# Override station ident
# --debug, -d Enable debug output
# --output OUTPUT, -o OUTPUT
# Send output to additional destination transport:host:port (where transport is "tcp" or "udp")
#
# For more information about Airframes, see https://app.airframes.io/about or contact [email protected].
#
import argparse
import json
import sys
import select
import time
import socket
airframes_ingest_host = 'feed.airframes.io'
airframes_ingest_port = 5590
debug = True
sock = None
sockets = {}
def reconnect():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((airframes_ingest_host, airframes_ingest_port))
print("Reconnected to Airframes Iridium ACARS ingest (%s:%d)" %
(airframes_ingest_host, airframes_ingest_port))
return s
def send_message(message):
global sockets
for k, sock in sockets.items():
try:
sock.sendall(("%s\n" % message).encode())
if debug:
print("Sent message to %s" % (k))
except Exception as e:
print('Error sending message: %s' % e)
# check if socket is tcp
if sock.getsockopt(socket.SOL_SOCKET, socket.SO_TYPE) == socket.SOCK_STREAM:
print("Reconnecting to %s:%d" % (sock.getpeername()))
sock.close()
sock = reconnect()
sockets[k] = sock
sock.sendall(message.encode('utf-8'))
if __name__ == '__main__':
args_parser = argparse.ArgumentParser(
prog='iridium-acars-to-airframes.py',
description='Feed Iridium ACARS to Airframes.io and additional remote destinations',
)
args_parser.add_argument(
'--station', '-s', help='Override station ident', required=False)
args_parser.add_argument(
'--debug', '-d', help='Enable debug output', action='store_true')
args_parser.add_argument(
'--output', '-o', help='Send output via TCP to additional destination transport:host:port (where transport is "tcp" or "udp")', default=[], action='append')
args = args_parser.parse_args()
debug = args.debug
if args.station and debug:
print("Overriding station ident to %s" % (args.station,))
args.output = ['tcp:%s:%d' %
(airframes_ingest_host, airframes_ingest_port)] + args.output
for output in args.output:
transport, host, port = output.split(':')
try:
if transport == 'tcp':
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, int(port)))
sockets[output] = sock
if debug:
print("Connected to %s:%s:%s" % (transport, host, port))
elif transport == 'udp':
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect((host, int(port)))
sockets[output] = sock
if debug:
print("Connected to %s:%s:%s" % (transport, host, port))
else:
print("Unknown transport %s" % (transport,))
except TimeoutError as e:
print("Error connecting to output (%s:%s): %s" % (host, port, e))
sys.exit(1)
except Exception as e:
print("Error connecting to output (%s:%s): %s" % (host, port, e))
print("Not adding output to (%s:%s)" % (host, port))
while True:
line = sys.stdin.readline().strip()
if debug:
print("Received: %s" % (line,))
try:
message = json.loads(line)
except Exception as e:
print("Error parsing JSON (%s): %s" % (e, line,))
continue
if args.station:
if message['source'] is None:
message['source'] = {}
message['source']['station'] = args.station
send_message(json.dumps(message))