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());
}
}
}
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.");
}
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;
}
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);
}
}
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 + "]");
}
Aggregations