Skip to content

Commit

Permalink
[melcloud] Improve null handling (openhab#17295)
Browse files Browse the repository at this point in the history
* Add null annotations

Signed-off-by: Leo Siepel <leosiepel@gmail.com>
  • Loading branch information
lsiepel authored and joni1993 committed Oct 15, 2024
1 parent 57af593 commit 5ae1e7e
Show file tree
Hide file tree
Showing 25 changed files with 154 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.ThingTypeUID;

/**
Expand All @@ -26,6 +27,7 @@
* @author Luca Calcaterra - Initial contribution
* @author Wietse van Buitenen - Added heatpump device
*/
@NonNullByDefault
public class MelCloudBindingConstants {

private static final String BINDING_ID = "melcloud";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import static org.openhab.binding.melcloud.internal.MelCloudBindingConstants.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.melcloud.internal.handler.MelCloudAccountHandler;
import org.openhab.binding.melcloud.internal.handler.MelCloudDeviceHandler;
Expand All @@ -33,6 +34,7 @@
* @author Luca Calcaterra - Initial contribution
* @author Wietse van Buitenen - Added heatpump device
*/
@NonNullByDefault
@Component(configurationPid = "binding.melcloud", service = ThingHandlerFactory.class)
public class MelCloudHandlerFactory extends BaseThingHandlerFactory {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Properties;

import org.openhab.binding.melcloud.internal.api.json.Device;
import org.openhab.binding.melcloud.internal.api.json.DeviceStatus;
import org.openhab.binding.melcloud.internal.api.json.HeatpumpDeviceStatus;
import org.openhab.binding.melcloud.internal.api.json.ListDevicesResponse;
import org.openhab.binding.melcloud.internal.api.json.LoginClientResponse;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.melcloud.internal.api.dto.Device;
import org.openhab.binding.melcloud.internal.api.dto.DeviceStatus;
import org.openhab.binding.melcloud.internal.api.dto.HeatpumpDeviceStatus;
import org.openhab.binding.melcloud.internal.api.dto.ListDevicesResponse;
import org.openhab.binding.melcloud.internal.api.dto.LoginClientResponse;
import org.openhab.binding.melcloud.internal.exceptions.MelCloudCommException;
import org.openhab.binding.melcloud.internal.exceptions.MelCloudLoginException;
import org.openhab.core.io.net.http.HttpUtil;
Expand All @@ -45,6 +47,7 @@
* @author Pauli Anttila - Refactoring
* @author Wietse van Buitenen - Return all devices, added heatpump device
*/
@NonNullByDefault
public class MelCloudConnection {

private static final String LOGIN_URL = "https://app.melcloud.com/Mitsubishi.Wifi.Client/Login/ClientLogin";
Expand All @@ -54,18 +57,18 @@ public class MelCloudConnection {
private static final int TIMEOUT_MILLISECONDS = 10000;

// Gson objects are safe to share across threads and are somewhat expensive to construct. Use a single instance.
private static final Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
private static final Gson GSON = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();

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

private boolean isConnected = false;
private String sessionKey;
private String sessionKey = "";

public void login(String username, String password, int languageId)
throws MelCloudCommException, MelCloudLoginException {
setConnected(false);
sessionKey = null;
sessionKey = "";
JsonObject jsonReq = new JsonObject();
jsonReq.addProperty("Email", username);
jsonReq.addProperty("Password", password);
Expand All @@ -79,7 +82,7 @@ public void login(String username, String password, int languageId)
String loginResponse = HttpUtil.executeUrl("POST", LOGIN_URL, null, data, "application/json",
TIMEOUT_MILLISECONDS);
logger.debug("Login response: {}", loginResponse);
LoginClientResponse resp = gson.fromJson(loginResponse, LoginClientResponse.class);
LoginClientResponse resp = Objects.requireNonNull(GSON.fromJson(loginResponse, LoginClientResponse.class));
if (resp.getErrorId() != null) {
String errorMsg = String.format("Login failed, error code: %s", resp.getErrorId());
if (resp.getErrorMessage() != null) {
Expand All @@ -101,7 +104,7 @@ public List<Device> fetchDeviceList() throws MelCloudCommException {
TIMEOUT_MILLISECONDS);
logger.debug("Device list response: {}", response);
List<Device> devices = new ArrayList<>();
ListDevicesResponse[] buildings = gson.fromJson(response, ListDevicesResponse[].class);
ListDevicesResponse[] buildings = GSON.fromJson(response, ListDevicesResponse[].class);
Arrays.asList(buildings).forEach(building -> {
if (building.getStructure().getDevices() != null) {
devices.addAll(building.getStructure().getDevices());
Expand Down Expand Up @@ -137,7 +140,7 @@ public DeviceStatus fetchDeviceStatus(int deviceId, int buildingId) throws MelCl
try {
String response = HttpUtil.executeUrl("GET", url, getHeaderProperties(), null, null, TIMEOUT_MILLISECONDS);
logger.debug("Device status response: {}", response);
return gson.fromJson(response, DeviceStatus.class);
return Objects.requireNonNull(GSON.fromJson(response, DeviceStatus.class));
} catch (IOException | JsonSyntaxException e) {
setConnected(false);
throw new MelCloudCommException("Error occurred during device status fetch", e);
Expand All @@ -146,14 +149,14 @@ public DeviceStatus fetchDeviceStatus(int deviceId, int buildingId) throws MelCl

public DeviceStatus sendDeviceStatus(DeviceStatus deviceStatus) throws MelCloudCommException {
assertConnected();
String content = gson.toJson(deviceStatus, DeviceStatus.class);
String content = GSON.toJson(deviceStatus, DeviceStatus.class);
logger.debug("Sending device status: {}", content);
InputStream data = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
try {
String response = HttpUtil.executeUrl("POST", DEVICE_URL + "/SetAta", getHeaderProperties(), data,
"application/json", TIMEOUT_MILLISECONDS);
logger.debug("Device status sending response: {}", response);
return gson.fromJson(response, DeviceStatus.class);
return Objects.requireNonNull(GSON.fromJson(response, DeviceStatus.class));
} catch (IOException | JsonSyntaxException e) {
setConnected(false);
throw new MelCloudCommException("Error occurred during device command sending", e);
Expand All @@ -166,7 +169,7 @@ public HeatpumpDeviceStatus fetchHeatpumpDeviceStatus(int deviceId, int building
try {
String response = HttpUtil.executeUrl("GET", url, getHeaderProperties(), null, null, TIMEOUT_MILLISECONDS);
logger.debug("Device heatpump status response: {}", response);
return gson.fromJson(response, HeatpumpDeviceStatus.class);
return Objects.requireNonNull(GSON.fromJson(response, HeatpumpDeviceStatus.class));
} catch (IOException | JsonSyntaxException e) {
setConnected(false);
throw new MelCloudCommException("Error occurred during heatpump device status fetch", e);
Expand All @@ -176,14 +179,14 @@ public HeatpumpDeviceStatus fetchHeatpumpDeviceStatus(int deviceId, int building
public HeatpumpDeviceStatus sendHeatpumpDeviceStatus(HeatpumpDeviceStatus heatpumpDeviceStatus)
throws MelCloudCommException {
assertConnected();
String content = gson.toJson(heatpumpDeviceStatus, HeatpumpDeviceStatus.class);
String content = GSON.toJson(heatpumpDeviceStatus, HeatpumpDeviceStatus.class);
logger.debug("Sending heatpump device status: {}", content);
InputStream data = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
try {
String response = HttpUtil.executeUrl("POST", DEVICE_URL + "/SetAtw", getHeaderProperties(), data,
"application/json", TIMEOUT_MILLISECONDS);
logger.debug("Device heatpump status sending response: {}", response);
return gson.fromJson(response, HeatpumpDeviceStatus.class);
return Objects.requireNonNull(GSON.fromJson(response, HeatpumpDeviceStatus.class));
} catch (IOException | JsonSyntaxException e) {
setConnected(false);
throw new MelCloudCommException("Error occurred during heatpump device command sending", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.melcloud.internal.api.json;
package org.openhab.binding.melcloud.internal.api.dto;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.melcloud.internal.api.json;
package org.openhab.binding.melcloud.internal.api.dto;

import java.security.Permissions;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.melcloud.internal.api.json;
package org.openhab.binding.melcloud.internal.api.dto;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.melcloud.internal.api.json;
package org.openhab.binding.melcloud.internal.api.dto;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.melcloud.internal.api.json;
package org.openhab.binding.melcloud.internal.api.dto;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.melcloud.internal.api.json;
package org.openhab.binding.melcloud.internal.api.dto;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.melcloud.internal.api.json;
package org.openhab.binding.melcloud.internal.api.dto;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.melcloud.internal.api.json;
package org.openhab.binding.melcloud.internal.api.dto;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.melcloud.internal.api.json;
package org.openhab.binding.melcloud.internal.api.dto;

import com.google.gson.annotations.Expose;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.melcloud.internal.api.json;
package org.openhab.binding.melcloud.internal.api.dto;

import com.google.gson.annotations.Expose;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.melcloud.internal.api.json;
package org.openhab.binding.melcloud.internal.api.dto;

import com.google.gson.annotations.Expose;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.melcloud.internal.api.json;
package org.openhab.binding.melcloud.internal.api.dto;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.melcloud.internal.api.json;
package org.openhab.binding.melcloud.internal.api.dto;

import com.google.gson.annotations.Expose;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@
*/
package org.openhab.binding.melcloud.internal.config;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* Config class for an A.C. device.
*
* @author Pauli Anttila - Initial Contribution
*
*/
@NonNullByDefault
public class AcDeviceConfig {

public Integer deviceID;
public Integer buildingID;
public Integer pollingInterval;
public Integer deviceID = 0;
public @Nullable Integer buildingID;
public Integer pollingInterval = 360;

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@
*/
package org.openhab.binding.melcloud.internal.config;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* Config class for MELCloud account parameters.
*
* @author Pauli Anttila - Initial Contribution
*
*/
@NonNullByDefault
public class AccountConfig {

public String username;
public String password;
public int language;
public String username = "";
public String password = "";
public int language = 0;

@Override
public String toString() {
return "[username=" + username + ", password=" + getPasswordForPrinting() + ", languageId=" + language + "]";
}

private String getPasswordForPrinting() {
if (password != null) {
return password.isEmpty() ? "<empty>" : "*********";
}
return "<null>";
return password.isEmpty() ? "<empty>" : "*********";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
*/
package org.openhab.binding.melcloud.internal.config;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* Config class for a Heatpump device.
*
* @author Wietse van Buitenen - Initial Contribution
*
*/
@NonNullByDefault
public class HeatpumpDeviceConfig {
public Integer deviceID;
public Integer buildingID;
public Integer pollingInterval;
public Integer deviceID = 0;
public @Nullable Integer buildingID;
public Integer pollingInterval = 360;

@Override
public String toString() {
Expand Down
Loading

0 comments on commit 5ae1e7e

Please sign in to comment.