use of io.openems.core.utilities.api.WriteObject 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());
}
}
}
use of io.openems.core.utilities.api.WriteObject 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);
}
}
Aggregations