def get_device_twin(device_id): iot_hub = IoTHub(os.environ['IOT_HUB_NAME'], os.environ['IOT_HUB_OWNER_KEY']) twin_data = iot_hub.get_device_twin(device_id) resp = Response(twin_data) resp.headers['Content-type'] = 'application/json' return resp
def device_driver(): driver_unique_id = str(uuid.uuid4()) iot_hub = IoTHub(IOT_HUB_NAME, IOT_HUB_OWNER_KEY) device, device_twin = iot_hub.claim_device(driver_unique_id) device_twin_json = json.loads(device_twin) device_id = device_twin_json['deviceId'] iothub_device = IoTHubDevice(IOT_HUB_NAME, device_id, device.primaryKey) def report_state(state): iothub_device.send_reported_state(state) def send_telemetry(data): iothub_device.send_message(data) device_simulator = SimulatorFactory.create('devices.engines.Engine', report_state, send_telemetry) device_simulator.initialize(device_twin_json) def device_twin_callback(update_state, payload, user_context): device_simulator.on_update(update_state, json.loads(payload)) iothub_device.client.set_device_twin_callback(device_twin_callback, 0) device_simulator.run()
def telemetry(): iot_hub = IoTHub(os.environ['IOT_HUB_NAME'], os.environ['IOT_HUB_OWNER_KEY']) assets = table_service.query_entities('equipment') devices = iot_hub.get_device_list() devices.sort(key=lambda x: x.deviceId) return render_template('telemetry.html', assets=devices)
def claim_and_run_device(driver_id): iot_hub = IoTHub(IOT_HUB_NAME, IOT_HUB_OWNER_KEY) device, device_twin = iot_hub.claim_device(driver_id) device_twin_json = json.loads(device_twin) device_id = device_twin_json['deviceId'] iothub_device = IoTHubDevice(IOT_HUB_NAME, device_id, device.primaryKey) append_blob_service = AppendBlobService(account_name=STORAGE_ACCOUNT_NAME, account_key=STORAGE_ACCOUNT_KEY) logs_container_name = 'logs' append_blob_service.create_container(logs_container_name, fail_on_exist=False) log_blob_name = '{0}.log'.format(device_id) def report_state(state): iothub_device.send_reported_state(state) def send_telemetry(data): iothub_device.send_message(data) def log(message, code, level): if not append_blob_service.exists(logs_container_name, log_blob_name): append_blob_service.create_blob( logs_container_name, log_blob_name, if_none_match='*', ) level_name = logging.getLevelName(level) output = io.StringIO() entry_data = [ str(datetime.datetime.utcnow()) + 'Z', level_name, device_id, code, message ] writer = csv.writer(output, quoting=csv.QUOTE_MINIMAL) writer.writerow(entry_data) entry_text = output.getvalue() append_blob_service.append_blob_from_text(logs_container_name, log_blob_name, entry_text) device_simulator = SimulatorFactory.create('devices.engines.Engine', report_state, send_telemetry, log) if not device_simulator.initialize(device_twin_json): return def device_twin_callback(update_state, payload, user_context): device_simulator.on_update(str(update_state), json.loads(payload)) iothub_device.client.set_device_twin_callback(device_twin_callback, 0) device_simulator.run()
def set_desired_properties(device_id): speed = request.form["speed"] payload = {'properties': {'desired': {'speed': int(speed)}}} payload_json = json.dumps(payload) iot_hub = IoTHub(os.environ['IOT_HUB_NAME'], os.environ['IOT_HUB_OWNER_KEY']) twin_data = iot_hub.update_twin(device_id, payload_json) resp = Response(twin_data) resp.headers['Content-type'] = 'application/json' return resp
def set_desired_properties(device_id): desired_props = {} for key in request.form: if key == 'speed': desired_props[key] = int(request.form[key]) else: desired_props[key] = request.form[key] payload = {'properties': {'desired': desired_props}} payload_json = json.dumps(payload) iot_hub = IoTHub(os.environ['IOT_HUB_NAME'], os.environ['IOT_HUB_OWNER_KEY']) twin_data = iot_hub.update_twin(device_id, payload_json) resp = Response(twin_data) resp.headers['Content-type'] = 'application/json' return resp
def create_devices(): iot_hub = IoTHub(os.environ['IOT_HUB_NAME'], os.environ['IOT_HUB_OWNER_KEY']) iot_device_count = 10 devices = [] for i in range(iot_device_count): device_id = 'MACHINE-{0:03d}'.format(i) device = iot_hub.create_device(device_id) devices.append(device) rotor_imbalance_device_id = devices[-1].deviceId low_pressure_device_id = devices[-2].deviceId def failure_onset(device_id): if device_id == rotor_imbalance_device_id: return 'F01' if device_id == low_pressure_device_id: return 'F02' return None for device in devices: twin_properties = { 'tags': { 'simulated': True, 'simulator': 'devices.engines.Engine' }, 'properties': { 'desired': { 'speed': random.randint(600, 1500), 'mode': 'auto', 'failureOnset': failure_onset(device.deviceId) } } } iot_hub.update_twin(device.deviceId, json.dumps(twin_properties)) return redirect(url_for('telemetry'))
def telemetry(): iot_hub = IoTHub(os.environ['IOT_HUB_NAME'], os.environ['IOT_HUB_OWNER_KEY']) devices = iot_hub.get_device_list() devices.sort(key = lambda x: x.deviceId) return render_template('telemetry.html', assets = devices)
state['temperature'], 'pressure': state['pressure'], 'ambientTemperature': state['ambient_temperature'], 'ambientPressure': state['ambient_pressure'] }) time_elapsed = time.time() - interval_start # print('Cadence: {0}'.format(time_elapsed)) time.sleep(max(1 - time_elapsed, 0)) if __name__ == '__main__': iot_hub = IoTHub(IOT_HUB_NAME, IOT_HUB_OWNER_KEY) iot_device_count = 10 devices = iot_hub.get_device_list() if len(devices) == 0: devices = [] for i in range(iot_device_count): device_id = 'MACHINE-{0:03d}'.format(i) device = iot_hub.create_device(device_id) devices.append(device) healthy_spectral_profile = { 'W': [1, 2, 3, 4, 5, 12, 15], 'A': [5, 8, 2 / 3, 9, 8, 13, 5]