Accept Messages

This commit is contained in:
2024-10-09 19:04:19 +02:00
parent 4199b1c347
commit 5e0945f9ae
8 changed files with 226 additions and 82 deletions

View File

@ -15,44 +15,9 @@ from datetime import datetime
from threading import Lock
from typing import Generator
from pydantic import validate_call, BaseModel
from pydantic import validate_call
from ums.utils import PERSIST_PATH, AgentMessage
class RowObject(BaseModel):
"""
Object representing a database row.
"""
count : int
"""
The count (primary key) of the item.
"""
sender : str
"""
The sender of the message.
"""
recipient : str
"""
The recipient of the message
"""
time : int
"""
The time (unix timestamp) the message was received/ sent.
"""
message : AgentMessage
"""
The message received/ sent.
"""
processed : bool
"""
Did the management process the message, i.e., did the tasks necessary for this message (mostly only relevant for received messages).
"""
from ums.utils import PERSIST_PATH, AgentMessage, MessageDbRow
class DB():
@ -117,7 +82,7 @@ class DB():
finally:
self.db_lock.release()
def __iter__(self) -> Generator[RowObject, None, None]:
def __iter__(self) -> Generator[MessageDbRow, None, None]:
yield from self.iterate()
@validate_call
@ -126,7 +91,7 @@ class DB():
processed:bool|None=None,
time_after:int|None=None, time_before:int|None=None,
limit:int=20, offset:int=0, _count_only:bool=False
) -> Generator[RowObject|int, None, None]:
) -> Generator[MessageDbRow|int, None, None]:
where = []
params = {
@ -177,8 +142,8 @@ class DB():
kwargs['_count_only'] = True
return next(self.iterate(**kwargs))
def _create_row_object(self, row:sqlite3.Row) -> RowObject:
return RowObject(
def _create_row_object(self, row:sqlite3.Row) -> MessageDbRow:
return MessageDbRow(
count=row['count'],
sender=row['sender'],
recipient=row['recipient'],
@ -187,7 +152,7 @@ class DB():
processed=row['processed']
)
def by_count(self, count:int) -> RowObject|None:
def by_count(self, count:int) -> MessageDbRow|None:
with self.db:
try:
return self._create_row_object(