]> git.basschouten.com Git - openhab-addons.git/blob
51419676db1153ea420eb5ade61d40c1549b4321
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.easee.internal.model;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * this enum represents the charger operation states as documented by https://developer.easee.cloud/docs/enumerations
21  *
22  * @author Alexander Friese - initial contribution
23  */
24 @NonNullByDefault
25 public enum ChargerOpState {
26     OFFLINE(0),
27     DISCONNECTED(1),
28     WAITING(2),
29     CHARGING(3),
30     COMPLETED(4),
31     ERROR(5),
32     READY_TO_CHARGE(6),
33     NOT_AUTHENTICATED(7),
34     DEAUTHENTICATING(8),
35     UNKNOWN_STATE(-1);
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(ChargerOpState.class);
38     private final int code;
39
40     private ChargerOpState(int code) {
41         this.code = code;
42     }
43
44     public boolean isAuthenticatedState() {
45         switch (this) {
46             case WAITING:
47             case CHARGING:
48             case COMPLETED:
49             case READY_TO_CHARGE:
50                 return true;
51             default:
52                 return false;
53         }
54     }
55
56     public static ChargerOpState fromCode(String code) {
57         try {
58             return ChargerOpState.fromCode(Integer.parseInt(code));
59         } catch (NumberFormatException ex) {
60             LOGGER.warn("caught exception while parsing ChargerOpState code: '{}' - exception: {}", code,
61                     ex.getMessage());
62             return UNKNOWN_STATE;
63         }
64     }
65
66     public static ChargerOpState fromCode(int code) {
67         for (ChargerOpState state : ChargerOpState.values()) {
68             if (state.code == code) {
69                 return state;
70             }
71         }
72         LOGGER.info("unknown ChargerOpState code: '{}'", code);
73         return UNKNOWN_STATE;
74     }
75 }