Skip to content

Commit

Permalink
Added motion detector thing
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-kaestle committed Apr 4, 2020
1 parent 5751aeb commit 9cbcde4
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 1 deletion.
Expand Up @@ -32,6 +32,7 @@ public class BoschSHCBindingConstants {
public static final ThingTypeUID THING_TYPE_INWALL_SWITCH = new ThingTypeUID(BINDING_ID, "in-wall-switch");
public static final ThingTypeUID THING_TYPE_TWINGUARD = new ThingTypeUID(BINDING_ID, "twinguard");
public static final ThingTypeUID THING_TYPE_WINDOW_CONTACT = new ThingTypeUID(BINDING_ID, "window-contact");
public static final ThingTypeUID THING_TYPE_MOTION_DETECTOR = new ThingTypeUID(BINDING_ID, "motion-detector");

// List of all Channel IDs
// Auto-generated from thing-types.xml via script, don't modify
Expand All @@ -47,5 +48,6 @@ public class BoschSHCBindingConstants {
public static final String CHANNEL_PURITY_RATING = "purity-rating";
public static final String CHANNEL_COMBINED_RATING = "combined-rating";
public static final String CHANNEL_CONTACT = "contact";
public static final String CHANNEL_LATEST_MOTION = "latest-motion";

}
Expand Up @@ -46,7 +46,7 @@ public class BoschSHCHandlerFactory extends BaseThingHandlerFactory {

// List of all supported Bosch devices.
public static final Collection<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Arrays.asList(THING_TYPE_SHC,
THING_TYPE_INWALL_SWITCH, THING_TYPE_TWINGUARD, THING_TYPE_WINDOW_CONTACT);
THING_TYPE_INWALL_SWITCH, THING_TYPE_TWINGUARD, THING_TYPE_WINDOW_CONTACT, THING_TYPE_MOTION_DETECTOR);

@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
Expand Down Expand Up @@ -77,6 +77,10 @@ else if (THING_TYPE_WINDOW_CONTACT.equals(thingTypeUID)) {
return new WindowContactHandler(thing);
}

else if (THING_TYPE_MOTION_DETECTOR.equals(thingTypeUID)) {
return new MotionDetectorHandler(thing);
}

else {
logger.warn("Failed to find handler for device: {}", thingTypeUID);
}
Expand Down
@@ -0,0 +1,29 @@
package org.openhab.binding.boschshc.internal;

import com.google.gson.annotations.SerializedName;

/**
* {
* "result": [
* {
* "path": "/devices/hdm:ZigBee:000d6f0004b95a62/services/LatestMotion",
* "@type": "DeviceServiceData",
* "id": "LatestMotion",
* "state": {
* "latestMotionDetected": "2020-04-03T19:02:19.054Z",
* "@type": "latestMotionState"
* },
* "deviceId": "hdm:ZigBee:000d6f0004b95a62"
* }
* ],
* "jsonrpc": "2.0"
* }
*
*/
public class LatestMotionState {

@SerializedName("@type")
String type;

String latestMotionDetected;
}
@@ -0,0 +1,73 @@
package org.openhab.binding.boschshc.internal;

import static org.openhab.binding.boschshc.internal.BoschSHCBindingConstants.CHANNEL_LATEST_MOTION;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.smarthome.core.library.types.DateTimeType;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.ThingStatusDetail;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.RefreshType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonSyntaxException;

public class MotionDetectorHandler extends BoschSHCHandler {
private final Logger logger = LoggerFactory.getLogger(BoschSHCHandler.class);

public MotionDetectorHandler(Thing thing) {
super(thing);
logger.warn("Creating motion detector thing: {}", thing.getLabel());
}

@Override
public void handleCommand(ChannelUID channelUID, Command command) {

BoschSHCConfiguration config = super.getBoschConfig();
Bridge bridge = this.getBridge();

if (bridge != null && config != null) {

logger.info("Handle command for: {} - {}", config.id, command);
BoschSHCBridgeHandler bridgeHandler = (BoschSHCBridgeHandler) bridge.getHandler();

if (bridgeHandler != null) {

if (CHANNEL_LATEST_MOTION.equals(channelUID.getId())) {
if (command instanceof RefreshType) {

// Refresh the temperature from the Bosch Twinguard device.
// Might not be necessary, can just wait until we get one
logger.warn("Refreshing the temperature is not yet supported.");
}
// Otherwise: not action supported here.
}
}
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Bridge or config is NUL");
}
}

@Override
public void processUpdate(String id, @NonNull JsonElement state) {
logger.warn("Motion detector: received update: {} {}", id, state);
Gson gson = new Gson();

try {
LatestMotionState parsed = gson.fromJson(state, LatestMotionState.class);

DateTimeType date = new DateTimeType(parsed.latestMotionDetected);
logger.warn("Parsed date of latest motion to {}: {} as date {}", this.getBoschID(), parsed, date);
updateState(CHANNEL_LATEST_MOTION, date);

} catch (JsonSyntaxException e) {
logger.warn("Received unknown update in in-wall switch: {}", state);
}
}
}
Expand Up @@ -98,6 +98,24 @@
</parameter>
</config-description>

</thing-type>

<thing-type id="motion-detector">
<label>Motion Detector</label>
<description>Bosch Motion Detector</description>

<channels>
<channel id="latest-motion" typeId="latest-motion" />
</channels>

<config-description>
<parameter name="id" type="text" required="true">
<label>Device ID</label>
<description>Device ID of the contact</description>
<required>true</required>
</parameter>
</config-description>

</thing-type>

<!-- https://www.eclipse.org/smarthome/documentation/concepts/items.html -->
Expand Down Expand Up @@ -173,4 +191,10 @@
<description>A Bosch window and door contact.</description>
</channel-type>

<channel-type id="latest-motion">
<item-type>DateTime</item-type>
<label>Latest motion</label>
<description>Timestamp of the latest motion.</description>
</channel-type>

</thing:thing-descriptions>

0 comments on commit 9cbcde4

Please sign in to comment.