2 * Copyright (c) 2010-2023 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.energenie.internal;
15 import javax.measure.Unit;
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;
26 * The {@link EnergeniePWMStateEnum} contains informations for parsing the readState() result.
28 * @author Hans-Jörg Merk - Initial contribution
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);
38 private final Logger logger = LoggerFactory.getLogger(EnergeniePWMStateEnum.class);
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;
46 private EnergeniePWMStateEnum(final String stateResponseSearch, final int start, final int stop, final int divisor,
48 this.stateResponseSearch = stateResponseSearch;
51 this.divisor = divisor;
55 public String getStateResponseSearch() {
56 return stateResponseSearch;
59 public int getStart() {
63 public int getStop() {
67 public int getDivisor() {
71 public State readState(final String loginResponseString) {
72 final int findState = loginResponseString.lastIndexOf(stateResponseSearch);
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]);
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;
89 logger.trace("searchstring '{} not found", stateResponseSearch);
90 return UnDefType.UNDEF;