]> git.basschouten.com Git - openhab-addons.git/blob
4e7671e0f7132a969196958fc60135934489b70e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.airquality.internal.api.dto;
14
15 import java.util.List;
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.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.airquality.internal.api.Pollutant;
23
24 /**
25  * The {@link AirQualityData} is responsible for storing
26  * the "data" node from the waqi.org JSON response
27  *
28  * @author Kuba Wolanin - Initial contribution
29  */
30 @NonNullByDefault
31 public class AirQualityData extends ResponseRoot {
32
33     private int aqi;
34     private int idx;
35
36     private @Nullable AirQualityTime time;
37     private @Nullable AirQualityCity city;
38     private List<Attribution> attributions = List.of();
39     private Map<String, AirQualityValue> iaqi = Map.of();
40     private String dominentpol = "";
41
42     /**
43      * Air Quality Index
44      *
45      * @return {Integer}
46      */
47     public int getAqi() {
48         return aqi;
49     }
50
51     /**
52      * Measuring Station ID
53      *
54      * @return {Integer}
55      */
56     public int getStationId() {
57         return idx;
58     }
59
60     /**
61      * Receives "time" node from the "data" object in JSON response
62      *
63      * @return {AirQualityJsonTime}
64      */
65     public Optional<AirQualityTime> getTime() {
66         return Optional.ofNullable(time);
67     }
68
69     /**
70      * Receives "city" node from the "data" object in JSON response
71      *
72      * @return {AirQualityJsonCity}
73      */
74     public Optional<AirQualityCity> getCity() {
75         return Optional.ofNullable(city);
76     }
77
78     /**
79      * Collects a list of attributions (vendors making data available)
80      * and transforms it into readable string.
81      *
82      * @return {String}
83      */
84     public String getAttributions() {
85         return attributions.stream().map(Attribution::getName).collect(Collectors.joining(", "));
86     }
87
88     public String getDominentPol() {
89         return dominentpol;
90     }
91
92     public double getIaqiValue(String key) {
93         AirQualityValue result = iaqi.get(key);
94         return result != null ? result.getValue() : -1;
95     }
96
97     public double getIaqiValue(Pollutant pollutant) {
98         return getIaqiValue(pollutant.name().toLowerCase());
99     }
100 }