#!/usr/bin/python3 # # import sys import os.path import configparser import importlib.util import asyncio import signal # Import our utilities. _pathname = os.path.join(os.path.dirname(__file__), '../lib/util.py') _spec = importlib.util.spec_from_file_location('util', _pathname) util = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(util) CONFIG_FNAME = '/etc/homesvr/weather.conf' # These directions are used for Wind direction. These values are # index-specific from N==0 to NNW==15. These cardinal, integer # values are used for communication with the weather-controller. DIRECTIONS = ( 'N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', ) def record_data(idb, tf, h, r, wd, ws): print('RECORD:', tf, h, r, wd, ws) body = [{ 'measurement': 'weather', #'tags': { }, 'fields': { 'tempF': tf, 'humidity': h, 'rain': r, 'windDir': wd, 'windSpeed': ws, }, }] _ = idb.write_points(body) def fetch_data(): ### one big massive TODO import random tf = random.uniform(70.0, 90.0) # Outdoor temperature (F) h = random.uniform(30.0, 90.0) # Outdoor humidity r = random.uniform(0.1, 1.0) # Rainfall (inches since last query) wd = random.choice(DIRECTIONS) # Wind direction (one of DIRECTIONS) ws = random.uniform(0.0, 10.0) # Wind speed (mph) return tf, h, r, wd, ws def take_measurement(idb): print('HERE:', asyncio.get_event_loop().time()) # Fetch data from our measurement-microcontroller. tf, h, r, wd, ws = fetch_data() assert wd in DIRECTIONS # Push that data into the InfluxDB record_data(idb, tf, h, r, wd, ws) def main(): cfg = configparser.ConfigParser() cfg.read(CONFIG_FNAME) sect = cfg['influxdb'] idb = util.prepare_influxdb(sect['host'], sect['user'], sect['pwd'], sect['db']) loop = asyncio.get_event_loop() loop.add_signal_handler(signal.SIGINT, loop.stop) loop.add_signal_handler(signal.SIGTERM, loop.stop) period = cfg.getfloat('measure', 'period') util.create_periodic(loop, period, take_measurement, idb) loop.run_forever() # Shut down nicely. util.graceful_close(loop) if __name__ == '__main__': main()