more network functionality for daemon

This commit is contained in:
DJ2LS 2021-07-09 19:26:02 +02:00 committed by GitHub
parent 0ec882f2e5
commit c66b112e46
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 14 deletions

View file

@ -14,7 +14,13 @@ import socketserver
import pyaudio
import time
import json
PORT = 3001
import subprocess
import os
import static
#PORT = 3001
#TNCPROCESS = 0
#TNCSTARTED = False
#p = pyaudio.PyAudio()
#info = p.get_host_api_info_by_index(0)
@ -91,22 +97,39 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
# "dxcallsign" : "..."
# "data" : "..."
print(received_json)
#print(received_json)
#print(received_json["type"])
#print(received_json["command"])
try:
print(static.TNCSTARTED)
if received_json["type"] == 'SET' and received_json["command"] == 'TNC_STATUS':
if received_json["type"] == 'SET' and received_json["command"] == 'STARTTNC' and not static.TNCSTARTED:
parameter = received_json["parameter"]
print("STARTING TNC !!!!!")
#os.system("python3 main.py --rx 3 --tx 3 --deviceport /dev/ttyUSB0 --deviceid 2028")
p = subprocess.Popen("exec python3 main.py --rx 3 --tx 3 --deviceport /dev/ttyUSB0 --deviceid 2028", shell=True)
static.TNCPROCESS = p#.pid
print(static.TNCPROCESS)
static.TNCSTARTED = True
if received_json["type"] == 'SET' and received_json["command"] == 'STOPTNC':
parameter = received_json["parameter"]
static.TNCPROCESS.kill()
print("KILLING PROCESS ------------")
#os.kill(static.TNCPROCESS, signal.SIGKILL)
static.TNCSTARTED = False
if received_json["type"] == 'GET' and received_json["command"] == 'DAEMON_STATE':
data = {'COMMAND' : 'DAEMON_STATE', 'DAEMON_STATE' : [], 'INPUT_DEVICES': [], 'OUTPUT_DEVICES': []}
data["DAEMON_STATE"].append({"STATUS": "stopped"})
if static.TNCSTARTED:
data["DAEMON_STATE"].append({"STATUS": "running"})
else:
data["DAEMON_STATE"].append({"STATUS": "stopped"})
p = pyaudio.PyAudio()
for i in range(0, p.get_device_count()):
@ -120,15 +143,19 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
if maxOutputChannels > 0:
data["OUTPUT_DEVICES"].append({"ID": i, "NAME" : name})
print(data)
#print(data)
jsondata = json.dumps(data)
self.request.sendall(bytes(jsondata, encoding))
#exception, if JSON cant be decoded
except:
print("Wrong command")
except Exception as e:
print('PROGRAM ERROR: %s' %str(e))
print("Wrong command")
print("Client disconnected...")
@ -142,7 +169,6 @@ if __name__ == '__main__':
ARGS = PARSER.parse_args()
PORT = ARGS.socket_port
# --------------------------------------------START CMD SERVER
DAEMON_THREAD = threading.Thread(target=start_daemon, name="daemon")

16
sock.py
View file

@ -50,7 +50,7 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
try:
received_json = json.loads(data)
print(received_json)
#print(received_json)
except:
received_json = ''
@ -191,12 +191,22 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
"SNR": str(static.SNR),
"FREQUENCY" : str(static.HAMLIB_FREQUENCY),
"MODE" : str(static.HAMLIB_MODE),
"BANDWITH" : str(static.HAMLIB_BANDWITH)
"BANDWITH" : str(static.HAMLIB_BANDWITH),
"FFT" : str(static.FFT)
}
jsondata = json.dumps(output)
print(len(jsondata))
self.request.sendall(bytes(jsondata, encoding))
if received_json["type"] == 'GET' and received_json["command"] == 'FFT':
output = {
"FFT" : str(static.FFT)
}
jsondata = json.dumps(output)
self.request.sendall(bytes(jsondata, encoding))
if received_json["type"] == 'GET' and received_json["command"] == 'DATA_STATE':
output = {
"COMMAND": "DATA_STATE",

View file

@ -6,6 +6,15 @@ Created on Wed Dec 23 11:13:57 2020
@author: DJ2LS
"""
# DAEMON
DAEMONPORT = 3001
TNCSTARTED = False
TNCPROCESS = 0
# Operator Defaults
MYCALLSIGN = b'AA0AA'
MYCALLSIGN_CRC8 = b'A'
@ -83,6 +92,7 @@ MODEM_SAMPLE_RATE = 8000 # 8000
AUDIO_FRAMES_PER_BUFFER = 8192 # 256 # 512 # 1024 #2048 --> nicht 880 # 128 gut, 256, 1024 16384
AUDIO_CHANNELS = 1
AUDIO_RMS = 0
FFT = []
# ---------------------------------
# ARQ DEFAULTS