Search in sources :

Example 6 with WriteChannel

use of io.openems.api.channel.WriteChannel 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)

Aggregations

WriteChannel (io.openems.api.channel.WriteChannel)6 ConfigChannel (io.openems.api.channel.ConfigChannel)5 Channel (io.openems.api.channel.Channel)4 JsonElement (com.google.gson.JsonElement)3 ArrayList (java.util.ArrayList)3 OpenemsException (io.openems.common.exceptions.OpenemsException)2 WriteJsonObject (io.openems.core.utilities.api.WriteJsonObject)2 WriteObject (io.openems.core.utilities.api.WriteObject)2 HashMap (java.util.HashMap)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 ReadChannel (io.openems.api.channel.ReadChannel)1 ThingStateChannel (io.openems.api.channel.ThingStateChannel)1 ChannelDoc (io.openems.api.doc.ChannelDoc)1 ThingDoc (io.openems.api.doc.ThingDoc)1 InvalidValueException (io.openems.api.exception.InvalidValueException)1 ReflectionException (io.openems.api.exception.ReflectionException)1 NotImplementedException (io.openems.common.exceptions.NotImplementedException)1 ApiWorker (io.openems.core.utilities.api.ApiWorker)1 WritePOJO (io.openems.core.utilities.api.WritePOJO)1