]> git.basschouten.com Git - openhab-addons.git/blob
8d07e9228716d98947ca102af8ce862cad4b7485
[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.omnilink.internal.handler;
14
15 import static com.digitaldan.jomnilinkII.MessageTypes.properties.AuxSensorProperties.SENSOR_TYPE_PROGRAMMABLE_ENERGY_SAVER_MODULE;
16 import static org.openhab.binding.omnilink.internal.OmnilinkBindingConstants.*;
17
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Optional;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequest;
25 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequests;
26 import org.openhab.binding.omnilink.internal.exceptions.BridgeOfflineException;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.library.types.OpenClosedType;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingStatusDetail;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.RefreshType;
36 import org.openhab.core.types.State;
37 import org.openhab.core.types.UnDefType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import com.digitaldan.jomnilinkII.Message;
42 import com.digitaldan.jomnilinkII.MessageTypes.CommandMessage;
43 import com.digitaldan.jomnilinkII.MessageTypes.ObjectStatus;
44 import com.digitaldan.jomnilinkII.MessageTypes.SecurityCodeValidation;
45 import com.digitaldan.jomnilinkII.MessageTypes.properties.AreaProperties;
46 import com.digitaldan.jomnilinkII.MessageTypes.properties.ZoneProperties;
47 import com.digitaldan.jomnilinkII.MessageTypes.statuses.ExtendedZoneStatus;
48 import com.digitaldan.jomnilinkII.OmniInvalidResponseException;
49 import com.digitaldan.jomnilinkII.OmniUnknownMessageTypeException;
50
51 /**
52  * The {@link ZoneHandler} defines some methods that are used to
53  * interface with an OmniLink Zone. This by extension also defines the
54  * OmniPro Zone thing that openHAB will be able to pick up and interface with.
55  *
56  * @author Craig Hamilton - Initial contribution
57  */
58 @NonNullByDefault
59 public class ZoneHandler extends AbstractOmnilinkStatusHandler<ExtendedZoneStatus> {
60     private final Logger logger = LoggerFactory.getLogger(ZoneHandler.class);
61     private final int thingID = getThingNumber();
62     public @Nullable String number;
63
64     public ZoneHandler(Thing thing) {
65         super(thing);
66     }
67
68     @Override
69     public void initialize() {
70         super.initialize();
71         final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
72         if (bridgeHandler != null) {
73             updateZoneProperties(bridgeHandler);
74         } else {
75             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
76                     "Received null bridge while initializing Zone!");
77         }
78     }
79
80     private void updateZoneProperties(OmnilinkBridgeHandler bridgeHandler) {
81         final List<AreaProperties> areas = super.getAreaProperties();
82         if (areas != null) {
83             for (AreaProperties areaProperties : areas) {
84                 int areaFilter = super.bitFilterForArea(areaProperties);
85
86                 ObjectPropertyRequest<ZoneProperties> objectPropertyRequest = ObjectPropertyRequest
87                         .builder(bridgeHandler, ObjectPropertyRequests.ZONE, getThingNumber(), 0).selectNamed()
88                         .areaFilter(areaFilter).build();
89
90                 for (ZoneProperties zoneProperties : objectPropertyRequest) {
91                     if (zoneProperties.getZoneType() <= SENSOR_TYPE_PROGRAMMABLE_ENERGY_SAVER_MODULE) {
92                         Map<String, String> properties = editProperties();
93                         properties.put(THING_PROPERTIES_NAME, zoneProperties.getName());
94                         properties.put(THING_PROPERTIES_AREA, Integer.toString(areaProperties.getNumber()));
95                         updateProperties(properties);
96                     }
97                 }
98             }
99         }
100     }
101
102     @Override
103     public void handleCommand(ChannelUID channelUID, Command command) {
104         logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
105         int mode;
106
107         if (command instanceof RefreshType) {
108             retrieveStatus().ifPresentOrElse(this::updateChannels, () -> updateStatus(ThingStatus.OFFLINE,
109                     ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Received null status update!"));
110             return;
111         }
112
113         if (!(command instanceof StringType)) {
114             logger.debug("Invalid command: {}, must be StringType", command);
115             return;
116         }
117
118         switch (channelUID.getId()) {
119             case CHANNEL_ZONE_BYPASS:
120                 mode = CommandMessage.CMD_SECURITY_BYPASS_ZONE;
121                 break;
122             case CHANNEL_ZONE_RESTORE:
123                 mode = CommandMessage.CMD_SECURITY_RESTORE_ZONE;
124                 break;
125             default:
126                 mode = -1;
127         }
128         int areaNumber = getAreaNumber();
129         logger.debug("mode {} on zone {} with code {}", mode, thingID, command.toFullString());
130         char[] code = command.toFullString().toCharArray();
131         if (code.length != 4) {
132             logger.warn("Invalid code length, code must be 4 digits");
133         } else {
134             try {
135                 final OmnilinkBridgeHandler bridge = getOmnilinkBridgeHandler();
136                 if (bridge != null) {
137                     SecurityCodeValidation codeValidation = bridge.reqSecurityCodeValidation(areaNumber,
138                             Character.getNumericValue(code[0]), Character.getNumericValue(code[1]),
139                             Character.getNumericValue(code[2]), Character.getNumericValue(code[3]));
140                     /*
141                      * 0 Invalid code
142                      * 1 Master
143                      * 2 Manager
144                      * 3 User
145                      */
146                     logger.debug("User code number: {} level: {}", codeValidation.getCodeNumber(),
147                             codeValidation.getAuthorityLevel());
148                     /*
149                      * Valid user code number are 1-99, 251 is duress code, 0 means code does not exist
150                      */
151                     if ((codeValidation.getCodeNumber() > 0 && codeValidation.getCodeNumber() <= 99)
152                             && codeValidation.getAuthorityLevel() > 0) {
153                         sendOmnilinkCommand(mode, codeValidation.getCodeNumber(), thingID);
154                     } else {
155                         logger.warn("System reported an invalid code");
156                     }
157                 } else {
158                     logger.debug("Received null bridge while sending zone command!");
159                 }
160             } catch (OmniInvalidResponseException e) {
161                 logger.debug("Zone command failed: {}", e.getMessage());
162             } catch (OmniUnknownMessageTypeException | BridgeOfflineException e) {
163                 logger.debug("Could not send zone command: {}", e.getMessage());
164             }
165         }
166         // This is a send only channel, so don't store the user code
167         updateState(channelUID, UnDefType.UNDEF);
168     }
169
170     @Override
171     protected void updateChannels(ExtendedZoneStatus zoneStatus) {
172         // 0 Secure. 1 Not ready, 3 Trouble
173         int current = ((zoneStatus.getStatus() >> 0) & 0x03);
174         // 0 Secure, 1 Tripped, 2 Reset, but previously tripped
175         int latched = ((zoneStatus.getStatus() >> 2) & 0x03);
176         // 0 Disarmed, 1 Armed, 2 Bypass user, 3 Bypass system
177         int arming = ((zoneStatus.getStatus() >> 4) & 0x03);
178         State contactState = Integer.valueOf(current).equals(0) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
179         logger.debug("handling Zone Status change to state: {}, current: {}, latched: {}, arming: {}", contactState,
180                 current, latched, arming);
181         updateState(CHANNEL_ZONE_CONTACT, contactState);
182         updateState(CHANNEL_ZONE_CURRENT_CONDITION, new DecimalType(current));
183         updateState(CHANNEL_ZONE_LATCHED_ALARM_STATUS, new DecimalType(latched));
184         updateState(CHANNEL_ZONE_ARMING_STATUS, new DecimalType(arming));
185     }
186
187     @Override
188     protected Optional<ExtendedZoneStatus> retrieveStatus() {
189         try {
190             final OmnilinkBridgeHandler bridge = getOmnilinkBridgeHandler();
191             if (bridge != null) {
192                 ObjectStatus objStatus = bridge.requestObjectStatus(Message.OBJ_TYPE_ZONE, thingID, thingID, true);
193                 return Optional.of((ExtendedZoneStatus) objStatus.getStatuses()[0]);
194             } else {
195                 logger.debug("Received null bridge while updating Zone status!");
196                 return Optional.empty();
197             }
198         } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
199             logger.debug("Received exception while refreshing Zone status: {}", e.getMessage());
200             return Optional.empty();
201         }
202     }
203 }