Search in sources :

Example 1 with EventPublisher

use of org.openhab.core.events.EventPublisher in project openhab1-addons by openhab.

the class ExecuteCommandJob method execute.

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    String content = (String) context.getJobDetail().getJobDataMap().get(JOB_DATA_CONTENT_KEY);
    ItemRegistry registry = GCalActivator.itemRegistryTracker.getService();
    EventPublisher publisher = GCalActivator.eventPublisherTracker.getService();
    if (registry == null) {
        logger.warn("Sorry, no item registry service available!");
        return;
    }
    if (publisher == null) {
        logger.warn("Sorry, no event publisher service available!");
        return;
    }
    if (content.startsWith("[PresenceSimulation]")) {
        try {
            Item item = registry.getItem("PresenceSimulation");
            if (item.getState() != OnOffType.ON) {
                logger.debug("Presence Simulation job detected, but PresenceSimulation is not in ON state. Job is not executed");
                return;
            }
        } catch (ItemNotFoundException e) {
            logger.warn("Presence Simulation job detected, but PresenceSimulation item does not exists. Check configuration");
            return;
        }
    }
    if (StringUtils.isNotBlank(content)) {
        String[] commands = parseCommands(content);
        for (String command : commands) {
            String[] args = parseCommand(command);
            try {
                if (args[0].equals("send")) {
                    if (args.length > 2) {
                        Item item = registry.getItem(args[1]);
                        Command cmd = TypeParser.parseCommand(item.getAcceptedCommandTypes(), args[2]);
                        if (cmd != null) {
                            publisher.sendCommand(item.getName(), cmd);
                            logger.debug("Command {} has been sent", Arrays.asList(args));
                        } else {
                            logger.warn("Command '{}' is not valid. Command not sent.", Arrays.asList(args));
                        }
                    }
                } else if (args[0].equals("update")) {
                    if (args.length > 2) {
                        Item item = registry.getItem(args[1]);
                        State state = TypeParser.parseState(item.getAcceptedDataTypes(), args[2]);
                        publisher.postUpdate(item.getName(), state);
                        logger.debug("Published update {}", Arrays.asList(args));
                    } else {
                        logger.warn("Command '{}' is not valid. Update not sent.", Arrays.asList(args));
                    }
                } else {
                    logger.warn("Command {} not supported", args[0]);
                }
            } catch (ItemNotFoundException e) {
                logger.warn("Executing command failed. Item {} not found", args[1]);
            }
        }
    }
}
Also used : Item(org.openhab.core.items.Item) EventPublisher(org.openhab.core.events.EventPublisher) Command(org.openhab.core.types.Command) State(org.openhab.core.types.State) ItemRegistry(org.openhab.core.items.ItemRegistry) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 2 with EventPublisher

use of org.openhab.core.events.EventPublisher in project openhab1-addons by openhab.

the class EnoceanBinding method processEEPs.

private void processEEPs(EnoceanBindingProvider enoceanBindingProvider, String itemName) {
    EnoceanParameterAddress parameterAddress = enoceanBindingProvider.getParameterAddress(itemName);
    EEPId eep = enoceanBindingProvider.getEEP(itemName);
    esp3Host.addDeviceProfile(parameterAddress.getEnoceanDeviceId(), eep);
    Item item = enoceanBindingProvider.getItem(itemName);
    if (profiles.containsKey(parameterAddress.getAsString())) {
        Profile profile = profiles.get(parameterAddress.getAsString());
        profile.removeItem(item);
    }
    Class<Profile> customProfileClass = enoceanBindingProvider.getCustomProfile(itemName);
    if (customProfileClass != null) {
        Constructor<Profile> constructor;
        Profile profile;
        try {
            constructor = customProfileClass.getConstructor(Item.class, EventPublisher.class);
            profile = constructor.newInstance(item, eventPublisher);
            addProfile(item, parameterAddress, profile);
        } catch (Exception e) {
            logger.error("Could not create class for profile " + customProfileClass, e);
        }
    } else if (EEPId.EEP_F6_02_01.equals(eep) || EEPId.EEP_F6_10_00.equals(eep)) {
        if (item.getClass().equals(RollershutterItem.class)) {
            RollershutterProfile profile = new RollershutterProfile(item, eventPublisher);
            addProfile(item, parameterAddress, profile);
        }
        if (item.getClass().equals(DimmerItem.class)) {
            DimmerOnOffProfile profile = new DimmerOnOffProfile(item, eventPublisher);
            addProfile(item, parameterAddress, profile);
        }
        if (item.getClass().equals(SwitchItem.class) && parameterAddress.getParameterId() == null) {
            SwitchOnOffProfile profile = new SwitchOnOffProfile(item, eventPublisher);
            addProfile(item, parameterAddress, profile);
        }
        if (item.getClass().equals(StringItem.class) && EEPId.EEP_F6_10_00.equals(eep)) {
            WindowHandleProfile profile = new WindowHandleProfile(item, eventPublisher);
            addProfile(item, parameterAddress, profile);
        }
    }
}
Also used : EventPublisher(org.openhab.core.events.EventPublisher) EEPId(org.opencean.core.common.EEPId) RollershutterProfile(org.openhab.binding.enocean.internal.profiles.RollershutterProfile) SwitchOnOffProfile(org.openhab.binding.enocean.internal.profiles.SwitchOnOffProfile) Profile(org.openhab.binding.enocean.internal.profiles.Profile) SwitchOnOffProfile(org.openhab.binding.enocean.internal.profiles.SwitchOnOffProfile) StandardProfile(org.openhab.binding.enocean.internal.profiles.StandardProfile) DimmerOnOffProfile(org.openhab.binding.enocean.internal.profiles.DimmerOnOffProfile) WindowHandleProfile(org.openhab.binding.enocean.internal.profiles.WindowHandleProfile) RollershutterProfile(org.openhab.binding.enocean.internal.profiles.RollershutterProfile) ConfigurationException(org.osgi.service.cm.ConfigurationException) NoSuchPortException(gnu.io.NoSuchPortException) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) Item(org.openhab.core.items.Item) StringItem(org.openhab.core.library.items.StringItem) EnoceanParameterAddress(org.opencean.core.address.EnoceanParameterAddress) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DimmerItem(org.openhab.core.library.items.DimmerItem) DimmerOnOffProfile(org.openhab.binding.enocean.internal.profiles.DimmerOnOffProfile) WindowHandleProfile(org.openhab.binding.enocean.internal.profiles.WindowHandleProfile)

Aggregations

EventPublisher (org.openhab.core.events.EventPublisher)2 Item (org.openhab.core.items.Item)2 NoSuchPortException (gnu.io.NoSuchPortException)1 EnoceanParameterAddress (org.opencean.core.address.EnoceanParameterAddress)1 EEPId (org.opencean.core.common.EEPId)1 DimmerOnOffProfile (org.openhab.binding.enocean.internal.profiles.DimmerOnOffProfile)1 Profile (org.openhab.binding.enocean.internal.profiles.Profile)1 RollershutterProfile (org.openhab.binding.enocean.internal.profiles.RollershutterProfile)1 StandardProfile (org.openhab.binding.enocean.internal.profiles.StandardProfile)1 SwitchOnOffProfile (org.openhab.binding.enocean.internal.profiles.SwitchOnOffProfile)1 WindowHandleProfile (org.openhab.binding.enocean.internal.profiles.WindowHandleProfile)1 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)1 ItemRegistry (org.openhab.core.items.ItemRegistry)1 DimmerItem (org.openhab.core.library.items.DimmerItem)1 RollershutterItem (org.openhab.core.library.items.RollershutterItem)1 StringItem (org.openhab.core.library.items.StringItem)1 SwitchItem (org.openhab.core.library.items.SwitchItem)1 Command (org.openhab.core.types.Command)1 State (org.openhab.core.types.State)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1