2021-06-13 15:21:37 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
2022-01-07 10:25:28 +00:00
|
|
|
daemon.py
|
2021-06-13 15:21:37 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
Author: DJ2LS, January 2022
|
2021-06-13 15:21:37 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import threading
|
|
|
|
import socketserver
|
|
|
|
import time
|
2021-08-23 16:14:00 +00:00
|
|
|
import sys
|
2021-09-25 13:24:25 +00:00
|
|
|
import subprocess
|
|
|
|
import ujson as json
|
|
|
|
import psutil
|
2021-09-04 20:13:15 +00:00
|
|
|
import serial.tools.list_ports
|
2021-09-25 13:24:25 +00:00
|
|
|
import static
|
2021-10-13 18:19:54 +00:00
|
|
|
import crcengine
|
2021-11-18 18:40:22 +00:00
|
|
|
import re
|
2022-01-07 10:25:28 +00:00
|
|
|
import structlog
|
|
|
|
import log_handler
|
2022-01-05 13:15:59 +00:00
|
|
|
import helpers
|
2022-01-12 06:27:42 +00:00
|
|
|
import os
|
2021-12-28 16:05:48 +00:00
|
|
|
|
2021-11-18 18:40:22 +00:00
|
|
|
log_handler.setup_logging("daemon")
|
2022-01-10 08:06:08 +00:00
|
|
|
structlog.get_logger("structlog").info("[DMN] Starting FreeDATA daemon", author="DJ2LS", year="2022", version="0.1")
|
|
|
|
|
2021-11-24 18:16:09 +00:00
|
|
|
# get python version, which is needed later for determining installation path
|
|
|
|
python_version = str(sys.version_info[0]) + "." + str(sys.version_info[1])
|
2022-01-10 08:06:08 +00:00
|
|
|
structlog.get_logger("structlog").info("[DMN] Python", version=python_version)
|
2021-11-07 11:17:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
####################################################
|
|
|
|
# https://stackoverflow.com/questions/7088672/pyaudio-working-but-spits-out-error-messages-each-time
|
|
|
|
# https://github.com/DJ2LS/FreeDATA/issues/22
|
|
|
|
# we need to have a look at this if we want to run this on Windows and MacOS !
|
|
|
|
# Currently it seems, this is a Linux-only problem
|
|
|
|
|
|
|
|
from ctypes import *
|
|
|
|
from contextlib import contextmanager
|
|
|
|
import pyaudio
|
|
|
|
|
|
|
|
ERROR_HANDLER_FUNC = CFUNCTYPE(None, c_char_p, c_int, c_char_p, c_int, c_char_p)
|
|
|
|
|
|
|
|
def py_error_handler(filename, line, function, err, fmt):
|
|
|
|
pass
|
|
|
|
|
|
|
|
c_error_handler = ERROR_HANDLER_FUNC(py_error_handler)
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def noalsaerr():
|
|
|
|
asound = cdll.LoadLibrary('libasound.so')
|
|
|
|
asound.snd_lib_error_set_handler(c_error_handler)
|
|
|
|
yield
|
|
|
|
asound.snd_lib_error_set_handler(None)
|
|
|
|
|
|
|
|
# with noalsaerr():
|
|
|
|
# p = pyaudio.PyAudio()
|
|
|
|
######################################################
|
2021-11-24 18:16:09 +00:00
|
|
|
|
2022-01-12 06:27:42 +00:00
|
|
|
# check if we are running in a pyinstaller environment
|
|
|
|
try:
|
|
|
|
app_path = sys._MEIPASS
|
|
|
|
except:
|
|
|
|
app_path = os.path.abspath(".")
|
|
|
|
sys.path.append(app_path)
|
|
|
|
|
2021-11-24 18:16:09 +00:00
|
|
|
# try importing hamlib
|
2021-10-17 13:57:41 +00:00
|
|
|
try:
|
2021-11-24 18:16:09 +00:00
|
|
|
# installation path for Ubuntu 20.04 LTS python modules
|
|
|
|
sys.path.append('/usr/local/lib/python'+ python_version +'/site-packages')
|
2022-01-11 13:57:11 +00:00
|
|
|
|
2021-11-24 18:16:09 +00:00
|
|
|
# installation path for Ubuntu 20.10 +
|
|
|
|
sys.path.append('/usr/local/lib/')
|
2022-01-11 13:57:11 +00:00
|
|
|
|
|
|
|
# everything else... not nice, but an attempt
|
|
|
|
sys.path.append('/usr/local/lib/python3.6/site-packages')
|
|
|
|
sys.path.append('/usr/local/lib/python3.7/site-packages')
|
|
|
|
sys.path.append('/usr/local/lib/python3.8/site-packages')
|
|
|
|
sys.path.append('/usr/local/lib/python3.9/site-packages')
|
|
|
|
sys.path.append('/usr/local/lib/python3.10/site-packages')
|
|
|
|
|
|
|
|
|
2021-11-24 18:16:09 +00:00
|
|
|
import Hamlib
|
|
|
|
|
2021-11-18 18:40:22 +00:00
|
|
|
# https://stackoverflow.com/a/4703409
|
|
|
|
hamlib_version = re.findall(r"[-+]?\d*\.?\d+|\d+", Hamlib.cvar.hamlib_version)
|
|
|
|
hamlib_version = float(hamlib_version[0])
|
2021-11-24 18:16:09 +00:00
|
|
|
|
|
|
|
min_hamlib_version = 4.1
|
|
|
|
if hamlib_version > min_hamlib_version:
|
|
|
|
structlog.get_logger("structlog").info("[DMN] Hamlib found", version=hamlib_version)
|
|
|
|
else:
|
|
|
|
structlog.get_logger("structlog").warning("[DMN] Hamlib outdated", found=hamlib_version, recommend=min_hamlib_version)
|
|
|
|
except Exception as e:
|
|
|
|
structlog.get_logger("structlog").critical("[DMN] Hamlib not found", error=e)
|
|
|
|
|
|
|
|
# load crc engine
|
2021-10-13 18:19:54 +00:00
|
|
|
crc_algorithm = crcengine.new('crc16-ccitt-false') # load crc8 library
|
|
|
|
|
2021-06-13 15:21:37 +00:00
|
|
|
|
|
|
|
def start_daemon():
|
|
|
|
|
|
|
|
try:
|
2021-11-18 18:40:22 +00:00
|
|
|
structlog.get_logger("structlog").info("[DMN] Starting TCP/IP socket", port=PORT)
|
2021-09-25 13:24:25 +00:00
|
|
|
# https://stackoverflow.com/a/16641793
|
|
|
|
socketserver.TCPServer.allow_reuse_address = True
|
2021-11-18 18:40:22 +00:00
|
|
|
daemon = socketserver.TCPServer(('0.0.0.0', PORT), CMDTCPRequestHandler)
|
2021-06-13 15:21:37 +00:00
|
|
|
daemon.serve_forever()
|
|
|
|
|
|
|
|
finally:
|
2021-11-18 18:40:22 +00:00
|
|
|
structlog.get_logger("structlog").warning("[DMN] Closing socket", port=PORT)
|
2021-06-13 15:21:37 +00:00
|
|
|
daemon.server_close()
|
2021-09-25 13:24:25 +00:00
|
|
|
|
|
|
|
|
2021-06-13 15:21:37 +00:00
|
|
|
class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
|
|
|
|
|
|
|
|
def handle(self):
|
2021-11-18 18:40:22 +00:00
|
|
|
structlog.get_logger("structlog").debug("[DMN] Client connected", ip=self.client_address[0])
|
2021-09-25 13:24:25 +00:00
|
|
|
|
2021-06-13 15:21:37 +00:00
|
|
|
# loop through socket buffer until timeout is reached. then close buffer
|
2022-01-11 13:57:11 +00:00
|
|
|
socketTimeout = time.time() + 6
|
2021-06-13 15:21:37 +00:00
|
|
|
while socketTimeout > time.time():
|
|
|
|
|
|
|
|
time.sleep(0.01)
|
|
|
|
encoding = 'utf-8'
|
|
|
|
#data = str(self.request.recv(1024), 'utf-8')
|
|
|
|
|
|
|
|
data = bytes()
|
2021-09-25 13:24:25 +00:00
|
|
|
|
2021-06-13 15:21:37 +00:00
|
|
|
# we need to loop through buffer until end of chunk is reached or timeout occured
|
2022-01-07 10:25:28 +00:00
|
|
|
while socketTimeout > time.time():
|
2021-09-24 15:16:38 +00:00
|
|
|
chunk = self.request.recv(45)
|
2021-06-13 15:21:37 +00:00
|
|
|
data += chunk
|
2021-09-25 13:24:25 +00:00
|
|
|
# or chunk.endswith(b'\n'):
|
2022-01-07 10:25:28 +00:00
|
|
|
if chunk.startswith(b'{"type"') and chunk.endswith(b'}\n'):
|
2021-06-13 15:21:37 +00:00
|
|
|
break
|
|
|
|
data = data[:-1] # remove b'\n'
|
2022-01-07 10:25:28 +00:00
|
|
|
data = str(data, encoding)
|
2021-09-25 13:24:25 +00:00
|
|
|
|
2021-06-13 15:21:37 +00:00
|
|
|
if len(data) > 0:
|
2022-01-07 10:25:28 +00:00
|
|
|
# reset socket timeout
|
|
|
|
socketTimeout = time.time() + static.SOCKET_TIMEOUT
|
2021-09-24 15:16:38 +00:00
|
|
|
# only read first line of string. multiple lines will cause an json error
|
|
|
|
# this occurs possibly, if we are getting data too fast
|
2022-01-07 10:25:28 +00:00
|
|
|
# data = data.splitlines()[0]
|
2021-09-24 15:16:38 +00:00
|
|
|
data = data.splitlines()[0]
|
2022-01-07 10:25:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
# we need to do some error handling in case of socket timeout or decoding issue
|
|
|
|
try:
|
|
|
|
|
|
|
|
# convert data to json object
|
2021-06-13 15:21:37 +00:00
|
|
|
received_json = json.loads(data)
|
|
|
|
|
|
|
|
# GET COMMANDS
|
|
|
|
# "command" : "..."
|
2021-09-25 13:24:25 +00:00
|
|
|
|
2021-06-13 15:21:37 +00:00
|
|
|
# SET COMMANDS
|
|
|
|
# "command" : "..."
|
|
|
|
# "parameter" : " ..."
|
2021-09-25 13:24:25 +00:00
|
|
|
|
2021-06-13 15:21:37 +00:00
|
|
|
# DATA COMMANDS
|
|
|
|
# "command" : "..."
|
|
|
|
# "type" : "..."
|
|
|
|
# "dxcallsign" : "..."
|
|
|
|
# "data" : "..."
|
2021-09-25 13:24:25 +00:00
|
|
|
|
2021-07-10 21:27:33 +00:00
|
|
|
# print(received_json)
|
2021-09-25 13:24:25 +00:00
|
|
|
# print(received_json["type"])
|
|
|
|
# print(received_json["command"])
|
|
|
|
# try:
|
2022-01-05 13:15:59 +00:00
|
|
|
|
|
|
|
if received_json["type"] == 'SET' and received_json["command"] == 'MYCALLSIGN':
|
|
|
|
callsign = received_json["parameter"]
|
|
|
|
print(received_json)
|
|
|
|
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_CRC8)
|
|
|
|
else:
|
|
|
|
static.MYCALLSIGN = bytes(callsign, 'utf-8')
|
|
|
|
static.MYCALLSIGN_CRC8 = helpers.get_crc_8(static.MYCALLSIGN)
|
|
|
|
|
|
|
|
structlog.get_logger("structlog").info("[DMN] SET MYCALL", call=static.MYCALLSIGN, crc=static.MYCALLSIGN_CRC8)
|
|
|
|
|
|
|
|
if received_json["type"] == 'SET' and received_json["command"] == 'MYGRID':
|
|
|
|
mygrid = received_json["parameter"]
|
|
|
|
|
|
|
|
if bytes(mygrid, 'utf-8') == b'':
|
|
|
|
self.request.sendall(b'INVALID GRID')
|
|
|
|
else:
|
|
|
|
static.MYGRID = bytes(mygrid, 'utf-8')
|
|
|
|
structlog.get_logger("structlog").info("[DMN] SET MYGRID", grid=static.MYGRID)
|
|
|
|
|
2021-07-09 17:26:02 +00:00
|
|
|
|
|
|
|
if received_json["type"] == 'SET' and received_json["command"] == 'STARTTNC' and not static.TNCSTARTED:
|
2022-01-05 13:15:59 +00:00
|
|
|
mycall = str(received_json["parameter"][0]["mycall"])
|
|
|
|
mygrid = str(received_json["parameter"][0]["mygrid"])
|
2021-09-25 12:57:44 +00:00
|
|
|
rx_audio = str(received_json["parameter"][0]["rx_audio"])
|
|
|
|
tx_audio = str(received_json["parameter"][0]["tx_audio"])
|
2021-12-26 08:20:58 +00:00
|
|
|
devicename = str(received_json["parameter"][0]["devicename"])
|
2021-10-17 13:57:41 +00:00
|
|
|
deviceport = str(received_json["parameter"][0]["deviceport"])
|
|
|
|
serialspeed = str(received_json["parameter"][0]["serialspeed"])
|
|
|
|
pttprotocol = str(received_json["parameter"][0]["pttprotocol"])
|
2021-09-25 12:57:44 +00:00
|
|
|
pttport = str(received_json["parameter"][0]["pttport"])
|
2021-12-27 11:30:43 +00:00
|
|
|
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"])
|
|
|
|
|
|
|
|
|
2021-11-18 18:40:22 +00:00
|
|
|
|
2021-12-26 08:20:58 +00:00
|
|
|
structlog.get_logger("structlog").warning("[DMN] Starting TNC", rig=devicename, port=deviceport)
|
2021-11-18 18:40:22 +00:00
|
|
|
#print(received_json["parameter"][0])
|
2021-09-03 14:34:46 +00:00
|
|
|
|
2021-09-25 13:24:25 +00:00
|
|
|
# command = "--rx "+ rx_audio +" \
|
2021-09-25 12:57:44 +00:00
|
|
|
# --tx "+ tx_audio +" \
|
|
|
|
# --deviceport "+ deviceport +" \
|
|
|
|
# --deviceid "+ deviceid + " \
|
|
|
|
# --serialspeed "+ serialspeed + " \
|
|
|
|
# --pttprotocol "+ pttprotocol + " \
|
|
|
|
# --pttport "+ pttport
|
2021-09-25 13:24:25 +00:00
|
|
|
|
2021-09-25 12:57:44 +00:00
|
|
|
# list of parameters, necessary for running subprocess command as a list
|
|
|
|
options = []
|
2022-01-05 13:15:59 +00:00
|
|
|
options.append('--mycall')
|
|
|
|
options.append(mycall)
|
|
|
|
options.append('--mygrid')
|
|
|
|
options.append(mygrid)
|
2021-09-25 12:57:44 +00:00
|
|
|
options.append('--rx')
|
|
|
|
options.append(rx_audio)
|
|
|
|
options.append('--tx')
|
|
|
|
options.append(tx_audio)
|
|
|
|
options.append('--deviceport')
|
|
|
|
options.append(deviceport)
|
2021-12-26 08:20:58 +00:00
|
|
|
options.append('--devicename')
|
|
|
|
options.append(devicename)
|
2021-09-25 12:57:44 +00:00
|
|
|
options.append('--serialspeed')
|
|
|
|
options.append(serialspeed)
|
|
|
|
options.append('--pttprotocol')
|
|
|
|
options.append(pttprotocol)
|
|
|
|
options.append('--pttport')
|
|
|
|
options.append(pttport)
|
2021-12-27 11:30:43 +00:00
|
|
|
options.append('--data_bits')
|
|
|
|
options.append(data_bits)
|
|
|
|
options.append('--stop_bits')
|
|
|
|
options.append(stop_bits)
|
|
|
|
options.append('--handshake')
|
|
|
|
options.append(handshake)
|
2021-12-28 16:05:48 +00:00
|
|
|
|
|
|
|
if HAMLIB_USE_RIGCTL:
|
|
|
|
options.append('--rigctl')
|
2021-12-27 11:30:43 +00:00
|
|
|
|
2021-12-28 16:05:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-09-25 13:24:25 +00:00
|
|
|
|
2021-09-16 15:17:55 +00:00
|
|
|
# try running tnc from binary, else run from source
|
|
|
|
# this helps running the tnc in a developer environment
|
|
|
|
try:
|
2021-09-25 12:57:44 +00:00
|
|
|
command = []
|
2022-01-12 06:27:42 +00:00
|
|
|
if sys.platform == 'linux' or sys.platform == 'darwin':
|
|
|
|
command.append('./tnc')
|
|
|
|
elif sys.platform == 'win32' or sys.platform == 'win64':
|
|
|
|
command.append('tnc.exe')
|
|
|
|
|
2021-09-25 12:57:44 +00:00
|
|
|
command += options
|
|
|
|
p = subprocess.Popen(command)
|
2021-11-18 18:40:22 +00:00
|
|
|
structlog.get_logger("structlog").info("[DMN] TNC started", path="binary")
|
|
|
|
except:
|
2021-09-25 12:57:44 +00:00
|
|
|
command = []
|
2022-01-12 06:27:42 +00:00
|
|
|
if sys.platform == 'linux' or sys.platform == 'darwin':
|
|
|
|
command.append('python3')
|
|
|
|
elif sys.platform == 'win32' or sys.platform == 'win64':
|
|
|
|
command.append('python')
|
|
|
|
|
2021-09-25 12:57:44 +00:00
|
|
|
command.append('main.py')
|
|
|
|
command += options
|
|
|
|
p = subprocess.Popen(command)
|
2021-11-18 18:40:22 +00:00
|
|
|
structlog.get_logger("structlog").info("[DMN] TNC started", path="source")
|
2021-09-25 13:24:25 +00:00
|
|
|
|
|
|
|
static.TNCPROCESS = p # .pid
|
2021-07-09 17:26:02 +00:00
|
|
|
static.TNCSTARTED = True
|
|
|
|
|
|
|
|
if received_json["type"] == 'SET' and received_json["command"] == 'STOPTNC':
|
|
|
|
static.TNCPROCESS.kill()
|
2021-11-18 18:40:22 +00:00
|
|
|
structlog.get_logger("structlog").warning("[DMN] Stopping TNC")
|
2021-07-09 17:26:02 +00:00
|
|
|
#os.kill(static.TNCPROCESS, signal.SIGKILL)
|
|
|
|
static.TNCSTARTED = False
|
2021-09-25 13:24:25 +00:00
|
|
|
|
2021-06-13 15:21:37 +00:00
|
|
|
if received_json["type"] == 'GET' and received_json["command"] == 'DAEMON_STATE':
|
|
|
|
|
2021-12-20 17:05:00 +00:00
|
|
|
data = {
|
|
|
|
'COMMAND': 'DAEMON_STATE',
|
|
|
|
'DAEMON_STATE': [],
|
|
|
|
'PYTHON_VERSION': str(python_version),
|
|
|
|
'HAMLIB_VERSION': str(hamlib_version),
|
|
|
|
'INPUT_DEVICES': [],
|
|
|
|
'OUTPUT_DEVICES': [],
|
|
|
|
'SERIAL_DEVICES': [
|
2021-09-25 13:24:25 +00:00
|
|
|
], "CPU": str(psutil.cpu_percent()), "RAM": str(psutil.virtual_memory().percent), "VERSION": "0.1-prototype"}
|
2021-06-13 15:21:37 +00:00
|
|
|
|
2021-07-09 17:26:02 +00:00
|
|
|
if static.TNCSTARTED:
|
|
|
|
data["DAEMON_STATE"].append({"STATUS": "running"})
|
|
|
|
else:
|
|
|
|
data["DAEMON_STATE"].append({"STATUS": "stopped"})
|
2021-06-13 15:21:37 +00:00
|
|
|
|
2021-11-07 15:12:19 +00:00
|
|
|
# UPDATE LIST OF AUDIO DEVICES
|
|
|
|
try:
|
|
|
|
# we need to "try" this, because sometimes libasound.so isn't in the default place
|
|
|
|
# try to supress error messages
|
|
|
|
with noalsaerr(): # https://github.com/DJ2LS/FreeDATA/issues/22
|
|
|
|
p = pyaudio.PyAudio()
|
|
|
|
# else do it the default way
|
|
|
|
except:
|
2022-01-07 15:58:45 +00:00
|
|
|
p = pyaudio.PyAudio()
|
2021-11-07 15:12:19 +00:00
|
|
|
|
2021-10-05 19:03:15 +00:00
|
|
|
for i in range(0, p.get_device_count()):
|
2022-01-07 15:58:45 +00:00
|
|
|
# we need to do a try exception, beacuse for windows theres now audio device range
|
|
|
|
try:
|
|
|
|
maxInputChannels = p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')
|
|
|
|
maxOutputChannels = p.get_device_info_by_host_api_device_index(0, i).get('maxOutputChannels')
|
|
|
|
name = p.get_device_info_by_host_api_device_index(0, i).get('name')
|
|
|
|
except:
|
|
|
|
maxInputChannels = 0
|
|
|
|
maxOutputChannels = 0
|
|
|
|
name = ''
|
2021-12-27 14:28:58 +00:00
|
|
|
#crc_name = crc_algorithm(bytes(name, encoding='utf-8'))
|
|
|
|
#crc_name = crc_name.to_bytes(2, byteorder='big')
|
|
|
|
#crc_name = crc_name.hex()
|
|
|
|
#name = name + ' [' + crc_name + ']'
|
2021-12-27 12:33:53 +00:00
|
|
|
|
2021-10-05 19:03:15 +00:00
|
|
|
if maxInputChannels > 0:
|
|
|
|
data["INPUT_DEVICES"].append(
|
|
|
|
{"ID": i, "NAME": str(name)})
|
|
|
|
if maxOutputChannels > 0:
|
|
|
|
data["OUTPUT_DEVICES"].append(
|
|
|
|
{"ID": i, "NAME": str(name)})
|
|
|
|
p.terminate()
|
|
|
|
|
|
|
|
# UPDATE LIST OF SERIAL DEVICES
|
|
|
|
ports = serial.tools.list_ports.comports()
|
|
|
|
for port, desc, hwid in ports:
|
2021-10-13 18:19:54 +00:00
|
|
|
|
|
|
|
# calculate hex of hwid if we have unique names
|
|
|
|
crc_hwid = crc_algorithm(bytes(hwid, encoding='utf-8'))
|
|
|
|
crc_hwid = crc_hwid.to_bytes(2, byteorder='big')
|
|
|
|
crc_hwid = crc_hwid.hex()
|
|
|
|
description = desc + ' [' + crc_hwid + ']'
|
|
|
|
|
2021-10-05 19:03:15 +00:00
|
|
|
data["SERIAL_DEVICES"].append(
|
2021-10-13 18:19:54 +00:00
|
|
|
{"PORT": str(port), "DESCRIPTION": str(description) })
|
2021-10-05 19:03:15 +00:00
|
|
|
|
|
|
|
|
2021-06-13 15:21:37 +00:00
|
|
|
jsondata = json.dumps(data)
|
|
|
|
self.request.sendall(bytes(jsondata, encoding))
|
2021-09-25 13:24:25 +00:00
|
|
|
|
2021-10-17 13:57:41 +00:00
|
|
|
|
|
|
|
if received_json["type"] == 'GET' and received_json["command"] == 'TEST_HAMLIB':
|
|
|
|
|
|
|
|
try:
|
2021-12-26 08:55:20 +00:00
|
|
|
print(received_json["parameter"])
|
|
|
|
|
|
|
|
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"])
|
2021-10-17 13:57:41 +00:00
|
|
|
|
2022-01-11 13:57:11 +00:00
|
|
|
|
2021-12-26 08:55:20 +00:00
|
|
|
hamlib = rig.radio()
|
2021-12-27 11:30:43 +00:00
|
|
|
hamlib.open_rig(devicename=devicename, deviceport=deviceport, hamlib_ptt_type=pttprotocol, serialspeed=serialspeed, pttport=pttport, data_bits=data_bits, stop_bits=stop_bits, handshake=handshake)
|
2021-11-28 11:17:51 +00:00
|
|
|
|
2021-12-26 08:55:20 +00:00
|
|
|
hamlib.set_ptt(True)
|
2021-12-26 09:43:55 +00:00
|
|
|
pttstate = hamlib.get_ptt()
|
2021-12-26 08:55:20 +00:00
|
|
|
if pttstate:
|
2021-11-28 11:17:51 +00:00
|
|
|
structlog.get_logger("structlog").info("[DMN] Hamlib PTT", status = 'SUCCESS')
|
2021-10-17 13:57:41 +00:00
|
|
|
data = {'COMMAND': 'TEST_HAMLIB', 'RESULT': 'SUCCESS'}
|
2021-12-26 08:55:20 +00:00
|
|
|
elif not pttstate:
|
2021-11-28 11:17:51 +00:00
|
|
|
structlog.get_logger("structlog").warning("[DMN] Hamlib PTT", status = 'NO SUCCESS')
|
2021-10-17 13:57:41 +00:00
|
|
|
data = {'COMMAND': 'TEST_HAMLIB', 'RESULT': 'NOSUCCESS'}
|
|
|
|
else:
|
2021-11-28 11:17:51 +00:00
|
|
|
structlog.get_logger("structlog").error("[DMN] Hamlib PTT", status = 'FAILED')
|
2021-10-17 13:57:41 +00:00
|
|
|
data = {'COMMAND': 'TEST_HAMLIB', 'RESULT': 'FAILED'}
|
2021-12-26 08:55:20 +00:00
|
|
|
|
|
|
|
hamlib.set_ptt(False)
|
|
|
|
hamlib.close_rig()
|
|
|
|
|
2021-10-17 13:57:41 +00:00
|
|
|
jsondata = json.dumps(data)
|
|
|
|
self.request.sendall(bytes(jsondata, encoding))
|
|
|
|
|
2021-11-28 11:17:51 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2021-11-26 18:14:11 +00:00
|
|
|
structlog.get_logger("structlog").error("[DMN] Hamlib: Can't open rig", e = sys.exc_info()[0])
|
2021-10-17 13:57:41 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
except Exception as e:
|
|
|
|
#socketTimeout = 0
|
|
|
|
structlog.get_logger("structlog").error("[DMN] Network error", e=e)
|
|
|
|
structlog.get_logger("structlog").warning("[DMN] Closing client socket", ip=self.client_address[0], port=self.client_address[1])
|
|
|
|
|
2021-06-13 15:21:37 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-09-13 18:01:39 +00:00
|
|
|
|
2021-06-13 15:21:37 +00:00
|
|
|
# --------------------------------------------GET PARAMETER INPUTS
|
|
|
|
PARSER = argparse.ArgumentParser(description='Simons TEST TNC')
|
2021-11-18 18:40:22 +00:00
|
|
|
PARSER.add_argument('--port', dest="socket_port",default=3001, help="Socket port", type=int)
|
2021-12-28 16:05:48 +00:00
|
|
|
PARSER.add_argument('--rigctl', dest="hamlib_use_rigctl",action="store_true", default=False, help="force using of rigctl")
|
|
|
|
|
2021-06-13 15:21:37 +00:00
|
|
|
ARGS = PARSER.parse_args()
|
|
|
|
PORT = ARGS.socket_port
|
2021-12-28 16:05:48 +00:00
|
|
|
HAMLIB_USE_RIGCTL = ARGS.hamlib_use_rigctl
|
2022-01-07 16:42:11 +00:00
|
|
|
|
|
|
|
# force use of rigctl when on windows
|
|
|
|
if sys.platform == 'win32' or sys.platform == 'win64':
|
|
|
|
HAMLIB_USE_RIGCTL = True
|
|
|
|
|
2021-12-28 16:05:48 +00:00
|
|
|
if HAMLIB_USE_RIGCTL:
|
2022-01-07 10:25:28 +00:00
|
|
|
structlog.get_logger("structlog").warning("using hamlib rigctl module...")
|
2022-01-07 15:58:45 +00:00
|
|
|
hamlib_version = 0
|
2021-12-28 16:05:48 +00:00
|
|
|
import rigctl as rig
|
|
|
|
else:
|
2022-01-07 10:25:28 +00:00
|
|
|
structlog.get_logger("structlog").info("using hamlib rig module...")
|
2021-12-28 16:05:48 +00:00
|
|
|
import rig
|
2021-06-13 15:21:37 +00:00
|
|
|
# --------------------------------------------START CMD SERVER
|
|
|
|
|
|
|
|
DAEMON_THREAD = threading.Thread(target=start_daemon, name="daemon")
|
|
|
|
DAEMON_THREAD.start()
|