annotate service/wifi/wifi.py @ 1223:34de6cfa0b6b

rename historical 'tomatoWifi' Ignore-this: 8a3f1f261df50e8029cf9de5b11a6896 darcs-hash:1b2345513349fd24e5b3992141f0b0f72ad43910
author drewp <drewp@bigasterisk.com>
date Sat, 30 Mar 2019 16:58:08 -0700
parents
children e1202af42d4d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1223
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
1 import re, ast, logging, socket, json
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
2 import lxml.html.soupparser
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
3 from twisted.internet.defer import inlineCallbacks, returnValue
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
4 from cyclone.httpclient import fetch
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
5 from rdflib import Literal, Graph, RDFS, URIRef
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
6
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
7 log = logging.getLogger()
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
8
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
9 class Router(object):
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
10 def __repr__(self):
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
11 return repr(self.__dict__)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
12
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
13 class Wifi(object):
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
14 """
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
15 gather the users of wifi from the tomato routers
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
16 """
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
17 def __init__(self, accessN3="/my/proj/openid_proxy/access.n3"):
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
18 self.rereadConfig()
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
19 #self._loadRouters(accessN3, tomatoUrl)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
20
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
21 def rereadConfig(self):
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
22 self.graph = Graph()
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
23 self.graph.parse('config.n3', format='n3')
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
24
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
25
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
26 def _loadRouters(self, accessN3, tomatoUrl):
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
27 g = Graph()
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
28 g.parse(accessN3, format="n3")
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
29 repl = {
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
30 '/wifiRouter1/' : None,
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
31 #'/tomato2/' : None
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
32 }
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
33 for k in repl:
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
34 rows = list(g.query('''
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
35 PREFIX p: <http://bigasterisk.com/openid_proxy#>
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
36 SELECT ?prefix WHERE {
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
37 ?site
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
38 p:requestPrefix ?public;
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
39 p:proxyUrlPrefix ?prefix
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
40 .
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
41 }''', initBindings={"public" : Literal(k)}))
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
42 repl[k] = str(rows[0][0])
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
43 log.debug('repl %r', repl)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
44
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
45 self.routers = []
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
46 for url in tomatoUrl:
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
47 name = url
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
48 for k, v in repl.items():
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
49 url = url.replace(k, v)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
50
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
51 r = Router()
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
52 http, tail = url.split('//', 1)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
53 userPass, tail = tail.split("@", 1)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
54 r.url = http + '//' + tail
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
55 r.headers = {'Authorization': ['Basic %s' % userPass.encode('base64').strip()]}
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
56 r.name = {'wifiRouter1' : 'bigasterisk5',
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
57 'tomato2' : 'bigasterisk4'}[name.split('/')[1]]
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
58 self.routers.append(r)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
59
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
60 @inlineCallbacks
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
61 def getPresentMacAddrs(self):
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
62 self.rereadConfig()
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
63 rows = yield loadOrbiData()
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
64 for row in rows:
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
65 if 'clientHostname' in row:
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
66 row['name'] = row['clientHostname']
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
67 mac = URIRef('http://bigasterisk.com/mac/%s' % row['mac'].lower())
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
68 label = self.graph.value(mac, RDFS.label)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
69 if label:
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
70 row['name'] = label
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
71 returnValue(rows)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
72
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
73 @inlineCallbacks
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
74 def getPresentMacAddrs_multirouter(self):
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
75 rows = []
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
76
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
77 for router in self.routers:
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
78 log.debug("GET %s", router)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
79 try:
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
80 resp = yield fetch(router.url, headers=router.headers,
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
81 timeout=2)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
82 except socket.error:
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
83 log.warn("get on %s failed" % router)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
84 continue
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
85 data = resp.body
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
86 if 'Wireless -- Authenticated Stations' in data:
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
87 # zyxel 'Station Info' page
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
88 rows.extend(self._parseZyxel(data, router.name))
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
89 else:
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
90 # tomato page
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
91 rows.extend(self._parseTomato(data, router.name))
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
92
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
93 for r in rows:
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
94 try:
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
95 r['name'] = self.knownMacAddr[r['mac']]
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
96 except KeyError:
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
97 pass
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
98
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
99 returnValue(rows)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
100
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
101 def _parseZyxel(self, data, routerName):
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
102 root = lxml.html.soupparser.fromstring(data)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
103 for tr in root.cssselect('tr'):
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
104 mac, assoc, uth, ssid, iface = [td.text_content().strip() for td in tr.getchildren()]
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
105 if mac == "MAC":
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
106 continue
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
107 assoc = assoc.lower() == 'yes'
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
108 yield dict(router=routerName, mac=mac, assoc=assoc, connected=assoc)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
109
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
110 def _parseTomato(self, data, routerName):
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
111 for iface, mac, signal in jsValue(data, 'wldev'):
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
112 yield dict(router=routerName, mac=mac, signal=signal, connected=bool(signal))
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
113
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
114
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
115 @inlineCallbacks
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
116 def loadUvaData():
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
117 config = json.load(open("priv-uva.json"))
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
118 headers = {'Authorization': ['Basic %s' % config['userPass'].encode('base64').strip()]}
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
119 resp = yield fetch('http://10.2.0.2/wlstationlist.cmd', headers=headers)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
120 root = lxml.html.soupparser.fromstring(resp.body)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
121 byMac = {}
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
122 for tr in root.cssselect('tr'):
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
123 mac, connected, auth, ssid, iface = [td.text_content().strip() for td in tr.getchildren()]
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
124 if mac == "MAC":
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
125 continue
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
126 connected = connected.lower() == 'yes'
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
127 byMac[mac] = dict(mac=mac, connected=connected, auth=auth == 'Yes', ssid=ssid, iface=iface)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
128
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
129 resp = yield fetch('http://10.2.0.2/DHCPTable.asp', headers=headers)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
130 for row in re.findall(r'new AAA\((.*)\)', resp.body):
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
131 clientHostname, ipaddr, mac, expires, iface = [s.strip("'") for s in row.rsplit(',', 4)]
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
132 if clientHostname == 'wlanadv.none':
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
133 continue
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
134 byMac.setdefault(mac, {}).update(dict(
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
135 clientHostname=clientHostname, connection=iface, ipaddr=ipaddr, dhcpExpires=expires))
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
136
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
137 returnValue(sorted(byMac.values()))
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
138
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
139 @inlineCallbacks
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
140 def loadCiscoData():
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
141 config = json.load(open("priv-uva.json"))
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
142 headers = {'Authorization': ['Basic %s' % config['userPass'].encode('base64').strip()]}
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
143 print headers
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
144 resp = yield fetch('http://10.2.0.2/', headers=headers)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
145 print resp.body
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
146 returnValue([])
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
147
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
148 @inlineCallbacks
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
149 def loadOrbiData():
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
150 config = json.load(open("priv-uva.json"))
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
151 headers = {'Authorization': ['Basic %s' % config['userPass'].encode('base64').strip()]}
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
152 resp = yield fetch('http://orbi.bigasterisk.com/DEV_device_info.htm', headers=headers)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
153
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
154 if not resp.body.startswith(('device=', 'device_changed=0\ndevice=', 'device_changed=1\ndevice=')):
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
155 raise ValueError(resp.body)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
156
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
157 ret = []
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
158 for row in json.loads(resp.body.split('device=', 1)[-1]):
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
159 ret.append(dict(
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
160 connected=True,
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
161 ipaddr=row['ip'],
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
162 mac=row['mac'].lower(),
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
163 contype=row['contype'],
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
164 model=row['model'],
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
165 clientHostname=row['name'] if row['name'] != 'Unknown' else None))
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
166 returnValue(ret)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
167
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
168
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
169 def jsValue(js, variableName):
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
170 # using literal_eval instead of json parser to handle the trailing commas
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
171 val = re.search(variableName + r'\s*=\s*(.*?);', js, re.DOTALL).group(1)
34de6cfa0b6b rename historical 'tomatoWifi'
drewp <drewp@bigasterisk.com>
parents:
diff changeset
172 return ast.literal_eval(val)