]> git.basschouten.com Git - openhab-addons.git/blob
a4e44caa6cca2975db6e0d27f5b75efbb43f825c
[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.util.List;
18 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.verisure.internal.dto.VerisureGatewayDTO;
22 import org.openhab.binding.verisure.internal.dto.VerisureGatewayDTO.CommunicationState;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.thing.Channel;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.types.State;
29 import org.openhab.core.types.UnDefType;
30
31 /**
32  * Handler for the Gateway thing type that Verisure provides.
33  *
34  * @author Jan Gustafsson - Initial contribution
35  *
36  */
37 @NonNullByDefault
38 public class VerisureGatewayThingHandler extends VerisureThingHandler<VerisureGatewayDTO> {
39
40     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_GATEWAY);
41
42     public VerisureGatewayThingHandler(Thing thing) {
43         super(thing);
44     }
45
46     @Override
47     public Class<VerisureGatewayDTO> getVerisureThingClass() {
48         return VerisureGatewayDTO.class;
49     }
50
51     @Override
52     public synchronized void update(VerisureGatewayDTO thing) {
53         updateGatewayState(thing);
54         updateStatus(ThingStatus.ONLINE);
55     }
56
57     private void updateGatewayState(VerisureGatewayDTO gatewayJSON) {
58         List<CommunicationState> communicationStateList = gatewayJSON.getData().getInstallation()
59                 .getCommunicationState();
60         if (!communicationStateList.isEmpty()) {
61             communicationStateList.forEach(communicationState -> {
62                 getThing().getChannels().stream().map(Channel::getUID).filter(channelUID -> isLinked(channelUID))
63                         .forEach(channelUID -> {
64                             if (!channelUID.getId().contains("testTime")) {
65                                 State state = getValue(channelUID.getId(), gatewayJSON, communicationState);
66                                 updateState(channelUID, state);
67                             } else {
68                                 String timestamp = communicationState.getTestDate();
69                                 String carrierType = communicationState.getHardwareCarrierType();
70                                 if (timestamp != null && carrierType != null
71                                         && channelUID.toString().contains(carrierType)) {
72                                     updateTimeStamp(timestamp, channelUID);
73                                 }
74                             }
75                         });
76             });
77             updateInstallationChannels(gatewayJSON);
78         } else {
79             logger.debug("Empty communication state list.");
80         }
81     }
82
83     public State getValue(String channelId, VerisureGatewayDTO verisureGateway, CommunicationState communicationState) {
84         switch (channelId) {
85             case CHANNEL_STATUS_GSM_OVER_UDP:
86             case CHANNEL_STATUS_GSM_OVER_SMS:
87             case CHANNEL_STATUS_GPRS_OVER_UDP:
88             case CHANNEL_STATUS_ETH_OVER_UDP:
89                 String state = communicationState.getResult();
90                 return state != null ? new StringType(state) : UnDefType.NULL;
91             case CHANNEL_GATEWAY_MODEL:
92                 String model = communicationState.getDevice().getGui().getLabel();
93                 return model != null ? new StringType(model) : UnDefType.NULL;
94             case CHANNEL_LOCATION:
95                 String location = communicationState.getDevice().getArea();
96                 return location != null ? new StringType(location) : UnDefType.NULL;
97         }
98         return UnDefType.UNDEF;
99     }
100
101     @Override
102     public void updateTriggerChannel(String event) {
103         logger.debug("GatewayThingHandler trigger event {}", event);
104         triggerChannel(CHANNEL_GATEWAY_TRIGGER_CHANNEL, event);
105     }
106 }