Mercurial > code > home > repos > homeauto
annotate service/rfid_pn532_py/rfid.py @ 414:f1a9b4670470
less logging
Ignore-this: b58a3d1af5373f8f76554e5b02fb80ec
author | drewp@bigasterisk.com |
---|---|
date | Sat, 23 Mar 2019 04:38:07 -0700 |
parents | 91162a54553c |
children | 91b4a04a33e7 |
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 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
2 os.environ['LIBNFC_DEFAULT_DEVICE'] = "pn532_i2c:/dev/i2c-1" |
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 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
9 from twisted.internet import reactor, task |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
10 import cyclone.web |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
11 from cyclone.httpclient import fetch |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
12 import cyclone |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
13 import logging, time, json, random, string |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
14 from logsetup import log, enableTwistedLog |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
15 from greplin import scales |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
16 from greplin.scales.cyclonehandler import StatsHandler |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
17 from tags import NfcDevice, FakeNfc |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
18 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
19 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
|
20 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
21 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
|
22 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
23 STATS = scales.collection('/web', |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
24 scales.PmfStat('cardReadPoll'), |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
25 ) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
26 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
27 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
|
28 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
|
29 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
|
30 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
|
31 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
|
32 else: |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
33 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
|
34 post = put |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
35 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
|
36 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
|
37 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
|
38 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
|
39 try: |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
40 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
|
41 except ValueError: |
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(turtleLiteral) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
43 self._onStatements([(subj, pred, obj)]) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
44 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
45 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
|
46 g = Graph() |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
47 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
|
48 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
|
49 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
|
50 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
|
51 post = put |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
52 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
53 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
|
54 # 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
|
55 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
|
56 return |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
57 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
|
58 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
59 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
|
60 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
|
61 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
62 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
|
63 def randomBody(): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
64 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
|
65 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
66 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
|
67 return text.startswith(BODY_VERSION + "*") |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
68 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
69 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
|
70 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
|
71 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
|
72 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
|
73 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
74 _, 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
|
75 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
|
76 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
|
77 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
|
78 # 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
|
79 return |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
80 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
81 text = randomBody() |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
82 log.info('%s rewrites %s to %s, to be owned by %s', |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
83 agent, uid, text, body['user']) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
84 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
85 #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
|
86 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
|
87 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
|
88 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
89 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
90 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
|
91 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
92 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
|
93 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
|
94 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
|
95 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
|
96 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
|
97 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
|
98 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
99 self.pollPeriodSecs = 5.1 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
100 self.expireSecs = 5 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
101 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
102 task.LoopingCall(self.poll).start(self.pollPeriodSecs) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
103 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
104 @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
|
105 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
|
106 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
|
107 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
108 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
|
109 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
110 for tag in self.reader.getTags(): # blocks for a bit |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
111 uid = tag.uid() |
414 | 112 log.debug('detected tag uid=%r', uid) |
412
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
113 cardIdUri = uidUri(uid) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
114 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
115 is_new = cardIdUri not in self.log |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
116 self.log[cardIdUri] = now |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
117 if is_new: |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
118 tag.connect() |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
119 try: |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
120 textLit = Literal(tag.readBlock(1).rstrip('\x00')) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
121 if self.overwrite_any_tag and not looksLikeBigasterisk(textLit): |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
122 log.info("block 1 was %r; rewriting it", textLit) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
123 tag.writeBlock(1, randomBody()) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
124 textLit = Literal(tag.readBlock(1).rstrip('\x00')) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
125 finally: |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
126 tag.disconnect() |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
127 self.startCardRead(cardIdUri, textLit) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
128 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
129 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
|
130 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
|
131 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
|
132 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
|
133 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
|
134 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
135 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
|
136 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
|
137 (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
|
138 (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
|
139 delQuads=[])) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
140 log.info('read card at id=%s %r', cardUri, str(text)) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
141 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
|
142 (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
|
143 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
144 def endCardRead(self, cardUri): |
414 | 145 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
|
146 delQuads = [] |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
147 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
|
148 (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
|
149 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
|
150 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
|
151 (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
|
152 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
|
153 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
154 self.masterGraph.patch(Patch(addQuads=[], delQuads=delQuads)) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
155 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
156 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
|
157 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
|
158 for s,p,o in oneshot)).encode('utf8') |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
159 url = b'http://bang6:19071/oneShot' |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
160 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
|
161 url=url, |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
162 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
|
163 postdata=body, |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
164 timeout=5) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
165 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
|
166 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
|
167 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
|
168 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
|
169 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
170 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
171 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
172 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
|
173 arg = docopt(""" |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
174 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
|
175 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
176 -v Verbose |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
177 --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
|
178 -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
|
179 """) |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
180 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
|
181 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
|
182 enableTwistedLog() |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
183 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
|
184 log.info(f'cyclone {cyclone.__version__}') |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
185 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
186 masterGraph = PatchableGraph() |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
187 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
|
188 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
189 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
|
190 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
191 port = 10012 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
192 reactor.listenTCP(port, cyclone.web.Application([ |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
193 (r"/()", cyclone.web.StaticFileHandler, |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
194 {"path": ".", "default_filename": "index.html"}), |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
195 (r"/graph", CycloneGraphHandler, {'masterGraph': masterGraph}), |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
196 (r"/graph/events", CycloneGraphEventsHandler, |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
197 {'masterGraph': masterGraph}), |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
198 (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
|
199 (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
|
200 (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
|
201 ], 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
|
202 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
|
203 |
91162a54553c
copy rest of rfid service from the first try. fix some crashes in tags.py
drewp@bigasterisk.com
parents:
diff
changeset
|
204 reactor.run() |