Search in sources :

Example 1 with WriteJsonObject

use of io.openems.core.utilities.api.WriteJsonObject in project openems by OpenEMS.

the class EdgeWebsocketHandler method config.

/**
 * Handle "config" messages
 *
 * @param jConfig
 * @return
 */
private synchronized void config(Role role, JsonObject jMessageId, Optional<ApiWorker> apiWorkerOpt, JsonObject jConfig) {
    Optional<String> modeOpt = JsonUtils.getAsOptionalString(jConfig, "mode");
    switch(modeOpt.orElse("")) {
        case "query":
            /*
			 * Query current config
			 */
            try {
                String language = JsonUtils.getAsString(jConfig, "language");
                JsonObject jReplyConfig = Config.getInstance().getJson(ConfigFormat.OPENEMS_UI, role, language);
                WebSocketUtils.send(this.websocket, DefaultMessages.configQueryReply(jMessageId, jReplyConfig));
                return;
            } catch (OpenemsException e) {
                WebSocketUtils.sendNotificationOrLogError(this.websocket, jMessageId, LogBehaviour.WRITE_TO_LOG, Notification.UNABLE_TO_READ_CURRENT_CONFIG, e.getMessage());
            }
        case "update":
            /*
			 * Update thing/channel config
			 */
            Optional<String> thingIdOpt = JsonUtils.getAsOptionalString(jConfig, "thing");
            Optional<String> channelIdOpt = JsonUtils.getAsOptionalString(jConfig, "channel");
            try {
                String thingId = thingIdOpt.get();
                String channelId = channelIdOpt.get();
                JsonElement jValue = JsonUtils.getSubElement(jConfig, "value");
                Optional<Channel> channelOpt = ThingRepository.getInstance().getChannel(thingId, channelId);
                if (channelOpt.isPresent()) {
                    Channel channel = channelOpt.get();
                    // check write permissions
                    channel.assertWriteAllowed(role);
                    if (channel instanceof ConfigChannel<?>) {
                        /*
						 * ConfigChannel
						 */
                        ConfigChannel<?> configChannel = (ConfigChannel<?>) channel;
                        Object value = ConfigUtils.getConfigObject(configChannel, jValue);
                        configChannel.updateValue(value, true);
                        WebSocketUtils.sendNotificationOrLogError(this.websocket, jMessageId, LogBehaviour.WRITE_TO_LOG, Notification.EDGE_CHANNEL_UPDATE_SUCCESS, channel.address() + " => " + jValue);
                    } else if (channel instanceof WriteChannel<?>) {
                        /*
						 * WriteChannel
						 */
                        WriteChannel<?> writeChannel = (WriteChannel<?>) channel;
                        if (!apiWorkerOpt.isPresent()) {
                            WebSocketUtils.sendNotificationOrLogError(this.websocket, jMessageId, LogBehaviour.WRITE_TO_LOG, Notification.BACKEND_NOT_ALLOWED, "set " + channel.address() + " => " + jValue);
                        } else {
                            ApiWorker apiWorker = apiWorkerOpt.get();
                            WriteObject writeObject = new WriteJsonObject(jValue).onFirstSuccess(() -> {
                                WebSocketUtils.sendNotificationOrLogError(this.websocket, jMessageId, LogBehaviour.WRITE_TO_LOG, Notification.EDGE_CHANNEL_UPDATE_SUCCESS, "set " + channel.address() + " => " + jValue);
                            }).onFirstError((e) -> {
                                WebSocketUtils.sendNotificationOrLogError(this.websocket, jMessageId, LogBehaviour.WRITE_TO_LOG, Notification.EDGE_CHANNEL_UPDATE_FAILED, "set " + channel.address() + " => " + jValue, e.getMessage());
                            }).onTimeout(() -> {
                                WebSocketUtils.sendNotificationOrLogError(this.websocket, jMessageId, LogBehaviour.WRITE_TO_LOG, Notification.EDGE_CHANNEL_UPDATE_TIMEOUT, "set " + channel.address() + " => " + jValue);
                            });
                            apiWorker.addValue(writeChannel, writeObject);
                        }
                    }
                } else {
                    WebSocketUtils.sendNotificationOrLogError(this.websocket, jMessageId, LogBehaviour.WRITE_TO_LOG, Notification.CHANNEL_NOT_FOUND, thingId + "/" + channelId);
                }
            } catch (NoSuchElementException | OpenemsException e) {
                WebSocketUtils.sendNotificationOrLogError(this.websocket, jMessageId, LogBehaviour.WRITE_TO_LOG, Notification.EDGE_CHANNEL_UPDATE_FAILED, thingIdOpt.orElse("UNDEFINED") + "/" + channelIdOpt.orElse("UNDEFINED"), e.getMessage());
            }
    }
}
Also used : ApiWorker(io.openems.core.utilities.api.ApiWorker) ConfigChannel(io.openems.api.channel.ConfigChannel) WriteChannel(io.openems.api.channel.WriteChannel) ConfigChannel(io.openems.api.channel.ConfigChannel) Channel(io.openems.api.channel.Channel) JsonObject(com.google.gson.JsonObject) WriteJsonObject(io.openems.core.utilities.api.WriteJsonObject) OpenemsException(io.openems.common.exceptions.OpenemsException) JsonElement(com.google.gson.JsonElement) WriteChannel(io.openems.api.channel.WriteChannel) JsonObject(com.google.gson.JsonObject) WriteObject(io.openems.core.utilities.api.WriteObject) WriteJsonObject(io.openems.core.utilities.api.WriteJsonObject) WriteJsonObject(io.openems.core.utilities.api.WriteJsonObject) NoSuchElementException(java.util.NoSuchElementException) WriteObject(io.openems.core.utilities.api.WriteObject)

Example 2 with WriteJsonObject

use of io.openems.core.utilities.api.WriteJsonObject 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

JsonElement (com.google.gson.JsonElement)2 ConfigChannel (io.openems.api.channel.ConfigChannel)2 WriteChannel (io.openems.api.channel.WriteChannel)2 WriteJsonObject (io.openems.core.utilities.api.WriteJsonObject)2 WriteObject (io.openems.core.utilities.api.WriteObject)2 JsonObject (com.google.gson.JsonObject)1 Channel (io.openems.api.channel.Channel)1 NotImplementedException (io.openems.common.exceptions.NotImplementedException)1 OpenemsException (io.openems.common.exceptions.OpenemsException)1 ApiWorker (io.openems.core.utilities.api.ApiWorker)1 NoSuchElementException (java.util.NoSuchElementException)1 ResourceException (org.restlet.resource.ResourceException)1