def iothub_twin_sample_run(): try: iothub_twin_method = IoTHubDeviceTwin(CONNECTION_STRING) if (MODULE_ID is None): # Query for device twin twin_info = iothub_twin_method.get_twin(DEVICE_ID) else: # Query for module twin twin_info = iothub_twin_method.get_twin(DEVICE_ID, MODULE_ID) print("") print("Twin before update :") print("{0}".format(twin_info)) if (MODULE_ID is None): # Update device twin twin_info = iothub_twin_method.update_twin(DEVICE_ID, UPDATE_JSON) else: # Update module twin twin_info = iothub_twin_method.update_twin(DEVICE_ID, MODULE_ID, UPDATE_JSON) print("") print("Twin after update :") print("{0}".format(twin_info)) except IoTHubError as iothub_error: print("") print("Unexpected error {0}" % iothub_error) return except KeyboardInterrupt: print("") print("IoTHubModuleTwin sample stopped")
class IoTHub: def __init__(self, iothub_name, owner_key, suffix='.azure-devices.net'): self.iothub_name = iothub_name self.owner_key = owner_key self.suffix = suffix self.owner_connection_string = 'HostName={0}{1};SharedAccessKeyName=iothubowner;SharedAccessKey={2}'.format( self.iothub_name, self.suffix, owner_key) self.registry_manager = IoTHubRegistryManager( self.owner_connection_string) self.device_twin = IoTHubDeviceTwin(self.owner_connection_string) self.__device_clients = {} def create_device(self, device_id, primary_key='', secondary_key=''): return self.registry_manager.create_device( device_id, primary_key, secondary_key, IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY) def get_device_list(self): return self.registry_manager.get_device_list( 1000) # NOTE: this API is marked as deprecated, # but Python SDK doesn't seem to offer # an alternative yet (03/25/2018). def get_device_twin(self, device_id): return self.device_twin.get_twin(device_id) def update_twin(self, device_id, payload): return self.device_twin.update_twin(device_id, payload)
def _update_reboot(self): UPDATE_JSON = "{\"properties\":{\"desired\":{\"reboot\":\"%s\"}}}" try: post = json.load(request.body) msg_json = UPDATE_JSON % (str(post['reboot'])) iothub_twin_method = IoTHubDeviceTwin(CONNECTION_STRING) twin_info = iothub_twin_method.update_twin(DEVICE_ID, msg_json) except Exception as err: return str(err) return "200 OK\n"
def iothub_devicetwin(): iothub_twin_method = IoTHubDeviceTwin(CONNECTION_STRING) twin_info = iothub_twin_method.get_twin(DEVICE_ID) print("") print("Device Twin before update :") print("{0}".format(twin_info)) twin_info = iothub_twin_method.update_twin(DEVICE_ID, TWIN_MSG) print("") print("Device Twin after update :") print("{0}".format(twin_info))
def _update_device_twin(self): #self._enable_cors() UPDATE_JSON = "{\"properties\":{\"desired\":{\"software_version\":\"%s\", \"reboot\":\"%s\", \"url\":\"%s\"}}}" try: post = json.load(request.body) msg_json = UPDATE_JSON % (str(post['software_version']), str(post['reboot']), str(post['url'])) iothub_twin_method = IoTHubDeviceTwin(CONNECTION_STRING) twin_info = iothub_twin_method.update_twin(DEVICE_ID, msg_json) except Exception as err: return str(err) return "200 OK\n"
def _update_log_time(self): self._enable_cors() UPDATE_JSON = "{\"properties\":{\"desired\":{\"time\":\"%s\"}}}" try: byte_str = request.body.read() text_obj = byte_str.decode('UTF-8') post = json.loads(text_obj) msg_json = UPDATE_JSON % (str(post['time'])) iothub_twin_method = IoTHubDeviceTwin(CONNECTION_STRING) twin_info = iothub_twin_method.update_twin(DEVICE_ID, msg_json) except Exception as err: return str(err) return "200 OK\n"
def iothub_devicetwin_sample_run(): try: iothub_twin_method = IoTHubDeviceTwin(CONNECTION_STRING) twin_info = iothub_twin_method.get_twin(DEVICE_ID) print("") print("Device Twin before update :") print("{0}".format(twin_info)) twin_info = iothub_twin_method.update_twin(DEVICE_ID, UPDATE_JSON) print("") print("Device Twin after update :") print("{0}".format(twin_info)) except IoTHubError as iothub_error: print("") print("Unexpected error {0}" % iothub_error) return except KeyboardInterrupt: print("") print("IoTHubDeviceTwin sample stopped")
def run_e2e_twin(iothub_connection_string, testing_modules): try: # prepare device_id = generate_device_name() assert isinstance(device_id, str), 'Invalid type returned!' primary_key = "" secondary_key = "" auth_method = IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY iothub_registry_manager = IoTHubRegistryManager( iothub_connection_string) new_device = iothub_registry_manager.create_device( device_id, primary_key, secondary_key, auth_method) if testing_modules == True: new_module = iothub_registry_manager.create_module( device_id, primary_key, secondary_key, TEST_MODULE_ID, auth_method) ########################################################################### # IoTHubDeviceTwin # act iothub_device_twin = IoTHubDeviceTwin(IOTHUB_CONNECTION_STRING) # verify assert iothub_device_twin != None, "iothub_device_twin is NULL" ########################################################################### ########################################################################### # Wait before get twin... sleep_before_device_action() ########################################################################### # get_twin # act if testing_modules == True: twin_info = iothub_device_twin.get_twin(device_id, TEST_MODULE_ID) else: twin_info = iothub_device_twin.get_twin(device_id) # verify assert twin_info != None, "twin_info is NULL" json_ok = twin_info.find("deviceId") assert json_ok > 0, "twin_info does not contain deviceId tag" json_ok = twin_info.find(device_id) assert json_ok > 0, "twin_info does not contain the correct device id" if testing_modules == True: json_ok = twin_info.find("moduleId") assert json_ok > 0, "twin_info does not contain moduleId tag" json_ok = twin_info.find("etag") assert json_ok > 0, "twin_info does not contain etag tag" json_ok = twin_info.find("properties") assert json_ok > 0, "twin_info does not contain properties tag" ########################################################################### print("") print("Twin before update:") print("{0}".format(twin_info)) ########################################################################### # update_twin # prepare new_property_name = "telemetryInterval" new_property_value = "42" UPDATE_JSON = "{\"properties\":{\"desired\":{\"" + new_property_name + "\":" + new_property_value + "}}}" # act if testing_modules == True: twin_info = iothub_device_twin.update_twin(device_id, TEST_MODULE_ID, UPDATE_JSON) else: twin_info = iothub_device_twin.update_twin(device_id, UPDATE_JSON) # verify assert twin_info != None, "twin_info is NULL" json_ok = twin_info.find("deviceId") assert json_ok > 0, "twin_info does not contain deviceId tag" json_ok = twin_info.find(device_id) assert json_ok > 0, "twin_info does not contain the correct device id" json_ok = twin_info.find("etag") assert json_ok > 0, "twin_info does not contain etag tag" json_ok = twin_info.find("properties") assert json_ok > 0, "twin_info does not contain properties tag" json_ok = twin_info.find(new_property_name) assert json_ok > 0, "twin_info does not contain " + new_property_name + " tag" json_ok = twin_info.find(new_property_value) assert json_ok > 0, "twin_info does not contain " + new_property_value ########################################################################### print("") print("Device Twin after update:") print("{0}".format(twin_info)) print("") retval = 0 except Exception as e: print("") print("run_e2e_twin() failed with exception: {0}".format(e)) retval = 1 finally: # clean-up if testing_modules == True: iothub_registry_manager.delete_module(device_id, TEST_MODULE_ID) iothub_registry_manager.delete_device(device_id) return retval
def run_e2e_devicetwin(iothub_connection_string): try: # prepare device_id = generate_device_name() assert isinstance(device_id, str), 'Invalid type returned!' primary_key = "" secondary_key = "" auth_method = IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY iothub_registry_manager = IoTHubRegistryManager(iothub_connection_string) new_device = iothub_registry_manager.create_device(device_id, primary_key, secondary_key, auth_method) ########################################################################### # IoTHubDeviceTwin # act iothub_device_twin = IoTHubDeviceTwin(IOTHUB_CONNECTION_STRING) # verify assert iothub_device_twin != None, "iothub_device_twin is NULL" ########################################################################### ########################################################################### # Wait before get twin... time.sleep(SLEEP_BEFORE_DEVICE_ACTION) ########################################################################### # get_twin # act twin_info = iothub_device_twin.get_twin(device_id) # verify assert twin_info != None, "twin_info is NULL" json_ok = twin_info.find("deviceId") assert json_ok > 0, "twin_info does not contain deviceId tag" json_ok = twin_info.find(device_id) assert json_ok > 0, "twin_info does not contain the correct device id" json_ok = twin_info.find("etag") assert json_ok > 0, "twin_info does not contain etag tag" json_ok = twin_info.find("properties") assert json_ok > 0, "twin_info does not contain properties tag" ########################################################################### print ( "" ) print ( "Device Twin before update:" ) print ( "{0}".format(twin_info) ) ########################################################################### # update_twin # prepare new_property_name = "telemetryInterval" new_property_value = "42" UPDATE_JSON = "{\"properties\":{\"desired\":{\"" + new_property_name + "\":" + new_property_value + "}}}" # act twin_info = iothub_device_twin.update_twin(device_id, UPDATE_JSON) # verify assert twin_info != None, "twin_info is NULL" json_ok = twin_info.find("deviceId") assert json_ok > 0, "twin_info does not contain deviceId tag" json_ok = twin_info.find(device_id) assert json_ok > 0, "twin_info does not contain the correct device id" json_ok = twin_info.find("etag") assert json_ok > 0, "twin_info does not contain etag tag" json_ok = twin_info.find("properties") assert json_ok > 0, "twin_info does not contain properties tag" json_ok = twin_info.find(new_property_name) assert json_ok > 0, "twin_info does not contain " + new_property_name + " tag" json_ok = twin_info.find(new_property_value) assert json_ok > 0, "twin_info does not contain " + new_property_value ########################################################################### print ( "" ) print ( "Device Twin after update:" ) print ( "{0}".format(twin_info) ) print ( "" ) retval = 0 except Exception as e: print ( "" ) print ("run_e2e_devicetwin() failed with exception: {0}".format(e)) retval = 1 finally: # clean-up iothub_registry_manager.delete_device(device_id) return retval