Search in sources :

Example 1 with PowerMaxState

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

the class PowerMaxStatusMessage method handleMessage.

/**
     * {@inheritDoc}
     */
@Override
public PowerMaxState handleMessage() {
    super.handleMessage();
    PowerMaxState updatedState = new PowerMaxState();
    byte[] message = getRawData();
    byte eventType = message[3];
    if (eventType == 0x02) {
        int zoneStatus = (message[4] & 0x000000FF) | ((message[5] << 8) & 0x0000FF00) | ((message[6] << 16) & 0x00FF0000) | ((message[7] << 24) & 0xFF000000);
        int batteryStatus = (message[8] & 0x000000FF) | ((message[9] << 8) & 0x0000FF00) | ((message[10] << 16) & 0x00FF0000) | ((message[11] << 24) & 0xFF000000);
        for (int i = 1; i <= PowerMaxPanelSettings.getThePanelSettings().getNbZones(); i++) {
            updatedState.setSensorTripped(i, ((zoneStatus >> (i - 1)) & 0x1) > 0);
            updatedState.setSensorLowBattery(i, ((batteryStatus >> (i - 1)) & 0x1) > 0);
        }
    } else if (eventType == 0x04) {
        byte sysStatus = message[4];
        byte sysFlags = message[5];
        byte eventZone = message[6];
        byte zoneEType = message[7];
        int x10Status = (message[10] & 0x000000FF) | ((message[11] << 8) & 0x0000FF00);
        String zoneETypeStr = ((zoneEType & 0x000000FF) < eventTypeTable.length) ? eventTypeTable[zoneEType & 0x000000FF] : "UNKNOWN";
        if (zoneEType == 0x03) {
            updatedState.setSensorTripped(eventZone, Boolean.TRUE);
            updatedState.setSensorLastTripped(eventZone, System.currentTimeMillis());
        } else if (zoneEType == 0x04) {
            updatedState.setSensorTripped(eventZone, Boolean.FALSE);
        } else if (zoneEType == 0x05) {
            PowerMaxZoneSettings zone = PowerMaxPanelSettings.getThePanelSettings().getZoneSettings(eventZone);
            if ((zone != null) && zone.getSensorType().equalsIgnoreCase("unknown")) {
                zone.setSensorType("Motion");
            }
            updatedState.setSensorTripped(eventZone, Boolean.TRUE);
            updatedState.setSensorLastTripped(eventZone, System.currentTimeMillis());
        }
        // PGM & X10 devices
        for (int i = 0; i < PowerMaxPanelSettings.getThePanelSettings().getNbPGMX10Devices(); i++) {
            updatedState.setPGMX10DeviceStatus(i, ((x10Status >> i) & 0x1) > 0);
        }
        String sysStatusStr = "";
        if ((sysFlags & 0x1) == 1) {
            sysStatusStr = sysStatusStr + "Ready, ";
            updatedState.setReady(true);
        } else {
            sysStatusStr = sysStatusStr + "Not ready, ";
            updatedState.setReady(false);
        }
        if (((sysFlags >> 1) & 0x1) == 1) {
            sysStatusStr = sysStatusStr + "Alert in memory, ";
            updatedState.setAlertInMemory(true);
        } else {
            updatedState.setAlertInMemory(false);
        }
        if (((sysFlags >> 2) & 0x1) == 1) {
            sysStatusStr = sysStatusStr + "Trouble, ";
            updatedState.setTrouble(true);
        } else {
            updatedState.setTrouble(false);
        }
        if (((sysFlags >> 3) & 0x1) == 1) {
            sysStatusStr = sysStatusStr + "Bypass on, ";
            updatedState.setBypass(true);
        } else {
            updatedState.setBypass(false);
            for (int i = 1; i <= PowerMaxPanelSettings.getThePanelSettings().getNbZones(); i++) {
                updatedState.setSensorBypassed(i, false);
            }
        }
        if (((sysFlags >> 4) & 0x1) == 1) {
            sysStatusStr = sysStatusStr + "Last 10 seconds, ";
        }
        if (((sysFlags >> 5) & 0x1) == 1) {
            sysStatusStr = sysStatusStr + zoneETypeStr;
            if (eventZone == 0xFF) {
                sysStatusStr = sysStatusStr + " from Panel, ";
            } else if (eventZone > 0) {
                sysStatusStr = sysStatusStr + String.format(" in Zone %d, ", eventZone);
            } else {
                sysStatusStr = sysStatusStr + ", ";
            }
        }
        if (((sysFlags >> 6) & 0x1) == 1) {
            sysStatusStr = sysStatusStr + "Status changed, ";
        }
        if (((sysFlags >> 7) & 0x1) == 1) {
            sysStatusStr = sysStatusStr + "Alarm event, ";
            updatedState.setAlarmActive(true);
        } else {
            updatedState.setAlarmActive(false);
        }
        sysStatusStr = sysStatusStr.substring(0, sysStatusStr.length() - 2);
        String statusStr = ((sysStatus & 0x000000FF) < sysStatusTable.length) ? sysStatusTable[sysStatus & 0x000000FF] : "UNKNOWN";
        updatedState.setArmMode(statusStr);
        updatedState.setStatusStr(statusStr + ", " + sysStatusStr);
        for (int i = 1; i <= PowerMaxPanelSettings.getThePanelSettings().getNbZones(); i++) {
            PowerMaxZoneSettings zone = PowerMaxPanelSettings.getThePanelSettings().getZoneSettings(i);
            if (zone != null) {
                // mode: armed or not: 4=armed home; 5=armed away
                int mode = sysStatus & 0x0000000F;
                // Zone is shown as armed if
                // the sensor type always triggers an alarm
                // or the system is armed away (mode = 5)
                // or the system is armed home (mode = 4) and the zone is not interior(-follow)
                boolean armed = (!zone.getType().equalsIgnoreCase("Non-Alarm") && (zone.isAlwaysInAlarm() || (mode == 0x5) || ((mode == 0x4) && !zone.getType().equalsIgnoreCase("Interior-Follow") && !zone.getType().equalsIgnoreCase("Interior"))));
                updatedState.setSensorArmed(i, armed);
            }
        }
    } else if (eventType == 0x06) {
        int zoneBypass = (message[8] & 0x000000FF) | ((message[9] << 8) & 0x0000FF00) | ((message[10] << 16) & 0x00FF0000) | ((message[11] << 24) & 0xFF000000);
        for (int i = 1; i <= PowerMaxPanelSettings.getThePanelSettings().getNbZones(); i++) {
            updatedState.setSensorBypassed(i, ((zoneBypass >> (i - 1)) & 0x1) > 0);
        }
    }
    return updatedState;
}
Also used : PowerMaxZoneSettings(org.openhab.binding.powermax.internal.state.PowerMaxZoneSettings) PowerMaxState(org.openhab.binding.powermax.internal.state.PowerMaxState)

Example 2 with PowerMaxState

use of org.openhab.binding.powermax.internal.state.PowerMaxState 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 3 with PowerMaxState

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

the class PowerMaxEventLogMessage method handleMessage.

/**
     * {@inheritDoc}
     */
@Override
public PowerMaxState handleMessage() {
    super.handleMessage();
    PowerMaxState updatedState = new PowerMaxState();
    byte[] message = getRawData();
    int eventNum = message[3] & 0x000000FF;
    eventNum--;
    if (eventNum == 0) {
        int eventCnt = message[2] & 0x000000FF;
        updatedState.setEventLogSize(eventCnt - 1);
    } else {
        int second = message[4] & 0x000000FF;
        int minute = message[5] & 0x000000FF;
        int hour = message[6] & 0x000000FF;
        int day = message[7] & 0x000000FF;
        int month = message[8] & 0x000000FF;
        int year = (message[9] & 0x000000FF) + 2000;
        byte eventZone = message[10];
        byte logEvent = message[11];
        String logEventStr = ((logEvent & 0x000000FF) < logEventTable.length) ? logEventTable[logEvent & 0x000000FF] : "UNKNOWN";
        String logUserStr = ((eventZone & 0x000000FF) < logUserTable.length) ? logUserTable[eventZone & 0x000000FF] : "UNKNOWN";
        String eventStr;
        if (PowerMaxPanelSettings.getThePanelSettings().getPanelType().getPartitions() > 1) {
            String part;
            if ((second & 0x01) == 0x01) {
                part = "Part. 1";
            } else if ((second & 0x02) == 0x02) {
                part = "Part. 2";
            } else if ((second & 0x04) == 0x04) {
                part = "Part. 3";
            } else {
                part = "Panel";
            }
            eventStr = String.format("%02d/%02d/%04d %02d:%02d / %s: %s (%s)", day, month, year, hour, minute, part, logEventStr, logUserStr);
        } else {
            eventStr = String.format("%02d/%02d/%04d %02d:%02d:%02d: %s (%s)", day, month, year, hour, minute, second, logEventStr, logUserStr);
        }
        updatedState.setEventLogSize(eventNum);
        updatedState.setEventLog(eventNum, eventStr);
    }
    return updatedState;
}
Also used : PowerMaxState(org.openhab.binding.powermax.internal.state.PowerMaxState)

Example 4 with PowerMaxState

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

the class PowerMaxPanelMessage method handleMessage.

/**
     * {@inheritDoc}
     */
@Override
public PowerMaxState handleMessage() {
    super.handleMessage();
    PowerMaxState updatedState = new PowerMaxState();
    byte[] message = getRawData();
    int msgCnt = message[2] & 0x000000FF;
    for (int i = 1; i <= msgCnt; i++) {
        byte eventZone = message[2 + 2 * i];
        byte logEvent = message[3 + 2 * i];
        int eventType = logEvent & 0x0000007F;
        String logEventStr = (eventType < PowerMaxEventLogMessage.logEventTable.length) ? PowerMaxEventLogMessage.logEventTable[eventType] : "UNKNOWN";
        String logUserStr = ((eventZone & 0x000000FF) < PowerMaxEventLogMessage.logUserTable.length) ? PowerMaxEventLogMessage.logUserTable[eventZone & 0x000000FF] : "UNKNOWN";
        updatedState.setPanelStatus(logEventStr + " (" + logUserStr + ")");
        HashMap<Integer, String> alarmTypes = new HashMap<Integer, String>();
        alarmTypes.put(0x01, "Intruder");
        alarmTypes.put(0x02, "Intruder");
        alarmTypes.put(0x03, "Intruder");
        alarmTypes.put(0x04, "Intruder");
        alarmTypes.put(0x05, "Intruder");
        alarmTypes.put(0x06, "Tamper");
        alarmTypes.put(0x07, "Tamper");
        alarmTypes.put(0x08, "Tamper");
        alarmTypes.put(0x09, "Tamper");
        alarmTypes.put(0x0B, "Panic");
        alarmTypes.put(0x0C, "Panic");
        alarmTypes.put(0x20, "Fire");
        alarmTypes.put(0x23, "Emergency");
        alarmTypes.put(0x49, "Gas");
        alarmTypes.put(0x4D, "Flood");
        String alarmStatus = alarmTypes.get(eventType);
        if (alarmStatus == null) {
            alarmStatus = "None";
        }
        updatedState.setAlarmType(alarmStatus);
        HashMap<Integer, String> troubleTypes = new HashMap<Integer, String>();
        troubleTypes.put(0x0A, "Communication");
        troubleTypes.put(0x0F, "General");
        troubleTypes.put(0x29, "Battery");
        troubleTypes.put(0x2B, "Power");
        troubleTypes.put(0x2D, "Battery");
        troubleTypes.put(0x2F, "Jamming");
        troubleTypes.put(0x31, "Communication");
        troubleTypes.put(0x33, "Telephone");
        troubleTypes.put(0x36, "Power");
        troubleTypes.put(0x38, "Battery");
        troubleTypes.put(0x3B, "Battery");
        troubleTypes.put(0x3C, "Battery");
        troubleTypes.put(0x40, "Battery");
        troubleTypes.put(0x43, "Battery");
        String troubleStatus = troubleTypes.get(eventType);
        if (troubleStatus == null) {
            troubleStatus = "None";
        }
        updatedState.setTroubleType(troubleStatus);
        if (eventType == 0x60) {
            // System reset
            updatedState.setDownloadSetupRequired(true);
        }
    }
    return updatedState;
}
Also used : HashMap(java.util.HashMap) PowerMaxState(org.openhab.binding.powermax.internal.state.PowerMaxState)

Example 5 with PowerMaxState

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

the class PowerMaxBinding method powerMaxEventReceived.

/**
     * PowerMax Alarm incoming message event handler
     *
     * @param event
     */
@Override
public void powerMaxEventReceived(EventObject event) {
    PowerMaxEvent powerMaxEvent = (PowerMaxEvent) event;
    PowerMaxBaseMessage message = powerMaxEvent.getPowerMaxMessage();
    if (logger.isDebugEnabled()) {
        logger.debug("powerMaxEventReceived(): received message {}", (message.getReceiveType() != null) ? message.getReceiveType().toString() : String.format("%02X", message.getCode()));
    }
    if (message instanceof PowerMaxInfoMessage) {
        ((PowerMaxInfoMessage) message).setAutoSyncTime(autoSyncTime);
    }
    PowerMaxState updateState = message.handleMessage();
    if (updateState != null) {
        if ((currentState.isPowerlinkMode() != null) && currentState.isPowerlinkMode().equals(Boolean.TRUE) && (updateState.isDownloadSetupRequired() != null) && updateState.isDownloadSetupRequired().equals(Boolean.TRUE)) {
            // After Enrolling Powerlink or if a reset is required
            logger.info("PowerMax alarm binding: Reset");
            PowerMaxCommDriver.getTheCommDriver().startDownload();
            if (currentState.getLastKeepAlive() != null) {
                currentState.setLastKeepAlive(System.currentTimeMillis());
            }
        } else if ((currentState.isPowerlinkMode() != null) && currentState.isPowerlinkMode().equals(Boolean.FALSE) && (updateState.getLastKeepAlive() != null)) {
            // Were are in standard mode but received a keep alive message
            // so we switch in PowerLink mode
            logger.info("PowerMax alarm binding: Switching to Powerlink mode");
            PowerMaxCommDriver.getTheCommDriver().startDownload();
        }
        boolean doProcessSettings = (updateState.isPowerlinkMode() != null);
        for (int i = 1; i <= PowerMaxPanelSettings.getThePanelSettings().getNbZones(); i++) {
            if ((updateState.isSensorArmed(i) != null) && updateState.isSensorArmed(i).equals(Boolean.TRUE) && (currentState.isSensorBypassed(i) != null) && currentState.isSensorBypassed(i).equals(Boolean.TRUE)) {
                updateState.setSensorArmed(i, false);
            }
        }
        updateState.keepOnlyDifferencesWith(currentState);
        updateItemsFromAlarmState(updateState);
        currentState.merge(updateState);
        if (updateState.getUpdateSettings() != null) {
            PowerMaxPanelSettings.getThePanelSettings().updateRawSettings(updateState.getUpdateSettings());
        }
        if (doProcessSettings) {
            // There is a change of mode (standard or Powerlink)
            PowerMaxPanelSettings.getThePanelSettings().process(currentState.isPowerlinkMode(), panelType, PowerMaxCommDriver.getTheCommDriver().getSyncTimeCheck());
            updateItemsFromPanelSettings();
            if (currentState.isPowerlinkMode()) {
                logger.info("PowerMax alarm binding: running in Powerlink mode");
                PowerMaxCommDriver.getTheCommDriver().sendMessage(PowerMaxSendType.RESTORE);
            } else {
                logger.info("PowerMax alarm binding: running in Standard mode");
                PowerMaxCommDriver.getTheCommDriver().sendMessage(PowerMaxSendType.ZONESNAME);
                PowerMaxCommDriver.getTheCommDriver().sendMessage(PowerMaxSendType.ZONESTYPE);
                PowerMaxCommDriver.getTheCommDriver().sendMessage(PowerMaxSendType.STATUS);
            }
            PowerMaxCommDriver.getTheCommDriver().exitDownload();
        }
    }
}
Also used : PowerMaxEvent(org.openhab.binding.powermax.internal.connector.PowerMaxEvent) PowerMaxInfoMessage(org.openhab.binding.powermax.internal.message.PowerMaxInfoMessage) PowerMaxBaseMessage(org.openhab.binding.powermax.internal.message.PowerMaxBaseMessage) PowerMaxState(org.openhab.binding.powermax.internal.state.PowerMaxState)

Aggregations

PowerMaxState (org.openhab.binding.powermax.internal.state.PowerMaxState)11 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 PowerMaxBindingConfig (org.openhab.binding.powermax.PowerMaxBindingConfig)1 PowerMaxBindingProvider (org.openhab.binding.powermax.PowerMaxBindingProvider)1 PowerMaxEvent (org.openhab.binding.powermax.internal.connector.PowerMaxEvent)1 PowerMaxBaseMessage (org.openhab.binding.powermax.internal.message.PowerMaxBaseMessage)1 PowerMaxInfoMessage (org.openhab.binding.powermax.internal.message.PowerMaxInfoMessage)1 PowerMaxPanelSettings (org.openhab.binding.powermax.internal.state.PowerMaxPanelSettings)1 PowerMaxPanelType (org.openhab.binding.powermax.internal.state.PowerMaxPanelType)1 PowerMaxZoneSettings (org.openhab.binding.powermax.internal.state.PowerMaxZoneSettings)1 DateTimeType (org.openhab.core.library.types.DateTimeType)1 State (org.openhab.core.types.State)1