]> git.basschouten.com Git - openhab-addons.git/blob
963f2bd42b3e75d93efcb3616983526ea39e0683
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.satel.internal.event;
14
15 import java.util.BitSet;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.satel.internal.types.StateType;
21
22 /**
23  * Event class describing current state of zones/partitions/outputs/doors/troubles.
24  *
25  * @author Krzysztof Goworek - Initial contribution
26  */
27 @NonNullByDefault
28 public class IntegraStateEvent implements SatelEvent {
29
30     private byte command;
31     private BitSet stateBits;
32     private boolean extendedData;
33     private Map<StateType, BitSet> stateBitsMap = new HashMap<>();
34
35     /**
36      * Constructs new event instance from given state type and state bits.
37      *
38      * @param command the command byte
39      * @param stateBits state bits as byte array
40      * @param extendedData whether state bits are for extended command
41      */
42     public IntegraStateEvent(byte command, byte[] stateBits, boolean extendedData) {
43         this.command = command;
44         this.stateBits = BitSet.valueOf(stateBits);
45         this.extendedData = extendedData;
46     }
47
48     /**
49      * Checks whether data in the event is valid for given type of state.
50      *
51      * @param stateType state type
52      * @return <code>true</code> if this event is valid for given stae
53      */
54     public boolean hasDataForState(StateType stateType) {
55         return stateType.getRefreshCommand() == this.command;
56     }
57
58     /**
59      * Returns state bits as {@link BitSet}.
60      *
61      * @param stateType type of state
62      * @return bits for given state
63      * @throws IllegalArgumentException when wrong state type given
64      */
65     public BitSet getStateBits(StateType stateType) {
66         if (!hasDataForState(stateType)) {
67             throw new IllegalArgumentException("Event does not have data for " + stateType);
68         }
69         int bitsCount = stateType.getBytesCount(extendedData) * 8;
70         // whole payload is a single state
71         if (stateType.getStartByte() == 0 && bitsCount == stateBits.size()) {
72             return stateBits;
73         }
74         return stateBitsMap.computeIfAbsent(stateType, k -> {
75             int startBit = k.getStartByte() * 8;
76             return stateBits.get(startBit, startBit + bitsCount);
77         });
78     }
79
80     /**
81      * Returns <code>true</code> if specified state bit is set for given state.
82      *
83      * @param stateType type of state
84      * @param nbr state bit number
85      * @return <code>true</code> if state bit is set
86      */
87     public boolean isSet(StateType stateType, int nbr) {
88         return getStateBits(stateType).get(nbr);
89     }
90
91     /**
92      * Returns number of state bits that are active for given state.
93      *
94      * @param stateType type of state
95      * @return number of active states
96      */
97     public int statesSet(StateType stateType) {
98         return getStateBits(stateType).cardinality();
99     }
100
101     @Override
102     public String toString() {
103         return String.format("IntegraStateEvent: command = %02X, extended = %b, active = %s", command, extendedData,
104                 stateBits);
105     }
106 }