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
|
|
|
|
2022-03-04 15:50:32 +00:00
|
|
|
daemon for providing basic information for the tnc like audio or serial devices
|
|
|
|
|
2021-06-13 15:21:37 +00:00
|
|
|
"""
|
2022-05-11 22:10:59 +00:00
|
|
|
# pylint: disable=invalid-name, line-too-long, c-extension-no-member
|
|
|
|
# pylint: disable=import-outside-toplevel
|
|
|
|
|
2021-06-13 15:21:37 +00:00
|
|
|
import argparse
|
2022-05-11 22:10:59 +00:00
|
|
|
import atexit
|
|
|
|
import multiprocessing
|
|
|
|
import os
|
|
|
|
import signal
|
2021-06-13 15:21:37 +00:00
|
|
|
import socketserver
|
2021-09-25 13:24:25 +00:00
|
|
|
import subprocess
|
2022-05-11 22:10:59 +00:00
|
|
|
import sys
|
|
|
|
import threading
|
|
|
|
import time
|
2022-01-20 19:38:56 +00:00
|
|
|
import audio
|
2022-05-26 01:23:30 +00:00
|
|
|
import crcengine
|
2022-05-11 22:10:59 +00:00
|
|
|
import log_handler
|
2022-05-26 01:23:30 +00:00
|
|
|
import serial.tools.list_ports
|
2022-01-22 19:39:37 +00:00
|
|
|
import sock
|
2022-05-11 22:10:59 +00:00
|
|
|
import static
|
2022-05-26 01:23:30 +00:00
|
|
|
import structlog
|
|
|
|
import ujson as json
|
2022-09-20 09:34:28 +00:00
|
|
|
import config
|
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
|
2022-02-16 08:11:32 +00:00
|
|
|
|
2022-09-05 09:54:50 +00:00
|
|
|
# signal handler for closing application
|
2022-02-16 08:11:32 +00:00
|
|
|
def signal_handler(sig, frame):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-11 22:10:59 +00:00
|
|
|
Signal handler for closing the network socket on app exit
|
2022-03-04 15:50:32 +00:00
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
sig:
|
|
|
|
frame:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Returns: system exit
|
|
|
|
"""
|
2022-05-26 01:23:30 +00:00
|
|
|
print("Closing daemon...")
|
2022-02-16 08:11:32 +00:00
|
|
|
sock.CLOSE_SIGNAL = True
|
|
|
|
sys.exit(0)
|
2022-04-11 09:03:54 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
2022-01-22 19:39:37 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
|
|
|
|
class DAEMON:
|
2022-05-09 00:41:49 +00:00
|
|
|
"""
|
2022-05-11 22:10:59 +00:00
|
|
|
Daemon class
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-30 17:47:51 +00:00
|
|
|
|
2022-05-28 02:17:15 +00:00
|
|
|
log = structlog.get_logger("DAEMON")
|
2022-05-26 01:23:30 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
def __init__(self):
|
2022-05-09 00:41:49 +00:00
|
|
|
# load crc engine
|
2022-05-26 01:23:30 +00:00
|
|
|
self.crc_algorithm = crcengine.new("crc16-ccitt-false") # load crc8 library
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
self.daemon_queue = sock.DAEMON_QUEUE
|
2022-05-26 01:23:30 +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-05-26 01:23:30 +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-05-09 00:41:49 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
def update_audio_devices(self):
|
2022-05-09 00:41:49 +00:00
|
|
|
"""
|
2022-05-11 22:10:59 +00:00
|
|
|
Update audio devices and set to static
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-28 02:17:15 +00:00
|
|
|
while True:
|
2022-02-15 17:10:14 +00:00
|
|
|
try:
|
2022-02-17 13:25:22 +00:00
|
|
|
if not static.TNCSTARTED:
|
2022-05-26 01:23:30 +00:00
|
|
|
(
|
|
|
|
static.AUDIO_INPUT_DEVICES,
|
|
|
|
static.AUDIO_OUTPUT_DEVICES,
|
|
|
|
) = audio.get_audio_devices()
|
|
|
|
except Exception as err1:
|
|
|
|
self.log.error(
|
|
|
|
"[DMN] update_audio_devices: Exception gathering audio devices:",
|
|
|
|
e=err1,
|
|
|
|
)
|
2022-02-19 20:30:52 +00:00
|
|
|
time.sleep(1)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
def update_serial_devices(self):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-11 22:10:59 +00:00
|
|
|
Update serial devices and set to static
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-28 02:17:15 +00:00
|
|
|
while True:
|
2022-02-15 17:10:14 +00:00
|
|
|
try:
|
|
|
|
serial_devices = []
|
|
|
|
ports = serial.tools.list_ports.comports()
|
|
|
|
for port, desc, hwid in ports:
|
|
|
|
# calculate hex of hwid if we have unique names
|
2022-05-26 01:23:30 +00:00
|
|
|
crc_hwid = self.crc_algorithm(bytes(hwid, encoding="utf-8"))
|
|
|
|
crc_hwid = crc_hwid.to_bytes(2, byteorder="big")
|
2022-02-15 17:10:14 +00:00
|
|
|
crc_hwid = crc_hwid.hex()
|
2022-05-11 22:10:59 +00:00
|
|
|
description = f"{desc} [{crc_hwid}]"
|
2022-05-26 01:23:30 +00:00
|
|
|
serial_devices.append(
|
|
|
|
{"port": str(port), "description": str(description)}
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
static.SERIAL_DEVICES = serial_devices
|
|
|
|
time.sleep(1)
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err1:
|
|
|
|
self.log.error(
|
|
|
|
"[DMN] update_serial_devices: Exception gathering serial devices:",
|
|
|
|
e=err1,
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
def worker(self):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-11 22:10:59 +00:00
|
|
|
Worker to handle the received commands
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-28 02:17:15 +00:00
|
|
|
while True:
|
2022-02-15 17:10:14 +00:00
|
|
|
try:
|
|
|
|
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
|
2022-05-28 12:08:33 +00:00
|
|
|
# data[18] low_bandwidth_mode
|
2022-03-19 11:42:10 +00:00
|
|
|
# data[19] tuning_range_fmin
|
|
|
|
# data[20] tuning_range_fmax
|
2022-04-18 15:17:53 +00:00
|
|
|
# data[21] enable FSK
|
|
|
|
# data[22] tx-audio-level
|
|
|
|
# data[23] respond_to_cq
|
2022-09-05 09:54:50 +00:00
|
|
|
# data[24] rx_buffer_size
|
|
|
|
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if data[0] == "STARTTNC":
|
|
|
|
self.log.warning("[DMN] Starting TNC", rig=data[5], port=data[6])
|
2022-02-15 17:10:14 +00:00
|
|
|
|
|
|
|
# list of parameters, necessary for running subprocess command as a list
|
|
|
|
options = []
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--port")
|
|
|
|
options.append(str(static.DAEMONPORT - 1))
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--mycall")
|
|
|
|
options.append(data[1])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--mygrid")
|
|
|
|
options.append(data[2])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--rx")
|
|
|
|
options.append(data[3])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--tx")
|
|
|
|
options.append(data[4])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-02-23 11:39:24 +00:00
|
|
|
# if radiocontrol != disabled
|
2022-05-09 00:41:49 +00:00
|
|
|
# this should hopefully avoid a ton of problems if we are just running in
|
2022-02-23 11:39:24 +00:00
|
|
|
# disabled mode
|
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if data[13] != "disabled":
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--devicename")
|
|
|
|
options.append(data[5])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--deviceport")
|
|
|
|
options.append(data[6])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--serialspeed")
|
|
|
|
options.append(data[7])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--pttprotocol")
|
|
|
|
options.append(data[8])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--pttport")
|
|
|
|
options.append(data[9])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--data_bits")
|
|
|
|
options.append(data[10])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--stop_bits")
|
|
|
|
options.append(data[11])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--handshake")
|
|
|
|
options.append(data[12])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--radiocontrol")
|
|
|
|
options.append(data[13])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if data[13] == "rigctld":
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--rigctld_ip")
|
|
|
|
options.append(data[14])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--rigctld_port")
|
|
|
|
options.append(data[15])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if data[16] == "True":
|
|
|
|
options.append("--scatter")
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if data[17] == "True":
|
|
|
|
options.append("--fft")
|
2022-04-11 09:10:32 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if data[18] == "True":
|
|
|
|
options.append("--500hz")
|
2022-04-11 09:10:32 +00:00
|
|
|
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--tuning_range_fmin")
|
|
|
|
options.append(data[19])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--tuning_range_fmax")
|
|
|
|
options.append(data[20])
|
2022-03-19 11:42:10 +00:00
|
|
|
|
2022-04-10 09:37:09 +00:00
|
|
|
# overriding FSK mode
|
2022-05-26 01:23:30 +00:00
|
|
|
# if data[21] == "True":
|
|
|
|
# options.append("--fsk")
|
2022-03-31 19:13:30 +00:00
|
|
|
|
2022-05-30 20:33:36 +00:00
|
|
|
options.append("--tx-audio-level")
|
|
|
|
options.append(data[22])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if data[23] == "True":
|
|
|
|
options.append("--qrv")
|
2022-04-18 15:17:53 +00:00
|
|
|
|
2022-09-05 09:54:50 +00:00
|
|
|
options.append("--rx-buffer-size")
|
|
|
|
options.append(data[24])
|
|
|
|
|
2022-09-20 09:34:28 +00:00
|
|
|
# safe data to config file
|
|
|
|
config.write_entire_config(data)
|
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
# Try running tnc from binary, else run from source
|
|
|
|
# This helps running the tnc in a developer environment
|
2022-02-15 17:10:14 +00:00
|
|
|
try:
|
|
|
|
command = []
|
2022-05-26 01:23:30 +00:00
|
|
|
if sys.platform in ["linux", "darwin"]:
|
|
|
|
command.append("./freedata-tnc")
|
|
|
|
elif sys.platform in ["win32", "win64"]:
|
|
|
|
command.append("freedata-tnc.exe")
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
command += options
|
2022-05-28 02:17:15 +00:00
|
|
|
proc = subprocess.Popen(command)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-28 02:17:15 +00:00
|
|
|
atexit.register(proc.kill)
|
2022-02-08 14:27:34 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
self.log.info("[DMN] TNC started", path="binary")
|
|
|
|
except FileNotFoundError as err1:
|
|
|
|
self.log.info("[DMN] worker: ", e=err1)
|
2022-02-15 17:10:14 +00:00
|
|
|
command = []
|
2022-05-26 01:23:30 +00:00
|
|
|
if sys.platform in ["linux", "darwin"]:
|
|
|
|
command.append("python3")
|
|
|
|
elif sys.platform in ["win32", "win64"]:
|
|
|
|
command.append("python")
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
command.append("main.py")
|
2022-02-15 17:10:14 +00:00
|
|
|
command += options
|
2022-05-28 02:17:15 +00:00
|
|
|
proc = subprocess.Popen(command)
|
|
|
|
atexit.register(proc.kill)
|
2022-02-16 08:11:32 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
self.log.info("[DMN] TNC started", path="source")
|
2022-01-22 19:39:37 +00:00
|
|
|
|
2022-05-28 02:17:15 +00:00
|
|
|
static.TNCPROCESS = proc
|
2022-02-15 17:10:14 +00:00
|
|
|
static.TNCSTARTED = True
|
2022-05-26 01:23:30 +00:00
|
|
|
"""
|
2022-02-15 17:10:14 +00:00
|
|
|
# WE HAVE THIS PART in SOCKET
|
2022-05-26 01:23:30 +00:00
|
|
|
if data[0] == "STOPTNC":
|
2022-02-15 17:10:14 +00:00
|
|
|
static.TNCPROCESS.kill()
|
2022-05-26 01:23:30 +00:00
|
|
|
self.log.warning("[DMN] Stopping TNC")
|
2022-02-15 17:10:14 +00:00
|
|
|
#os.kill(static.TNCPROCESS, signal.SIGKILL)
|
|
|
|
static.TNCSTARTED = False
|
2022-05-26 01:23:30 +00:00
|
|
|
"""
|
2022-02-15 17:10:14 +00:00
|
|
|
# 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
|
2022-05-26 01:23:30 +00:00
|
|
|
if data[0] == "TEST_HAMLIB":
|
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
|
2022-05-26 01:23:30 +00:00
|
|
|
if radiocontrol == "direct":
|
2022-02-15 17:10:14 +00:00
|
|
|
import rig
|
2022-05-26 01:23:30 +00:00
|
|
|
elif radiocontrol == "rigctl":
|
2022-02-15 17:10:14 +00:00
|
|
|
import rigctl as rig
|
2022-05-26 01:23:30 +00:00
|
|
|
elif radiocontrol == "rigctld":
|
2022-02-15 17:10:14 +00:00
|
|
|
import rigctld as rig
|
|
|
|
else:
|
|
|
|
import rigdummy as rig
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
hamlib = rig.radio()
|
2022-05-26 01:23:30 +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,
|
|
|
|
rigctld_ip=rigctld_ip,
|
|
|
|
rigctld_port=rigctld_port,
|
|
|
|
)
|
2022-01-18 18:38:05 +00:00
|
|
|
|
2022-05-23 07:37:24 +00:00
|
|
|
# hamlib_version = rig.hamlib_version
|
2021-10-17 13:57:41 +00:00
|
|
|
|
2022-05-09 00:41:49 +00:00
|
|
|
hamlib.set_ptt(True)
|
2022-02-15 17:10:14 +00:00
|
|
|
pttstate = hamlib.get_ptt()
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
if pttstate:
|
2022-05-26 01:23:30 +00:00
|
|
|
self.log.info("[DMN] Hamlib PTT", status="SUCCESS")
|
|
|
|
response = {"command": "test_hamlib", "result": "SUCCESS"}
|
2022-05-30 20:01:25 +00:00
|
|
|
else:
|
2022-05-26 01:23:30 +00:00
|
|
|
self.log.warning("[DMN] Hamlib PTT", status="NO SUCCESS")
|
|
|
|
response = {"command": "test_hamlib", "result": "NOSUCCESS"}
|
2022-05-09 00:41:49 +00:00
|
|
|
|
|
|
|
hamlib.set_ptt(False)
|
2022-02-15 17:10:14 +00:00
|
|
|
hamlib.close_rig()
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
jsondata = json.dumps(response)
|
|
|
|
sock.SOCKET_QUEUE.put(jsondata)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err1:
|
|
|
|
self.log.error("[DMN] worker: Exception: ", e=err1)
|
2021-06-13 15:21:37 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
mainlog = structlog.get_logger(__file__)
|
2022-05-23 07:37:24 +00:00
|
|
|
# we need to run this on Windows for multiprocessing support
|
2022-02-17 13:25:22 +00:00
|
|
|
multiprocessing.freeze_support()
|
2022-02-17 15:52:11 +00:00
|
|
|
|
2021-06-13 15:21:37 +00:00
|
|
|
# --------------------------------------------GET PARAMETER INPUTS
|
2022-05-26 01:23:30 +00:00
|
|
|
PARSER = argparse.ArgumentParser(description="FreeDATA Daemon")
|
|
|
|
PARSER.add_argument(
|
|
|
|
"--port",
|
|
|
|
dest="socket_port",
|
|
|
|
default=3001,
|
2022-05-28 15:52:05 +00:00
|
|
|
help="Socket port in the range of 1024-65535",
|
2022-05-26 01:23:30 +00:00
|
|
|
type=int,
|
|
|
|
)
|
2021-06-13 15:21:37 +00:00
|
|
|
ARGS = PARSER.parse_args()
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
static.DAEMONPORT = ARGS.socket_port
|
2022-03-04 15:50:32 +00:00
|
|
|
|
2022-02-21 16:58:44 +00:00
|
|
|
try:
|
2022-05-26 01:23:30 +00:00
|
|
|
if sys.platform == "linux":
|
|
|
|
logging_path = os.getenv("HOME") + "/.config/" + "FreeDATA/" + "daemon"
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if sys.platform == "darwin":
|
|
|
|
logging_path = (
|
|
|
|
os.getenv("HOME")
|
|
|
|
+ "/Library/"
|
|
|
|
+ "Application Support/"
|
|
|
|
+ "FreeDATA/"
|
|
|
|
+ "daemon"
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if sys.platform in ["win32", "win64"]:
|
|
|
|
logging_path = os.getenv("APPDATA") + "/" + "FreeDATA/" + "daemon"
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-02-21 16:58:44 +00:00
|
|
|
if not os.path.exists(logging_path):
|
|
|
|
os.makedirs(logging_path)
|
|
|
|
log_handler.setup_logging(logging_path)
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
|
|
|
mainlog.error("[DMN] logger init error", exception=err)
|
2022-01-22 19:39:37 +00:00
|
|
|
|
2022-09-20 09:34:28 +00:00
|
|
|
# init config
|
|
|
|
config = config.CONFIG()
|
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
try:
|
2022-05-26 01:23:30 +00:00
|
|
|
mainlog.info("[DMN] Starting TCP/IP socket", port=static.DAEMONPORT)
|
2022-01-22 19:39:37 +00:00
|
|
|
# https://stackoverflow.com/a/16641793
|
|
|
|
socketserver.TCPServer.allow_reuse_address = True
|
2022-05-26 01:23:30 +00:00
|
|
|
cmdserver = sock.ThreadedTCPServer(
|
|
|
|
(static.HOST, static.DAEMONPORT), sock.ThreadedTCPRequestHandler
|
|
|
|
)
|
2022-01-22 19:39:37 +00:00
|
|
|
server_thread = threading.Thread(target=cmdserver.serve_forever)
|
|
|
|
server_thread.daemon = True
|
|
|
|
server_thread.start()
|
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
|
|
|
mainlog.error(
|
|
|
|
"[DMN] Starting TCP/IP socket failed", port=static.DAEMONPORT, e=err
|
|
|
|
)
|
2022-05-11 22:10:59 +00:00
|
|
|
sys.exit(1)
|
2022-05-09 01:27:24 +00:00
|
|
|
daemon = DAEMON()
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
mainlog.info(
|
|
|
|
"[DMN] Starting FreeDATA Daemon",
|
|
|
|
author="DJ2LS",
|
|
|
|
year="2022",
|
|
|
|
version=static.VERSION,
|
|
|
|
)
|
2022-01-22 19:39:37 +00:00
|
|
|
while True:
|
2022-02-17 13:25:22 +00:00
|
|
|
time.sleep(1)
|