]> git.basschouten.com Git - openhab-addons.git/blob
2bf2c8808b2b02e580e75e2f73e1266fbb5b8467
[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.energenie.internal;
14
15 import javax.measure.Unit;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.library.types.QuantityType;
19 import org.openhab.core.library.unit.Units;
20 import org.openhab.core.types.State;
21 import org.openhab.core.types.UnDefType;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * The {@link EnergeniePWMStateEnum} contains informations for parsing the readState() result.
27  *
28  * @author Hans-Jörg Merk - Initial contribution
29  */
30
31 @NonNullByDefault
32 public enum EnergeniePWMStateEnum {
33     VOLTAGE("var V  = ", 9, 20, 10, Units.VOLT),
34     CURRENT("var V  = ", 9, 20, 100, Units.AMPERE),
35     POWER("var P=", 6, 20, 466, Units.WATT),
36     ENERGY("var E=", 6, 20, 25600, Units.WATT_HOUR);
37
38     private final Logger logger = LoggerFactory.getLogger(EnergeniePWMStateEnum.class);
39
40     private final String stateResponseSearch;
41     private final int start;
42     private final int stop;
43     private final int divisor;
44     private final Unit<?> unit;
45
46     private EnergeniePWMStateEnum(final String stateResponseSearch, final int start, final int stop, final int divisor,
47             final Unit<?> unit) {
48         this.stateResponseSearch = stateResponseSearch;
49         this.start = start;
50         this.stop = stop;
51         this.divisor = divisor;
52         this.unit = unit;
53     }
54
55     public String getStateResponseSearch() {
56         return stateResponseSearch;
57     }
58
59     public int getStart() {
60         return start;
61     }
62
63     public int getStop() {
64         return stop;
65     }
66
67     public int getDivisor() {
68         return divisor;
69     }
70
71     public State readState(final String loginResponseString) {
72         final int findState = loginResponseString.lastIndexOf(stateResponseSearch);
73
74         if (findState > 0) {
75             logger.trace("searchstring {} found at position {}", stateResponseSearch, findState);
76             final String slicedResponseTmp = loginResponseString.substring(findState + start, findState + stop);
77             logger.trace("transformed state response = {}", slicedResponseTmp);
78             final String[] slicedResponse = slicedResponseTmp.split(";");
79             logger.trace("transformed state response = {} - {}", slicedResponse[0], slicedResponse[1]);
80             final double value;
81             try {
82                 value = Double.parseDouble(slicedResponse[0]) / divisor;
83                 return QuantityType.valueOf(value, unit);
84             } catch (NumberFormatException e) {
85                 logger.debug("Could not Parse State", e);
86                 return UnDefType.UNDEF;
87             }
88         } else {
89             logger.trace("searchstring '{} not found", stateResponseSearch);
90             return UnDefType.UNDEF;
91         }
92     }
93 }