-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlrsn-example.py
303 lines (253 loc) · 10.6 KB
/
lrsn-example.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""
Copyright (c) 2017, Long Range Systems, LLC
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of LRSN nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import argparse
import socket
import select
import sys
import xml.etree.ElementTree as ET
import errno
import os
import logging
import sys
import time
from threading import Event, Thread
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s %(levelname)s - %(message)s')
fh = logging.FileHandler('lrsn.log')
# ch = logging.StreamHandler()
# ch.setLevel(logging.DEBUG)
# ch.setFormatter(formatter)
fh.setFormatter(formatter)
# log.addHandler(ch)
log.addHandler(fh)
# Quit event to set when to close socket and end thread and user input loops.
t_quit = Event()
# Reconnect event
t_reconn = Event()
def read(sock, quit_event=None, timeout=5):
"""Reads through data in buffer and stops when
there is no more data or a new line is received."""
buf = b''
start = time.time()
while quit_event is None or not quit_event.isSet():
try:
chunk = sock.recv(1024)
buf += chunk
if chunk == b"" or b"\n" in buf:
# Ignore trailing newline
return buf[:-1]
except socket.error as e:
if e.errno != errno.EWOULDBLOCK and e.errno != errno.EAGAIN:
log.error("Error reading from socket: {}".format(os.strerror(e.errno)))
return None
if time.time() - start >= timeout:
return None
def lrsn_listener(sock, quit_event=None):
"""Listener for LRSN messages such as Heartbeat and PageRequestStatus.
Will log messages and keep track of heartbeats and whether to disconnect.
"""
first_heartbeat_recieved = False
last_heartbeat = -1
heartbeat_interval = -1
# We'll now enter a loop to expect heartbeats and status
# message to our paging requests.
while (quit_event is None or not quit_event.isSet()):
# Read and parse incoming XML
out = read(sock, t_quit)
if t_quit.isSet():
break;
if out is not None:
root = ET.fromstring(out)
log.info("Got: {}".format(out))
if root.tag == "Heartbeat":
# Keep track of heartbeats
heartbeat_interval = int(root.attrib["interval"])
last_heartbeat = time.time()
first_heartbeat_recieved = True
else:
# Check heartbeat time window
if first_heartbeat_recieved and time.time() - last_heartbeat > heartbeat_interval:
# Did not receive a heartbeat since the last time within
# the interval. Break out, close, and reconnect.
log.info("Did not receive LRSN Heartbeat within {} since {}. Closing.".format(heartbeat_interval, last_heartbeat))
t_reconn.set()
break
# Idle
time.sleep(0.1)
def compat_input(msg):
if sys.version_info[0] >= 3:
return input(msg)
else:
return raw_input(msg)
def main(ip_address, system_id=None):
# Create a socket for transmitter connection
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setblocking(1)
# Connect to transmitter at LRSN port 3700
try:
sock.connect((ip_address, 3700))
except socket.error as e:
log.error("Unable to connect to {}:3700 - {}".format(ip_address, os.strerror(e.errno)))
print("Unable to connect to {}:3700 - {}".format(ip_address, os.strerror(e.errno)))
# If we're in the process of reconnecting, don't quit.
if not t_reconn.isSet():
t_quit.set()
return
# If this is a retry of a connection and it succeeds,
# we can clear it.
if t_reconn.isSet():
t_reconn.clear()
# At this point, we should get an intro message from the transmitter.
# On the transmitter's LCD, you should see the client count increase
# from 0 to 1.
log.info("Got: {}".format(read(sock)))
# The payload should look like this:
# <LRSN services="NetPage:2.0;Config:1.0;Heartbeat:1.0;Inputs:1.0;PocsagRX:1.0;LrsRX:1.0;TableMan:1.0" device="T7470" swver="8.3.0.13" serno="20384" login="none" />
# It's now going to wait for a Login message. Here, you will want to
# define which services you are interested in. For majority use, NetPage
# and Heartbeat is what you'll want. NetPage provides paging access and
# Heartbeat provides pinging to help determine connection loss. When the transmitter
# sends a heartbeat and the underlying TCP Ack is not received, then it will
# reset the connection.
try:
sock.sendall(b'<Login services="NetPage;Heartbeat" />\n')
except socket.error as e:
log.error("Unable to send PageRequest. Attempting to reconnect.")
t_reconn.set()
sock.close()
return
# ! Don't forget to append a newline at the end. If you use a XML library
# ! to parse and compile XML nodes to send, ensure the resulting buffer
# ! has a newline at the end.
# If everything works out, a LoginAck XML node will be sent with a attribute "ret".
out = read(sock)
log.info("Got: {}".format(out))
# We'll parse the XML node to check to return code.
root = ET.fromstring(out)
logged_in = False
if root.tag == "LoginAck":
# Get return code from attribute "ret". It will be a num berstring,
# so the code should be converted into an integer.
return_code = int(root.attrib["ret"])
# For all return codes, see
# https://paging-systems.readme.io/docs/result-codes
# For the purpose of logging in, we're only checking
# to see if it's successful or not.
if return_code == 0:
# We have logged in.
logged_in = True
else:
# Anything else, we'll print out
log.info("Unable to log in. Received: {}".format(return_code))
sock.close()
t_reconn.set()
return
# Set connection to non-blocking
sock.setblocking(0)
if not logged_in:
print("Cannot log in. Retrying...")
# Wait 5 seconds before reattempting.
time.sleep(5)
t_reconn.set()
try:
sock.close()
except socket.error:
pass
return
# Start listener thread
listener = Thread(target=lrsn_listener, args=(sock,))
listener.start()
page_request_id = 1
# Start input loop. Break out if user explicitely quits or there needs to be
# a reconnect.
while not t_quit.isSet() and not t_reconn.isSet():
number = compat_input("\nType a page number or type quit: ")
if number == "":
continue
elif number == "quit":
t_quit.set()
break
# Verify pager number
try:
number = int(number)
except ValueError:
print("Invalid number. Try again.")
continue
pager_type = compat_input("\nEnter pager type by number (e.g. 0=>AlphaPager): ")
# Verify pager type
try:
pager_type = int(pager_type)
# 0 - Alphanumeric POCSAG pager
# 1 - Numeric POCSAG pager
# 2 - LRS guest pager (coaster, Adverteaser, specialty)
# 3 - Star pager (LRS staff pager)
# 4 - Vibe-only POCSAG pager
if pager_type < 0 or pager_type > 4:
raise ValueError
except ValueError:
print("Invalid pager type. Try again.")
continue
# System ID attribute if provided.
if system_id:
system_id_attr = 'system_id="{}"'.format(system_id)
else:
system_id_attr = ''
if pager_type == 0:
message = compat_input("\nType a message: ")
else:
# This could be any page mode, but we'll keep it constantly as Vibe1
# as an example.
message = 'Vibe1'
try:
# Don't forget the newline at then end!
sock.sendall('<PageRequest id="{}" pager="{};{}" message="{}" {} />\n'.format(page_request_id, pager_type, number, message, system_id_attr).encode('utf-8'))
except socket.error as e:
log.error("Unable to send PageRequest. Attempting to reconnect.")
t_reconn.set()
# Increment page request id.
page_request_id += 1
# Stop listener thread
listener.join()
# Close connection
try:
sock.close()
except:
pass
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='LRSN Python Example Client.')
parser.add_argument('ipaddress', help='IP Address of transmitter.', type=str)
parser.add_argument('-s', '--systemid', help='System ID to use for transmitter.', type=int)
args = parser.parse_args()
while not t_quit.isSet():
try:
main(args.ipaddress, system_id=args.systemid)
if t_reconn.isSet():
time.sleep(5)
log.info("Reconnecting...")
except KeyboardInterrupt:
print("Closing...")
t_quit.set()