2024-04-17 19:47:40 +00:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
script_directory = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
sys.path.append(script_directory)
|
2024-03-11 19:07:15 +00:00
|
|
|
|
2024-04-17 19:47:40 +00:00
|
|
|
|
|
|
|
import time
|
2023-11-22 13:56:52 +00:00
|
|
|
from flask import Flask, request, jsonify, make_response, abort, Response
|
2023-11-06 12:49:37 +00:00
|
|
|
from flask_sock import Sock
|
2023-11-08 09:54:08 +00:00
|
|
|
from flask_cors import CORS
|
2023-11-06 20:44:36 +00:00
|
|
|
import serial_ports
|
2023-11-06 12:49:37 +00:00
|
|
|
from config import CONFIG
|
2023-11-06 20:44:36 +00:00
|
|
|
import audio
|
2023-11-08 17:49:38 +00:00
|
|
|
import queue
|
2023-11-09 21:11:53 +00:00
|
|
|
import service_manager
|
2023-11-11 19:01:15 +00:00
|
|
|
import state_manager
|
2024-02-28 19:01:26 +00:00
|
|
|
import json
|
2023-11-22 11:04:07 +00:00
|
|
|
import websocket_manager as wsm
|
2023-11-22 13:56:52 +00:00
|
|
|
import api_validations as validations
|
2023-11-25 23:11:39 +00:00
|
|
|
import command_cq
|
2024-03-07 14:33:42 +00:00
|
|
|
import command_beacon
|
2023-11-25 23:11:39 +00:00
|
|
|
import command_ping
|
|
|
|
import command_feq
|
|
|
|
import command_test
|
2023-12-03 13:11:43 +00:00
|
|
|
import command_arq_raw
|
2024-01-18 10:35:44 +00:00
|
|
|
import command_message_send
|
2024-01-04 14:46:58 +00:00
|
|
|
import event_manager
|
2024-03-11 19:07:15 +00:00
|
|
|
import atexit
|
|
|
|
|
2024-01-25 14:17:38 +00:00
|
|
|
from message_system_db_manager import DatabaseManager
|
2024-02-06 08:22:55 +00:00
|
|
|
from message_system_db_messages import DatabaseManagerMessages
|
|
|
|
from message_system_db_attachments import DatabaseManagerAttachments
|
2024-02-03 09:57:56 +00:00
|
|
|
from message_system_db_beacon import DatabaseManagerBeacon
|
2024-05-01 10:16:52 +00:00
|
|
|
from message_system_db_station import DatabaseManagerStations
|
2024-02-02 18:37:02 +00:00
|
|
|
from schedule_manager import ScheduleManager
|
2023-12-03 13:11:43 +00:00
|
|
|
|
2023-11-06 12:49:37 +00:00
|
|
|
app = Flask(__name__)
|
2023-11-08 09:54:08 +00:00
|
|
|
CORS(app, resources={r"/*": {"origins": "*"}})
|
2023-11-06 12:49:37 +00:00
|
|
|
sock = Sock(app)
|
2024-05-05 14:13:35 +00:00
|
|
|
MODEM_VERSION = "0.15.7-alpha"
|
2023-11-19 11:31:56 +00:00
|
|
|
|
2023-11-06 14:36:11 +00:00
|
|
|
# set config file to use
|
|
|
|
def set_config():
|
|
|
|
if 'FREEDATA_CONFIG' in os.environ:
|
|
|
|
config_file = os.environ['FREEDATA_CONFIG']
|
|
|
|
else:
|
2024-03-01 20:29:11 +00:00
|
|
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
config_file = os.path.join(script_dir, 'config.ini')
|
2023-11-06 12:49:37 +00:00
|
|
|
|
2023-11-06 14:36:11 +00:00
|
|
|
if os.path.exists(config_file):
|
2023-11-14 14:01:01 +00:00
|
|
|
print(f"Using config from {config_file}")
|
2023-11-06 14:36:11 +00:00
|
|
|
else:
|
2023-11-14 14:01:01 +00:00
|
|
|
print(f"Config file '{config_file}' not found. Exiting.")
|
2024-02-04 12:44:03 +00:00
|
|
|
sys.exit(1)
|
2024-01-14 20:05:53 +00:00
|
|
|
return config_file
|
2023-11-06 14:36:11 +00:00
|
|
|
|
2023-11-09 21:11:53 +00:00
|
|
|
|
2023-11-11 19:01:15 +00:00
|
|
|
|
2023-11-09 21:11:53 +00:00
|
|
|
|
|
|
|
# returns a standard API response
|
2023-11-22 13:56:52 +00:00
|
|
|
def api_response(data, status = 200):
|
|
|
|
return make_response(jsonify(data), status)
|
|
|
|
|
|
|
|
def api_abort(message, code):
|
|
|
|
jsonError = json.dumps({'error': message})
|
|
|
|
abort(Response(jsonError, code))
|
|
|
|
|
2023-11-26 11:45:51 +00:00
|
|
|
def api_ok(message = "ok"):
|
|
|
|
return api_response({'message': message})
|
|
|
|
|
2023-11-22 13:56:52 +00:00
|
|
|
# validates a parameter
|
|
|
|
def validate(req, param, validator, isRequired = True):
|
|
|
|
if param not in req:
|
|
|
|
if isRequired:
|
|
|
|
api_abort(f"Required parameter '{param}' is missing.", 400)
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
if not validator(req[param]):
|
|
|
|
api_abort(f"Value of '{param}' is invalid.", 400)
|
2023-11-07 10:36:49 +00:00
|
|
|
|
2023-11-23 15:59:53 +00:00
|
|
|
# Takes a transmit command and puts it in the transmit command queue
|
2024-05-09 20:52:25 +00:00
|
|
|
def enqueue_tx_command(cmd_class, params={}):
|
2024-02-21 16:05:28 +00:00
|
|
|
try:
|
|
|
|
command = cmd_class(app.config_manager.read(), app.state_manager, app.event_manager, params)
|
|
|
|
app.logger.info(f"Command {command.get_name()} running...")
|
|
|
|
if command.run(app.modem_events, app.service_manager.modem): # TODO remove the app.modem_event custom queue
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
|
|
app.logger.warning(f"Command {command.get_name()} failed...: {e}")
|
|
|
|
return False
|
2024-02-02 18:37:02 +00:00
|
|
|
|
2023-11-06 12:49:37 +00:00
|
|
|
## REST API
|
|
|
|
@app.route('/', methods=['GET'])
|
|
|
|
def index():
|
2023-11-06 14:36:11 +00:00
|
|
|
return api_response({'name': 'FreeDATA API',
|
2023-11-06 12:49:37 +00:00
|
|
|
'description': '',
|
|
|
|
'api_version': 1,
|
2024-01-14 20:05:53 +00:00
|
|
|
'modem_version': MODEM_VERSION,
|
2023-11-06 12:49:37 +00:00
|
|
|
'license': 'GPL3.0',
|
|
|
|
'documentation': 'https://wiki.freedata.app',
|
|
|
|
})
|
|
|
|
|
|
|
|
# get and set config
|
|
|
|
@app.route('/config', methods=['GET', 'POST'])
|
|
|
|
def config():
|
2023-11-11 12:37:18 +00:00
|
|
|
if request.method in ['POST']:
|
2024-02-25 20:08:45 +00:00
|
|
|
|
|
|
|
if not validations.validate_remote_config(request.json):
|
|
|
|
return api_abort("wrong config", 500)
|
2024-02-22 09:58:28 +00:00
|
|
|
# check if config already exists
|
|
|
|
if app.config_manager.read() == request.json:
|
|
|
|
return api_response(request.json)
|
|
|
|
|
2023-11-06 19:52:33 +00:00
|
|
|
set_config = app.config_manager.write(request.json)
|
2023-11-06 12:49:37 +00:00
|
|
|
if not set_config:
|
|
|
|
response = api_response(None, 'error writing config')
|
|
|
|
else:
|
2024-02-18 20:34:10 +00:00
|
|
|
app.modem_service.put("restart")
|
2023-11-06 12:49:37 +00:00
|
|
|
response = api_response(set_config)
|
|
|
|
return response
|
|
|
|
elif request.method == 'GET':
|
2023-11-06 14:36:11 +00:00
|
|
|
return api_response(app.config_manager.read())
|
2023-11-06 12:49:37 +00:00
|
|
|
|
2023-11-06 20:44:36 +00:00
|
|
|
@app.route('/devices/audio', methods=['GET'])
|
|
|
|
def get_audio_devices():
|
|
|
|
dev_in, dev_out = audio.get_audio_devices()
|
|
|
|
devices = { 'in': dev_in, 'out': dev_out }
|
|
|
|
return api_response(devices)
|
|
|
|
|
|
|
|
@app.route('/devices/serial', methods=['GET'])
|
|
|
|
def get_serial_devices():
|
|
|
|
devices = serial_ports.get_ports()
|
|
|
|
return api_response(devices)
|
|
|
|
|
2024-04-18 21:03:11 +00:00
|
|
|
@app.route('/modem/state', methods=['GET'])
|
2023-11-17 21:35:52 +00:00
|
|
|
def get_modem_state():
|
2023-11-22 17:05:31 +00:00
|
|
|
return api_response(app.state_manager.sendState())
|
2023-11-17 21:35:52 +00:00
|
|
|
|
2024-04-18 21:03:11 +00:00
|
|
|
@app.route('/modem/cqcqcq', methods=['POST', 'GET'])
|
2023-11-09 09:37:45 +00:00
|
|
|
def post_cqcqcq():
|
2023-11-11 12:37:18 +00:00
|
|
|
if request.method not in ['POST']:
|
2023-11-09 09:37:45 +00:00
|
|
|
return api_response({"info": "endpoint for triggering a CQ via POST"})
|
2023-11-25 23:11:39 +00:00
|
|
|
if not app.state_manager.is_modem_running:
|
|
|
|
api_abort('Modem not running', 503)
|
|
|
|
enqueue_tx_command(command_cq.CQCommand)
|
2023-11-26 11:45:51 +00:00
|
|
|
return api_ok()
|
2023-11-09 09:37:45 +00:00
|
|
|
|
2024-04-18 21:03:11 +00:00
|
|
|
@app.route('/modem/beacon', methods=['POST'])
|
2023-11-09 14:48:10 +00:00
|
|
|
def post_beacon():
|
2023-11-11 12:37:18 +00:00
|
|
|
if request.method not in ['POST']:
|
2023-11-09 14:48:10 +00:00
|
|
|
return api_response({"info": "endpoint for controlling BEACON STATE via POST"})
|
2024-04-13 10:57:33 +00:00
|
|
|
if not isinstance(request.json['enabled'], bool) or not isinstance(request.json['away_from_key'], bool):
|
2023-11-22 17:05:31 +00:00
|
|
|
api_abort(f"Incorrect value for 'enabled'. Shoud be bool.")
|
|
|
|
if not app.state_manager.is_modem_running:
|
2023-11-22 13:56:52 +00:00
|
|
|
api_abort('Modem not running', 503)
|
2023-12-21 16:47:48 +00:00
|
|
|
|
|
|
|
if not app.state_manager.is_beacon_running:
|
|
|
|
app.state_manager.set('is_beacon_running', request.json['enabled'])
|
2024-04-13 10:57:33 +00:00
|
|
|
app.state_manager.set('is_away_from_key', request.json['away_from_key'])
|
2024-04-13 08:56:11 +00:00
|
|
|
|
2024-03-07 14:33:42 +00:00
|
|
|
if not app.state_manager.getARQ():
|
|
|
|
enqueue_tx_command(command_beacon.BeaconCommand, request.json)
|
2023-12-21 16:47:48 +00:00
|
|
|
else:
|
|
|
|
app.state_manager.set('is_beacon_running', request.json['enabled'])
|
2024-05-02 06:49:17 +00:00
|
|
|
app.state_manager.set('is_away_from_key', request.json['away_from_key'])
|
2023-12-21 16:47:48 +00:00
|
|
|
|
2024-05-04 19:10:19 +00:00
|
|
|
return api_ok()
|
2023-11-09 14:48:10 +00:00
|
|
|
|
2024-04-18 21:03:11 +00:00
|
|
|
@app.route('/modem/ping_ping', methods=['POST'])
|
2023-11-09 14:48:10 +00:00
|
|
|
def post_ping():
|
2023-11-11 12:37:18 +00:00
|
|
|
if request.method not in ['POST']:
|
2023-11-09 14:48:10 +00:00
|
|
|
return api_response({"info": "endpoint for controlling PING via POST"})
|
2023-11-22 17:05:31 +00:00
|
|
|
if not app.state_manager.is_modem_running:
|
2023-11-22 13:56:52 +00:00
|
|
|
api_abort('Modem not running', 503)
|
|
|
|
validate(request.json, 'dxcall', validations.validate_freedata_callsign)
|
2023-11-25 23:11:39 +00:00
|
|
|
enqueue_tx_command(command_ping.PingCommand, request.json)
|
2023-11-26 11:45:51 +00:00
|
|
|
return api_ok()
|
2023-11-11 12:37:18 +00:00
|
|
|
|
2024-04-18 21:03:11 +00:00
|
|
|
@app.route('/modem/send_test_frame', methods=['POST'])
|
2023-11-11 12:37:18 +00:00
|
|
|
def post_send_test_frame():
|
|
|
|
if request.method not in ['POST']:
|
|
|
|
return api_response({"info": "endpoint for triggering a TEST_FRAME via POST"})
|
2023-11-25 23:11:39 +00:00
|
|
|
if not app.state_manager.is_modem_running:
|
|
|
|
api_abort('Modem not running', 503)
|
|
|
|
enqueue_tx_command(command_test.TestCommand)
|
2023-11-26 11:45:51 +00:00
|
|
|
return api_ok()
|
2023-11-11 12:37:18 +00:00
|
|
|
|
2024-04-18 21:03:11 +00:00
|
|
|
@app.route('/modem/fec_transmit', methods=['POST'])
|
2023-11-11 12:37:18 +00:00
|
|
|
def post_send_fec_frame():
|
|
|
|
if request.method not in ['POST']:
|
|
|
|
return api_response({"info": "endpoint for triggering a FEC frame via POST"})
|
2023-11-25 23:11:39 +00:00
|
|
|
if not app.state_manager.is_modem_running:
|
|
|
|
api_abort('Modem not running', 503)
|
|
|
|
enqueue_tx_command(command_feq.FecCommand, request.json)
|
2023-11-26 11:45:51 +00:00
|
|
|
return api_ok()
|
2023-11-11 12:37:18 +00:00
|
|
|
|
2024-04-18 21:03:11 +00:00
|
|
|
@app.route('/modem/fec_is_writing', methods=['POST'])
|
2023-11-11 19:01:15 +00:00
|
|
|
def post_send_fec_is_writing():
|
2023-11-11 12:37:18 +00:00
|
|
|
if request.method not in ['POST']:
|
|
|
|
return api_response({"info": "endpoint for triggering a IS WRITING frame via POST"})
|
2023-11-25 23:11:39 +00:00
|
|
|
if not app.state_manager.is_modem_running:
|
|
|
|
api_abort('Modem not running', 503)
|
|
|
|
#server_commands.modem_fec_is_writing(request.json)
|
|
|
|
return 'Not implemented yet'
|
2023-11-09 14:48:10 +00:00
|
|
|
|
2024-04-18 21:03:11 +00:00
|
|
|
@app.route('/modem/start', methods=['POST'])
|
2023-11-09 21:11:53 +00:00
|
|
|
def post_modem_start():
|
2023-11-11 12:37:18 +00:00
|
|
|
if request.method not in ['POST']:
|
2024-04-18 09:04:25 +00:00
|
|
|
return api_response({"info": "endpoint for STARTING freedata_server via POST"})
|
2023-11-11 12:37:18 +00:00
|
|
|
print("start received...")
|
|
|
|
app.modem_service.put("start")
|
|
|
|
return api_response(request.json)
|
2023-11-09 21:11:53 +00:00
|
|
|
|
2024-04-18 21:03:11 +00:00
|
|
|
@app.route('/modem/stop', methods=['POST'])
|
2023-11-09 21:11:53 +00:00
|
|
|
def post_modem_stop():
|
2023-11-11 12:37:18 +00:00
|
|
|
if request.method not in ['POST']:
|
2024-04-18 09:04:25 +00:00
|
|
|
return api_response({"info": "endpoint for STOPPING freedata_server via POST"})
|
2023-11-11 12:37:18 +00:00
|
|
|
print("stop received...")
|
|
|
|
|
|
|
|
app.modem_service.put("stop")
|
2023-11-26 11:45:51 +00:00
|
|
|
return api_ok()
|
2023-11-11 12:37:18 +00:00
|
|
|
|
2023-11-13 17:50:46 +00:00
|
|
|
@app.route('/version', methods=['GET'])
|
|
|
|
def get_modem_version():
|
2023-11-23 06:01:29 +00:00
|
|
|
return api_response({"version": app.MODEM_VERSION})
|
2023-11-13 17:50:46 +00:00
|
|
|
|
2024-04-18 21:03:11 +00:00
|
|
|
@app.route('/modem/send_arq_raw', methods=['POST'])
|
2023-11-19 13:03:48 +00:00
|
|
|
def post_modem_send_raw():
|
|
|
|
if request.method not in ['POST']:
|
|
|
|
return api_response({"info": "endpoint for SENDING RAW DATA via POST"})
|
2023-11-25 23:11:39 +00:00
|
|
|
if not app.state_manager.is_modem_running:
|
|
|
|
api_abort('Modem not running', 503)
|
2024-01-15 10:16:33 +00:00
|
|
|
if app.state_manager.check_if_running_arq_session():
|
|
|
|
api_abort('Modem busy', 503)
|
2024-01-04 20:44:59 +00:00
|
|
|
if enqueue_tx_command(command_arq_raw.ARQRawCommand, request.json):
|
|
|
|
return api_response(request.json)
|
|
|
|
else:
|
|
|
|
api_abort('Error executing command...', 500)
|
2024-04-18 21:03:11 +00:00
|
|
|
@app.route('/modem/stop_transmission', methods=['POST'])
|
2023-12-21 14:05:22 +00:00
|
|
|
def post_modem_send_raw_stop():
|
|
|
|
|
|
|
|
if request.method not in ['POST']:
|
|
|
|
return api_response({"info": "endpoint for SENDING a STOP command via POST"})
|
|
|
|
if not app.state_manager.is_modem_running:
|
|
|
|
api_abort('Modem not running', 503)
|
2023-12-28 21:27:49 +00:00
|
|
|
|
2024-02-18 14:06:13 +00:00
|
|
|
if app.state_manager.getARQ():
|
|
|
|
for id in app.state_manager.arq_irs_sessions:
|
|
|
|
app.state_manager.arq_irs_sessions[id].abort_transmission()
|
|
|
|
for id in app.state_manager.arq_iss_sessions:
|
|
|
|
app.state_manager.arq_iss_sessions[id].abort_transmission()
|
2023-12-28 21:27:49 +00:00
|
|
|
|
2023-12-21 14:05:22 +00:00
|
|
|
return api_response(request.json)
|
|
|
|
|
2024-01-12 15:29:22 +00:00
|
|
|
@app.route('/radio', methods=['GET', 'POST'])
|
|
|
|
def get_post_radio():
|
|
|
|
if request.method in ['POST']:
|
2024-02-20 08:13:34 +00:00
|
|
|
if "radio_frequency" in request.json:
|
2024-02-20 07:09:05 +00:00
|
|
|
app.radio_manager.set_frequency(request.json['radio_frequency'])
|
2024-02-20 08:13:34 +00:00
|
|
|
if "radio_mode" in request.json:
|
2024-02-20 07:09:05 +00:00
|
|
|
app.radio_manager.set_mode(request.json['radio_mode'])
|
2024-02-20 08:13:34 +00:00
|
|
|
if "radio_rf_level" in request.json:
|
2024-02-20 07:09:05 +00:00
|
|
|
app.radio_manager.set_rf_level(int(request.json['radio_rf_level']))
|
2024-04-26 09:54:24 +00:00
|
|
|
if "radio_tuner" in request.json:
|
2024-04-30 08:49:03 +00:00
|
|
|
app.radio_manager.set_tuner(request.json['radio_tuner'])
|
2024-01-12 15:29:22 +00:00
|
|
|
return api_response(request.json)
|
|
|
|
elif request.method == 'GET':
|
|
|
|
return api_response(app.state_manager.get_radio_status())
|
2023-12-21 14:05:22 +00:00
|
|
|
|
2024-01-25 14:17:38 +00:00
|
|
|
@app.route('/freedata/messages', methods=['POST', 'GET'])
|
2024-01-25 14:48:00 +00:00
|
|
|
def get_post_freedata_message():
|
2024-01-25 14:17:38 +00:00
|
|
|
if request.method in ['GET']:
|
2024-02-06 08:25:03 +00:00
|
|
|
result = DatabaseManagerMessages(app.event_manager).get_all_messages_json()
|
2024-02-21 16:05:28 +00:00
|
|
|
return api_response(result)
|
2024-05-02 09:55:34 +00:00
|
|
|
elif request.method in ['POST']:
|
2024-02-21 16:05:28 +00:00
|
|
|
enqueue_tx_command(command_message_send.SendMessageCommand, request.json)
|
|
|
|
return api_response(request.json)
|
2024-05-02 09:55:34 +00:00
|
|
|
else:
|
|
|
|
api_abort('Error executing command...', 500)
|
2024-01-18 10:35:44 +00:00
|
|
|
|
2024-02-03 14:01:01 +00:00
|
|
|
@app.route('/freedata/messages/<string:message_id>', methods=['GET', 'POST', 'PATCH', 'DELETE'])
|
2024-01-28 11:08:55 +00:00
|
|
|
def handle_freedata_message(message_id):
|
|
|
|
if request.method == 'GET':
|
2024-02-06 08:22:55 +00:00
|
|
|
message = DatabaseManagerMessages(app.event_manager).get_message_by_id_json(message_id)
|
2024-01-28 11:08:55 +00:00
|
|
|
return message
|
2024-02-03 13:16:23 +00:00
|
|
|
elif request.method == 'POST':
|
2024-02-06 08:22:55 +00:00
|
|
|
result = DatabaseManagerMessages(app.event_manager).update_message(message_id, update_data={'status': 'queued'})
|
|
|
|
DatabaseManagerMessages(app.event_manager).increment_message_attempts(message_id)
|
2024-02-03 14:01:01 +00:00
|
|
|
return api_response(result)
|
|
|
|
elif request.method == 'PATCH':
|
|
|
|
# Fixme We need to adjust this
|
2024-02-06 08:22:55 +00:00
|
|
|
result = DatabaseManagerMessages(app.event_manager).mark_message_as_read(message_id)
|
2024-02-03 13:16:23 +00:00
|
|
|
return api_response(result)
|
2024-01-28 11:08:55 +00:00
|
|
|
elif request.method == 'DELETE':
|
2024-02-06 08:22:55 +00:00
|
|
|
result = DatabaseManagerMessages(app.event_manager).delete_message(message_id)
|
2024-01-28 11:08:55 +00:00
|
|
|
return api_response(result)
|
|
|
|
else:
|
|
|
|
api_abort('Error executing command...', 500)
|
|
|
|
|
|
|
|
@app.route('/freedata/messages/<string:message_id>/attachments', methods=['GET'])
|
|
|
|
def get_message_attachments(message_id):
|
2024-02-06 08:22:55 +00:00
|
|
|
attachments = DatabaseManagerAttachments(app.event_manager).get_attachments_by_message_id_json(message_id)
|
2024-01-28 11:08:55 +00:00
|
|
|
return api_response(attachments)
|
2024-02-02 18:37:02 +00:00
|
|
|
|
2024-02-06 19:09:20 +00:00
|
|
|
@app.route('/freedata/messages/attachment/<string:data_sha512>', methods=['GET'])
|
|
|
|
def get_message_attachment(data_sha512):
|
|
|
|
attachment = DatabaseManagerAttachments(app.event_manager).get_attachment_by_sha512(data_sha512)
|
|
|
|
return api_response(attachment)
|
|
|
|
|
2024-02-03 09:57:56 +00:00
|
|
|
@app.route('/freedata/beacons', methods=['GET'])
|
|
|
|
def get_all_beacons():
|
|
|
|
beacons = DatabaseManagerBeacon(app.event_manager).get_all_beacons()
|
|
|
|
return api_response(beacons)
|
|
|
|
|
|
|
|
@app.route('/freedata/beacons/<string:callsign>', methods=['GET'])
|
|
|
|
def get_beacons_by_callsign(callsign):
|
|
|
|
beacons = DatabaseManagerBeacon(app.event_manager).get_beacons_by_callsign(callsign)
|
|
|
|
return api_response(beacons)
|
2023-11-08 18:21:36 +00:00
|
|
|
|
2024-05-02 09:55:34 +00:00
|
|
|
@app.route('/freedata/station/<string:callsign>', methods=['GET', 'POST'])
|
|
|
|
def get_set_station_info_by_callsign(callsign):
|
|
|
|
if request.method in ['GET']:
|
|
|
|
station = DatabaseManagerStations(app.event_manager).get_station(callsign)
|
|
|
|
return api_response(station)
|
2024-05-09 20:52:25 +00:00
|
|
|
elif request.method in ['POST'] and "info" in request.json:
|
|
|
|
result = DatabaseManagerStations(app.event_manager).update_station_info(callsign, new_info=request.json["info"])
|
2024-05-02 09:55:34 +00:00
|
|
|
return api_response(result)
|
|
|
|
else:
|
|
|
|
api_abort('Error using endpoint...', 500)
|
2024-05-01 10:16:52 +00:00
|
|
|
|
2023-11-06 12:49:37 +00:00
|
|
|
# Event websocket
|
|
|
|
@sock.route('/events')
|
2023-11-09 16:14:22 +00:00
|
|
|
def sock_events(sock):
|
2024-01-04 20:13:32 +00:00
|
|
|
wsm.handle_connection(sock, wsm.events_client_list, app.modem_events) # TODO remove the app.modem_event custom queue
|
2023-11-09 16:14:22 +00:00
|
|
|
|
|
|
|
@sock.route('/fft')
|
|
|
|
def sock_fft(sock):
|
2023-11-22 11:04:07 +00:00
|
|
|
wsm.handle_connection(sock, wsm.fft_client_list, app.modem_fft)
|
2023-11-11 12:37:18 +00:00
|
|
|
|
2023-11-11 19:46:49 +00:00
|
|
|
@sock.route('/states')
|
|
|
|
def sock_states(sock):
|
2023-11-22 11:04:07 +00:00
|
|
|
wsm.handle_connection(sock, wsm.states_client_list, app.state_queue)
|
2023-11-23 06:01:29 +00:00
|
|
|
|
2024-03-11 19:07:15 +00:00
|
|
|
@atexit.register
|
|
|
|
def stop_server():
|
2024-04-10 15:45:48 +00:00
|
|
|
print("------------------------------------------")
|
2024-04-18 13:01:16 +00:00
|
|
|
# TODO This is causing problems for some reasons.
|
|
|
|
#if "service_manager" :
|
|
|
|
# app.service_manager.modem_service.put("stop")
|
|
|
|
# if app.socket_interface_manager:
|
|
|
|
# app.socket_interface_manager.stop_servers()
|
|
|
|
|
|
|
|
# if app.service_manager.modem:
|
|
|
|
# app.service_manager.modem.sd_input_stream.stop
|
|
|
|
#time.sleep(1)
|
2024-01-13 10:28:45 +00:00
|
|
|
|
2024-04-17 19:47:40 +00:00
|
|
|
def main():
|
2024-01-14 20:05:53 +00:00
|
|
|
app.config['SOCK_SERVER_OPTIONS'] = {'ping_interval': 10}
|
|
|
|
# define global MODEM_VERSION
|
|
|
|
app.MODEM_VERSION = MODEM_VERSION
|
|
|
|
|
|
|
|
config_file = set_config()
|
|
|
|
app.config_manager = CONFIG(config_file)
|
|
|
|
|
2024-04-18 09:04:25 +00:00
|
|
|
# start freedata_server
|
2024-03-09 09:47:27 +00:00
|
|
|
app.p2p_data_queue = queue.Queue() # queue which holds processing data of p2p connections
|
2024-01-14 20:05:53 +00:00
|
|
|
app.state_queue = queue.Queue() # queue which holds latest states
|
|
|
|
app.modem_events = queue.Queue() # queue which holds latest events
|
|
|
|
app.modem_fft = queue.Queue() # queue which holds latest fft data
|
2024-04-18 09:04:25 +00:00
|
|
|
app.modem_service = queue.Queue() # start / stop freedata_server service
|
2024-04-18 13:01:16 +00:00
|
|
|
|
2024-01-14 20:05:53 +00:00
|
|
|
app.event_manager = event_manager.EventManager([app.modem_events]) # TODO remove the app.modem_event custom queue
|
|
|
|
# init state manager
|
|
|
|
app.state_manager = state_manager.StateManager(app.state_queue)
|
2024-02-02 18:37:02 +00:00
|
|
|
# initialize message system schedule manager
|
|
|
|
app.schedule_manager = ScheduleManager(app.MODEM_VERSION, app.config_manager, app.state_manager, app.event_manager)
|
2024-01-14 20:05:53 +00:00
|
|
|
# start service manager
|
|
|
|
app.service_manager = service_manager.SM(app)
|
2024-03-16 12:44:58 +00:00
|
|
|
|
2024-04-18 09:04:25 +00:00
|
|
|
# start freedata_server service
|
2024-01-14 20:05:53 +00:00
|
|
|
app.modem_service.put("start")
|
2024-02-02 18:37:02 +00:00
|
|
|
# initialize database default values
|
2024-04-18 13:01:16 +00:00
|
|
|
|
2024-01-27 11:07:07 +00:00
|
|
|
DatabaseManager(app.event_manager).initialize_default_values()
|
2024-01-14 20:05:53 +00:00
|
|
|
wsm.startThreads(app)
|
2024-03-04 14:50:08 +00:00
|
|
|
|
|
|
|
conf = app.config_manager.read()
|
|
|
|
modemaddress = conf['NETWORK']['modemaddress']
|
|
|
|
modemport = conf['NETWORK']['modemport']
|
|
|
|
|
|
|
|
if not modemaddress:
|
|
|
|
modemaddress = '127.0.0.1'
|
|
|
|
if not modemport:
|
|
|
|
modemport = 5000
|
2024-03-15 08:45:51 +00:00
|
|
|
|
2024-04-21 12:18:45 +00:00
|
|
|
app.run(modemaddress, modemport, debug=False)
|
2024-03-16 12:44:58 +00:00
|
|
|
|
2024-04-17 19:47:40 +00:00
|
|
|
if __name__ == "__main__":
|
2024-04-18 13:01:16 +00:00
|
|
|
main()
|