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