Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[avmfritz] Fixed NPE if temperature, powermeter or switch model is null #1887

Merged
merged 1 commit into from
May 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public void initialize() {
public void dispose() {
logger.debug("Handler disposed.");
if (pollingJob != null && !pollingJob.isCancelled()) {
logger.debug("stop polling job");
pollingJob.cancel(true);
pollingJob = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public interface IFritzHandler {
* Called from {@link FritzAhaUpdateXmlCallback} to provide new values for
* things.
*
* @param model
* Device model with updated data.
* @param model Device model with updated data.
*/
public void addDeviceList(DeviceModel model);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ public class DeviceModel {
@XmlElement(name = "name")
private String name;

@XmlElement(name = "switch")
private SwitchModel switchModel;

@XmlElement(name = "powermeter")
private PowerMeterModel powermeterModel;

@XmlElement(name = "temperature")
private TemperatureModel temperatureModel;

@XmlElement(name = "hkr")
private HeatingModel heatingModel;

public PowerMeterModel getPowermeter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public void startScan() {
*/
@Override
protected synchronized void stopScan() {
logger.debug("stop scan on bridge {}", bridgeHandler.getThing().getUID());
super.stopScan();
removeOlderResults(getTimestampOfLastScan());
}
Expand Down Expand Up @@ -140,6 +141,7 @@ protected void startBackgroundDiscovery() {
@Override
protected void stopBackgroundDiscovery() {
if (scanningJob != null && !scanningJob.isCancelled()) {
logger.debug("stop background scanning job");
scanningJob.cancel(false);
scanningJob = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* {@link UpnpDiscoveryService}.
*
* @author Robert Bausdorf - Initial contribution
*
*/
public class AvmUpnpDiscoveryParticipant implements UpnpDiscoveryParticipant {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.openhab.binding.avmfritz.internal.ahamodel.DeviceModel;
import org.openhab.binding.avmfritz.internal.ahamodel.DevicelistModel;
import org.openhab.binding.avmfritz.internal.discovery.AvmDiscoveryService;
import org.openhab.binding.avmfritz.internal.hardware.FritzahaWebInterface;
import org.openhab.binding.avmfritz.util.JAXBtUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -30,8 +30,12 @@
*
*/
public class FritzAhaDiscoveryCallback extends FritzAhaReauthCallback {
private final Logger logger = LoggerFactory.getLogger(getClass());

private final Logger logger = LoggerFactory.getLogger(FritzAhaDiscoveryCallback.class);

/**
* Handler to update
*/
private AvmDiscoveryService service;

/**
Expand All @@ -51,23 +55,23 @@ public FritzAhaDiscoveryCallback(FritzahaWebInterface webIface, AvmDiscoveryServ
@Override
public void execute(int status, String response) {
super.execute(status, response);
if (this.isValidRequest()) {
logger.debug("discovery callback response {}", response);
logger.trace("Received discovery callback response: {}", response);
if (isValidRequest()) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(DevicelistModel.class);
Unmarshaller jaxbUM = jaxbContext.createUnmarshaller();

DevicelistModel model = (DevicelistModel) jaxbUM.unmarshal(new StringReader(response));
final Unmarshaller jaxbUnmarshaller = JAXBtUtils.JAXBCONTEXT.createUnmarshaller();
final DevicelistModel model = (DevicelistModel) jaxbUnmarshaller.unmarshal(new StringReader(response));
if (model != null) {
for (DeviceModel device : model.getDevicelist()) {
this.service.onDeviceAddedInternal(device);
for (final DeviceModel device : model.getDevicelist()) {
service.onDeviceAddedInternal(device);
}
} else {
logger.warn("no model in response");
}
} catch (JAXBException e) {
logger.error("{}", e.getLocalizedMessage(), e);
logger.error("Exception creating Unmarshaller: {}", e.getLocalizedMessage(), e);
}
} else {
logger.info("request is invalid: {}", status);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

Expand All @@ -20,6 +19,7 @@
import org.openhab.binding.avmfritz.internal.ahamodel.DeviceModel;
import org.openhab.binding.avmfritz.internal.ahamodel.DevicelistModel;
import org.openhab.binding.avmfritz.internal.hardware.FritzahaWebInterface;
import org.openhab.binding.avmfritz.util.JAXBtUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -28,13 +28,12 @@
* response. Supports reauthorization.
*
* @author Robert Bausdorf
*
* @author Christoph Weitkamp
*
*/
public class FritzAhaUpdateXmlCallback extends FritzAhaReauthCallback {
/**
* logger
*/
private final Logger logger = LoggerFactory.getLogger(getClass());

private final Logger logger = LoggerFactory.getLogger(FritzAhaUpdateXmlCallback.class);

/**
* Handler to update
Expand All @@ -43,9 +42,9 @@ public class FritzAhaUpdateXmlCallback extends FritzAhaReauthCallback {

/**
* Constructor
*
*
* @param webIface Webinterface to FRITZ!Box
* @param handler Bridge handler taht will update things.
* @param handler Bridge handler that will update things.
*/
public FritzAhaUpdateXmlCallback(FritzahaWebInterface webIface, IFritzHandler handler) {
super(WEBSERVICE_PATH, "switchcmd=getdevicelistinfos", webIface, Method.GET, 1);
Expand All @@ -58,23 +57,21 @@ public FritzAhaUpdateXmlCallback(FritzahaWebInterface webIface, IFritzHandler ha
@Override
public void execute(int status, String response) {
super.execute(status, response);
if (this.isValidRequest()) {
logger.trace("Received State response {}", response);
logger.trace("Received State response {}", response);
if (isValidRequest()) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(DevicelistModel.class);
Unmarshaller jaxbUM = jaxbContext.createUnmarshaller();

DevicelistModel model = (DevicelistModel) jaxbUM.unmarshal(new StringReader(response));
final Unmarshaller jaxbUnmarshaller = JAXBtUtils.JAXBCONTEXT.createUnmarshaller();
final DevicelistModel model = (DevicelistModel) jaxbUnmarshaller.unmarshal(new StringReader(response));
if (model != null) {
for (DeviceModel device : model.getDevicelist()) {
for (final DeviceModel device : model.getDevicelist()) {
handler.addDeviceList(device);
}
handler.setStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, "FRITZ!Box online");
} else {
logger.warn("no model in response");
}
} catch (JAXBException e) {
logger.error("{}", e.getLocalizedMessage(), e);
logger.error("Exception creating Unmarshaller: {}", e.getLocalizedMessage(), e);
}
} else {
logger.info("request is invalid: {}", status);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) 2010-2017 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.avmfritz.util;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import org.openhab.binding.avmfritz.internal.ahamodel.DevicelistModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Implementation for a static use of JAXBContext as singleton instance.
*
* @author Christoph Weitkamp
*
*/
public class JAXBtUtils {

private static final Logger logger = LoggerFactory.getLogger(JAXBtUtils.class);

public static final JAXBContext JAXBCONTEXT = initJAXBContext();

private static JAXBContext initJAXBContext() {
JAXBContext jaxbContext = null;
try {
jaxbContext = JAXBContext.newInstance(DevicelistModel.class);
} catch (JAXBException e) {
logger.error("Exception creating JAXBContext: {}", e.getMessage(), e);
jaxbContext = null;
}
return jaxbContext;
}
}