Search in sources :

Example 6 with NotImplementedException

use of io.openems.common.exceptions.NotImplementedException in project openems by OpenEMS.

the class ConfigUtils method writeConfigToFile.

protected static synchronized void writeConfigToFile(Path path, TreeMap<String, Config> configs) throws IOException {
    // create JsonObject
    JsonObject j = new JsonObject();
    for (Entry<String, Config> entry : configs.entrySet()) {
        // ignore configs that should not be stored
        if (entry.getValue().isDoNotStore()) {
            continue;
        }
        JsonObject jSub = new JsonObject();
        // sort map by key to be able to write the json sorted
        TreeMap<String, Object> sortedSub = new TreeMap<>();
        for (Entry<String, Object> subEntry : entry.getValue().entrySet()) {
            sortedSub.put(subEntry.getKey(), subEntry.getValue());
        }
        for (Entry<String, Object> subEntry : sortedSub.entrySet()) {
            if (subEntry.getKey().equals("service.pid")) {
                // ignore. It's already the key of the JsonObject
                continue;
            }
            try {
                jSub.add(subEntry.getKey(), JsonUtils.getAsJsonElement(subEntry.getValue()));
            } catch (NotImplementedException e) {
                Log.warn("Unable to store [" + entry.getKey() + "/" + subEntry.getKey() + "] value [" + subEntry.getValue() + "] in config: " + e.getMessage());
            }
        }
        j.add(entry.getKey(), jSub);
    }
    // write to file
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String config = gson.toJson(j);
    Files.write(path, config.getBytes(DEFAULT_CHARSET));
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) NotImplementedException(io.openems.common.exceptions.NotImplementedException) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) TreeMap(java.util.TreeMap)

Aggregations

NotImplementedException (io.openems.common.exceptions.NotImplementedException)6 JsonElement (com.google.gson.JsonElement)3 JsonObject (com.google.gson.JsonObject)2 ConfigChannel (io.openems.api.channel.ConfigChannel)2 ThingMap (io.openems.api.controller.ThingMap)2 Thing (io.openems.api.thing.Thing)2 OpenemsException (io.openems.common.exceptions.OpenemsException)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonArray (com.google.gson.JsonArray)1 JsonParser (com.google.gson.JsonParser)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 Bridge (io.openems.api.bridge.Bridge)1 Channel (io.openems.api.channel.Channel)1 WriteChannel (io.openems.api.channel.WriteChannel)1 Device (io.openems.api.device.Device)1 ReflectionException (io.openems.api.exception.ReflectionException)1 ThingRepository (io.openems.core.ThingRepository)1 WriteJsonObject (io.openems.core.utilities.api.WriteJsonObject)1