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