diff --git a/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/KM200Device.java b/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/KM200Device.java index 3867fbc6e28..92a987852ee 100644 --- a/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/KM200Device.java +++ b/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/KM200Device.java @@ -282,7 +282,7 @@ public class KM200Device { object = serviceTreeMap.get(servicePath[1]); } for (int i = 2; i < len; i++) { - if (object.serviceTreeMap.containsKey(servicePath[i])) { + if (object != null && object.serviceTreeMap.containsKey(servicePath[i])) { object = object.serviceTreeMap.get(servicePath[i]); continue; } else { @@ -313,7 +313,7 @@ public class KM200Device { } } for (int i = 2; i < len; i++) { - if (object.serviceTreeMap.containsKey(servicePath[i])) { + if (object != null && object.serviceTreeMap.containsKey(servicePath[i])) { object = object.serviceTreeMap.get(servicePath[i]); continue; } else { diff --git a/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/KM200HandlerFactory.java b/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/KM200HandlerFactory.java index a4ea867034f..9e82f28661d 100644 --- a/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/KM200HandlerFactory.java +++ b/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/KM200HandlerFactory.java @@ -105,7 +105,9 @@ public class KM200HandlerFactory extends BaseThingHandlerFactory { protected synchronized void removeHandler(ThingHandler thingHandler) { if (thingHandler instanceof KM200GatewayHandler) { ServiceRegistration serviceReg = this.discoveryServiceRegs.get(thingHandler.getThing().getUID()); - serviceReg.unregister(); + if (serviceReg != null) { + serviceReg.unregister(); + } discoveryServiceRegs.remove(thingHandler.getThing().getUID()); } } diff --git a/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/discovery/KM200GatewayDiscoveryService.java b/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/discovery/KM200GatewayDiscoveryService.java index 055a7d87eb6..6e416569610 100644 --- a/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/discovery/KM200GatewayDiscoveryService.java +++ b/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/discovery/KM200GatewayDiscoveryService.java @@ -138,40 +138,44 @@ public class KM200GatewayDiscoveryService extends AbstractDiscoveryService imple KM200ServiceObject switchObject = object.serviceTreeMap.get(key).serviceTreeMap .get(SWITCH_PROGRAM_PATH_NAME); - if (switchObject.serviceTreeMap.isEmpty()) { - continue; - } - /* - * if the device has only one switching program then the "activeSwitchProgram" service is - * not existing. In this case we are using a fix path to this one service. - */ - if (!currExists) { - if (switchObject.serviceTreeMap.keySet().size() == 1) { - currParaRepl = switchObject.serviceTreeMap.entrySet().iterator().next().getKey(); + if (switchObject != null) { + if (switchObject.serviceTreeMap.isEmpty()) { + continue; } - } - KM200SwitchProgramServiceHandler valPara = (KM200SwitchProgramServiceHandler) switchObject.serviceTreeMap - .entrySet().iterator().next().getValue().getValueParameter(); - if (null != valPara) { - String posName = valPara.getPositiveSwitch(); - String negName = valPara.getNegativeSwitch(); - if (null == posName || null == negName) { - logger.warn("Service switches not found!"); - return; + /* + * if the device has only one switching program then the "activeSwitchProgram" service + * is + * not existing. In this case we are using a fix path to this one service. + */ + if (!currExists) { + if (switchObject.serviceTreeMap.keySet().size() == 1) { + currParaRepl = switchObject.serviceTreeMap.entrySet().iterator().next() + .getKey(); + } + } + KM200SwitchProgramServiceHandler valPara = (KM200SwitchProgramServiceHandler) switchObject.serviceTreeMap + .entrySet().iterator().next().getValue().getValueParameter(); + if (null != valPara) { + String posName = valPara.getPositiveSwitch(); + String negName = valPara.getNegativeSwitch(); + if (null == posName || null == negName) { + logger.warn("Service switches not found!"); + return; + } + ThingUID subThingUID = new ThingUID(tType.getThingTypeUID(), bridgeUID, + key + "-switchprogram"); + Map subProperties = new HashMap<>(4); + subProperties.put("root", KM200Utils.translatesPathToName( + root + "/" + key + "/" + SWITCH_PROGRAM_PATH_NAME + "/" + currParaRepl)); + subProperties.put(SWITCH_PROGRAM_CURRENT_PATH_NAME, + KM200Utils.translatesPathToName(currentPathName)); + subProperties.put(SWITCH_PROGRAM_POSITIVE, posName); + subProperties.put(SWITCH_PROGRAM_NEGATIVE, negName); + DiscoveryResult subDiscoveryResult = DiscoveryResultBuilder.create(subThingUID) + .withBridge(bridgeUID).withLabel(key + " switch program") + .withProperties(subProperties).build(); + thingDiscovered(subDiscoveryResult); } - ThingUID subThingUID = new ThingUID(tType.getThingTypeUID(), bridgeUID, - key + "-switchprogram"); - Map subProperties = new HashMap<>(4); - subProperties.put("root", KM200Utils.translatesPathToName( - root + "/" + key + "/" + SWITCH_PROGRAM_PATH_NAME + "/" + currParaRepl)); - subProperties.put(SWITCH_PROGRAM_CURRENT_PATH_NAME, - KM200Utils.translatesPathToName(currentPathName)); - subProperties.put(SWITCH_PROGRAM_POSITIVE, posName); - subProperties.put(SWITCH_PROGRAM_NEGATIVE, negName); - DiscoveryResult subDiscoveryResult = DiscoveryResultBuilder.create(subThingUID) - .withBridge(bridgeUID).withLabel(key + " switch program") - .withProperties(subProperties).build(); - thingDiscovered(subDiscoveryResult); } } } diff --git a/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/handler/KM200GatewayHandler.java b/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/handler/KM200GatewayHandler.java index 8c2327b893b..d8fd1d5170b 100644 --- a/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/handler/KM200GatewayHandler.java +++ b/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/handler/KM200GatewayHandler.java @@ -17,6 +17,7 @@ import static org.openhab.binding.km200.internal.KM200BindingConstants.*; import java.math.BigDecimal; import java.net.InetAddress; import java.net.UnknownHostException; +import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -30,6 +31,8 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import javax.crypto.Cipher; + import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jetty.client.HttpClient; @@ -109,6 +112,17 @@ public class KM200GatewayHandler extends BaseBridgeHandler { @Override public void initialize() { + try { + int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES/ECB/NoPadding"); + if (maxKeyLen <= 128) { + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, + "Java Cryptography Extension (JCE) have to be installed"); + return; + } + } catch (NoSuchAlgorithmException e) { + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "AES encoding not supported"); + return; + } if (!getDevice().getInited()) { logger.info("Update KM50/100/200 gateway configuration, it takes a minute...."); getConfiguration(); @@ -323,17 +337,20 @@ public class KM200GatewayHandler extends BaseBridgeHandler { for (String subKey : asProperties) { if (serObj.serviceTreeMap.containsKey(subKey)) { KM200ServiceObject subKeyObj = serObj.serviceTreeMap.get(subKey); - String subKeyType = subKeyObj.getServiceType(); - if (!DATA_TYPE_STRING_VALUE.equals(subKeyType) && !DATA_TYPE_FLOAT_VALUE.equals(subKeyType)) { - continue; - } - if (bridgeProperties.containsKey(subKey)) { - bridgeProperties.remove(subKey); - } - Object value = subKeyObj.getValue(); - logger.trace("Add Property: {} :{}", subKey, value); - if (null != value) { - bridgeProperties.put(subKey, value.toString()); + if (subKeyObj != null) { + String subKeyType = subKeyObj.getServiceType(); + if (!DATA_TYPE_STRING_VALUE.equals(subKeyType) + && !DATA_TYPE_FLOAT_VALUE.equals(subKeyType)) { + continue; + } + if (bridgeProperties.containsKey(subKey)) { + bridgeProperties.remove(subKey); + } + Object value = subKeyObj.getValue(); + logger.trace("Add Property: {} :{}", subKey, value); + if (null != value) { + bridgeProperties.put(subKey, value.toString()); + } } } } diff --git a/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/handler/KM200SwitchProgramServiceHandler.java b/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/handler/KM200SwitchProgramServiceHandler.java index 3926553a4bd..cfd5cad9021 100644 --- a/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/handler/KM200SwitchProgramServiceHandler.java +++ b/bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/handler/KM200SwitchProgramServiceHandler.java @@ -20,7 +20,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.km200.internal.KM200Device; import org.openhab.binding.km200.internal.KM200ServiceObject; @@ -68,11 +67,10 @@ public class KM200SwitchProgramServiceHandler { private static List days = new ArrayList<>(Arrays.asList(TYPE_MONDAY, TYPE_TUESDAY, TYPE_WEDNESDAY, TYPE_THURSDAY, TYPE_FRIDAY, TYPE_SATURDAY, TYPE_SUNDAY)); - public static List<@NonNull StateOption> daysList = new ArrayList<>( - Arrays.asList(new StateOption(TYPE_MONDAY, "Monday"), new StateOption(TYPE_TUESDAY, "Tuesday"), - new StateOption(TYPE_WEDNESDAY, "Wednesday"), new StateOption(TYPE_THURSDAY, "Thursday"), - new StateOption(TYPE_FRIDAY, "Friday"), new StateOption(TYPE_SATURDAY, "Saturday"), - new StateOption(TYPE_SUNDAY, "Sunday"))); + public static List daysList = List.of(new StateOption(TYPE_MONDAY, "Monday"), + new StateOption(TYPE_TUESDAY, "Tuesday"), new StateOption(TYPE_WEDNESDAY, "Wednesday"), + new StateOption(TYPE_THURSDAY, "Thursday"), new StateOption(TYPE_FRIDAY, "Friday"), + new StateOption(TYPE_SATURDAY, "Saturday"), new StateOption(TYPE_SUNDAY, "Sunday")); /* List with setpoints */ private List setpoints = new ArrayList<>(); @@ -118,9 +116,13 @@ public class KM200SwitchProgramServiceHandler { initWeeklist(setpoint); weekMap = switchMap.get(setpoint); } - List dayList = weekMap.get(day); - dayList.add(time); - Collections.sort(dayList); + if (weekMap != null) { + List dayList = weekMap.get(day); + if (dayList != null) { + dayList.add(time); + Collections.sort(dayList); + } + } } /** @@ -484,12 +486,20 @@ public class KM200SwitchProgramServiceHandler { synchronized (switchMap) { Map> weekP = switchMap.get(getPositiveSwitch()); Map> weekN = switchMap.get(getNegativeSwitch()); - if (weekP.isEmpty() && weekN.isEmpty()) { + if (weekP != null && weekN != null) { + if (weekP.isEmpty() && weekN.isEmpty()) { + return 0; + } + List daysListP = weekP.get(getActiveDay()); + List daysListN = weekN.get(getActiveDay()); + if (daysListP != null && daysListN != null) { + return Math.min(daysListP.size(), daysListN.size()); + } else { + return 0; + } + } else { return 0; } - List daysListP = weekP.get(getActiveDay()); - List daysListN = weekN.get(getActiveDay()); - return Math.min(daysListP.size(), daysListN.size()); } } @@ -515,7 +525,7 @@ public class KM200SwitchProgramServiceHandler { Map> week = switchMap.get(getPositiveSwitch()); if (week != null) { List daysList = week.get(getActiveDay()); - if (!daysList.isEmpty()) { + if (daysList != null && !daysList.isEmpty()) { Integer cycl = getActiveCycle(); if (cycl <= daysList.size()) { return (daysList.get(getActiveCycle() - 1)); @@ -534,7 +544,7 @@ public class KM200SwitchProgramServiceHandler { Map> week = switchMap.get(getNegativeSwitch()); if (week != null) { List daysList = week.get(getActiveDay()); - if (!daysList.isEmpty()) { + if (daysList != null && !daysList.isEmpty()) { Integer cycl = getActiveCycle(); if (cycl <= daysList.size()) { return (daysList.get(getActiveCycle() - 1));