Search in sources :

Example 1 with PowerMaxPanelSettings

use of org.openhab.binding.powermax.internal.state.PowerMaxPanelSettings in project openhab1-addons by openhab.

the class PowerMaxBinding method execute.

/**
     * {@inheritDoc}
     */
@Override
protected void execute() {
    logger.debug("PowerMax alarm Execute");
    if (!isProperlyConfigured()) {
        logger.debug("execute(): not yet properly configured");
        return;
    }
    //
    // Off items linked to motion sensors after the delay defined by
    // the variable motionOffDelay
    //
    long now = System.currentTimeMillis();
    PowerMaxPanelSettings settings = PowerMaxPanelSettings.getThePanelSettings();
    PowerMaxState updateState = null;
    if ((currentState != null) && (settings != null)) {
        for (int i = 1; i <= settings.getNbZones(); i++) {
            if ((settings.getZoneSettings(i) != null) && settings.getZoneSettings(i).getSensorType().equalsIgnoreCase("Motion") && (currentState.isSensorTripped(i) == Boolean.TRUE) && (currentState.getSensorLastTripped(i) != null) && ((now - currentState.getSensorLastTripped(i)) > motionOffDelay)) {
                if (updateState == null) {
                    updateState = new PowerMaxState();
                }
                updateState.setSensorTripped(i, false);
            }
        }
    }
    if (updateState != null) {
        updateItemsFromAlarmState(PowerMaxSelectorType.ZONE_STATUS, updateState);
        currentState.merge(updateState);
    }
    if (PowerMaxCommDriver.getTheCommDriver() != null) {
        connected = PowerMaxCommDriver.getTheCommDriver().isConnected();
    }
    // Check that we receive a keep alive message during the last minute
    if (connected && (currentState.isPowerlinkMode() != null) && currentState.isPowerlinkMode().equals(Boolean.TRUE) && (currentState.getLastKeepAlive() != null) && ((now - currentState.getLastKeepAlive()) > ONE_MINUTE)) {
        // Let Powermax know we are alive
        PowerMaxCommDriver.getTheCommDriver().sendMessage(PowerMaxSendType.RESTORE);
        currentState.setLastKeepAlive(now);
    }
    if (!connected) {
        logger.debug("execute(): trying to reconnect...");
        closeConnection();
        if (triggerItemsUpdate) {
            logger.debug("execute(): items update enabled");
            itemUpdateDisabled = false;
            triggerItemsUpdate = false;
        }
        currentState = new PowerMaxState();
        openConnection();
        if (connected) {
            if (forceStandardMode) {
                currentState.setPowerlinkMode(false);
                updateItemsFromAlarmState(PowerMaxSelectorType.PANEL_MODE, currentState);
                settings = PowerMaxPanelSettings.getThePanelSettings();
                settings.process(false, panelType, null);
                updateItemsFromPanelSettings();
                logger.info("PowerMax alarm binding: running in Standard mode");
                PowerMaxCommDriver.getTheCommDriver().sendMessage(PowerMaxSendType.ZONESNAME);
                PowerMaxCommDriver.getTheCommDriver().sendMessage(PowerMaxSendType.ZONESTYPE);
                PowerMaxCommDriver.getTheCommDriver().sendMessage(PowerMaxSendType.STATUS);
            } else {
                PowerMaxCommDriver.getTheCommDriver().startDownload();
            }
        } else {
            logger.debug("execute(): reconnection failed");
        }
    } else if (triggerItemsUpdate) {
        logger.debug("execute(): update all items");
        itemUpdateDisabled = false;
        triggerItemsUpdate = false;
        // Adjust all the items to the current alarm state
        updateItemsFromAlarmState(currentState);
        // Adjust all the items to the current alarm settings
        updateItemsFromPanelSettings();
    }
}
Also used : PowerMaxPanelSettings(org.openhab.binding.powermax.internal.state.PowerMaxPanelSettings) PowerMaxState(org.openhab.binding.powermax.internal.state.PowerMaxState)

Example 2 with PowerMaxPanelSettings

use of org.openhab.binding.powermax.internal.state.PowerMaxPanelSettings in project openhab1-addons by openhab.

the class PowerMaxBinding method updateItemsFromPanelSettings.

/**
     * Post item updates on the bus to match the alarm panel settings
     *
     * @param provider
     *            filter on provider; if null, no filter on provider
     * @param name
     *            filter on item name; if null, no filter on item name
     * @param selector
     *            filter on selector type; if null, no filter on selector type
     */
private synchronized void updateItemsFromPanelSettings(PowerMaxBindingProvider provider, String name, PowerMaxSelectorType selector) {
    if (itemUpdateDisabled) {
        logger.debug("updateItemsFromPanelSettings(): items update disabled");
        return;
    }
    if (provider == null) {
        for (PowerMaxBindingProvider prov : providers) {
            if (prov != null) {
                updateItemsFromPanelSettings(prov, name, selector);
            }
        }
    } else {
        PowerMaxPanelSettings settings = PowerMaxPanelSettings.getThePanelSettings();
        for (String itemName : provider.getItemNames()) {
            if ((name == null) || itemName.equals(name)) {
                String value = null;
                PowerMaxBindingConfig config = provider.getConfig(itemName);
                if ((selector == null) || (selector == config.getSelectorType())) {
                    switch(config.getSelectorType()) {
                        case PANEL_TYPE:
                            value = (settings.getPanelType() != null) ? settings.getPanelType().getLabel() : null;
                            break;
                        case PANEL_SERIAL:
                            value = settings.getPanelSerial();
                            break;
                        case PANEL_EPROM:
                            value = settings.getPanelEprom();
                            break;
                        case PANEL_SOFTWARE:
                            value = settings.getPanelSoftware();
                            break;
                        default:
                            break;
                    }
                }
                if (value != null) {
                    State itemState = TypeParser.parseState(config.getAcceptedDataTypes(), value);
                    if (itemState != null) {
                        eventPublisher.postUpdate(itemName, itemState);
                    }
                }
            }
        }
    }
}
Also used : PowerMaxBindingConfig(org.openhab.binding.powermax.PowerMaxBindingConfig) PowerMaxState(org.openhab.binding.powermax.internal.state.PowerMaxState) State(org.openhab.core.types.State) PowerMaxPanelSettings(org.openhab.binding.powermax.internal.state.PowerMaxPanelSettings) PowerMaxBindingProvider(org.openhab.binding.powermax.PowerMaxBindingProvider)

Example 3 with PowerMaxPanelSettings

use of org.openhab.binding.powermax.internal.state.PowerMaxPanelSettings in project openhab1-addons by openhab.

the class PowerMaxBinding method internalReceiveCommand.

/**
     * {@inheritDoc}
     */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    // the code being executed when a command was sent on the openHAB
    // event bus goes here. This method is only called if one of the
    // BindingProviders provide a binding for the given 'itemName'.
    logger.debug("internalReceiveCommand({},{}) is called", itemName, command);
    if (!isProperlyConfigured() || !connected) {
        logger.warn("PowerMax alarm binding is not properly configured. Command is ignored.");
        return;
    }
    String armMode = null;
    String actionPGMX10 = null;
    Byte devicePGMX10 = null;
    Boolean bypass = null;
    byte zoneNr = 0;
    String commandStr = null;
    PowerMaxPanelSettings settings = PowerMaxPanelSettings.getThePanelSettings();
    PowerMaxCommDriver comm = PowerMaxCommDriver.getTheCommDriver();
    for (PowerMaxBindingProvider provider : providers) {
        PowerMaxBindingConfig config = provider.getConfig(itemName);
        if (config == null) {
            continue;
        }
        switch(config.getSelectorType()) {
            case PARTITION_ARM_MODE:
                if (command instanceof StringType) {
                    armMode = command.toString();
                }
                break;
            case PARTITION_ARMED:
                if (command instanceof OnOffType) {
                    armMode = command.equals(OnOffType.ON) ? "Armed" : "Disarmed";
                }
                break;
            case PGM_STATUS:
                if (command instanceof OnOffType) {
                    actionPGMX10 = command.toString();
                }
                break;
            case X10_STATUS:
                if (command instanceof OnOffType || command instanceof StringType) {
                    actionPGMX10 = command.toString();
                }
                try {
                    devicePGMX10 = Byte.parseByte(config.getSelectorParam());
                } catch (NumberFormatException e) {
                    logger.warn("PowerMax alarm binding: invalid X10 device id: {}", config.getSelectorParam());
                    actionPGMX10 = null;
                }
                break;
            case ZONE_BYPASSED:
                if (command instanceof OnOffType) {
                    bypass = command.equals(OnOffType.ON) ? Boolean.TRUE : Boolean.FALSE;
                }
                try {
                    zoneNr = Byte.parseByte(config.getSelectorParam());
                } catch (NumberFormatException e) {
                    logger.warn("PowerMax alarm binding: invalid zone number: {}", config.getSelectorParam());
                    bypass = null;
                }
                break;
            case COMMAND:
                if (command instanceof StringType) {
                    commandStr = command.toString();
                    eventPublisher.postUpdate(itemName, new StringType(commandStr));
                }
                break;
            default:
                break;
        }
        if (armMode != null) {
            HashMap<String, Boolean> allowedModes = new HashMap<String, Boolean>();
            allowedModes.put("Disarmed", allowDisarming);
            allowedModes.put("Stay", allowArming);
            allowedModes.put("Armed", allowArming);
            allowedModes.put("StayInstant", allowArming);
            allowedModes.put("ArmedInstant", allowArming);
            allowedModes.put("Night", allowArming);
            allowedModes.put("NightInstant", allowArming);
            Boolean allowed = allowedModes.get(armMode);
            if ((allowed == null) || !allowed) {
                logger.warn("PowerMax alarm binding: rejected command {}", armMode);
            } else {
                comm.requestArmMode(armMode, currentState.isPowerlinkMode() ? PowerMaxPanelSettings.getThePanelSettings().getFirstPinCode() : pinCode);
            }
            break;
        } else if (actionPGMX10 != null) {
            comm.sendPGMX10(actionPGMX10, devicePGMX10);
            break;
        } else if (bypass != null) {
            if ((currentState.isPowerlinkMode() == null) || currentState.isPowerlinkMode().equals(Boolean.FALSE)) {
                logger.warn("PowerMax alarm binding: Bypass option only supported in Powerlink mode");
            } else if (!PowerMaxPanelSettings.getThePanelSettings().isBypassEnabled()) {
                logger.warn("PowerMax alarm binding: Bypass option not enabled in panel settings");
            } else {
                comm.sendZoneBypass(bypass.booleanValue(), zoneNr, PowerMaxPanelSettings.getThePanelSettings().getFirstPinCode());
            }
            break;
        } else if (commandStr != null) {
            if (commandStr.equalsIgnoreCase("get_event_log")) {
                comm.requestEventLog(currentState.isPowerlinkMode() ? PowerMaxPanelSettings.getThePanelSettings().getFirstPinCode() : pinCode);
            } else if (commandStr.equalsIgnoreCase("download_setup")) {
                if ((currentState.isPowerlinkMode() == null) || currentState.isPowerlinkMode().equals(Boolean.FALSE)) {
                    logger.warn("PowerMax alarm binding: download setup only supported in Powerlink mode");
                } else {
                    comm.startDownload();
                    if (currentState.getLastKeepAlive() != null) {
                        currentState.setLastKeepAlive(System.currentTimeMillis());
                    }
                }
            } else if (commandStr.equalsIgnoreCase("log_setup")) {
                settings.log();
            } else if (commandStr.equalsIgnoreCase("help_items")) {
                settings.helpItems();
            } else {
                logger.warn("PowerMax alarm binding: rejected command {}", commandStr);
            }
            break;
        }
    }
}
Also used : PowerMaxBindingConfig(org.openhab.binding.powermax.PowerMaxBindingConfig) StringType(org.openhab.core.library.types.StringType) HashMap(java.util.HashMap) PowerMaxCommDriver(org.openhab.binding.powermax.internal.message.PowerMaxCommDriver) OnOffType(org.openhab.core.library.types.OnOffType) PowerMaxPanelSettings(org.openhab.binding.powermax.internal.state.PowerMaxPanelSettings) PowerMaxBindingProvider(org.openhab.binding.powermax.PowerMaxBindingProvider)

Aggregations

PowerMaxPanelSettings (org.openhab.binding.powermax.internal.state.PowerMaxPanelSettings)3 PowerMaxBindingConfig (org.openhab.binding.powermax.PowerMaxBindingConfig)2 PowerMaxBindingProvider (org.openhab.binding.powermax.PowerMaxBindingProvider)2 PowerMaxState (org.openhab.binding.powermax.internal.state.PowerMaxState)2 HashMap (java.util.HashMap)1 PowerMaxCommDriver (org.openhab.binding.powermax.internal.message.PowerMaxCommDriver)1 OnOffType (org.openhab.core.library.types.OnOffType)1 StringType (org.openhab.core.library.types.StringType)1 State (org.openhab.core.types.State)1