mirror of https://github.com/DJ2LS/FreeDATA.git
get ping dxcallsign from database
parent
f8c3423018
commit
d86d36a16e
|
@ -1,5 +1,7 @@
|
||||||
from command import TxCommand
|
from command import TxCommand
|
||||||
import api_validations
|
import api_validations
|
||||||
|
from message_system_db_manager import DatabaseManager
|
||||||
|
|
||||||
|
|
||||||
class PingCommand(TxCommand):
|
class PingCommand(TxCommand):
|
||||||
|
|
||||||
|
@ -8,6 +10,9 @@ class PingCommand(TxCommand):
|
||||||
if not api_validations.validate_freedata_callsign(self.dxcall):
|
if not api_validations.validate_freedata_callsign(self.dxcall):
|
||||||
self.dxcall = f"{self.dxcall}-0"
|
self.dxcall = f"{self.dxcall}-0"
|
||||||
|
|
||||||
|
# update callsign database...
|
||||||
|
DatabaseManager(self.event_manager).get_or_create_station(self.dxcall)
|
||||||
|
|
||||||
return super().set_params_from_api(apiParams)
|
return super().set_params_from_api(apiParams)
|
||||||
|
|
||||||
def build_frame(self):
|
def build_frame(self):
|
||||||
|
|
|
@ -5,6 +5,7 @@ from queue import Queue
|
||||||
import structlog
|
import structlog
|
||||||
import time, uuid
|
import time, uuid
|
||||||
from codec2 import FREEDV_MODE
|
from codec2 import FREEDV_MODE
|
||||||
|
from message_system_db_manager import DatabaseManager
|
||||||
|
|
||||||
TESTMODE = False
|
TESTMODE = False
|
||||||
|
|
||||||
|
@ -105,6 +106,7 @@ class FrameHandler():
|
||||||
)
|
)
|
||||||
|
|
||||||
def make_event(self):
|
def make_event(self):
|
||||||
|
|
||||||
event = {
|
event = {
|
||||||
"type": "frame-handler",
|
"type": "frame-handler",
|
||||||
"received": self.details['frame']['frame_type'],
|
"received": self.details['frame']['frame_type'],
|
||||||
|
@ -115,6 +117,10 @@ class FrameHandler():
|
||||||
}
|
}
|
||||||
if 'origin' in self.details['frame']:
|
if 'origin' in self.details['frame']:
|
||||||
event['dxcallsign'] = self.details['frame']['origin']
|
event['dxcallsign'] = self.details['frame']['origin']
|
||||||
|
|
||||||
|
if 'origin_crc' in self.details['frame']:
|
||||||
|
event['dxcallsign'] = DatabaseManager(self.event_manager).get_callsign_by_checksum(self.details['frame']['origin_crc'])
|
||||||
|
|
||||||
return event
|
return event
|
||||||
|
|
||||||
def emit_event(self):
|
def emit_event(self):
|
||||||
|
|
|
@ -8,6 +8,7 @@ from message_system_db_model import Base, Station, Status, Attachment, P2PMessag
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import json
|
import json
|
||||||
import structlog
|
import structlog
|
||||||
|
import helpers
|
||||||
|
|
||||||
class DatabaseManager:
|
class DatabaseManager:
|
||||||
def __init__(self, event_manger, uri='sqlite:///freedata-messages.db'):
|
def __init__(self, event_manger, uri='sqlite:///freedata-messages.db'):
|
||||||
|
@ -57,13 +58,49 @@ class DatabaseManager:
|
||||||
self.thread_local.session = scoped_session(self.session_factory)
|
self.thread_local.session = scoped_session(self.session_factory)
|
||||||
return self.thread_local.session
|
return self.thread_local.session
|
||||||
|
|
||||||
def get_or_create_station(self, session, callsign):
|
def get_or_create_station(self, callsign, session=None):
|
||||||
station = session.query(Station).filter_by(callsign=callsign).first()
|
own_session = False
|
||||||
if not station:
|
if not session:
|
||||||
station = Station(callsign=callsign)
|
session = self.get_thread_scoped_session()
|
||||||
session.add(station)
|
own_session = True
|
||||||
session.flush() # To get the callsign immediately
|
|
||||||
return station
|
try:
|
||||||
|
station = session.query(Station).filter_by(callsign=callsign).first()
|
||||||
|
if not station:
|
||||||
|
self.log(f"Updating station list with {callsign}")
|
||||||
|
station = Station(callsign=callsign, checksum=helpers.get_crc_24(callsign).hex())
|
||||||
|
session.add(station)
|
||||||
|
session.flush()
|
||||||
|
|
||||||
|
if own_session:
|
||||||
|
session.commit() # Only commit if we created the session
|
||||||
|
|
||||||
|
return station
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
|
||||||
|
if own_session:
|
||||||
|
session.rollback()
|
||||||
|
|
||||||
|
finally:
|
||||||
|
if own_session:
|
||||||
|
session.remove()
|
||||||
|
|
||||||
|
def get_callsign_by_checksum(self, checksum):
|
||||||
|
session = self.get_thread_scoped_session()
|
||||||
|
try:
|
||||||
|
station = session.query(Station).filter_by(checksum=checksum).first()
|
||||||
|
if station:
|
||||||
|
self.log(f"Found callsign [{station.callsign}] for checksum [{station.checksum}]")
|
||||||
|
return station.callsign
|
||||||
|
else:
|
||||||
|
self.log(f"No callsign found for [{checksum}]")
|
||||||
|
return None
|
||||||
|
except Exception as e:
|
||||||
|
self.log(f"Error fetching callsign for checksum {checksum}: {e}", isWarning=True)
|
||||||
|
return {'status': 'failure', 'message': str(e)}
|
||||||
|
finally:
|
||||||
|
session.remove()
|
||||||
|
|
||||||
def get_or_create_status(self, session, status_name):
|
def get_or_create_status(self, session, status_name):
|
||||||
status = session.query(Status).filter_by(name=status_name).first()
|
status = session.query(Status).filter_by(name=status_name).first()
|
||||||
|
@ -77,8 +114,8 @@ class DatabaseManager:
|
||||||
session = self.get_thread_scoped_session()
|
session = self.get_thread_scoped_session()
|
||||||
try:
|
try:
|
||||||
# Create and add the origin and destination Stations
|
# Create and add the origin and destination Stations
|
||||||
origin = self.get_or_create_station(session, message_data['origin'])
|
origin = self.get_or_create_station(message_data['origin'], session)
|
||||||
destination = self.get_or_create_station(session, message_data['destination'])
|
destination = self.get_or_create_station(message_data['destination'], session)
|
||||||
|
|
||||||
# Create and add Status if provided
|
# Create and add Status if provided
|
||||||
if status:
|
if status:
|
||||||
|
|
|
@ -8,9 +8,17 @@ Base = declarative_base()
|
||||||
class Station(Base):
|
class Station(Base):
|
||||||
__tablename__ = 'station'
|
__tablename__ = 'station'
|
||||||
callsign = Column(String, primary_key=True)
|
callsign = Column(String, primary_key=True)
|
||||||
|
checksum = Column(String, nullable=True)
|
||||||
location = Column(JSON, nullable=True)
|
location = Column(JSON, nullable=True)
|
||||||
info = Column(JSON, nullable=True)
|
info = Column(JSON, nullable=True)
|
||||||
|
def to_dict(self):
|
||||||
|
return {
|
||||||
|
'callsign': self.callsign,
|
||||||
|
'checksum': self.checksum,
|
||||||
|
'location': self.location,
|
||||||
|
'info': self.info,
|
||||||
|
|
||||||
|
}
|
||||||
class Status(Base):
|
class Status(Base):
|
||||||
__tablename__ = 'status'
|
__tablename__ = 'status'
|
||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
|
|
|
@ -51,7 +51,7 @@ class TestProtocols(unittest.TestCase):
|
||||||
|
|
||||||
def testPingWithAck(self):
|
def testPingWithAck(self):
|
||||||
# Run ping command
|
# Run ping command
|
||||||
api_params = { "dxcall": "XX1XXX-7"}
|
api_params = { "dxcall": "XX1XXX-6"}
|
||||||
ping_cmd = PingCommand(self.config, self.state_manager, self.event_manager, api_params)
|
ping_cmd = PingCommand(self.config, self.state_manager, self.event_manager, api_params)
|
||||||
#ping_cmd.run(self.event_queue, self.modem)
|
#ping_cmd.run(self.event_queue, self.modem)
|
||||||
frame = ping_cmd.test(self.event_queue)
|
frame = ping_cmd.test(self.event_queue)
|
||||||
|
|
Loading…
Reference in New Issue