]> git.basschouten.com Git - openhab-addons.git/blob
a9f1d0fe73c16fac6beafb91bbd72267bfd3e196
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.tplinksmarthome.internal.model;
14
15 /**
16  * Data class for reading tp-Link Smart Plug energy monitoring.
17  * Only getter methods as the values are set by gson based on the retrieved json.
18  *
19  * @author Hilbrand Bouwkamp - Initial contribution
20  */
21 public class Realtime extends ErrorResponse {
22
23     private static final int MILLIWATT_TO_WATT = 1000;
24     private static final int MILLIAMP_TO_AMP = 1000;
25     private static final int WATTHOUR_TO_KILOWATTHOUR = 1000;
26     private static final int MILLIVOLT_TO_VOLT = 1000;
27
28     private double current;
29     private double power;
30     private double total;
31     private double voltage;
32
33     // JSON names used for v2 hardware
34     private double currentMa;
35     private double powerMw;
36     private double totalWh;
37     private double voltageMv;
38
39     public double getCurrent() {
40         return currentMa > 0.0 ? currentMa / MILLIAMP_TO_AMP : current;
41     }
42
43     public double getPower() {
44         return powerMw > 0.0 ? powerMw / MILLIWATT_TO_WATT : power;
45     }
46
47     public double getTotal() {
48         return totalWh > 0.0 ? totalWh / WATTHOUR_TO_KILOWATTHOUR : total;
49     }
50
51     public double getVoltage() {
52         return voltageMv > 0.0 ? voltageMv / MILLIVOLT_TO_VOLT : voltage;
53     }
54
55     @Override
56     public String toString() {
57         return "current:" + getCurrent() + ", power:" + getPower() + ", total:" + getTotal() + ", voltage:"
58                 + getVoltage() + super.toString();
59     }
60 }