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