Mercurial > code > home > repos > homeauto
annotate service/rfid_pn532_py/rfid.py @ 434:2409446ccfa2
switch to uart
Ignore-this: b5285d12100bc182b970ea392f48e259
author | drewp@bigasterisk.com |
---|---|
date | Tue, 09 Apr 2019 09:05:42 -0700 |
parents | d7ede2330b6d |
children | 80e403e107b1 |
rev | line source |
---|---|
412
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
1 import os |
434 | 2 os.environ['LIBNFC_DEFAULT_DEVICE'] = "pn532_uart:/dev/ttyUSB0" |
412
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
3 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
4 from docopt import docopt |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
5 from rdfdb.patch import Patch |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
6 from patchablegraph import PatchableGraph, CycloneGraphHandler, CycloneGraphEventsHandler |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
7 from rdflib import Namespace, URIRef, Literal, Graph |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
8 from rdflib.parser import StringInputSource |
432 | 9 from twisted.internet import reactor, task, defer |
412
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
10 import cyclone.web |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
11 from cyclone.httpclient import fetch |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
12 import cyclone |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
13 import logging, time, json, random, string |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
14 from logsetup import log, enableTwistedLog |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
15 from greplin import scales |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
16 from greplin.scales.cyclonehandler import StatsHandler |
432 | 17 from export_to_influxdb import InfluxExporter |
412
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
18 from tags import NfcDevice, FakeNfc |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
19 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
20 ROOM = Namespace('http://projects.bigasterisk.com/room/') |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
21 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
22 ctx = ROOM['frontDoorWindowRfidCtx'] |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
23 |
432 | 24 STATS = scales.collection('/root', |
412
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
25 scales.PmfStat('cardReadPoll'), |
432 | 26 scales.IntStat('newCardReads'), |
412
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
27 ) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
28 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
29 class OutputPage(cyclone.web.RequestHandler): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
30 def put(self): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
31 arg = self.request.arguments |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
32 if arg.get('s') and arg.get('p'): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
33 self._onQueryStringStatement(arg['s'][-1], arg['p'][-1], self.request.body) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
34 else: |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
35 self._onGraphBodyStatements(self.request.body, self.request.headers) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
36 post = put |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
37 def _onQueryStringStatement(self, s, p, body): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
38 subj = URIRef(s) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
39 pred = URIRef(p) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
40 turtleLiteral = self.request.body |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
41 try: |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
42 obj = Literal(float(turtleLiteral)) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
43 except ValueError: |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
44 obj = Literal(turtleLiteral) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
45 self._onStatements([(subj, pred, obj)]) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
46 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
47 def _onGraphBodyStatements(self, body, headers): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
48 g = Graph() |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
49 g.parse(StringInputSource(body), format='nt') |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
50 if not g: |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
51 raise ValueError("expected graph body") |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
52 self._onStatements(list(g.triples((None, None, None)))) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
53 post = put |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
54 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
55 def _onStatements(self, stmts): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
56 # write rfid to new key, etc. |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
57 if len(stmts) > 0 and stmts[0][1] == ROOM['keyContents']: |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
58 return |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
59 log.warn("ignoring %s", stmts) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
60 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
61 def uidUri(card_id): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
62 return URIRef('http://bigasterisk.com/rfidCard/%s' % card_id) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
63 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
64 BODY_VERSION = "1" |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
65 def randomBody(): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
66 return BODY_VERSION + '*' + ''.join(random.choice(string.ascii_uppercase) for n in range(16 - 2)) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
67 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
68 def looksLikeBigasterisk(text): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
69 return text.startswith(BODY_VERSION + "*") |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
70 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
71 class Rewrite(cyclone.web.RequestHandler): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
72 def post(self): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
73 agent = URIRef(self.request.headers['x-foaf-agent']) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
74 body = json.loads(self.request.body) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
75 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
76 _, uid = reader.read_id() |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
77 log.info('current card id: %r %r', _, uid) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
78 if uid is None: |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
79 self.set_status(404, "no card present") |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
80 # maybe retry a few more times since the card might be nearby |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
81 return |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
82 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
83 text = randomBody() |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
84 log.info('%s rewrites %s to %s, to be owned by %s', |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
85 agent, uid, text, body['user']) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
86 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
87 #reader.KEY = private.rfid_key |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
88 reader.write(uid, text) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
89 log.info('done with write') |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
90 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
91 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
92 sensor = ROOM['frontDoorWindowRfid'] |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
93 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
94 class ReadLoop(object): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
95 def __init__(self, reader, masterGraph, overwrite_any_tag): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
96 self.reader = reader |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
97 self.masterGraph = masterGraph |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
98 self.overwrite_any_tag = overwrite_any_tag |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
99 self.log = {} # cardIdUri : most recent seentime |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
100 |
432 | 101 self.pollPeriodSecs = .1 |
412
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
102 self.expireSecs = 5 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
103 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
104 task.LoopingCall(self.poll).start(self.pollPeriodSecs) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
105 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
106 @STATS.cardReadPoll.time() |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
107 def poll(self): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
108 now = time.time() |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
109 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
110 self.flushOldReads(now) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
111 |
432 | 112 try: |
113 for tag in self.reader.getTags(): # blocks for a bit | |
114 uid = tag.uid() | |
115 log.debug('detected tag uid=%r', uid) | |
116 cardIdUri = uidUri(uid) | |
412
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
117 |
432 | 118 is_new = cardIdUri not in self.log |
119 self.log[cardIdUri] = now | |
120 if is_new: | |
121 STATS.newCardReads += 1 | |
122 tag.connect() | |
123 try: | |
412
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
124 textLit = Literal(tag.readBlock(1).rstrip('\x00')) |
432 | 125 if self.overwrite_any_tag and not looksLikeBigasterisk(textLit): |
126 log.info("block 1 was %r; rewriting it", textLit) | |
127 tag.writeBlock(1, randomBody()) | |
128 textLit = Literal(tag.readBlock(1).rstrip('\x00')) | |
129 finally: | |
130 tag.disconnect() | |
131 self.startCardRead(cardIdUri, textLit) | |
132 except OSError as e: | |
133 log.error(e) | |
134 reactor.stop() | |
412
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
135 def flushOldReads(self, now): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
136 for uri in list(self.log): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
137 if self.log[uri] < now - self.expireSecs: |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
138 self.endCardRead(uri) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
139 del self.log[uri] |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
140 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
141 def startCardRead(self, cardUri, text): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
142 self.masterGraph.patch(Patch(addQuads=[ |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
143 (sensor, ROOM['reading'], cardUri, ctx), |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
144 (cardUri, ROOM['cardText'], text, ctx)], |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
145 delQuads=[])) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
146 log.info('read card at id=%s %r', cardUri, str(text)) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
147 self._sendOneshot([(sensor, ROOM['startReading'], cardUri), |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
148 (cardUri, ROOM['cardText'], text)]) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
149 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
150 def endCardRead(self, cardUri): |
414 | 151 log.debug(f'{cardUri} has been gone for {self.expireSecs} sec') |
412
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
152 delQuads = [] |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
153 for spo in self.masterGraph._graph.triples( |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
154 (sensor, ROOM['reading'], cardUri)): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
155 delQuads.append(spo + (ctx,)) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
156 for spo in self.masterGraph._graph.triples( |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
157 (cardUri, ROOM['cardText'], None)): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
158 delQuads.append(spo + (ctx,)) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
159 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
160 self.masterGraph.patch(Patch(addQuads=[], delQuads=delQuads)) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
161 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
162 def _sendOneshot(self, oneshot): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
163 body = (' '.join('%s %s %s .' % (s.n3(), p.n3(), o.n3()) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
164 for s,p,o in oneshot)).encode('utf8') |
433 | 165 url = b'http://bang:9071/oneShot' |
412
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
166 d = fetch(method=b'POST', |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
167 url=url, |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
168 headers={b'Content-Type': [b'text/n3']}, |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
169 postdata=body, |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
170 timeout=5) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
171 def err(e): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
172 log.info('oneshot post to %r failed: %s', |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
173 url, e.getErrorMessage()) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
174 d.addErrback(err) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
175 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
176 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
177 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
178 if __name__ == '__main__': |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
179 arg = docopt(""" |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
180 Usage: rfid.py [options] |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
181 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
182 -v Verbose |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
183 --overwrite_any_tag Rewrite any unknown tag with a new random body |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
184 -n Fake reader |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
185 """) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
186 log.setLevel(logging.INFO) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
187 if arg['-v']: |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
188 enableTwistedLog() |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
189 log.setLevel(logging.DEBUG) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
190 log.info(f'cyclone {cyclone.__version__}') |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
191 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
192 masterGraph = PatchableGraph() |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
193 reader = NfcDevice() if not arg['-n'] else FakeNfc() |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
194 |
432 | 195 ie=InfluxExporter(Graph()) |
196 ie.exportStats(STATS, ['root.cardReadPoll.count', | |
197 'root.cardReadPoll.95percentile', | |
198 'root.newCardReads', | |
199 ], | |
200 period_secs=10, | |
201 retain_days=7, | |
202 ) | |
203 | |
412
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
204 loop = ReadLoop(reader, masterGraph, overwrite_any_tag=arg['--overwrite_any_tag']) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
205 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
206 port = 10012 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
207 reactor.listenTCP(port, cyclone.web.Application([ |
415 | 208 (r"/(|.+\.html)", cyclone.web.StaticFileHandler, |
412
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
209 {"path": ".", "default_filename": "index.html"}), |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
210 (r"/graph", CycloneGraphHandler, {'masterGraph': masterGraph}), |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
211 (r"/graph/events", CycloneGraphEventsHandler, |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
212 {'masterGraph': masterGraph}), |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
213 (r'/output', OutputPage), |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
214 (r'/rewrite', Rewrite), |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
215 (r'/stats/(.*)', StatsHandler, {'serverName': 'rfid'}), |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
216 ], masterGraph=masterGraph, debug=arg['-v']), interface='::') |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
217 log.warn('serving on %s', port) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
218 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
219 reactor.run() |