annotate service/cardReader/rfid.py @ 725:1ecceb2e92a3

rename rfid_pn532_py Ignore-this: 89356f780bb5df4d9ec639c44d2a3a67
author drewp@bigasterisk.com
date Wed, 05 Feb 2020 16:40:46 -0800
parents service/rfid_pn532_py/rfid.py@b87b6e9cedb2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
2409446ccfa2 switch to uart
drewp@bigasterisk.com
parents: 433
diff changeset
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
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
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
435
80e403e107b1 more error handling
drewp@bigasterisk.com
parents: 434
diff changeset
13 import logging, time, json, random, string, traceback
412
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
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
17 from export_to_influxdb import InfluxExporter
435
80e403e107b1 more error handling
drewp@bigasterisk.com
parents: 434
diff changeset
18 from tags import NfcDevice, FakeNfc, NfcError, AuthFailedError
412
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
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
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
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
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)])
723
b87b6e9cedb2 whitespace
drewp@bigasterisk.com
parents: 722
diff changeset
46
412
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
723
b87b6e9cedb2 whitespace
drewp@bigasterisk.com
parents: 722
diff changeset
54
412
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 + "*")
723
b87b6e9cedb2 whitespace
drewp@bigasterisk.com
parents: 722
diff changeset
70
412
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
723
b87b6e9cedb2 whitespace
drewp@bigasterisk.com
parents: 722
diff changeset
82
412
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()
723
b87b6e9cedb2 whitespace
drewp@bigasterisk.com
parents: 722
diff changeset
84 log.info('%s rewrites %s to %s, to be owned by %s',
412
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'])
723
b87b6e9cedb2 whitespace
drewp@bigasterisk.com
parents: 722
diff changeset
86
412
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
723
b87b6e9cedb2 whitespace
drewp@bigasterisk.com
parents: 722
diff changeset
91
412
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
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
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
476
e1f33b9fb3df fix hang-at-startup bug
drewp@bigasterisk.com
parents: 437
diff changeset
103
e1f33b9fb3df fix hang-at-startup bug
drewp@bigasterisk.com
parents: 437
diff changeset
104 # now=False avoids a serious bug where the first read error
e1f33b9fb3df fix hang-at-startup bug
drewp@bigasterisk.com
parents: 437
diff changeset
105 # could happen before reactor.run() is called, and then the
e1f33b9fb3df fix hang-at-startup bug
drewp@bigasterisk.com
parents: 437
diff changeset
106 # error fails to crash the reactor and get us restarted.
e1f33b9fb3df fix hang-at-startup bug
drewp@bigasterisk.com
parents: 437
diff changeset
107 task.LoopingCall(self.poll).start(self.pollPeriodSecs, now=False)
412
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
108
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
109 @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
110 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
111 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
112
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
113 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
114
432
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
115 try:
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
116 for tag in self.reader.getTags(): # blocks for a bit
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
117 uid = tag.uid()
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
118 log.debug('detected tag uid=%r', uid)
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
119 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
120
432
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
121 is_new = cardIdUri not in self.log
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
122 self.log[cardIdUri] = now
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
123 if is_new:
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
124 STATS.newCardReads += 1
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
125 tag.connect()
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
126 try:
412
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
127 textLit = Literal(tag.readBlock(1).rstrip('\x00'))
432
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
128 if self.overwrite_any_tag and not looksLikeBigasterisk(textLit):
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
129 log.info("block 1 was %r; rewriting it", textLit)
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
130 tag.writeBlock(1, randomBody())
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
131 textLit = Literal(tag.readBlock(1).rstrip('\x00'))
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
132 finally:
435
80e403e107b1 more error handling
drewp@bigasterisk.com
parents: 434
diff changeset
133 # This might not be appropriate to call after
80e403e107b1 more error handling
drewp@bigasterisk.com
parents: 434
diff changeset
134 # readBlock fails. I am getting double
80e403e107b1 more error handling
drewp@bigasterisk.com
parents: 434
diff changeset
135 # exceptions.
432
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
136 tag.disconnect()
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
137 self.startCardRead(cardIdUri, textLit)
435
80e403e107b1 more error handling
drewp@bigasterisk.com
parents: 434
diff changeset
138 except AuthFailedError as e:
80e403e107b1 more error handling
drewp@bigasterisk.com
parents: 434
diff changeset
139 log.error(e)
80e403e107b1 more error handling
drewp@bigasterisk.com
parents: 434
diff changeset
140 except (NfcError, OSError) as e:
80e403e107b1 more error handling
drewp@bigasterisk.com
parents: 434
diff changeset
141 traceback.print_exc()
432
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
142 log.error(e)
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
143 reactor.stop()
476
e1f33b9fb3df fix hang-at-startup bug
drewp@bigasterisk.com
parents: 437
diff changeset
144
412
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
145 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
146 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
147 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
148 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
149 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
150
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
151 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
152 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
153 (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
154 (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
155 delQuads=[]))
436
aff8ca01f8eb log reads in a more n3-like format for easier pasting
drewp@bigasterisk.com
parents: 435
diff changeset
156 log.info('%s :cardText %s .', cardUri.n3(), text.n3())
412
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
157 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
158 (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
159
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
160 def endCardRead(self, cardUri):
414
f1a9b4670470 less logging
drewp@bigasterisk.com
parents: 412
diff changeset
161 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
162 delQuads = []
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
163 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
164 (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
165 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
166 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
167 (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
168 delQuads.append(spo + (ctx,))
723
b87b6e9cedb2 whitespace
drewp@bigasterisk.com
parents: 722
diff changeset
169
412
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
170 self.masterGraph.patch(Patch(addQuads=[], delQuads=delQuads))
723
b87b6e9cedb2 whitespace
drewp@bigasterisk.com
parents: 722
diff changeset
171
412
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
172 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
173 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
174 for s,p,o in oneshot)).encode('utf8')
433
d7ede2330b6d update debug host
drewp@bigasterisk.com
parents: 432
diff changeset
175 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
176 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
177 url=url,
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
178 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
179 postdata=body,
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
180 timeout=5)
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
181 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
182 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
183 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
184 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
185
723
b87b6e9cedb2 whitespace
drewp@bigasterisk.com
parents: 722
diff changeset
186
b87b6e9cedb2 whitespace
drewp@bigasterisk.com
parents: 722
diff changeset
187
412
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
188 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
189 arg = docopt("""
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
190 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
191
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
192 -v Verbose
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
193 --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
194 -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
195 """)
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
196 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
197 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
198 enableTwistedLog()
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
199 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
200 log.info(f'cyclone {cyclone.__version__}')
437
2dc7756dfc5e debug Deferreds in -v mode
drewp@bigasterisk.com
parents: 436
diff changeset
201 defer.setDebugging(True)
723
b87b6e9cedb2 whitespace
drewp@bigasterisk.com
parents: 722
diff changeset
202
412
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
203 masterGraph = PatchableGraph()
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
204 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
205
432
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
206 ie=InfluxExporter(Graph())
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
207 ie.exportStats(STATS, ['root.cardReadPoll.count',
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
208 'root.cardReadPoll.95percentile',
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
209 'root.newCardReads',
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
210 ],
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
211 period_secs=10,
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
212 retain_days=7,
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
213 )
f134b64a0ab7 py3, rfid-console rename
drewp@bigasterisk.com
parents: 415
diff changeset
214
412
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
215 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
216
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
217 port = 10012
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
218 reactor.listenTCP(port, cyclone.web.Application([
415
91b4a04a33e7 split console web component
drewp@bigasterisk.com
parents: 414
diff changeset
219 (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
220 {"path": ".", "default_filename": "index.html"}),
722
a93fbf0d0daa dep updates; graph url renames; and other build updates
drewp@bigasterisk.com
parents: 476
diff changeset
221 (r"/graph/rfid", CycloneGraphHandler, {'masterGraph': masterGraph}),
a93fbf0d0daa dep updates; graph url renames; and other build updates
drewp@bigasterisk.com
parents: 476
diff changeset
222 (r"/graph/rfid/events", CycloneGraphEventsHandler,
412
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
223 {'masterGraph': masterGraph}),
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
224 (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
225 (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
226 (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
227 ], 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
228 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
229
91162a54553c copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff changeset
230 reactor.run()