Skip to content

Commit

Permalink
[openwebnet] alarm:Adds timestamp feature
Browse files Browse the repository at this point in the history
Signed-off-by: Giovanni Fabiani <[email protected]>
  • Loading branch information
fabgio committed Apr 2, 2023
1 parent 14521bb commit 16f9f08
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
2 changes: 2 additions & 0 deletions bundles/org.openhab.binding.openwebnet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ Switch iAlarm_System_Network "Alarm network" (
String iAlarm_System_Battery "Alarm battery" (gAlarm) { channel="openwebnet:bus_alarm_system:mybridge:Alarm_Sys:battery" }
Switch iAlarm_Zone_3_State "Zone 3 state" (gAlarm) { channel="openwebnet:bus_alarm_zone:mybridge:Alarm_Zone_3:state" }
String iAlarm_Zone_3_Alarm "Zone 3 alarm" (gAlarm) { channel="openwebnet:bus_alarm_zone:mybridge:Alarm_Zone_3:alarm" }
DateTime iAlarm_Zone_3_Timestamp "Zone 3 timestamp" (gAlarm) { channel="openwebnet:bus_alarm_zone:mybridge:Alarm_Zone_3:timestamp" }
```

Example items linked to OpenWebNet Zigbee devices:
Expand Down Expand Up @@ -454,6 +455,7 @@ sitemap openwebnet label="OpenWebNet Binding Example Sitemap"
Default item=iAlarm_System_Battery label="Battery" icon="battery"
Switch item=iAlarm_Zone_3_State label="Zone 3 state"
Default item=iAlarm_Zone_3_Alarm label="Zone 3 alarm" icon="siren"
Default item=iAlarm_Zone_3_Timestamp label="Zone 3 timestamp"
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public class OpenWebNetBindingConstants {
public static final String CHANNEL_ALARM_SYSTEM_BATTERY = "battery";
public static final String CHANNEL_ALARM_ZONE_STATE = "state";
public static final String CHANNEL_ALARM_ZONE_ALARM = "alarm";
public static final String CHANNEL_ALARM_ZONE_TIMESTAMP = "timestamp";

// devices config properties
public static final String CONFIG_PROPERTY_WHERE = "where";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@

import static org.openhab.binding.openwebnet.internal.OpenWebNetBindingConstants.*;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.openwebnet.internal.OpenWebNetBindingConstants;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.ChannelUID;
Expand All @@ -43,6 +47,8 @@
* {@link OpenWebNetThingHandler}.
*
* @author Massimo Valla - Initial contribution
* @author Giovanni Fabiani - further improvements
*
*/
@NonNullByDefault
public class OpenWebNetAlarmHandler extends OpenWebNetThingHandler {
Expand Down Expand Up @@ -206,6 +212,9 @@ private void updateZone(Alarm msg) {
logger.debug("Alarm.updateZone() WHAT is null. Frame={}", msg);
return;
}

String timeStamp = String.valueOf(getThing().getChannel("timestamp"));

switch (w) {
case ZONE_DISENGAGED:
case ZONE_ENGAGED:
Expand All @@ -217,6 +226,7 @@ private void updateZone(Alarm msg) {
case ZONE_ALARM_SILENT:
case ZONE_ALARM_TECHNICAL:
case ZONE_ALARM_TECHNICAL_RESET:
updateTimeStamp(timeStamp);
updateZoneAlarm(w);
break;
default:
Expand Down Expand Up @@ -254,6 +264,31 @@ private void updateZoneAlarm(WhatAlarm w) {
}
}

protected void updateTimeStamp(@Nullable String lastUpdatedTimeStamp) {
updateTimeStamp(lastUpdatedTimeStamp, CHANNEL_ALARM_ZONE_TIMESTAMP);
}

protected void updateTimeStamp(@Nullable String lastUpdatedTimeStamp, ChannelUID cuid) {
if (lastUpdatedTimeStamp != null) {
try {
logger.trace("Parsing date {} for channel {}", lastUpdatedTimeStamp, cuid);
ZonedDateTime zdt = ZonedDateTime.parse(lastUpdatedTimeStamp);
ZonedDateTime zdtLocal = zdt.withZoneSameInstant(ZoneId.systemDefault());
logger.trace("Parsing datetime successful. Using date. {}", new DateTimeType(zdtLocal));
updateState(cuid, new DateTimeType(zdtLocal));
} catch (IllegalArgumentException e) {
logger.warn("Parsing date failed: {}.", e.getMessage(), e);
}
} else {
logger.debug("Timestamp is null!");
}
}

protected void updateTimeStamp(@Nullable String lastUpdatedTimeStamp, String channel) {
ChannelUID cuid = new ChannelUID(getThing().getUID(), channel);
updateTimeStamp(lastUpdatedTimeStamp, cuid);
}

private void resetAllZonesAlarmState() {
for (OpenWebNetAlarmHandler h : zoneHandlers) {
h.updateState(CHANNEL_ALARM_ZONE_ALARM, new StringType(ALARM_NONE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
<channels>
<!-- read only -->
<channel id="state" typeId="alarmZoneState"/>
<channel id="alarm" typeId="zoneAlarm"/>
<channel id="alarm" typeId="AlarmZoneState"/>
<channel id="timestamp" typeId="AlarmZoneState"/>
</channels>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,12 @@
<item-type>Switch</item-type>
<label>Alarm Zone State</label>
<description>Alarm zone is active (ON) or inactive (OFF) (read only).</description>
</channel-type>

<channel-type id="timestamp">
<item-type>DateTime</item-type>
<label>Alarm Time stamp</label>
<description>Alarm zone timestamp (read only)</description>
<state readOnly="true"/>
</channel-type>
</thing:thing-descriptions>

0 comments on commit 16f9f08

Please sign in to comment.