Mercurial > code > home > repos > homeauto
comparison service/wifi/scrape.py @ 1368:140d8d419cf0
redo wifi's statements about access points so we can distinguish who is connecting to where, and on which wifi band
Ignore-this: 9f629f8f5ccc2e08e6c61c80844c341a
darcs-hash:800b14d56d67efbefd1332896a8ed5c8246bad38
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Fri, 03 May 2019 18:54:42 -0700 |
parents | ec0db53aa833 |
children | 4afb1830bb5e |
comparison
equal
deleted
inserted
replaced
1367:b287950fbcf4 | 1368:140d8d419cf0 |
---|---|
1 import logging, json, base64 | 1 import logging, json, base64 |
2 from typing import List | 2 from typing import List, Iterable |
3 | 3 |
4 from cyclone.httpclient import fetch | 4 from cyclone.httpclient import fetch |
5 from rdflib import Literal, Graph, RDF, URIRef, Namespace | 5 from rdflib import Literal, Graph, RDF, URIRef, Namespace, RDFS |
6 from twisted.internet.defer import inlineCallbacks, returnValue | 6 from twisted.internet.defer import inlineCallbacks, returnValue |
7 | 7 |
8 log = logging.getLogger() | 8 log = logging.getLogger() |
9 ROOM = Namespace("http://projects.bigasterisk.com/room/") | 9 ROOM = Namespace("http://projects.bigasterisk.com/room/") |
10 AST = Namespace("http://bigasterisk.com/") | 10 AST = Namespace("http://bigasterisk.com/") |
11 | 11 |
12 def macUri(macAddress: str) -> URIRef: | 12 def macUri(macAddress: str) -> URIRef: |
13 return URIRef("http://bigasterisk.com/mac/%s" % macAddress.lower()) | 13 return URIRef("http://bigasterisk.com/mac/%s" % macAddress.lower()) |
14 | 14 |
15 class SeenNode(object): | 15 class SeenNode(object): |
16 def __init__(self, uri: URIRef, mac: str, ip: str, pred_objs: List): | 16 def __init__(self, uri: URIRef, mac: str, ip: str, stmts: Iterable): |
17 self.connected = True | 17 self.connected = True |
18 self.uri = uri | 18 self.uri = uri |
19 self.mac = mac | 19 self.mac = mac |
20 self.ip = ip | 20 self.ip = ip |
21 self.stmts = [(uri, p, o) for p, o in pred_objs] | 21 self.stmts = stmts |
22 | 22 |
23 class Wifi(object): | 23 class Wifi(object): |
24 """ | 24 """ |
25 gather the users of wifi from the tomato routers | 25 gather the users of wifi from the tomato routers |
26 """ | 26 """ |
53 | 53 |
54 if not resp.body.startswith((b'device=', | 54 if not resp.body.startswith((b'device=', |
55 b'device_changed=0\ndevice=', | 55 b'device_changed=0\ndevice=', |
56 b'device_changed=1\ndevice=')): | 56 b'device_changed=1\ndevice=')): |
57 raise ValueError(resp.body) | 57 raise ValueError(resp.body) |
58 | 58 |
59 log.debug(resp.body) | 59 log.debug(resp.body) |
60 rows = [] | 60 rows = [] |
61 for row in json.loads(resp.body.split(b'device=', 1)[-1]): | 61 for row in json.loads(resp.body.split(b'device=', 1)[-1]): |
62 extra = [] | 62 triples = set() |
63 extra.append((ROOM['connectedToNetwork'], { | 63 uri = macUri(row['mac'].lower()) |
64 'wireless': AST['wifiAccessPoints'], | 64 |
65 '2.4G': AST['wifiAccessPoints'], | 65 if row['contype'] in ['2.4G', '5G']: |
66 '5G': AST['wifiAccessPoints'], | 66 orbi = macUri(row['conn_orbi_mac']) |
67 '-': AST['wifiUnknownConnectionType'], | 67 triples.add((orbi, ROOM['wifiBand'], |
68 'Unknown': AST['wifiUnknownConnectionType'], | 68 ROOM['wifiBand/%s' % row['contype']])) |
69 'wired': AST['houseOpenNet']}[row['contype']])) | 69 triples.add((uri, ROOM['connectedToAp'], orbi)) |
70 triples.add((orbi, RDF.type, ROOM['AccessPoint'])) | |
71 triples.add((orbi, ROOM['macAddress'], | |
72 Literal(row['conn_orbi_mac'].lower()))) | |
73 triples.add((orbi, RDFS.label, Literal(row['conn_orbi_name']))) | |
74 elif row['contype'] == 'wireless': | |
75 pass | |
76 elif row['contype'] == 'wired': | |
77 pass | |
78 elif row['contype'] == '-': | |
79 pass | |
80 else: | |
81 pass | |
82 triples.add((uri, ROOM['connectedToNet'], ROOM['HouseOpenNet'])) | |
83 | |
70 if row['model'] != 'Unknown': | 84 if row['model'] != 'Unknown': |
71 extra.append((ROOM['networkModel'], Literal(row['model']))) | 85 triples.add((uri, ROOM['networkModel'], Literal(row['model']))) |
72 | 86 |
73 rows.append(SeenNode( | 87 rows.append(SeenNode( |
74 uri=macUri(row['mac'].lower()), | 88 uri=uri, |
75 mac=row['mac'].lower(), | 89 mac=row['mac'].lower(), |
76 ip=row['ip'], | 90 ip=row['ip'], |
77 pred_objs=extra)) | 91 stmts=triples)) |
78 returnValue(rows) | 92 returnValue(rows) |