def create_device_module(self, connect_string, device_name, module_name):
     mod_connect = None
     try:
         helper = Helper(connect_string)
         helper.create_device_module(device_name, module_name)
         print("create_device_module: ({}/{}) returned: ({})".format(device_name, module_name, 'OK'))
         mod_connect = helper.get_module_connection_string(device_name, module_name)
     except:
          print(Fore.RED + "Exception creating device: {}/{}".format(device_name, module_name), file=sys.stderr)
          traceback.print_exc()
          print(Fore.RESET, file=sys.stderr)
     return mod_connect
 def create_device_module(self, connect_string, device_name, module_name):
     mod_connect = ""
     try:
         helper = Helper(connect_string)
         helper.create_device_module(device_name, module_name)
         mod_connect = helper.get_module_connection_string(
             device_name, module_name)
     except:
         print(Fore.RED + "Exception creating module: {}/{}".format(
             device_name, module_name),
               file=sys.stderr)
         traceback.print_exc()
         sys.exit(-1)
     return mod_connect
    def get_edge_modules_connect_string(self, save_manifest_file):
        deployment_json = self.get_deployment_model_json(save_manifest_file)
        hub_connect_string = self.get_env_connect_string()
        json_updated = False
        try:
            identity_json = deployment_json['identities']
            service_helper = Helper(hub_connect_string)

            for azure_device in identity_json:
                device_json = identity_json[azure_device]
                if device_json['objectType'] == "iotedge_device":
                    device_id = device_json['deviceId']
                    if 'modules' in device_json:
                        modules = device_json['modules']
                        for module_name in modules:
                            module_json = modules[module_name]
                            try:
                                module_connect_string = service_helper.get_module_connection_string(
                                    device_id, module_name)
                            except:
                                module_connect_string = "Module connect Not Found"
                            module_json[
                                'connectionString'] = module_connect_string
                            json_updated = True

                        device_json['modules'][module_name] = module_json
                    deployment_json['identities'][azure_device] = device_json
            if json_updated:
                with open(save_manifest_file, 'w') as f:
                    f.write(
                        json.dumps(deployment_json,
                                   default=lambda x: x.__dict__,
                                   sort_keys=False,
                                   indent=2))
        except:
            print(Fore.RED + "Exception Processing HortonManifest: " +
                  save_manifest_file,
                  file=sys.stderr)
            traceback.print_exc()
            sys.exit(-1)
Ejemplo n.º 4
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())