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.util.Collections;
18 import java.util.List;
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;
33 * Handler for the Gateway thing type that Verisure provides.
35 * @author Jan Gustafsson - Initial contribution
39 public class VerisureGatewayThingHandler extends VerisureThingHandler<VerisureGatewayDTO> {
41 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_GATEWAY);
43 public VerisureGatewayThingHandler(Thing thing) {
48 public Class<VerisureGatewayDTO> getVerisureThingClass() {
49 return VerisureGatewayDTO.class;
53 public synchronized void update(VerisureGatewayDTO thing) {
54 updateGatewayState(thing);
55 updateStatus(ThingStatus.ONLINE);
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);
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);
78 updateInstallationChannels(gatewayJSON);
80 logger.debug("Empty communication state list.");
84 public State getValue(String channelId, VerisureGatewayDTO verisureGateway, CommunicationState communicationState) {
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;
99 return UnDefType.UNDEF;
103 public void updateTriggerChannel(String event) {
104 logger.debug("GatewayThingHandler trigger event {}", event);
105 triggerChannel(CHANNEL_GATEWAY_TRIGGER_CHANNEL, event);