]> git.basschouten.com Git - openhab-addons.git/blob
ddcbab2d9511f698fd301da59f2e76263c1c57b2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.daikin.internal.api;
14
15 import java.util.Arrays;
16 import java.util.Map;
17 import java.util.Optional;
18 import java.util.stream.Collectors;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Holds information from the get_week_power_ex call.
26  *
27  * @author Wouter Denayer - Initial contribution
28  *
29  */
30 @NonNullByDefault
31 public class EnergyInfoDayAndWeek {
32     public Optional<Double> energyHeatingToday = Optional.empty();
33     public Optional<Double> energyHeatingThisWeek = Optional.empty();
34     public Optional<Double> energyHeatingLastWeek = Optional.empty();
35     public Optional<Double> energyCoolingToday = Optional.empty();
36     public Optional<Double> energyCoolingThisWeek = Optional.empty();
37     public Optional<Double> energyCoolingLastWeek = Optional.empty();
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(EnergyInfoDayAndWeek.class);
40
41     private EnergyInfoDayAndWeek() {
42     }
43
44     public static EnergyInfoDayAndWeek parse(String response) {
45         EnergyInfoDayAndWeek info = new EnergyInfoDayAndWeek();
46
47         LOGGER.trace("Parsing string: \"{}\"", response);
48
49         // /aircon/get_week_power_ex
50         // ret=OK,s_dayw=0,week_heat=1/1/1/1/1/5/2/1/1/1/1/2/1/1,week_cool=0/0/0/0/0/0/0/0/0/0/0/0/0/0
51         // week_heat=<today>/<today-1>/<today-2>/<today-3>/...
52         Map<String, String> responseMap = Arrays.asList(response.split(",")).stream().filter(kv -> kv.contains("="))
53                 .map(kv -> {
54                     String[] keyValue = kv.split("=");
55                     String key = keyValue[0];
56                     String value = keyValue.length > 1 ? keyValue[1] : "";
57                     return new String[] { key, value };
58                 }).collect(Collectors.toMap(x -> x[0], x -> x[1]));
59
60         if (responseMap.get("ret") != null && ("OK".equals(responseMap.get("ret")))) {
61             Optional<Integer> dayOfWeek = Optional.ofNullable(responseMap.get("s_dayw"))
62                     .flatMap(value -> InfoParser.parseInt(value));
63
64             if (dayOfWeek.isPresent()) {
65                 // Daikin API week starts on Sunday, ours on Monday
66                 int thisWeekLastDayIndex = (dayOfWeek.get().intValue() == 0) ? 7 : dayOfWeek.get().intValue();
67
68                 // get the heating info
69                 String[] heatingValues = responseMap.get("week_heat").split("/");
70                 info.energyHeatingToday = Optional.of(Double.parseDouble(heatingValues[0]) / 10);
71                 double thisWeekEnergy = 0;
72                 for (int i = 0; i < thisWeekLastDayIndex; i += 1) {
73                     thisWeekEnergy += Integer.parseInt(heatingValues[i]);
74                 }
75                 double previousWeekEnergy = 0;
76                 for (int i = thisWeekLastDayIndex; i < thisWeekLastDayIndex + 7; i += 1) {
77                     previousWeekEnergy += Integer.parseInt(heatingValues[i]);
78                 }
79                 info.energyHeatingThisWeek = Optional.of(thisWeekEnergy / 10);
80                 info.energyHeatingLastWeek = Optional.of(previousWeekEnergy / 10);
81
82                 // get the cooling info
83                 String[] coolingValues = responseMap.get("week_cool").split("/");
84                 info.energyCoolingToday = Optional.of(Double.parseDouble(coolingValues[0]) / 10);
85                 thisWeekEnergy = 0;
86                 for (int i = 0; i < thisWeekLastDayIndex; i += 1) {
87                     thisWeekEnergy += Integer.parseInt(coolingValues[i]);
88                 }
89                 previousWeekEnergy = 0;
90                 for (int i = thisWeekLastDayIndex; i < thisWeekLastDayIndex + 7; i += 1) {
91                     previousWeekEnergy += Integer.parseInt(coolingValues[i]);
92                 }
93                 info.energyCoolingThisWeek = Optional.of(thisWeekEnergy / 10);
94                 info.energyCoolingLastWeek = Optional.of(previousWeekEnergy / 10);
95             }
96         } else {
97             LOGGER.debug("did not receive 'ret=OK' from adapter");
98         }
99         return info;
100     }
101 }