Search in sources :

Example 1 with NeeoDevice

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;
        }
    }
}
Also used : NeeoDeviceChannel(org.openhab.io.neeo.internal.models.NeeoDeviceChannel) NeeoCapabilityType(org.openhab.io.neeo.internal.models.NeeoCapabilityType) NeeoDeviceTiming(org.openhab.io.neeo.internal.models.NeeoDeviceTiming) Channel(org.openhab.core.thing.Channel) NeeoDeviceChannel(org.openhab.io.neeo.internal.models.NeeoDeviceChannel) ArrayList(java.util.ArrayList) NeeoDevice(org.openhab.io.neeo.internal.models.NeeoDevice) Item(org.openhab.core.items.Item) NeeoThingUID(org.openhab.io.neeo.internal.models.NeeoThingUID) ChannelUID(org.openhab.core.thing.ChannelUID) ThingUID(org.openhab.core.thing.ThingUID) NeeoThingUID(org.openhab.io.neeo.internal.models.NeeoThingUID) ChannelType(org.openhab.core.thing.type.ChannelType) HashSet(java.util.HashSet) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 2 with NeeoDevice

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);
}
Also used : ArrayList(java.util.ArrayList) NeeoDevice(org.openhab.io.neeo.internal.models.NeeoDevice) ThingType(org.openhab.core.thing.type.ThingType) TokenScore(org.openhab.io.neeo.internal.models.TokenScore) BindingInfo(org.openhab.core.binding.BindingInfo) Thing(org.openhab.core.thing.Thing)

Example 3 with NeeoDevice

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;
    }
}
Also used : NeeoDevice(org.openhab.io.neeo.internal.models.NeeoDevice) Thing(org.openhab.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 4 with NeeoDevice

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);
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) NeeoDevice(org.openhab.io.neeo.internal.models.NeeoDevice) ThingRegistry(org.openhab.core.thing.ThingRegistry)

Example 5 with NeeoDevice

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);
    }
}
Also used : NeeoThingUID(org.openhab.io.neeo.internal.models.NeeoThingUID) NeeoDevice(org.openhab.io.neeo.internal.models.NeeoDevice)

Aggregations

NeeoDevice (org.openhab.io.neeo.internal.models.NeeoDevice)19 NeeoThingUID (org.openhab.io.neeo.internal.models.NeeoThingUID)10 NeeoDeviceChannel (org.openhab.io.neeo.internal.models.NeeoDeviceChannel)9 ArrayList (java.util.ArrayList)8 JsonParseException (com.google.gson.JsonParseException)4 Item (org.openhab.core.items.Item)4 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)4 JsonObject (com.google.gson.JsonObject)3 IOException (java.io.IOException)3 Nullable (org.eclipse.jdt.annotation.Nullable)3 ChannelUID (org.openhab.core.thing.ChannelUID)3 ReturnStatus (org.openhab.io.neeo.internal.servletservices.models.ReturnStatus)3 List (java.util.List)2 ItemCommandEvent (org.openhab.core.items.events.ItemCommandEvent)2 Thing (org.openhab.core.thing.Thing)2 Command (org.openhab.core.types.Command)2 NeeoBrainServlet (org.openhab.io.neeo.internal.NeeoBrainServlet)2 NeeoDeviceTiming (org.openhab.io.neeo.internal.models.NeeoDeviceTiming)2 NeeoDeviceType (org.openhab.io.neeo.internal.models.NeeoDeviceType)2 Gson (com.google.gson.Gson)1