Mercurial > code > home > repos > homeauto
changeset 724:2866c155eb07
rm older rfid attempt using MFRC522 chip
Ignore-this: b84ff118c941f594b71bcbdec9d58db4
author | drewp@bigasterisk.com |
---|---|
date | Wed, 05 Feb 2020 16:39:08 -0800 |
parents | b87b6e9cedb2 |
children | 1ecceb2e92a3 |
files | service/rfid/Dockerfile service/rfid/Dockerfile.pi service/rfid/index.html service/rfid/makefile service/rfid/requirements.txt service/rfid/rfid.py |
diffstat | 6 files changed, 0 insertions(+), 375 deletions(-) [+] |
line wrap: on
line diff
--- a/service/rfid/Dockerfile Wed Feb 05 00:29:13 2020 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -FROM bang6:5000/base_x86 - -WORKDIR /opt - - - -COPY requirements.txt . - -RUN pip install -r requirements.txt - -COPY *.py *.html *.js ./ - -EXPOSE 10012 - -
--- a/service/rfid/Dockerfile.pi Wed Feb 05 00:29:13 2020 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,12 +0,0 @@ -FROM bang6:5000/base_pi - -WORKDIR /opt - -COPY requirements.txt . - -RUN pip install -r requirements.txt - -COPY *.py *.html *.js ./ - -EXPOSE 10012 -
--- a/service/rfid/index.html Wed Feb 05 00:29:13 2020 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,128 +0,0 @@ -<!doctype html> -<html> - <head> - <title>rfid</title> - <meta charset="utf-8" /> - <meta name="mobile-web-app-capable" content="yes"> - <meta name="viewport" content="width=device-width, initial-scale=1"> - <script src="/lib/polymer/1.0.9/webcomponentsjs/webcomponents.min.js"></script> - <script src="/lib/require/require-2.3.3.js"></script> - <script> - requirejs.config({ - paths: { - "streamed-graph": "/rdf/streamed-graph", - "quadstore": "/rdf/quadstore", - "async-module": "/lib/async/80f1793/async", - "async": "/lib/async/80f1793/async", - "jsonld-module": "/lib/jsonld.js/0.4.11/js/jsonld", - "jsonld": "/lib/jsonld.js/0.4.11/js/jsonld", - "rdfstore": "/lib/rdf_store/0.9.7/dist/rdfstore", - "moment": "/lib/moment.min", - "underscore": "/lib/underscore-1.5.2.min", - } - }); - </script> - <script> - window.NS = { - dev: 'http://projects.bigasterisk.com/device/', - room: 'http://projects.bigasterisk.com/room/', - rdfs: 'http://www.w3.org/2000/01/rdf-schema#', - sensor: 'http://bigasterisk.com/homeauto/sensor/', - }; - </script> - <link rel="import" href="/lib/polymer/1.0.9/iron-ajax/iron-ajax.html"> - <link rel="import" href="/rdf/streamed-graph.html"> - <link rel="import" href="/lib/polymer/1.0.9/polymer/polymer.html"> - <link rel="import" href="/rdf/rdf-oneshot.html"> - <link rel="import" href="/rdf/rdf-uri.html"> - </head> - <body> - <dom-module id="rfid-control"> - <style> - button { - min-width: 60px; - min-height: 40px; - } - table { - border-collapse: collapse; - } - - td, th { - border: 1px solid gray; - } - </style> - <template> - <div> - <streamed-graph url="graph/events" graph="{{graph}}"></streamed-graph> - <!-- also get a graph of users so we can look up cards --> - </div> - - <iron-ajax id="rewrite" url="rewrite" method="POST"></iron-ajax> - - Current reads: - <table> - <tr><th>Card UID</th><th>Card text</th><th></th></tr> - <template is="dom-repeat" items="{{currentReads}}"> - <tr> - <td>{{item.uidDisplay}}</td> - <td>{{item.text}}</td> - <td> - <div id="form"> - <button on-click="rewrite">Rewrite</button> - </div> - </td> - </tr> - </template> - </table> - - </template> - <script> - HTMLImports.whenReady(function () { - Polymer({ - is: 'rfid-control', - properties: { - graph: { type: Object, notify: true, observer: "_onGraph" }, - currentReads: { type: Array, value: [] }, - }, - behaviors: [BigastUri], - _onGraph: function(graph) { - if (!graph.graph) return; - const env = graph.graph.store.rdf; - - this.splice('currentReads', 0, this.currentReads.length); - graph.graph.quadStore.quads( - {subject: env.createNamedNode('room:frontDoorWindowRfid'), - predicate: env.createNamedNode('room:reading'), - }, - (q) => { - graph.graph.quadStore.quads( - {subject: q.object, - predicate: env.createNamedNode('room:cardText'), - }, - (q2) => { - this.push( - 'currentReads', { - 'cardUid': q.object, - 'uidDisplay': q.object.toString().replace(/.*\//, ""), - 'text': q2.object.toString() - }); - }); - }); - }, - rewrite: function(ev) { - const cardUid = ev.model.item.cardUid; - - // ask for user first - - this.$.rewrite.contentType = "application/json"; - this.$.rewrite.body = {'cardUid': cardUid.toString(), - 'user': "some foaf"}; - this.$.rewrite.generateRequest(); - } - }); - }); - </script> - </dom-module> - <rfid-control></rfid-control> - </body> -</html>
--- a/service/rfid/makefile Wed Feb 05 00:29:13 2020 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ -SERVICE=rfid - -build_image: - rm -rf tmp_ctx - mkdir -p tmp_ctx - cp -a Dockerfile ../../lib/*.py *.py *.txt *.html MFRC522-python tmp_ctx - docker build --network=host -t bang6:5000/$(SERVICE)_x86:latest tmp_ctx - docker push bang6:5000/$(SERVICE)_x86:latest - rm -rf tmp_ctx - - -build_image_pi: - rm -rf tmp_ctx - mkdir -p tmp_ctx - cp -a Dockerfile.pi ../../lib/*.py *.py *.txt *.html MFRC522-python tmp_ctx - docker build --file Dockerfile.pi --network=host -t bang6:5000/$(SERVICE)_pi:latest tmp_ctx - docker push bang6:5000/$(SERVICE)_pi:latest - rm -rf tmp_ctx - -shell: - docker run --rm -it --cap-add SYS_PTRACE --net=host bang6:5000/$(SERVICE)_x86:latest /bin/sh - -local_run: - docker run --rm -it --net=host bang6:5000/$(SERVICE)_x86:latest
--- a/service/rfid/requirements.txt Wed Feb 05 00:29:13 2020 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -cyclone -docopt -rdflib-jsonld==0.4.0 -rdflib==4.2.2 -https://projects.bigasterisk.com/rdfdb/rdfdb-0.7.0.tar.gz -https://github.com/drewp/MFRC522-python/archive/877f5568627b5e67d622c08191aae0f4ffd6dd32.zip -git+http://github.com/drewp/scales.git@448d59fb491b7631877528e7695a93553bfaaa93#egg=scales
--- a/service/rfid/rfid.py Wed Feb 05 00:29:13 2020 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,189 +0,0 @@ -from docopt import docopt -from rdfdb.patch import Patch -from patchablegraph import PatchableGraph, CycloneGraphHandler, CycloneGraphEventsHandler -from rdflib import Namespace, URIRef, Literal, Graph -from rdflib.parser import StringInputSource -from twisted.internet import reactor, task -import cyclone.web -from cyclone.httpclient import fetch -import logging, time, json, random, string -from MFRC522.SimpleMFRC522 import SimpleMFRC522 -from logsetup import log, enableTwistedLog -import private -from greplin import scales -from greplin.scales.cyclonehandler import StatsHandler - -ROOM = Namespace('http://projects.bigasterisk.com/room/') - -ctx = ROOM['frontDoorWindowRfidCtx'] - -cardOwner = { - URIRef('http://bigasterisk.com/rfidCard/93a7591a77'): - URIRef('http://bigasterisk.com/foaf.rdf#drewp'), -} - -STATS = scales.collection('/web', - scales.PmfStat('cardReadPoll'), -) -def rdfGraphBody(body, headers): - g = Graph() - g.parse(StringInputSource(body), format='nt') - return g - -class OutputPage(cyclone.web.RequestHandler): - def put(self): - user = URIRef(self.request.headers['x-foaf-agent']) - arg = self.request.arguments - if arg.get('s') and arg.get('p'): - subj = URIRef(arg['s'][-1]) - pred = URIRef(arg['p'][-1]) - obj = URIRef(self.request.body) - stmt = (subj, pred, obj) - else: - g = rdfGraphBody(self.request.body, self.request.headers) - assert len(g) == 1, len(g) - stmt = g.triples((None, None, None)).next() - self._onStatement(user, stmt) - post = put - - def _onStatement(self, user, stmt): - # write rfid to new key, etc. - if stmt[1] == ROOM['keyContents']: - return - log.warn("ignoring %s", stmt) - -def uidUri(card_id): - return URIRef('http://bigasterisk.com/rfidCard/%010x' % card_id) - -def uidArray(uri): - prefix, h = uri.rsplit('/', 1) - if prefix != 'http://bigasterisk.com/rfidCard': - raise ValueError(uri) - return [int(h[i * 2: i * 2 + 2], 16) for i in range(0, len(h), 2)] - -class Rewrite(cyclone.web.RequestHandler): - def post(self): - agent = URIRef(self.request.headers['x-foaf-agent']) - body = json.loads(self.request.body) - - _, uid = reader.read_id() - log.info('current card id: %r %r', _, uid) - if uid is None: - self.set_status(404, "no card present") - # maybe retry a few more times since the card might be nearby - return - - text = ''.join(random.choice(string.uppercase) for n in range(32)) - log.info('%s rewrites %s to %s, to be owned by %s', - agent, uid, text, body['user']) - - #reader.KEY = private.rfid_key - reader.write(uid, text) - log.info('done with write') - - -sensor = ROOM['frontDoorWindowRfid'] - -class ReadLoop(object): - def __init__(self, reader, masterGraph): - self.reader = reader - self.masterGraph = masterGraph - self.log = {} # cardIdUri : most recent seentime - - self.pollPeriodSecs = .1 - self.expireSecs = 2 - - task.LoopingCall(self.poll).start(self.pollPeriodSecs) - - @STATS.cardReadPoll.time() - def poll(self): - now = time.time() - - self.flushOldReads(now) - - card_id, text = self.reader.read() - if card_id is None or text == '': - # text=='' could be legit, but it's probably a card that's - # still being read. - return - - cardIdUri = uidUri(card_id) - textLit = Literal(text.rstrip().decode('ascii', 'replace')) - - is_new = cardIdUri not in self.log - self.log[cardIdUri] = now - if is_new: - self.startCardRead(cardIdUri, textLit) - - def flushOldReads(self, now): - for uri in self.log.keys(): - if self.log[uri] < now - self.expireSecs: - self.endCardRead(uri) - del self.log[uri] - - def startCardRead(self, cardUri, text): - p = Patch(addQuads=[(sensor, ROOM['reading'], cardUri, ctx), - (cardUri, ROOM['cardText'], text, ctx)], - delQuads=[]) - self.masterGraph.patch(p) - log.info('read card: id=%s %r', cardUri, str(text)) - self._sendOneshot([(sensor, ROOM['startReading'], cardUri), - (cardUri, ROOM['cardText'], text)]) - - def endCardRead(self, cardUri): - delQuads = [] - for spo in self.masterGraph._graph.triples( - (sensor, ROOM['reading'], cardUri)): - delQuads.append(spo + (ctx,)) - for spo in self.masterGraph._graph.triples( - (cardUri, ROOM['cardText'], None)): - delQuads.append(spo + (ctx,)) - - self.masterGraph.patch(Patch(addQuads=[], delQuads=delQuads)) - - def _sendOneshot(self, oneshot): - body = (' '.join('%s %s %s .' % (s.n3(), p.n3(), o.n3()) - for s,p,o in oneshot)).encode('utf8') - url = 'http://bang6:9071/oneShot' - d = fetch(method='POST', - url=url, - headers={'Content-Type': ['text/n3']}, - postdata=body, - timeout=5) - def err(e): - log.info('oneshot post to %r failed: %s', - url, e.getErrorMessage()) - d.addErrback(err) - - - -if __name__ == '__main__': - arg = docopt(""" - Usage: rfid.py [options] - - -v Verbose - """) - log.setLevel(logging.INFO) - if arg['-v']: - enableTwistedLog() - log.setLevel(logging.DEBUG) - - masterGraph = PatchableGraph() - reader = SimpleMFRC522(gain=0x07) - - loop = ReadLoop(reader, masterGraph) - - port = 10012 - reactor.listenTCP(port, cyclone.web.Application([ - (r"/()", cyclone.web.StaticFileHandler, - {"path": ".", "default_filename": "index.html"}), - (r"/graph/rfid", CycloneGraphHandler, {'masterGraph': masterGraph}), - (r"/graph/rfid/events", CycloneGraphEventsHandler, - {'masterGraph': masterGraph}), - (r'/output', OutputPage), - (r'/rewrite', Rewrite), - (r'/stats/(.*)', StatsHandler, {'serverName': 'rfid'}), - ], masterGraph=masterGraph, debug=arg['-v']), interface='::') - log.warn('serving on %s', port) - - reactor.run()