-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
130 lines (102 loc) · 3.69 KB
/
Copy pathmodel.py
File metadata and controls
130 lines (102 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from __future__ import annotations
"""Primary runtime event model used by the router and all adapters."""
import hashlib
import time
from dataclasses import dataclass, field
from enum import Enum
from typing import Any
class Source(str, Enum):
APRS = "aprs"
MESHTASTIC = "meshtastic"
COT = "cot"
SARTRACK = "sartrack"
INTERNAL = "internal"
@dataclass(slots=True)
class BaseEvent:
"""Shared metadata for all runtime events flowing through the router."""
id: str
timestamp: int = field(default_factory=lambda: int(time.time()))
source: Source = Source.INTERNAL
ttl: int = 30
raw: dict[str, Any] = field(default_factory=dict)
@property
def kind(self) -> str:
raise NotImplementedError
@property
def event_uid(self) -> str:
base = f"{self.kind}:{self.id}:{self.timestamp}:{self.source.value}:{self._dedup_payload()}"
return hashlib.sha1(base.encode("utf-8")).hexdigest()[:16]
@property
def dedup_key(self) -> str:
return self.event_uid
def _dedup_payload(self) -> str:
return ""
@dataclass(slots=True)
class PositionEvent(BaseEvent):
"""Normalized location-bearing event for stations, objects and future markers/tasks."""
entity_kind: str = "station"
type: str = "unit"
lat: float | None = None
lon: float | None = None
alt: float | None = None
speed: float | None = None
heading: float | None = None
tactical_name: str | None = None
message: str | None = None
symbol_table: str | None = None
symbol_code: str | None = None
object_name: str | None = None
owner_id: str | None = None
@property
def kind(self) -> str:
return "position"
def _dedup_payload(self) -> str:
return (
f"{self.entity_kind}:{self.type}:{self.object_name or ''}:{self.owner_id or ''}:"
f"{self.message or ''}:{self.tactical_name or ''}:"
f"{self.symbol_table or ''}:{self.symbol_code or ''}:"
f"{self.lat}:{self.lon}:{self.alt}:{self.speed}:{self.heading}"
)
@property
def dedup_key(self) -> str:
lat = round(self.lat or 0.0, 5)
lon = round(self.lon or 0.0, 5)
time_bucket = self.timestamp // 30
return f"{self.kind}:{self.id}:{lat}:{lon}:{time_bucket}"
def is_position(self) -> bool:
return self.lat is not None and self.lon is not None
def is_object(self) -> bool:
return self.entity_kind == "object" or self.type == "object" or self.object_name is not None
def is_station(self) -> bool:
return not self.is_object()
@dataclass(slots=True)
class MessageEvent(BaseEvent):
"""Normalized text message event routed between protocol adapters."""
message: str = ""
target: str | None = None
message_id: str | None = None
@property
def kind(self) -> str:
return "message"
def _dedup_payload(self) -> str:
return f"{self.target or ''}:{self.message_id or ''}:{self.message}"
@property
def dedup_key(self) -> str:
if self.message_id:
return f"{self.kind}:{self.id}:{self.target or ''}:{self.message_id}"
return f"{self.kind}:{self.id}:{self.target or ''}:{self.message}"
def build_raw_payload(
*,
canonical: dict[str, Any] | None = None,
ingress_adapter: str | None = None,
raw_context: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""Build a consistent raw payload envelope stored on runtime events."""
payload: dict[str, Any] = {}
if canonical is not None:
payload["canonical"] = canonical
if raw_context:
payload.update(raw_context)
if ingress_adapter:
payload["ingress_adapter"] = ingress_adapter
return payload