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 reread(self): """update the graph with any diffs from this file n3 parser fails on "1.e+0" even though rdflib was emitting that itself """ old = self.getSubgraph(self.uri) new = Graph() try: contents = open(self.path).read() if contents.startswith("#new"): log.debug("%s ignoring empty contents of my new file", self.path) # this is a new file we're starting, and we should not # patch our graph as if it had just been cleared. We # shouldn't even be here reading this, but # lastWriteTimestamp didn't work. return new.parse(location=self.path, format='n3') self.namespaces.update(dict(new.namespaces())) except SyntaxError as e: print e traceback.print_exc() log.error("%s syntax error", self.path) # todo: likely bug- if a file has this error upon first # read, I think we don't retry it right. return except IOError as e: log.error("%s rereading %s: %r", self.path, self.uri, e) return old = inContext(old, self.uri) new = inContext(new, self.uri) p = Patch.fromDiff(old, new) if p: log.debug("%s applying patch for changes in file", self.path) self.patch(p, dueToFileChange=True) else: log.debug("old == new after reread of %s", self.path)
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 new = i.poll() pollTime[i.uri] = time.time() - now if isinstance(new, dict): # new style oneshot = new['oneshot'] new = new['latest'] else: oneshot = None prev = self._statementsFromInputs.get(i.uri, set()) 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 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) self._influx.exportToInflux( set.union(*[set(v) for v in self._statementsFromInputs.values()]))