2022-12-05 07:47:07 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
2023-11-09 21:11:53 +00:00
|
|
|
deprecated_daemon.py
|
2022-12-05 07:47:07 +00:00
|
|
|
|
|
|
|
Author: DJ2LS, January 2022
|
|
|
|
|
2024-04-17 20:49:08 +00:00
|
|
|
daemon for providing basic information for the freedata-server like audio or serial devices
|
2022-12-05 07:47:07 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
# pylint: disable=invalid-name, line-too-long, c-extension-no-member
|
|
|
|
# pylint: disable=import-outside-toplevel
|
|
|
|
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import socket
|
|
|
|
import structlog
|
|
|
|
import queue
|
|
|
|
import json
|
2022-12-09 09:22:42 +00:00
|
|
|
import base64
|
2022-12-16 16:09:48 +00:00
|
|
|
import os
|
2022-12-05 07:47:07 +00:00
|
|
|
|
|
|
|
log = structlog.get_logger("CLIENT")
|
|
|
|
|
2022-12-09 09:22:42 +00:00
|
|
|
|
|
|
|
split_char = b"\x00;"
|
2022-12-05 07:47:07 +00:00
|
|
|
# --------------------------------------------GET PARAMETER INPUTS
|
|
|
|
parser = argparse.ArgumentParser(description='Simons TEST TNC')
|
|
|
|
parser.add_argument('--port', dest="socket_port", default=3000, help="Set the port, the socket is listening on.", type=int)
|
2022-12-09 21:48:29 +00:00
|
|
|
parser.add_argument('--host', dest="socket_host", default='localhost', help="Set the host, the socket is listening on.", type=str)
|
2022-12-05 07:47:07 +00:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2022-12-09 21:48:29 +00:00
|
|
|
ip, port = args.socket_host, args.socket_port
|
2022-12-05 07:47:07 +00:00
|
|
|
connected = True
|
|
|
|
data = bytes()
|
2022-12-15 16:14:34 +00:00
|
|
|
|
2022-12-26 09:25:50 +00:00
|
|
|
"""
|
|
|
|
Nachricht
|
|
|
|
{'command': 'rx_buffer', 'data-array': [{'uuid': '8dde227d-3a09-4f39-b34c-5f8281d719d1', 'timestamp': 1672043316, 'dxcallsign': 'DJ2LS-1', 'dxgrid': 'JN48cs', 'data': 'bQA7c2VuZF9tZXNzYWdlADsxMjMAO2VkY2NjZDAyLTUzMTQtNDc3Ni1hMjlkLTFmY2M1ZDI4OTM4ZAA7VGVzdAoAOwA7cGxhaW4vdGV4dAA7ADsxNjcyMDQzMzA5'}]}
|
|
|
|
"""
|
2022-12-15 16:14:34 +00:00
|
|
|
|
2022-12-16 16:09:48 +00:00
|
|
|
def decode_and_save_data(encoded_data):
|
|
|
|
decoded_data = base64.b64decode(encoded_data)
|
2022-12-15 16:14:34 +00:00
|
|
|
decoded_data = decoded_data.split(split_char)
|
|
|
|
|
|
|
|
if decoded_data[0] == b'm':
|
|
|
|
print(jsondata)
|
|
|
|
log.info(f"{jsondata.get('mycallsign')} <<< {jsondata.get('dxcallsign')}", uuid=decoded_data[3])
|
|
|
|
log.info(f"{jsondata.get('mycallsign')} <<< {jsondata.get('dxcallsign')}", message=decoded_data[4])
|
|
|
|
log.info(f"{jsondata.get('mycallsign')} <<< {jsondata.get('dxcallsign')}", filename=decoded_data[5])
|
|
|
|
log.info(f"{jsondata.get('mycallsign')} <<< {jsondata.get('dxcallsign')}", filetype=decoded_data[6])
|
|
|
|
log.info(f"{jsondata.get('mycallsign')} <<< {jsondata.get('dxcallsign')}", data=decoded_data[7])
|
|
|
|
|
|
|
|
try:
|
2022-12-16 16:09:48 +00:00
|
|
|
folderpath = "received"
|
|
|
|
if not os.path.exists(folderpath):
|
|
|
|
os.makedirs(folderpath)
|
2022-12-15 16:14:34 +00:00
|
|
|
filename = decoded_data[8].decode("utf-8") + "_" + decoded_data[5].decode("utf-8")
|
|
|
|
|
2022-12-16 16:09:48 +00:00
|
|
|
with open(f"{folderpath}/{filename}", "wb") as file:
|
|
|
|
file.write(decoded_data[7])
|
|
|
|
|
|
|
|
with open(f"{folderpath}/{decoded_data[8].decode('utf-8')}_msg.txt", "wb") as file:
|
|
|
|
file.write(decoded_data[4])
|
|
|
|
|
2022-12-15 16:14:34 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
else:
|
|
|
|
print(decoded_data)
|
|
|
|
|
|
|
|
|
2022-12-05 07:47:07 +00:00
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
|
|
|
sock.connect((ip, port))
|
2022-12-26 09:25:50 +00:00
|
|
|
print(sock)
|
2022-12-05 07:47:07 +00:00
|
|
|
while connected:
|
2022-12-24 23:29:57 +00:00
|
|
|
chunk = sock.recv(1024)
|
2022-12-05 07:47:07 +00:00
|
|
|
data += chunk
|
2022-12-24 23:29:57 +00:00
|
|
|
if data.startswith(b"{") and data.endswith(b"}\n"):
|
|
|
|
# split data by \n if we have multiple commands in socket buffer
|
|
|
|
data = data.split(b"\n")
|
|
|
|
# remove empty data
|
|
|
|
data.remove(b"")
|
2022-12-05 07:47:07 +00:00
|
|
|
|
2022-12-25 07:57:05 +00:00
|
|
|
# iterate through data list
|
2022-12-24 23:29:57 +00:00
|
|
|
for command in data:
|
2022-12-05 07:47:07 +00:00
|
|
|
|
2022-12-24 23:29:57 +00:00
|
|
|
jsondata = json.loads(command)
|
2022-12-11 10:13:55 +00:00
|
|
|
|
2022-12-24 23:29:57 +00:00
|
|
|
if jsondata.get('command') == "tnc_state":
|
2023-05-28 14:06:07 +00:00
|
|
|
#pass
|
|
|
|
print(jsondata.get("routing_table"))
|
2022-12-11 10:13:55 +00:00
|
|
|
|
2024-04-17 20:49:08 +00:00
|
|
|
if jsondata.get('freedata') == "freedata-server-message":
|
2022-12-24 23:29:57 +00:00
|
|
|
log.info(jsondata)
|
2022-12-15 16:14:34 +00:00
|
|
|
|
2022-12-24 23:29:57 +00:00
|
|
|
if jsondata.get('ping') == "acknowledge":
|
|
|
|
log.info(f"PING {jsondata.get('mycallsign')} >><< {jsondata.get('dxcallsign')}", snr=jsondata.get('snr'), dxsnr=jsondata.get('dxsnr'))
|
2022-12-15 16:14:34 +00:00
|
|
|
|
2022-12-24 23:29:57 +00:00
|
|
|
if jsondata.get('status') == 'receiving':
|
|
|
|
log.info(jsondata)
|
|
|
|
|
|
|
|
if jsondata.get('command') == 'rx_buffer':
|
|
|
|
for rxdata in jsondata["data-array"]:
|
|
|
|
log.info(f"rx buffer {rxdata.get('uuid')}")
|
|
|
|
decode_and_save_data(rxdata.get('data'))
|
|
|
|
|
|
|
|
if jsondata.get('status') == 'received' and jsondata.get('arq') == 'transmission':
|
|
|
|
decode_and_save_data(jsondata["data"])
|
|
|
|
|
2023-05-28 14:06:07 +00:00
|
|
|
|
|
|
|
|
2022-12-26 09:25:50 +00:00
|
|
|
# clear data buffer as soon as data has been read
|
|
|
|
data = bytes()
|
2022-12-15 16:14:34 +00:00
|
|
|
|
|
|
|
|