def index(): data = phatsniffer.get_sniffer_data() data_beacons = data['beacons'] data_clients = data['clients'] beacons = sorted(data_beacons.iteritems(), key=lambda x: -x[1]['rssi']) clients = sorted(data_clients.iteritems(), key=lambda x: -x[1]['rssi']) beacon_clients = {} app.logger.info(beacons) for beacon in data_beacons: beacon_clients[beacon] = [] for client in data_clients: beacon = data_clients[client]['beacon'] if beacon in data_beacons: if beacon not in beacon_clients: beacon_clients[beacon] = [] beacon_clients[beacon].append(client) circles = {} circles['name'] = 'root' circles['children'] = [] circles_beacons = circles['children'] for beacon in beacon_clients: data_beacon = data_beacons[beacon] circles_beacon = {} if 'vendor' in data_beacon: circles_beacon['name'] = data_beacon['ssid'] else: circles_beacon['name'] = 'Unknown' if len(beacon_clients[beacon]) == 0: if data_beacon['rssi'] > -99: circles_beacon['size'] = 2 * (math.sqrt(100 + data_beacon['rssi'])) else: circles_beacon['size'] = 1 else: circles_beacon['children'] = [] circles_clients = circles_beacon['children'] for client in beacon_clients[beacon]: data_client = data_clients[client] circles_client = {} if 'vendor' in data_client: circles_client['name'] = data_client['vendor'] else: circles_client['name'] = 'Unknown' if data_client['rssi'] > -99: circles_client['size'] = 2 * ( math.sqrt(100 + data_client['rssi'])) else: data_client['size'] = 1 circles_clients.append(circles_client) circles_beacons.append(circles_beacon) return render_template('index.html', beacons=beacons, clients=clients, circles=json.dumps(circles), circles2=json.dumps(circles))
#!/usr/bin/python from paho.mqtt import publish import json, time import phatsniffer def publish_sniffer_data(hostname, root, data): messages = [] if 'beacons' in data: for beacon, beacon_data in data['beacons'].items(): messages.append(('%s/beacons/%s' % (root, beacon), json.dumps(beacon_data, sort_keys=True), 0, True)) if 'clients' in data: for client, client_data in data['clients'].items(): messages.append(('%s/clients/%s' % (root, client), json.dumps(client_data, sort_keys=True), 0, True)) publish.multiple(messages, hostname=hostname) if __name__ == '__main__': hostname = 'iot.eclipse.org' root = 'phatsniffer' phatsniffer.read_vendors('data/vendors.tsv') while True: publish_sniffer_data(hostname, root, phatsniffer.get_sniffer_data()) time.sleep(60)
def download(): return jsonify(phatsniffer.get_sniffer_data())