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);
}
}
Aggregations