use of org.openhab.binding.netatmo.internal.weather.GetStationsDataResponse.Module in project openhab1-addons by openhab.
the class NetatmoWeatherBinding method execute.
/**
* Execute the weather binding from Netatmo Binding Class
*
* @param oauthCredentials
* @param providers
* @param eventPublisher
*/
public void execute(OAuthCredentials oauthCredentials, Collection<NetatmoBindingProvider> providers, EventPublisher eventPublisher) {
logger.debug("Querying Netatmo Weather API");
try {
GetStationsDataResponse stationsDataResponse = processGetStationsData(oauthCredentials, providers, eventPublisher);
if (stationsDataResponse == null) {
return;
}
DeviceMeasureValueMap deviceMeasureValueMap = processMeasurements(oauthCredentials, providers, eventPublisher);
if (deviceMeasureValueMap == null) {
return;
}
for (final NetatmoBindingProvider provider : providers) {
for (final String itemName : provider.getItemNames()) {
final String deviceId = provider.getDeviceId(itemName);
final String moduleId = provider.getModuleId(itemName);
final NetatmoMeasureType measureType = provider.getMeasureType(itemName);
final NetatmoScale scale = provider.getNetatmoScale(itemName);
State state = null;
if (measureType != null) {
switch(measureType) {
case MODULENAME:
if (moduleId == null) {
for (Device device : stationsDataResponse.getDevices()) {
if (device.getId().equals(deviceId)) {
state = new StringType(device.getModuleName());
break;
}
}
} else {
for (Device device : stationsDataResponse.getDevices()) {
for (Module module : device.getModules()) {
if (module.getId().equals(moduleId)) {
state = new StringType(module.getModuleName());
break;
}
}
}
}
break;
case TIMESTAMP:
state = deviceMeasureValueMap.timeStamp;
break;
case TEMPERATURE:
case CO2:
case HUMIDITY:
case NOISE:
case PRESSURE:
case RAIN:
case MIN_TEMP:
case MAX_TEMP:
case MIN_HUM:
case MAX_HUM:
case MIN_PRESSURE:
case MAX_PRESSURE:
case MIN_NOISE:
case MAX_NOISE:
case MIN_CO2:
case MAX_CO2:
case SUM_RAIN:
case WINDSTRENGTH:
case WINDANGLE:
case GUSTSTRENGTH:
case GUSTANGLE:
{
BigDecimal value = getValue(deviceMeasureValueMap, measureType, createKey(deviceId, moduleId, scale));
// numeric value is awaited (issue #1848)
if (value != null) {
if (NetatmoMeasureType.isTemperature(measureType)) {
value = unitSystem.convertTemp(value);
} else if (NetatmoMeasureType.isRain(measureType)) {
value = unitSystem.convertRain(value);
} else if (NetatmoMeasureType.isPressure(measureType)) {
value = pressureUnit.convertPressure(value);
} else if (NetatmoMeasureType.isWind(measureType)) {
value = unitSystem.convertWind(value);
}
state = new DecimalType(value);
}
}
break;
case DATE_MIN_TEMP:
case DATE_MAX_TEMP:
case DATE_MIN_HUM:
case DATE_MAX_HUM:
case DATE_MIN_PRESSURE:
case DATE_MAX_PRESSURE:
case DATE_MIN_NOISE:
case DATE_MAX_NOISE:
case DATE_MIN_CO2:
case DATE_MAX_CO2:
case DATE_MAX_GUST:
{
final BigDecimal value = getValue(deviceMeasureValueMap, measureType, createKey(deviceId, moduleId, scale));
if (value != null) {
final Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(value.longValue() * 1000);
state = new DateTimeType(calendar);
}
}
break;
case BATTERYPERCENT:
case BATTERYSTATUS:
case BATTERYVP:
case RFSTATUS:
for (Device device : stationsDataResponse.getDevices()) {
for (Module module : device.getModules()) {
if (module.getId().equals(moduleId)) {
switch(measureType) {
case BATTERYPERCENT:
case BATTERYVP:
state = new DecimalType(module.getBatteryPercentage());
break;
case BATTERYSTATUS:
state = new DecimalType(module.getBatteryLevel());
break;
case RFSTATUS:
state = new DecimalType(module.getRfLevel());
break;
case MODULENAME:
state = new StringType(module.getModuleName());
break;
default:
break;
}
}
}
}
break;
case ALTITUDE:
case LATITUDE:
case LONGITUDE:
case WIFISTATUS:
case COORDINATE:
case STATIONNAME:
for (Device device : stationsDataResponse.getDevices()) {
if (device.getId().equals(deviceId)) {
if (stationPositions.get(device) == null) {
DecimalType altitude = DecimalType.ZERO;
if (device.getAltitude() != null) {
altitude = new DecimalType(device.getAltitude());
}
stationPositions.put(device, new PointType(new DecimalType(new BigDecimal(device.getLatitude()).setScale(6, BigDecimal.ROUND_HALF_UP)), new DecimalType(new BigDecimal(device.getLongitude()).setScale(6, BigDecimal.ROUND_HALF_UP)), altitude));
}
switch(measureType) {
case LATITUDE:
state = stationPositions.get(device).getLatitude();
break;
case LONGITUDE:
state = stationPositions.get(device).getLongitude();
break;
case ALTITUDE:
state = new DecimalType(Math.round(unitSystem.convertAltitude(stationPositions.get(device).getAltitude().doubleValue())));
break;
case WIFISTATUS:
state = new DecimalType(device.getWifiLevel());
break;
case COORDINATE:
state = stationPositions.get(device);
break;
case STATIONNAME:
state = new StringType(device.getStationName());
break;
default:
break;
}
}
}
break;
}
}
if (state != null) {
eventPublisher.postUpdate(itemName, state);
}
}
}
} catch (NetatmoException ne) {
logger.error(ne.getMessage());
}
}
use of org.openhab.binding.netatmo.internal.weather.GetStationsDataResponse.Module in project openhab1-addons by openhab.
the class NetatmoWeatherBinding method processGetStationsDataResponse.
/**
* Processes an incoming {@link GetStationsDataResponse}.
* <p>
*/
private void processGetStationsDataResponse(final GetStationsDataResponse response, Collection<NetatmoBindingProvider> providers) {
// Prepare a map of all known device measurements
final Map<String, Device> deviceMap = new HashMap<String, Device>();
final Map<String, Set<String>> deviceMeasurements = new HashMap<String, Set<String>>();
for (final Device device : response.getDevices()) {
final String deviceId = device.getId();
deviceMap.put(deviceId, device);
for (final String measurement : device.getMeasurements()) {
if (!deviceMeasurements.containsKey(deviceId)) {
deviceMeasurements.put(deviceId, new HashSet<String>());
}
deviceMeasurements.get(deviceId).add(measurement);
}
}
// Prepare a map of all known module measurements
final Map<String, Module> moduleMap = new HashMap<String, Module>();
final Map<String, Set<String>> moduleMeasurements = new HashMap<String, Set<String>>();
final Map<String, String> mainDeviceMap = new HashMap<String, String>();
for (final Device device : response.getDevices()) {
final String deviceId = device.getId();
for (final Module module : device.getModules()) {
final String moduleId = module.getId();
moduleMap.put(moduleId, module);
for (final String measurement : module.getMeasurements()) {
if (!moduleMeasurements.containsKey(moduleId)) {
moduleMeasurements.put(moduleId, new HashSet<String>());
mainDeviceMap.put(moduleId, deviceId);
}
moduleMeasurements.get(moduleId).add(measurement);
}
}
}
// Remove all configured items from the maps
for (final NetatmoBindingProvider provider : providers) {
for (final String itemName : provider.getItemNames()) {
final String deviceId = provider.getDeviceId(itemName);
final String moduleId = provider.getModuleId(itemName);
final NetatmoMeasureType measureType = provider.getMeasureType(itemName);
final Set<String> measurements;
if (moduleId != null) {
measurements = moduleMeasurements.get(moduleId);
} else {
measurements = deviceMeasurements.get(deviceId);
}
if (measurements != null) {
String measure = measureType != NetatmoMeasureType.WINDSTRENGTH ? measureType.getMeasure() : WIND;
measurements.remove(measure);
}
}
}
// Log all unconfigured measurements
final StringBuilder message = new StringBuilder();
for (Entry<String, Set<String>> entry : deviceMeasurements.entrySet()) {
final String deviceId = entry.getKey();
final Device device = deviceMap.get(deviceId);
for (String measurement : entry.getValue()) {
message.append("\t" + deviceId + "#" + measurement + " (" + device.getModuleName() + ")\n");
}
}
for (Entry<String, Set<String>> entry : moduleMeasurements.entrySet()) {
final String moduleId = entry.getKey();
final Module module = moduleMap.get(moduleId);
for (String measurement : entry.getValue()) {
if (measurement.equals(WIND)) {
measurement = NetatmoMeasureType.WINDSTRENGTH.toString().toLowerCase();
}
message.append("\t" + mainDeviceMap.get(moduleId) + "#" + moduleId + "#" + measurement + " (" + module.getModuleName() + ")\n");
}
}
if (message.length() > 0) {
message.insert(0, "The following Netatmo measurements are not yet configured:\n");
logger.info(message.toString());
}
}
use of org.openhab.binding.netatmo.internal.weather.GetStationsDataResponse.Module in project openhab1-addons by openhab.
the class GetStationsDataTest method testSuccess.
@Test
public void testSuccess() throws Exception {
final GetStationsDataRequestStub request = createRequest("/getstationsdata.json");
final GetStationsDataResponse response = request.execute();
assertFalse(response.isError());
assertEquals("access_token=" + ACCESS_TOKEN, request.getContent());
assertEquals("ok", response.getStatus());
final List<Device> devices = response.getDevices();
assertEquals(2, devices.size());
final Device device1 = devices.get(0);
final List<Module> modules1 = device1.getModules();
assertEquals(4, modules1.size());
final Device device2 = devices.get(1);
final List<Module> modules2 = device2.getModules();
assertEquals(4, modules2.size());
final Place place1 = device1.getPlace();
assertEquals(30.478512648583, place1.getAltitude());
final Place place2 = device2.getPlace();
assertEquals(150.0, place2.getAltitude());
}
Aggregations