Search in sources :

Example 1 with NotImplementedException

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

the class ConfigChannel method setChannelDoc.

/**
 * Sets values for this ConfigChannel using its annotation
 *
 * This method is called by reflection from {@link InjectionUtils.getThingInstance}
 *
 * @param parent
 * @throws OpenemsException
 */
@Override
public void setChannelDoc(ChannelDoc channelDoc) throws OpenemsException {
    super.setChannelDoc(channelDoc);
    if (!this.isOptional.isPresent()) {
        this.isOptional = Optional.of(channelDoc.isOptional());
    }
    if (!this.defaultValue.isPresent() && !channelDoc.getDefaultValue().isEmpty()) {
        JsonElement jValue = null;
        try {
            jValue = (new JsonParser()).parse(channelDoc.getDefaultValue());
            @SuppressWarnings("unchecked") T value = (T) JsonUtils.getAsType(type().get(), jValue);
            this.defaultValue(value);
        } catch (NotImplementedException | JsonSyntaxException e) {
            throw new OpenemsException("Unable to set defaultValue [" + jValue + "] " + e.getMessage());
        }
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) NotImplementedException(io.openems.common.exceptions.NotImplementedException) OpenemsException(io.openems.common.exceptions.OpenemsException) JsonParser(com.google.gson.JsonParser)

Example 2 with NotImplementedException

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

the class ConfigUtils method getAsJsonElement.

/**
 * Converts an object to a JsonElement
 *
 * @param value
 * @return
 * @throws NotImplementedException
 */
public static JsonElement getAsJsonElement(Object value, ConfigFormat format, Role role) throws NotImplementedException {
    // null
    if (value == null) {
        return null;
    }
    // optional
    if (value instanceof Optional<?>) {
        if (!((Optional<?>) value).isPresent()) {
            return null;
        } else {
            value = ((Optional<?>) value).get();
        }
    }
    try {
        /*
			 * test for simple types
			 */
        return JsonUtils.getAsJsonElement(value);
    } catch (NotImplementedException e) {
        ;
    }
    if (value instanceof Thing) {
        /*
			 * type Thing
			 */
        Thing thing = (Thing) value;
        JsonObject j = new JsonObject();
        if (format == ConfigFormat.OPENEMS_UI || !thing.id().startsWith("_")) {
            // ignore generated id names starting with "_"
            j.addProperty("id", thing.id());
            j.addProperty("alias", thing.getAlias());
        }
        // for file-format class is not needed for DeviceNatures
        j.addProperty("class", thing.getClass().getCanonicalName());
        ThingRepository thingRepository = ThingRepository.getInstance();
        for (ConfigChannel<?> channel : thingRepository.getConfigChannels(thing)) {
            if (channel.isReadAllowed(role)) {
                // check read permissions
                JsonElement jChannel = null;
                jChannel = ConfigUtils.getAsJsonElement(channel, format, role);
                if (jChannel != null) {
                    j.add(channel.id(), jChannel);
                }
            }
        }
        // for Bridge: add 'devices' array of thingIds
        if (value instanceof Bridge) {
            Bridge bridge = (Bridge) value;
            JsonArray jDevices = new JsonArray();
            for (Device device : bridge.getDevices()) {
                jDevices.add(device.id());
            }
            j.add("devices", jDevices);
        }
        return j;
    } else if (value instanceof ConfigChannel<?>) {
        /*
			 * type ConfigChannel
			 */
        ConfigChannel<?> channel = (ConfigChannel<?>) value;
        if (!channel.valueOptional().isPresent()) {
            // no value set
            return null;
        } else if (format == ConfigFormat.FILE && channel.getDefaultValue().equals(channel.valueOptional())) {
            // default value not changed
            return null;
        } else {
            // recursive call
            return ConfigUtils.getAsJsonElement(channel.valueOptional().get(), format, role);
        }
    } else if (value instanceof ThingMap) {
        /*
			 * ThingMap (we need only id)
			 */
        return new JsonPrimitive(((ThingMap) value).id());
    } else if (value instanceof List<?>) {
        /*
			 * List
			 */
        JsonArray jArray = new JsonArray();
        for (Object v : (List<?>) value) {
            jArray.add(ConfigUtils.getAsJsonElement(v, format, role));
        }
        return jArray;
    } else if (value instanceof Set<?>) {
        /*
			 * Set
			 */
        JsonArray jArray = new JsonArray();
        for (Object v : (Set<?>) value) {
            jArray.add(ConfigUtils.getAsJsonElement(v, format, role));
        }
        return jArray;
    }
    throw new NotImplementedException(// 
    "Converter for [" + value + "]" + " of type [" + value.getClass().getSimpleName() + // 
    "]" + " to JSON is not implemented.");
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Optional(java.util.Optional) JsonPrimitive(com.google.gson.JsonPrimitive) Device(io.openems.api.device.Device) ConfigChannel(io.openems.api.channel.ConfigChannel) NotImplementedException(io.openems.common.exceptions.NotImplementedException) JsonObject(com.google.gson.JsonObject) ThingRepository(io.openems.core.ThingRepository) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) ThingMap(io.openems.api.controller.ThingMap) Thing(io.openems.api.thing.Thing) Bridge(io.openems.api.bridge.Bridge)

Example 3 with NotImplementedException

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

the class MyRegister method toBytes.

@Override
public byte[] toBytes() {
    Channel channel;
    try {
        channel = parent.getChannel();
    } catch (OpenemsException e) {
        log.warn(e.getMessage());
        return VALUE_ON_ERROR;
    }
    Optional<?> valueOpt = Databus.getInstance().getValue(channel);
    if (valueOpt.isPresent()) {
        // we got a value
        Object object = valueOpt.get();
        try {
            byte[] b = BitUtils.toBytes(object);
            return new byte[] { b[this.registerNo * 2], b[this.registerNo * 2 + 1] };
        } catch (NotImplementedException e) {
            // unable to convert value to byte
            try {
                throw new OpenemsException("Unable to convert Channel [" + this.parent.getChannelAddress() + "] value [" + object + "] to byte format.");
            } catch (OpenemsException e2) {
                log.warn(e2.getMessage());
            }
        }
    } else {
        // we got no value
        try {
            throw new OpenemsException("Value for Channel [" + this.parent.getChannelAddress() + "] is not available.");
        } catch (OpenemsException e) {
            log.warn(e.getMessage());
        }
    }
    return VALUE_ON_ERROR;
}
Also used : Channel(io.openems.api.channel.Channel) NotImplementedException(io.openems.common.exceptions.NotImplementedException) OpenemsException(io.openems.common.exceptions.OpenemsException)

Example 4 with NotImplementedException

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

the class ChannelRestlet method setValue.

/**
 * handle HTTP POST request
 *
 * @param thingId
 * @param channelId
 * @param jHttpPost
 */
private void setValue(Channel channel, JsonObject jHttpPost) {
    // check for writable channel
    if (!(channel instanceof WriteChannel<?>)) {
        throw new ResourceException(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
    }
    // parse value
    JsonElement jValue;
    if (jHttpPost.has("value")) {
        jValue = jHttpPost.get("value");
    } else {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Value is missing");
    }
    // set channel value
    if (channel instanceof ConfigChannel<?>) {
        // is a ConfigChannel
        ConfigChannel<?> configChannel = (ConfigChannel<?>) channel;
        try {
            configChannel.updateValue(jValue, true);
            log.info("Updated Channel [" + channel.address() + "] to value [" + jValue.toString() + "].");
        } catch (NotImplementedException e) {
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Conversion not implemented");
        }
    } else if (channel instanceof WriteChannel<?>) {
        /*
			 * WriteChannel
			 */
        WriteChannel<?> writeChannel = (WriteChannel<?>) channel;
        WriteObject writeObject = new WriteJsonObject(jValue).onFirstSuccess(() -> {
            Notification.EDGE_CHANNEL_UPDATE_SUCCESS.writeToLog(log, "set " + channel.address() + " => " + jValue);
        }).onFirstError((e) -> {
            Notification.EDGE_CHANNEL_UPDATE_FAILED.writeToLog(log, "set " + channel.address() + " => " + jValue);
        }).onTimeout(() -> {
            Notification.EDGE_CHANNEL_UPDATE_TIMEOUT.writeToLog(log, "set " + channel.address() + " => " + jValue);
        });
        this.apiWorker.addValue(writeChannel, writeObject);
    }
}
Also used : JsonElement(com.google.gson.JsonElement) ConfigChannel(io.openems.api.channel.ConfigChannel) WriteChannel(io.openems.api.channel.WriteChannel) NotImplementedException(io.openems.common.exceptions.NotImplementedException) ResourceException(org.restlet.resource.ResourceException) WriteJsonObject(io.openems.core.utilities.api.WriteJsonObject) WriteObject(io.openems.core.utilities.api.WriteObject)

Example 5 with NotImplementedException

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

the class ConfigUtils method getConfigObject.

/**
 * Receives a matching value for the ConfigChannel from a JsonElement
 *
 * @param channel
 * @param j
 * @return
 * @throws OpenemsException
 */
public static Object getConfigObject(ConfigChannel<?> channel, JsonElement j, Object... args) throws OpenemsException {
    Optional<Class<?>> typeOptional = channel.type();
    if (!typeOptional.isPresent()) {
        String clazz = channel.parent() != null ? " in implementation [" + channel.parent().getClass() + "]" : "";
        throw new ReflectionException("Type is null for channel [" + channel.address() + "]" + clazz);
    }
    Class<?> type = typeOptional.get();
    /*
		 * test for simple types
		 */
    try {
        return JsonUtils.getAsType(type, j);
    } catch (NotImplementedException e1) {
        ;
    }
    if (Thing.class.isAssignableFrom(type)) {
        /*
			 * Asking for a Thing
			 */
        @SuppressWarnings("unchecked") Class<Thing> thingType = (Class<Thing>) type;
        return getThingFromConfig(thingType, j, args);
    } else if (ThingMap.class.isAssignableFrom(type)) {
        /*
			 * Asking for a ThingMap
			 */
        return InjectionUtils.getThingMapsFromConfig(channel, j);
    } else if (Inet4Address.class.isAssignableFrom(type)) {
        /*
			 * Asking for an IPv4
			 */
        try {
            return Inet4Address.getByName(j.getAsString());
        } catch (UnknownHostException e) {
            throw new ReflectionException("Unable to convert [" + j + "] to IPv4 address");
        }
    } else if (Long[].class.isAssignableFrom(type)) {
        /*
			 * Asking for an Array of Long
			 */
        return getLongArrayFromConfig(channel, j);
    }
    throw new ReflectionException("Unable to match config [" + j + "] to class type [" + type + "]");
}
Also used : ReflectionException(io.openems.api.exception.ReflectionException) UnknownHostException(java.net.UnknownHostException) NotImplementedException(io.openems.common.exceptions.NotImplementedException) ThingMap(io.openems.api.controller.ThingMap) Thing(io.openems.api.thing.Thing)

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