Search in sources :

Example 1 with PlugwiseCommandType

use of org.openhab.binding.plugwise.PlugwiseCommandType in project openhab1-addons by openhab.

the class PlugwiseBinding method scheduleJobs.

private void scheduleJobs(Scheduler scheduler) {
    for (PlugwiseBindingProvider provider : providers) {
        for (PlugwiseBindingConfigElement element : provider.getIntervalList()) {
            PlugwiseCommandType type = element.getCommandType();
            if (type.getJobClass() == null) {
                continue;
            }
            if (stick.getDevice(element.getId()) == null) {
                logger.debug("The Plugwise device with id {} is not yet defined", element.getId());
                // check if the config string really contains a MAC address
                Pattern MAC_PATTERN = Pattern.compile("(\\w{16})");
                Matcher matcher = MAC_PATTERN.matcher(element.getId());
                if (matcher.matches()) {
                    List<CirclePlus> cps = stick.getDevicesByClass(CirclePlus.class);
                    if (!cps.isEmpty()) {
                        CirclePlus cp = cps.get(0);
                        if (!cp.getMAC().equals(element.getId())) {
                            // a circleplus has been added/detected and it is not what is in the binding config
                            PlugwiseDevice device = new Circle(element.getId(), stick, element.getId());
                            stick.addDevice(device);
                            logger.debug("Plugwise added Circle with MAC address: {}", element.getId());
                        }
                    } else {
                        logger.warn("Plugwise can not guess the device that should be added. Consider defining it in the openHAB configuration file");
                    }
                } else {
                    logger.warn("Plugwise can not add a valid device without a proper MAC address. {} can not be used", element.getId());
                }
            }
            if (stick.getDevice(element.getId()) != null) {
                String jobName = element.getId() + "-" + type.getJobClass().toString();
                if (!isExistingJob(scheduler, jobName)) {
                    // set up the Quartz jobs
                    JobDataMap map = new JobDataMap();
                    map.put(STICK_JOB_DATA_KEY, stick);
                    map.put(MAC_JOB_DATA_KEY, stick.getDevice(element.getId()).MAC);
                    JobDetail job = newJob(type.getJobClass()).withIdentity(jobName, "Plugwise-" + provider.toString()).usingJobData(map).build();
                    Trigger trigger = newTrigger().withIdentity(element.getId() + "-" + type.getJobClass().toString(), "Plugwise-" + provider.toString()).startNow().withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(element.getInterval())).build();
                    try {
                        scheduler.scheduleJob(job, trigger);
                    } catch (SchedulerException e) {
                        logger.error("An exception occurred while scheduling a Plugwise Quartz Job", e);
                    }
                }
            } else {
                logger.error("Error scheduling a Quartz Job for a non-defined Plugwise device (" + element.getId() + ")");
            }
        }
    }
    List<CirclePlus> cps = stick.getDevicesByClass(CirclePlus.class);
    if (!cps.isEmpty()) {
        CirclePlus cp = cps.get(0);
        String jobName = cp.MAC + "-SetCirclePlusClock";
        if (!isExistingJob(scheduler, jobName)) {
            JobDataMap map = new JobDataMap();
            map.put(CirclePlus.CIRCLE_PLUS_JOB_DATA_KEY, cp);
            JobDetail job = newJob(SetClockJob.class).withIdentity(jobName, "Plugwise").usingJobData(map).build();
            CronTrigger trigger = newTrigger().withIdentity(jobName, "Plugwise").startNow().withSchedule(CronScheduleBuilder.cronSchedule("0 0 0 * * ?")).build();
            try {
                Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
                sched.scheduleJob(job, trigger);
            } catch (SchedulerException e) {
                logger.error("Error scheduling Circle+ setClock Quartz Job", e);
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) JobDataMap(org.quartz.JobDataMap) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException) Matcher(java.util.regex.Matcher) PlugwiseCommandType(org.openhab.binding.plugwise.PlugwiseCommandType) Scheduler(org.quartz.Scheduler) PlugwiseBindingConfigElement(org.openhab.binding.plugwise.internal.PlugwiseGenericBindingProvider.PlugwiseBindingConfigElement) SetClockJob(org.openhab.binding.plugwise.internal.CirclePlus.SetClockJob) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) CronTrigger(org.quartz.CronTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) PlugwiseBindingProvider(org.openhab.binding.plugwise.PlugwiseBindingProvider)

Example 2 with PlugwiseCommandType

use of org.openhab.binding.plugwise.PlugwiseCommandType in project openhab1-addons by openhab.

the class PlugwiseBinding method internalReceiveCommand.

@Override
protected void internalReceiveCommand(String itemName, Command command) {
    PlugwiseBindingProvider provider = findFirstMatchingBindingProvider(itemName);
    if (command != null) {
        String commandAsString = command.toString();
        List<Command> commands = new ArrayList<Command>();
        // check if the command is valid for this item by checking if a pw ID exists
        String checkID = provider.getPlugwiseID(itemName, command);
        if (checkID != null) {
            commands.add(command);
        } else {
            // ooops - command is not defined, but maybe we have something of the same Type (e.g Decimal, String
            // types)
            // commands = provider.getCommandsByType(itemName, command.getClass());
            commands = provider.getAllCommands(itemName);
        }
        for (Command someCommand : commands) {
            String plugwiseID = provider.getPlugwiseID(itemName, someCommand);
            PlugwiseCommandType plugwiseCommandType = provider.getPlugwiseCommandType(itemName, someCommand);
            if (plugwiseID != null) {
                if (plugwiseCommandType != null) {
                    @SuppressWarnings("unused") boolean result = executeCommand(plugwiseID, plugwiseCommandType, commandAsString);
                // Each command is responsible to make sure that a result value for the action is polled from
                // the device
                // which then will be used to do a postUpdate
                // if new commands would be added later on that do not have this possibility, then a kind of
                // auto-update has to be performed here below
                } else {
                    logger.error("wrong command type for binding [Item={}, command={}]", itemName, commandAsString);
                }
            } else {
                logger.error("{} is an unrecognised command for Item {}", commandAsString, itemName);
            }
        }
    }
}
Also used : Command(org.openhab.core.types.Command) PlugwiseBindingProvider(org.openhab.binding.plugwise.PlugwiseBindingProvider) PlugwiseCommandType(org.openhab.binding.plugwise.PlugwiseCommandType) ArrayList(java.util.ArrayList)

Example 3 with PlugwiseCommandType

use of org.openhab.binding.plugwise.PlugwiseCommandType in project openhab1-addons by openhab.

the class PlugwiseGenericBindingProvider method parseBindingConfig.

/**
     * Parses the configuration string and update the provided config
     *
     * @param config
     * @param item
     * @param bindingConfig
     * @throws BindingConfigParseException
     */
private void parseBindingConfig(PlugwiseBindingConfig config, Item item, String bindingConfig) throws BindingConfigParseException {
    String commandAsString = null;
    String plugwiseID = null;
    String plugwiseCommand = null;
    int interval = 60;
    if (bindingConfig == null) {
        logger.warn("bindingConfig for item '{}' is null", item.getName());
        return;
    }
    Matcher actionWithJobMatcher = ACTION_CONFIG_WITH_JOB_PATTERN.matcher(bindingConfig);
    Matcher statusWithJobMatcher = STATUS_CONFIG_WITH_JOB_PATTERN.matcher(bindingConfig);
    Matcher actionWithoutJobMatcher = ACTION_CONFIG_WITHOUT_JOB_PATTERN.matcher(bindingConfig);
    Matcher statusWithoutJobMatcher = STATUS_CONFIG_WITHOUT_JOB_PATTERN.matcher(bindingConfig);
    if (!actionWithJobMatcher.matches() && !statusWithJobMatcher.matches() && !actionWithoutJobMatcher.matches() && !statusWithoutJobMatcher.matches()) {
        throw new //
        BindingConfigParseException(//
        "Plugwise binding configuration must consist of either:\n" + "* 2 parts: [config=" + statusWithoutJobMatcher + //
        "]\n" + "* 3 parts: [config=" + statusWithJobMatcher + //
        "]\n" + "           [config=" + actionWithoutJobMatcher + //
        "]\n" + "* 4 parts: [config=" + actionWithJobMatcher + "]");
    }
    if (actionWithJobMatcher.matches()) {
        commandAsString = actionWithJobMatcher.group(1);
        plugwiseID = actionWithJobMatcher.group(2);
        plugwiseCommand = actionWithJobMatcher.group(3);
        interval = Integer.valueOf(actionWithJobMatcher.group(4));
    } else if (statusWithJobMatcher.matches()) {
        commandAsString = null;
        plugwiseID = statusWithJobMatcher.group(1);
        plugwiseCommand = statusWithJobMatcher.group(2);
        interval = Integer.valueOf(statusWithJobMatcher.group(3));
    } else if (actionWithoutJobMatcher.matches()) {
        commandAsString = actionWithoutJobMatcher.group(1);
        plugwiseID = actionWithoutJobMatcher.group(2);
        plugwiseCommand = actionWithoutJobMatcher.group(3);
        interval = -1;
    } else if (statusWithoutJobMatcher.matches()) {
        commandAsString = null;
        plugwiseID = statusWithoutJobMatcher.group(1);
        plugwiseCommand = statusWithoutJobMatcher.group(2);
        interval = -1;
    }
    PlugwiseCommandType type = PlugwiseCommandType.getCommandType(plugwiseCommand);
    if (PlugwiseCommandType.validateBinding(type, item)) {
        PlugwiseBindingConfigElement newElement = new PlugwiseBindingConfigElement(plugwiseID, type, interval);
        Command command = null;
        if (commandAsString == null) {
            // for those configuration strings that are not really linked to a openHAB command we
            // create a dummy Command to be able to store the configuration information
            // I have choosen to do that with NumberItems
            NumberItem dummy = new NumberItem(Integer.toString(counter));
            command = createCommandFromString(dummy, Integer.toString(counter));
            counter++;
            config.put(command, newElement);
        } else {
            command = createCommandFromString(item, commandAsString);
            config.put(command, newElement);
        }
    } else {
        String validItemType = PlugwiseCommandType.getValidItemTypes(plugwiseCommand);
        if (StringUtils.isEmpty(validItemType)) {
            throw new BindingConfigParseException("'" + bindingConfig + "' is no valid binding type");
        } else {
            throw new BindingConfigParseException("'" + bindingConfig + "' is not bound to a valid item type. Valid item type(s): " + validItemType);
        }
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) Matcher(java.util.regex.Matcher) Command(org.openhab.core.types.Command) PlugwiseCommandType(org.openhab.binding.plugwise.PlugwiseCommandType) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Aggregations

PlugwiseCommandType (org.openhab.binding.plugwise.PlugwiseCommandType)3 Matcher (java.util.regex.Matcher)2 PlugwiseBindingProvider (org.openhab.binding.plugwise.PlugwiseBindingProvider)2 Command (org.openhab.core.types.Command)2 ArrayList (java.util.ArrayList)1 Pattern (java.util.regex.Pattern)1 SetClockJob (org.openhab.binding.plugwise.internal.CirclePlus.SetClockJob)1 PlugwiseBindingConfigElement (org.openhab.binding.plugwise.internal.PlugwiseGenericBindingProvider.PlugwiseBindingConfigElement)1 NumberItem (org.openhab.core.library.items.NumberItem)1 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)1 CronTrigger (org.quartz.CronTrigger)1 JobDataMap (org.quartz.JobDataMap)1 JobDetail (org.quartz.JobDetail)1 Scheduler (org.quartz.Scheduler)1 SchedulerException (org.quartz.SchedulerException)1 Trigger (org.quartz.Trigger)1 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)1