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
|
2022-01-20 19:38:56 +00:00
|
|
|
import queue
|
|
|
|
import audio
|
2022-01-22 19:39:37 +00:00
|
|
|
import sock
|
2022-02-08 14:27:34 +00:00
|
|
|
import atexit
|
2022-02-16 08:11:32 +00:00
|
|
|
import signal
|
2022-02-17 13:25:22 +00:00
|
|
|
import multiprocessing
|
2022-02-16 08:11:32 +00:00
|
|
|
|
|
|
|
# signal handler for closing aplication
|
|
|
|
def signal_handler(sig, frame):
|
|
|
|
print('Closing daemon...')
|
|
|
|
sock.CLOSE_SIGNAL = True
|
|
|
|
sys.exit(0)
|
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
|
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
|
|
|
|
class DAEMON():
|
|
|
|
def __init__(self):
|
2022-01-23 06:10:04 +00:00
|
|
|
|
|
|
|
# load crc engine
|
|
|
|
self.crc_algorithm = crcengine.new('crc16-ccitt-false') # load crc8 library
|
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
self.daemon_queue = sock.DAEMON_QUEUE
|
2022-02-16 08:11:32 +00:00
|
|
|
update_audio_devices = threading.Thread(target=self.update_audio_devices, name="UPDATE_AUDIO_DEVICES", daemon=True)
|
2022-01-22 19:39:37 +00:00
|
|
|
update_audio_devices.start()
|
|
|
|
|
2022-02-16 08:11:32 +00:00
|
|
|
update_serial_devices = threading.Thread(target=self.update_serial_devices, name="UPDATE_SERIAL_DEVICES", daemon=True)
|
2022-01-22 19:39:37 +00:00
|
|
|
update_serial_devices.start()
|
|
|
|
|
2022-02-16 08:11:32 +00:00
|
|
|
worker = threading.Thread(target=self.worker, name="WORKER", daemon=True)
|
2022-01-22 19:39:37 +00:00
|
|
|
worker.start()
|
|
|
|
|
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
def update_audio_devices(self):
|
|
|
|
while 1:
|
2022-02-15 17:10:14 +00:00
|
|
|
try:
|
2022-02-17 13:25:22 +00:00
|
|
|
if not static.TNCSTARTED:
|
2022-02-19 20:30:52 +00:00
|
|
|
|
2022-02-17 13:25:22 +00:00
|
|
|
static.AUDIO_INPUT_DEVICES, static.AUDIO_OUTPUT_DEVICES = audio.get_audio_devices()
|
2022-02-15 17:10:14 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2022-02-19 20:30:52 +00:00
|
|
|
time.sleep(1)
|
2022-02-17 15:52:11 +00:00
|
|
|
|
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
def update_serial_devices(self):
|
|
|
|
while 1:
|
2022-02-15 17:10:14 +00:00
|
|
|
try:
|
|
|
|
#print("update serial")
|
|
|
|
serial_devices = []
|
|
|
|
ports = serial.tools.list_ports.comports()
|
|
|
|
for port, desc, hwid in ports:
|
|
|
|
|
|
|
|
# calculate hex of hwid if we have unique names
|
|
|
|
crc_hwid = self.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 + ']'
|
|
|
|
serial_devices.append({"port": str(port), "description": str(description) })
|
|
|
|
|
|
|
|
static.SERIAL_DEVICES = serial_devices
|
|
|
|
time.sleep(1)
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
def worker(self):
|
|
|
|
while 1:
|
2022-02-15 17:10:14 +00:00
|
|
|
try:
|
2022-02-08 14:27:34 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
data = self.daemon_queue.get()
|
2022-01-22 19:39:37 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
# data[1] mycall
|
|
|
|
# data[2] mygrid
|
|
|
|
# data[3] rx_audio
|
|
|
|
# data[4] tx_audio
|
|
|
|
# data[5] devicename
|
|
|
|
# data[6] deviceport
|
|
|
|
# data[7] serialspeed
|
|
|
|
# data[8] pttprotocol
|
|
|
|
# data[9] pttport
|
|
|
|
# data[10] data_bits
|
|
|
|
# data[11] stop_bits
|
|
|
|
# data[12] handshake
|
|
|
|
# data[13] radiocontrol
|
|
|
|
# data[14] rigctld_ip
|
|
|
|
# data[15] rigctld_port
|
|
|
|
# data[16] send_scatter
|
|
|
|
# data[17] send_fft
|
|
|
|
# data[18] low_bandwith_mode
|
2022-01-22 19:39:37 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
if data[0] == 'STARTTNC':
|
|
|
|
structlog.get_logger("structlog").warning("[DMN] Starting TNC", rig=data[5], port=data[6])
|
|
|
|
|
|
|
|
# list of parameters, necessary for running subprocess command as a list
|
|
|
|
options = []
|
|
|
|
|
|
|
|
options.append('--port')
|
|
|
|
options.append(str(static.DAEMONPORT - 1))
|
|
|
|
|
|
|
|
options.append('--mycall')
|
|
|
|
options.append(data[1])
|
|
|
|
|
|
|
|
options.append('--mygrid')
|
|
|
|
options.append(data[2])
|
|
|
|
|
|
|
|
options.append('--rx')
|
|
|
|
options.append(data[3])
|
|
|
|
|
|
|
|
options.append('--tx')
|
|
|
|
options.append(data[4])
|
|
|
|
|
|
|
|
options.append('--devicename')
|
|
|
|
options.append(data[5])
|
|
|
|
|
|
|
|
options.append('--deviceport')
|
|
|
|
options.append(data[6])
|
|
|
|
|
|
|
|
options.append('--serialspeed')
|
|
|
|
options.append(data[7])
|
|
|
|
|
|
|
|
options.append('--pttprotocol')
|
|
|
|
options.append(data[8])
|
|
|
|
|
|
|
|
options.append('--pttport')
|
|
|
|
options.append(data[9])
|
|
|
|
|
|
|
|
options.append('--data_bits')
|
|
|
|
options.append(data[10])
|
|
|
|
|
|
|
|
options.append('--stop_bits')
|
|
|
|
options.append(data[11])
|
2022-02-02 20:12:16 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
options.append('--handshake')
|
|
|
|
options.append(data[12])
|
|
|
|
|
|
|
|
options.append('--radiocontrol')
|
|
|
|
options.append(data[13])
|
|
|
|
|
|
|
|
options.append('--rigctld_ip')
|
|
|
|
options.append(data[14])
|
|
|
|
|
|
|
|
options.append('--rigctld_port')
|
|
|
|
options.append(data[15])
|
|
|
|
|
|
|
|
if data[16] == 'True':
|
|
|
|
options.append('--scatter')
|
|
|
|
|
|
|
|
if data[17] == 'True':
|
|
|
|
options.append('--fft')
|
2022-02-08 14:27:34 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
if data[18] == 'True':
|
|
|
|
options.append('--500hz')
|
2022-02-08 14:27:34 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
# try running tnc from binary, else run from source
|
|
|
|
# this helps running the tnc in a developer environment
|
|
|
|
try:
|
|
|
|
command = []
|
|
|
|
if sys.platform == 'linux' or sys.platform == 'darwin':
|
|
|
|
command.append('./tnc')
|
|
|
|
elif sys.platform == 'win32' or sys.platform == 'win64':
|
|
|
|
command.append('tnc.exe')
|
|
|
|
|
|
|
|
command += options
|
|
|
|
p = subprocess.Popen(command)
|
2022-02-16 08:11:32 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
atexit.register(p.kill)
|
2022-02-08 14:27:34 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
structlog.get_logger("structlog").info("[DMN] TNC started", path="binary")
|
|
|
|
except:
|
|
|
|
command = []
|
|
|
|
if sys.platform == 'linux' or sys.platform == 'darwin':
|
|
|
|
command.append('python3')
|
|
|
|
elif sys.platform == 'win32' or sys.platform == 'win64':
|
|
|
|
command.append('python')
|
|
|
|
|
|
|
|
command.append('main.py')
|
|
|
|
command += options
|
|
|
|
p = subprocess.Popen(command)
|
2022-02-16 08:11:32 +00:00
|
|
|
atexit.register(p.kill)
|
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
structlog.get_logger("structlog").info("[DMN] TNC started", path="source")
|
2022-01-22 19:39:37 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
static.TNCPROCESS = p # .pid
|
|
|
|
static.TNCSTARTED = True
|
|
|
|
'''
|
|
|
|
# WE HAVE THIS PART in SOCKET
|
|
|
|
if data[0] == 'STOPTNC':
|
|
|
|
static.TNCPROCESS.kill()
|
|
|
|
structlog.get_logger("structlog").warning("[DMN] Stopping TNC")
|
|
|
|
#os.kill(static.TNCPROCESS, signal.SIGKILL)
|
|
|
|
static.TNCSTARTED = False
|
|
|
|
'''
|
|
|
|
# data[1] devicename
|
|
|
|
# data[2] deviceport
|
|
|
|
# data[3] serialspeed
|
|
|
|
# data[4] pttprotocol
|
|
|
|
# data[5] pttport
|
|
|
|
# data[6] data_bits
|
|
|
|
# data[7] stop_bits
|
|
|
|
# data[8] handshake
|
|
|
|
# data[9] radiocontrol
|
|
|
|
# data[10] rigctld_ip
|
|
|
|
# data[11] rigctld_port
|
|
|
|
if data[0] == 'TEST_HAMLIB':
|
2022-01-22 19:39:37 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
devicename = data[1]
|
|
|
|
deviceport = data[2]
|
|
|
|
serialspeed = data[3]
|
|
|
|
pttprotocol = data[4]
|
|
|
|
pttport = data[5]
|
|
|
|
data_bits = data[6]
|
|
|
|
stop_bits = data[7]
|
|
|
|
handshake = data[8]
|
|
|
|
radiocontrol = data[9]
|
|
|
|
rigctld_ip = data[10]
|
|
|
|
rigctld_port = data[11]
|
2022-01-22 19:39:37 +00:00
|
|
|
|
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
# check how we want to control the radio
|
|
|
|
if radiocontrol == 'direct':
|
|
|
|
import rig
|
|
|
|
elif radiocontrol == 'rigctl':
|
|
|
|
import rigctl as rig
|
|
|
|
elif radiocontrol == 'rigctld':
|
|
|
|
import rigctld as rig
|
|
|
|
else:
|
|
|
|
import rigdummy as rig
|
|
|
|
|
|
|
|
hamlib = rig.radio()
|
|
|
|
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, rigctld_ip=rigctld_ip, rigctld_port = rigctld_port)
|
2022-01-18 18:38:05 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
hamlib_version = rig.hamlib_version
|
2021-10-17 13:57:41 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
hamlib.set_ptt(True)
|
|
|
|
pttstate = hamlib.get_ptt()
|
2022-01-22 19:39:37 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
if pttstate:
|
|
|
|
structlog.get_logger("structlog").info("[DMN] Hamlib PTT", status = 'SUCCESS')
|
|
|
|
response = {'command': 'test_hamlib', 'result': 'SUCCESS'}
|
|
|
|
elif not pttstate:
|
|
|
|
structlog.get_logger("structlog").warning("[DMN] Hamlib PTT", status = 'NO SUCCESS')
|
|
|
|
response = {'command': 'test_hamlib', 'result': 'NOSUCCESS'}
|
|
|
|
else:
|
|
|
|
structlog.get_logger("structlog").error("[DMN] Hamlib PTT", status = 'FAILED')
|
|
|
|
response = {'command': 'test_hamlib', 'result': 'FAILED'}
|
|
|
|
|
|
|
|
hamlib.set_ptt(False)
|
|
|
|
hamlib.close_rig()
|
|
|
|
|
|
|
|
jsondata = json.dumps(response)
|
|
|
|
sock.SOCKET_QUEUE.put(jsondata)
|
2022-01-22 19:39:37 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2021-06-13 15:21:37 +00:00
|
|
|
|
2022-02-16 08:11:32 +00:00
|
|
|
|
|
|
|
|
2021-06-13 15:21:37 +00:00
|
|
|
if __name__ == '__main__':
|
2022-02-17 13:25:22 +00:00
|
|
|
# we need to run this on windows for multiprocessing support
|
|
|
|
multiprocessing.freeze_support()
|
2022-02-17 15:52:11 +00:00
|
|
|
|
2022-02-19 20:30:52 +00:00
|
|
|
|
2021-06-13 15:21:37 +00:00
|
|
|
# --------------------------------------------GET PARAMETER INPUTS
|
2022-02-19 20:30:52 +00:00
|
|
|
PARSER = argparse.ArgumentParser(description='FreeDATA Daemon')
|
|
|
|
PARSER.add_argument('--port', dest="socket_port",default=3001, help="Socket port", type=int)
|
2021-06-13 15:21:37 +00:00
|
|
|
ARGS = PARSER.parse_args()
|
2022-01-22 19:39:37 +00:00
|
|
|
static.DAEMONPORT = ARGS.socket_port
|
2022-02-21 16:58:44 +00:00
|
|
|
try:
|
|
|
|
if sys.platform == 'linux':
|
|
|
|
logging_path = os.getenv("HOME") + '/.config/' + 'FreeDATA/' + 'daemon'
|
|
|
|
|
|
|
|
if sys.platform == 'darwin':
|
|
|
|
logging_path = os.getenv("HOME") + '/Library/' + 'Application Support' + 'FreeDATA/' + 'daemon'
|
|
|
|
|
|
|
|
if sys.platform == 'win32' or sys.platform == 'win64':
|
|
|
|
logging_path = os.getenv('APPDATA') + '/' + 'FreeDATA/' + 'daemon'
|
|
|
|
|
|
|
|
if not os.path.exists(logging_path):
|
|
|
|
os.makedirs(logging_path)
|
|
|
|
log_handler.setup_logging(logging_path)
|
|
|
|
except:
|
|
|
|
structlog.get_logger("structlog").error("[TNC] logger init error")
|
2022-01-22 19:39:37 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
structlog.get_logger("structlog").info("[DMN] Starting TCP/IP socket", port=static.DAEMONPORT)
|
|
|
|
# https://stackoverflow.com/a/16641793
|
|
|
|
socketserver.TCPServer.allow_reuse_address = True
|
|
|
|
cmdserver = sock.ThreadedTCPServer((static.HOST, static.DAEMONPORT), sock.ThreadedTCPRequestHandler)
|
|
|
|
server_thread = threading.Thread(target=cmdserver.serve_forever)
|
|
|
|
server_thread.daemon = True
|
|
|
|
server_thread.start()
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
structlog.get_logger("structlog").error("[DMN] Starting TCP/IP socket failed", port=static.DAEMONPORT, e=e)
|
2022-02-19 20:30:52 +00:00
|
|
|
os._exit(1)
|
2022-01-22 19:39:37 +00:00
|
|
|
daemon = DAEMON()
|
2022-02-16 08:11:32 +00:00
|
|
|
|
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
while True:
|
2022-02-17 13:25:22 +00:00
|
|
|
time.sleep(1)
|