Search in sources :

Example 11 with VelbusPacket

use of org.openremote.agent.protocol.velbus.VelbusPacket in project openremote by openremote.

the class InputProcessor method getStatusRequestPackets.

@Override
public List<VelbusPacket> getStatusRequestPackets(VelbusDevice device) {
    // Initialise basic state as disabled addresses don't broadcast this info
    for (int i = 1; i <= getMaxChannelNumber(device.getDeviceType()); i++) {
        device.setProperty("CH" + i, ChannelState.RELEASED);
        device.setProperty("CH" + i + "_ENABLED", BooleanDevicePropertyValue.FALSE);
        device.setProperty("CH" + i + "_LOCKED", BooleanDevicePropertyValue.FALSE);
        device.setProperty("CH" + i + "_INVERTED", BooleanDevicePropertyValue.FALSE);
        device.setProperty("CH" + i + "_LED", LedState.OFF);
    }
    if (device.getDeviceType() == VelbusDeviceType.VMB4AN) {
        return Collections.singletonList(new VelbusPacket(device.getBaseAddress(), VelbusPacket.OutboundCommand.MODULE_STATUS.getCode(), (byte) 0xFF));
    }
    return Collections.singletonList(new VelbusPacket(device.getBaseAddress(), VelbusPacket.OutboundCommand.MODULE_STATUS.getCode(), (byte) 0x00));
}
Also used : VelbusPacket(org.openremote.agent.protocol.velbus.VelbusPacket)

Example 12 with VelbusPacket

use of org.openremote.agent.protocol.velbus.VelbusPacket in project openremote by openremote.

the class InputProcessor method getLedStatePackets.

protected static List<VelbusPacket> getLedStatePackets(VelbusDevice velbusDevice, int address, int channelNumber, LedState ledState) {
    byte ledStateByte = (byte) (Math.pow(2, channelNumber) - 1);
    VelbusPacket.OutboundCommand ledCommand = VelbusPacket.OutboundCommand.valueOf("LED_" + ledState.toString());
    return Collections.singletonList(new VelbusPacket(address, ledCommand.getCode(), VelbusPacket.PacketPriority.LOW, ledStateByte));
}
Also used : VelbusPacket(org.openremote.agent.protocol.velbus.VelbusPacket)

Example 13 with VelbusPacket

use of org.openremote.agent.protocol.velbus.VelbusPacket in project openremote by openremote.

the class ProgramsProcessor method getStatusRequestPackets.

@Override
public List<VelbusPacket> getStatusRequestPackets(VelbusDevice device) {
    // Add alarm memory read requests
    Integer alarm1 = getAlarmMemoryLocation(1, device.getDeviceType());
    Integer alarm2 = getAlarmMemoryLocation(2, device.getDeviceType());
    if (alarm1 != null && alarm2 != null) {
        return Arrays.asList(new VelbusPacket(device.getBaseAddress(), VelbusPacket.OutboundCommand.READ_MEMORY_BLOCK.getCode(), (byte) (alarm1 >> 8), alarm1.byteValue()), new VelbusPacket(device.getBaseAddress(), VelbusPacket.OutboundCommand.READ_MEMORY_BLOCK.getCode(), (byte) (alarm2 >> 8), alarm2.byteValue()));
    }
    return Collections.emptyList();
}
Also used : VelbusPacket(org.openremote.agent.protocol.velbus.VelbusPacket)

Example 14 with VelbusPacket

use of org.openremote.agent.protocol.velbus.VelbusPacket in project openremote by openremote.

the class ProgramsProcessor method getPropertyWritePackets.

@Override
public List<VelbusPacket> getPropertyWritePackets(VelbusDevice device, String property, Value value) {
    Optional<Pair<Integer, String>> channelNumberAndPropertySuffix = getChannelNumberAndPropertySuffix(device, CHANNEL_REGEX, property);
    if (channelNumberAndPropertySuffix.isPresent()) {
        switch(channelNumberAndPropertySuffix.get().value) {
            case PROGRAM_STEPS_ENABLED_SUFFIX:
                return Values.getBooleanCoerced(value).map(enabled -> getProgramStepsPackets(device, channelNumberAndPropertySuffix.get().key, enabled, 0xFFFFF)).orElse(null);
            case PROGRAM_STEPS_DISABLED_SECONDS_SUFFIX:
                return Values.getIntegerCoerced(value).map(durationSeconds -> getProgramStepsPackets(device, channelNumberAndPropertySuffix.get().key, false, durationSeconds)).orElse(null);
        }
        return null;
    }
    if (property.equals("ALL" + PROGRAM_STEPS_ENABLED_SUFFIX)) {
        return Values.getBooleanCoerced(value).map(enabled -> getProgramStepsPackets(device, 0xFF, enabled, 0xFFFFF)).orElse(null);
    }
    if (property.equals("ALL" + PROGRAM_STEPS_DISABLED_SECONDS_SUFFIX)) {
        return Values.getIntegerCoerced(value).map(durationSeconds -> getProgramStepsPackets(device, 0xFF, false, durationSeconds)).orElse(null);
    }
    if (property.equals("PROGRAM")) {
        return EnumUtil.enumFromValue(Program.class, value).map(program -> Collections.singletonList(new VelbusPacket(device.getBaseAddress(), VelbusPacket.OutboundCommand.PROGRAM_SELECT.getCode(), VelbusPacket.PacketPriority.LOW, (byte) program.getCode()))).orElse(null);
    }
    if (property.equals("SUNRISE_ENABLED") || property.equals("SUNSET_ENABLED")) {
        return Values.getBooleanCoerced(value).map(enabled -> {
            boolean sunriseEnabled = device.getPropertyValue("SUNRISE_ENABLED") != null && ((BooleanDevicePropertyValue) device.getPropertyValue("SUNRISE_ENABLED")).getPropertyValue();
            boolean sunsetEnabled = device.getPropertyValue("SUNSET_ENABLED") != null && ((BooleanDevicePropertyValue) device.getPropertyValue("SUNSET_ENABLED")).getPropertyValue();
            if (property.equals("SUNRISE_ENABLED")) {
                sunriseEnabled = enabled;
            } else {
                sunsetEnabled = enabled;
            }
            int sunriseSunsetByte = 0;
            if (sunriseEnabled) {
                sunriseSunsetByte += 1;
            }
            if (sunsetEnabled) {
                sunriseSunsetByte += 2;
            }
            // Push new values into the device cache so they are instantly available
            device.setProperty("SUNRISE_ENABLED", sunriseEnabled ? BooleanDevicePropertyValue.TRUE : BooleanDevicePropertyValue.FALSE);
            device.setProperty("SUNSET_ENABLED", sunsetEnabled ? BooleanDevicePropertyValue.TRUE : BooleanDevicePropertyValue.FALSE);
            return Collections.singletonList(new VelbusPacket(device.getBaseAddress(), VelbusPacket.OutboundCommand.SET_SUNRISE_SUNSET.getCode(), VelbusPacket.PacketPriority.LOW, (byte) 0xFF, (byte) sunriseSunsetByte));
        }).orElse(null);
    }
    if (property.startsWith("ALARM")) {
        int alarmNumber;
        try {
            alarmNumber = Integer.parseInt(property.substring(5, 6));
        } catch (NumberFormatException e) {
            LOG.log(Level.INFO, "Alarm number must be number 1 or 2");
            return null;
        }
        String alarmProperty = property.substring(7);
        switch(alarmProperty) {
            case "ENABLED":
            case "WAKE_TIME":
            case "BED_TIME":
                String alarmEnabledProperty = "ALARM" + alarmNumber + "_ENABLED";
                String alarmWakeTimeProperty = "ALARM" + alarmNumber + "_WAKE_TIME";
                String alarmBedTimeProperty = "ALARM" + alarmNumber + "_BED_TIME";
                String alarmMasterProperty = "ALARM" + alarmNumber + "_MASTER";
                if (!device.hasPropertyValue(alarmEnabledProperty) || !device.hasPropertyValue(alarmWakeTimeProperty) || !device.hasPropertyValue(alarmBedTimeProperty) || !device.hasPropertyValue(alarmMasterProperty)) {
                    LOG.info("Device cache doesn't contain alarm enabled, type and/or time properties");
                    return null;
                }
                boolean enabled = ((BooleanDevicePropertyValue) device.getPropertyValue(alarmEnabledProperty)).getPropertyValue();
                String wakeTime = ((StringDevicePropertyValue) device.getPropertyValue(alarmWakeTimeProperty)).getPropertyValue();
                String bedTime = ((StringDevicePropertyValue) device.getPropertyValue(alarmBedTimeProperty)).getPropertyValue();
                boolean isGlobal = ((BooleanDevicePropertyValue) device.getPropertyValue(alarmMasterProperty)).getPropertyValue();
                if ("ENABLED".equals(alarmProperty)) {
                    Optional<Boolean> setEnabled = Values.getBooleanCoerced(value);
                    if (!setEnabled.isPresent()) {
                        return null;
                    }
                    enabled = setEnabled.get();
                } else if ("WAKE_TIME".equals(alarmProperty)) {
                    if (value == null) {
                        return null;
                    }
                    wakeTime = value.toString();
                } else if ("BED_TIME".equals(alarmProperty)) {
                    if (value == null) {
                        return null;
                    }
                    bedTime = value.toString();
                }
                String[] wakeTimeValues = wakeTime.split(":");
                String[] bedTimeValues = bedTime.split(":");
                if (wakeTimeValues.length != 2 || bedTimeValues.length != 2) {
                    LOG.info("Time property values must be in the format HH:mm");
                    return null;
                }
                int wakeHours, wakeMins, bedHours, bedMins;
                try {
                    wakeHours = Integer.parseInt(wakeTimeValues[0]);
                    wakeMins = Integer.parseInt(wakeTimeValues[1]);
                    bedHours = Integer.parseInt(bedTimeValues[0]);
                    bedMins = Integer.parseInt(bedTimeValues[1]);
                    if (wakeHours < 0 || wakeHours > 23 || wakeMins < 0 || wakeMins > 59 || bedHours < 0 || bedHours > 23 || bedMins < 0 || bedMins > 59) {
                        throw new NumberFormatException("Hours and/or minutes out of allowed range");
                    }
                } catch (NumberFormatException e) {
                    LOG.log(Level.INFO, "Time property values must be in the format HH:mm", e);
                    return null;
                }
                // Update property values and also send memory read request to ensure update occurred
                device.setProperty(alarmEnabledProperty, enabled ? BooleanDevicePropertyValue.TRUE : BooleanDevicePropertyValue.FALSE);
                device.setProperty(alarmWakeTimeProperty, new StringDevicePropertyValue(wakeTime));
                device.setProperty(alarmBedTimeProperty, new StringDevicePropertyValue(bedTime));
                device.velbusNetwork.scheduleTask(() -> {
                    List<VelbusPacket> packets = getStatusRequestPackets(device);
                    device.velbusNetwork.sendPackets(packets.toArray(new VelbusPacket[packets.size()]));
                }, 500);
                return Collections.singletonList(new VelbusPacket(isGlobal ? 0x00 : device.getBaseAddress(), VelbusPacket.OutboundCommand.SET_ALARM.getCode(), VelbusPacket.PacketPriority.LOW, (byte) alarmNumber, (byte) wakeHours, (byte) wakeMins, (byte) bedHours, (byte) bedMins, (byte) (enabled ? 1 : 0)));
        }
    }
    return null;
}
Also used : Value(org.openremote.model.value.Value) LOG(org.openremote.agent.protocol.velbus.AbstractVelbusProtocol.LOG) java.util(java.util) ValueType(org.openremote.model.value.ValueType) Pair(org.openremote.model.util.Pair) EnumUtil(org.openremote.model.util.EnumUtil) Values(org.openremote.model.value.Values) VelbusPacket(org.openremote.agent.protocol.velbus.VelbusPacket) AttributeType(org.openremote.model.attribute.AttributeType) Level(java.util.logging.Level) VelbusPacket(org.openremote.agent.protocol.velbus.VelbusPacket) Pair(org.openremote.model.util.Pair)

Example 15 with VelbusPacket

use of org.openremote.agent.protocol.velbus.VelbusPacket in project openremote by openremote.

the class ThermostatProcessor method getTempModePackets.

protected static List<VelbusPacket> getTempModePackets(VelbusDevice device, TemperatureMode mode, int durationMinutes) {
    // Check thermostat is supported
    Optional<Integer> thermostatChannel = getThermostatChannelNumber(device);
    if (!thermostatChannel.isPresent()) {
        LOG.info("Thermostat mode is not supported on this device");
        return null;
    }
    // Max minutes = 65279 but 65280 means program step
    durationMinutes = Math.min(durationMinutes, 0xFF00);
    if (durationMinutes < 0) {
        // Permanent change
        durationMinutes = 0xFFFF;
    }
    List<VelbusPacket> packets = new ArrayList<>();
    // If thermostat is disabled we first need to enable it again
    if (device.getPropertyValue("TEMP_STATE") == TemperatureState.DISABLED) {
        packets.addAll(InputProcessor.getLockStatePackets(device, thermostatChannel.get(), 0));
    }
    packets.add(new VelbusPacket(device.getBaseAddress(), mode.getMode1Command().getCode(), VelbusPacket.PacketPriority.LOW, (byte) 0x00));
    packets.add(new VelbusPacket(device.getBaseAddress(), mode.getMode2Command().getCode(), VelbusPacket.PacketPriority.LOW, (byte) (durationMinutes >> 8), (byte) durationMinutes));
    return packets;
}
Also used : VelbusPacket(org.openremote.agent.protocol.velbus.VelbusPacket)

Aggregations

VelbusPacket (org.openremote.agent.protocol.velbus.VelbusPacket)15 java.util (java.util)1 Level (java.util.logging.Level)1 LOG (org.openremote.agent.protocol.velbus.AbstractVelbusProtocol.LOG)1 AttributeType (org.openremote.model.attribute.AttributeType)1 EnumUtil (org.openremote.model.util.EnumUtil)1 Pair (org.openremote.model.util.Pair)1 Value (org.openremote.model.value.Value)1 ValueType (org.openremote.model.value.ValueType)1 Values (org.openremote.model.value.Values)1