Search in sources :

Example 61 with PercentType

use of org.eclipse.smarthome.core.library.types.PercentType in project smarthome by eclipse.

the class HueLightHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    HueBridgeHandler hueBridge = getHueBridgeHandler();
    if (hueBridge == null) {
        logger.warn("hue bridge handler not found. Cannot handle command without bridge.");
        return;
    }
    FullLight light = getLight();
    if (light == null) {
        logger.debug("hue light not known on bridge. Cannot handle command.");
        return;
    }
    StateUpdate lightState = null;
    switch(channelUID.getId()) {
        case CHANNEL_COLORTEMPERATURE:
            if (command instanceof PercentType) {
                lightState = LightStateConverter.toColorTemperatureLightState((PercentType) command);
            } else if (command instanceof OnOffType) {
                lightState = LightStateConverter.toOnOffLightState((OnOffType) command);
                if (isOsramPar16) {
                    lightState = addOsramSpecificCommands(lightState, (OnOffType) command);
                }
            } else if (command instanceof IncreaseDecreaseType) {
                lightState = convertColorTempChangeToStateUpdate((IncreaseDecreaseType) command, light);
            }
            break;
        case CHANNEL_BRIGHTNESS:
            if (command instanceof PercentType) {
                lightState = LightStateConverter.toBrightnessLightState((PercentType) command);
            } else if (command instanceof OnOffType) {
                lightState = LightStateConverter.toOnOffLightState((OnOffType) command);
                if (isOsramPar16) {
                    lightState = addOsramSpecificCommands(lightState, (OnOffType) command);
                }
            } else if (command instanceof IncreaseDecreaseType) {
                lightState = convertBrightnessChangeToStateUpdate((IncreaseDecreaseType) command, light);
            }
            break;
        case CHANNEL_SWITCH:
            logger.trace("CHANNEL_SWITCH handling command {}", command);
            if (command instanceof OnOffType) {
                lightState = LightStateConverter.toOnOffLightState((OnOffType) command);
                if (isOsramPar16) {
                    lightState = addOsramSpecificCommands(lightState, (OnOffType) command);
                }
            }
            break;
        case CHANNEL_COLOR:
            if (command instanceof HSBType) {
                HSBType hsbCommand = (HSBType) command;
                if (hsbCommand.getBrightness().intValue() == 0) {
                    lightState = LightStateConverter.toOnOffLightState(OnOffType.OFF);
                } else {
                    lightState = LightStateConverter.toColorLightState(hsbCommand);
                }
            } else if (command instanceof PercentType) {
                lightState = LightStateConverter.toBrightnessLightState((PercentType) command);
            } else if (command instanceof OnOffType) {
                lightState = LightStateConverter.toOnOffLightState((OnOffType) command);
            } else if (command instanceof IncreaseDecreaseType) {
                lightState = convertBrightnessChangeToStateUpdate((IncreaseDecreaseType) command, light);
            }
            break;
        case CHANNEL_ALERT:
            if (command instanceof StringType) {
                lightState = LightStateConverter.toAlertState((StringType) command);
                if (lightState == null) {
                    // Unsupported StringType is passed. Log a warning
                    // message and return.
                    logger.warn("Unsupported String command: {}. Supported commands are: {}, {}, {} ", command, LightStateConverter.ALERT_MODE_NONE, LightStateConverter.ALERT_MODE_SELECT, LightStateConverter.ALERT_MODE_LONG_SELECT);
                    return;
                } else {
                    scheduleAlertStateRestore(command);
                }
            }
            break;
        case CHANNEL_EFFECT:
            if (command instanceof OnOffType) {
                lightState = LightStateConverter.toOnOffEffectState((OnOffType) command);
            }
            break;
    }
    if (lightState != null) {
        hueBridge.updateLightState(light, lightState);
    } else {
        logger.warn("Command sent to an unknown channel id: {}", channelUID);
    }
}
Also used : StateUpdate(org.eclipse.smarthome.binding.hue.internal.StateUpdate) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) StringType(org.eclipse.smarthome.core.library.types.StringType) IncreaseDecreaseType(org.eclipse.smarthome.core.library.types.IncreaseDecreaseType) FullLight(org.eclipse.smarthome.binding.hue.internal.FullLight) PercentType(org.eclipse.smarthome.core.library.types.PercentType) HSBType(org.eclipse.smarthome.core.library.types.HSBType)

Example 62 with PercentType

use of org.eclipse.smarthome.core.library.types.PercentType in project smarthome by eclipse.

the class WemoLightHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    if (command instanceof RefreshType) {
        try {
            getDeviceState();
        } catch (Exception e) {
            logger.debug("Exception during poll : {}", e);
        }
    } else {
        Configuration configuration = getConfig();
        configuration.get(DEVICE_ID);
        WemoBridgeHandler wemoBridge = getWemoBridgeHandler();
        if (wemoBridge == null) {
            logger.debug("wemoBridgeHandler not found, cannot handle command");
            return;
        }
        String devUDN = "uuid:" + wemoBridge.getThing().getConfiguration().get(UDN).toString();
        logger.trace("WeMo Bridge to send command to : {}", devUDN);
        String value = null;
        String capability = null;
        switch(channelUID.getId()) {
            case CHANNEL_BRIGHTNESS:
                capability = "10008";
                if (command instanceof PercentType) {
                    int newBrightness = ((PercentType) command).intValue();
                    logger.trace("wemoLight received Value {}", newBrightness);
                    int value1 = Math.round(newBrightness * 255 / 100);
                    value = value1 + ":0";
                    currentBrightness = newBrightness;
                } else if (command instanceof OnOffType) {
                    switch(command.toString()) {
                        case "ON":
                            value = "255:0";
                            break;
                        case "OFF":
                            value = "0:0";
                            break;
                    }
                } else if (command instanceof IncreaseDecreaseType) {
                    int newBrightness;
                    switch(command.toString()) {
                        case "INCREASE":
                            currentBrightness = currentBrightness + DIM_STEPSIZE;
                            newBrightness = Math.round(currentBrightness * 255 / 100);
                            if (newBrightness > 255) {
                                newBrightness = 255;
                            }
                            value = newBrightness + ":0";
                            break;
                        case "DECREASE":
                            currentBrightness = currentBrightness - DIM_STEPSIZE;
                            newBrightness = Math.round(currentBrightness * 255 / 100);
                            if (newBrightness < 0) {
                                newBrightness = 0;
                            }
                            value = newBrightness + ":0";
                            break;
                    }
                }
                break;
            case CHANNEL_STATE:
                capability = "10006";
                switch(command.toString()) {
                    case "ON":
                        value = "1";
                        break;
                    case "OFF":
                        value = "0";
                        break;
                }
                break;
        }
        try {
            String soapHeader = "\"urn:Belkin:service:bridge:1#SetDeviceStatus\"";
            String content = "<?xml version=\"1.0\"?>" + "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" + "<s:Body>" + "<u:SetDeviceStatus xmlns:u=\"urn:Belkin:service:bridge:1\">" + "<DeviceStatusList>" + "&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;DeviceStatus&gt;&lt;DeviceID&gt;" + wemoLightID + "&lt;/DeviceID&gt;&lt;IsGroupAction&gt;NO&lt;/IsGroupAction&gt;&lt;CapabilityID&gt;" + capability + "&lt;/CapabilityID&gt;&lt;CapabilityValue&gt;" + value + "&lt;/CapabilityValue&gt;&lt;/DeviceStatus&gt;" + "</DeviceStatusList>" + "</u:SetDeviceStatus>" + "</s:Body>" + "</s:Envelope>";
            String wemoURL = getWemoURL();
            if (wemoURL != null && capability != null && value != null) {
                String wemoCallResponse = WemoHttpCall.executeCall(wemoURL, soapHeader, content);
                if (wemoCallResponse != null) {
                    if (capability != null && capability.equals("10008") && value != null) {
                        OnOffType binaryState = null;
                        binaryState = value.equals("0") ? OnOffType.OFF : OnOffType.ON;
                        if (binaryState != null) {
                            updateState(CHANNEL_STATE, binaryState);
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("Could not send command to WeMo Bridge", e);
        }
    }
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) IncreaseDecreaseType(org.eclipse.smarthome.core.library.types.IncreaseDecreaseType) PercentType(org.eclipse.smarthome.core.library.types.PercentType) RefreshType(org.eclipse.smarthome.core.types.RefreshType)

Example 63 with PercentType

use of org.eclipse.smarthome.core.library.types.PercentType in project smarthome by eclipse.

the class WemoLightHandler method getDeviceState.

/**
 * The {@link getDeviceState} is used for polling the actual state of a WeMo Light and updating the according
 * channel states.
 */
public void getDeviceState() {
    logger.debug("Request actual state for LightID '{}'", wemoLightID);
    try {
        String soapHeader = "\"urn:Belkin:service:bridge:1#GetDeviceStatus\"";
        String content = "<?xml version=\"1.0\"?>" + "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" + "<s:Body>" + "<u:GetDeviceStatus xmlns:u=\"urn:Belkin:service:bridge:1\">" + "<DeviceIDs>" + wemoLightID + "</DeviceIDs>" + "</u:GetDeviceStatus>" + "</s:Body>" + "</s:Envelope>";
        String wemoURL = getWemoURL();
        if (wemoURL != null) {
            String wemoCallResponse = WemoHttpCall.executeCall(wemoURL, soapHeader, content);
            if (wemoCallResponse != null) {
                wemoCallResponse = StringEscapeUtils.unescapeXml(wemoCallResponse);
                String response = StringUtils.substringBetween(wemoCallResponse, "<CapabilityValue>", "</CapabilityValue>");
                logger.trace("wemoNewLightState = {}", response);
                String[] splitResponse = response.split(",");
                if (splitResponse[0] != null) {
                    OnOffType binaryState = null;
                    binaryState = splitResponse[0].equals("0") ? OnOffType.OFF : OnOffType.ON;
                    if (binaryState != null) {
                        updateState(CHANNEL_STATE, binaryState);
                    }
                }
                if (splitResponse[1] != null) {
                    String[] splitBrightness = splitResponse[1].split(":");
                    if (splitBrightness[0] != null) {
                        int newBrightnessValue = Integer.valueOf(splitBrightness[0]);
                        int newBrightness = Math.round(newBrightnessValue * 100 / 255);
                        logger.trace("newBrightness = {}", newBrightness);
                        State newBrightnessState = new PercentType(newBrightness);
                        updateState(CHANNEL_BRIGHTNESS, newBrightnessState);
                        currentBrightness = newBrightness;
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Could not retrieve new Wemo light state", e);
    }
}
Also used : OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) State(org.eclipse.smarthome.core.types.State) PercentType(org.eclipse.smarthome.core.library.types.PercentType)

Aggregations

PercentType (org.eclipse.smarthome.core.library.types.PercentType)63 Test (org.junit.Test)25 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)17 State (org.eclipse.smarthome.core.types.State)17 HSBType (org.eclipse.smarthome.core.library.types.HSBType)16 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)13 LifxMessageUtil.increaseDecreasePercentType (org.eclipse.smarthome.binding.lifx.internal.util.LifxMessageUtil.increaseDecreasePercentType)9 StringType (org.eclipse.smarthome.core.library.types.StringType)9 IncreaseDecreaseType (org.eclipse.smarthome.core.library.types.IncreaseDecreaseType)7 RefreshType (org.eclipse.smarthome.core.types.RefreshType)7 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)6 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)4 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)4 QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)4 BigDecimal (java.math.BigDecimal)3 ValueSet (org.eclipse.smarthome.binding.dmx.internal.ValueSet)3 FadeAction (org.eclipse.smarthome.binding.dmx.internal.action.FadeAction)3 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)3 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)3 LinkedList (java.util.LinkedList)2