use of org.openhab.binding.meteoblue.internal.Forecast in project openhab-addons by openhab.
the class MeteoBlueHandler method updateChannel.
/**
* Update the channel from the last weather data retrieved.
*
* @param channelId the id of the channel to be updated
*/
private void updateChannel(String channelId) {
Channel channel = getThing().getChannel(channelId);
if (channel == null || !isLinked(channelId)) {
logger.trace("Channel '{}' was null or not linked! Not updated.", channelId);
return;
}
// get the set of channel parameters.
// the first will be the forecast day (eg. forecastToday),
// and the second will be the datapoint (eg. snowFraction)
String[] channelParts = channelId.split("#");
String forecastDay = channelParts[0];
String datapointName = channelParts[1];
if (channelParts.length != 2) {
logger.debug("Skipped invalid channelId '{}'", channelId);
return;
}
logger.debug("Updating channel '{}'", channelId);
Forecast forecast = getForecast(forecastDay);
if (forecast == null) {
logger.debug("No forecast found for '{}'. Not updating.", forecastDay);
return;
}
Object datapoint = forecast.getDatapoint(datapointName);
logger.debug("Value for datapoint '{}' is '{}'", datapointName, datapoint);
if (datapoint == null) {
logger.debug("Couldn't get datapoint '{}' for '{}'. Not updating.", datapointName, forecastDay);
return;
}
// Build a State from this value
State state = null;
if (datapoint instanceof Calendar) {
state = new DateTimeType(ZonedDateTime.ofInstant(((Calendar) datapoint).toInstant(), ZoneId.systemDefault()));
} else if (datapoint instanceof Integer) {
state = getStateForType(channel.getAcceptedItemType(), (Integer) datapoint);
} else if (datapoint instanceof Number) {
BigDecimal decimalValue = new BigDecimal(datapoint.toString()).setScale(2, RoundingMode.HALF_UP);
state = getStateForType(channel.getAcceptedItemType(), decimalValue);
} else if (datapoint instanceof String) {
state = new StringType(datapoint.toString());
} else if (datapoint instanceof BufferedImage) {
ImageItem item = new ImageItem("rain area");
state = new RawType(renderImage((BufferedImage) datapoint), "image/png");
item.setState(state);
} else {
logger.debug("Unsupported value type {}", datapoint.getClass().getSimpleName());
}
// Update the channel
if (state != null) {
logger.trace("Updating channel with state value {}. (object type {})", state, datapoint.getClass().getSimpleName());
updateState(channelId, state);
}
}
Aggregations