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.List;
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;
32 * Handler for the Gateway thing type that Verisure provides.
34 * @author Jan Gustafsson - Initial contribution
38 public class VerisureGatewayThingHandler extends VerisureThingHandler<VerisureGatewayDTO> {
40 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_GATEWAY);
42 public VerisureGatewayThingHandler(Thing thing) {
47 public Class<VerisureGatewayDTO> getVerisureThingClass() {
48 return VerisureGatewayDTO.class;
52 public synchronized void update(VerisureGatewayDTO thing) {
53 updateGatewayState(thing);
54 updateStatus(ThingStatus.ONLINE);
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);
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);
77 updateInstallationChannels(gatewayJSON);
79 logger.debug("Empty communication state list.");
83 public State getValue(String channelId, VerisureGatewayDTO verisureGateway, CommunicationState communicationState) {
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;
98 return UnDefType.UNDEF;
102 public void updateTriggerChannel(String event) {
103 logger.debug("GatewayThingHandler trigger event {}", event);
104 triggerChannel(CHANNEL_GATEWAY_TRIGGER_CHANNEL, event);