def update_twin(data_structure, vin_list, Campaign_name): iothub_registry_manager = get_registry_manager() for i in vin_list: twin = iothub_registry_manager.get_twin(str(i)) x = {'Campaign_name': Campaign_name, 'eculist': data_structure} twin_patch = Twin(properties=TwinProperties(desired=x)) iothub_registry_manager.update_twin(str(i), twin_patch, twin.etag)
def update_twin(iteration_id, download_uri, rtsp): if iot is None: return if iteration_id in iteration_ids: print('[INFO] This iteration already deployed in the Edge') return try: module = iot.get_module(DEVICE_ID, MODULE_ID) except: print('[ERROR] module does not exist', DEVICE_ID, MODULE_ID) return twin = Twin() twin.properties = TwinProperties( desired={ 'inference_files_zip_url': download_uri, 'cam_type': 'rtsp_stream', 'cam_source': rtsp }) iot.update_module_twin(DEVICE_ID, MODULE_ID, twin, module.etag) print('[INFO] Updated IoT Module Twin with uri and rtsp', download_uri, rtsp) iteration_ids.add(iteration_id)
def update_twin(iteration_id, download_uri, rtsp): """Update twin""" if iot is None: return if iteration_id in iteration_ids: logger.info("This iteration already deployed on the Edge") return try: module = iot.get_module(DEVICE_ID, MODULE_ID) except: logger.error("Module does not exist. Device ID: %s, Module ID: %s", DEVICE_ID, MODULE_ID) return twin = Twin() twin.properties = TwinProperties( desired={ "inference_files_zip_url": download_uri, "cam_type": "rtsp_stream", "cam_source": rtsp, }) iot.update_module_twin(DEVICE_ID, MODULE_ID, twin, module.etag) logger.info( "Updated IoT Module Twin with uri and rtsp. Download URI: %s RSTP: %s", download_uri, rtsp, ) iteration_ids.add(iteration_id)
def create_twin_update_job_request(): job = JobRequest() job.job_id = "twinjob-" + str(uuid.uuid4()) job.type = "scheduleUpdateTwin" job.start_time = datetime.datetime.utcnow().isoformat() job.update_twin = Twin( etag="*", properties=TwinProperties(desired={"temperature": 98.6})) job.query_condition = "*" return job
def update_desired_property_and_file(DEVICE_ID, version, file_name): try: iothub_registry_manager = get_registry_manager() twin = iothub_registry_manager.get_twin(DEVICE_ID) twin_patch = Twin(properties=TwinProperties(desired={ 'fwVersion': version, 'fwPackageURI': file_name })) iothub_registry_manager.update_twin(DEVICE_ID, twin_patch, twin.etag) except: print("something went wrong")
def stdin_listener(): iothub_registry_manager = IoTHubRegistryManager( IOTHUB_CONNECTION_STRING) twin = iothub_registry_manager.get_twin(DEVICE_ID) while True: selection = input("Q: quit, F: forward, S: stop/free\n") if selection == "Q" or selection == "q": print("Quitting...") break elif selection == "F" or selection == "f": twin_patch = Twin(properties=TwinProperties( desired={'command': 'forward'})) twin = iothub_registry_manager.update_twin( DEVICE_ID, twin_patch, twin.etag) elif selection == "S" or selection == "s": twin_patch = Twin(properties=TwinProperties( desired={'command': 'stop'})) twin = iothub_registry_manager.update_twin( DEVICE_ID, twin_patch, twin.etag)
def handle_twin(twin): codes_start = int(time.time()) #print("Twin received", twin) if ('desired' in twin): desired = twin['desired'] #print(desired['code']) code = str(desired['code']) #Create a new executable file if (desired['updated'] == 1): print('code is updated') fin = open("setup.c", "rt") #output file to write the result to name = "test{}".format(codes_start) nfout = "{}.c".format(name) sofout = "{}.so".format(name) fout = open(nfout, "wt") print(nfout, 'name of code') #for each line in the input file for line in fin: #read replace the string and write to output file fout.write(line) fout.write(code) fout.write("shutdown(sockfd, SHUT_RDWR);}") #close input and output files fout.close() fin.close() print("files are written") time.sleep(0.3) #make the binding to the so file try: bashCommand = "cc -fPIC -shared -o {} {}".format(sofout, nfout) import subprocess subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE) except: print('failed making binding') time.sleep(0.3) print('so is bounded') #call uploaded code from ctypes import CDLL so_file = "/home/pi/remotelab/{}".format(sofout) try: my_functions = CDLL(so_file) my_functions.main() del my_functions print('code is executed') except: print('code could not be executed') #codes_start = codes_start + 1 twin_patch = Twin(properties=TwinProperties( desired={'updated': 0})) iothub_registry_manager.update_twin(DEVICE_ID, twin_patch, twin2.etag)
def update_multiple_device(DEVICE_ID_LIST, version, file_name): try: iothub_registry_manager = get_registry_manager() for device_name in DEVICE_ID_LIST: twin = iothub_registry_manager.get_twin(device_name) twin_patch = Twin(properties=TwinProperties( desired={ 'fwVersion': version, 'fwPackageURI': file_name })) iothub_registry_manager.update_twin(device_name, twin_patch, twin.etag) except: print("something went wrong")
print_device_info("update_device", device_updated) # Get device twin twin = iothub_registry_manager.get_twin(device_id) print(twin) print("") additional_props = twin.additional_properties if "modelId" in additional_props: print("Model id for digital twin is") print("ModelId:" + additional_props["modelId"]) # # Replace twin new_twin = Twin() new_twin = twin new_twin.properties = TwinProperties(desired={"telemetryInterval": 9000}) print(new_twin) print("") replaced_twin = iothub_registry_manager.replace_twin(device_id, new_twin) print(replaced_twin) print("") # Update twin twin_patch = Twin() twin_patch.properties = TwinProperties(desired={"telemetryInterval": 3000}) updated_twin = iothub_registry_manager.update_twin(device_id, twin_patch, twin.etag) print(updated_twin) print("The twin patch has been successfully applied")
print("The device twin is: ") print("") print(twin) print("") # Print the device's model ID additional_props = twin.additional_properties if "modelId" in additional_props: print("The Model ID for this device is:") print(additional_props["modelId"]) print("") # Update twin twin_patch = Twin() twin_patch.properties = TwinProperties( desired={"targetTemperature": 42}) # this is relevant for the thermostat device sample updated_twin = iothub_registry_manager.update_twin(device_id, twin_patch, twin.etag) print("The twin patch has been successfully applied") print("") # invoke device method device_method = CloudToDeviceMethod(method_name=method_name, payload=method_payload) iothub_registry_manager.invoke_device_method(device_id, device_method) print("The device method has been successfully invoked") print("") # Set registry manager object to `None` so all open files get closed iothub_registry_manager = None
"3. 20 seconds \n" "4. Input your Value \n" )) if defined_telemetry_value == 1: telemetry_interval_value = 5 if defined_telemetry_value == 2: telemetry_interval_value = 10 if defined_telemetry_value == 3: telemetry_interval_value = 20 if defined_telemetry_value == 4: telemetry_interval_value = defined_device_option try: # Create IoTHubRegistryManager iothub_registry_manager = IoTHubRegistryManager(connection_str) twin = iothub_registry_manager.get_twin(device_id) # Update twin twin_patch = Twin() twin_patch = twin twin_patch.properties = TwinProperties(desired={"Telemetry_Interval": telemetry_interval_value}) updated_twin = iothub_registry_manager.update_twin(device_id, twin_patch, twin.etag) print("Set Telemetry Interval to: " + str(telemetry_interval_value) + "\n", updated_twin) print("") except Exception as ex: print("Unexpected error {0}".format(ex)) except KeyboardInterrupt: print("iothub_registry_manager_sample stopped") if defined_device_option == 4: # Set Send_Data Switch defined_Send_Data_value = int(input( "1. Set Send_Data ON \n" "2. Set Send Data OFF \n" ))
import sys from time import sleep from azure.iot.hub import IoTHubRegistryManager from azure.iot.hub.models import Twin, TwinProperties, QuerySpecification, QueryResult iothub_registry_manager = IoTHubRegistryManager("Service Connection String") twin = iothub_registry_manager.get_twin("Device_id") twin_patch = Twin(properties=TwinProperties( desired={'Vision_Model_Version': 1.2})) twin = iothub_registry_manager.update_twin(DEVICE_ID, twin_patch, twin.etag) query_spec = QuerySpecification( query= "SELECT * FROM devices WHERE properties.reported.Vision_Model_Version <> 1.2" ) query_result = iothub_registry_manager.query_iot_hub(query_spec, None, 100) print("Devices that did not update: {}".format(', '.join( [twin.device_id for twin in query_result.items]))) print() query_spec = QuerySpecification( query= "SELECT * FROM devices WHERE tags.location.plant = 'Redmond43' AND properties.reported.connectivity = 'cellular'" ) query_result = iothub_registry_manager.query_iot_hub(query_spec, None, 100) print("Devices in Redmond43 plant using cellular network: {}".format(', '.join( [twin.device_id for twin in query_result.items])))