001/*
002 * Copyright 2017-2019, Digi International Inc.
003 *
004 * This Source Code Form is subject to the terms of the Mozilla Public
005 * License, v. 2.0. If a copy of the MPL was not distributed with this
006 * file, you can obtain one at http://mozilla.org/MPL/2.0/.
007 *
008 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 
009 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
010 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 
011 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
012 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
013 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
014 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
015 */
016package com.digi.xbee.api.models;
017
018import java.util.HashMap;
019
020/**
021 * Enumerates the different working modes of the XBee device. The operating 
022 * mode establishes the way a user communicates with an XBee device through 
023 * its serial interface.
024 */
025public enum OperatingMode {
026
027        // Enumeration types
028        AT(0, "AT mode"),
029        API(1, "API mode"),
030        API_ESCAPE(2, "API mode with escaped characters"),
031        /** @since 1.3.1 */
032        MICROPYTHON(4, "MicroPython REPL"),
033        /** @since 1.3.1 */
034        BYPASS(5, "Bypass mode"),
035        UNKNOWN(3, "Unknown");
036        
037        // Variables
038        private final int id;
039        
040        private final String name;
041        
042        private final static HashMap<Integer, OperatingMode> lookupTable = new HashMap<Integer, OperatingMode>();
043        
044        static {
045                for (OperatingMode operatingMode:values())
046                        lookupTable.put(operatingMode.getID(), operatingMode);
047        }
048        
049        /**
050         * Class constructor. Instantiates a new {@code OperatingMode} enumeration 
051         * entry with the given parameters.
052         * 
053         * @param id Operating mode ID.
054         * @param name Operating mode name.
055         */
056        private OperatingMode(int id, String name) {
057                this.id = id;
058                this.name = name;
059        }
060        
061        /**
062         * Returns the operating mode ID.
063         * 
064         * @return Operating mode ID.
065         */
066        public int getID() {
067                return id;
068        }
069        
070        /**
071         * Returns the operating mode name.
072         * 
073         * @return Operating mode name.
074         */
075        public String getName() {
076                return name;
077        }
078        
079        /**
080         * Returns the {@code OperatingMode} entry associated to the given value.
081         * 
082         * @param value Value of the {@code OperatingMode} to retrieve.
083         * 
084         * @return The {@code OperatingMode} entry associated to the given value,
085         *         {@code #UNKNOWN} if the value could not be found in the list.
086         * 
087         * @since 1.3.1
088         */
089        public static OperatingMode get(int value) {
090                OperatingMode operatingMode = lookupTable.get(value);
091                if (operatingMode != null)
092                        return operatingMode;
093                return OperatingMode.UNKNOWN;
094        }
095        
096        /*
097         * (non-Javadoc)
098         * @see java.lang.Enum#toString()
099         */
100        @Override
101        public String toString() {
102                return name;
103        }
104}