def testReadConfig(self): api = ApiClientMock() device = cloud4rpi.Device(api) device.declare({'SomeVar': {'type': 'string'}}) device.declare({'CPUTemp': {'type': 'numeric', 'bind': MockSensor()}}) cfg = device.read_config() self.assertEqual(cfg, [{'name': 'CPUTemp', 'type': 'numeric'}])
def main(): # Put variable declarations here variables = {'STATUS': {'type': 'string', 'bind': listen_for_events}} device = cloud4rpi.Device() device.declare(variables) api = cloud4rpi.connect_mqtt(DEVICE_TOKEN) cfg = device.read_config() api.publish_config(cfg) # Adds a 1 second delay to ensure device variables are created time.sleep(1) try: data_timer = 0 while True: if data_timer <= 0: data = device.read_data() api.publish_data(data) data_timer = DATA_SENDING_INTERVAL data_timer -= POLL_INTERVAL time.sleep(POLL_INTERVAL) except KeyboardInterrupt: cloud4rpi.log.info('Keyboard interrupt received. Stopping...') except Exception as e: error = cloud4rpi.get_error_message(e) cloud4rpi.log.error("ERROR! %s %s", error, sys.exc_info()[0]) finally: sys.exit(0)
def testReadBeforePublishConfig(self): api = ApiClientMock() device = cloud4rpi.Device(api) device.declare({'CPUTemp': {'type': 'numeric', 'bind': MockSensor()}}) device.publish_config() cfg = [{'name': 'CPUTemp', 'type': 'numeric'}] api.publish_config.assert_called_with(cfg)
def main(): # Put variable declarations here variables = { 'Room Temp': { 'type': 'numeric', 'bind': room_temp }, 'Outside Temp': { 'type': 'numeric', 'bind': outside_temp } } # Put system data declarations here diagnostics = { 'CPU Temp': cpu_temp, 'IP Address': ip_address, 'Host Name': hostname, 'Operating System': osname } device = cloud4rpi.Device() device.declare(variables) device.declare_diag(diagnostics) api = cloud4rpi.connect_mqtt(DEVICE_TOKEN) cfg = device.read_config() api.publish_config(cfg) # Adds a 1 second delay to ensure device variables are created sleep(1) try: diag_timer = 0 data_timer = 0 while True: if data_timer <= 0: data = device.read_data() api.publish_data(data) data_timer = DATA_SENDING_INTERVAL if diag_timer <= 0: diag = device.read_diag() api.publish_diag(diag) diag_timer = DIAG_SENDING_INTERVAL diag_timer -= POLL_INTERVAL data_timer -= POLL_INTERVAL sleep(POLL_INTERVAL) except KeyboardInterrupt: cloud4rpi.log.info('Keyboard interrupt received. Stopping...') except Exception as e: error = cloud4rpi.get_error_message(e) cloud4rpi.log.error("ERROR! %s %s", error, sys.exc_info()[0]) finally: sys.exit(0)
def testDeclareDiag(self): api = ApiClientMock() device = cloud4rpi.Device(api) device.declare_diag({ 'IPAddress': '8.8.8.8', 'Host': 'hostname', }) diag = device.read_diag() self.assertEqual(diag, {'IPAddress': '8.8.8.8', 'Host': 'hostname'})
def main(): # Put variable declarations here variables = { 'XIO-P0': { 'type': 'bool', 'value': False, 'bind': P0_control } } # Put system data declarations here diagnostics = { 'IP Address': chip.ip_address, 'Host': gethostname(), 'Operating System': " ".join(uname()), 'CPU Temperature': chip.cpu_temp } device = cloud4rpi.Device() device.declare(variables) device.declare_diag(diagnostics) api = cloud4rpi.connect_mqtt(DEVICE_TOKEN) api.on_command = device.handle_mqtt_commands(api) cfg = device.read_config() api.publish_config(cfg) # Adds a 1 second delay to ensure device variables are created sleep(1) try: diag_timer = 0 data_timer = 0 while True: if data_timer <= 0: data = device.read_data() api.publish_data(data) data_timer = DATA_SENDING_INTERVAL if diag_timer <= 0: diag = device.read_diag() api.publish_diag(diag) diag_timer = DIAG_SENDING_INTERVAL diag_timer -= POLL_INTERVAL data_timer -= POLL_INTERVAL sleep(POLL_INTERVAL) except KeyboardInterrupt: cloud4rpi.log.info('Keyboard interrupt received. Stopping...') sys.exit(0) except Exception as e: error = cloud4rpi.get_error_message(e) cloud4rpi.log.error("ERROR! %s %s", error, sys.exc_info()[0]) sys.exit(1)
def testReadBeforePublishDiag(self): temperature_sensor = MockSensor(24) api = ApiClientMock() device = cloud4rpi.Device(api) device.declare_diag({ 'CPUTemperature': temperature_sensor, 'IPAddress': lambda x: '8.8.8.8', }) device.publish_diag() diag = {'IPAddress': '8.8.8.8', 'CPUTemperature': 24} api.publish_diag.assert_called_with(diag)
def testPublishConfig(self): api = ApiClientMock() device = cloud4rpi.Device(api) cfg = [{ 'name': 'CPUTemp', 'type': 'numeric' }, { 'name': 'Cooler', 'type': 'bool' }] device.publish_config(cfg) api.publish_config.assert_called_with(cfg)
def testCallsBoundFunctionOnCommand(self): handler = Mock() api = ApiClientMock() device = cloud4rpi.Device(api) device.declare( {'LEDOn': { 'type': 'bool', 'value': False, 'bind': handler }}) api.raise_on_command({'LEDOn': True}) handler.assert_called_with(True)
def testReadBeforePublishData(self): temperature_sensor = MockSensor(24) api = ApiClientMock() device = cloud4rpi.Device(api) device.declare({ 'Temperature': { 'type': 'numeric', 'value': True, 'bind': temperature_sensor } }) device.publish_data() data = {'Temperature': 24} api.publish_data.assert_called_with(data)
def testReadVariablesFromClassMethod(self): api = ApiClientMock() device = cloud4rpi.Device(api) sensor = MockSensor(10) device.declare({ 'MyParam': { 'type': 'numeric', 'bind': sensor.get_state }, }) data = device.read_data() self.assertEqual(data, { 'MyParam': 10, })
def main(): variables = { 'STATUS': { 'type': 'string', 'bind': listen_for_events }, 'LED On': { 'type': 'bool', 'value': False, 'bind': led_control }, } device = cloud4rpi.Device() device.declare(variables) api = cloud4rpi.HttpApi(DEVICE_TOKEN) cfg = device.read_config() publish_config(api, cfg) try: data_timer = 0 while True: if data_timer <= 0: res = fetch_commands(api) cmds = json.loads(res.content) for x in cmds: cloud4rpi.log.info('Applying commands: %s', x) device.apply_commands(x) data = device.read_data() publish_data(api, data) data_timer = DATA_SENDING_INTERVAL time.sleep(POLL_INTERVAL) data_timer -= POLL_INTERVAL except KeyboardInterrupt: cloud4rpi.log.info('Keyboard interrupt received. Stopping...') except Exception as e: error = cloud4rpi.get_error_message(e) cloud4rpi.log.error("ERROR! %s %s", error, sys.exc_info()[0]) finally: sys.exit(0)
def testReadDiag(self): temperature_sensor = MockSensor(73) api = ApiClientMock() device = cloud4rpi.Device(api) device.declare_diag({ 'CPUTemperature': temperature_sensor, 'IPAddress': lambda x: '8.8.8.8', 'OSName': lambda x: 'Linux', 'Host': 'weather_station' }) diag = device.read_diag() self.assertEqual( diag, { 'CPUTemperature': 73, 'IPAddress': '8.8.8.8', 'OSName': 'Linux', 'Host': 'weather_station' })
def main(): o2 = omega2.Omega2() # Put variable declarations here variables = { 'Omega LED': { 'type': 'bool', 'value': False, 'bind': o2.led_control } } # Put system data declarations here diagnostics = { 'Host': gethostname(), 'Operating System': " ".join(uname()), 'Omega2 version': 'Omega2 Plus' if 'p' in o2.version else 'Omega2' } device = cloud4rpi.Device() device.declare(variables) device.declare_diag(diagnostics) api = cloud4rpi.connect_mqtt(DEVICE_TOKEN) api.on_command = device.handle_mqtt_commands(api) cfg = device.read_config() api.publish_config(cfg) try: diag = device.read_diag() api.publish_diag(diag) while True: data = device.read_data() api.publish_data(data) sleep(DATA_SENDING_INTERVAL) except KeyboardInterrupt: cloud4rpi.log.info('Keyboard interrupt received. Stopping...') sys.exit(0) except Exception as e: error = cloud4rpi.get_error_message(e) cloud4rpi.log.error("ERROR! %s %s", error, sys.exc_info()[0]) sys.exit(1)
def testReadVariablesAfterCommandApplied(self): api = ApiClientMock() device = cloud4rpi.Device(api) device.declare({ 'LEDOn': { 'type': 'bool', 'value': False, 'bind': lambda x: True }, 'Cooler': { 'type': 'bool', 'value': True, 'bind': lambda x: False } }) api.raise_on_command({'LEDOn': True, 'Cooler': False}) data = device.read_data() self.assertEqual(data, {'LEDOn': True, 'Cooler': False})
def testReadVariables(self): handler = {} temperature_sensor = MockSensor(73) api = ApiClientMock() device = cloud4rpi.Device(api) device.declare({ 'LEDOn': { 'type': 'bool', 'value': False, 'bind': handler }, 'Temperature': { 'type': 'numeric', 'value': True, 'bind': temperature_sensor } }) data = device.read_data() self.assertEqual(data, {'LEDOn': False, 'Temperature': 73})
def testSkipsVarIfItsBindIsNotAFunctionOnCommand(self): api = ApiClientMock() device = cloud4rpi.Device(api) device.declare({ 'LEDOn': { 'type': 'bool', 'value': False, 'bind': 'this is not a function' } }) api.raise_on_command({'LEDOn': True}) # consider behavior in the case of the incorrect bind implementation data = device.read_data() # result of conversation 'this is not a function' to bool self.assertEqual( data, { 'LEDOn': True # False })
def testPublishBackActualVarValuesOnCommand(self): api = ApiClientMock() device = cloud4rpi.Device(api) device.declare({ 'LEDOn': { 'type': 'bool', 'value': False, 'bind': lambda x: True }, 'Cooler': { 'type': 'bool', 'value': True, 'bind': lambda x: False } }) api.raise_on_command({'LEDOn': True, 'Cooler': False}) self.assertEqual(device.read_data(), {'LEDOn': True, 'Cooler': False}) api.publish_data.assert_called_with({'LEDOn': True, 'Cooler': False})
def testReadConfigIfNotDeclared(self): api = ApiClientMock() device = cloud4rpi.Device(api) self.assertEqual(device.read_config(), [])
def testReadVariablesDoesNotContainsEmptyVars(self): api = ApiClientMock() device = cloud4rpi.Device(api) self.assertEqual(device.read_data(), {})
def testPublishData(self): api = ApiClientMock() device = cloud4rpi.Device(api) data = {'Temperature': 36.6, 'Cooler': True, 'TheAnswer': 42} device.publish_data(data) api.publish_data.assert_called_with(data)
def main(): # load w1 modules ds18b20.init_w1() # detect DS18B20 temperature sensors ds_sensors = ds18b20.DS18b20.find_all() # Put variable declarations here variables = { 'Room Temp': { 'type': 'numeric', 'bind': ds_sensors[0] }, # 'Outside Temp': { # 'type': 'numeric', # 'bind': ds_sensors[1] # }, 'CPU Temp': { 'type': 'numeric', 'bind': rpi.cpu_temp } } diagnostics = { 'IP Address': rpi.ip_address, 'Host': gethostname(), 'Operating System': " ".join(uname()) } device = cloud4rpi.Device() device.declare(variables) device.declare_diag(diagnostics) api = cloud4rpi.connect_mqtt(DEVICE_TOKEN) cfg = device.read_config() api.publish_config(cfg) # adds a 1 second delay to ensure device variables are created sleep(1) try: diag_timer = 0 data_timer = 0 while True: if data_timer <= 0: data = device.read_data() api.publish_data(data) data_timer = DATA_SENDING_INTERVAL if diag_timer <= 0: diag = device.read_diag() api.publish_diag(diag) diag_timer = DIAG_SENDING_INTERVAL diag_timer -= POLL_INTERVAL data_timer -= POLL_INTERVAL sleep(POLL_INTERVAL) except KeyboardInterrupt: cloud4rpi.log.info('Keyboard interrupt received. Stopping...') except Exception as e: error = cloud4rpi.get_error_message(e) cloud4rpi.log.error("ERROR! %s %s", error, sys.exc_info()[0]) finally: sys.exit(0)
def testPublishDiag(self): api = ApiClientMock() device = cloud4rpi.Device(api) diag = {'IPAddress': '8.8.8.8', 'Host': 'hostname'} device.publish_diag(diag) api.publish_diag.assert_called_with(diag)