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.daikin.internal.api;
16 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Holds information from the get_week_power_ex call.
25 * @author Wouter Denayer - Initial contribution
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();
37 private static final Logger LOGGER = LoggerFactory.getLogger(EnergyInfoDayAndWeek.class);
39 private EnergyInfoDayAndWeek() {
42 public static EnergyInfoDayAndWeek parse(String response) {
43 LOGGER.trace("Parsing string: \"{}\"", response);
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));
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();
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]);
65 double previousWeekEnergy = 0;
66 for (int i = thisWeekLastDayIndex; i < thisWeekLastDayIndex + 7; i += 1) {
67 previousWeekEnergy += Integer.parseInt(heatingValues[i]);
69 info.energyHeatingThisWeek = Optional.of(thisWeekEnergy / 10);
70 info.energyHeatingLastWeek = Optional.of(previousWeekEnergy / 10);
72 // get the cooling info
73 String[] coolingValues = responseMap.get("week_cool").split("/");
74 info.energyCoolingToday = Optional.of(Double.parseDouble(coolingValues[0]) / 10);
76 for (int i = 0; i < thisWeekLastDayIndex; i += 1) {
77 thisWeekEnergy += Integer.parseInt(coolingValues[i]);
79 previousWeekEnergy = 0;
80 for (int i = thisWeekLastDayIndex; i < thisWeekLastDayIndex + 7; i += 1) {
81 previousWeekEnergy += Integer.parseInt(coolingValues[i]);
83 info.energyCoolingThisWeek = Optional.of(thisWeekEnergy / 10);
84 info.energyCoolingLastWeek = Optional.of(previousWeekEnergy / 10);
87 LOGGER.debug("EnergyInfoDayAndWeek::parse() did not receive 'ret=OK' from adapter");