]> git.basschouten.com Git - openhab-addons.git/blob
49cb3eb28e5c9846ccf319e4faae47af76e6ea61
[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.awattar.internal;
14
15 import static org.openhab.binding.awattar.internal.AwattarUtil.formatDate;
16 import static org.openhab.binding.awattar.internal.AwattarUtil.getHourFrom;
17
18 import java.time.Instant;
19 import java.time.ZoneId;
20 import java.util.List;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23
24 /**
25  * Stores a consecutive bestprice result
26  *
27  * @author Wolfgang Klimt - initial contribution
28  */
29 @NonNullByDefault
30 public class AwattarConsecutiveBestPriceResult extends AwattarBestPriceResult {
31
32     private double priceSum = 0;
33     private int length = 0;
34     private String hours;
35     private ZoneId zoneId;
36
37     public AwattarConsecutiveBestPriceResult(List<AwattarPrice> prices, ZoneId zoneId) {
38         super();
39         this.zoneId = zoneId;
40         StringBuilder hours = new StringBuilder();
41         boolean second = false;
42         for (AwattarPrice price : prices) {
43             priceSum += price.getPrice();
44             length++;
45             updateStart(price.getStartTimestamp());
46             updateEnd(price.getEndTimestamp());
47             if (second) {
48                 hours.append(',');
49             }
50             hours.append(getHourFrom(price.getStartTimestamp(), zoneId));
51             second = true;
52         }
53         this.hours = hours.toString();
54     }
55
56     @Override
57     public boolean isActive() {
58         return contains(Instant.now().toEpochMilli());
59     }
60
61     public boolean contains(long timestamp) {
62         return timestamp >= getStart() && timestamp < getEnd();
63     }
64
65     public double getPriceSum() {
66         return priceSum;
67     }
68
69     @Override
70     public String toString() {
71         return String.format("{%s, %s, %.2f}", formatDate(getStart(), zoneId), formatDate(getEnd(), zoneId),
72                 priceSum / length);
73     }
74
75     @Override
76     public String getHours() {
77         return hours;
78     }
79 }