FreeDATA/sock.py

202 lines
7.4 KiB
Python
Raw Normal View History

2021-02-16 13:23:57 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 25 21:25:14 2020
@author: DJ2LS
"""
import socketserver
import threading
import logging
2021-03-08 09:49:50 +00:00
import json
import asyncio
2021-03-17 10:22:06 +00:00
import time
2021-02-16 13:23:57 +00:00
import static
2021-02-24 13:22:28 +00:00
import data_handler
2021-02-16 19:49:02 +00:00
import helpers
2021-03-12 13:14:36 +00:00
class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
2021-02-16 13:36:01 +00:00
2021-02-16 18:39:08 +00:00
def handle(self):
2021-03-12 13:14:36 +00:00
encoding = 'utf-8'
#data = str(self.request.recv(1024), 'utf-8')
2021-03-12 13:14:36 +00:00
data = bytes()
2021-03-12 13:14:36 +00:00
while True:
chunk = self.request.recv(8192) # .strip()
data += chunk
if chunk.endswith(b'\n'):
break
2021-03-12 13:14:36 +00:00
data = data[:-1] # remove b'\n'
data = str(data, 'utf-8')
2021-03-15 17:43:37 +00:00
# convert data to json object
received_json = json.loads(data)
# GET COMMANDS
# "command" : "..."
# SET COMMANDS
# "command" : "..."
# "parameter" : " ..."
# DATA COMMANDS
# "command" : "..."
# "type" : "..."
# "dxcallsign" : "..."
# "data" : "..."
2021-02-16 18:39:08 +00:00
# SOCKETTEST ---------------------------------------------------
2021-03-15 17:43:37 +00:00
#if data == 'SOCKETTEST':
if received_json["command"] == "SOCKETTEST":
2021-03-09 10:05:59 +00:00
#cur_thread = threading.current_thread()
2021-02-18 14:11:37 +00:00
response = bytes("WELL DONE! YOU ARE ABLE TO COMMUNICATE WITH THE TNC", encoding)
2021-02-16 18:39:08 +00:00
self.request.sendall(response)
2021-03-12 13:14:36 +00:00
# CQ CQ CQ -----------------------------------------------------
2021-03-15 17:43:37 +00:00
#if data == 'CQCQCQ':
if received_json["command"] == "CQCQCQ":
2021-03-09 10:05:59 +00:00
asyncio.run(data_handler.transmit_cq())
2021-03-15 17:43:37 +00:00
# PING ----------------------------------------------------------
2021-03-15 17:43:37 +00:00
#if data.startswith('PING:'):
if received_json["command"] == "PING":
2021-03-12 13:14:36 +00:00
# send ping frame and wait for ACK
2021-03-15 17:43:37 +00:00
dxcallsign = received_json["dxcallsign"]
asyncio.run(data_handler.transmit_ping(dxcallsign))
2021-03-12 13:14:36 +00:00
# ARQ CONNECT TO CALLSIGN ----------------------------------------
2021-03-15 17:43:37 +00:00
#if data.startswith('ARQ:CONNECT:'):
if received_json["command"] == "ARQ:CONNECT":
dxcallsign = received_json["dxcallsign"]
2021-02-28 14:24:14 +00:00
static.DXCALLSIGN = bytes(dxcallsign, 'utf-8')
static.DXCALLSIGN_CRC8 = helpers.get_crc_8(static.DXCALLSIGN)
2021-03-12 13:14:36 +00:00
2021-02-24 16:41:14 +00:00
if static.ARQ_STATE == 'CONNECTED':
2021-02-28 14:24:14 +00:00
# here we could disconnect
pass
2021-03-12 13:14:36 +00:00
2021-02-24 16:41:14 +00:00
if static.TNC_STATE == 'IDLE':
2021-03-12 13:14:36 +00:00
asyncio.run(data_handler.arq_connect())
2021-03-15 17:43:37 +00:00
# ARQ DISCONNECT FROM CALLSIGN ----------------------------------------
#if received_json["command"] == "ARQ:DISCONNECT":
# asyncio.run(data_handler.arq_disconnect())
2021-03-12 13:14:36 +00:00
2021-03-15 17:43:37 +00:00
if received_json["command"] == "ARQ:OPEN_DATA_CHANNEL": # and static.ARQ_STATE == 'CONNECTED':
2021-02-28 14:24:14 +00:00
static.ARQ_READY_FOR_DATA = False
static.TNC_STATE = 'BUSY'
dxcallsign = received_json["dxcallsign"]
static.DXCALLSIGN = bytes(dxcallsign, 'utf-8')
static.DXCALLSIGN_CRC8 = helpers.get_crc_8(static.DXCALLSIGN)
2021-02-28 14:24:14 +00:00
asyncio.run(data_handler.arq_open_data_channel())
2021-03-12 16:03:04 +00:00
2021-03-12 13:14:36 +00:00
if received_json["command"] == "ARQ:DATA" and static.ARQ_READY_FOR_DATA == True: # and static.ARQ_STATE == 'CONNECTED' :
static.TNC_STATE = 'BUSY'
2021-03-17 10:22:06 +00:00
#on a new transmission we reset the timer
static.ARQ_START_OF_TRANSMISSION = int(time.time())
2021-03-15 17:43:37 +00:00
data_out = bytes(received_json["data"], 'utf-8')
2021-03-12 13:14:36 +00:00
ARQ_DATA_THREAD = threading.Thread(target=data_handler.arq_transmit, args=[data_out], name="ARQ_DATA")
2021-03-12 13:14:36 +00:00
ARQ_DATA_THREAD.start()
# asyncio.run(data_handler.arq_transmit(data_out))
# SETTINGS AND STATUS ---------------------------------------------
2021-03-15 17:43:37 +00:00
if received_json["command"] == 'SET:MYCALLSIGN':
callsign = received_json["parameter"]
if bytes(callsign, encoding) == b'':
2021-02-18 14:11:37 +00:00
self.request.sendall(b'INVALID CALLSIGN')
2021-03-12 13:14:36 +00:00
else:
2021-03-15 17:43:37 +00:00
static.MYCALLSIGN = bytes(callsign, encoding)
2021-02-18 14:11:37 +00:00
static.MYCALLSIGN_CRC8 = helpers.get_crc_8(static.MYCALLSIGN)
logging.info("CMD | MYCALLSIGN: " + str(static.MYCALLSIGN))
2021-03-15 17:43:37 +00:00
if received_json["command"] == 'GET:STATION_INFO':
output = {
"MY_CALLSIGN": str(static.MYCALLSIGN, encoding),
"DX_CALLSIGN": str(static.DXCALLSIGN, encoding)
}
jsondata = json.dumps(output)
self.request.sendall(bytes(jsondata, encoding))
2021-03-12 13:14:36 +00:00
2021-03-15 17:43:37 +00:00
if received_json["command"] == 'GET:TNC_STATE':
2021-03-08 09:49:50 +00:00
output = {
"PTT_STATE": str(static.PTT_STATE),
2021-03-09 10:05:59 +00:00
"CHANNEL_STATE": str(static.CHANNEL_STATE),
"TNC_STATE": str(static.TNC_STATE),
2021-03-09 15:45:27 +00:00
"ARQ_STATE": str(static.ARQ_STATE),
2021-03-10 12:43:31 +00:00
"AUDIO_RMS": str(static.AUDIO_RMS),
2021-03-16 15:37:23 +00:00
"BER": str(static.BER),
"SNR": str(static.SNR)
2021-03-08 09:49:50 +00:00
}
2021-03-15 17:43:37 +00:00
2021-03-08 09:49:50 +00:00
jsondata = json.dumps(output)
self.request.sendall(bytes(jsondata, encoding))
2021-03-12 13:14:36 +00:00
2021-03-15 17:43:37 +00:00
if received_json["command"] == 'GET:DATA_STATE':
2021-03-08 09:49:50 +00:00
output = {
"RX_BUFFER_LENGTH": str(len(static.RX_BUFFER)),
"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_BURSTS": str(static.ARQ_TX_N_BURSTS),
2021-03-10 08:12:49 +00:00
"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_RX_FRAME_N_BURSTS": str(static.ARQ_RX_FRAME_N_BURSTS),
2021-03-12 13:14:36 +00:00
"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)
2021-03-08 09:49:50 +00:00
}
2021-03-15 17:43:37 +00:00
2021-03-08 09:49:50 +00:00
jsondata = json.dumps(output)
2021-03-12 13:14:36 +00:00
self.request.sendall(bytes(jsondata, encoding))
2021-03-11 19:04:31 +00:00
2021-03-15 17:43:37 +00:00
if received_json["command"] == 'GET:HEARD_STATIONS':
2021-03-11 19:04:31 +00:00
output = []
2021-03-12 13:14:36 +00:00
for i in range(0, len(static.HEARD_STATIONS)):
2021-03-16 14:21:58 +00:00
output.append({"CALLSIGN": str(static.HEARD_STATIONS[i][0], 'utf-8'), "TIMESTAMP": static.HEARD_STATIONS[i][1], "DATATYPE": static.HEARD_STATIONS[i][2]})
2021-03-12 13:14:36 +00:00
2021-03-11 19:04:31 +00:00
jsondata = json.dumps(output)
self.request.sendall(bytes(jsondata, encoding))
2021-02-16 13:23:57 +00:00
2021-03-15 17:43:37 +00:00
if received_json["command"] == 'GET:RX_BUFFER':
2021-02-16 19:49:02 +00:00
data = data.split('GET:RX_BUFFER:')
2021-03-12 13:14:36 +00:00
bufferposition = int(data[1]) - 1
2021-02-16 21:41:06 +00:00
if bufferposition == -1:
2021-02-16 19:49:02 +00:00
if len(static.RX_BUFFER) > 0:
self.request.sendall(static.RX_BUFFER[-1])
2021-03-12 13:14:36 +00:00
if bufferposition <= len(static.RX_BUFFER) > 0:
2021-03-12 13:14:36 +00:00
self.request.sendall(bytes(static.RX_BUFFER[bufferposition]))
2021-03-15 17:43:37 +00:00
if received_json["command"] == 'DEL:RX_BUFFER':
2021-02-18 14:11:37 +00:00
static.RX_BUFFER = []
2021-02-16 13:36:01 +00:00
def start_cmd_socket():
try:
logging.info("SRV | STARTING TCP/IP SOCKET FOR CMD ON PORT: " + str(static.PORT))
2021-03-12 13:14:36 +00:00
socketserver.TCPServer.allow_reuse_address = True # https://stackoverflow.com/a/16641793
cmdserver = socketserver.TCPServer((static.HOST, static.PORT), CMDTCPRequestHandler)
cmdserver.serve_forever()
2021-03-12 13:14:36 +00:00
finally:
cmdserver.server_close()