[km200] Added JCE check and removed some warnings (#11841)

* Added a check if JCE is installed
* removed warnings

Signed-off-by: Markus Eckhardt <github@familie-eckhardt.eu>
This commit is contained in:
Markus Eckhardt 2022-01-17 18:47:38 +01:00 committed by GitHub
parent e44dfe7dbc
commit 94401fc798
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 94 additions and 61 deletions

View 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 {

View 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());
}
}

View 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);
}
}
}

View 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());
}
}
}
}

View 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));