class Ticker(object): """ """ def __init__(self, *args, **kwargs): self.counter = 0 self._nodes = {} self._pre_tick_fns = kwargs.get('pre_tick', []) self._post_tick_fns = kwargs.get('post_tick', []) if hasattr(self._pre_tick_fns, '__call__'): self._pre_tick_fns = [self._pre_tick_fns] if hasattr(self._post_tick_fns, '__call__'): self._post_tick_fns = [self._post_tick_fns] self._watcher = Watcher(pre_cycle=self._doPreTick, post_cycle=self._doEmits) for node in args: self.watch(node) return super(Ticker, self).__init__() def addPreTick(self, *args): self._pre_tick_fns += args def addPostTick(self, *args): self._post_tick_fns += args def _doPreTick(self): for fn in self._pre_tick_fns: fn(self) def _doPostTick(self): for fn in self._post_tick_fns: fn(self) def _doEmits(self): for node_id in self._nodes: self._nodes[node_id].emitData() self.counter += 1 self._doPostTick() def start(self, period=10, duration=float('inf')): self._watcher.start(period=period, duration=duration, silent=True) def watch(self, node): self._nodes[node.id] = node if hasattr(node, 'device'): self._watcher.watch(node.device)
def __init__(self, *args, **kwargs): self.counter = 0 self._nodes = {} self._pre_tick_fns = kwargs.get('pre_tick', []) self._post_tick_fns = kwargs.get('post_tick', []) if hasattr(self._pre_tick_fns, '__call__'): self._pre_tick_fns = [self._pre_tick_fns] if hasattr(self._post_tick_fns, '__call__'): self._post_tick_fns = [self._post_tick_fns] self._watcher = Watcher(pre_cycle=self._doPreTick, post_cycle=self._doEmits) for node in args: self.watch(node) return super(Ticker, self).__init__()
from ninja.api import NinjaAPI, Watcher from ninja.devices import TemperatureSensor, HumiditySensor from datetime import datetime # Set up the NinjaAPI and Device wrappers: # Access token from https://a.ninja.is/you#apiTab api = NinjaAPI(settings.ACCESS_TOKEN) device1 = TemperatureSensor(api, settings.TEMP_ID) device2 = HumiditySensor(api, settings.HUMIDITY_ID) # The watcher will provide a single loop for polling all of the devices. watcher = Watcher(device1, device2) # Output the temperature to stdio. def printTempCelsius(inst, temperature): date = inst.last_read.isoformat() print '{date} - {id}: {temperature} C'.format( date=date, id=inst.guid, temperature=temperature.c, ) def printRelHumidity(inst, humidity): date = inst.last_read.isoformat() print '{date} - {id}: {humidity}%'.format(
from ninja.api import NinjaAPI, Watcher from ninja.devices import TemperatureSensor, HumiditySensor from datetime import datetime # Set up the NinjaAPI and Device wrappers: # Access token from https://a.ninja.is/you#apiTab api = NinjaAPI(settings.ACCESS_TOKEN) device1 = TemperatureSensor(api, settings.TEMP_ID) device2 = HumiditySensor(api, settings.HUMIDITY_ID) # The watcher will provide a single loop for polling all of the devices. watcher = Watcher(device1, device2) # Output the temperature to stdio. def printTempCelsius(inst, temperature): date = inst.last_read.isoformat() print "{date} - {id}: {temperature} C".format(date=date, id=inst.guid, temperature=temperature.c) def printRelHumidity(inst, humidity): date = inst.last_read.isoformat() print "{date} - {id}: {humidity}%".format(date=date, id=inst.guid, humidity=humidity) # Bind the output to the heartbeat event. device1.onHeartbeat(printTempCelsius)