def test_registry__register_controllers(registry_fixture): registry = Registry(registry_filename=TEST_FILENAME) registry.reset_defaults() new_controller = copy.deepcopy(DEFAULT_REGISTRY["Controllers"][CTYPE][0]) new_name = new_controller["name"] + "_new" new_controller["name"] = new_name registry.register_controller(CTYPE, new_controller) # Test that the the changes are reflected with a new instance. registry2 = Registry(registry_filename=TEST_FILENAME) controllers1 = registry.list_controllers(CTYPE) controllers2 = registry2.list_controllers(CTYPE) for data1, data2 in zip(controllers1, controllers2): for field in DEFAULT_REGISTRY["Controllers"][CTYPE][0]: assert data1[field] == data2[field] registry2.unregister_controller(CTYPE, new_name) assert not registry2.is_controller_registered(CTYPE, new_name)
def read_controller_settings_from_registry(path): registry = Registry() controllers = defaultdict(dict) controller_settings = defaultdict(dict) filenames = os.listdir(path) for filename in filenames: controller_type, ext = os.path.splitext(filename) # This file contains a mapping of controller to an array of names. # The controller settings must be stored in the PyDSS registry. controller_to_name = load_data(os.path.join(path, filename)) for controller, names in controller_to_name.items(): settings = controller_settings[controller_type].get(controller) if settings is None: if not registry.is_controller_registered( controller_type, controller): raise InvalidConfiguration( f"{controller_type} / {controller} is not registered") settings = registry.read_controller_settings( controller_type, controller) controller_settings[controller_type][controller] = settings for name in names: controllers[controller_type][name] = settings return controllers
def test_registry__is_controller_registered(registry_fixture): registry = Registry(registry_filename=TEST_FILENAME) registry.reset_defaults() assert registry.is_controller_registered( CTYPE, DEFAULT_REGISTRY["Controllers"][CTYPE][0]["name"])
def update_pydss_controllers(project_path, scenario, controller_type, controller, dss_file): """Update a scenario's controllers from an OpenDSS file. Parameters ---------- project_path : str PyDSS project path. scenario : str PyDSS scenario name in project. controller_type : str A type of PyDSS controler controller : str The controller name dss_file : str A DSS file path """ if controller_type not in READ_CONTROLLER_FUNCTIONS: supported_types = list(READ_CONTROLLER_FUNCTIONS.keys()) print(f"Currently only {supported_types} types are supported") sys.exit(1) sim_file = os.path.join(project_path, SIMULATION_SETTINGS_FILENAME) config = load_data(sim_file) if not config["Project"].get("Use Controller Registry", False): print(f"'Use Controller Registry' must be set to true in {sim_file}") sys.exit(1) registry = Registry() if not registry.is_controller_registered(controller_type, controller): print(f"{controller_type} / {controller} is not registered") sys.exit(1) data = {} filename = f"{project_path}/Scenarios/{scenario}/pyControllerList/{controller_type}.toml" if os.path.exists(filename): data = load_data(filename) for val in data.values(): if not isinstance(val, list): print(f"{filename} has an invalid format") sys.exit(1) element_names = READ_CONTROLLER_FUNCTIONS[controller_type](dss_file) num_added = 0 if controller in data: existing = set(data[controller]) final = list(existing.union(set(element_names))) data[controller] = final num_added = len(final) - len(existing) else: data[controller] = element_names num_added = len(element_names) # Remove element_names from any other controllers. set_names = set(element_names) for _controller, values in data.items(): if _controller != controller: final = set(values).difference_update(set_names) if final is None: final_list = None else: final_list = list(final) data[_controller] = final_list dump_data(data, filename) print(f"Added {num_added} names to {filename}")