]> git.basschouten.com Git - openhab-addons.git/blob
ebfe32a2bef2311320c3becb08eab29b5a41bab4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.insteon.internal.message;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19
20 /**
21  * Represents insteon message type flags
22  *
23  * @author Daniel Pfrommer - Initial contribution
24  * @author Rob Nielsen - Port to openHAB 2 insteon binding
25  */
26 @NonNullByDefault
27 public enum MsgType {
28     /*
29      * From the official Insteon docs: the message flags are as follows:
30      *
31      * Bit 0 max hops low bit
32      * Bit 1 max hops high bit
33      * Bit 2 hops left low bit
34      * Bit 3 hops left high bit
35      * Bit 4 0: is standard message, 1: is extended message
36      * Bit 5 ACK
37      * Bit 6 0: not link related, 1: is ALL-Link message
38      * Bit 7 Broadcast/NAK
39      */
40     BROADCAST(0x80),
41     DIRECT(0x00),
42     ACK_OF_DIRECT(0x20),
43     NACK_OF_DIRECT(0xa0),
44     ALL_LINK_BROADCAST(0xc0),
45     ALL_LINK_CLEANUP(0x40),
46     ALL_LINK_CLEANUP_ACK(0x60),
47     ALL_LINK_CLEANUP_NACK(0xe0),
48     INVALID(0xff); // should never happen
49
50     private static Map<Integer, MsgType> hash = new HashMap<>();
51
52     private byte byteValue = 0;
53
54     /**
55      * Constructor
56      *
57      * @param b byte with insteon message type flags set
58      */
59     MsgType(int b) {
60         this.byteValue = (byte) b;
61     }
62
63     static {
64         for (MsgType t : MsgType.values()) {
65             int i = t.getByteValue() & 0xff;
66             hash.put(i, t);
67         }
68     }
69
70     private int getByteValue() {
71         return byteValue;
72     }
73
74     public static MsgType fromValue(byte b) throws IllegalArgumentException {
75         int i = b & 0xe0;
76         MsgType mt = hash.get(i);
77         if (mt == null) {
78             throw new IllegalArgumentException("msg type of byte value " + i + " not found");
79         }
80         return mt;
81     }
82 }