]> www.average.org Git - loctrkd.git/blobdiff - loctrkd/common.py
Convert recitifier to multiprotocol support
[loctrkd.git] / loctrkd / common.py
index f112fbd53a51176e279b77c01cc05e6f32529bd2..30a17194ffcb5f645bc01d43cf265261a718e339 100644 (file)
@@ -3,11 +3,13 @@
 from configparser import ConfigParser
 from importlib import import_module
 from getopt import getopt
+from json import dumps
 from logging import Formatter, getLogger, Logger, StreamHandler, DEBUG, INFO
 from logging.handlers import SysLogHandler
 from pkg_resources import get_distribution, DistributionNotFound
 from sys import argv, stderr, stdout
 from typing import Any, cast, Dict, List, Optional, Tuple, Union
+from types import SimpleNamespace
 
 from .protomodule import ProtoModule
 
@@ -76,3 +78,69 @@ def parse_message(proto: str, packet: bytes, is_incoming: bool = True) -> Any:
 
 def exposed_protos() -> List[Tuple[str, bool]]:
     return [item for pmod in pmods for item in pmod.exposed_protos()]
+
+
+class Report(SimpleNamespace):
+    TYPE: str
+
+    @property
+    def json(self) -> str:
+        self.type = self.TYPE
+        return dumps(self.__dict__)
+
+
+class CoordReport(Report):
+    TYPE = "location"
+
+    def __init__(
+        self,
+        *,
+        devtime: str,
+        battery_percentage: int,
+        accuracy: float,
+        altitude: float,
+        speed: float,
+        direction: float,
+        latitude: float,
+        longitude: float
+    ) -> None:
+        super().__init__(
+            devtime=devtime,
+            battery_percentage=battery_percentage,
+            accuracy=accuracy,
+            altitude=altitude,
+            speed=speed,
+            direction=direction,
+            latitude=latitude,
+            longitude=longitude,
+        )
+
+
+class HintReport(Report):
+    TYPE = "approximate_location"
+
+    def __init__(
+        self,
+        *,
+        devtime: str,
+        battery_percentage: int,
+        mcc: int,
+        mnc: int,
+        gsm_cells: List[Tuple[int, int, int]],
+        wifi_aps: List[Tuple[str, str, int]]
+    ) -> None:
+        super().__init__(
+            devtime=devtime,
+            battery_percentage=battery_percentage,
+            mcc=mcc,
+            mnc=mnc,
+            gsm_cells=gsm_cells,
+            wifi_aps=wifi_aps,
+        )
+
+
+class StatusReport(Report):
+    TYPE = "status"
+
+    def __init__(self, *, battery_percentage: int) -> None:
+        super().__init__(battery_percentage=battery_percentage)