Search in sources :

Example 1 with MyqBindingProvider

use of org.openhab.binding.myq.MyqBindingProvider in project openhab1-addons by openhab.

the class MyqBinding method poll.

/**
     * Poll for device changes
     */
private void poll() {
    if (invalidCredentials || this.myqOnlineData == null) {
        logger.trace("Invalid Account Credentials");
        return;
    }
    try {
        // Get myQ Data
        MyqDeviceData myqStatus = myqOnlineData.getMyqData();
        for (MyqBindingProvider provider : providers) {
            for (String mygItemName : provider.getInBindingItemNames()) {
                MyqBindingConfig deviceConfig = getConfigForItemName(mygItemName);
                if (deviceConfig != null) {
                    MyqDevice device = myqStatus.getDevice(deviceConfig.deviceIndex);
                    if (device != null) {
                        if (device instanceof GarageDoorDevice) {
                            GarageDoorDevice garageopener = (GarageDoorDevice) device;
                            State newState = UnDefType.UNDEF;
                            if (!deviceConfig.attribute.isEmpty() && garageopener.hasAttribute(deviceConfig.attribute)) {
                                newState = TypeParser.parseState(deviceConfig.acceptedDataTypes, garageopener.getAttribute(deviceConfig.attribute));
                                if (newState == null) {
                                    newState = UnDefType.UNDEF;
                                }
                            } else {
                                for (Class<? extends State> type : deviceConfig.acceptedDataTypes) {
                                    if (OpenClosedType.class == type) {
                                        if (garageopener.getStatus().isClosed()) {
                                            newState = OpenClosedType.CLOSED;
                                            break;
                                        } else {
                                            newState = OpenClosedType.OPEN;
                                            break;
                                        }
                                    } else if (UpDownType.class == type) {
                                        if (garageopener.getStatus().isClosed()) {
                                            newState = UpDownType.DOWN;
                                            break;
                                        } else if (garageopener.getStatus().isOpen()) {
                                            newState = UpDownType.UP;
                                            break;
                                        }
                                    } else if (OnOffType.class == type) {
                                        if (garageopener.getStatus().isClosed()) {
                                            newState = OnOffType.OFF;
                                            break;
                                        } else {
                                            newState = OnOffType.ON;
                                            break;
                                        }
                                    } else if (PercentType.class == type) {
                                        if (garageopener.getStatus().isClosed()) {
                                            newState = PercentType.HUNDRED;
                                            break;
                                        } else if (garageopener.getStatus().isOpen()) {
                                            newState = PercentType.ZERO;
                                            break;
                                        } else if (garageopener.getStatus().inMotion()) {
                                            newState = new PercentType(50);
                                            break;
                                        }
                                    } else if (StringType.class == type) {
                                        newState = new StringType(garageopener.getStatus().getLabel());
                                        break;
                                    }
                                }
                            }
                            eventPublisher.postUpdate(mygItemName, newState);
                            // make sure we are polling frequently
                            if (garageopener.getStatus().inMotion()) {
                                beginRapidPoll(false);
                            }
                        } else if (device instanceof LampDevice) {
                            LampDevice lampDevice = (LampDevice) device;
                            State newState = UnDefType.UNDEF;
                            if (!deviceConfig.attribute.isEmpty() && lampDevice.hasAttribute(deviceConfig.attribute)) {
                                newState = TypeParser.parseState(deviceConfig.acceptedDataTypes, lampDevice.getAttribute(deviceConfig.attribute));
                                if (newState == null) {
                                    newState = UnDefType.UNDEF;
                                }
                            } else {
                                for (Class<? extends State> type : deviceConfig.acceptedDataTypes) {
                                    if (OnOffType.class == type) {
                                        newState = lampDevice.getState();
                                        break;
                                    }
                                }
                            }
                            eventPublisher.postUpdate(mygItemName, newState);
                        }
                    }
                }
            }
        }
    } catch (InvalidLoginException e) {
        logger.error("Could not log in, please check your credentials.", e);
        invalidCredentials = true;
    } catch (IOException e) {
        logger.error("Could not connect to MyQ service", e);
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) IOException(java.io.IOException) OnOffType(org.openhab.core.library.types.OnOffType) State(org.openhab.core.types.State) MyqBindingProvider(org.openhab.binding.myq.MyqBindingProvider)

Aggregations

IOException (java.io.IOException)1 MyqBindingProvider (org.openhab.binding.myq.MyqBindingProvider)1 OnOffType (org.openhab.core.library.types.OnOffType)1 PercentType (org.openhab.core.library.types.PercentType)1 StringType (org.openhab.core.library.types.StringType)1 UpDownType (org.openhab.core.library.types.UpDownType)1 State (org.openhab.core.types.State)1