[touchwand] - better handle unit update when currStatus key is null (#10520)

* notify listeners on status change using discovery 


Signed-off-by: Roie Geron <roie.geron@gmail.com>

* adapt bridge discovery name to new string broadcast

Signed-off-by: Roie Geron <roie.geron@gmail.com>

* check correctly if current status key is not null

Signed-off-by: Roie Geron <roie.geron@gmail.com>

* change log from warn to debug

Signed-off-by: Roie Geron <roie.geron@gmail.com>

* better handle when currStatus is null

also move logs from warn to debug

Signed-off-by: Roie Geron <roie.geron@gmail.com>

* remove nonNullByDefault annotation

as dto can be null 

Signed-off-by: Roie Geron <roie.geron@gmail.com>
This commit is contained in:
Roie Geron 2021-04-17 16:42:37 +03:00 committed by GitHub
parent 265fd30ba1
commit 8258d9d722
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 24 additions and 17 deletions

View File

@ -56,7 +56,7 @@ public class TouchWandDimmerHandler extends TouchWandBaseUnitHandler {
state = new PercentType(convertStatus);
updateState(CHANNEL_DIMMER, state);
} else {
logger.warn("updateTouchWandUnitState incompatible TouchWandUnitData instance");
logger.debug("updateTouchWandUnitState incompatible TouchWandUnitData instance");
}
}
}

View File

@ -66,7 +66,7 @@ public class TouchWandShutterHandler extends TouchWandBaseUnitHandler {
state = new PercentType(convertStatus);
updateState(CHANNEL_SHUTTER, state);
} else {
logger.warn("updateTouchWandUnitState incompatible TouchWandUnitData instance");
logger.debug("updateTouchWandUnitState incompatible TouchWandUnitData instance");
}
}
}

View File

@ -52,7 +52,7 @@ public class TouchWandSwitchHandler extends TouchWandBaseUnitHandler {
}
updateState(CHANNEL_SWITCH, state);
} else {
logger.warn("updateTouchWandUnitState incompatible TouchWandUnitData instance");
logger.debug("updateTouchWandUnitState incompatible TouchWandUnitData instance");
}
}

View File

@ -58,6 +58,8 @@ public class TouchWandWallControllerHandler extends TouchWandBaseUnitHandler {
triggerChannel(CHANNEL_WALLCONTROLLER_ACTION, action);
}
timeLastEventMs = status.getTs();
} else {
logger.debug("updateTouchWandUnitState incompatible TouchWandUnitData instance");
}
}
}

View File

@ -35,6 +35,10 @@ import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
/**
* The {@link TouchWandControllerDiscoveryService} Discovery service for Touchwand Controllers.
*
@ -132,13 +136,15 @@ public class TouchWandControllerDiscoveryService extends AbstractDiscoveryServic
mySocket.receive(datagram);
InetAddress address = datagram.getAddress();
String sentence = new String(dgram.getData(), 0, dgram.getLength(), StandardCharsets.US_ASCII);
addDeviceDiscoveryResult(sentence, address.getHostAddress().toString());
JsonObject bridge = JsonParser.parseString(sentence).getAsJsonObject();//
String name = bridge.get("name").getAsString();
addDeviceDiscoveryResult(name, address.getHostAddress().toString());
logger.debug("Received Datagram from {}:{} on Port {} message {}", address.getHostAddress(),
dgram.getPort(), mySocket.getLocalPort(), sentence);
}
} catch (IOException e) {
} catch (IOException | JsonSyntaxException e) {
if (!isInterrupted()) {
logger.warn("Error while receiving {}", e.getMessage());
logger.debug("Error while receiving {}", e.getMessage());
} else {
logger.debug("Receiver thread was interrupted {}", e.getMessage());
}

View File

@ -71,7 +71,7 @@ public class TouchWandUnitDiscoveryService extends AbstractDiscoveryService
@Override
protected void startScan() {
if (touchWandBridgeHandler.getThing().getStatus() != ThingStatus.ONLINE) {
logger.warn("Could not scan units while bridge offline");
logger.debug("Could not scan units while bridge offline");
return;
}
@ -105,11 +105,9 @@ public class TouchWandUnitDiscoveryService extends AbstractDiscoveryService
break;
case TYPE_SWITCH:
addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_SWITCH);
notifyListeners(touchWandUnit);
break;
case TYPE_DIMMER:
addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_DIMMER);
notifyListeners(touchWandUnit);
break;
case TYPE_SHUTTER:
addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_SHUTTER);
@ -120,6 +118,7 @@ public class TouchWandUnitDiscoveryService extends AbstractDiscoveryService
default:
continue;
}
notifyListeners(touchWandUnit);
}
} catch (JsonSyntaxException e) {
logger.warn("Could not parse unit {}", e.getMessage());

View File

@ -22,7 +22,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
@NonNullByDefault
public class TouchWandShutterSwitchUnitData extends TouchWandUnitData {
private Integer currStatus = 0;
private int currStatus = 0;
@Override
public Integer getCurrStatus() {

View File

@ -12,21 +12,25 @@
*/
package org.openhab.binding.touchwand.internal.dto;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* The {@link TouchWandUnitDataWallController} implements WallController unit
* property.
*
* @author Roie Geron - Initial contribution
*/
@NonNullByDefault
public class TouchWandUnitDataWallController extends TouchWandUnitData {
private CurrStatus currStatus = new CurrStatus();
// currStatus can be null since the object is created by gson fromJson
// in case the key is null or not exist , the variable will be null.
// if this is the case , default status is created
@Override
public Csc getCurrStatus() {
if (currStatus == null) {
currStatus = new CurrStatus();
}
return currStatus.getCsc();
}

View File

@ -47,10 +47,6 @@ public class TouchWandUnitFromJson {
type = TYPE_UNKNOWN;
}
if (!jsonUnit.has("currStatus") || (jsonUnit.get("currStatus") == null)) {
type = TYPE_UNKNOWN;
}
switch (type) {
case TYPE_WALLCONTROLLER:
touchWandUnit = gson.fromJson(jsonUnit, TouchWandUnitDataWallController.class);