Skip to content

Commit

Permalink
Minor changes in code showing how code could be organized.
Browse files Browse the repository at this point in the history
Signed-off-by: Łukasz Dywicki <luke@code-house.org>
  • Loading branch information
splatch committed Jan 21, 2017
1 parent 83b315c commit b913e96
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 48 deletions.
Expand Up @@ -44,5 +44,8 @@ public class AirQualityBindingConstants {
public final static String HUMIDITY = "humidity";

public final static Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = ImmutableSet.of(THING_TYPE_AQI);
public final static Set<String> SUPPORTED_CHANNEL_IDS = ImmutableSet.of(AQI, AQIDESCRIPTION, PM25, PM10, O3, NO2, CO,
LOCATIONNAME, LOCATIONURL, OBSERVATIONTIME, STATIONID, TEMPERATURE, PRESSURE, HUMIDITY
);

}
Expand Up @@ -33,8 +33,10 @@
import org.eclipse.smarthome.core.types.RefreshType;
import org.eclipse.smarthome.core.types.State;
import org.eclipse.smarthome.core.types.UnDefType;
import org.openhab.binding.airquality.AirQualityBindingConstants;
import org.openhab.binding.airquality.internal.AirQualityConfiguration;
import org.openhab.binding.airquality.internal.AirQualityJsonResponse;
import org.openhab.binding.airquality.internal.json.AirQualityJsonData;
import org.openhab.binding.airquality.internal.json.AirQualityJsonResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -55,8 +57,6 @@ public class AirQualityHandler extends BaseThingHandler {

private static final int DEFAULT_REFRESH_PERIOD = 30;

private AirQualityJsonResponse aqiData = null;

private ScheduledFuture<?> refreshJob;

private Gson gson;
Expand Down Expand Up @@ -110,11 +110,11 @@ private void startAutomaticRefresh() {
public void run() {
try {
// Request new air quality data to the aqicn.org service
updateAirQualityData();
AirQualityJsonData aqiData = updateAirQualityData();

// Update all channels from the updated AQI data
for (Channel channel : getThing().getChannels()) {
updateChannel(channel.getUID().getId());
updateChannel(channel.getUID().getId(), aqiData);
}
} catch (Exception e) {
logger.error("Exception occurred during execution: {}", e.getMessage());
Expand Down Expand Up @@ -152,7 +152,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
*
* @param channelId the id identifying the channel to be updated
*/
private void updateChannel(String channelId) {
private void updateChannel(String channelId, AirQualityJsonData aqiData) {
if (isLinked(channelId)) {
Object value;
try {
Expand Down Expand Up @@ -190,10 +190,9 @@ private void updateChannel(String channelId) {
*
* @return {boolean}
*/
private synchronized boolean updateAirQualityData() {
private AirQualityJsonData updateAirQualityData() {
AirQualityConfiguration config = getConfigAs(AirQualityConfiguration.class);
aqiData = getAirQualityData(StringUtils.trimToEmpty(config.location));
return aqiData != null;
return getAirQualityData(StringUtils.trimToEmpty(config.location));
}

/**
Expand Down Expand Up @@ -267,26 +266,26 @@ private AirQualityJsonResponse getAirQualityData(String location) {
return resultOk ? result : null;
}

public static Object getValue(String channelId, Object data) throws Exception {
public static Object getValue(String channelId, AirQualityJsonData data) throws Exception {
String[] fields = StringUtils.split(channelId, "#");
return getValue(data, fields, 0);
}

/**
* Iterates through the fields and returns the getter value.
*/
@SuppressWarnings("all")
private static Object getValue(Object data, String[] fields, int index) throws Exception {
if (data == null) {
return null;
}
String fieldName = fields[index];
Method method = data.getClass().getMethod(toGetterString(fieldName), null);
Object result = method.invoke(data, (Object[]) null);
if (++index < fields.length) {
result = getValue(result, fields, index);

String fieldName = fields[0];

switch (fieldName) {
case AirQualityBindingConstants.PM25:
return data.getIaqi().getPm25();
case AirQualityBindingConstants.PM10:
return data.getIaqi().getPm10();
case AirQualityBindingConstants.CO:
return data.getIaqi().getCo();
// and so on...
}
return result;

return null;
}

/**
Expand Down
Expand Up @@ -6,7 +6,7 @@
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.airquality.internal;
package org.openhab.binding.airquality.internal.json;

/**
* The {@link AirQualityJsonCity} is responsible for storing
Expand Down
Expand Up @@ -6,12 +6,11 @@
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.airquality.internal;
package org.openhab.binding.airquality.internal.json;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonArray;

/**
* The {@link AirQualityJsonData} is responsible for storing
Expand All @@ -25,7 +24,7 @@ public class AirQualityJsonData {
private int aqi;
private AirQualityJsonTime time;
private AirQualityJsonCity city;
private JsonArray attributions;
private List<Attribute> attributions;
private AirQualityJsonIaqi iaqi;

public AirQualityJsonData() {
Expand Down Expand Up @@ -77,7 +76,7 @@ public AirQualityJsonCity getCity() {
public String getAttributions() {
List<String> list = new ArrayList<String>();
for (int i = 0; i < attributions.size(); i++) {
list.add(attributions.get(i).getAsJsonObject().get("name").getAsString());
list.add(attributions.get(i).getName());
}
return "Attributions: " + String.join(", ", list);
}
Expand Down
Expand Up @@ -6,9 +6,9 @@
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.airquality.internal;
package org.openhab.binding.airquality.internal.json;

import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;

/**
* The {@link AirQualityJsonIaqi} is responsible for storing
Expand All @@ -20,49 +20,51 @@
*/
public class AirQualityJsonIaqi {

private JsonObject pm25;
private JsonObject pm10;
private JsonObject o3;
private JsonObject no2;
private JsonObject co;
private JsonObject t;
private JsonObject p;
private JsonObject h;
private AirQualityValue<Integer> pm25;
private AirQualityValue<Integer> pm10;
private AirQualityValue<Integer> o3;
private AirQualityValue<Integer> no2;
private AirQualityValue<Integer> co;
private AirQualityValue<Integer> t;

@SerializedName("p")
private AirQualityValue<Integer> pressure;
private AirQualityValue<Integer> h;

public AirQualityJsonIaqi() {

}

public int getPm25() {
return pm25.get("v").getAsInt();
return pm25.getValue();
}

public int getPm10() {
return pm10.get("v").getAsInt();
return pm10.getValue();
}

public int getO3() {
return o3.get("v").getAsInt();
return o3.getValue();
}

public int getNo2() {
return no2.get("v").getAsInt();
return no2.getValue();
}

public int getCo() {
return co.get("v").getAsInt();
return co.getValue();
}

public int getT() {
return t.get("v").getAsInt();
return t.getValue();
}

public int getP() {
return p.get("v").getAsInt();
return pressure.getValue();
}

public int getH() {
return h.get("v").getAsInt();
return h.getValue();
}

}
Expand Up @@ -6,7 +6,7 @@
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.airquality.internal;
package org.openhab.binding.airquality.internal.json;

import java.util.Calendar;

Expand Down
Expand Up @@ -6,7 +6,7 @@
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.airquality.internal;
package org.openhab.binding.airquality.internal.json;

import java.text.SimpleDateFormat;
import java.util.Calendar;
Expand Down
@@ -0,0 +1,20 @@
package org.openhab.binding.airquality.internal.json;

import com.google.gson.annotations.SerializedName;

/**
* Wrapper type around values reported by aqicn index values.
*/
public class AirQualityValue<T extends Number> {

@SerializedName("v")
private T value;

public void setValue(T value) {
this.value = value;
}

public T getValue() {
return value;
}
}
@@ -0,0 +1,18 @@
package org.openhab.binding.airquality.internal.json;

/**
* Attribute representation.
*/
public class Attribute {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

0 comments on commit b913e96

Please sign in to comment.