use of io.openems.api.controller.IsThingMap in project openems by OpenEMS.
the class ChannelDoc method getAsJsonObject.
public JsonObject getAsJsonObject() {
JsonObject j = new JsonObject();
j.addProperty("name", this.name);
j.addProperty("title", this.title);
j.addProperty("description", this.description);
if (typeOpt.isPresent()) {
Class<?> type = this.typeOpt.get();
if (ThingMap.class.isAssignableFrom(type)) {
// for ThingMap type: get the types from annotation and return JsonArray
IsThingMap isThingMapAnnotation = type.getAnnotation(IsThingMap.class);
j.add("type", InjectionUtils.getImplementsAsJson(isThingMapAnnotation.type()));
} else if (DeviceNature.class.isAssignableFrom(type)) {
// for DeviceNatures add complete class name
j.addProperty("type", type.getCanonicalName());
} else {
// for simple types, use only simple name (e.g. 'Long', 'Integer',...)
j.addProperty("type", type.getSimpleName());
}
}
j.addProperty("optional", this.optional);
j.addProperty("array", this.array);
JsonArray jReadRoles = new JsonArray();
for (Role role : this.readRoles) {
jReadRoles.add(role.name().toLowerCase());
}
j.add("readRoles", jReadRoles);
JsonArray jWriteRoles = new JsonArray();
for (Role role : this.writeRoles) {
jWriteRoles.add(role.name().toLowerCase());
}
j.add("writeRoles", jWriteRoles);
j.addProperty("defaultValue", this.defaultValue);
j.addProperty("jsonSchema", this.jsonSchema);
return j;
}
use of io.openems.api.controller.IsThingMap in project openems by OpenEMS.
the class InjectionUtils method getThingMapsFromConfig.
public static Object getThingMapsFromConfig(ConfigChannel<?> channel, JsonElement j) throws ReflectionException {
/*
* Get "Field" in Channels parent class
*/
Field field;
try {
field = channel.parent().getClass().getField(channel.id());
} catch (NoSuchFieldException | SecurityException e) {
throw new ReflectionException("Field for ConfigChannel [" + channel.address() + "] is not named [" + channel.id() + "] in [" + channel.getClass().getSimpleName() + "]");
}
/*
* Get expected Object Type (List, Set, simple Object)
*/
Type expectedObjectType = ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
if (expectedObjectType instanceof ParameterizedType) {
expectedObjectType = ((ParameterizedType) expectedObjectType).getRawType();
}
Class<?> expectedObjectClass = (Class<?>) expectedObjectType;
/*
* Get the ThingMap class
*/
Class<?> thingMapClass = channel.type().get();
/*
* Get the referenced Thing class
*/
IsThingMap isThingMapAnnotation = thingMapClass.getAnnotation(IsThingMap.class);
Class<? extends Thing> thingClass = isThingMapAnnotation.type();
/*
* Prepare filter for matching Things
* - Empty filter: accept nothing
* - Asterisk: accept everything
* - Otherwise: accept only exact string matches on the thing id
*/
Set<String> filter = new HashSet<>();
if (j.isJsonPrimitive()) {
String id = j.getAsJsonPrimitive().getAsString();
filter.add(id);
} else if (j.isJsonArray()) {
j.getAsJsonArray().forEach(id -> filter.add(id.getAsString()));
}
/*
* Create ThingMap instance(s) for each matching Thing
*/
ThingRepository thingRepository = ThingRepository.getInstance();
Set<Thing> matchingThings = thingRepository.getThingsAssignableByClass(thingClass);
Set<ThingMap> thingMaps = new HashSet<>();
for (Thing thing : matchingThings) {
if (filter.contains(thing.id()) || filter.contains("*")) {
ThingMap thingMap = (ThingMap) InjectionUtils.getInstance(thingMapClass, thing);
thingMaps.add(thingMap);
}
}
/*
* Prepare return
*/
if (thingMaps.isEmpty() && !filter.isEmpty()) {
throw new ReflectionException("No matching ThingMap found for ConfigChannel [" + channel.address() + "]");
}
if (Collection.class.isAssignableFrom(expectedObjectClass)) {
if (Set.class.isAssignableFrom(expectedObjectClass)) {
return thingMaps;
} else if (List.class.isAssignableFrom(expectedObjectClass)) {
return new ArrayList<>(thingMaps);
} else {
throw new ReflectionException("Only List and Set ConfigChannels are currently implemented, not [" + expectedObjectClass + "]. ConfigChannel [" + channel.address() + "]");
}
} else {
// No collection
if (thingMaps.size() > 1) {
throw new ReflectionException("Field for ConfigChannel [" + channel.address() + "] is no collection, but more than one ThingMaps [" + thingMaps + "] is fitting for [" + channel.id() + "] in [" + channel.getClass().getSimpleName() + "]");
} else {
return thingMaps.iterator().next();
}
}
}
Aggregations