def connect_ws(self): log.msg( 'WebSocket protocol has been selected. URL: {0}; HOST: {1}; PORT: {2};' .format(self.ws_url, self.ws_host, self.ws_port)) factory = WebSocketFactory(self) factory.timestamp = self.server_time reactor.connectDeviceHive(self.ws_url, factory)
def send_report(self, cmdid, cmdres): """ Sends command result of a command been invoked to devicehive server. """ def ok(result): LOG_MSG('Command {0} result has been sent.'.format(cmdid)) def err(reason): LOG_MSG('Failed to send command {0} result. Reason: {1}.'.format(cmdid, reason)) factory = RequestFactory(ReportRequest(self.info, self.url, self.host, cmdid, cmdres), ok, err) reactor.connectDeviceHive(self.url, factory)
def subscribe(self, device_id = None, device_key = None): if device_id in self.devices : defer = Deferred() factory = DevicePollFactory(self, self.devices[device_id], defer) self.factories[device_id] = factory LOG_MSG('Connecting command poll factory to {0}:{1}.'.format(self.host, self.port)) reactor.connectDeviceHive(self.url, factory) return defer else : return fail(DhError('Failed to subscribe device "{0}".'.format(device_id)))
def connect(self, url): reactor.connectDeviceHive(url, self)
else : finish_deferred.errback(NotImplementedError('Command is not supported.')) # end do_command def run_me(self): print('do something') i = 0 def looping_call(dev): global i print ('Sending looping notification {0}.'.format(i)) dev.notify('looping_notification', count = i, one_more_parameter = 'parameter_value') i += 1 def init_logging(): log.startLogging(sys.stdout) if __name__ == '__main__' : init_logging() # device = LEDHiveDelegate() led_factory = devicehive.HTTP11DeviceHiveFactory(device_delegate = device) reactor.connectDeviceHive("http://pg.devicehive.com/api/", led_factory) reactor.callWhenRunning(device.run_me) lc = task.LoopingCall(looping_call, device) lc.start(10) reactor.run()
def connect(self, url): LOG_MSG('Connecting to {0} server.'.format(url)) self.url = url reactor.connectDeviceHive(url, self)
def execute_request(self, request, ok, err): factory = RequestFactory(request, ok, err) reactor.connectDeviceHive(self.url, factory)
sleep(0.2) def set_on(self): GPIO.output(_LED_PIN, True) def set_off(self): GPIO.output(_LED_PIN, False) # # main # if __name__ == '__main__' : log.startLogging(sys.stdout) led = LedDevice(_LED_PIN) # Blink on start to ensure device is working led.blink(3) # create a delegate to handle commands device = RasPiDelegate(led) led_factory = devicehive.HTTP11DeviceHiveFactory(device_delegate = device) reactor.connectDeviceHive(_API_URL, led_factory) # create temp sensor and queue it to check for temperature in a separate thread tempSensor = TempSensor(_W1_FILENAME) lc = task.LoopingCall(tempSensor.get_temp, device) lc.start(1) # off we go! reactor.run()
def connect_ws(self): log.msg('WebSocket protocol has been selected. URL: {0}; HOST: {1}; PORT: {2};'.format(self.ws_url, self.ws_host, self.ws_port)) factory = WebSocketFactory(self) factory.timestamp = self.server_time reactor.connectDeviceHive(self.ws_url, factory)