2 * Copyright (c) 2010-2021 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.daikin.internal.api;
15 import java.util.Arrays;
17 import java.util.Optional;
18 import java.util.stream.Collectors;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Holds information from the get_week_power_ex call.
27 * @author Wouter Denayer - Initial contribution
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();
39 private static final Logger LOGGER = LoggerFactory.getLogger(EnergyInfoDayAndWeek.class);
41 private EnergyInfoDayAndWeek() {
44 public static EnergyInfoDayAndWeek parse(String response) {
45 EnergyInfoDayAndWeek info = new EnergyInfoDayAndWeek();
47 LOGGER.trace("Parsing string: \"{}\"", response);
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("="))
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]));
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));
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();
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]);
75 double previousWeekEnergy = 0;
76 for (int i = thisWeekLastDayIndex; i < thisWeekLastDayIndex + 7; i += 1) {
77 previousWeekEnergy += Integer.parseInt(heatingValues[i]);
79 info.energyHeatingThisWeek = Optional.of(thisWeekEnergy / 10);
80 info.energyHeatingLastWeek = Optional.of(previousWeekEnergy / 10);
82 // get the cooling info
83 String[] coolingValues = responseMap.get("week_cool").split("/");
84 info.energyCoolingToday = Optional.of(Double.parseDouble(coolingValues[0]) / 10);
86 for (int i = 0; i < thisWeekLastDayIndex; i += 1) {
87 thisWeekEnergy += Integer.parseInt(coolingValues[i]);
89 previousWeekEnergy = 0;
90 for (int i = thisWeekLastDayIndex; i < thisWeekLastDayIndex + 7; i += 1) {
91 previousWeekEnergy += Integer.parseInt(coolingValues[i]);
93 info.energyCoolingThisWeek = Optional.of(thisWeekEnergy / 10);
94 info.energyCoolingLastWeek = Optional.of(previousWeekEnergy / 10);
97 LOGGER.debug("did not receive 'ret=OK' from adapter");