def __init__(self, graph, masterGraph, uri): self.graph, self.uri = graph, uri self.masterGraph = masterGraph self.masterGraph.patch(Patch(addQuads=self.staticStmts())) self.pi = pigpio.pi() self._devs = devices.makeDevices(graph, self.uri, self.pi) log.debug('found %s devices', len(self._devs)) self._statementsFromInputs = {} # input device uri: latest statements self._lastPollTime = {} # input device uri: time() self._influx = InfluxExporter(self.graph) for d in self._devs: self.syncMasterGraphToHostStatements(d)
def __init__(self, dev, configGraph, masterGraph, uri): """ each connected thing has some pins. """ self.uri = uri self.configGraph = configGraph self.masterGraph = masterGraph self.dev = dev self.masterGraph.setToGraph(self.staticStmts()) # The order of this list needs to be consistent between the # deployToArduino call and the poll call. self._devs = devices.makeDevices(configGraph, self.uri) self._devCommandNum = dict((dev.uri, ACTION_BASE + devIndex) for devIndex, dev in enumerate(self._devs)) self._polledDevs = [d for d in self._devs if d.generatePollCode()] self._statementsFromInputs = {} # input device uri: latest statements self._lastPollTime = {} # input device uri: time() self._influx = InfluxExporter(self.configGraph) self.open() for d in self._devs: self.syncMasterGraphToHostStatements(d)
def __init__(self, dev, configGraph, masterGraph, uri): """ each connected thing has some pins. """ self.uri = uri self.configGraph = configGraph self.masterGraph = masterGraph self.dev = dev self.masterGraph.patch(Patch(addQuads=self.staticStmts())) # The order of this list needs to be consistent between the # deployToArduino call and the poll call. self._devs = devices.makeDevices(configGraph, self.uri) self._devCommandNum = dict((dev.uri, ACTION_BASE + devIndex) for devIndex, dev in enumerate(self._devs)) self._polledDevs = [d for d in self._devs if d.generatePollCode()] self._statementsFromInputs = {} # input device uri: latest statements self._lastPollTime = {} # input device uri: time() self._influx = InfluxExporter(self.configGraph) self.open() for d in self._devs: self.syncMasterGraphToHostStatements(d)
class Board(object): """an arduino connected to this computer""" baudrate = 115200 def __init__(self, dev, configGraph, masterGraph, uri): """ each connected thing has some pins. """ self.uri = uri self.configGraph = configGraph self.masterGraph = masterGraph self.dev = dev self.masterGraph.patch(Patch(addQuads=self.staticStmts())) # The order of this list needs to be consistent between the # deployToArduino call and the poll call. self._devs = devices.makeDevices(configGraph, self.uri) self._devCommandNum = dict((dev.uri, ACTION_BASE + devIndex) for devIndex, dev in enumerate(self._devs)) self._polledDevs = [d for d in self._devs if d.generatePollCode()] self._statementsFromInputs = {} # input device uri: latest statements self._lastPollTime = {} # input device uri: time() self._influx = InfluxExporter(self.configGraph) self.open() for d in self._devs: self.syncMasterGraphToHostStatements(d) def description(self): """for web page""" return { 'uri': self.uri, 'dev': self.dev, 'baudrate': self.baudrate, 'devices': [d.description() for d in self._devs], } def open(self): self.ser = LoggingSerial(port=self.dev, baudrate=self.baudrate, timeout=2) def startPolling(self, period=.5): task.LoopingCall(self._poll).start(period) def _poll(self): """ even boards with no inputs need some polling to see if they're still ok """ try: self._pollWork() except serial.SerialException: reactor.crash() raise except Exception as e: import traceback; traceback.print_exc() log.warn("poll: %r" % e) def _pollWork(self): t1 = time.time() self.ser.write("\x60\x00") # "poll everything" for i in self._polledDevs: try: now = time.time() new = i.readFromPoll(self.ser.read) if isinstance(new, dict): # new style oneshot = new['oneshot'] new = new['latest'] else: oneshot = None prev = self._statementsFromInputs.get(i.uri, []) if new or prev: self._statementsFromInputs[i.uri] = new # it's important that quads from different devices # don't clash, since that can lead to inconsistent # patches (e.g. # dev1 changes value from 1 to 2; # dev2 changes value from 2 to 3; # dev1 changes from 2 to 4 but this patch will # fail since the '2' statement is gone) self.masterGraph.patch(Patch.fromDiff(inContext(prev, i.uri), inContext(new, i.uri))) if oneshot: self._sendOneshot(oneshot) self._lastPollTime[i.uri] = now except: log.warn('while polling %r:', i.uri) raise #plus statements about succeeding or erroring on the last poll byte = self.ser.read(1) if byte != 'x': raise ValueError("after poll, got %x instead of 'x'" % byte) elapsed = time.time() - t1 if elapsed > 1.0: log.warn('poll took %.1f seconds' % elapsed) stmts = set() for v in self._statementsFromInputs.values(): stmts.update(v) self._influx.exportToInflux(stmts) def _sendOneshot(self, oneshot): body = (' '.join('%s %s %s .' % (s.n3(), p.n3(), o.n3()) for s,p,o in oneshot)).encode('utf8') bang6 = 'fcb8:4119:fb46:96f8:8b07:1260:0f50:fcfa' fetch(method='POST', url='http://[%s]:9071/oneShot' % bang6, headers={'Content-Type': ['text/n3']}, postdata=body, timeout=5) def outputStatements(self, stmts): unused = set(stmts) for dev in self._devs: stmtsForDev = [] for pat in dev.outputPatterns(): if [term is None for term in pat] != [False, False, True]: raise NotImplementedError for stmt in stmts: if stmt[:2] == pat[:2]: stmtsForDev.append(stmt) unused.discard(stmt) if stmtsForDev: log.info("output goes to action handler for %s" % dev.uri) self.ser.write("\x60" + chr(self._devCommandNum[dev.uri])) dev.sendOutput(stmtsForDev, self.ser.write, self.ser.read) if self.ser.read(1) != 'k': raise ValueError( "%s sendOutput/generateActionCode didn't use " "matching output bytes" % dev.__class__) # Dev *could* change hostStatements at any time, and # we're not currently tracking that, but the usual is # to change them in response to sendOutput so this # should be good enough. The right answer is to give # each dev the masterGraph for it to write to. self.syncMasterGraphToHostStatements(dev) log.info("output and masterGraph sync complete") if unused: log.info("Board %s doesn't care about these statements:", self.uri) for s in unused: log.warn("%r", s) def syncMasterGraphToHostStatements(self, dev): hostStmtCtx = URIRef(dev.uri + '/host') newQuads = inContext(dev.hostStatements(), hostStmtCtx) p = self.masterGraph.patchSubgraph(hostStmtCtx, newQuads) log.debug("patch master with these host stmts %s", p) def staticStmts(self): return [(HOST[hostname], ROOM['connectedTo'], self.uri, CTX)] def generateArduinoCode(self): code = write_arduino_code.writeCode(self.baudrate, self._devs, self._devCommandNum) code = write_arduino_code.indent(code) cksum = hashlib.sha1(code).hexdigest() code = code.replace('CODE_CHECKSUM', cksum) return code, cksum def _readBoardChecksum(self, length): # this is likely right after reset, so it might take 2 seconds for tries in range(6): self.ser.write("\x60\x01") try: return self.ser.read(length) except ValueError: if tries == 5: raise time.sleep(.5) raise ValueError def _boardIsCurrent(self, currentChecksum): try: boardCksum = self._readBoardChecksum(len(currentChecksum)) if boardCksum == currentChecksum: log.info("board has current code (%s)" % currentChecksum) return True else: log.info("board responds with incorrect code version") except Exception as e: log.info("can't get code version from board: %r" % e) return False def deployToArduino(self): code, cksum = self.generateArduinoCode() if self._boardIsCurrent(cksum): return try: if hasattr(self, 'ser'): self.ser.close() workDir = tempfile.mkdtemp(prefix='arduinoNode_board_deploy') try: self._arduinoMake(workDir, code) finally: shutil.rmtree(workDir) finally: self.open() def _arduinoMake(self, workDir, code): with open(workDir + '/makefile', 'w') as makefile: makefile.write(write_arduino_code.writeMakefile( dev=self.dev, tag=self.configGraph.value(self.uri, ROOM['boardTag']), allLibs=sum((d.generateArduinoLibs() for d in self._devs), []))) with open(workDir + '/main.ino', 'w') as main: main.write(code) subprocess.check_call(['make', 'upload'], cwd=workDir) def currentGraph(self): g = Graph() for dev in self._devs: for stmt in dev.hostStatements(): g.add(stmt) return g
-v Verbose --overwrite_any_tag Rewrite any unknown tag with a new random body -n Fake reader """) log.setLevel(logging.INFO) if arg['-v']: enableTwistedLog() log.setLevel(logging.DEBUG) log.info(f'cyclone {cyclone.__version__}') defer.setDebugging(True) masterGraph = PatchableGraph() reader = NfcDevice() if not arg['-n'] else FakeNfc() ie = InfluxExporter(Graph()) ie.exportStats( STATS, [ 'root.cardReadPoll.count', 'root.cardReadPoll.95percentile', 'root.newCardReads', ], period_secs=10, retain_days=7, ) loop = ReadLoop(reader, masterGraph, overwrite_any_tag=arg['--overwrite_any_tag'])
-v Verbose --overwrite_any_tag Rewrite any unknown tag with a new random body -n Fake reader """) log.setLevel(logging.INFO) if arg['-v']: enableTwistedLog() log.setLevel(logging.DEBUG) log.info(f'cyclone {cyclone.__version__}') defer.setDebugging(True) masterGraph = PatchableGraph() reader = NfcDevice() if not arg['-n'] else FakeNfc() ie=InfluxExporter(Graph()) ie.exportStats(STATS, ['root.cardReadPoll.count', 'root.cardReadPoll.95percentile', 'root.newCardReads', ], period_secs=10, retain_days=7, ) loop = ReadLoop(reader, masterGraph, overwrite_any_tag=arg['--overwrite_any_tag']) port = 10012 reactor.listenTCP(port, cyclone.web.Application([ (r"/(|.+\.html)", cyclone.web.StaticFileHandler, {"path": ".", "default_filename": "index.html"}), (r"/graph", CycloneGraphHandler, {'masterGraph': masterGraph}),
class Board(object): """similar to arduinoNode.Board but without the communications stuff""" def __init__(self, graph, masterGraph, uri, hubHost): self.graph, self.uri = graph, uri self.hubHost = hubHost self.masterGraph = masterGraph self.masterGraph.setToGraph(self.staticStmts()) self.pi = pigpio.pi() self._devs = devices.makeDevices(graph, self.uri, self.pi) log.debug('found %s devices', len(self._devs)) self._statementsFromInputs = {} # input device uri: latest statements self._lastPollTime = {} # input device uri: time() self._influx = InfluxExporter(self.graph) for d in self._devs: self.syncMasterGraphToHostStatements(d) def startPolling(self): task.LoopingCall(self._poll).start(.05) @STATS.boardPoll.time() # not differentiating multiple boards here def _poll(self): try: self._pollMaybeError() except Exception: STATS.pollException += 1 log.exception("During poll:") def _pollMaybeError(self): pollTime = {} # uri: sec for i in self._devs: now = time.time() if (hasattr(i, 'pollPeriod') and self._lastPollTime.get(i.uri, 0) + i.pollPeriod > now): continue #need something like: # with i.pollTiming.time(): new = i.poll() pollTime[i.uri] = time.time() - now if isinstance(new, dict): # new style oneshot = new['oneshot'] new = new['latest'] else: oneshot = None self._updateMasterWithNewPollStatements(i.uri, new) if oneshot: self._sendOneshot(oneshot) self._lastPollTime[i.uri] = now if log.isEnabledFor(logging.DEBUG): log.debug('poll times:') for u, s in sorted(pollTime.items()): log.debug(" %.4f ms %s", s * 1000, u) log.debug('total poll time: %f ms', sum(pollTime.values()) * 1000) pollResults = map(set, self._statementsFromInputs.values()) if pollResults: self._influx.exportToInflux(set.union(*pollResults)) def _updateMasterWithNewPollStatements(self, dev, new): prev = self._statementsFromInputs.get(dev, set()) # it's important that quads from different devices # don't clash, since that can lead to inconsistent # patches (e.g. # dev1 changes value from 1 to 2; # dev2 changes value from 2 to 3; # dev1 changes from 2 to 4 but this patch will # fail since the '2' statement is gone) self.masterGraph.patch(Patch.fromDiff(inContext(prev, dev), inContext(new, dev))) self._statementsFromInputs[dev] = new @STATS.sendOneshot.time() 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://%s:9071/oneShot' % self.hubHost 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) @STATS.outputStatements.time() def outputStatements(self, stmts): unused = set(stmts) for dev in self._devs: stmtsForDev = [] for pat in dev.outputPatterns(): if [term is None for term in pat] != [False, False, True]: raise NotImplementedError for stmt in stmts: if stmt[:2] == pat[:2]: stmtsForDev.append(stmt) unused.discard(stmt) if stmtsForDev: log.info("output goes to action handler for %s" % dev.uri) dev.sendOutput(stmtsForDev) # Dev *could* change hostStatements at any time, and # we're not currently tracking that, but the usual is # to change them in response to sendOutput so this # should be good enough. The right answer is to give # each dev the masterGraph for it to write to. self.syncMasterGraphToHostStatements(dev) log.info("output and masterGraph sync complete") if unused: log.info("Board %s doesn't care about these statements:", self.uri) for s in unused: log.warn("%r", s) def syncMasterGraphToHostStatements(self, dev): hostStmtCtx = URIRef(dev.uri + '/host') newQuads = inContext(dev.hostStatements(), hostStmtCtx) p = self.masterGraph.patchSubgraph(hostStmtCtx, newQuads) log.debug("patch master with these host stmts %s", p) def staticStmts(self): return [(HOST[hostname], ROOM['connectedTo'], self.uri, CTX)] def description(self): """for web page""" return { 'uri': self.uri, 'devices': [d.description() for d in self._devs], 'graph': 'http://sticker:9059/graph', #todo }
class Board(object): """an arduino connected to this computer""" baudrate = 115200 def __init__(self, dev, configGraph, masterGraph, uri): """ each connected thing has some pins. """ self.uri = uri self.configGraph = configGraph self.masterGraph = masterGraph self.dev = dev self.masterGraph.setToGraph(self.staticStmts()) # The order of this list needs to be consistent between the # deployToArduino call and the poll call. self._devs = devices.makeDevices(configGraph, self.uri) self._devCommandNum = dict((dev.uri, ACTION_BASE + devIndex) for devIndex, dev in enumerate(self._devs)) self._polledDevs = [d for d in self._devs if d.generatePollCode()] self._statementsFromInputs = {} # input device uri: latest statements self._lastPollTime = {} # input device uri: time() self._influx = InfluxExporter(self.configGraph) self.open() for d in self._devs: self.syncMasterGraphToHostStatements(d) def description(self): """for web page""" return { 'uri': self.uri, 'dev': self.dev, 'baudrate': self.baudrate, 'devices': [d.description() for d in self._devs], } def open(self): self.ser = LoggingSerial(port=self.dev, baudrate=self.baudrate, timeout=2) def startPolling(self, period=.5): task.LoopingCall(self._poll).start(period) def _poll(self): """ even boards with no inputs need some polling to see if they're still ok """ try: self._pollWork() except serial.SerialException: reactor.crash() raise except Exception as e: import traceback; traceback.print_exc() log.warn("poll: %r" % e) def _pollWork(self): t1 = time.time() self.ser.write("\x60\x00") # "poll everything" for i in self._polledDevs: with i._stats.poll.time(): try: now = time.time() new = i.readFromPoll(self.ser.read) if isinstance(new, dict): # new style oneshot = new['oneshot'] new = new['latest'] else: oneshot = None self._updateMasterWithNewPollStatements(i.uri, new) if oneshot: self._sendOneshot(oneshot) self._lastPollTime[i.uri] = now except: log.warn('while polling %r:', i.uri) raise #plus statements about succeeding or erroring on the last poll byte = self.ser.read(1) if byte != 'x': raise ValueError("after poll, got %x instead of 'x'" % byte) for i in self._devs: if i.wantIdleOutput(): self.ser.write("\x60" + chr(self._devCommandNum[i.uri])) i.outputIdle(self.ser.write) if self.ser.read(1) != 'k': raise ValueError('no ack after outputIdle') elapsed = time.time() - t1 if elapsed > 1.0: log.warn('poll took %.1f seconds' % elapsed) stmts = set() for v in self._statementsFromInputs.values(): stmts.update(v) self._influx.exportToInflux(stmts) def _updateMasterWithNewPollStatements(self, dev, new): prev = self._statementsFromInputs.get(dev, set()) # it's important that quads from different devices # don't clash, since that can lead to inconsistent # patches (e.g. # dev1 changes value from 1 to 2; # dev2 changes value from 2 to 3; # dev1 changes from 2 to 4 but this patch will # fail since the '2' statement is gone) self.masterGraph.patch(Patch.fromDiff(inContext(prev, dev), inContext(new, dev))) self._statementsFromInputs[dev] = new def _sendOneshot(self, oneshot): body = (' '.join('%s %s %s .' % (s.n3(), p.n3(), o.n3()) for s,p,o in oneshot)).encode('utf8') fetch(method='POST', url='http://bang6:9071/oneShot', headers={'Content-Type': ['text/n3']}, postdata=body, timeout=5) def outputStatements(self, stmts): unused = set(stmts) for dev in self._devs: stmtsForDev = [] for pat in dev.outputPatterns(): if [term is None for term in pat] != [False, False, True]: raise NotImplementedError for stmt in stmts: if stmt[:2] == pat[:2]: stmtsForDev.append(stmt) unused.discard(stmt) if stmtsForDev: log.info("output goes to action handler for %s" % dev.uri) with dev._stats.output.time(): self.ser.write("\x60" + chr(self._devCommandNum[dev.uri])) dev.sendOutput(stmtsForDev, self.ser.write, self.ser.read) if self.ser.read(1) != 'k': raise ValueError( "%s sendOutput/generateActionCode didn't use " "matching output bytes" % dev.__class__) # Dev *could* change hostStatements at any time, and # we're not currently tracking that, but the usual is # to change them in response to sendOutput so this # should be good enough. The right answer is to give # each dev the masterGraph for it to write to. self.syncMasterGraphToHostStatements(dev) log.info("output and masterGraph sync complete") if unused: log.info("Board %s doesn't care about these statements:", self.uri) for s in unused: log.info("%r", s) def syncMasterGraphToHostStatements(self, dev): hostStmtCtx = URIRef(dev.uri + '/host') newQuads = inContext(dev.hostStatements(), hostStmtCtx) p = self.masterGraph.patchSubgraph(hostStmtCtx, newQuads) log.debug("patch master with these host stmts %s", p) def staticStmts(self): return [(HOST[hostname], ROOM['connectedTo'], self.uri, CTX)] def generateArduinoCode(self): code = write_arduino_code.writeCode(self.baudrate, self._devs, self._devCommandNum) code = write_arduino_code.indent(code) cksum = hashlib.sha1(code).hexdigest() code = code.replace('CODE_CHECKSUM', cksum) return code, cksum def _readBoardChecksum(self, length): # this is likely right after reset, so it might take 2 seconds for tries in range(6): self.ser.write("\x60\x01") try: return self.ser.read(length) except ValueError: if tries == 5: raise time.sleep(.5) raise ValueError def _boardIsCurrent(self, currentChecksum): try: boardCksum = self._readBoardChecksum(len(currentChecksum)) if boardCksum == currentChecksum: log.info("board has current code (%s)" % currentChecksum) return True else: log.info("board responds with incorrect code version") except Exception as e: log.info("can't get code version from board: %r" % e) return False def deployToArduino(self): code, cksum = self.generateArduinoCode() if self._boardIsCurrent(cksum): return try: if hasattr(self, 'ser'): self.ser.close() workDir = tempfile.mkdtemp(prefix='arduinoNode_board_deploy') try: self._arduinoMake(workDir, code) finally: shutil.rmtree(workDir) finally: self.open() def _arduinoMake(self, workDir, code): with open(workDir + '/makefile', 'w') as makefile: makefile.write(write_arduino_code.writeMakefile( dev=self.dev, tag=self.configGraph.value(self.uri, ROOM['boardTag']), allLibs=sum((d.generateArduinoLibs() for d in self._devs), []))) with open(workDir + '/main.ino', 'w') as main: main.write(code) subprocess.check_call(['make', 'upload'], cwd=workDir) def currentGraph(self): g = Graph() for dev in self._devs: for stmt in dev.hostStatements(): g.add(stmt) return g
"config_nightlight_ari.n3", "config_bed_bar.n3", "config_air_quality_indoor.n3", "config_air_quality_outdoor.n3", "config_living_lamps.n3", "config_kitchen.n3", ]: if not arg['--cs'] or arg['--cs'] in fn: config.parse(fn, format='n3') masterGraph = PatchableGraph() mqtt = MqttClient(clientId='mqtt_to_rdf', brokerHost='bang', brokerPort=1883) influx = InfluxExporter(config) srcs = [] for src in config.subjects(RDF.type, ROOM['MqttStatementSource']): srcs.append(MqttStatementSource(src, config, masterGraph, mqtt, influx)) log.info(f'set up {len(srcs)} sources') port = 10018 reactor.listenTCP( port, cyclone.web.Application([ (r"/()", cyclone.web.StaticFileHandler, { "path": ".", "default_filename": "index.html" }),