]> git.basschouten.com Git - openhab-addons.git/blob
d533c1e558c4abd491e9c3f2032349e58bbc33bb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.enocean.internal.handler;
14
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
16
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.Comparator;
20 import java.util.Hashtable;
21 import java.util.Set;
22 import java.util.function.Predicate;
23
24 import org.openhab.binding.enocean.internal.config.EnOceanBaseConfig;
25 import org.openhab.binding.enocean.internal.eep.EEP;
26 import org.openhab.binding.enocean.internal.eep.EEPFactory;
27 import org.openhab.binding.enocean.internal.eep.EEPType;
28 import org.openhab.binding.enocean.internal.messages.BasePacket;
29 import org.openhab.binding.enocean.internal.messages.ERP1Message;
30 import org.openhab.binding.enocean.internal.messages.ERP1Message.RORG;
31 import org.openhab.binding.enocean.internal.transceiver.PacketListener;
32 import org.openhab.core.config.core.Configuration;
33 import org.openhab.core.thing.Channel;
34 import org.openhab.core.thing.ChannelUID;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.link.ItemChannelLinkRegistry;
38 import org.openhab.core.thing.type.ChannelKind;
39 import org.openhab.core.thing.type.ChannelTypeUID;
40 import org.openhab.core.types.Command;
41 import org.openhab.core.types.State;
42 import org.openhab.core.types.UnDefType;
43 import org.openhab.core.util.HexUtils;
44
45 /**
46  * @author Daniel Weber - Initial contribution
47  *         This class defines base functionality for receiving eep messages.
48  */
49 public class EnOceanBaseSensorHandler extends EnOceanBaseThingHandler implements PacketListener {
50
51     // List of all thing types which support receiving of eep messages
52     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_ROOMOPERATINGPANEL,
53             THING_TYPE_MECHANICALHANDLE, THING_TYPE_CONTACT, THING_TYPE_TEMPERATURESENSOR,
54             THING_TYPE_TEMPERATUREHUMIDITYSENSOR, THING_TYPE_ROCKERSWITCH, THING_TYPE_OCCUPANCYSENSOR,
55             THING_TYPE_LIGHTTEMPERATUREOCCUPANCYSENSOR, THING_TYPE_LIGHTSENSOR, THING_TYPE_PUSHBUTTON,
56             THING_TYPE_AUTOMATEDMETERSENSOR, THING_TYPE_ENVIRONMENTALSENSOR, THING_TYPE_MULTFUNCTIONSMOKEDETECTOR);
57
58     protected final Hashtable<RORG, EEPType> receivingEEPTypes = new Hashtable<>();
59
60     public EnOceanBaseSensorHandler(Thing thing, ItemChannelLinkRegistry itemChannelLinkRegistry) {
61         super(thing, itemChannelLinkRegistry);
62     }
63
64     @Override
65     void initializeConfig() {
66         config = getConfigAs(EnOceanBaseConfig.class);
67     }
68
69     @Override
70     Collection<EEPType> getEEPTypes() {
71         return Collections.unmodifiableCollection(receivingEEPTypes.values());
72     }
73
74     @Override
75     boolean validateConfig() {
76         receivingEEPTypes.clear();
77         try {
78             config.receivingEEPId.forEach(receivingEEP -> {
79                 EEPType receivingEEPType = EEPType.getType(receivingEEP);
80                 if (receivingEEPTypes.putIfAbsent(receivingEEPType.getRORG(), receivingEEPType) != null) {
81                     throw new IllegalArgumentException("Receiving more than one EEP of the same RORG is not supported");
82                 }
83             });
84             if (config.receivingSIGEEP) {
85                 receivingEEPTypes.put(EEPType.SigBatteryStatus.getRORG(), EEPType.SigBatteryStatus);
86             }
87         } catch (IllegalArgumentException e) {
88             configurationErrorDescription = e.getMessage();
89             return false;
90         }
91
92         updateChannels();
93
94         if (!validateEnoceanId(config.enoceanId)) {
95             configurationErrorDescription = "EnOceanId is not a valid EnOceanId";
96             return false;
97         }
98
99         if (!config.enoceanId.equals(EMPTYENOCEANID)) {
100             getBridgeHandler().addPacketListener(this);
101         }
102
103         return true;
104     }
105
106     @Override
107     public long getSenderIdToListenTo() {
108         return Long.parseLong(config.enoceanId, 16);
109     }
110
111     @Override
112     public void handleRemoval() {
113         if (getBridgeHandler() != null) {
114             getBridgeHandler().removePacketListener(this);
115         }
116         super.handleRemoval();
117     }
118
119     @Override
120     public void handleCommand(ChannelUID channelUID, Command command) {
121         // sensor things cannot send any messages, hence they are not allowed to handle any command
122         // The only possible command would be "Refresh"
123     }
124
125     protected Predicate<Channel> channelFilter(EEPType eepType, byte[] senderId) {
126         return c -> {
127             boolean result = eepType.GetSupportedChannels().containsKey(c.getUID().getId());
128             return (isLinked(c.getUID()) || c.getKind() == ChannelKind.TRIGGER) && result;
129         };
130     }
131
132     @Override
133     public void packetReceived(BasePacket packet) {
134         ERP1Message msg = (ERP1Message) packet;
135         EEPType receivingEEPType = receivingEEPTypes.get(msg.getRORG());
136         if (receivingEEPType == null) {
137             return;
138         }
139
140         EEP eep = EEPFactory.buildEEP(receivingEEPType, (ERP1Message) packet);
141         logger.debug("ESP Packet payload {} for {} received", HexUtils.bytesToHex(packet.getPayload()),
142                 HexUtils.bytesToHex(msg.getSenderId()));
143
144         if (eep.isValid()) {
145             byte[] senderId = msg.getSenderId();
146
147             // try to interpret received message for all linked or trigger channels
148             getThing().getChannels().stream().filter(channelFilter(receivingEEPType, senderId))
149                     .sorted(Comparator.comparing(Channel::getKind)) // handle state channels first
150                     .forEachOrdered(channel -> {
151
152                         ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
153                         String channelTypeId = (channelTypeUID != null) ? channelTypeUID.getId() : "";
154
155                         String channelId = channel.getUID().getId();
156                         Configuration channelConfig = channel.getConfiguration();
157
158                         switch (channel.getKind()) {
159                             case STATE:
160                                 State result = eep.convertToState(channelId, channelTypeId, channelConfig,
161                                         this::getCurrentState);
162
163                                 // if message can be interpreted (result != UnDefType.UNDEF) => update item state
164                                 if (result != null && result != UnDefType.UNDEF) {
165                                     updateState(channelId, result);
166                                 }
167                                 break;
168                             case TRIGGER:
169                                 String lastEvent = lastEvents.get(channelId);
170                                 String event = eep.convertToEvent(channelId, channelTypeId, lastEvent, channelConfig);
171                                 if (event != null) {
172                                     triggerChannel(channel.getUID(), event);
173                                     lastEvents.put(channelId, event);
174                                 }
175                                 break;
176                         }
177                     });
178         }
179     }
180 }