[thing] Fix removal of thing properties (single and bulk) (#2735)
* fix removal of thing properties (single and bulk) Signed-off-by: Jan N. Klug <github@klug.nrw>pull/2741/head
parent
ea6f21f74d
commit
d6f63e7500
|
@ -484,17 +484,37 @@ public abstract class BaseThingHandler implements ThingHandler {
|
|||
* Informs the framework, that the given properties map of the thing was updated. This method performs a check, if
|
||||
* the properties were updated. If the properties did not change, the framework is not informed about changes.
|
||||
*
|
||||
* @param properties properties map, that was updated and should be persisted
|
||||
* @param properties properties map, that was updated and should be persisted (all properties cleared if null)
|
||||
*/
|
||||
protected void updateProperties(Map<String, String> properties) {
|
||||
protected void updateProperties(@Nullable Map<String, String> properties) {
|
||||
if (properties == null) {
|
||||
updatePropertiesInternal(null);
|
||||
} else {
|
||||
Map<String, @Nullable String> updatedPropertiesMap = new HashMap<>(properties);
|
||||
updatePropertiesInternal(updatedPropertiesMap);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Informs the framework, that the given properties map of the thing was updated. This method performs a check, if
|
||||
* the properties were updated. If the properties did not change, the framework is not informed about changes.
|
||||
*
|
||||
* @param properties properties map, that was updated and should be persisted (all properties cleared if null)
|
||||
*/
|
||||
private void updatePropertiesInternal(@Nullable Map<String, @Nullable String> properties) {
|
||||
boolean propertiesUpdated = false;
|
||||
for (Entry<String, String> property : properties.entrySet()) {
|
||||
String propertyName = property.getKey();
|
||||
String propertyValue = property.getValue();
|
||||
String existingPropertyValue = thing.getProperties().get(propertyName);
|
||||
if (existingPropertyValue == null || !existingPropertyValue.equals(propertyValue)) {
|
||||
this.thing.setProperty(propertyName, propertyValue);
|
||||
propertiesUpdated = true;
|
||||
if (properties == null && !this.thing.getProperties().isEmpty()) {
|
||||
this.thing.setProperties(Map.of());
|
||||
propertiesUpdated = true;
|
||||
} else if (properties != null) {
|
||||
for (Entry<String, @Nullable String> property : properties.entrySet()) {
|
||||
String propertyName = property.getKey();
|
||||
String propertyValue = property.getValue();
|
||||
String existingPropertyValue = thing.getProperties().get(propertyName);
|
||||
if (existingPropertyValue == null || !existingPropertyValue.equals(propertyValue)) {
|
||||
this.thing.setProperty(propertyName, propertyValue);
|
||||
propertiesUpdated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (propertiesUpdated) {
|
||||
|
@ -523,8 +543,8 @@ public abstract class BaseThingHandler implements ThingHandler {
|
|||
* @param name the name of the property to be set
|
||||
* @param value the value of the property
|
||||
*/
|
||||
protected void updateProperty(String name, String value) {
|
||||
updateProperties(Collections.singletonMap(name, value));
|
||||
protected void updateProperty(String name, @Nullable String value) {
|
||||
updatePropertiesInternal(Collections.singletonMap(name, value));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -139,7 +139,7 @@ public class BindingBaseClassesOSGiTest extends JavaOSGiTest {
|
|||
}
|
||||
}
|
||||
|
||||
class SimpleThingHandler extends BaseThingHandler {
|
||||
static class SimpleThingHandler extends BaseThingHandler {
|
||||
|
||||
SimpleThingHandler(Thing thing) {
|
||||
super(thing);
|
||||
|
@ -245,7 +245,7 @@ public class BindingBaseClassesOSGiTest extends JavaOSGiTest {
|
|||
thingHandlerFactory.deactivate(componentContext);
|
||||
}
|
||||
|
||||
class ConfigStatusInfoEventSubscriber implements EventSubscriber {
|
||||
static class ConfigStatusInfoEventSubscriber implements EventSubscriber {
|
||||
private final ThingUID thingUID;
|
||||
private Event receivedEvent;
|
||||
|
||||
|
@ -368,7 +368,7 @@ public class BindingBaseClassesOSGiTest extends JavaOSGiTest {
|
|||
}, 4000, DFL_SLEEP_TIME);
|
||||
}
|
||||
|
||||
class ConfigStatusProviderThingHandlerFactory extends BaseThingHandlerFactory {
|
||||
static class ConfigStatusProviderThingHandlerFactory extends BaseThingHandlerFactory {
|
||||
|
||||
@Override
|
||||
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
|
||||
|
@ -425,7 +425,7 @@ public class BindingBaseClassesOSGiTest extends JavaOSGiTest {
|
|||
}
|
||||
}
|
||||
|
||||
class YetAnotherThingHandler extends BaseThingHandler {
|
||||
static class YetAnotherThingHandler extends BaseThingHandler {
|
||||
|
||||
YetAnotherThingHandler(Thing thing) {
|
||||
super(thing);
|
||||
|
@ -449,19 +449,9 @@ public class BindingBaseClassesOSGiTest extends JavaOSGiTest {
|
|||
configuration.put("key", "value");
|
||||
updateConfiguration(configuration);
|
||||
}
|
||||
|
||||
public void updateProperties(String value) {
|
||||
Map<String, String> properties = editProperties();
|
||||
properties.put(Thing.PROPERTY_MODEL_ID, value);
|
||||
updateProperties(properties);
|
||||
}
|
||||
|
||||
public void updateProperty(String value) {
|
||||
updateProperty(Thing.PROPERTY_VENDOR, value);
|
||||
}
|
||||
}
|
||||
|
||||
class ThingRegistryChangeListener implements RegistryChangeListener<Thing> {
|
||||
static class ThingRegistryChangeListener implements RegistryChangeListener<Thing> {
|
||||
private boolean updated;
|
||||
private Thing thing;
|
||||
|
||||
|
@ -540,23 +530,27 @@ public class BindingBaseClassesOSGiTest extends JavaOSGiTest {
|
|||
|
||||
// set properties
|
||||
String modelId = "1234";
|
||||
((YetAnotherThingHandler) listener.getThing().getHandler()).updateProperties(modelId);
|
||||
String firmwareVersion = "1.2.3";
|
||||
((YetAnotherThingHandler) listener.getThing().getHandler()).updateProperties(
|
||||
Map.of(Thing.PROPERTY_MODEL_ID, modelId, Thing.PROPERTY_FIRMWARE_VERSION, firmwareVersion));
|
||||
|
||||
assertThat(listener.getThing().getProperties().get(Thing.PROPERTY_MODEL_ID), is(modelId));
|
||||
assertThat(listener.getThing().getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION), is(firmwareVersion));
|
||||
|
||||
String vendor = "vendor";
|
||||
((YetAnotherThingHandler) listener.getThing().getHandler()).updateProperty(vendor);
|
||||
((YetAnotherThingHandler) listener.getThing().getHandler()).updateProperty(Thing.PROPERTY_VENDOR, vendor);
|
||||
|
||||
assertThat(listener.getThing().getProperties().get(Thing.PROPERTY_VENDOR), is(vendor));
|
||||
|
||||
// unset properties
|
||||
((YetAnotherThingHandler) listener.getThing().getHandler()).updateProperties((String) null);
|
||||
// unset single property
|
||||
((YetAnotherThingHandler) listener.getThing().getHandler()).updateProperty(Thing.PROPERTY_MODEL_ID, null);
|
||||
|
||||
assertThat(listener.getThing().getProperties().get(Thing.PROPERTY_MODEL_ID), is(nullValue()));
|
||||
|
||||
((YetAnotherThingHandler) listener.getThing().getHandler()).updateProperty(null);
|
||||
// unset all properties
|
||||
((YetAnotherThingHandler) listener.getThing().getHandler()).updateProperties(null);
|
||||
|
||||
assertThat(listener.getThing().getProperties().get(Thing.PROPERTY_VENDOR), is(nullValue()));
|
||||
assertTrue(listener.getThing().getProperties().isEmpty());
|
||||
} finally {
|
||||
thingRegistry.removeRegistryChangeListener(listener);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue