]> git.basschouten.com Git - openhab-addons.git/commitdiff
[km200] Added JCE check and removed some warnings (#11841)
authorMarkus Eckhardt <Markinus@users.noreply.github.com>
Mon, 17 Jan 2022 17:47:38 +0000 (18:47 +0100)
committerGitHub <noreply@github.com>
Mon, 17 Jan 2022 17:47:38 +0000 (18:47 +0100)
* Added a check if JCE is installed
* removed warnings

Signed-off-by: Markus Eckhardt <github@familie-eckhardt.eu>
bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/KM200Device.java
bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/KM200HandlerFactory.java
bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/discovery/KM200GatewayDiscoveryService.java
bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/handler/KM200GatewayHandler.java
bundles/org.openhab.binding.km200/src/main/java/org/openhab/binding/km200/internal/handler/KM200SwitchProgramServiceHandler.java

index 3867fbc6e286e3de16d203719cdffa7800e22719..92a987852eef76e703321b3ac3cc80650ba0fa93 100644 (file)
@@ -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 {
index a4ea867034ffd1ca7f8c0a059c46f7ef1700df4b..9e82f28661d02dd57a6a2b85ee0be9491a9bae68 100644 (file)
@@ -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());
         }
     }
index 055a7d87eb6f2f9913449efcb0285d63211776b4..6e416569610a8202647b552fe9804d2f4988bf75 100644 (file)
@@ -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<String, Object> 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<String, Object> 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);
                             }
                         }
                     }
index 8c2327b893b10f59582247f8fc723aefd9087716..d8fd1d5170b10ba382cb333a3fe38af24cd78576 100644 (file)
@@ -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());
+                            }
                         }
                     }
                 }
index 3926553a4bda42a2f75541c835d7f1d0d8198a6e..cfd5cad9021914853abd6b2b090b6c88b4019b73 100644 (file)
@@ -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<String> 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<StateOption> 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<String> setpoints = new ArrayList<>();
@@ -118,9 +116,13 @@ public class KM200SwitchProgramServiceHandler {
             initWeeklist(setpoint);
             weekMap = switchMap.get(setpoint);
         }
-        List<Integer> dayList = weekMap.get(day);
-        dayList.add(time);
-        Collections.sort(dayList);
+        if (weekMap != null) {
+            List<Integer> dayList = weekMap.get(day);
+            if (dayList != null) {
+                dayList.add(time);
+                Collections.sort(dayList);
+            }
+        }
     }
 
     /**
@@ -484,12 +486,20 @@ public class KM200SwitchProgramServiceHandler {
         synchronized (switchMap) {
             Map<String, List<Integer>> weekP = switchMap.get(getPositiveSwitch());
             Map<String, List<Integer>> weekN = switchMap.get(getNegativeSwitch());
-            if (weekP.isEmpty() && weekN.isEmpty()) {
+            if (weekP != null && weekN != null) {
+                if (weekP.isEmpty() && weekN.isEmpty()) {
+                    return 0;
+                }
+                List<Integer> daysListP = weekP.get(getActiveDay());
+                List<Integer> daysListN = weekN.get(getActiveDay());
+                if (daysListP != null && daysListN != null) {
+                    return Math.min(daysListP.size(), daysListN.size());
+                } else {
+                    return 0;
+                }
+            } else {
                 return 0;
             }
-            List<Integer> daysListP = weekP.get(getActiveDay());
-            List<Integer> daysListN = weekN.get(getActiveDay());
-            return Math.min(daysListP.size(), daysListN.size());
         }
     }
 
@@ -515,7 +525,7 @@ public class KM200SwitchProgramServiceHandler {
             Map<String, List<Integer>> week = switchMap.get(getPositiveSwitch());
             if (week != null) {
                 List<Integer> 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<String, List<Integer>> week = switchMap.get(getNegativeSwitch());
             if (week != null) {
                 List<Integer> daysList = week.get(getActiveDay());
-                if (!daysList.isEmpty()) {
+                if (daysList != null && !daysList.isEmpty()) {
                     Integer cycl = getActiveCycle();
                     if (cycl <= daysList.size()) {
                         return (daysList.get(getActiveCycle() - 1));