annotate service/rfid_pn532_py/rfid.py @ 1449:a2bc6602a0db

dockerfile deps Ignore-this: b12d406bc1fee7c07092d273904b8b3d darcs-hash:8b40b2634a6df26e0de4f6ef31c6ef2aabf14c31
author drewp <drewp@bigasterisk.com>
date Wed, 25 Sep 2019 17:25:19 -0700
parents b4004739640e
children a93fbf0d0daa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
c8ac652d37b7 switch to uart
drewp <drewp@bigasterisk.com>
parents: 1236
diff changeset
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
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
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
cce999b5c840 more error handling
drewp <drewp@bigasterisk.com>
parents: 1237
diff changeset
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
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
17 from export_to_influxdb import InfluxExporter
1238
cce999b5c840 more error handling
drewp <drewp@bigasterisk.com>
parents: 1237
diff changeset
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
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
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
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
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
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
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
1279
b4004739640e fix hang-at-startup bug
drewp <drewp@bigasterisk.com>
parents: 1240
diff changeset
103
b4004739640e fix hang-at-startup bug
drewp <drewp@bigasterisk.com>
parents: 1240
diff changeset
104 # now=False avoids a serious bug where the first read error
b4004739640e fix hang-at-startup bug
drewp <drewp@bigasterisk.com>
parents: 1240
diff changeset
105 # could happen before reactor.run() is called, and then the
b4004739640e fix hang-at-startup bug
drewp <drewp@bigasterisk.com>
parents: 1240
diff changeset
106 # error fails to crash the reactor and get us restarted.
b4004739640e fix hang-at-startup bug
drewp <drewp@bigasterisk.com>
parents: 1240
diff changeset
107 task.LoopingCall(self.poll).start(self.pollPeriodSecs, now=False)
1215
b48f1ecbc078 copy rest of rfid service from the first try. fix some crashes in tags.py
drewp <drewp@bigasterisk.com>
parents:
diff changeset
108
b48f1ecbc078 copy rest of rfid service from the first try. fix some crashes in tags.py
drewp <drewp@bigasterisk.com>
parents:
diff changeset
109 @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
110 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
111 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
112
b48f1ecbc078 copy rest of rfid service from the first try. fix some crashes in tags.py
drewp <drewp@bigasterisk.com>
parents:
diff changeset
113 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
114
1235
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
115 try:
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
116 for tag in self.reader.getTags(): # blocks for a bit
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
117 uid = tag.uid()
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
118 log.debug('detected tag uid=%r', uid)
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
119 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
120
1235
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
121 is_new = cardIdUri not in self.log
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
122 self.log[cardIdUri] = now
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
123 if is_new:
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
124 STATS.newCardReads += 1
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
125 tag.connect()
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
126 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
127 textLit = Literal(tag.readBlock(1).rstrip('\x00'))
1235
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
128 if self.overwrite_any_tag and not looksLikeBigasterisk(textLit):
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
129 log.info("block 1 was %r; rewriting it", textLit)
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
130 tag.writeBlock(1, randomBody())
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
131 textLit = Literal(tag.readBlock(1).rstrip('\x00'))
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
132 finally:
1238
cce999b5c840 more error handling
drewp <drewp@bigasterisk.com>
parents: 1237
diff changeset
133 # This might not be appropriate to call after
cce999b5c840 more error handling
drewp <drewp@bigasterisk.com>
parents: 1237
diff changeset
134 # readBlock fails. I am getting double
cce999b5c840 more error handling
drewp <drewp@bigasterisk.com>
parents: 1237
diff changeset
135 # exceptions.
1235
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
136 tag.disconnect()
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
137 self.startCardRead(cardIdUri, textLit)
1238
cce999b5c840 more error handling
drewp <drewp@bigasterisk.com>
parents: 1237
diff changeset
138 except AuthFailedError as e:
cce999b5c840 more error handling
drewp <drewp@bigasterisk.com>
parents: 1237
diff changeset
139 log.error(e)
cce999b5c840 more error handling
drewp <drewp@bigasterisk.com>
parents: 1237
diff changeset
140 except (NfcError, OSError) as e:
cce999b5c840 more error handling
drewp <drewp@bigasterisk.com>
parents: 1237
diff changeset
141 traceback.print_exc()
1235
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
142 log.error(e)
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
143 reactor.stop()
1279
b4004739640e fix hang-at-startup bug
drewp <drewp@bigasterisk.com>
parents: 1240
diff changeset
144
1215
b48f1ecbc078 copy rest of rfid service from the first try. fix some crashes in tags.py
drewp <drewp@bigasterisk.com>
parents:
diff changeset
145 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
146 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
147 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
148 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
149 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
150
b48f1ecbc078 copy rest of rfid service from the first try. fix some crashes in tags.py
drewp <drewp@bigasterisk.com>
parents:
diff changeset
151 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
152 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
153 (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
154 (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
155 delQuads=[]))
1239
0bc41be76de7 log reads in a more n3-like format for easier pasting
drewp <drewp@bigasterisk.com>
parents: 1238
diff changeset
156 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
157 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
158 (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
159
b48f1ecbc078 copy rest of rfid service from the first try. fix some crashes in tags.py
drewp <drewp@bigasterisk.com>
parents:
diff changeset
160 def endCardRead(self, cardUri):
1217
c21f433ddbac less logging
drewp <drewp@bigasterisk.com>
parents: 1215
diff changeset
161 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
162 delQuads = []
b48f1ecbc078 copy rest of rfid service from the first try. fix some crashes in tags.py
drewp <drewp@bigasterisk.com>
parents:
diff changeset
163 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
164 (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
165 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
166 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
167 (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
168 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
169
b48f1ecbc078 copy rest of rfid service from the first try. fix some crashes in tags.py
drewp <drewp@bigasterisk.com>
parents:
diff changeset
170 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
171
b48f1ecbc078 copy rest of rfid service from the first try. fix some crashes in tags.py
drewp <drewp@bigasterisk.com>
parents:
diff changeset
172 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
173 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
174 for s,p,o in oneshot)).encode('utf8')
1236
7364c259de8a update debug host
drewp <drewp@bigasterisk.com>
parents: 1235
diff changeset
175 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
176 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
177 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
178 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
179 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
180 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
181 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
182 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
183 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
184 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
185
b48f1ecbc078 copy rest of rfid service from the first try. fix some crashes in tags.py
drewp <drewp@bigasterisk.com>
parents:
diff changeset
186
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 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
189 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
190 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
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 -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
193 --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
194 -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
195 """)
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.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
197 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
198 enableTwistedLog()
b48f1ecbc078 copy rest of rfid service from the first try. fix some crashes in tags.py
drewp <drewp@bigasterisk.com>
parents:
diff changeset
199 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
200 log.info(f'cyclone {cyclone.__version__}')
1240
3930f997ceeb debug Deferreds in -v mode
drewp <drewp@bigasterisk.com>
parents: 1239
diff changeset
201 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
202
b48f1ecbc078 copy rest of rfid service from the first try. fix some crashes in tags.py
drewp <drewp@bigasterisk.com>
parents:
diff changeset
203 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
204 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
205
1235
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
206 ie=InfluxExporter(Graph())
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
207 ie.exportStats(STATS, ['root.cardReadPoll.count',
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
208 'root.cardReadPoll.95percentile',
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
209 'root.newCardReads',
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
210 ],
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
211 period_secs=10,
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
212 retain_days=7,
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
213 )
d8aa414f21d9 py3, rfid-console rename
drewp <drewp@bigasterisk.com>
parents: 1218
diff changeset
214
1215
b48f1ecbc078 copy rest of rfid service from the first try. fix some crashes in tags.py
drewp <drewp@bigasterisk.com>
parents:
diff changeset
215 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
216
b48f1ecbc078 copy rest of rfid service from the first try. fix some crashes in tags.py
drewp <drewp@bigasterisk.com>
parents:
diff changeset
217 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
218 reactor.listenTCP(port, cyclone.web.Application([
1218
c571a45c944f split console web component
drewp <drewp@bigasterisk.com>
parents: 1217
diff changeset
219 (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
220 {"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
221 (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
222 (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
223 {'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
224 (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
225 (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
226 (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
227 ], 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
228 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
229
b48f1ecbc078 copy rest of rfid service from the first try. fix some crashes in tags.py
drewp <drewp@bigasterisk.com>
parents:
diff changeset
230 reactor.run()