Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Grześlowski <martin.grzeslowski@gmail.com>
  • Loading branch information
magx2 committed Dec 15, 2023
1 parent 06fe6ee commit 0ee36bd
Show file tree
Hide file tree
Showing 26 changed files with 410 additions and 520 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
*/
package org.openhab.binding.salus.internal;

import java.util.Set;

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

import java.util.Set;

/**
* The {@link SalusBindingConstants} class defines common constants, which are
* used across the whole binding.
Expand All @@ -34,9 +34,8 @@ public class SalusBindingConstants {
public static final ThingTypeUID SALUS_IT600_DEVICE_TYPE = new ThingTypeUID(BINDING_ID, "salus-it600-device");
public static final ThingTypeUID SALUS_SERVER_TYPE = new ThingTypeUID(BINDING_ID, "salus-cloud-bridge");


public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(SALUS_DEVICE_TYPE, SALUS_IT600_DEVICE_TYPE, SALUS_SERVER_TYPE);

public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(SALUS_DEVICE_TYPE,
SALUS_IT600_DEVICE_TYPE, SALUS_SERVER_TYPE);

public static class SalusCloud {
}
Expand All @@ -50,9 +49,10 @@ public static class HoldType {
public static final int AUTO = 0;
public static final int MANUAL = 2;
public static final int TEMPORARY_MANUAL = 1;
public static final int OFF= 7;
public static final int OFF = 7;
}
}

public static class Channels {
public static class It600 {
public static final String TEMPERATURE = "temperature";
Expand All @@ -68,38 +68,21 @@ public static class It600 {
public static final String GENERIC_INPUT_NUMBER_CHANNEL = "generic-input-number-channel";
public static final String TEMPERATURE_OUTPUT_NUMBER_CHANNEL = "temperature-output-channel";
public static final String TEMPERATURE_INPUT_NUMBER_CHANNEL = "temperature-input-channel";
public static final Set<String> TEMPERATURE_CHANNELS = Set.of(
"ep_9:sIT600TH:AutoCoolingSetpoint_x100",
"ep_9:sIT600TH:AutoCoolingSetpoint_x100_a",
"ep_9:sIT600TH:AutoHeatingSetpoint_x100",
"ep_9:sIT600TH:AutoHeatingSetpoint_x100_a",
"ep_9:sIT600TH:CoolingSetpoint_x100",
"ep_9:sIT600TH:CoolingSetpoint_x100_a",
"ep_9:sIT600TH:FloorCoolingMax_x100",
"ep_9:sIT600TH:FloorCoolingMin_x100",
"ep_9:sIT600TH:FloorHeatingMax_x100",
"ep_9:sIT600TH:FloorHeatingMin_x100",
"ep_9:sIT600TH:FrostSetpoint_x100",
"ep_9:sIT600TH:HeatingSetpoint_x100",
"ep_9:sIT600TH:HeatingSetpoint_x100_a",
"ep_9:sIT600TH:LocalTemperature_x100",
"ep_9:sIT600TH:MaxCoolSetpoint_x100",
"ep_9:sIT600TH:MaxHeatSetpoint_x100",
"ep_9:sIT600TH:MaxHeatSetpoint_x100_a",
"ep_9:sIT600TH:MinCoolSetpoint_x100",
"ep_9:sIT600TH:MinCoolSetpoint_x100_a",
"ep_9:sIT600TH:MinHeatSetpoint_x100",
"ep_9:sIT600TH:PipeTemperature_x100",
"ep_9:sIT600TH:SetAutoCoolingSetpoint_x100",
"ep_9:sIT600TH:SetAutoHeatingSetpoint_x100",
"ep_9:sIT600TH:SetCoolingSetpoint_x100",
"ep_9:sIT600TH:SetFloorCoolingMin_x100",
"ep_9:sIT600TH:SetFloorHeatingMax_x100",
"ep_9:sIT600TH:SetFloorHeatingMin_x100",
"ep_9:sIT600TH:SetFrostSetpoint_x100",
"ep_9:sIT600TH:SetHeatingSetpoint_x100",
"ep_9:sIT600TH:SetMaxHeatSetpoint_x100",
"ep_9:sIT600TH:SetMinCoolSetpoint_x100"
);
public static final Set<String> TEMPERATURE_CHANNELS = Set.of("ep_9:sIT600TH:AutoCoolingSetpoint_x100",
"ep_9:sIT600TH:AutoCoolingSetpoint_x100_a", "ep_9:sIT600TH:AutoHeatingSetpoint_x100",
"ep_9:sIT600TH:AutoHeatingSetpoint_x100_a", "ep_9:sIT600TH:CoolingSetpoint_x100",
"ep_9:sIT600TH:CoolingSetpoint_x100_a", "ep_9:sIT600TH:FloorCoolingMax_x100",
"ep_9:sIT600TH:FloorCoolingMin_x100", "ep_9:sIT600TH:FloorHeatingMax_x100",
"ep_9:sIT600TH:FloorHeatingMin_x100", "ep_9:sIT600TH:FrostSetpoint_x100",
"ep_9:sIT600TH:HeatingSetpoint_x100", "ep_9:sIT600TH:HeatingSetpoint_x100_a",
"ep_9:sIT600TH:LocalTemperature_x100", "ep_9:sIT600TH:MaxCoolSetpoint_x100",
"ep_9:sIT600TH:MaxHeatSetpoint_x100", "ep_9:sIT600TH:MaxHeatSetpoint_x100_a",
"ep_9:sIT600TH:MinCoolSetpoint_x100", "ep_9:sIT600TH:MinCoolSetpoint_x100_a",
"ep_9:sIT600TH:MinHeatSetpoint_x100", "ep_9:sIT600TH:PipeTemperature_x100",
"ep_9:sIT600TH:SetAutoCoolingSetpoint_x100", "ep_9:sIT600TH:SetAutoHeatingSetpoint_x100",
"ep_9:sIT600TH:SetCoolingSetpoint_x100", "ep_9:sIT600TH:SetFloorCoolingMin_x100",
"ep_9:sIT600TH:SetFloorHeatingMax_x100", "ep_9:sIT600TH:SetFloorHeatingMin_x100",
"ep_9:sIT600TH:SetFrostSetpoint_x100", "ep_9:sIT600TH:SetHeatingSetpoint_x100",
"ep_9:sIT600TH:SetMaxHeatSetpoint_x100", "ep_9:sIT600TH:SetMinCoolSetpoint_x100");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
*/
package org.openhab.binding.salus.internal;

import static org.openhab.binding.salus.internal.SalusBindingConstants.*;

import java.util.Hashtable;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.salus.internal.handler.CloudBridgeHandler;
import org.openhab.binding.salus.internal.discovery.CloudDiscovery;
import org.openhab.binding.salus.internal.handler.CloudBridgeHandler;
import org.openhab.binding.salus.internal.handler.DeviceHandler;
import org.openhab.binding.salus.internal.handler.It600Handler;
import org.openhab.core.config.discovery.DiscoveryService;
Expand All @@ -32,10 +36,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Hashtable;

import static org.openhab.binding.salus.internal.SalusBindingConstants.*;

/**
* The {@link SalusHandlerFactory} is responsible for creating things and thing
* handlers.
Expand Down Expand Up @@ -85,6 +85,7 @@ private ThingHandler newIt600(Thing thing) {
logger.debug("Registering IT600");
return new It600Handler(thing);
}

private ThingHandler newSalusCloudBridge(Thing thing) {
logger.debug("Registering CloudBridgeHandler");
var handler = new CloudBridgeHandler((Bridge) thing, httpClientFactory);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package org.openhab.binding.salus.internal.discovery;

Check failure on line 1 in bundles/org.openhab.binding.salus/src/main/java/org/openhab/binding/salus/internal/discovery/CloudDiscovery.java

View workflow job for this annotation

GitHub Actions / Build (Java 17, ubuntu-22.04)

Header is missing

import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.openhab.binding.salus.internal.SalusBindingConstants.*;
import static org.openhab.binding.salus.internal.SalusBindingConstants.SalusDevice.DSN;

import java.util.Locale;
import java.util.Map;

import org.openhab.binding.salus.internal.handler.CloudApi;
import org.openhab.binding.salus.internal.handler.CloudBridgeHandler;
import org.openhab.binding.salus.internal.rest.Device;
Expand All @@ -11,19 +18,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Locale;
import java.util.Map;

import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.openhab.binding.salus.internal.SalusBindingConstants.*;
import static org.openhab.binding.salus.internal.SalusBindingConstants.SalusDevice.DSN;

public class CloudDiscovery extends AbstractDiscoveryService {

Check failure on line 21 in bundles/org.openhab.binding.salus/src/main/java/org/openhab/binding/salus/internal/discovery/CloudDiscovery.java

View workflow job for this annotation

GitHub Actions / Build (Java 17, ubuntu-22.04)

An author tag is missing
private final Logger logger = LoggerFactory.getLogger(CloudDiscovery.class);
private final CloudApi cloudApi;
private final ThingUID bridgeUid;

public CloudDiscovery(CloudBridgeHandler bridgeHandler, CloudApi cloudApi, ThingUID bridgeUid) throws IllegalArgumentException {
public CloudDiscovery(CloudBridgeHandler bridgeHandler, CloudApi cloudApi, ThingUID bridgeUid)
throws IllegalArgumentException {
super(SUPPORTED_THING_TYPES_UIDS, 10, true);
this.cloudApi = cloudApi;
this.bridgeUid = bridgeUid;
Expand All @@ -35,9 +36,7 @@ protected void startScan() {
try {
var devices = cloudApi.findDevices();
logger.debug("Found {} devices while scanning", devices.size());
devices.stream()
.filter(Device::isConnected)
.forEach(this::addThing);
devices.stream().filter(Device::isConnected).forEach(this::addThing);
} catch (Exception e) {
logger.error("Error while scanning", e);
}
Expand Down Expand Up @@ -65,10 +64,7 @@ private static ThingTypeUID findDeviceType(Device device) {
}

private DiscoveryResult createDiscoveryResult(ThingUID thingUID, String label, Map<String, Object> properties) {
return DiscoveryResultBuilder.create(thingUID)
.withBridge(bridgeUid)
.withProperties(properties)
.withLabel(label)
return DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUid).withProperties(properties).withLabel(label)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package org.openhab.binding.salus.internal.handler;

import org.openhab.binding.salus.internal.rest.Device;
import org.openhab.binding.salus.internal.rest.DeviceProperty;

import java.util.Optional;
import java.util.SortedSet;

import org.openhab.binding.salus.internal.rest.Device;
import org.openhab.binding.salus.internal.rest.DeviceProperty;

public interface CloudApi {
SortedSet<Device> findDevices();

Optional<Device> findDevice(String dsn);

void setValueForProperty(String dsn, String propertyName, Object value) ;
void setValueForProperty(String dsn, String propertyName, Object value);

SortedSet<DeviceProperty<?>> findPropertiesForDevice(String dsn);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
package org.openhab.binding.salus.internal.handler;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.gson.Gson;
import static java.util.Collections.emptySortedSet;
import static java.util.Objects.requireNonNull;
import static java.util.Objects.requireNonNullElse;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.openhab.core.thing.ThingStatus.OFFLINE;
import static org.openhab.core.thing.ThingStatus.ONLINE;
import static org.openhab.core.thing.ThingStatusDetail.CONFIGURATION_ERROR;
import static org.openhab.core.types.RefreshType.REFRESH;

import java.math.BigDecimal;
import java.time.Duration;
import java.util.Optional;
import java.util.SortedSet;
import java.util.concurrent.ScheduledFuture;

import org.apache.commons.lang3.StringUtils;
import org.openhab.binding.salus.internal.rest.*;
import org.openhab.core.common.ThreadPoolManager;
Expand All @@ -14,20 +26,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.time.Duration;
import java.util.Optional;
import java.util.SortedSet;
import java.util.concurrent.ScheduledFuture;

import static java.util.Collections.emptySortedSet;
import static java.util.Objects.requireNonNull;
import static java.util.Objects.requireNonNullElse;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.openhab.core.thing.ThingStatus.OFFLINE;
import static org.openhab.core.thing.ThingStatus.ONLINE;
import static org.openhab.core.thing.ThingStatusDetail.CONFIGURATION_ERROR;
import static org.openhab.core.types.RefreshType.REFRESH;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;

public final class CloudBridgeHandler extends BaseBridgeHandler implements CloudApi {
private Logger logger = LoggerFactory.getLogger(CloudBridgeHandler.class.getName());
Expand Down Expand Up @@ -78,7 +78,8 @@ private void internalInitialize() {
logger.warn("At this point SalusApi should be null!");
}
salusApi = new SalusApi(username, password, url, httpClient, GsonMapper.INSTANCE);
logger = LoggerFactory.getLogger(CloudBridgeHandler.class.getName() + "[" + username.replaceAll("\\.", "_") + "]");
logger = LoggerFactory
.getLogger(CloudBridgeHandler.class.getName() + "[" + username.replaceAll("\\.", "_") + "]");
try {
var devices = salusApi.findDevices();
} catch (Exception ex) {
Expand All @@ -87,17 +88,12 @@ private void internalInitialize() {
updateStatus(OFFLINE, CONFIGURATION_ERROR, msg + " " + ex.getMessage());
return;
}
this.devicePropertiesCache = Caffeine.newBuilder()
.maximumSize(10_000)
this.devicePropertiesCache = Caffeine.newBuilder().maximumSize(10_000)
.expireAfterWrite(Duration.ofSeconds(propertiesRefreshInterval))
.refreshAfterWrite(Duration.ofSeconds(propertiesRefreshInterval))
.build(this::loadPropertiesForDevice);
.refreshAfterWrite(Duration.ofSeconds(propertiesRefreshInterval)).build(this::loadPropertiesForDevice);
var scheduledPool = ThreadPoolManager.getScheduledPool("Salus");
this.scheduledFuture = scheduledPool.scheduleWithFixedDelay(
this::refreshCloudDevices,
refreshInterval * 2,
refreshInterval,
SECONDS);
this.scheduledFuture = scheduledPool.scheduleWithFixedDelay(this::refreshCloudDevices, refreshInterval * 2,
refreshInterval, SECONDS);

// done
updateStatus(ONLINE);
Expand Down Expand Up @@ -149,7 +145,8 @@ private void refreshCloudDevices() {
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
// no commands in this bridge
logger.debug("Bridge does not support any commands to any channels. channelUID={}, command={}", channelUID, command);
logger.debug("Bridge does not support any commands to any channels. channelUID={}, command={}", channelUID,
command);
}

@Override
Expand Down Expand Up @@ -180,33 +177,34 @@ private SortedSet<DeviceProperty<?>> loadPropertiesForDevice(String dsn) {
return response.body();
}


@Override
public void setValueForProperty(String dsn, String propertyName, Object value) {
if (salusApi == null) {
logger.error("Cannot set value for property {} on device {} because salusClient is null", propertyName, dsn);
return ;
logger.error("Cannot set value for property {} on device {} because salusClient is null", propertyName,
dsn);
return;
}
logger.debug("Setting property {} on device {} to value {} using salusClient", propertyName, dsn, value);
var response = salusApi.setValueForProperty(dsn, propertyName, value);
if (response.failed()) {
logger.error("Cannot set property {} on device {} to value {} using salusClient\n{}",
propertyName, dsn, value, response.error());
return ;
logger.error("Cannot set property {} on device {} to value {} using salusClient\n{}", propertyName, dsn,
value, response.error());
return;
}
var setValue = response.body();
if (setValue instanceof Boolean || setValue instanceof String || setValue instanceof Long || setValue instanceof Integer) {
var property = devicePropertiesCache.get(dsn)
.stream()
.filter(prop -> prop.getName().equals(propertyName))
if (setValue instanceof Boolean || setValue instanceof String || setValue instanceof Long
|| setValue instanceof Integer) {
var property = devicePropertiesCache.get(dsn).stream().filter(prop -> prop.getName().equals(propertyName))
.findFirst();
if(property.isPresent()) {
if (property.isPresent()) {
var prop = property.get();
if (setValue instanceof Boolean b && prop instanceof DeviceProperty.BooleanDeviceProperty boolProp) {
boolProp.setValue(b);
} else if (setValue instanceof String s && prop instanceof DeviceProperty.StringDeviceProperty stringProp) {
} else if (setValue instanceof String s
&& prop instanceof DeviceProperty.StringDeviceProperty stringProp) {
stringProp.setValue(s);
} else if ((setValue instanceof Long || setValue instanceof Integer) && prop instanceof DeviceProperty.LongDeviceProperty longProp) {
} else if ((setValue instanceof Long || setValue instanceof Integer)
&& prop instanceof DeviceProperty.LongDeviceProperty longProp) {
long v;
if (setValue instanceof Integer i) {
v = i.longValue();
Expand All @@ -215,16 +213,20 @@ public void setValueForProperty(String dsn, String propertyName, Object value) {
}
longProp.setValue(v);
} else {
logger.warn("Cannot set value {} ({}) for property {} ({}) on device {} because value class does not match property class",
setValue, setValue.getClass().getSimpleName(), propertyName, prop.getClass().getSimpleName(), dsn);
logger.warn(
"Cannot set value {} ({}) for property {} ({}) on device {} because value class does not match property class",
setValue, setValue.getClass().getSimpleName(), propertyName,
prop.getClass().getSimpleName(), dsn);
}
} else {
logger.warn("Cannot set value {} ({}) for property {} on device {} because it is not found in the cache. Invalidating cache",
logger.warn(
"Cannot set value {} ({}) for property {} on device {} because it is not found in the cache. Invalidating cache",
setValue, setValue.getClass().getSimpleName(), propertyName, dsn);
devicePropertiesCache.invalidate(dsn);
}
} else {
logger.warn("Cannot set value {} ({}) for property {} on device {} because it is not a Boolean, String, Long or Integer",
logger.warn(
"Cannot set value {} ({}) for property {} on device {} because it is not a Boolean, String, Long or Integer",
setValue, setValue.getClass().getSimpleName(), propertyName, dsn);
}
}
Expand All @@ -247,9 +249,6 @@ public SortedSet<Device> findDevices() {

@Override
public Optional<Device> findDevice(String dsn) {
return findDevices()
.stream()
.filter(device -> device.dsn().equals(dsn))
.findFirst();
return findDevices().stream().filter(device -> device.dsn().equals(dsn)).findFirst();
}
}
Loading

0 comments on commit 0ee36bd

Please sign in to comment.