use of org.openhab.io.neeo.internal.models.NeeoDevice in project openhab-addons by openhab.
the class OpenHabToDeviceConverter method convert.
/**
* Convert the {@link Thing} to a {@link NeeoDevice}
*
* @param thing the non-null thing
* @return a potentially null neeo device
*/
@Nullable
NeeoDevice convert(Thing thing) {
Objects.requireNonNull(thing, "thing cannot be null");
final List<NeeoDeviceChannel> channels = new ArrayList<>();
final ThingUID thingUID = thing.getUID();
final Set<String> existingLabels = new HashSet<>();
for (Channel channel : thing.getChannels()) {
final ChannelUID uid = channel.getUID();
if (channel.getKind() == ChannelKind.TRIGGER) {
channels.addAll(NeeoDeviceChannel.from(channel, NeeoCapabilityType.BUTTON, ItemSubType.NONE, existingLabels));
} else {
final ChannelType channelType = context.getChannelTypeRegistry().getChannelType(channel.getChannelTypeUID());
NeeoCapabilityType type = NeeoCapabilityType.EXCLUDE;
if (NeeoConstants.NEEOBINDING_BINDING_ID.equalsIgnoreCase(thingUID.getBindingId())) {
if (thingUID.getAsString().toLowerCase().startsWith(NeeoConstants.NEEOBINDING_DEVICE_ID.toLowerCase())) {
// all device channels are currently macros - so buttons are appropriate
type = NeeoCapabilityType.BUTTON;
} else {
type = NeeoCapabilityType.guessType(channelType);
}
} else if (exposeAll) {
type = NeeoCapabilityType.guessType(channelType);
}
final Set<Item> linkedItems = context.getItemChannelLinkRegistry().getLinkedItems(uid);
if (linkedItems != null) {
for (Item item : linkedItems) {
channels.addAll(NeeoDeviceChannel.from(item, channel, channelType, type, existingLabels));
}
}
}
}
if (channels.isEmpty()) {
logger.debug("No linked channels found for thing {} - ignoring", thing.getLabel());
return null;
}
if (NeeoConstants.NEEOBINDING_BINDING_ID.equalsIgnoreCase(thing.getUID().getBindingId())) {
final Map<String, String> properties = thing.getProperties();
/**
* The following properties have matches in org.openhab.binding.neeo.NeeoDeviceHandler.java
*/
String neeoType = properties.get("Type");
if (neeoType == null || neeoType.isEmpty()) {
neeoType = NeeoDeviceType.ACCESSOIRE.toString();
}
String manufacturer = properties.get("Manufacturer");
if (manufacturer == null || manufacturer.isEmpty()) {
manufacturer = "openHAB";
}
final Integer standbyDelay = parseInteger(properties.getOrDefault("Standby Command Delay", "0"));
final Integer switchDelay = parseInteger(properties.getOrDefault("Source Switch Delay", "0"));
final Integer shutDownDelay = parseInteger(properties.getOrDefault("Shutdown Delay", "0"));
final NeeoDeviceTiming timing = new NeeoDeviceTiming(standbyDelay, switchDelay, shutDownDelay);
final String dc = properties.get("Device Capabilities");
final String[] deviceCapabilities = dc == null || dc.isEmpty() ? new String[0] : StringUtils.split(dc, ',');
try {
return new NeeoDevice(new NeeoThingUID(thing.getUID()), 0, exposeNeeoBinding ? NeeoDeviceType.parse(neeoType) : NeeoDeviceType.EXCLUDE, manufacturer, thing.getLabel(), channels, timing, Arrays.asList(deviceCapabilities), null, null);
} catch (IllegalArgumentException e) {
logger.debug("NeeoDevice constructor threw an IAE - ignoring device: {} - {}", thing.getUID(), e.getMessage(), e);
return null;
}
} else {
try {
return new NeeoDevice(thing, channels, exposeAll ? NeeoUtil.guessType(thing) : NeeoDeviceType.EXCLUDE, null);
} catch (IllegalArgumentException e) {
logger.debug("NeeoDevice constructor threw an IAE - ignoring device: {} - {}", thing.getUID(), e.getMessage(), e);
return null;
}
}
}
use of org.openhab.io.neeo.internal.models.NeeoDevice in project openhab-addons by openhab.
the class TokenSearch method search.
/**
* Searches the registry for all {@link NeeoDevice} matching the query
*
* @param query the non-empty query
* @return a non-null result
*/
public Result search(String query) {
NeeoUtil.requireNotEmpty(query, "query cannot be empty");
final List<TokenScore<NeeoDevice>> results = new ArrayList<>();
final String[] needles = StringUtils.split(query, DELIMITER);
int maxScore = -1;
for (NeeoDevice device : context.getDefinitions().getExposed()) {
int score = search(device.getName(), needles);
score += search("openhab", needles);
// score += searchAlgorithm(thing.getLocation(), needles);
score += search(device.getUid().getBindingId(), needles);
final Thing thing = context.getThingRegistry().get(device.getUid().asThingUID());
if (thing != null) {
final String location = thing.getLocation();
if (location != null && !location.isEmpty()) {
score += search(location, needles);
}
final Map<@NonNull String, String> properties = thing.getProperties();
final String vendor = properties.get(Thing.PROPERTY_VENDOR);
if (vendor != null && !vendor.isEmpty()) {
score += search(vendor, needles);
}
final ThingType tt = context.getThingTypeRegistry().getThingType(thing.getThingTypeUID());
if (tt != null) {
score += search(tt.getLabel(), needles);
final BindingInfo bi = context.getBindingInfoRegistry().getBindingInfo(tt.getBindingId());
if (bi != null) {
score += search(bi.getName(), needles);
}
}
}
maxScore = Math.max(maxScore, score);
results.add(new TokenScore<>(score, device));
}
return new Result(applyThreshold(results, maxScore, threshold), maxScore);
}
use of org.openhab.io.neeo.internal.models.NeeoDevice in project openhab-addons by openhab.
the class NeeoDeviceDefinitions method getDevice.
/**
* Gets the {@link NeeoDevice} for the given {@link NeeoThingUID}. If no definition has been created yet, the
* definition
* will be created (but not saved) and returned. Note that any saved definition will be merged with the latest
* openHAB thing definition to pick up on any channel changes (added or removed)
*
* @param uid the non-null uid
* @return the neeo device or null if unknown (or a neeo uid)
*/
@Nullable
public NeeoDevice getDevice(NeeoThingUID uid) {
Objects.requireNonNull(uid, "uid cannot be null");
final NeeoDevice device = uidToDevice.get(uid);
if (device == null) {
final Thing thing = context.getThingRegistry().get(uid.asThingUID());
if (thing == null) {
logger.debug("Unknown thing uid {}", uid);
return null;
} else {
return converter.convert(thing);
}
} else {
return device;
}
}
use of org.openhab.io.neeo.internal.models.NeeoDevice in project openhab-addons by openhab.
the class NeeoDeviceDefinitions method save.
/**
* Saves the current definitions to the {@link #file}. Any {@link IOException} will be logged and ignored.
*/
public void save() {
logger.debug("Saving devices to {}", file.toPath());
try {
// ensure full path exists
file.getParentFile().mkdirs();
final List<NeeoDevice> devices = new ArrayList<>();
// filter for only things that are still valid
final ThingRegistry thingRegistry = context.getThingRegistry();
for (NeeoDevice device : uidToDevice.values()) {
if (NeeoConstants.NEEOIO_BINDING_ID.equalsIgnoreCase(device.getUid().getBindingId())) {
devices.add(device);
} else {
if (thingRegistry.get(device.getUid().asThingUID()) != null) {
devices.add(device);
}
}
}
final String json = gson.toJson(devices);
final byte[] contents = json.getBytes(StandardCharsets.UTF_8);
Files.write(file.toPath(), contents);
} catch (IOException e) {
logger.debug("IOException writing {}: {}", file.toPath(), e.getMessage(), e);
}
}
use of org.openhab.io.neeo.internal.models.NeeoDevice in project openhab-addons by openhab.
the class NeeoBrainSearchService method doAdapterDefinition.
/**
* Does a query for the NEEO device definition
*
* @param id the non-empty (last) search identifier
* @param resp the non-null response to write to
* @throws IOException Signals that an I/O exception has occurred.
*/
private void doAdapterDefinition(String id, HttpServletResponse resp) throws IOException {
NeeoThingUID thingUID;
try {
thingUID = new NeeoThingUID(id);
} catch (IllegalArgumentException e) {
logger.debug("Not a valid thingUID: {}", id);
NeeoUtil.write(resp, "{}");
return;
}
final NeeoDevice device = context.getDefinitions().getDevice(thingUID);
if (device == null) {
logger.debug("Called with index position {} but nothing was found", id);
NeeoUtil.write(resp, "{}");
} else {
final String jos = gson.toJson(device);
NeeoUtil.write(resp, jos);
logger.debug("Query '{}', response: {}", id, jos);
}
}
Aggregations