-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsts.py
More file actions
324 lines (245 loc) · 8.39 KB
/
sts.py
File metadata and controls
324 lines (245 loc) · 8.39 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import socket
from threading import Thread, Lock
import configparser
import serial
import time
import sys
import os
import os.path
import RPi.GPIO as GPIO
from datetime import datetime
# import serial_reader as sreader
_SCRIPT_PATH = os.path.dirname(__file__)
_CONFIG_FILE = os.path.join(_SCRIPT_PATH, 'sts.conf')
_LOG_FILE = os.path.join(_SCRIPT_PATH, 'storage/sts.log')
_DUMP_FILE = os.path.join(_SCRIPT_PATH, 'storage/dump.data')
_BUZZER_PIN = 23
_MAX_SERIAL_RETRIES = 5
_MAX_SOCKET_RETRIES = 5
_AUTO_REBOOT = False
mutex = Lock()
barcode_data = []
host = ''
port = 0
serial_port = ''
end_char = 0
def main():
show_credits()
read_config_or_die()
th_serial = Thread(target=read_from_serialport_thread, args=())
th_serial.start()
def show_credits():
print("")
print("Network tools :: STS - Serial to Socket service, v1.78 Yakuma 2022")
print("==================================================================")
print("This tool will work until it reads a barcode that contains the words 'exit', 'bye' or 'quit'")
print("")
def read_config_or_die():
global host
global port
global serial_port
global end_char
print("Using Python '{0}'".format(sys.executable))
print("Process PID {0}".format(os.getpid()))
if not os.path.isfile(_CONFIG_FILE):
sys.stderr.write(
"Configuration file {0} not found\n".format(_CONFIG_FILE))
sys.exit(-1)
if not os.access(_CONFIG_FILE, os.R_OK):
sys.stderr.write(
"Configuration file {0} not readable\n".format(_CONFIG_FILE))
sys.exit(-2)
config = configparser.ConfigParser()
config.read(_CONFIG_FILE)
host = config.get('hostinfo', 'write.ip')
port = config.getint('hostinfo', 'write.port')
serial_port = config.get('hostinfo', 'read.port')
end_char = config.getint('hostinfo', 'read.end_char')
print("")
print("Configuration read:")
print(" Serial port: {0}".format(serial_port))
print(" Network: host {0}:{1}".format(host, port))
# Configure GPIO (required by the buzzer - beeping)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(_BUZZER_PIN, GPIO.OUT, initial=GPIO.LOW)
read_dump_file()
def dump_list():
if len(barcode_data) > 0:
print("Dump of buffered data:")
for b in barcode_data:
dump_bc_to_file("{0}".format(b))
def dump_bc_to_file(bc):
print(" Barcode: {0}".format(bc))
with open(_DUMP_FILE, "a") as f:
f.write(bc + "\n")
def beep(duration_seconds=0.5):
try:
GPIO.output(_BUZZER_PIN, GPIO.HIGH)
time.sleep(duration_seconds)
GPIO.output(_BUZZER_PIN, GPIO.LOW)
except Exception as e:
log_error("Error on beep {0}".format(e))
def beep_ack_communications_ok():
beep(0.15)
beep(0.15)
def beep_ack_communications_wrong():
beep(0.25)
beep(0.25)
beep(0.25)
def read_dump_file():
if not os.path.isfile(_DUMP_FILE):
return
try:
with open(_DUMP_FILE, "r") as f:
lines = f.readlines()
for bc in lines:
add_barcode(bc)
finally:
os.remove(_DUMP_FILE)
def reboot_device():
return
# print ("Rebooting device")
# os.system("sudo shutdown -r")
def add_barcode(barcode):
b_to_append = barcode.strip().replace("\n", "").replace("\r", "").replace("\t", "")
b_to_append = ''.join(e for e in b_to_append if e.isalnum())
if b_to_append == "":
return
mutex.acquire()
try:
print("Barcode queued '{0}'".format(b_to_append))
barcode_data.append(b_to_append)
beep()
finally:
mutex.release()
def log_error(text):
sys.stderr.write("ERROR " + text + "\n")
with open(_LOG_FILE, "a") as f:
# [2020-01-02 07:44:28] ERROR
f.write(datetime.now().strftime(
"[%Y-%m-%d %H:%M:%S]") + " ERROR " + text + "\n")
def read_from_serialport_thread():
ser = open_serial_port()
if not ser:
log_error("Opening serial port {0}".format(serial_port))
return
finish_socket_thread = False
id = 0
print("Reading data from serial port '{0}'".format(serial_port))
print("")
th_socket = Thread(target=socket_thread, args=(id, lambda: finish_socket_thread))
th_socket.start()
reboot = False
while 1:
try:
if not ser:
raise Exception("Serial port not assigned")
barcode = read_serial_port(ser)
bclean = barcode.strip()
if bclean == "quit" or bclean == "exit" or bclean == "bye":
print("Exit requested. Ending process.")
break
add_barcode(bclean)
except Exception as e:
log_error("Reading serial port. Exception: '{0}'".format(e))
ser = reopen_serial_port(ser)
if not ser:
log_error("Trying to reopen serial port '{0}'".format(serial_port))
if _AUTO_REBOOT:
reboot = True
break
finish_socket_thread = True
close_serial_port(ser)
dump_list()
print("")
print("Cleaning GPIO")
GPIO.cleanup()
print("")
print("Finishing main thread.")
if reboot:
reboot_device()
def socket_thread(id, stop):
while 1:
if stop():
print("Finishing socket thread. OK.")
break
time.sleep(.250)
mutex.acquire()
while len(barcode_data) != 0:
barcode = barcode_data[0]
if send_to_socket(barcode):
del barcode_data[0]
mutex.release()
def close_serial_port(ser):
if isinstance(ser, serial.Serial):
if ser.is_open:
ser.close()
def open_serial_port():
print("Opening serial port '{0}'".format(serial_port))
connected = False
attempts = 0
while not connected and attempts < _MAX_SERIAL_RETRIES:
try:
ser = serial.Serial(serial_port, 9600, timeout=1)
connected = ser.is_open
except Exception:
attempts += 1
time.sleep(2) # Wait prior to attempt to open the port once again
if connected:
print("Port '{0}' opened successfully".format(ser.name))
return ser
else:
log_error("Unable to open serial port '{0}'".format(serial_port))
return False
def reopen_serial_port(ser):
print("Reopening serial port")
close_serial_port(ser)
return open_serial_port()
def read_serial_port(ser):
if isinstance(ser, serial.Serial):
# In case the CPU usage is really high we can replace the serial.readline() by our own implementation
# return sreader.ReadLine(ser).readline().decode()
return ser.readline().decode()
else:
return ""
def send_to_socket(barcode):
if barcode.strip() == "":
return True
connected = False
retries = 0
client_socket = None
while not connected and retries < _MAX_SOCKET_RETRIES:
try:
print("Sending barcode '{2}' to {0}:{1}".format(
host, port, barcode))
client_socket = socket.socket()
client_socket.settimeout(2)
client_socket.connect((host, port))
connected = True
except Exception as e:
log_error("Connecting: {0}".format(e))
retries += 1
if client_socket is not None:
client_socket.close()
time.sleep(1)
if connected:
try:
client_socket.send(barcode.encode())
data = client_socket.recv(128).decode()
if data == "ok":
return True
else:
log_error("Wrong response received: '{0}'".format(data))
beep_ack_communications_wrong()
return False
except Exception as e:
log_error("Sending barcode '{0}' got '{1}'".format(barcode, e))
beep_ack_communications_wrong()
return False
else:
log_error(
"Unable to connect to {0}:{1}! Check remote host!".format(host, port))
beep_ack_communications_wrong()
if __name__ == '__main__':
main()