def on_signal(sig, action): if action == mapper.ADDED or action == mapper.MODIFIED: # print('ON_SIGNAL (added or modified)', sig_props(sig)) server.send_command("add_signals", [sig_props(sig)]) elif action == mapper.REMOVED: # print('ON_SIGNAL (removed)', sig_props(sig)) server.send_command("del_signal", sig_props(sig))
def on_link(link, action): if action == mapper.ADDED or action == mapper.MODIFIED: # print('ON_LINK (added or modified)', link_props(link)) server.send_command("add_links", [link_props(link)]) elif action == mapper.REMOVED: # print('ON_LINK (removed)', link_props(link)) server.send_command("del_link", link_props(link))
def on_map(map, action): if action == mapper.ADDED or action == mapper.MODIFIED: # print('ON_MAP', '(added)' if action == mapper.ADDED else '(modified)', map_props(map)) server.send_command("add_maps", [map_props(map)]) elif action == mapper.REMOVED: # print('ON_MAP (removed)', map_props(map)) server.send_command("del_map", map_props(map))
def on_signal(sig, action): if action == mapper.ADDED or action == mapper.MODIFIED: # print 'ON_SIGNAL (added or modified)', sig_props(sig) server.send_command("add_signals", [sig_props(sig)]) elif action == mapper.REMOVED: # print 'ON_SIGNAL (removed)', sig_props(sig) server.send_command("del_signal", sig_props(sig))
def on_link(link, action): if action == mapper.ADDED or action == mapper.MODIFIED: # print 'ON_LINK (added or modified)', link_props(link) server.send_command("add_links", [link_props(link)]) elif action == mapper.REMOVED: # print 'ON_LINK (removed)', link_props(link) server.send_command("del_link", link_props(link))
def on_map(map, action): if action == mapper.ADDED or action == mapper.MODIFIED: # print 'ON_MAP (added or modified)', map_props(map) server.send_command("add_maps", [map_props(map)]) elif action == mapper.REMOVED: # print 'ON_MAP (removed)', map_props(map) server.send_command("del_map", map_props(map))
def select_network(newNetwork): global db if networkInterfaces['active'] == newNetwork: return db.flush(0) networkInterfaces['active'] = newNetwork net = mapper.network(networkInterfaces['active']) db = mapper.database(net) server.send_command("active_network", newNetwork)
def on_device(dev, action): if action == mapper.ADDED or action == mapper.MODIFIED: # print 'ON_DEVICE (added or modified)', dev_props(dev) server.send_command("add_devices", [dev_props(dev)]) elif action == mapper.REMOVED: # print 'ON_DEVICE (removed)', dev_props(dev) server.send_command("del_device", dev_props(dev)) elif action == mapper.EXPIRED: # print 'ON_DEVICE (expired)', dev_props(dev) db.flush()
def on_device(dev, action): if action == mapper.ADDED or action == mapper.MODIFIED: # print('ON_DEVICE (added or modified)', dev_props(dev)) server.send_command("add_devices", [dev_props(dev)]) elif action == mapper.REMOVED: # print('ON_DEVICE (removed)', dev_props(dev)) server.send_command("del_device", dev_props(dev)) elif action == mapper.EXPIRED: # print('ON_DEVICE (expired)', dev_props(dev)) db.flush()
def get_networks(arg): location = netifaces.AF_INET # A computer specific integer for internet addresses totalInterfaces = netifaces.interfaces() # A list of all possible interfaces connectedInterfaces = [] for i in totalInterfaces: addrs = netifaces.ifaddresses(i) if location in addrs: # Test to see if the interface is actually connected connectedInterfaces.append(i) server.send_command("available_networks", connectedInterfaces) networkInterfaces['available'] = connectedInterfaces server.send_command("active_network", networkInterfaces['active'])
def on_connection(con, action): if action == mapper.MDB_NEW: server.send_command("new_connection", con) if action == mapper.MDB_MODIFY: server.send_command("mod_connection", con) if action == mapper.MDB_REMOVE: server.send_command("del_connection", con)
def on_device(dev, action): if action == mapper.MDB_NEW: server.send_command("new_device", dev) if action == mapper.MDB_MODIFY: server.send_command("mod_device", dev) if action == mapper.MDB_REMOVE: server.send_command("del_device", dev)
def on_signal(sig, action): if action == mapper.MDB_NEW: server.send_command("new_signal", sig) if action == mapper.MDB_MODIFY: server.send_command("mod_signal", sig) if action == mapper.MDB_REMOVE: server.send_command("del_signal", sig)
def on_link(link, action): if action == mapper.MDB_NEW: server.send_command("new_link", link) if action == mapper.MDB_MODIFY: server.send_command("mod_link", link) if action == mapper.MDB_REMOVE: server.send_command("del_link", link)
def init_database(arg): global db global initialized if initialized == False: db.subscribe(mapper.OBJ_DEVICES | mapper.OBJ_LINKS) db.add_device_callback(on_device) db.add_link_callback(on_link) db.add_signal_callback(on_signal) db.add_map_callback(on_map) initialized = True else: server.send_command("add_devices", map(dev_props, db.devices())) server.send_command("add_links", map(link_props, db.links())) server.send_command("add_signals", map(sig_props, db.signals())) server.send_command("add_maps", map(map_props, db.maps()))
def init_database(arg): global db global initialized if initialized == False: db.subscribe(mapper.OBJ_DEVICES | mapper.OBJ_LINKS) db.add_device_callback(on_device) db.add_link_callback(on_link) db.add_signal_callback(on_signal) db.add_map_callback(on_map) initialized = True else: server.send_command("add_devices", [dev_props(d) for d in db.devices()]) server.send_command("add_links", [link_props(l) for l in db.links()]) server.send_command("add_signals", [sig_props(s) for s in db.signals()]) server.send_command("add_maps", [map_props(m) for m in db.maps()])
def init_graph(arg): print('REFRESH!') global g # remove old callbacks (if they are registered) g.remove_callback(on_device) g.remove_callback(on_signal) g.remove_callback(on_map) # register callbacks g.add_callback(on_device, mpr.Type.DEVICE) g.add_callback(on_signal, mpr.Type.SIGNAL) g.add_callback(on_map, mpr.Type.MAP) # (re)subscribe: currently this does nothing but could refresh graph database? g.subscribe(None, mpr.Type.OBJECT, -1) for d in g.devices(): server.send_command("add_devices", [dev_props(d)]) for s in g.signals(): server.send_command("add_signals", [sig_props(s)]) for m in g.maps(): server.send_command("add_maps", [map_props(m)])
def select_network(newNetwork): # print 'select_network', newNetwork networkInterfaces['active'] = newNetwork server.send_command('set_network', newNetwork)
def get_active_network(arg): print networkInterfaces['active'] server.send_command("active_network", networkInterfaces['active'])
def select_interface(iface): print('switching interface to', iface) global g g.set_interface(iface) networkInterfaces['active'] = iface server.send_command("active_interface", iface)
def poll_and_push(): global g g.poll(50) if len(new_devs) > 0: server.send_command("add_devices", list(new_devs.values())) new_devs.clear() if len(del_devs) > 0: server.send_command("del_devices", list(del_devs.values())) del_devs.clear() if len(new_sigs) > 0: server.send_command("add_signals", list(new_sigs.values())) new_sigs.clear() if len(del_sigs) > 0: server.send_command("del_signals", list(del_sigs.values())) del_sigs.clear() if len(new_maps) > 0: server.send_command("add_maps", list(new_maps.values())) new_maps.clear() if len(del_maps) > 0: server.send_command("del_maps", list(del_maps.values())) del_maps.clear()