async def main(loop): global debug_mode global gn_conn global refuid global compuid args = parse_cmdline() if args.debug: debug_mode = args.debug # look for a config file, if it doesn't exist, build a generic one if not os.path.isfile(args.conf): await initial_setup(args, loop) loop.stop() return # instantiate the gnhast class with the conf file as an argument gn_conn = gnhast.gnhast(loop, args.conf) gn_conn.debug = debug_mode # connect to gnhast await gn_conn.gn_build_client('presdiff') gn_conn.LOG("Pressure Differential collector starting up") # Read the update value from the presdiff section of the config file poll_time = gn_conn.config['presdiff']['update'] refuid = gn_conn.config['presdiff']['refuid'] compuid = gn_conn.config['presdiff']['compuid'] if compuid == '' or refuid == '': gn_conn.LOG_ERROR('compuid or refuid not specified sanely') loop.stop() return # set up a signal handler for sig in [signal.SIGTERM, signal.SIGINT]: loop.add_signal_handler(sig, lambda: asyncio.ensure_future(gn_conn.shutdown(sig, loop))) # fire up the listener and do gnhastly things.. asyncio.ensure_future(gn_conn.gnhastd_listener()) asyncio.ensure_future(register_devices(gn_conn)) # wire up callbacks gn_conn.coll_reg_cb = coll_reg_cb gn_conn.coll_upd_cb = coll_upd_cb # poll your sensor for data gn_conn.LOG('Asking gnhast for data on sensors') await gn_conn.gn_ldevs(refuid) await gn_conn.gn_ldevs(compuid) return
async def main(loop): global debug_mode try: args = parse_cmdline() except SystemExit: loop.stop() return if args.debug: debug_mode = args.debug # look for a config file, if it doesn't exist, build a generic one if not os.path.isfile(args.conf): await initial_setup(args, loop) loop.stop() return # instantiate the gnhast class with the conf file as an argument gn_conn = gnhast.gnhast(loop, args.conf) gn_conn.debug = debug_mode # connect to gnhast await gn_conn.gn_build_client('skeleton') gn_conn.LOG("Skeleton collector starting up") # Read the update value from the skeleton section of the config file poll_time = gn_conn.config['skeleton']['update'] # set up a signal handler for sig in [signal.SIGTERM, signal.SIGINT]: loop.add_signal_handler( sig, lambda: asyncio.ensure_future(gn_conn.shutdown(sig, loop))) # log reopen on SIGHUP loop.add_signal_handler(signal.SIGHUP, lambda: asyncio.ensure_future(gn_conn.log_open())) # fire up the listener and do gnhastly things.. asyncio.ensure_future(gn_conn.gnhastd_listener()) asyncio.ensure_future(register_devices(gn_conn)) # poll your sensor for data gn_conn.LOG('starting poller') asyncio.ensure_future(poll_sensor(gn_conn, poll_time)) return
async def initial_setup(args, loop): print("This is your first run of the collector, setting up") print("Using gnhast server at {0}:{1}".format(args.server, str(args.port))) try: cf = open(args.conf, 'w') except PermissionError as error: print('ERROR: Cannot open {0} for writing'.format(args.conf)) print('ERROR: {0}'.format(error)) exit(1) print('gnhastd {', file=cf) print(' hostname = "{0}"'.format(args.server), file=cf) print(' port = {0}'.format(str(args.port)), file=cf) print('}', file=cf) print('', file=cf) print('presdiff {', file=cf) print(' update = {0}'.format(str(args.poll_time)), file=cf) print(' refuid = ""', file=cf) print(' compuid = ""', file=cf) print('}', file=cf) cf.close() print("Wrote initial config file at {0}, connecting to gnhastd".format(args.conf)) gn_conn = gnhast.gnhast(loop, args.conf) await gn_conn.gn_build_client('presdiff') print("Connection established, wiring devices") diff_dev = gn_conn.new_device('presdiff', 'Pressure Difference', gn_conn.cf_type.index('sensor'), gn_conn.cf_subt.index('pressure')) diff_dev['rrdname'] = diff_dev['name'].replace(' ', '_')[:20] # calculated type diff_dev['proto'] = 16 print("Re-writing config file: {0}".format(args.conf)) gn_conn.write_conf_file(args.conf) print("Disconnecting from gnhastd") await gn_conn.gn_disconnect() print("Config file written.") print("Edit it to fill in refuid and compuid, then restart collector")
async def initial_setup(args, loop): print("This is your first run of the collector, setting up") print("Using gnhast server at {0}:{1}".format(args.server, str(args.port))) try: cf = open(args.conf, 'w') except PermissionError as error: print('ERROR: Cannot open {0} for writing'.format(args.conf)) print('ERROR: {0}'.format(error)) exit(1) print('gnhastd {', file=cf) print(' hostname = "{0}"'.format(args.server), file=cf) print(' port = {0}'.format(str(args.port)), file=cf) print('}', file=cf) print('', file=cf) print('skeleton {', file=cf) print(' update = 5', file=cf) print('}', file=cf) print('misc {', file=cf) print(' logfile = "/usr/local/var/log/influxcoll.log"', file=cf) print('}', file=cf) cf.close() print("Wrote initial config file at {0}, connecting to gnhastd".format( args.conf)) gn_conn = gnhast.gnhast(loop, args.conf) await gn_conn.gn_build_client('skeleton') print("Connection established, wiring devices") test_dev = gn_conn.new_device('testdev', 'Skeleton Test Device', gn_conn.cf_type.index('sensor'), gn_conn.cf_subt.index('number')) test_dev['rrdname'] = test_dev['name'].replace(' ', '_')[:20] print("Re-writing config file: {0}".format(args.conf)) gn_conn.write_conf_file(args.conf) print("Disconnecting from gnhastd") await gn_conn.gn_disconnect() print("Config file written.") print("Edit it if needed, then restart collector")
async def main(loop): global debug_mode args = parse_cmdline() if args.debug: debug_mode = args.debug uid_prefix = args.uid_prefix + 'BME680-' + args.address + '-' if not os.path.isfile(args.conf): await initial_setup(args, uid_prefix, loop) exit(0) gn_conn = gnhast.gnhast(loop, args.conf) gn_conn.debug = debug_mode await gn_conn.gn_build_client('BME680-{0}'.format(args.address)) gn_conn.LOG("BME680 collector starting up") i2c_addr = gn_conn.config['bme680coll']['i2c_addr'] i2c_addr_int = int(i2c_addr, 16) sensor = init_bme680(i2c_addr_int) if sensor is None: gn_conn.LOG_ERROR('Could not intialize BME680') return burn_in = gn_conn.config['bme680coll']['burn_in'] burn_in = 50 poll_time = gn_conn.config['bme680coll']['update'] # set up a signal handler for sig in [signal.SIGTERM, signal.SIGINT]: loop.add_signal_handler( sig, lambda: asyncio.ensure_future(gn_conn.shutdown(sig, loop))) # fire up the listener and do gnhastly things.. asyncio.ensure_future(gn_conn.gnhastd_listener()) asyncio.ensure_future(register_devices(gn_conn)) # burn in the sensor and then fire it up await burn_in_sensor(sensor, burn_in, gn_conn) gn_conn.LOG('Burn-in complete, starting poller') asyncio.ensure_future(poll_sensor(gn_conn, sensor, poll_time, uid_prefix)) return
async def main(loop): global debug_mode global db_client global gn_conn try: args = parse_cmdline() except SystemExit: loop.stop() return if args.debug: debug_mode = args.debug # look for a config file, if it doesn't exist, build a generic one if not os.path.isfile(args.conf): await initial_setup(args, loop) loop.stop() return # instantiate the gnhast class with the conf file as an argument gn_conn = gnhast.gnhast(loop, args.conf) gn_conn.debug = debug_mode # connect to gnhast await gn_conn.gn_build_client('influxcoll') gn_conn.LOG("InfluxDB collector starting up") # Read the db info from the influx section of the config and connect if 'user' in gn_conn.config['influxcoll'] and 'pass' in gn_conn.config['influxcoll']: db_client = InfluxDBClient(host=gn_conn.config['influxcoll']['host'], port=gn_conn.config['influxcoll']['port'], username=gn_conn.config['influxcoll']['user'], password=gn_conn.config['influxcoll']['pass']) else: db_client = InfluxDBClient(host=gn_conn.config['influxcoll']['host'], port=gn_conn.config['influxcoll']['port']) db_list = db_client.get_list_database() if not any(d.get('name', None) == gn_conn.config['influxcoll']['influxdb_name'] for d in db_list): loop.stop() print('Cannot find DB named {0} in influx, create please.'.format(gn_conn.config['influxcoll']['influxdb_name'])) return db_client.switch_database(gn_conn.config['influxcoll']['influxdb_name']) # set up a signal handler for sig in [signal.SIGTERM, signal.SIGINT]: loop.add_signal_handler(sig, lambda: asyncio.ensure_future(gn_conn.shutdown(sig, loop))) # log reopen on SIGHUP loop.add_signal_handler(signal.SIGHUP, lambda: asyncio.ensure_future(gn_conn.log_open())) # fire up the listener and do gnhastly things.. asyncio.ensure_future(gn_conn.gnhastd_listener()) # wire up all the callbacks gn_conn.coll_reg_cb = coll_reg_cb gn_conn.coll_upd_cb = coll_upd_cb # Ask for a device list and begin the madness asyncio.ensure_future(ask_for_devicelist(gn_conn)) return
async def initial_setup(args, uid_prefix, loop): print("This is your first run of the collector, setting up") print("Using gnhast server at {0}:{1}".format(args.server, str(args.port))) try: cf = open(args.conf, 'w') except PermissionError as error: print('ERROR: Cannot open {0} for writing'.format(args.conf)) print('ERROR: {0}'.format(error)) exit(1) print('gnhastd {', file=cf) print(' hostname = "{0}"'.format(args.server), file=cf) print(' port = {0}'.format(str(args.port)), file=cf) print('}', file=cf) print('', file=cf) print('bme680coll {', file=cf) print(' update = {0}'.format(str(args.poll_time)), file=cf) print(' tscale = C', file=cf) print(' i2c_addr = "{0}"'.format(args.address), file=cf) print(' burn_in = {0}'.format(str(args.burn_in)), file=cf) print('}', file=cf) cf.close() print("Wrote initial config file at {0}, connecting to gnhastd".format( args.conf)) gn_conn = gnhast.gnhast(loop, args.conf) await gn_conn.gn_build_client('BME680-{0}'.format(args.address)) print("Connection established, wiring devices") gas_dev = gn_conn.new_device(uid_prefix + 'gas', 'BME680 Gas Sensor', gn_conn.cf_type.index('sensor'), gn_conn.cf_subt.index('number')) gas_dev['rrdname'] = gas_dev['name'].replace(' ', '_')[:20] gas_dev['proto'] = 35 hum_dev = gn_conn.new_device(uid_prefix + 'humid', 'BME680 Humidity Sensor', gn_conn.cf_type.index('sensor'), gn_conn.cf_subt.index('humid')) hum_dev['rrdname'] = hum_dev['name'].replace(' ', '_')[:20] hum_dev['proto'] = 35 temp_dev = gn_conn.new_device(uid_prefix + 'temp', 'BME680 Temperature Sensor', gn_conn.cf_type.index('sensor'), gn_conn.cf_subt.index('temp')) temp_dev['rrdname'] = temp_dev['name'].replace(' ', '_')[:20] temp_dev['proto'] = 35 pres_dev = gn_conn.new_device(uid_prefix + 'pres', 'BME680 Pressure Sensor', gn_conn.cf_type.index('sensor'), gn_conn.cf_subt.index('pressure')) pres_dev['rrdname'] = pres_dev['name'].replace(' ', '_')[:20] pres_dev['proto'] = 35 print("Re-writing config file: {0}".format(args.conf)) gn_conn.write_conf_file(args.conf) print("Disconnecting from gnhastd") await gn_conn.gn_disconnect() print("Config file written.") print("Edit it if needed, then restart collector")
#!/usr/bin/env python from gnhast import gnhast import signal import asyncio from pprint import pprint x = gnhast.gnhast("/usr/src/gnhast/owsrvcoll/test.atlas.conf") x.loop.run_until_complete(x.gn_build_client('foo')) #pprint(x.config) x.write_conf_file("foo") async def fiddle(): msg = 'ldevs\n' x.writer.write(msg.encode()) await asyncio.sleep(5) await x.gn_disconnect() x.loop.create_task(x.gnhastd_listener()) x.loop.create_task(fiddle()) for sig in [signal.SIGTERM, signal.SIGINT]: x.loop.add_signal_handler( sig, lambda: asyncio.ensure_future(x.shutdown(sig, x.loop))) try: x.loop.run_forever() finally: x.loop.close()