ffmap-backend/alfred.py

54 lines
1.2 KiB
Python
Raw Normal View History

import subprocess
import json
2015-03-24 16:49:37 +01:00
def _fetch(data_type):
2015-03-24 17:41:02 +01:00
output = subprocess.check_output(
["alfred-json", "-z", "-f", "json", "-r", str(data_type)])
2015-03-24 16:49:37 +01:00
return json.loads(output.decode("utf-8")).values()
def nodeinfo():
2015-03-24 16:49:37 +01:00
return _fetch(158)
def statistics():
2015-03-24 16:49:37 +01:00
return _fetch(159)
def vis():
2015-03-24 16:49:37 +01:00
return _fetch(160)
def aliases():
2015-03-24 16:49:37 +01:00
alias = {}
for node in nodeinfo():
node_alias = {}
try:
# TODO: better pass lat, lng as a tuple?
2015-03-24 17:41:02 +01:00
node_alias['gps'] = "{lat}\x20{lng}".\
format(lat=node['location']['latitude'],
lng=node['location']['longitude'])
except KeyError:
pass
2015-03-24 16:49:37 +01:00
try:
node_alias['firmware'] = node['software']['firmware']['release']
except KeyError:
pass
try:
node_alias['id'] = node['network']['mac']
except KeyError:
pass
if 'hostname' in node:
node_alias['name'] = node['hostname']
elif 'name' in node:
node_alias['name'] = node['name']
if len(node_alias):
alias[node['network']['mac']] = node_alias
return alias