]> git.basschouten.com Git - openhab-addons.git/blob
79e4421f08c94bcae872d1818564db6f3a3e21a6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.sensebox.internal;
14
15 import static org.openhab.binding.sensebox.internal.SenseBoxBindingConstants.*;
16
17 import java.io.IOException;
18 import java.util.List;
19 import java.util.Properties;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.sensebox.internal.dto.SenseBoxData;
23 import org.openhab.binding.sensebox.internal.dto.SenseBoxDescriptor;
24 import org.openhab.binding.sensebox.internal.dto.SenseBoxLoc;
25 import org.openhab.binding.sensebox.internal.dto.SenseBoxLocation;
26 import org.openhab.binding.sensebox.internal.dto.SenseBoxSensor;
27 import org.openhab.core.io.net.http.HttpUtil;
28 import org.openhab.core.thing.ThingStatus;
29 import org.osgi.framework.FrameworkUtil;
30 import org.osgi.framework.Version;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.google.gson.Gson;
35
36 /**
37  * The {@link SenseBoxAPIConnection} is responsible for fetching data from the senseBox API server.
38  *
39  * @author Hakan Tandogan - Initial contribution
40  */
41 @NonNullByDefault
42 public class SenseBoxAPIConnection {
43
44     private final Logger logger = LoggerFactory.getLogger(SenseBoxAPIConnection.class);
45
46     private final Gson gson = new Gson();
47
48     private static final Properties HEADERS = new Properties();
49
50     private static final String METHOD = "GET";
51
52     private static final int TIMEOUT = 30 * 1000; // 30 seconds
53
54     public SenseBoxAPIConnection() {
55         Version version = FrameworkUtil.getBundle(this.getClass()).getVersion();
56         HEADERS.put("User-Agent", "openHAB / senseBox binding " + version.toString());
57         logger.trace("Headers: {}", HEADERS);
58     }
59
60     public SenseBoxData reallyFetchDataFromServer(String senseBoxId) {
61         String query = SENSEMAP_API_URL_BASE + "/boxes/" + senseBoxId;
62
63         // the caching layer does not like null values
64         SenseBoxData result = new SenseBoxData();
65
66         try {
67             String body = HttpUtil.executeUrl(METHOD, query, HEADERS, null, null, TIMEOUT);
68
69             logger.trace("Fetched Data: {}", body);
70             SenseBoxData parsedData = gson.fromJson(body, SenseBoxData.class);
71
72             // Assume all is well at first
73             parsedData.setStatus(ThingStatus.ONLINE);
74
75             // Could perhaps be simplified via triply-nested arrays
76             // http://stackoverflow.com/questions/36946875/how-can-i-parse-geojson-with-gson
77             for (SenseBoxLoc loc : parsedData.getLocs()) {
78                 if (loc.getGeometry() != null) {
79                     List<Double> locationData = loc.getGeometry().getData();
80                     if (locationData != null) {
81                         SenseBoxLocation location = new SenseBoxLocation();
82
83                         if (locationData.size() > 0) {
84                             location.setLongitude(locationData.get(0));
85                         }
86
87                         if (locationData.size() > 1) {
88                             location.setLatitude(locationData.get(1));
89                         }
90
91                         if (locationData.size() > 2) {
92                             location.setHeight(locationData.get(2));
93                         }
94
95                         parsedData.setLocation(location);
96                     }
97                 }
98             }
99
100             for (SenseBoxSensor sensor : parsedData.getSensors()) {
101                 if ("VEML6070".equals(sensor.getSensorType())) {
102                     // "unit" is not nicely comparable, so use sensor type for now
103                     parsedData.setUvIntensity(sensor);
104                 } else if ("SDS 011".equals(sensor.getSensorType())) {
105                     // "unit" is not nicely comparable, neither is type, so use sensor title for now
106                     if ("PM2.5".equals(sensor.getTitle())) {
107                         parsedData.setParticulateMatter2dot5(sensor);
108                     } else if ("PM10".equals(sensor.getTitle())) {
109                         parsedData.setParticulateMatter10(sensor);
110                     } else {
111                         logger.debug("SDS 011 sensor title is {}", sensor.getTitle());
112                     }
113                 } else if ("lx".equals(sensor.getUnit())) {
114                     if (sensor.getLastMeasurement() != null) {
115                         if (!(INVALID_BRIGHTNESS.equals(sensor.getLastMeasurement().getValue()))) {
116                             parsedData.setLuminance(sensor);
117                         }
118                     }
119                 } else if ("Pa".equals(sensor.getUnit()) || "hPa".equals(sensor.getUnit())) {
120                     parsedData.setPressure(sensor);
121                 } else if ("%".equals(sensor.getUnit())) {
122                     parsedData.setHumidity(sensor);
123                 } else if ("°C".equals(sensor.getUnit())) {
124                     parsedData.setTemperature(sensor);
125                 } else {
126                     if (logger.isDebugEnabled()) {
127                         logger.debug("    Sensor: {}", sensor);
128                         logger.debug("    Sensor unit: {}", sensor.getUnit());
129                         logger.debug("    Sensor type: {}", sensor.getSensorType());
130                         logger.debug("    Sensor LM: {}", sensor.getLastMeasurement());
131                         if (sensor.getLastMeasurement() != null) {
132                             logger.debug("    Sensor LM value: {}", sensor.getLastMeasurement().getValue());
133                             logger.debug("    Sensor LM date: '{}'", sensor.getLastMeasurement().getCreatedAt());
134                         }
135                     }
136                 }
137             }
138
139             SenseBoxDescriptor descriptor = new SenseBoxDescriptor();
140             descriptor.setApiUrl(query);
141             String image = parsedData.getImage();
142             if (image != null && !image.isEmpty()) {
143                 descriptor.setImageUrl(SENSEMAP_IMAGE_URL_BASE + "/" + image);
144             }
145             descriptor.setMapUrl(SENSEMAP_MAP_URL_BASE + "/explore/" + senseBoxId);
146             parsedData.setDescriptor(descriptor);
147
148             logger.trace("=================================");
149
150             result = parsedData;
151         } catch (IOException e) {
152             logger.debug("IO problems while fetching data: {} / {}", query, e.getMessage());
153             result.setStatus(ThingStatus.OFFLINE);
154         }
155
156         return result;
157     }
158 }