Mercurial > code > home > repos > homeauto
comparison service/rfid/rfid.py @ 1524:13b7e4de3824
whitespace
Ignore-this: c727f388f197a6fae88595fe6d455c7d
darcs-hash:971e4474b4dbd35a722be670f5599298fb5ec83f
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Wed, 05 Feb 2020 00:29:13 -0800 |
parents | 0da337780f22 |
children |
comparison
equal
deleted
inserted
replaced
1523:0da337780f22 | 1524:13b7e4de3824 |
---|---|
43 g = rdfGraphBody(self.request.body, self.request.headers) | 43 g = rdfGraphBody(self.request.body, self.request.headers) |
44 assert len(g) == 1, len(g) | 44 assert len(g) == 1, len(g) |
45 stmt = g.triples((None, None, None)).next() | 45 stmt = g.triples((None, None, None)).next() |
46 self._onStatement(user, stmt) | 46 self._onStatement(user, stmt) |
47 post = put | 47 post = put |
48 | 48 |
49 def _onStatement(self, user, stmt): | 49 def _onStatement(self, user, stmt): |
50 # write rfid to new key, etc. | 50 # write rfid to new key, etc. |
51 if stmt[1] == ROOM['keyContents']: | 51 if stmt[1] == ROOM['keyContents']: |
52 return | 52 return |
53 log.warn("ignoring %s", stmt) | 53 log.warn("ignoring %s", stmt) |
54 | 54 |
55 def uidUri(card_id): | 55 def uidUri(card_id): |
56 return URIRef('http://bigasterisk.com/rfidCard/%010x' % card_id) | 56 return URIRef('http://bigasterisk.com/rfidCard/%010x' % card_id) |
57 | 57 |
58 def uidArray(uri): | 58 def uidArray(uri): |
59 prefix, h = uri.rsplit('/', 1) | 59 prefix, h = uri.rsplit('/', 1) |
60 if prefix != 'http://bigasterisk.com/rfidCard': | 60 if prefix != 'http://bigasterisk.com/rfidCard': |
61 raise ValueError(uri) | 61 raise ValueError(uri) |
62 return [int(h[i * 2: i * 2 + 2], 16) for i in range(0, len(h), 2)] | 62 return [int(h[i * 2: i * 2 + 2], 16) for i in range(0, len(h), 2)] |
63 | 63 |
64 class Rewrite(cyclone.web.RequestHandler): | 64 class Rewrite(cyclone.web.RequestHandler): |
65 def post(self): | 65 def post(self): |
66 agent = URIRef(self.request.headers['x-foaf-agent']) | 66 agent = URIRef(self.request.headers['x-foaf-agent']) |
67 body = json.loads(self.request.body) | 67 body = json.loads(self.request.body) |
68 | 68 |
70 log.info('current card id: %r %r', _, uid) | 70 log.info('current card id: %r %r', _, uid) |
71 if uid is None: | 71 if uid is None: |
72 self.set_status(404, "no card present") | 72 self.set_status(404, "no card present") |
73 # maybe retry a few more times since the card might be nearby | 73 # maybe retry a few more times since the card might be nearby |
74 return | 74 return |
75 | 75 |
76 text = ''.join(random.choice(string.uppercase) for n in range(32)) | 76 text = ''.join(random.choice(string.uppercase) for n in range(32)) |
77 log.info('%s rewrites %s to %s, to be owned by %s', | 77 log.info('%s rewrites %s to %s, to be owned by %s', |
78 agent, uid, text, body['user']) | 78 agent, uid, text, body['user']) |
79 | 79 |
80 #reader.KEY = private.rfid_key | 80 #reader.KEY = private.rfid_key |
81 reader.write(uid, text) | 81 reader.write(uid, text) |
82 log.info('done with write') | 82 log.info('done with write') |
83 | 83 |
84 | 84 |
85 sensor = ROOM['frontDoorWindowRfid'] | 85 sensor = ROOM['frontDoorWindowRfid'] |
86 | 86 |
87 class ReadLoop(object): | 87 class ReadLoop(object): |
88 def __init__(self, reader, masterGraph): | 88 def __init__(self, reader, masterGraph): |
89 self.reader = reader | 89 self.reader = reader |
90 self.masterGraph = masterGraph | 90 self.masterGraph = masterGraph |
91 self.log = {} # cardIdUri : most recent seentime | 91 self.log = {} # cardIdUri : most recent seentime |
92 | 92 |
93 self.pollPeriodSecs = .1 | 93 self.pollPeriodSecs = .1 |
94 self.expireSecs = 2 | 94 self.expireSecs = 2 |
95 | 95 |
96 task.LoopingCall(self.poll).start(self.pollPeriodSecs) | 96 task.LoopingCall(self.poll).start(self.pollPeriodSecs) |
97 | 97 |
98 @STATS.cardReadPoll.time() | 98 @STATS.cardReadPoll.time() |
99 def poll(self): | 99 def poll(self): |
100 now = time.time() | 100 now = time.time() |
112 | 112 |
113 is_new = cardIdUri not in self.log | 113 is_new = cardIdUri not in self.log |
114 self.log[cardIdUri] = now | 114 self.log[cardIdUri] = now |
115 if is_new: | 115 if is_new: |
116 self.startCardRead(cardIdUri, textLit) | 116 self.startCardRead(cardIdUri, textLit) |
117 | 117 |
118 def flushOldReads(self, now): | 118 def flushOldReads(self, now): |
119 for uri in self.log.keys(): | 119 for uri in self.log.keys(): |
120 if self.log[uri] < now - self.expireSecs: | 120 if self.log[uri] < now - self.expireSecs: |
121 self.endCardRead(uri) | 121 self.endCardRead(uri) |
122 del self.log[uri] | 122 del self.log[uri] |
136 (sensor, ROOM['reading'], cardUri)): | 136 (sensor, ROOM['reading'], cardUri)): |
137 delQuads.append(spo + (ctx,)) | 137 delQuads.append(spo + (ctx,)) |
138 for spo in self.masterGraph._graph.triples( | 138 for spo in self.masterGraph._graph.triples( |
139 (cardUri, ROOM['cardText'], None)): | 139 (cardUri, ROOM['cardText'], None)): |
140 delQuads.append(spo + (ctx,)) | 140 delQuads.append(spo + (ctx,)) |
141 | 141 |
142 self.masterGraph.patch(Patch(addQuads=[], delQuads=delQuads)) | 142 self.masterGraph.patch(Patch(addQuads=[], delQuads=delQuads)) |
143 | 143 |
144 def _sendOneshot(self, oneshot): | 144 def _sendOneshot(self, oneshot): |
145 body = (' '.join('%s %s %s .' % (s.n3(), p.n3(), o.n3()) | 145 body = (' '.join('%s %s %s .' % (s.n3(), p.n3(), o.n3()) |
146 for s,p,o in oneshot)).encode('utf8') | 146 for s,p,o in oneshot)).encode('utf8') |
147 url = 'http://bang6:9071/oneShot' | 147 url = 'http://bang6:9071/oneShot' |
148 d = fetch(method='POST', | 148 d = fetch(method='POST', |
153 def err(e): | 153 def err(e): |
154 log.info('oneshot post to %r failed: %s', | 154 log.info('oneshot post to %r failed: %s', |
155 url, e.getErrorMessage()) | 155 url, e.getErrorMessage()) |
156 d.addErrback(err) | 156 d.addErrback(err) |
157 | 157 |
158 | 158 |
159 | 159 |
160 if __name__ == '__main__': | 160 if __name__ == '__main__': |
161 arg = docopt(""" | 161 arg = docopt(""" |
162 Usage: rfid.py [options] | 162 Usage: rfid.py [options] |
163 | 163 |
164 -v Verbose | 164 -v Verbose |