[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); state = new PercentType(convertStatus);
updateState(CHANNEL_DIMMER, state); updateState(CHANNEL_DIMMER, state);
} else { } 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); state = new PercentType(convertStatus);
updateState(CHANNEL_SHUTTER, state); updateState(CHANNEL_SHUTTER, state);
} else { } 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); updateState(CHANNEL_SWITCH, state);
} else { } 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); triggerChannel(CHANNEL_WALLCONTROLLER_ACTION, action);
} }
timeLastEventMs = status.getTs(); 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.Logger;
import org.slf4j.LoggerFactory; 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. * The {@link TouchWandControllerDiscoveryService} Discovery service for Touchwand Controllers.
* *
@ -132,13 +136,15 @@ public class TouchWandControllerDiscoveryService extends AbstractDiscoveryServic
mySocket.receive(datagram); mySocket.receive(datagram);
InetAddress address = datagram.getAddress(); InetAddress address = datagram.getAddress();
String sentence = new String(dgram.getData(), 0, dgram.getLength(), StandardCharsets.US_ASCII); 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(), logger.debug("Received Datagram from {}:{} on Port {} message {}", address.getHostAddress(),
dgram.getPort(), mySocket.getLocalPort(), sentence); dgram.getPort(), mySocket.getLocalPort(), sentence);
} }
} catch (IOException e) { } catch (IOException | JsonSyntaxException e) {
if (!isInterrupted()) { if (!isInterrupted()) {
logger.warn("Error while receiving {}", e.getMessage()); logger.debug("Error while receiving {}", e.getMessage());
} else { } else {
logger.debug("Receiver thread was interrupted {}", e.getMessage()); logger.debug("Receiver thread was interrupted {}", e.getMessage());
} }

View File

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

View File

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

View File

@ -12,21 +12,25 @@
*/ */
package org.openhab.binding.touchwand.internal.dto; package org.openhab.binding.touchwand.internal.dto;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* The {@link TouchWandUnitDataWallController} implements WallController unit * The {@link TouchWandUnitDataWallController} implements WallController unit
* property. * property.
* *
* @author Roie Geron - Initial contribution * @author Roie Geron - Initial contribution
*/ */
@NonNullByDefault
public class TouchWandUnitDataWallController extends TouchWandUnitData { public class TouchWandUnitDataWallController extends TouchWandUnitData {
private CurrStatus currStatus = new CurrStatus(); 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 @Override
public Csc getCurrStatus() { public Csc getCurrStatus() {
if (currStatus == null) {
currStatus = new CurrStatus();
}
return currStatus.getCsc(); return currStatus.getCsc();
} }

View File

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