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
|
2021-08-08 09:08:34 +00:00
|
|
|
|
|
|
|
# GET COMMANDS
|
|
|
|
# "command" : "..."
|
|
|
|
|
|
|
|
# SET COMMANDS
|
|
|
|
# "command" : "..."
|
|
|
|
# "parameter" : " ..."
|
|
|
|
|
|
|
|
# DATA COMMANDS
|
|
|
|
# "command" : "..."
|
|
|
|
# "type" : "..."
|
|
|
|
# "dxcallsign" : "..."
|
|
|
|
# "data" : "..."
|
|
|
|
|
2021-02-16 13:23:57 +00:00
|
|
|
"""
|
|
|
|
import socketserver
|
|
|
|
import threading
|
2021-08-23 16:14:00 +00:00
|
|
|
import ujson as json
|
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-09-25 13:24:25 +00:00
|
|
|
import sys
|
|
|
|
import os
|
2021-11-18 18:40:22 +00:00
|
|
|
import logging, structlog, log_handler
|
2022-01-20 19:38:56 +00:00
|
|
|
import queue
|
2022-01-22 19:39:37 +00:00
|
|
|
import psutil
|
|
|
|
import audio
|
2022-02-02 20:12:16 +00:00
|
|
|
import base64
|
2022-03-04 15:50:32 +00:00
|
|
|
import atexit
|
2021-11-18 18:40:22 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
SOCKET_QUEUE = queue.Queue()
|
2022-01-22 19:39:37 +00:00
|
|
|
DAEMON_QUEUE = queue.Queue()
|
2021-09-23 15:49:45 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
CONNECTED_CLIENTS = set()
|
2022-02-16 08:11:32 +00:00
|
|
|
CLOSE_SIGNAL = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-09-25 13:24:25 +00:00
|
|
|
|
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
the socket handler base class
|
|
|
|
"""
|
2022-01-22 19:39:37 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler):
|
2022-03-04 15:50:32 +00:00
|
|
|
""" """
|
2022-01-22 19:39:37 +00:00
|
|
|
|
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
def send_to_client(self):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
function called by socket handler
|
|
|
|
send data to a network client if available
|
|
|
|
|
|
|
|
"""
|
2022-02-08 14:27:34 +00:00
|
|
|
tempdata = b''
|
2022-02-16 08:11:32 +00:00
|
|
|
while self.connection_alive and not CLOSE_SIGNAL:
|
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
# send tnc state as network stream
|
2022-01-22 19:39:37 +00:00
|
|
|
# check server port against daemon port and send corresponding data
|
2022-01-28 19:07:39 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
if self.server.server_address[1] == static.PORT and not static.TNCSTARTED:
|
|
|
|
data = send_tnc_state()
|
2022-02-08 14:27:34 +00:00
|
|
|
if data != tempdata:
|
|
|
|
tempdata = data
|
|
|
|
SOCKET_QUEUE.put(data)
|
2022-01-22 19:39:37 +00:00
|
|
|
else:
|
|
|
|
data = send_daemon_state()
|
2022-02-08 14:27:34 +00:00
|
|
|
if data != tempdata:
|
|
|
|
tempdata = data
|
|
|
|
SOCKET_QUEUE.put(data)
|
2022-01-22 19:39:37 +00:00
|
|
|
time.sleep(0.5)
|
2022-01-28 19:07:39 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
|
|
|
|
while not SOCKET_QUEUE.empty():
|
|
|
|
data = SOCKET_QUEUE.get()
|
|
|
|
sock_data = bytes(data, 'utf-8')
|
|
|
|
sock_data += b'\n' # append line limiter
|
2022-02-17 09:11:12 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
# send data to all clients
|
2022-02-17 09:11:12 +00:00
|
|
|
#try:
|
|
|
|
for client in CONNECTED_CLIENTS:
|
|
|
|
try:
|
|
|
|
client.send(sock_data)
|
|
|
|
except:
|
|
|
|
print("connection lost...")
|
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
# we want to transmit scatter data only once to reduce network traffic
|
|
|
|
static.SCATTER = []
|
|
|
|
# we want to display INFO messages only once
|
2022-01-22 19:39:37 +00:00
|
|
|
static.INFO = []
|
|
|
|
#self.request.sendall(sock_data)
|
2022-01-20 19:38:56 +00:00
|
|
|
time.sleep(0.15)
|
|
|
|
|
|
|
|
def receive_from_client(self):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
function which is called by the socket handler
|
|
|
|
it processes the data which is returned by a client
|
|
|
|
"""
|
2022-01-20 19:38:56 +00:00
|
|
|
data = bytes()
|
2022-02-16 08:11:32 +00:00
|
|
|
while self.connection_alive and not CLOSE_SIGNAL:
|
|
|
|
try:
|
|
|
|
chunk = self.request.recv(1024)
|
|
|
|
data += chunk
|
2022-01-20 19:38:56 +00:00
|
|
|
|
2022-02-16 08:11:32 +00:00
|
|
|
if chunk == b'':
|
|
|
|
#print("connection broken. Closing...")
|
|
|
|
self.connection_alive = False
|
2022-03-06 16:23:04 +00:00
|
|
|
|
2022-02-17 09:11:12 +00:00
|
|
|
if data.startswith(b'{') and data.endswith(b'}\n'):
|
2022-02-16 08:11:32 +00:00
|
|
|
# split data by \n if we have multiple commands in socket buffer
|
|
|
|
data = data.split(b'\n')
|
|
|
|
# remove empty data
|
|
|
|
data.remove(b'')
|
|
|
|
|
|
|
|
# iterate thorugh data list
|
|
|
|
for commands in data:
|
|
|
|
if self.server.server_address[1] == static.PORT:
|
|
|
|
process_tnc_commands(commands)
|
|
|
|
else:
|
|
|
|
process_daemon_commands(commands)
|
2022-02-17 09:11:12 +00:00
|
|
|
|
|
|
|
# wait some time between processing multiple commands
|
|
|
|
# this is only a first test to avoid doubled transmission
|
|
|
|
# we might improve this by only processing one command or
|
|
|
|
# doing some kind of selection to determin which commands need to be dropped
|
|
|
|
# and which one can be processed during a running transmission
|
|
|
|
time.sleep(3)
|
|
|
|
|
|
|
|
|
2022-02-16 08:11:32 +00:00
|
|
|
# finally delete our rx buffer to be ready for new commands
|
|
|
|
data = bytes()
|
|
|
|
except Exception as e:
|
|
|
|
structlog.get_logger("structlog").info("[SCK] Connection closed", ip=self.client_address[0], port=self.client_address[1], e=e)
|
|
|
|
self.connection_alive = False
|
2022-02-08 14:27:34 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
|
|
|
|
def handle(self):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
socket handler
|
|
|
|
"""
|
2022-02-16 08:11:32 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
CONNECTED_CLIENTS.add(self.request)
|
2022-01-06 21:15:14 +00:00
|
|
|
|
2022-02-16 08:11:32 +00:00
|
|
|
structlog.get_logger("structlog").debug("[SCK] Client connected", ip=self.client_address[0], port=self.client_address[1])
|
2022-01-20 19:38:56 +00:00
|
|
|
self.connection_alive = True
|
2022-01-22 19:39:37 +00:00
|
|
|
|
2022-02-16 08:11:32 +00:00
|
|
|
self.sendThread = threading.Thread(target=self.send_to_client, args=[],daemon=True).start()
|
|
|
|
self.receiveThread = threading.Thread(target=self.receive_from_client, args=[],daemon=True).start()
|
2022-01-20 19:38:56 +00:00
|
|
|
|
|
|
|
# keep connection alive until we close it
|
2022-02-16 08:11:32 +00:00
|
|
|
while self.connection_alive and not CLOSE_SIGNAL:
|
2022-01-20 19:38:56 +00:00
|
|
|
time.sleep(1)
|
2022-01-06 21:15:14 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
|
2021-05-29 14:57:31 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
def finish(self):
|
2022-03-04 15:50:32 +00:00
|
|
|
""" """
|
2022-02-16 08:11:32 +00:00
|
|
|
structlog.get_logger("structlog").warning("[SCK] Closing client socket", ip=self.client_address[0], port=self.client_address[1])
|
2022-01-28 19:07:39 +00:00
|
|
|
try:
|
|
|
|
CONNECTED_CLIENTS.remove(self.request)
|
|
|
|
except:
|
|
|
|
print("client connection already removed from client list")
|
2021-05-29 14:57:31 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
def process_tnc_commands(data):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
process tnc commands
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-01-20 19:38:56 +00:00
|
|
|
# we need to do some error handling in case of socket timeout or decoding issue
|
|
|
|
try:
|
2022-01-07 10:25:28 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
# convert data to json object
|
|
|
|
received_json = json.loads(data)
|
|
|
|
# CQ CQ CQ -----------------------------------------------------
|
2022-01-28 19:07:39 +00:00
|
|
|
if received_json["command"] == "cqcqcq":
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
data_handler.DATA_QUEUE_TRANSMIT.put(['CQ'])
|
|
|
|
except Exception as e:
|
|
|
|
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
|
2022-01-20 19:38:56 +00:00
|
|
|
# START_BEACON -----------------------------------------------------
|
2022-01-28 19:07:39 +00:00
|
|
|
if received_json["command"] == "start_beacon":
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
static.BEACON_STATE = True
|
|
|
|
interval = int(received_json["parameter"])
|
|
|
|
data_handler.DATA_QUEUE_TRANSMIT.put(['BEACON', interval, True])
|
|
|
|
except Exception as e:
|
|
|
|
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
|
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
# STOP_BEACON -----------------------------------------------------
|
2022-01-28 19:07:39 +00:00
|
|
|
if received_json["command"] == "stop_beacon":
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
static.BEACON_STATE = False
|
|
|
|
structlog.get_logger("structlog").warning("[TNC] Stopping beacon!")
|
|
|
|
data_handler.DATA_QUEUE_TRANSMIT.put(['BEACON', None, False])
|
|
|
|
except Exception as e:
|
|
|
|
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
|
2022-01-20 19:38:56 +00:00
|
|
|
|
|
|
|
# PING ----------------------------------------------------------
|
2022-01-28 19:07:39 +00:00
|
|
|
if received_json["type"] == 'ping' and received_json["command"] == "ping":
|
2022-01-20 19:38:56 +00:00
|
|
|
# send ping frame and wait for ACK
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
dxcallsign = received_json["dxcallsign"]
|
2022-02-21 11:20:36 +00:00
|
|
|
|
|
|
|
# additional step for beeing sure our callsign is correctly
|
|
|
|
# in case we are not getting a station ssid
|
|
|
|
# then we are forcing a station ssid = 0
|
|
|
|
dxcallsign = helpers.callsign_to_bytes(dxcallsign)
|
|
|
|
dxcallsign = helpers.bytes_to_callsign(dxcallsign)
|
|
|
|
|
2022-01-30 13:16:08 +00:00
|
|
|
data_handler.DATA_QUEUE_TRANSMIT.put(['PING', dxcallsign])
|
|
|
|
except Exception as e:
|
|
|
|
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
# CONNECT ----------------------------------------------------------
|
|
|
|
if received_json["type"] == 'arq' and received_json["command"] == "connect":
|
|
|
|
# send ping frame and wait for ACK
|
|
|
|
try:
|
|
|
|
dxcallsign = received_json["dxcallsign"]
|
|
|
|
|
2022-02-21 11:20:36 +00:00
|
|
|
# additional step for beeing sure our callsign is correctly
|
|
|
|
# in case we are not getting a station ssid
|
|
|
|
# then we are forcing a station ssid = 0
|
|
|
|
dxcallsign = helpers.callsign_to_bytes(dxcallsign)
|
|
|
|
dxcallsign = helpers.bytes_to_callsign(dxcallsign)
|
|
|
|
|
2022-03-04 15:50:32 +00:00
|
|
|
static.DXCALLSIGN = dxcallsign
|
|
|
|
static.DXCALLSIGN_CRC = helpers.get_crc_16(static.DXCALLSIGN)
|
|
|
|
|
|
|
|
data_handler.DATA_QUEUE_TRANSMIT.put(['CONNECT', dxcallsign])
|
|
|
|
except Exception as e:
|
|
|
|
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
|
|
|
|
|
|
|
|
# DISCONNECT ----------------------------------------------------------
|
|
|
|
if received_json["type"] == 'arq' and received_json["command"] == "disconnect":
|
|
|
|
# send ping frame and wait for ACK
|
|
|
|
try:
|
|
|
|
data_handler.DATA_QUEUE_TRANSMIT.put(['DISCONNECT'])
|
|
|
|
except Exception as e:
|
|
|
|
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
|
|
|
|
|
|
|
|
# TRANSMIT RAW DATA -------------------------------------------
|
|
|
|
if received_json["type"] == 'arq' and received_json["command"] == "send_raw":
|
|
|
|
try:
|
|
|
|
if not static.ARQ_SESSION:
|
|
|
|
dxcallsign = received_json["parameter"][0]["dxcallsign"]
|
|
|
|
# additional step for beeing sure our callsign is correctly
|
|
|
|
# in case we are not getting a station ssid
|
|
|
|
# then we are forcing a station ssid = 0
|
|
|
|
dxcallsign = helpers.callsign_to_bytes(dxcallsign)
|
|
|
|
dxcallsign = helpers.bytes_to_callsign(dxcallsign)
|
|
|
|
static.DXCALLSIGN = dxcallsign
|
|
|
|
static.DXCALLSIGN_CRC = helpers.get_crc_16(static.DXCALLSIGN)
|
|
|
|
else:
|
2022-03-06 16:23:04 +00:00
|
|
|
dxcallsign = static.DXCALLSIGN
|
2022-03-04 15:50:32 +00:00
|
|
|
static.DXCALLSIGN_CRC = helpers.get_crc_16(static.DXCALLSIGN)
|
|
|
|
|
2022-01-30 13:16:08 +00:00
|
|
|
mode = int(received_json["parameter"][0]["mode"])
|
|
|
|
n_frames = int(received_json["parameter"][0]["n_frames"])
|
2022-02-02 20:12:16 +00:00
|
|
|
base64data = received_json["parameter"][0]["data"]
|
|
|
|
|
|
|
|
if not len(base64data) % 4:
|
|
|
|
binarydata = base64.b64decode(base64data)
|
2022-01-30 13:16:08 +00:00
|
|
|
|
2022-02-02 20:12:16 +00:00
|
|
|
data_handler.DATA_QUEUE_TRANSMIT.put(['ARQ_RAW', binarydata, mode, n_frames])
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise TypeError
|
2022-01-30 13:16:08 +00:00
|
|
|
except Exception as e:
|
2022-02-02 20:12:16 +00:00
|
|
|
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
# STOP TRANSMISSION ----------------------------------------------------------
|
2022-01-28 19:07:39 +00:00
|
|
|
if received_json["type"] == 'arq' and received_json["command"] == "stop_transmission":
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
if static.TNC_STATE == 'BUSY' or static.ARQ_STATE:
|
|
|
|
data_handler.DATA_QUEUE_TRANSMIT.put(['STOP'])
|
|
|
|
structlog.get_logger("structlog").warning("[TNC] Stopping transmission!")
|
|
|
|
static.TNC_STATE = 'IDLE'
|
|
|
|
static.ARQ_STATE = False
|
|
|
|
except Exception as e:
|
|
|
|
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
|
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
|
2022-01-28 19:07:39 +00:00
|
|
|
if received_json["type"] == 'get' and received_json["command"] == 'rx_buffer':
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
output = {
|
|
|
|
"command": "rx_buffer",
|
|
|
|
"data-array": [],
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in range(0, len(static.RX_BUFFER)):
|
2022-03-06 16:23:04 +00:00
|
|
|
#print(static.RX_BUFFER[i][4])
|
2022-02-02 20:12:16 +00:00
|
|
|
#rawdata = json.loads(static.RX_BUFFER[i][4])
|
|
|
|
base64_data = static.RX_BUFFER[i][4]
|
|
|
|
output["data-array"].append({"uuid": static.RX_BUFFER[i][0],"timestamp": static.RX_BUFFER[i][1], "dxcallsign": str(static.RX_BUFFER[i][2], 'utf-8'), "dxgrid": str(static.RX_BUFFER[i][3], 'utf-8'), "data": base64_data})
|
2022-01-30 13:16:08 +00:00
|
|
|
|
|
|
|
jsondata = json.dumps(output)
|
|
|
|
#self.request.sendall(bytes(jsondata, encoding))
|
|
|
|
SOCKET_QUEUE.put(jsondata)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
|
|
|
|
|
2022-02-08 14:27:34 +00:00
|
|
|
|
2022-01-28 19:07:39 +00:00
|
|
|
if received_json["type"] == 'set' and received_json["command"] == 'del_rx_buffer':
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
static.RX_BUFFER = []
|
|
|
|
except Exception as e:
|
|
|
|
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
|
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
# exception, if JSON cant be decoded
|
|
|
|
except Exception as e:
|
2022-01-30 13:16:08 +00:00
|
|
|
structlog.get_logger("structlog").error("[TNC] JSON decoding error", e=e)
|
2022-01-20 19:38:56 +00:00
|
|
|
|
|
|
|
def send_tnc_state():
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
send the tnc state to network
|
|
|
|
"""
|
2022-01-30 13:16:08 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
encoding = 'utf-8'
|
|
|
|
|
|
|
|
output = {
|
2022-01-28 19:07:39 +00:00
|
|
|
"command": "tnc_state",
|
|
|
|
"ptt_state": str(static.PTT_STATE),
|
|
|
|
"tnc_state": str(static.TNC_STATE),
|
|
|
|
"arq_state": str(static.ARQ_STATE),
|
2022-03-04 15:50:32 +00:00
|
|
|
"arq_session": str(static.ARQ_SESSION),
|
2022-01-28 19:07:39 +00:00
|
|
|
"audio_rms": str(static.AUDIO_RMS),
|
|
|
|
"snr": str(static.SNR),
|
|
|
|
"frequency": str(static.HAMLIB_FREQUENCY),
|
2022-02-22 20:05:48 +00:00
|
|
|
"speed_level": str(static.ARQ_SPEED_LEVEL),
|
2022-01-28 19:07:39 +00:00
|
|
|
"mode": str(static.HAMLIB_MODE),
|
|
|
|
"bandwith": str(static.HAMLIB_BANDWITH),
|
|
|
|
"fft": str(static.FFT),
|
2022-02-15 17:10:14 +00:00
|
|
|
"channel_busy": str(static.CHANNEL_BUSY),
|
2022-01-28 19:07:39 +00:00
|
|
|
"scatter": static.SCATTER,
|
|
|
|
"rx_buffer_length": str(len(static.RX_BUFFER)),
|
|
|
|
"rx_msg_buffer_length": str(len(static.RX_MSG_BUFFER)),
|
|
|
|
"arq_bytes_per_minute": str(static.ARQ_BYTES_PER_MINUTE),
|
|
|
|
"arq_bytes_per_minute_burst": str(static.ARQ_BYTES_PER_MINUTE_BURST),
|
|
|
|
"arq_compression_factor": str(static.ARQ_COMPRESSION_FACTOR),
|
|
|
|
"arq_transmission_percent": str(static.ARQ_TRANSMISSION_PERCENT),
|
|
|
|
"total_bytes": str(static.TOTAL_BYTES),
|
|
|
|
"info" : static.INFO,
|
|
|
|
"beacon_state" : str(static.BEACON_STATE),
|
|
|
|
"stations": [],
|
|
|
|
"mycallsign": str(static.MYCALLSIGN, encoding),
|
|
|
|
"dxcallsign": str(static.DXCALLSIGN, encoding),
|
|
|
|
"dxgrid": str(static.DXGRID, encoding),
|
2022-01-20 19:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# add heard stations to heard stations object
|
|
|
|
for i in range(0, len(static.HEARD_STATIONS)):
|
2022-01-28 19:07:39 +00:00
|
|
|
output["stations"].append({"dxcallsign": 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], "snr": static.HEARD_STATIONS[i][4], "offset": static.HEARD_STATIONS[i][5], "frequency": static.HEARD_STATIONS[i][6]})
|
2022-01-20 19:38:56 +00:00
|
|
|
|
|
|
|
jsondata = json.dumps(output)
|
|
|
|
return jsondata
|
|
|
|
|
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
def process_daemon_commands(data):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
process daemon commands
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-01-22 19:39:37 +00:00
|
|
|
# convert data to json object
|
|
|
|
received_json = json.loads(data)
|
2022-01-20 19:38:56 +00:00
|
|
|
|
2022-01-28 19:07:39 +00:00
|
|
|
if received_json["type"] == 'set' and received_json["command"] == 'mycallsign':
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
callsign = received_json["parameter"]
|
2022-03-06 16:23:04 +00:00
|
|
|
|
2022-01-30 13:16:08 +00:00
|
|
|
if bytes(callsign, 'utf-8') == b'':
|
|
|
|
self.request.sendall(b'INVALID CALLSIGN')
|
|
|
|
structlog.get_logger("structlog").warning("[DMN] SET MYCALL FAILED", call=static.MYCALLSIGN, crc=static.MYCALLSIGN_CRC)
|
|
|
|
else:
|
|
|
|
static.MYCALLSIGN = bytes(callsign, 'utf-8')
|
|
|
|
static.MYCALLSIGN_CRC = helpers.get_crc_16(static.MYCALLSIGN)
|
2022-01-20 19:38:56 +00:00
|
|
|
|
2022-01-30 13:16:08 +00:00
|
|
|
structlog.get_logger("structlog").info("[DMN] SET MYCALL", call=static.MYCALLSIGN, crc=static.MYCALLSIGN_CRC)
|
|
|
|
except Exception as e:
|
|
|
|
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
|
|
|
|
|
|
|
|
|
2022-01-28 19:07:39 +00:00
|
|
|
if received_json["type"] == 'set' and received_json["command"] == 'mygrid':
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
mygrid = received_json["parameter"]
|
2022-01-20 19:38:56 +00:00
|
|
|
|
2022-01-30 13:16:08 +00:00
|
|
|
if bytes(mygrid, 'utf-8') == b'':
|
|
|
|
self.request.sendall(b'INVALID GRID')
|
|
|
|
else:
|
|
|
|
static.MYGRID = bytes(mygrid, 'utf-8')
|
|
|
|
structlog.get_logger("structlog").info("[SCK] SET MYGRID", grid=static.MYGRID)
|
|
|
|
except Exception as e:
|
|
|
|
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
|
|
|
|
|
|
|
|
|
2022-01-28 19:07:39 +00:00
|
|
|
if received_json["type"] == 'set' and received_json["command"] == 'start_tnc' and not static.TNCSTARTED:
|
2022-01-22 19:39:37 +00:00
|
|
|
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
mycall = str(received_json["parameter"][0]["mycall"])
|
|
|
|
mygrid = str(received_json["parameter"][0]["mygrid"])
|
|
|
|
rx_audio = str(received_json["parameter"][0]["rx_audio"])
|
|
|
|
tx_audio = str(received_json["parameter"][0]["tx_audio"])
|
|
|
|
devicename = str(received_json["parameter"][0]["devicename"])
|
|
|
|
deviceport = str(received_json["parameter"][0]["deviceport"])
|
|
|
|
serialspeed = str(received_json["parameter"][0]["serialspeed"])
|
|
|
|
pttprotocol = str(received_json["parameter"][0]["pttprotocol"])
|
|
|
|
pttport = str(received_json["parameter"][0]["pttport"])
|
|
|
|
data_bits = str(received_json["parameter"][0]["data_bits"])
|
|
|
|
stop_bits = str(received_json["parameter"][0]["stop_bits"])
|
|
|
|
handshake = str(received_json["parameter"][0]["handshake"])
|
|
|
|
radiocontrol = str(received_json["parameter"][0]["radiocontrol"])
|
|
|
|
rigctld_ip = str(received_json["parameter"][0]["rigctld_ip"])
|
|
|
|
rigctld_port = str(received_json["parameter"][0]["rigctld_port"])
|
2022-02-02 20:12:16 +00:00
|
|
|
enable_scatter = str(received_json["parameter"][0]["enable_scatter"])
|
|
|
|
enable_fft = str(received_json["parameter"][0]["enable_fft"])
|
2022-02-08 14:27:34 +00:00
|
|
|
low_bandwith_mode = str(received_json["parameter"][0]["low_bandwith_mode"])
|
2022-02-02 20:12:16 +00:00
|
|
|
|
2022-01-30 13:16:08 +00:00
|
|
|
DAEMON_QUEUE.put(['STARTTNC', \
|
|
|
|
mycall, \
|
|
|
|
mygrid, \
|
|
|
|
rx_audio, \
|
|
|
|
tx_audio, \
|
|
|
|
devicename, \
|
|
|
|
deviceport, \
|
|
|
|
serialspeed, \
|
|
|
|
pttprotocol, \
|
|
|
|
pttport, \
|
|
|
|
data_bits, \
|
|
|
|
stop_bits, \
|
|
|
|
handshake, \
|
|
|
|
radiocontrol, \
|
|
|
|
rigctld_ip, \
|
2022-02-02 20:12:16 +00:00
|
|
|
rigctld_port, \
|
|
|
|
enable_scatter, \
|
2022-02-08 14:27:34 +00:00
|
|
|
enable_fft, \
|
|
|
|
low_bandwith_mode \
|
2022-01-30 13:16:08 +00:00
|
|
|
])
|
2022-02-02 20:12:16 +00:00
|
|
|
|
2022-01-30 13:16:08 +00:00
|
|
|
except Exception as e:
|
|
|
|
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
|
2022-01-22 19:39:37 +00:00
|
|
|
|
2022-01-28 19:07:39 +00:00
|
|
|
if received_json["type"] == 'get' and received_json["command"] == 'test_hamlib':
|
2022-01-22 19:39:37 +00:00
|
|
|
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
devicename = str(received_json["parameter"][0]["devicename"])
|
|
|
|
deviceport = str(received_json["parameter"][0]["deviceport"])
|
|
|
|
serialspeed = str(received_json["parameter"][0]["serialspeed"])
|
|
|
|
pttprotocol = str(received_json["parameter"][0]["pttprotocol"])
|
|
|
|
pttport = str(received_json["parameter"][0]["pttport"])
|
|
|
|
data_bits = str(received_json["parameter"][0]["data_bits"])
|
|
|
|
stop_bits = str(received_json["parameter"][0]["stop_bits"])
|
|
|
|
handshake = str(received_json["parameter"][0]["handshake"])
|
|
|
|
radiocontrol = str(received_json["parameter"][0]["radiocontrol"])
|
|
|
|
rigctld_ip = str(received_json["parameter"][0]["rigctld_ip"])
|
|
|
|
rigctld_port = str(received_json["parameter"][0]["rigctld_port"])
|
2022-02-08 14:27:34 +00:00
|
|
|
|
2022-01-30 13:16:08 +00:00
|
|
|
DAEMON_QUEUE.put(['TEST_HAMLIB', \
|
|
|
|
devicename, \
|
|
|
|
deviceport, \
|
|
|
|
serialspeed, \
|
|
|
|
pttprotocol, \
|
|
|
|
pttport, \
|
|
|
|
data_bits, \
|
|
|
|
stop_bits, \
|
|
|
|
handshake, \
|
|
|
|
radiocontrol, \
|
|
|
|
rigctld_ip, \
|
|
|
|
rigctld_port \
|
|
|
|
])
|
|
|
|
except Exception as e:
|
|
|
|
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
|
|
|
|
|
2022-01-28 19:07:39 +00:00
|
|
|
if received_json["type"] == 'set' and received_json["command"] == 'stop_tnc':
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
static.TNCPROCESS.kill()
|
2022-03-04 15:50:32 +00:00
|
|
|
# unregister process from atexit to avoid process zombies
|
|
|
|
atexit.unregister(static.TNCPROCESS.kill)
|
|
|
|
|
2022-01-30 13:16:08 +00:00
|
|
|
structlog.get_logger("structlog").warning("[DMN] Stopping TNC")
|
|
|
|
static.TNCSTARTED = False
|
|
|
|
except Exception as e:
|
|
|
|
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
|
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
def send_daemon_state():
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
send the daemon state to network
|
|
|
|
"""
|
2022-02-15 17:10:14 +00:00
|
|
|
try:
|
|
|
|
python_version = str(sys.version_info[0]) + "." + str(sys.version_info[1])
|
|
|
|
|
|
|
|
output = {
|
|
|
|
'command': 'daemon_state',
|
|
|
|
'daemon_state': [],
|
|
|
|
'python_version': str(python_version),
|
|
|
|
'hamlib_version': static.HAMLIB_VERSION,
|
|
|
|
'input_devices': static.AUDIO_INPUT_DEVICES,
|
|
|
|
'output_devices': static.AUDIO_OUTPUT_DEVICES,
|
|
|
|
'serial_devices': static.SERIAL_DEVICES,
|
|
|
|
#'cpu': str(psutil.cpu_percent()),
|
|
|
|
#'ram': str(psutil.virtual_memory().percent),
|
|
|
|
'version': '0.1'
|
|
|
|
}
|
2022-01-22 19:39:37 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
if static.TNCSTARTED:
|
|
|
|
output["daemon_state"].append({"status": "running"})
|
|
|
|
else:
|
|
|
|
output["daemon_state"].append({"status": "stopped"})
|
2022-02-17 15:52:11 +00:00
|
|
|
|
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
jsondata = json.dumps(output)
|
2022-02-17 15:52:11 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
return jsondata
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2022-02-17 09:11:12 +00:00
|
|
|
return None
|