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