2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.verisure.internal.handler;
15 import static org.openhab.binding.verisure.internal.VerisureBindingConstants.*;
17 import java.math.BigDecimal;
18 import java.math.BigInteger;
19 import java.time.ZonedDateTime;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
24 import java.util.stream.Collectors;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.openhab.binding.verisure.internal.VerisureSession;
28 import org.openhab.binding.verisure.internal.VerisureThingConfiguration;
29 import org.openhab.binding.verisure.internal.dto.VerisureBaseThingDTO.Device;
30 import org.openhab.binding.verisure.internal.dto.VerisureEventLogDTO;
31 import org.openhab.binding.verisure.internal.dto.VerisureEventLogDTO.EventLog;
32 import org.openhab.binding.verisure.internal.dto.VerisureEventLogDTO.PagedList;
33 import org.openhab.core.library.types.DecimalType;
34 import org.openhab.core.library.types.StringType;
35 import org.openhab.core.thing.Channel;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingStatus;
38 import org.openhab.core.thing.ThingTypeUID;
39 import org.openhab.core.types.State;
40 import org.openhab.core.types.UnDefType;
43 * Handler for the Event Log thing type that Verisure provides.
45 * @author Jan Gustafsson - Initial contribution
49 public class VerisureEventLogThingHandler extends VerisureThingHandler<VerisureEventLogDTO> {
51 private BigDecimal lastEventId = BigDecimal.ZERO;
52 private long lastEventTime = 0;
54 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_EVENT_LOG);
56 public VerisureEventLogThingHandler(Thing thing) {
61 public Class<VerisureEventLogDTO> getVerisureThingClass() {
62 return VerisureEventLogDTO.class;
66 public synchronized void update(VerisureEventLogDTO thing) {
67 updateEventLogState(thing);
68 updateStatus(ThingStatus.ONLINE);
72 public void initialize() {
73 logger.debug("initialize on thing: {}", thing);
74 VerisureSession session = getSession();
75 config = getConfigAs(VerisureThingConfiguration.class);
76 if (session != null) {
77 logger.debug("Set number of events to fetch from API to {} for thing {}", config.getNumberOfEvents(),
79 session.setNumberOfEvents(config.getNumberOfEvents());
84 private void updateEventLogState(VerisureEventLogDTO eventLogJSON) {
85 EventLog eventLog = eventLogJSON.getData().getInstallation().getEventLog();
86 if (!eventLog.getPagedList().isEmpty()) {
87 getThing().getChannels().stream().map(Channel::getUID).filter(channelUID -> isLinked(channelUID))
88 .forEach(channelUID -> {
89 State state = getValue(channelUID.getId(), eventLogJSON, eventLog);
90 updateState(channelUID, state);
92 updateInstallationChannels(eventLogJSON);
93 String eventTime = eventLogJSON.getData().getInstallation().getEventLog().getPagedList().get(0)
95 if (eventTime != null) {
96 updateTimeStamp(eventTime, CHANNEL_LAST_EVENT_TIME);
97 lastEventTime = ZonedDateTime.parse(eventTime).toEpochSecond();
100 logger.debug("Empty event log.");
104 public State getValue(String channelId, VerisureEventLogDTO verisureEventLog, EventLog eventLog) {
105 Device device = eventLog.getPagedList().get(0).getDevice();
108 case CHANNEL_LAST_EVENT_LOCATION:
109 return device != null && device.getArea() != null ? new StringType(device.getArea()) : UnDefType.NULL;
110 case CHANNEL_LAST_EVENT_DEVICE_ID:
111 return device != null && device.getDeviceLabel() != null ? new StringType(device.getDeviceLabel())
113 case CHANNEL_LAST_EVENT_ID:
114 String eventId = eventLog.getPagedList().get(0).getEventId();
115 if (eventId != null) {
116 if (eventId.contains("-")) {
117 eventId = eventId.replace("-", "");
118 lastEventId = new BigDecimal(new BigInteger(eventId, 16));
120 lastEventId = new BigDecimal(eventId);
122 return new DecimalType(lastEventId);
124 return UnDefType.NULL;
126 case CHANNEL_LAST_EVENT_TIME:
127 if (lastEventTime != 0) {
128 triggerEventChannels(eventLog);
130 case CHANNEL_LAST_EVENT_DEVICE_TYPE:
131 return device != null && device.getGui().getLabel() != null ? new StringType(device.getGui().getLabel())
133 case CHANNEL_LAST_EVENT_TYPE:
134 String lastEventType = eventLog.getPagedList().get(0).getEventType();
135 return lastEventType != null ? new StringType(lastEventType) : UnDefType.NULL;
136 case CHANNEL_LAST_EVENT_CATEGORY:
137 String lastEventCategory = eventLog.getPagedList().get(0).getEventCategory();
138 return lastEventCategory != null ? new StringType(lastEventCategory) : UnDefType.NULL;
139 case CHANNEL_LAST_EVENT_USER_NAME:
140 String lastEventUserName = eventLog.getPagedList().get(0).getUserName();
141 return lastEventUserName != null ? new StringType(lastEventUserName) : UnDefType.NULL;
142 case CHANNEL_EVENT_LOG:
143 String eventLogJSON = gson.toJson(eventLog);
144 return eventLogJSON != null ? new StringType(eventLogJSON) : UnDefType.NULL;
146 return UnDefType.UNDEF;
149 private void triggerEventChannels(EventLog eventLog) {
150 List<PagedList> newEventList = eventLog.getPagedList().stream().collect(Collectors.toList());
151 Collections.reverse(newEventList);
152 ArrayList<Event> events = new ArrayList<>();
153 for (PagedList newEvent : newEventList) {
154 String eventTimeString = newEvent.getEventTime();
155 if (eventTimeString == null) {
156 logger.debug("Event-Time is null: {}", newEvent);
159 long eventTime = ZonedDateTime.parse(eventTimeString).toEpochSecond();
160 logger.trace("Event time: {} Last Event time: {}", eventTime, lastEventTime);
161 if (eventTime > lastEventTime) {
162 logger.debug("Create event {} for event time {}", newEvent.getEventType(), eventTime);
164 Device device = newEvent.getDevice();
165 if (device != null) {
166 event = new Event(device.getDeviceLabel(), newEvent.getEventType(), newEvent.getEventCategory());
168 event = new Event("NA", newEvent.getEventType(), newEvent.getEventCategory());
173 updateTriggerChannel(events);
177 public void updateTriggerChannel(String event) {