mirror of
https://github.com/DJ2LS/FreeDATA
synced 2024-05-14 08:04:33 +00:00
better error handling
This commit is contained in:
parent
69b0ba4c5c
commit
3575a51398
255
sock.py
255
sock.py
|
@ -21,10 +21,12 @@ import helpers
|
||||||
class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
|
class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
|
||||||
|
|
||||||
def handle(self):
|
def handle(self):
|
||||||
|
print("Client connected...")
|
||||||
|
|
||||||
# loop through socket buffer until timeout is reached. then close buffer
|
# loop through socket buffer until timeout is reached. then close buffer
|
||||||
socketTimeout = time.time() + 3
|
socketTimeout = time.time() + 3
|
||||||
while socketTimeout > time.time():
|
while socketTimeout > time.time():
|
||||||
|
|
||||||
time.sleep(0.01)
|
time.sleep(0.01)
|
||||||
encoding = 'utf-8'
|
encoding = 'utf-8'
|
||||||
#data = str(self.request.recv(1024), 'utf-8')
|
#data = str(self.request.recv(1024), 'utf-8')
|
||||||
|
@ -50,7 +52,7 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
|
||||||
received_json = json.loads(data)
|
received_json = json.loads(data)
|
||||||
print(received_json)
|
print(received_json)
|
||||||
except:
|
except:
|
||||||
received_json = bytes()
|
received_json = ''
|
||||||
|
|
||||||
|
|
||||||
# GET COMMANDS
|
# GET COMMANDS
|
||||||
|
@ -67,171 +69,174 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
|
||||||
# "data" : "..."
|
# "data" : "..."
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
# SOCKETTEST ---------------------------------------------------
|
||||||
|
#if data == 'SOCKETTEST':
|
||||||
|
if received_json["command"] == "SOCKETTEST":
|
||||||
|
#cur_thread = threading.current_thread()
|
||||||
|
response = bytes("WELL DONE! YOU ARE ABLE TO COMMUNICATE WITH THE TNC", encoding)
|
||||||
|
self.request.sendall(response)
|
||||||
|
|
||||||
# SOCKETTEST ---------------------------------------------------
|
# CQ CQ CQ -----------------------------------------------------
|
||||||
#if data == 'SOCKETTEST':
|
#if data == 'CQCQCQ':
|
||||||
if received_json["command"] == "SOCKETTEST":
|
if received_json["command"] == "CQCQCQ":
|
||||||
#cur_thread = threading.current_thread()
|
asyncio.run(data_handler.transmit_cq())
|
||||||
response = bytes("WELL DONE! YOU ARE ABLE TO COMMUNICATE WITH THE TNC", encoding)
|
|
||||||
self.request.sendall(response)
|
|
||||||
|
|
||||||
# CQ CQ CQ -----------------------------------------------------
|
|
||||||
#if data == 'CQCQCQ':
|
|
||||||
if received_json["command"] == "CQCQCQ":
|
|
||||||
asyncio.run(data_handler.transmit_cq())
|
|
||||||
|
|
||||||
|
|
||||||
# PING ----------------------------------------------------------
|
# PING ----------------------------------------------------------
|
||||||
#if data.startswith('PING:'):
|
#if data.startswith('PING:'):
|
||||||
if received_json["command"] == "PING":
|
if received_json["command"] == "PING":
|
||||||
# send ping frame and wait for ACK
|
# send ping frame and wait for ACK
|
||||||
dxcallsign = received_json["dxcallsign"]
|
dxcallsign = received_json["dxcallsign"]
|
||||||
asyncio.run(data_handler.transmit_ping(dxcallsign))
|
asyncio.run(data_handler.transmit_ping(dxcallsign))
|
||||||
|
|
||||||
# ARQ CONNECT TO CALLSIGN ----------------------------------------
|
# ARQ CONNECT TO CALLSIGN ----------------------------------------
|
||||||
#if data.startswith('ARQ:CONNECT:'):
|
#if data.startswith('ARQ:CONNECT:'):
|
||||||
#if received_json["command"] == "ARQ:CONNECT":
|
#if received_json["command"] == "ARQ:CONNECT":
|
||||||
#
|
#
|
||||||
# dxcallsign = received_json["dxcallsign"]
|
# dxcallsign = received_json["dxcallsign"]
|
||||||
# static.DXCALLSIGN = bytes(dxcallsign, 'utf-8')
|
# static.DXCALLSIGN = bytes(dxcallsign, 'utf-8')
|
||||||
# static.DXCALLSIGN_CRC8 = helpers.get_crc_8(static.DXCALLSIGN)
|
# static.DXCALLSIGN_CRC8 = helpers.get_crc_8(static.DXCALLSIGN)
|
||||||
|
|
||||||
# if static.ARQ_STATE == 'CONNECTED':
|
# if static.ARQ_STATE == 'CONNECTED':
|
||||||
# # here we could disconnect
|
# # here we could disconnect
|
||||||
# pass
|
# pass
|
||||||
|
|
||||||
# if static.TNC_STATE == 'IDLE':
|
# if static.TNC_STATE == 'IDLE':
|
||||||
|
|
||||||
# asyncio.run(data_handler.arq_connect())
|
# asyncio.run(data_handler.arq_connect())
|
||||||
|
|
||||||
# ARQ DISCONNECT FROM CALLSIGN ----------------------------------------
|
# ARQ DISCONNECT FROM CALLSIGN ----------------------------------------
|
||||||
#if received_json["command"] == "ARQ:DISCONNECT":
|
#if received_json["command"] == "ARQ:DISCONNECT":
|
||||||
# asyncio.run(data_handler.arq_disconnect())
|
# asyncio.run(data_handler.arq_disconnect())
|
||||||
|
|
||||||
|
|
||||||
if received_json["type"] == 'ARQ' and received_json["command"] == "OPEN_DATA_CHANNEL": # and static.ARQ_STATE == 'CONNECTED':
|
if received_json["type"] == 'ARQ' and received_json["command"] == "OPEN_DATA_CHANNEL": # and static.ARQ_STATE == 'CONNECTED':
|
||||||
static.ARQ_READY_FOR_DATA = False
|
static.ARQ_READY_FOR_DATA = False
|
||||||
static.TNC_STATE = 'BUSY'
|
static.TNC_STATE = 'BUSY'
|
||||||
|
|
||||||
dxcallsign = received_json["dxcallsign"]
|
dxcallsign = received_json["dxcallsign"]
|
||||||
static.DXCALLSIGN = bytes(dxcallsign, 'utf-8')
|
static.DXCALLSIGN = bytes(dxcallsign, 'utf-8')
|
||||||
static.DXCALLSIGN_CRC8 = helpers.get_crc_8(static.DXCALLSIGN)
|
static.DXCALLSIGN_CRC8 = helpers.get_crc_8(static.DXCALLSIGN)
|
||||||
|
|
||||||
asyncio.run(data_handler.arq_open_data_channel())
|
asyncio.run(data_handler.arq_open_data_channel())
|
||||||
|
|
||||||
|
|
||||||
if received_json["type"] == 'ARQ' and received_json["command"] == "DATA":# and static.ARQ_READY_FOR_DATA == True: # and static.ARQ_STATE == 'CONNECTED' :
|
if received_json["type"] == 'ARQ' and received_json["command"] == "DATA":# and static.ARQ_READY_FOR_DATA == True: # and static.ARQ_STATE == 'CONNECTED' :
|
||||||
static.TNC_STATE = 'BUSY'
|
static.TNC_STATE = 'BUSY'
|
||||||
|
|
||||||
#on a new transmission we reset the timer
|
#on a new transmission we reset the timer
|
||||||
static.ARQ_START_OF_TRANSMISSION = int(time.time())
|
static.ARQ_START_OF_TRANSMISSION = int(time.time())
|
||||||
|
|
||||||
|
|
||||||
dxcallsign = received_json["dxcallsign"]
|
dxcallsign = received_json["dxcallsign"]
|
||||||
static.DXCALLSIGN = bytes(dxcallsign, 'utf-8')
|
static.DXCALLSIGN = bytes(dxcallsign, 'utf-8')
|
||||||
static.DXCALLSIGN_CRC8 = helpers.get_crc_8(static.DXCALLSIGN)
|
static.DXCALLSIGN_CRC8 = helpers.get_crc_8(static.DXCALLSIGN)
|
||||||
|
|
||||||
|
|
||||||
data_out = bytes(received_json["data"], 'utf-8')
|
data_out = bytes(received_json["data"], 'utf-8')
|
||||||
|
|
||||||
mode = int(received_json["mode"])
|
mode = int(received_json["mode"])
|
||||||
|
|
||||||
n_frames = int(received_json["n_frames"])
|
n_frames = int(received_json["n_frames"])
|
||||||
|
|
||||||
#ARQ_DATA_THREAD = threading.Thread(target=data_handler.arq_transmit, args=[data_out], name="ARQ_DATA")
|
#ARQ_DATA_THREAD = threading.Thread(target=data_handler.arq_transmit, args=[data_out], name="ARQ_DATA")
|
||||||
#ARQ_DATA_THREAD.start()
|
#ARQ_DATA_THREAD.start()
|
||||||
|
|
||||||
ARQ_DATA_THREAD = threading.Thread(target=data_handler.open_dc_and_transmit, args=[data_out, mode, n_frames], name="ARQ_DATA")
|
ARQ_DATA_THREAD = threading.Thread(target=data_handler.open_dc_and_transmit, args=[data_out, mode, n_frames], name="ARQ_DATA")
|
||||||
ARQ_DATA_THREAD.start()
|
ARQ_DATA_THREAD.start()
|
||||||
# asyncio.run(data_handler.arq_transmit(data_out))
|
# asyncio.run(data_handler.arq_transmit(data_out))
|
||||||
|
|
||||||
# SETTINGS AND STATUS ---------------------------------------------
|
# SETTINGS AND STATUS ---------------------------------------------
|
||||||
if received_json["type"] == 'SET' and received_json["command"] == 'MYCALLSIGN':
|
if received_json["type"] == 'SET' and received_json["command"] == 'MYCALLSIGN':
|
||||||
callsign = received_json["parameter"]
|
callsign = received_json["parameter"]
|
||||||
|
|
||||||
if bytes(callsign, encoding) == b'':
|
if bytes(callsign, encoding) == b'':
|
||||||
self.request.sendall(b'INVALID CALLSIGN')
|
self.request.sendall(b'INVALID CALLSIGN')
|
||||||
else:
|
else:
|
||||||
static.MYCALLSIGN = bytes(callsign, encoding)
|
static.MYCALLSIGN = bytes(callsign, encoding)
|
||||||
static.MYCALLSIGN_CRC8 = helpers.get_crc_8(static.MYCALLSIGN)
|
static.MYCALLSIGN_CRC8 = helpers.get_crc_8(static.MYCALLSIGN)
|
||||||
logging.info("CMD | MYCALLSIGN: " + str(static.MYCALLSIGN))
|
logging.info("CMD | MYCALLSIGN: " + str(static.MYCALLSIGN))
|
||||||
|
|
||||||
if received_json["type"] == 'SET' and received_json["command"] == 'MYGRID':
|
if received_json["type"] == 'SET' and received_json["command"] == 'MYGRID':
|
||||||
mygrid = received_json["parameter"]
|
mygrid = received_json["parameter"]
|
||||||
|
|
||||||
if bytes(mygrid, encoding) == b'':
|
if bytes(mygrid, encoding) == b'':
|
||||||
self.request.sendall(b'INVALID GRID')
|
self.request.sendall(b'INVALID GRID')
|
||||||
else:
|
else:
|
||||||
static.MYGRID = bytes(mygrid, encoding)
|
static.MYGRID = bytes(mygrid, encoding)
|
||||||
logging.info("CMD | MYGRID: " + str(static.MYGRID))
|
logging.info("CMD | MYGRID: " + str(static.MYGRID))
|
||||||
|
|
||||||
|
|
||||||
if received_json["type"] == 'GET' and received_json["command"] == 'STATION_INFO':
|
if received_json["type"] == 'GET' and received_json["command"] == 'STATION_INFO':
|
||||||
output = {
|
output = {
|
||||||
"COMMAND": "STATION_INFO",
|
"COMMAND": "STATION_INFO",
|
||||||
"MY_CALLSIGN": str(static.MYCALLSIGN, encoding),
|
"MY_CALLSIGN": str(static.MYCALLSIGN, encoding),
|
||||||
"DX_CALLSIGN": str(static.DXCALLSIGN, encoding),
|
"DX_CALLSIGN": str(static.DXCALLSIGN, encoding),
|
||||||
"DX_GRID": str(static.DXGRID, encoding)
|
"DX_GRID": str(static.DXGRID, encoding)
|
||||||
}
|
}
|
||||||
|
|
||||||
jsondata = json.dumps(output)
|
jsondata = json.dumps(output)
|
||||||
self.request.sendall(bytes(jsondata, encoding))
|
self.request.sendall(bytes(jsondata, encoding))
|
||||||
|
|
||||||
if received_json["type"] == 'GET' and received_json["command"] == 'TNC_STATE':
|
if received_json["type"] == 'GET' and received_json["command"] == 'TNC_STATE':
|
||||||
output = {
|
output = {
|
||||||
"COMMAND": "TNC_STATE",
|
"COMMAND": "TNC_STATE",
|
||||||
"PTT_STATE": str(static.PTT_STATE),
|
"PTT_STATE": str(static.PTT_STATE),
|
||||||
"CHANNEL_STATE": str(static.CHANNEL_STATE),
|
"CHANNEL_STATE": str(static.CHANNEL_STATE),
|
||||||
"TNC_STATE": str(static.TNC_STATE),
|
"TNC_STATE": str(static.TNC_STATE),
|
||||||
"ARQ_STATE": str(static.ARQ_STATE),
|
"ARQ_STATE": str(static.ARQ_STATE),
|
||||||
"AUDIO_RMS": str(static.AUDIO_RMS),
|
"AUDIO_RMS": str(static.AUDIO_RMS),
|
||||||
"BER": str(static.BER),
|
"BER": str(static.BER),
|
||||||
"SNR": str(static.SNR)
|
"SNR": str(static.SNR)
|
||||||
}
|
}
|
||||||
|
|
||||||
jsondata = json.dumps(output)
|
jsondata = json.dumps(output)
|
||||||
self.request.sendall(bytes(jsondata, encoding))
|
self.request.sendall(bytes(jsondata, encoding))
|
||||||
|
|
||||||
if received_json["type"] == 'GET' and received_json["command"] == 'DATA_STATE':
|
if received_json["type"] == 'GET' and received_json["command"] == 'DATA_STATE':
|
||||||
output = {
|
output = {
|
||||||
"COMMAND": "DATA_STATE",
|
"COMMAND": "DATA_STATE",
|
||||||
"RX_BUFFER_LENGTH": str(len(static.RX_BUFFER)),
|
"RX_BUFFER_LENGTH": str(len(static.RX_BUFFER)),
|
||||||
"TX_N_MAX_RETRIES": str(static.TX_N_MAX_RETRIES),
|
"TX_N_MAX_RETRIES": str(static.TX_N_MAX_RETRIES),
|
||||||
"ARQ_TX_N_FRAMES_PER_BURST": str(static.ARQ_TX_N_FRAMES_PER_BURST),
|
"ARQ_TX_N_FRAMES_PER_BURST": str(static.ARQ_TX_N_FRAMES_PER_BURST),
|
||||||
"ARQ_TX_N_BURSTS": str(static.ARQ_TX_N_BURSTS),
|
"ARQ_TX_N_BURSTS": str(static.ARQ_TX_N_BURSTS),
|
||||||
"ARQ_TX_N_CURRENT_ARQ_FRAME": str(int.from_bytes(bytes(static.ARQ_TX_N_CURRENT_ARQ_FRAME), "big")),
|
"ARQ_TX_N_CURRENT_ARQ_FRAME": str(int.from_bytes(bytes(static.ARQ_TX_N_CURRENT_ARQ_FRAME), "big")),
|
||||||
"ARQ_TX_N_TOTAL_ARQ_FRAMES": str(int.from_bytes(bytes(static.ARQ_TX_N_TOTAL_ARQ_FRAMES), "big")),
|
"ARQ_TX_N_TOTAL_ARQ_FRAMES": str(int.from_bytes(bytes(static.ARQ_TX_N_TOTAL_ARQ_FRAMES), "big")),
|
||||||
"ARQ_RX_FRAME_N_BURSTS": str(static.ARQ_RX_FRAME_N_BURSTS),
|
"ARQ_RX_FRAME_N_BURSTS": str(static.ARQ_RX_FRAME_N_BURSTS),
|
||||||
"ARQ_RX_N_CURRENT_ARQ_FRAME": str(static.ARQ_RX_N_CURRENT_ARQ_FRAME),
|
"ARQ_RX_N_CURRENT_ARQ_FRAME": str(static.ARQ_RX_N_CURRENT_ARQ_FRAME),
|
||||||
"ARQ_N_ARQ_FRAMES_PER_DATA_FRAME": str(static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME)
|
"ARQ_N_ARQ_FRAMES_PER_DATA_FRAME": str(static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME)
|
||||||
}
|
}
|
||||||
|
|
||||||
jsondata = json.dumps(output)
|
jsondata = json.dumps(output)
|
||||||
self.request.sendall(bytes(jsondata, encoding))
|
self.request.sendall(bytes(jsondata, encoding))
|
||||||
|
|
||||||
if received_json["type"] == 'GET' and received_json["command"] == 'HEARD_STATIONS':
|
if received_json["type"] == 'GET' and received_json["command"] == 'HEARD_STATIONS':
|
||||||
output = []
|
output = []
|
||||||
for i in range(0, len(static.HEARD_STATIONS)):
|
for i in range(0, len(static.HEARD_STATIONS)):
|
||||||
output.append({"COMMAND": "HEARD_STATIONS", "CALLSIGN": str(static.HEARD_STATIONS[i][0], 'utf-8'),"DXGRID": str(static.HEARD_STATIONS[i][1], 'utf-8'), "TIMESTAMP": static.HEARD_STATIONS[i][2], "DATATYPE": static.HEARD_STATIONS[i][3]})
|
output.append({"COMMAND": "HEARD_STATIONS", "CALLSIGN": str(static.HEARD_STATIONS[i][0], 'utf-8'),"DXGRID": str(static.HEARD_STATIONS[i][1], 'utf-8'), "TIMESTAMP": static.HEARD_STATIONS[i][2], "DATATYPE": static.HEARD_STATIONS[i][3]})
|
||||||
|
|
||||||
jsondata = json.dumps(output)
|
jsondata = json.dumps(output)
|
||||||
self.request.sendall(bytes(jsondata, encoding))
|
self.request.sendall(bytes(jsondata, encoding))
|
||||||
|
|
||||||
|
|
||||||
if received_json["type"] == 'GET' and received_json["command"] == 'RX_BUFFER':
|
if received_json["type"] == 'GET' and received_json["command"] == 'RX_BUFFER':
|
||||||
data = data.split('GET:RX_BUFFER:')
|
data = data.split('GET:RX_BUFFER:')
|
||||||
bufferposition = int(data[1]) - 1
|
bufferposition = int(data[1]) - 1
|
||||||
if bufferposition == -1:
|
if bufferposition == -1:
|
||||||
if len(static.RX_BUFFER) > 0:
|
if len(static.RX_BUFFER) > 0:
|
||||||
self.request.sendall(static.RX_BUFFER[-1])
|
self.request.sendall(static.RX_BUFFER[-1])
|
||||||
|
|
||||||
if bufferposition <= len(static.RX_BUFFER) > 0:
|
if bufferposition <= len(static.RX_BUFFER) > 0:
|
||||||
self.request.sendall(bytes(static.RX_BUFFER[bufferposition]))
|
self.request.sendall(bytes(static.RX_BUFFER[bufferposition]))
|
||||||
|
|
||||||
if received_json["type"] == 'SET' and received_json["command"] == 'DEL_RX_BUFFER':
|
if received_json["type"] == 'SET' and received_json["command"] == 'DEL_RX_BUFFER':
|
||||||
static.RX_BUFFER = []
|
static.RX_BUFFER = []
|
||||||
|
|
||||||
print("sock timeout...")
|
#exception, if JSON cant be decoded
|
||||||
|
except:
|
||||||
|
print("Wrong command")
|
||||||
|
print("Client disconnected...")
|
||||||
|
|
||||||
def start_cmd_socket():
|
def start_cmd_socket():
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue