-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from iKonoTelecomunicaciones/main
Merge all changes
- Loading branch information
Showing
24 changed files
with
459 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .base_event import BaseEvent | ||
from .event_generator import send_node_event | ||
from .event_types import MenuflowEventTypes, MenuflowNodeEvents | ||
from .nats_publisher import NatsPublisher | ||
from .node_events import NodeEntry, NodeInputData, NodeInputTimeout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import json | ||
import logging | ||
|
||
from attr import dataclass, ib | ||
from mautrix.types import SerializableAttrs, UserID | ||
from mautrix.util.logging import TraceLogger | ||
from nats.js.client import JetStreamContext | ||
|
||
from .event_types import MenuflowEventTypes, MenuflowNodeEvents | ||
from .nats_publisher import NatsPublisher | ||
|
||
log: TraceLogger = logging.getLogger("report.event") | ||
|
||
|
||
@dataclass | ||
class BaseEvent(SerializableAttrs): | ||
event_type: MenuflowEventTypes = ib(default=None) | ||
event: MenuflowNodeEvents = ib(default=None) | ||
timestamp: float = ib(factory=float) | ||
sender: UserID = ib(factory=UserID) | ||
|
||
def send(self): | ||
asyncio.create_task(self.send_to_nats()) | ||
|
||
async def send_to_nats(self): | ||
jetstream: JetStreamContext = None | ||
|
||
file = open("/data/room_events.txt", "a") | ||
file.write(f"{json.dumps(self.serialize())}\n\n") | ||
file.close() | ||
log.error(f"Sending event {self.serialize()}") | ||
|
||
_, jetstream = await NatsPublisher.get_connection() | ||
if jetstream: | ||
try: | ||
subject = NatsPublisher.config["nats.subject"] | ||
await jetstream.publish( | ||
subject=f"{subject}.{self.event_type}", | ||
payload=json.dumps(self.serialize()).encode(), | ||
) | ||
except Exception as e: | ||
log.error(f"Error publishing event to NATS: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from __future__ import annotations | ||
|
||
from datetime import datetime | ||
from logging import getLogger | ||
from typing import Optional | ||
|
||
from ..config import Config | ||
from .event_types import MenuflowEventTypes, MenuflowNodeEvents | ||
from .node_events import NodeEntry, NodeInputData, NodeInputTimeout | ||
|
||
log = getLogger() | ||
|
||
|
||
def send_node_event( | ||
event_type: MenuflowNodeEvents, config: Config, send_event: Optional[bool] = None, **kwargs | ||
): | ||
general_send_event = config["menuflow.send_events"] | ||
send_node_event = send_event if send_event is not None else general_send_event | ||
if not send_node_event: | ||
return | ||
|
||
if event_type == MenuflowNodeEvents.NodeEntry: | ||
event = NodeEntry( | ||
event_type=MenuflowEventTypes.NODE, | ||
event=MenuflowNodeEvents.NodeEntry, | ||
timestamp=datetime.utcnow().timestamp(), | ||
room_id=kwargs.get("room_id"), | ||
sender=kwargs.get("sender"), | ||
node_type=kwargs.get("node_type"), | ||
node_id=kwargs.get("node_id"), | ||
o_connection=kwargs.get("o_connection"), | ||
variables=kwargs.get("variables"), | ||
) | ||
elif event_type == MenuflowNodeEvents.NodeInputData: | ||
event = NodeInputData( | ||
event_type=MenuflowEventTypes.NODE, | ||
event=MenuflowNodeEvents.NodeInputData, | ||
timestamp=datetime.utcnow().timestamp(), | ||
room_id=kwargs.get("room_id"), | ||
sender=kwargs.get("sender"), | ||
node_id=kwargs.get("node_id"), | ||
o_connection=kwargs.get("o_connection"), | ||
variables=kwargs.get("variables"), | ||
) | ||
elif event_type == MenuflowNodeEvents.NodeInputTimeout: | ||
event = NodeInputTimeout( | ||
event_type=MenuflowEventTypes.NODE, | ||
event=MenuflowNodeEvents.NodeInputTimeout, | ||
timestamp=datetime.utcnow().timestamp(), | ||
room_id=kwargs.get("room_id"), | ||
sender=kwargs.get("sender"), | ||
node_id=kwargs.get("node_id"), | ||
o_connection=kwargs.get("o_connection"), | ||
variables=kwargs.get("variables"), | ||
) | ||
|
||
event.send() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from mautrix.types import SerializableEnum | ||
|
||
|
||
class MenuflowEventTypes(SerializableEnum): | ||
NODE = "NODE" | ||
|
||
|
||
class MenuflowNodeEvents(SerializableEnum): | ||
NodeEntry = "NodeEntry" | ||
NodeInputData = "NodeInputData" | ||
NodeInputTimeout = "NodeInputTimeout" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import logging | ||
|
||
from mautrix.util.logging import TraceLogger | ||
from nats import connect as nats_connect | ||
from nats.aio.client import Client as NATSClient | ||
from nats.js.client import JetStreamContext | ||
|
||
from ..config import Config | ||
|
||
log: TraceLogger = logging.getLogger("menuflow.nats") | ||
|
||
|
||
class NatsPublisher: | ||
_nats_conn: NATSClient = None | ||
_jetstream_conn: JetStreamContext = None | ||
config: Config = None | ||
|
||
@classmethod | ||
def init_cls(cls, config: Config): | ||
cls.config = config | ||
|
||
@classmethod | ||
async def get_connection(cls) -> JetStreamContext: | ||
if not cls.config["nats.enabled"]: | ||
return None, None | ||
|
||
if not cls._nats_conn: | ||
try: | ||
cls._nats_conn, cls._jetstream_conn = await cls.nats_jetstream_connection() | ||
except Exception as e: | ||
log.error(f"Error connecting to NATS: {e}") | ||
|
||
return cls._nats_conn, cls._jetstream_conn | ||
|
||
@classmethod | ||
async def nats_jetstream_connection(cls) -> JetStreamContext: | ||
log.info("Connecting to NATS JetStream") | ||
nc: NATSClient = await nats_connect(cls.config["nats.address"]) | ||
js = nc.jetstream() | ||
subject = f"{cls.config['nats.subject']}.*" | ||
await js.add_stream(name="menuflow", subjects=[subject]) | ||
return nc, js | ||
|
||
@classmethod | ||
async def close_connection(cls): | ||
if cls._nats_conn: | ||
log.info("Closing NATS connection") | ||
await cls._nats_conn.close() | ||
cls._nats_conn = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Dict | ||
|
||
from attr import dataclass, ib | ||
|
||
from .base_event import BaseEvent | ||
|
||
|
||
@dataclass | ||
class NodeEntry(BaseEvent): | ||
room_id: str = ib(factory=str) | ||
node_type: str = ib(factory=str) | ||
node_id: str = ib(factory=str) | ||
o_connection: str = ib(default=None) | ||
variables: Dict = ib(factory=dict) | ||
|
||
|
||
@dataclass | ||
class NodeInputData(BaseEvent): | ||
room_id: str = ib(factory=str) | ||
node_id: str = ib(factory=str) | ||
o_connection: str = ib(factory=str) | ||
variables: Dict = ib(factory=dict) | ||
|
||
|
||
@dataclass | ||
class NodeInputTimeout(BaseEvent): | ||
room_id: str = ib(factory=str) | ||
node_id: str = ib(factory=str) | ||
o_connection: str = ib(factory=str) | ||
variables: Dict = ib(factory=dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ | |
from .media import Media | ||
from .message import Message | ||
from .switch import Switch | ||
from .types import Nodes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.