diff --git a/modem/command_message_send.py b/modem/command_message_send.py index 0aeb226c..3507d54d 100644 --- a/modem/command_message_send.py +++ b/modem/command_message_send.py @@ -29,7 +29,6 @@ class SendMessageCommand(TxCommand): first_queued_message = DatabaseManager(self.event_manager).get_first_queued_message() if not first_queued_message: self.log("No queued message in database.") - self.state_manager.set_state("pending_messages", False) return self.log(f"Queued message found: {first_queued_message['id']}") diff --git a/modem/message_p2p.py b/modem/message_p2p.py index f66214a6..735aea34 100644 --- a/modem/message_p2p.py +++ b/modem/message_p2p.py @@ -10,7 +10,7 @@ def message_received(event_manager, state_manager, data): decompressed_json_string = data.decode('utf-8') received_message_obj = MessageP2P.from_payload(decompressed_json_string) received_message_dict = MessageP2P.to_dict(received_message_obj) - DatabaseManager(event_manager).add_message(received_message_dict, direction='receive', status='received') + DatabaseManager(event_manager).add_message(received_message_dict, direction='receive', status='received', is_read=False) def message_failed(event_manager, state_manager, data): decompressed_json_string = data.decode('utf-8') diff --git a/modem/message_system_db_manager.py b/modem/message_system_db_manager.py index 6de5bc1a..0e117472 100644 --- a/modem/message_system_db_manager.py +++ b/modem/message_system_db_manager.py @@ -111,7 +111,7 @@ class DatabaseManager: session.flush() # To get the ID immediately return status - def add_message(self, message_data, direction='receive', status=None): + def add_message(self, message_data, direction='receive', status=None, is_read=True): session = self.get_thread_scoped_session() try: # Create and add the origin and destination Stations @@ -132,7 +132,9 @@ class DatabaseManager: body=message_data['body'], timestamp=timestamp, direction=direction, - status_id=status.id if status else None + status_id=status.id if status else None, + is_read=is_read, + attempt=0 ) # Process and add attachments @@ -287,3 +289,34 @@ class DatabaseManager: finally: session.remove() + def increment_message_attempts(self, message_id): + session = self.get_thread_scoped_session() + try: + message = session.query(P2PMessage).filter_by(id=message_id).first() + if message: + message.attempts += 1 + session.commit() + self.log(f"Incremented attempt count for message {message_id}") + else: + self.log(f"Message with ID {message_id} not found") + except Exception as e: + session.rollback() + self.log(f"An error occurred while incrementing attempts for message {message_id}: {e}") + finally: + session.remove() + + def mark_message_as_read(self, message_id): + session = self.get_thread_scoped_session() + try: + message = session.query(P2PMessage).filter_by(id=message_id).first() + if message: + message.is_read = True + session.commit() + self.log(f"Marked message {message_id} as read") + else: + self.log(f"Message with ID {message_id} not found") + except Exception as e: + session.rollback() + self.log(f"An error occurred while marking message {message_id} as read: {e}") + finally: + session.remove() \ No newline at end of file diff --git a/modem/message_system_db_model.py b/modem/message_system_db_model.py index a561f886..31aa0cc7 100644 --- a/modem/message_system_db_model.py +++ b/modem/message_system_db_model.py @@ -1,6 +1,6 @@ # models.py -from sqlalchemy import Index, Column, String, Integer, JSON, ForeignKey, DateTime +from sqlalchemy import Index, Boolean, Column, String, Integer, JSON, ForeignKey, DateTime from sqlalchemy.orm import declarative_base, relationship Base = declarative_base() @@ -46,11 +46,13 @@ class P2PMessage(Base): destination_callsign = Column(String, ForeignKey('station.callsign')) body = Column(String, nullable=True) attachments = relationship('Attachment', backref='p2p_message') + attempt = Column(Integer, default=0) timestamp = Column(DateTime) status_id = Column(Integer, ForeignKey('status.id'), nullable=True) status = relationship('Status', backref='p2p_messages') direction = Column(String) statistics = Column(JSON, nullable=True) + is_read = Column(Boolean, default=True) Index('idx_p2p_message_origin_timestamp', 'origin_callsign', 'via_callsign', 'destination_callsign', 'timestamp', 'attachments') @@ -58,6 +60,7 @@ class P2PMessage(Base): return { 'id': self.id, 'timestamp': self.timestamp.isoformat() if self.timestamp else None, + 'attempt': self.attempt, 'origin': self.origin_callsign, 'via': self.via_callsign, 'destination': self.destination_callsign, @@ -65,6 +68,7 @@ class P2PMessage(Base): 'body': self.body, 'attachments': [attachment.to_dict() for attachment in self.attachments], 'status': self.status.name if self.status else None, + 'is_read': self.is_read, 'statistics': self.statistics } diff --git a/modem/server.py b/modem/server.py index 0a40c816..a2a1058e 100644 --- a/modem/server.py +++ b/modem/server.py @@ -247,13 +247,18 @@ def get_post_freedata_message(): else: api_abort('Error executing command...', 500) -@app.route('/freedata/messages/', methods=['GET', 'POST', 'DELETE']) +@app.route('/freedata/messages/', methods=['GET', 'POST', 'PATCH', 'DELETE']) def handle_freedata_message(message_id): if request.method == 'GET': message = DatabaseManager(app.event_manager).get_message_by_id_json(message_id) return message elif request.method == 'POST': result = DatabaseManager(app.event_manager).update_message(message_id, update_data={'status': 'queued'}) + DatabaseManager(app.event_manager).increment_message_attempts(message_id) + return api_response(result) + elif request.method == 'PATCH': + # Fixme We need to adjust this + result = DatabaseManager(app.event_manager).mark_message_as_read(message_id) return api_response(result) elif request.method == 'DELETE': result = DatabaseManager(app.event_manager).delete_message(message_id) diff --git a/modem/state_manager.py b/modem/state_manager.py index 85eb2c95..9cd93694 100644 --- a/modem/state_manager.py +++ b/modem/state_manager.py @@ -49,11 +49,6 @@ class StateManager: # Set rig control status regardless or rig control method self.radio_status = False - # message system related states - self.pending_messages = False - - - def sendState (self): currentState = self.get_state_event(False) self.statequeue.put(currentState)