def main(config): graphite_server = config.get("graphite", "server") graphite_prefix = config.get("graphite", "prefix") print "Initializing graphiteudp, server: {}, prefix: {}".format(graphite_server, graphite_prefix) graphiteudp.init(graphite_server, prefix=graphite_prefix) dbfile = os.path.join(os.environ.get('HOME'), 'scans.sqlite3') dbfile = 'scans.sqlite3' need_schema = False if not os.path.exists(dbfile): need_schema = True db = sqlite3.connect(dbfile) if need_schema: db.execute('create table scans(barcode text, timestamp datetime, event_id text)') dev = [InputDevice(device) for device in list_devices() if InputDevice(device).name == scanner_name][0] def signal_handler(signal, frame): print 'Stopping' dev.ungrab() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) dev.grab() barcode = "" last_event_time = 0 last_event_id = None for event in dev.read_loop(): if event.type == ecodes.EV_KEY: data = categorize(event) # Catch only keydown, and not Enter if data.keystate == 1 and data.scancode != 42: if data.scancode == 28: timestamp = time.time() if timestamp - last_event_time < 10: event_id = last_event_id else: event_id = uuid.uuid1() last_event_id = event_id last_event_time = timestamp graphiteudp.send('event.scan', 1) db.execute( 'insert into scans (barcode, timestamp, event_id) ' + 'values (:barcode, :timestamp, :event_id)', [barcode, timestamp, event_id.get_urn()]) db.commit() barcode = "" else: try: barcode += scancodes[data.scancode] except KeyError: print >>sys.stderr, "Unknown scancode: {0}".format(data.scancode)
def main(config): """Initializes the scanner and runs the main event loop. :param config: Scanner configuration object """ init_graphite(config.get("graphite", "server"), config.get("graphite", "prefix")) connection = init_database() dev = get_input_device() def signal_handler(incoming_signal, dataframe): # pylint: disable=unused-argument """Handle SIGINTs :param incoming_signal: The signal :param dataframe: The data associated with the signal. """ print 'Stopping' dev.ungrab() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) dev.grab() barcode = "" last_event_time = 0 last_event_id = None for event in dev.read_loop(): if event.type == ecodes.EV_KEY: data = categorize(event) # Catch only keydown, and not Enter if data.keystate == 1 and data.scancode != 42: if data.scancode == 28: timestamp = time.time() if timestamp - last_event_time < 10: event_id = last_event_id else: event_id = uuid.uuid1() last_event_id = event_id last_event_time = timestamp graphiteudp.send('event.scan', 1) connection.execute( 'insert into scans (barcode, timestamp, event_id) ' + 'values (:barcode, :timestamp, :event_id)', [barcode, timestamp, event_id.get_urn()]) connection.commit() barcode = "" else: try: barcode += SCANCODES[data.scancode] except KeyError: print >>sys.stderr, "Unknown scancode: {0}".format(data.scancode)
import sys import logging sys.path.insert(0, "../src/") import graphiteudp graphiteudp.send("foo", 1.2) logger = logging.getLogger() logger.setLevel(logging.DEBUG) handler = logging.StreamHandler() # Writes to stderr by default handler.setFormatter(logging.Formatter('%(process)d %(name)s %(levelname)s %(message)s')) logger.addHandler(handler) graphiteudp.init(debug = True) graphiteudp.send("foo2", 1.2) graphiteudp.init("example.com") graphiteudp.send("foo3", 1.2) graphiteudp.init("example.com", 2005) graphiteudp.send("foo3", 1.2) graphiteudp.init("example.com", 2005, prefix = "bzrt") graphiteudp.send("foo4", 1.2, 100)
import sys import logging sys.path.insert(0, "../src/") import graphiteudp graphiteudp.send("foo", 1.2) logger = logging.getLogger() logger.setLevel(logging.DEBUG) handler = logging.StreamHandler() # Writes to stderr by default handler.setFormatter( logging.Formatter('%(process)d %(name)s %(levelname)s %(message)s')) logger.addHandler(handler) graphiteudp.init(debug=True) graphiteudp.send("foo2", 1.2) graphiteudp.init("example.com") graphiteudp.send("foo3", 1.2) graphiteudp.init("example.com", 2005) graphiteudp.send("foo3", 1.2) graphiteudp.init("example.com", 2005, prefix="bzrt") graphiteudp.send("foo4", 1.2, 100)
def send(self, name, val): graphiteudp.send(name, val)
hvac_fan_on = n.status['shared'][n.serial]['hvac_fan_state'] while True: try: data = bus.read_i2c_block_data(0x48, 0) except IOError: logging.debug("Error getting temperature. Retrying...") else: break msb = data[0] lsb = data[1] current_temperature_c = (((msb << 8) | lsb) >> 4) * 0.0625 current_temperature = current_temperature_c * 1.8 + 32.0 logging.debug("Temperature: %s F (%s C)", current_temperature, current_temperature_c) graphiteudp.send("closet.temperature.f",current_temperature) graphiteudp.send("closet.temperature.c",current_temperature_c) if away: logging.debug("Auto away enabled. Turning on fan.") fan.turn_on() elif hvac_fan_on: logging.debug("HVAC system running. Turning on fan.") fan.turn_on() elif current_temperature > cfg.max_temperature: logging.debug("Current Temperature is over max threshold. Turning on fan.") fan.turn_on() else: logging.debug("Nothing going on. Turning off fan.") fan.turn_off() logging.debug("Sleeping for %s seconds.",cfg.timeout)
def graphite_metric_task(name, num, **kwargs): if graphiteudp is None: raise ImproperlyConfigured("You must install 'graphiteudp' in order to use this backend.") graphiteudp.send(name, num)
def graphite_metric_task(name, num, **kwargs): if graphiteudp is None: raise ImproperlyConfigured( "You must install 'graphiteudp' in order to use this backend.") graphiteudp.send(name, num)
while True: try: data = bus.read_i2c_block_data(0x48, 0) except IOError: logging.debug("Error getting temperature. Retrying...") else: break msb = data[0] lsb = data[1] current_temperature_c = (((msb << 8) | lsb) >> 4) * 0.0625 current_temperature = current_temperature_c * 1.8 + 32.0 logging.debug("Temperature: %s F (%s C)", current_temperature, current_temperature_c) graphiteudp.send("closet.temperature.f", current_temperature) graphiteudp.send("closet.temperature.c", current_temperature_c) if away: logging.debug("Auto away enabled. Turning on fan.") fan.turn_on() elif hvac_fan_on: logging.debug("HVAC system running. Turning on fan.") fan.turn_on() elif current_temperature > cfg.max_temperature: logging.debug( "Current Temperature is over max threshold. Turning on fan.") fan.turn_on() else: logging.debug("Nothing going on. Turning off fan.") fan.turn_off()