use of io.openems.api.thing.Thing in project openems by OpenEMS.
the class ThingRepository method removeThing.
/**
* Remove a Thing from the Repository.
*
* @param thing
*/
public synchronized void removeThing(String thingId) {
Thing thing = thingIds.get(thingId);
removeThing(thing);
}
use of io.openems.api.thing.Thing in project openems by OpenEMS.
the class ThingRepository method getChannel.
public Optional<Channel> getChannel(String thingId, String channelId) {
Thing thing = thingIds.get(thingId);
if (thing == null) {
return Optional.empty();
}
Map<String, Channel> channels = thingChannels.row(thing);
Channel channel = channels.get(channelId);
return Optional.ofNullable(channel);
}
use of io.openems.api.thing.Thing 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.api.thing.Thing in project openems by OpenEMS.
the class ConfigUtils method getAvailableClasses.
public static Set<Class<? extends Thing>> getAvailableClasses(String topLevelPackage, Class<? extends Thing> clazz, String suffix) throws ReflectionException {
Set<Class<? extends Thing>> clazzes = new HashSet<>();
try {
ClassPath classpath = ClassPath.from(ClassLoader.getSystemClassLoader());
for (ClassPath.ClassInfo classInfo : classpath.getTopLevelClassesRecursive(topLevelPackage)) {
if (classInfo.getName().endsWith(suffix)) {
Class<?> thisClazz = classInfo.load();
if (clazz.isAssignableFrom(thisClazz)) {
@SuppressWarnings("unchecked") Class<? extends Thing> thisThingClazz = (Class<? extends Thing>) thisClazz;
clazzes.add(thisThingClazz);
}
}
}
} catch (IllegalArgumentException | IOException e) {
throw new ReflectionException(e.getMessage());
}
return clazzes;
}
use of io.openems.api.thing.Thing in project openems by OpenEMS.
the class ConfigUtils method getThingFromConfig.
private static Thing getThingFromConfig(Class<? extends Thing> type, JsonElement j, Object... objects) throws OpenemsException {
String thingId = JsonUtils.getAsString(j, "id");
ThingRepository thingRepository = ThingRepository.getInstance();
Optional<Thing> existingThing = thingRepository.getThingById(thingId);
Thing thing;
if (existingThing.isPresent()) {
// reuse existing Thing
thing = existingThing.get();
} else {
// Thing is not existing. Create a new instance
Object[] args = new Object[objects.length + 1];
args[0] = thingId;
for (int i = 1; i < objects.length + 1; i++) {
args[i] = objects[i - 1];
}
thing = InjectionUtils.getThingInstance(type, args);
log.debug("Add Thing[" + thing.id() + "], Implementation[" + thing.getClass().getSimpleName() + "]");
thingRepository.addThing(thing);
}
// Recursive call to inject config parameters for the newly created Thing
injectConfigChannels(thingRepository.getConfigChannels(thing), j.getAsJsonObject());
// thing.init();
return thing;
}
Aggregations