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