]> git.basschouten.com Git - openhab-addons.git/blob
c29955ab9299fa1b7632538d8a69bcac640af76d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.verisure.internal.handler;
14
15 import static org.openhab.binding.verisure.internal.VerisureBindingConstants.*;
16
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;
23 import java.util.Set;
24 import java.util.stream.Collectors;
25
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;
41
42 /**
43  * Handler for the Event Log thing type that Verisure provides.
44  *
45  * @author Jan Gustafsson - Initial contribution
46  *
47  */
48 @NonNullByDefault
49 public class VerisureEventLogThingHandler extends VerisureThingHandler<VerisureEventLogDTO> {
50
51     private BigDecimal lastEventId = BigDecimal.ZERO;
52     private long lastEventTime = 0;
53
54     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_EVENT_LOG);
55
56     public VerisureEventLogThingHandler(Thing thing) {
57         super(thing);
58     }
59
60     @Override
61     public Class<VerisureEventLogDTO> getVerisureThingClass() {
62         return VerisureEventLogDTO.class;
63     }
64
65     @Override
66     public synchronized void update(VerisureEventLogDTO thing) {
67         updateEventLogState(thing);
68         updateStatus(ThingStatus.ONLINE);
69     }
70
71     @Override
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(),
78                     thing);
79             session.setNumberOfEvents(config.getNumberOfEvents());
80         }
81         super.initialize();
82     }
83
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);
91                     });
92             updateInstallationChannels(eventLogJSON);
93             String eventTime = eventLogJSON.getData().getInstallation().getEventLog().getPagedList().get(0)
94                     .getEventTime();
95             if (eventTime != null) {
96                 updateTimeStamp(eventTime, CHANNEL_LAST_EVENT_TIME);
97                 lastEventTime = ZonedDateTime.parse(eventTime).toEpochSecond();
98             }
99         } else {
100             logger.debug("Empty event log.");
101         }
102     }
103
104     public State getValue(String channelId, VerisureEventLogDTO verisureEventLog, EventLog eventLog) {
105         Device device = eventLog.getPagedList().get(0).getDevice();
106
107         switch (channelId) {
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())
112                         : UnDefType.NULL;
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));
119                     } else {
120                         lastEventId = new BigDecimal(eventId);
121                     }
122                     return new DecimalType(lastEventId);
123                 } else {
124                     return UnDefType.NULL;
125                 }
126             case CHANNEL_LAST_EVENT_TIME:
127                 if (lastEventTime != 0) {
128                     triggerEventChannels(eventLog);
129                 }
130             case CHANNEL_LAST_EVENT_DEVICE_TYPE:
131                 return device != null && device.getGui().getLabel() != null ? new StringType(device.getGui().getLabel())
132                         : UnDefType.NULL;
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;
145         }
146         return UnDefType.UNDEF;
147     }
148
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);
157                 continue;
158             }
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);
163                 Event event;
164                 Device device = newEvent.getDevice();
165                 if (device != null) {
166                     event = new Event(device.getDeviceLabel(), newEvent.getEventType(), newEvent.getEventCategory());
167                 } else {
168                     event = new Event("NA", newEvent.getEventType(), newEvent.getEventCategory());
169                 }
170                 events.add(event);
171             }
172         }
173         updateTriggerChannel(events);
174     }
175
176     @Override
177     public void updateTriggerChannel(String event) {
178     }
179 }