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 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 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
twin_data_json = json.loads(twin_data) twin_properties = twin_data_json['properties'] if 'speed' not in twin_properties['desired']: twin_properties = { 'properties': { 'desired': { 'speed': random.randint(600, 1500), 'spectralProfile': json.dumps(healthy_spectral_profile if device.deviceId != rotor_imbalance_device_id else rotor_imbalance_speactral_profile), 'pressureFactor': 2 if device.deviceId != low_pressure_device_id else 1.5 } } } iot_hub.update_twin(device.deviceId, json.dumps(twin_properties)) processes = [] for device in devices: processes.append( Process(target=device_driver, args=(device.deviceId, ))) for process in processes: process.daemon = True process.start() while all(map(lambda c: c.is_alive(), processes)): time.sleep(3)