2 * Copyright (c) 2010-2020 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.satel.internal.event;
15 import java.util.BitSet;
16 import java.util.HashMap;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.satel.internal.types.StateType;
23 * Event class describing current state of zones/partitions/outputs/doors/troubles.
25 * @author Krzysztof Goworek - Initial contribution
28 public class IntegraStateEvent implements SatelEvent {
31 private BitSet stateBits;
32 private boolean extendedData;
33 private Map<StateType, BitSet> stateBitsMap = new HashMap<>();
36 * Constructs new event instance from given state type and state bits.
38 * @param command the command byte
39 * @param stateBits state bits as byte array
40 * @param extendedData whether state bits are for extended command
42 public IntegraStateEvent(byte command, byte[] stateBits, boolean extendedData) {
43 this.command = command;
44 this.stateBits = BitSet.valueOf(stateBits);
45 this.extendedData = extendedData;
49 * Checks whether data in the event is valid for given type of state.
51 * @param stateType state type
52 * @return <code>true</code> if this event is valid for given stae
54 public boolean hasDataForState(StateType stateType) {
55 return stateType.getRefreshCommand() == this.command;
59 * Returns state bits as {@link BitSet}.
61 * @param stateType type of state
62 * @return bits for given state
63 * @throws IllegalArgumentException when wrong state type given
65 public BitSet getStateBits(StateType stateType) {
66 if (!hasDataForState(stateType)) {
67 throw new IllegalArgumentException("Event does not have data for " + stateType);
69 int bitsCount = stateType.getBytesCount(extendedData) * 8;
70 // whole payload is a single state
71 if (stateType.getStartByte() == 0 && bitsCount == stateBits.size()) {
74 return stateBitsMap.computeIfAbsent(stateType, k -> {
75 int startBit = k.getStartByte() * 8;
76 return stateBits.get(startBit, startBit + bitsCount);
81 * Returns <code>true</code> if specified state bit is set for given state.
83 * @param stateType type of state
84 * @param nbr state bit number
85 * @return <code>true</code> if state bit is set
87 public boolean isSet(StateType stateType, int nbr) {
88 return getStateBits(stateType).get(nbr);
92 * Returns number of state bits that are active for given state.
94 * @param stateType type of state
95 * @return number of active states
97 public int statesSet(StateType stateType) {
98 return getStateBits(stateType).cardinality();
102 public String toString() {
103 return String.format("IntegraStateEvent: command = %02X, extended = %b, active = %s", command, extendedData,