def main(): parser = argparse.ArgumentParser( description='Raspberry Pi sampler for LEaRN project') parser.add_argument('-c', '--config', type=str, required=False) parser.add_argument('-t', '--configtest', action='store_true') args = parser.parse_args() if args.config is not None: assert (os.path.exists(args.config)) os.environ[ENV_YAML_PATH] = args.config if args.configtest: try: c = Config().config except ConfigurationError as e: print("Configuration is invalid: {0}".format(e.message)) sys.exit(1) print("Configuration is valid.") sys.exit(0) config = Config().config global logger logger = configure_logger(config) logger.info("Raspberry Pi Sampler: entering.") try: GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) sample() except KeyboardInterrupt: GPIO.cleanup() finally: logger.info("Raspberry Pi Sampler: exiting.")
def spool_data(q): config = Config().config logger = configure_logger(config) logger.info("Spooler: entering.") repo = SqliteRepository(config) while True: try: logger.debug("Spooler: getting from queue...") obs = q.get() logger.debug("Spooler: received observation: {0}".format(str(obs))) if isinstance(obs, Observation): repo.create_observation(obs) elif isinstance(obs, MultiObservation): repo.create_multiobservation(obs) else: raise TypeError("Observation of type {0} is unknown".format( obs.__class__.__name__)) logger.debug("Spooler: observation stored in database") except KeyboardInterrupt: break except Exception as e: logger.exception("Spooler: caught exception: {0}".format(str(e))) finally: pass logger.info("Spooler: exiting.")
def generate_observations_minute(): config = Config().config logger = configure_logger(config) logger.debug("Begin generate_observations_minute...") thing = config[CFG_THING] # Iterate over sensors and generate observations sensors = config[CFG_SENSORS] logger.debug("Iterating over {0} sensors...".format(len(sensors))) for s in sensors: logger.debug(str(s)) logger.debug("Calling generate_observations for sensor type {0}...".format(s.typ)) observations = s.generate_observations() logger.debug("Enqueing observations...") [logger.debug(str(o)) for o in observations]
def main(): parser = argparse.ArgumentParser(description='Test Raspberry Pi sensors') parser.add_argument('-c', '--config', type=str, required=False) args = parser.parse_args() if args.config is not None: assert (os.path.exists(args.config)) os.environ[ENV_YAML_PATH] = args.config config = Config().config global logger logger = configure_logger(config) logger.info("Transmitter: entering.") repo = SqliteRepository(config) s = sched.scheduler(time.time, time.sleep) while True: try: # Get the transmit interval from the env var set by balenaCloud transmit_interval = os.environ.get('TRANSMIT_INTERVAL') if transmit_interval is None: logger.info("Transmitter: TRANSMIT_INTERVAL is not defined. We will default to 15 seconds unless this is set.") transmit_interval = 15 else: transmit_interval = float(transmit_interval) transports = config[CFG_TRANSPORTS] logger.debug("Transmitter: scheduling network transmissions for {0} transports...".format(len(transports))) for t in transports: s.enter(transmit_interval, SCHEDULE_PRIORITY_DEFAULT, t.transmit, argument=(repo,)) logger.debug("Transmitter: Running scheduler...") s.run() logger.debug("Transmitter: End of iteration.") except KeyboardInterrupt: break except AuthenticationException as ae: logger.exception("Transmitter: {0}".format(ae.message)) except TransmissionException as te: logger.exception("Transmitter: {0}".format(te.message)) finally: pass logger.info("Transmitter: exiting.")
def main(): parser = argparse.ArgumentParser(description='Simulate a sensor') parser.add_argument('-d', '--startdate', nargs=5, type=int, default=[2017, 1, 1, 0, 0], help='Start date for data: YYYY MM DD HH MM') parser.add_argument('-i', '--dateint', type=int, default=1, help='Number of minutes between subsequent data') parser.add_argument('-c', '--config', type=str, required=False) parser.add_argument('-t', '--configtest', action='store_true') args = parser.parse_args() if args.config is not None: assert (os.path.exists(args.config)) os.environ[ENV_YAML_PATH] = args.config if args.configtest: try: c = Config().config except ConfigurationError as e: print("Configuration is invalid: {0}".format(e.message)) sys.exit(1) print("Configuration is valid.") sys.exit(0) config = Config().config global logger logger = configure_logger(config) start_date = datetime(year=args.startdate[0], month=args.startdate[1], day=args.startdate[2], hour=args.startdate[3], minute=args.startdate[4]) print("Start date: {0}".format(str(start_date))) date_interval = timedelta(minutes=args.dateint) print("Date interval: {0} minutes".format(str(date_interval))) sample(start_date=start_date, date_interval=date_interval)
import sched import time import sys import datetime import os import io import requests from requests.exceptions import ConnectionError from requests_toolbelt.adapters import host_header_ssl from sensors.common.logging import configure_logger from sensors.persistence.sqlite import SqliteRepository logger = configure_logger() jwt_token = (None, None) # Requests session session = None # Per-device ID and key. JWT_ID = None JWT_KEY = None try: JWT_ID = os.environ['JWT_ID'] JWT_KEY = os.environ['JWT_KEY'] except KeyError: mesg = "Environment variable JWT_ID or JWT_KEY not defined." logger.error(mesg) sys.exit(mesg)