Skip to content

Commit

Permalink
Parse MOPComplete XML when starting the MopClient
Browse files Browse the repository at this point in the history
  • Loading branch information
lukipuki committed Oct 27, 2023
1 parent c9d2ebd commit c7d0df1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/yaroc/clients/mop.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ class MopClient:
STAT_OOC = 15
STAT_DNS = 20

def __init__(self, api_key: str):
def __init__(self, api_key: str, mop_xml: str | None):
self.api_key = api_key
if isinstance(mop_xml, str):
self.results = MopClient.results_from_file(mop_xml)
else:
self.results = []

@staticmethod
def _parse_int(s: str | None) -> int | None:
Expand Down Expand Up @@ -135,7 +139,8 @@ async def loop(self):
async with self.session:
await asyncio.sleep(1000000)

def results_from_file(self, filename: str) -> List[MeosResult]:
@staticmethod
def results_from_file(filename: str) -> List[MeosResult]:
xml = ET.parse(filename)
return MopClient._results_from_meos_xml(xml.getroot())

Expand All @@ -153,7 +158,7 @@ async def send_result(self, result: MeosResult):
else:
logging.error("Sending unsuccessful: {} {}", response, await response.text())

async def results(self, address: str, port: int) -> List[MeosResult]:
async def fetch_results(self, address: str, port: int) -> List[MeosResult]:
async with self.session.get(f"http://{address}:{port}/meos?difference=zero") as response:
assert response.status == 200
xml = ET.XML(response.text)
Expand Down
4 changes: 2 additions & 2 deletions src/yaroc/utils/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from dependency_injector.wiring import Provide, inject

from ..clients.client import Client
from ..clients.mop import MopClient
from ..clients.mqtt import MqttClient, SIM7020MqttClient
from ..clients.roc import RocClient
from ..clients.mop import MopClient
from ..clients.sirap import SirapClient
from ..utils.async_serial import AsyncATCom
from ..utils.si import FakeSiManager, UdevSiManager
Expand Down Expand Up @@ -59,7 +59,7 @@ class Container(containers.DeclarativeContainer):
sirap=providers.Factory(
SirapClient, config.client.sirap.ip, config.client.sirap.port, loop
),
mop=providers.Factory(MopClient, config.client.mop.api_key),
mop=providers.Factory(MopClient, config.client.mop.api_key, config.client.mop.mop_xml),
mqtt=providers.Factory(MqttClient),
roc=providers.Factory(RocClient),
sim7020=providers.Factory(SIM7020MqttClient, async_at=async_at, retry_loop=loop),
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import xml.etree.ElementTree as ET
from datetime import timedelta

from yaroc.clients.mop import MopClient, MeosCategory, MeosCompetitor, MeosResult
from yaroc.clients.mop import MeosCategory, MeosCompetitor, MeosResult, MopClient

TEST_XML = """<?xml version="1.0" encoding="UTF-8"?>
<MOPComplete xmlns="http://www.melin.nu/mop" nextdifference="1377871">
Expand Down

0 comments on commit c7d0df1

Please sign in to comment.