Skip to content

Commit

Permalink
Use PanBase for remote sensor
Browse files Browse the repository at this point in the history
* Change to `PanBase` rather than set up `db` and `logger` manually.
* Adding some types.
  • Loading branch information
wtgee committed May 8, 2024
1 parent 0d756ff commit 5ba310c
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions src/panoptes/pocs/sensor/remote.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,37 @@
import requests
from panoptes.pocs.utils.logger import get_logger
from panoptes.utils import error
from panoptes.utils.config.client import get_config
from panoptes.utils.database import PanDB
from panoptes.utils.time import current_time

from panoptes.pocs.base import PanBase

class RemoteMonitor(object):
"""Does a pull request on an endpoint to obtain a JSON document."""

def __init__(self, endpoint_url=None, sensor_name=None, *args, **kwargs):
self.logger = get_logger()
class RemoteMonitor(PanBase):
"""Does a pull request on an endpoint to obtain a JSON document."""

def __init__(self, endpoint_url: str = None, sensor_name: str = None, *args, **kwargs):
super().__init__(*args, **kwargs)

self.logger.info(f'Setting up remote sensor {sensor_name}')

# Setup the DB either from kwargs or config.
self.db = None
db_type = get_config('db.type', default='file')

if 'db_type' in kwargs:
self.logger.info(f"Setting up {kwargs['db_type']} type database")
db_type = kwargs.get('db_type', db_type)

self.db = PanDB(db_type=db_type)


self.sensor_name = sensor_name
self.sensor = None

if endpoint_url is None:
# Get the config for the sensor
endpoint_url = get_config(f'environment.{sensor_name}.url')
if endpoint_url is None:
raise error.PanError(f'No endpoint_url for {sensor_name}')

if not endpoint_url.startswith('http'):
endpoint_url = f'http://{endpoint_url}'

self.endpoint_url = endpoint_url

def disconnect(self):
self.logger.debug('Stop listening on {self.endpoint_url}')

def capture(self, store_result=True):
def capture(self, store_result: bool = True) -> dict:
"""Read JSON from endpoint url and capture data.
Note:
Expand All @@ -49,26 +40,27 @@ def capture(self, store_result=True):
Returns:
sensor_data (dict): Dictionary of sensors keyed by sensor name.
"""

self.logger.debug(f'Capturing data from remote url: {self.endpoint_url}')
try:
sensor_data = requests.get(self.endpoint_url).json()
except requests.exceptions.ConnectionError:
self.logger.warning(f'No connection at {self.endpoint_url}')
return {}
return {}
if isinstance(sensor_data, list):
sensor_data = sensor_data[0]

self.logger.debug(f'Captured on {self.sensor_name}: {sensor_data!r}')

sensor_data['date'] = current_time(flatten=True)

if store_result and len(sensor_data) > 0:
self.db.insert_current(self.sensor_name, sensor_data)

# Make a separate power entry
if 'power' in sensor_data:
self.db.insert_current('power', sensor_data['power'])

self.logger.debug(f'Remote data: {sensor_data}')

return sensor_data

0 comments on commit 5ba310c

Please sign in to comment.