Search in sources :

Example 1 with CommonId

use of org.openhab.binding.weather.internal.model.common.CommonId in project openhab1-addons by openhab.

the class CommonIdHandler method loadMapping.

/**
     * Load predefined common id mappings from an XML file.
     */
public void loadMapping() throws Exception {
    Unmarshaller um = JAXBContext.newInstance(CommonIdList.class).createUnmarshaller();
    InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("weather/common-id-mappings.xml");
    CommonIdList mappings = (CommonIdList) um.unmarshal(stream);
    for (CommonId commonId : mappings.getCommonIds()) {
        for (CommonIdProvider commonIdProvider : commonId.getProviders()) {
            Map<String, CommonId> commonIds = providerCommonIds.get(commonIdProvider.getName());
            if (commonIds == null) {
                commonIds = new HashMap<String, CommonId>();
                providerCommonIds.put(commonIdProvider.getName(), commonIds);
            }
            addCommonId(commonIdProvider.getIds(), "id", commonIdProvider, commonIds, commonId);
            addCommonId(commonIdProvider.getIcons(), "icon", commonIdProvider, commonIds, commonId);
        }
    }
}
Also used : CommonId(org.openhab.binding.weather.internal.model.common.CommonId) CommonIdList(org.openhab.binding.weather.internal.model.common.CommonIdList) InputStream(java.io.InputStream) CommonIdProvider(org.openhab.binding.weather.internal.model.common.CommonIdProvider) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 2 with CommonId

use of org.openhab.binding.weather.internal.model.common.CommonId in project openhab1-addons by openhab.

the class CommonIdHandler method setCommonId.

/**
     * Sets the common condition id into the weather object.
     */
public void setCommonId(Weather weather) {
    Map<String, CommonId> commonIds = providerCommonIds.get(weather.getProvider());
    if (commonIds == null) {
        throw new RuntimeException("No common ids for provider " + weather.getProvider() + " declared");
    }
    Condition cond = weather.getCondition();
    CommonId cid = commonIds.get(cond.getId());
    if (cid == null) {
        cid = commonIds.get(cond.getIcon());
    }
    if (cid != null) {
        cond.setCommonId(cid.getId());
    } else {
        ToStringBuilder tsb = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("provider", weather.getProvider()).append("id", cond.getId()).append("icon", cond.getIcon());
        logger.warn("CommonId not found: {}", tsb.toString());
    }
}
Also used : CommonId(org.openhab.binding.weather.internal.model.common.CommonId) Condition(org.openhab.binding.weather.internal.model.Condition) ToStringBuilder(org.apache.commons.lang.builder.ToStringBuilder)

Aggregations

CommonId (org.openhab.binding.weather.internal.model.common.CommonId)2 InputStream (java.io.InputStream)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 ToStringBuilder (org.apache.commons.lang.builder.ToStringBuilder)1 Condition (org.openhab.binding.weather.internal.model.Condition)1 CommonIdList (org.openhab.binding.weather.internal.model.common.CommonIdList)1 CommonIdProvider (org.openhab.binding.weather.internal.model.common.CommonIdProvider)1