Mercurial > code > home > repos > homeauto
changeset 1723:bacb13c10c7a
lookup ip in a roundabout way
author | drewp@bigasterisk.com |
---|---|
date | Fri, 16 Jun 2023 22:23:33 -0700 |
parents | a4e70a35f3a6 |
children | 8d2c75a93d82 |
files | service/powerEagle/reader.py |
diffstat | 1 files changed, 34 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/service/powerEagle/reader.py Fri Jun 16 22:23:05 2023 -0700 +++ b/service/powerEagle/reader.py Fri Jun 16 22:23:33 2023 -0700 @@ -1,6 +1,7 @@ import binascii import json import logging +from pprint import pprint from typing import Dict import aiohttp @@ -13,7 +14,7 @@ from starlette.routing import Route from starlette_exporter import PrometheusMiddleware, handle_metrics -from private_config import cloudId, deviceIp, installId, macId, periodSec +from private_config import cloudId, deviceMac, installId, macId, periodSec ROOM = Namespace("http://projects.bigasterisk.com/room/") @@ -24,6 +25,33 @@ auth = binascii.b2a_base64(authPlain.encode('ascii')).strip(b'=\n') +async def getOneSseEvent(url): + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + line1 = None + async for line_in_bytes in response.content: + if line1 is None: + line1 = line_in_bytes + else: + return line1, line_in_bytes + raise ValueError + + +def getJsonldValue(g, k): + return g[k][0]['@value'] + + +def ipLookup(collectorJsonLd, mac): + for add in collectorJsonLd['patch']['adds']: + for g in add['@graph']: + try: + if getJsonldValue(g, 'http://projects.bigasterisk.com/room/macAddress') == mac: + return getJsonldValue(g, 'http://projects.bigasterisk.com/room/assignedIp') + except (KeyError, ValueError): + pass + raise ValueError + + class Poller(object): def __init__(self, out: Dict[str, Gauge], graph): @@ -31,6 +59,11 @@ self.graph = graph async def poll(self, first_run: bool): + line1, line2 = await getOneSseEvent('http://collector.default.svc.cluster.local/collector/graph/lanscape') + p = json.loads(line2.split(b'data: ', 1)[1]) + + deviceIp = ipLookup(p, deviceMac) + url = (f'http://{deviceIp}/cgi-bin/cgi_manager') async with aiohttp.ClientSession() as session: