2 * Copyright (c) 2010-2020 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.sensebox.internal;
15 import static org.openhab.binding.sensebox.internal.SenseBoxBindingConstants.*;
17 import java.io.IOException;
18 import java.util.List;
19 import java.util.Properties;
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;
34 import com.google.gson.Gson;
37 * The {@link SenseBoxAPIConnection} is responsible for fetching data from the senseBox API server.
39 * @author Hakan Tandogan - Initial contribution
42 public class SenseBoxAPIConnection {
44 private final Logger logger = LoggerFactory.getLogger(SenseBoxAPIConnection.class);
46 private final Gson gson = new Gson();
48 private static final Properties HEADERS = new Properties();
50 private static final String METHOD = "GET";
52 private static final int TIMEOUT = 30 * 1000; // 30 seconds
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);
60 public SenseBoxData reallyFetchDataFromServer(String senseBoxId) {
61 String query = SENSEMAP_API_URL_BASE + "/boxes/" + senseBoxId;
63 // the caching layer does not like null values
64 SenseBoxData result = new SenseBoxData();
67 String body = HttpUtil.executeUrl(METHOD, query, HEADERS, null, null, TIMEOUT);
69 logger.trace("Fetched Data: {}", body);
70 SenseBoxData parsedData = gson.fromJson(body, SenseBoxData.class);
72 // Assume all is well at first
73 parsedData.setStatus(ThingStatus.ONLINE);
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();
83 if (locationData.size() > 0) {
84 location.setLongitude(locationData.get(0));
87 if (locationData.size() > 1) {
88 location.setLatitude(locationData.get(1));
91 if (locationData.size() > 2) {
92 location.setHeight(locationData.get(2));
95 parsedData.setLocation(location);
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);
111 logger.debug("SDS 011 sensor title is {}", sensor.getTitle());
113 } else if ("lx".equals(sensor.getUnit())) {
114 if (sensor.getLastMeasurement() != null) {
115 if (!(INVALID_BRIGHTNESS.equals(sensor.getLastMeasurement().getValue()))) {
116 parsedData.setLuminance(sensor);
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);
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());
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);
145 descriptor.setMapUrl(SENSEMAP_MAP_URL_BASE + "/explore/" + senseBoxId);
146 parsedData.setDescriptor(descriptor);
148 logger.trace("=================================");
151 } catch (IOException e) {
152 logger.debug("IO problems while fetching data: {} / {}", query, e.getMessage());
153 result.setStatus(ThingStatus.OFFLINE);