use of org.openhab.core.library.types.DateTimeType 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.core.library.types.DateTimeType in project openhab1-addons by openhab.
the class NetatmoWeatherBinding method processMeasurementResponse.
private void processMeasurementResponse(final MeasurementRequest request, final MeasurementResponse response, DeviceMeasureValueMap deviceMeasureValueMap) {
final List<BigDecimal> values = response.getBody().get(0).getValues().get(0);
Map<String, BigDecimal> valueMap = deviceMeasureValueMap.get(request.getKey());
if (valueMap == null) {
valueMap = new HashMap<String, BigDecimal>();
deviceMeasureValueMap.put(request.getKey(), valueMap);
deviceMeasureValueMap.timeStamp = new DateTimeType(response.getTimeStamp());
}
int index = 0;
for (final String measure : request.getMeasures()) {
final BigDecimal value = values.get(index);
valueMap.put(measure, value);
index++;
}
}
use of org.openhab.core.library.types.DateTimeType in project openhab1-addons by openhab.
the class NetatmoCameraBinding 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 camera API");
try {
oauthCredentials.refreshAccessToken();
configuredHomeKnownPersons.clear();
configuredHomeUnKnownPersons.clear();
configuredHomeMap.clear();
configuredhomeCameras.clear();
processGetHomeData(oauthCredentials, providers, eventPublisher);
for (final NetatmoBindingProvider provider : providers) {
for (final String itemName : provider.getItemNames()) {
final String homeId = provider.getHomeId(itemName);
final NetatmoCameraAttributes attribute = provider.getAttribute(itemName);
State state = null;
if (homeId != null) {
final String personId = provider.getPersonId(itemName);
final String cameraId = provider.getCameraId(itemName);
if (personId != null) {
if ("UNKNOWN".equals(personId)) {
final StringBuilder message = new StringBuilder();
int i = 0;
switch(attribute) {
case HOME_UNKNWOWN_HOME_COUNT:
if (configuredHomeUnKnownPersons.containsKey(homeId)) {
for (Entry<String, Person> entry : configuredHomeUnKnownPersons.get(homeId).entrySet()) {
final Person person = entry.getValue();
if (!person.getOut_of_sight()) {
i++;
}
}
}
state = new DecimalType(i);
break;
case HOME_UNKNWOWN_AWAY_COUNT:
if (configuredHomeUnKnownPersons.containsKey(homeId)) {
for (Entry<String, Person> entry : configuredHomeUnKnownPersons.get(homeId).entrySet()) {
final Person person = entry.getValue();
if (person.getOut_of_sight()) {
i++;
}
}
}
state = new DecimalType(i);
break;
case HOME_UNKNWOWN_OUTOFSIGHT_LIST:
if (configuredHomeUnKnownPersons.containsKey(homeId)) {
for (Entry<String, Person> entry : configuredHomeUnKnownPersons.get(homeId).entrySet()) {
final Person person = entry.getValue();
if (message.length() > 1) {
message.append("#");
}
message.append(person.getOut_of_sight());
}
}
state = new StringType(message.toString());
break;
case HOME_UNKNWOWN_LASTSEEN_LIST:
if (configuredHomeUnKnownPersons.containsKey(homeId)) {
for (Entry<String, Person> entry : configuredHomeUnKnownPersons.get(homeId).entrySet()) {
final Person person = entry.getValue();
if (message.length() > 1) {
message.append("#");
}
final Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(person.getLastSeen().getTime() * 1000);
message.append(calendar);
}
}
state = new StringType(message.toString());
break;
case HOME_UNKNWOWN_FACE_ID_LIST:
if (configuredHomeUnKnownPersons.containsKey(homeId)) {
for (Entry<String, Person> entry : configuredHomeUnKnownPersons.get(homeId).entrySet()) {
final Person person = entry.getValue();
if (message.length() > 1) {
message.append("#");
}
message.append(person.getFace().getId());
}
}
state = new StringType(message.toString());
break;
case HOME_UNKNWOWN_FACE_KEY_LIST:
if (configuredHomeUnKnownPersons.containsKey(homeId)) {
for (Entry<String, Person> entry : configuredHomeUnKnownPersons.get(homeId).entrySet()) {
final Person person = entry.getValue();
if (message.length() > 1) {
message.append("#");
}
message.append(person.getFace().getKey());
}
}
state = new StringType(message.toString());
break;
default:
break;
}
} else {
Person person = null;
if (configuredHomeKnownPersons.containsKey(homeId)) {
person = configuredHomeKnownPersons.get(homeId).get(personId);
}
if (person != null) {
switch(attribute) {
case HOME_PERSON_OUTOFSIGHT:
if (person.getOut_of_sight()) {
state = OnOffType.OFF;
} else {
state = OnOffType.ON;
}
break;
case HOME_PERSON_PSEUDO:
state = new StringType(person.getPseudo());
break;
case HOME_PERSON_LASTSEEN:
final Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(person.getLastSeen().getTime() * 1000);
state = new DateTimeType(calendar);
break;
case HOME_PERSON_FACE_ID:
state = new StringType(person.getFace().getId());
break;
case HOME_PERSON_FACE_KEY:
state = new StringType(person.getFace().getKey());
break;
default:
break;
}
}
}
} else if (cameraId != null) {
Camera camera = null;
if (configuredhomeCameras.containsKey(homeId)) {
camera = configuredhomeCameras.get(homeId).get(cameraId);
}
if (camera != null) {
switch(attribute) {
case HOME_CAMERA_NAME:
state = new StringType(camera.getName());
break;
case HOME_CAMERA_STATUS:
state = new StringType(camera.getStatus());
break;
case HOME_CAMERA_SD_STATUS:
state = new StringType(camera.getSd_status());
break;
case HOME_CAMERA_ALIM_STATUS:
state = new StringType(camera.getAlim_status());
break;
default:
break;
}
}
} else {
Home home = configuredHomeMap.get(homeId);
if (home != null) {
switch(attribute) {
case HOME_NAME:
state = new StringType(home.getName());
break;
case HOME_PLACE_COUNTRY:
state = new StringType(home.getPlace().getCountry());
break;
case HOME_PLACE_TIMEZONE:
state = new StringType(home.getPlace().getTimezone());
break;
default:
break;
}
}
}
}
if (state != null) {
eventPublisher.postUpdate(itemName, state);
}
}
}
} catch (NetatmoException ne) {
logger.error(ne.getMessage());
}
}
use of org.openhab.core.library.types.DateTimeType in project openhab1-addons by openhab.
the class WemoBinding method execute.
/**
* @{inheritDoc}
*/
@Override
protected void execute() {
logger.debug("execute() method is called!");
for (WemoBindingProvider provider : providers) {
for (String itemName : provider.getItemNames()) {
logger.debug("Wemo item '{}' state will be updated", itemName);
try {
if (provider.getUDN(itemName).toLowerCase().contains("insight")) {
String insightParams = getInsightParams(itemName);
if (insightParams != null) {
String[] splitInsightParams = insightParams.split("\\|");
if (splitInsightParams[0] != null) {
if (provider.getChannelType(itemName).equals(WemoChannelType.state)) {
OnOffType binaryState = null;
binaryState = splitInsightParams[0].equals("0") ? OnOffType.OFF : OnOffType.ON;
if (binaryState != null) {
logger.trace("New InsightParam binaryState '{}' for device '{}' received", binaryState, itemName);
eventPublisher.postUpdate(itemName, binaryState);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.lastChangedAt)) {
long lastChangedAt = 0;
try {
// convert s to ms
lastChangedAt = Long.parseLong(splitInsightParams[1]) * 1000;
} catch (NumberFormatException e) {
logger.error("Unable to parse lastChangedAt value '{}' for device '{}'; expected long", splitInsightParams[1], itemName);
}
GregorianCalendar cal = new GregorianCalendar();
cal.setTimeInMillis(lastChangedAt);
State lastChangedAtState = new DateTimeType(cal);
if (lastChangedAt != 0) {
logger.trace("New InsightParam lastChangedAt '{}' for device '{}' received", lastChangedAtState, itemName);
eventPublisher.postUpdate(itemName, lastChangedAtState);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.lastOnFor)) {
State lastOnFor = DecimalType.valueOf(splitInsightParams[2]);
if (lastOnFor != null) {
logger.trace("New InsightParam lastOnFor '{}' for device '{}' received", lastOnFor, itemName);
eventPublisher.postUpdate(itemName, lastOnFor);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.onToday)) {
State onToday = DecimalType.valueOf(splitInsightParams[3]);
if (onToday != null) {
logger.trace("New InsightParam onToday '{}' for device '{}' received", onToday, itemName);
eventPublisher.postUpdate(itemName, onToday);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.onTotal)) {
State onTotal = DecimalType.valueOf(splitInsightParams[4]);
if (onTotal != null) {
logger.trace("New InsightParam onTotal '{}' for device '{}' received", onTotal, itemName);
eventPublisher.postUpdate(itemName, onTotal);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.timespan)) {
State timespan = DecimalType.valueOf(splitInsightParams[5]);
if (timespan != null) {
logger.trace("New InsightParam timespan '{}' for device '{}' received", timespan, itemName);
eventPublisher.postUpdate(itemName, timespan);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.averagePower)) {
// natively given
State averagePower = DecimalType.valueOf(splitInsightParams[6]);
// in W
if (averagePower != null) {
logger.trace("New InsightParam averagePower '{}' for device '{}' received", averagePower, itemName);
eventPublisher.postUpdate(itemName, averagePower);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.currentPower)) {
BigDecimal currentMW = new BigDecimal(splitInsightParams[7]);
State currentPower = new DecimalType(// recalculate
currentMW.divide(new BigDecimal(1000), RoundingMode.HALF_UP));
// mW to W
if (currentPower != null) {
logger.trace("New InsightParam currentPower '{}' for device '{}' received", currentPower, itemName);
eventPublisher.postUpdate(itemName, currentPower);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.energyToday)) {
BigDecimal energyTodayMWMin = new BigDecimal(splitInsightParams[8]);
// recalculate mW-mins to Wh
State energyToday = new DecimalType(energyTodayMWMin.divide(new BigDecimal(60000), RoundingMode.HALF_UP));
if (energyToday != null) {
logger.trace("New InsightParam energyToday '{}' for device '{}' received", energyToday, itemName);
eventPublisher.postUpdate(itemName, energyToday);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.energyTotal)) {
BigDecimal energyTotalMWMin = new BigDecimal(splitInsightParams[9]);
// recalculate mW-mins to Wh
State energyTotal = new DecimalType(energyTotalMWMin.divide(new BigDecimal(60000), RoundingMode.HALF_UP));
if (energyTotal != null) {
logger.trace("New InsightParam energyTotal '{}' for device '{}' received", energyTotal, itemName);
eventPublisher.postUpdate(itemName, energyTotal);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.standbyLimit)) {
BigDecimal standbyLimitMW = new BigDecimal(splitInsightParams[10]);
// recalculate mW to W
State standbyLimit = new DecimalType(standbyLimitMW.divide(new BigDecimal(1000), RoundingMode.HALF_UP));
if (standbyLimit != null) {
logger.trace("New InsightParam standbyLimit '{}' for device '{}' received", standbyLimit, itemName);
eventPublisher.postUpdate(itemName, standbyLimit);
}
}
}
}
} else {
String state = getWemoState(itemName);
if (state != null) {
if (provider.getUDN(itemName).toLowerCase().contains("motion")) {
State newState = state.equals("0") ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
eventPublisher.postUpdate(itemName, newState);
} else {
State itemState = state.equals("0") ? OnOffType.OFF : OnOffType.ON;
eventPublisher.postUpdate(itemName, itemState);
}
}
}
} catch (Exception e) {
logger.error("Error in execute method: " + e.getMessage(), e);
}
}
}
}
use of org.openhab.core.library.types.DateTimeType in project openhab1-addons by openhab.
the class KM200Comm method getVirtualState.
/**
* This function checks the virtual state of a service
*
*/
public State getVirtualState(KM200CommObject object, Class<? extends Item> itemType, String service) {
State state = null;
String type = object.getServiceType();
logger.debug("Check virtual state of: {} type: {} item: {}", service, type, itemType.getName());
switch(type) {
case "switchProgram":
KM200SwitchProgramService sPService = ((KM200SwitchProgramService) device.serviceMap.get(object.getParent()).getValueParameter());
String[] servicePath = service.split("/");
String virtService = servicePath[servicePath.length - 1];
if (virtService.equals("weekday")) {
if (itemType.isAssignableFrom(StringItem.class)) {
String val = sPService.getActiveDay();
if (val == null) {
return null;
}
state = new StringType(val);
} else {
logger.warn("Bindingtype not supported for day service: {}", itemType.getClass());
return null;
}
} else if (virtService.equals("nbrCycles")) {
if (itemType.isAssignableFrom(NumberItem.class)) {
Integer val = sPService.getNbrCycles();
if (val == null) {
return null;
}
state = new DecimalType(val);
} else {
logger.warn("Bindingtype not supported for nbrCycles service: {}", itemType.getClass());
return null;
}
} else if (virtService.equals("cycle")) {
if (itemType.isAssignableFrom(NumberItem.class)) {
Integer val = sPService.getActiveCycle();
if (val == null) {
return null;
}
state = new DecimalType(val);
} else {
logger.warn("Bindingtype not supported for cycle service: {}", itemType.getClass());
return null;
}
} else if (virtService.equals(sPService.getPositiveSwitch())) {
if (itemType.isAssignableFrom(NumberItem.class)) {
Integer val = sPService.getActivePositiveSwitch();
if (val == null) {
return null;
}
state = new DecimalType(val);
} else if (itemType.isAssignableFrom(DateTimeItem.class)) {
Integer val = sPService.getActivePositiveSwitch();
if (val == null) {
return null;
}
Calendar rightNow = Calendar.getInstance();
Integer hour = val % 60;
Integer minute = val - (hour * 60);
rightNow.set(Calendar.HOUR_OF_DAY, hour);
rightNow.set(Calendar.MINUTE, minute);
state = new DateTimeType(rightNow);
} else {
logger.warn("Bindingtype not supported for cycle service: {}", itemType.getClass());
return null;
}
} else if (virtService.equals(sPService.getNegativeSwitch())) {
if (itemType.isAssignableFrom(NumberItem.class)) {
Integer val = sPService.getActiveNegativeSwitch();
if (val == null) {
return null;
}
state = new DecimalType(val);
} else if (itemType.isAssignableFrom(DateTimeItem.class)) {
Integer val = sPService.getActiveNegativeSwitch();
if (val == null) {
return null;
}
Calendar rightNow = Calendar.getInstance();
Integer hour = val % 60;
Integer minute = val - (hour * 60);
rightNow.set(Calendar.HOUR_OF_DAY, hour);
rightNow.set(Calendar.MINUTE, minute);
state = new DateTimeType(rightNow);
} else {
logger.warn("Bindingtype not supported for cycle service: {}", itemType.getClass());
return null;
}
}
break;
case "errorList":
KM200ErrorService eService = ((KM200ErrorService) device.serviceMap.get(object.getParent()).getValueParameter());
String[] nServicePath = service.split("/");
String nVirtService = nServicePath[nServicePath.length - 1];
/* Go through the parameters and read the values */
switch(nVirtService) {
case "nbrErrors":
if (itemType.isAssignableFrom(NumberItem.class)) {
Integer val = eService.getNbrErrors();
if (val == null) {
return null;
}
state = new DecimalType(val);
} else {
logger.warn("Bindingtype not supported for error number service: {}", itemType.getClass());
return null;
}
break;
case "error":
if (itemType.isAssignableFrom(NumberItem.class)) {
Integer val = eService.getActiveError();
if (val == null) {
return null;
}
state = new DecimalType(val);
} else {
logger.warn("Bindingtype not supported for error service: {}", itemType.getClass());
return null;
}
break;
case "errorString":
if (itemType.isAssignableFrom(StringItem.class)) {
String val = eService.getErrorString();
if (val == null) {
return null;
}
state = new StringType(val);
} else {
logger.warn("Bindingtype not supported for error string service: {}", itemType.getClass());
return null;
}
break;
}
break;
}
return state;
}
Aggregations