def create_iot_device(self, connect_string, device_name):
        dev_connect = ""
        try:
            helper = Helper(connect_string)
            helper.create_device(device_name)
            print("create_iot_device: ({}) returned: ({})".format(device_name, 'OK'))
            dev_connect = helper.get_device_connection_string(device_name)
        except:
             print(Fore.RED + "Exception creating device: " + device_name, file=sys.stderr)
             traceback.print_exc()
             print(Fore.RESET, file=sys.stderr)

        return dev_connect
 def create_iot_device(self,
                       hub_connect_string,
                       device_name,
                       is_edge=False):
     dev_connect = ""
     try:
         helper = Helper(hub_connect_string)
         helper.create_device(device_name, is_edge)
         dev_connect = helper.get_device_connection_string(device_name)
     except:
         print(Fore.RED + "Exception creating device: " + device_name,
               file=sys.stderr)
         traceback.print_exc()
         sys.exit(-1)
     return dev_connect
Exemple #3
0
class EdgeHub:
    def __init__(self, service_connection_string, edge_hub_device_id=None):
        self.service_connection_string = service_connection_string
        self.helper = Helper(self.service_connection_string)
        if not edge_hub_device_id:
            self._createNewHub()
        else:
            self._useExistingHub(edge_hub_device_id)

    def _createNewHub(self):
        self.edge_hub_device_id = get_random_device_name().lower()
        self.helper.create_device(self.edge_hub_device_id, True)
        self._finishHubSetup()

    def _useExistingHub(self, edge_hub_device_id):
        self.edge_hub_device_id = edge_hub_device_id
        self._finishHubSetup()

    def _finishHubSetup(self):
        self.edge_hub_connection_string = self.helper.get_device_connection_string(
            self.edge_hub_device_id)

        self.leaf_device_id = self.edge_hub_device_id + "_leaf_device"
        try:
            self.leaf_device_connection_string = self.helper.get_device_connection_string(
                self.leaf_device_id)
        except:
            self.helper.create_device(self.leaf_device_id)
            self.leaf_device_connection_string = self.helper.get_device_connection_string(
                self.leaf_device_id)

        self._discoverAllExistingModules()

    def _discoverAllExistingModules(self):
        self.modules = []
        for name in all_containers:
            mod = all_containers[name]
            try:
                thisModString = self.helper.get_module_connection_string(
                    self.edge_hub_device_id, mod.module_id)
            except:
                pass
            else:
                mod.connection_string = thisModString
                self.modules.append(mod)

    def deployModules(self, modules):
        configuration = EdgeConfiguration()
        configuration.add_module(all_containers["friend"])

        for name in modules.split(","):
            name = name.strip()
            if name in all_containers:
                configuration.add_module(all_containers[name])
            else:
                print("module {} is invalid".format(name))
                print("valid modules are {0}".format(", ".join(
                    all_containers.keys())))
                raise Exception("module " + name + " not defined")

        self.helper.apply_configuration(self.edge_hub_device_id,
                                        configuration.get_module_config())