Skip to content

Commit

Permalink
[hue] Little optimization in light and sensor handlers (openhab#7512)
Browse files Browse the repository at this point in the history
- [hue] Little optimization in light and sensor handlers

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo authored and andrewfg committed Aug 31, 2020
1 parent 1c90ba7 commit 24b5aa4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.eclipse.smarthome.core.thing.binding.ThingHandlerService;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.UnDefType;
import org.openhab.binding.hue.internal.FullHueObject;
import org.openhab.binding.hue.internal.FullLight;
import org.openhab.binding.hue.internal.HueBridge;
import org.openhab.binding.hue.internal.State;
Expand Down Expand Up @@ -94,14 +93,13 @@ public class HueLightHandler extends BaseThingHandler implements LightStatusList

private static final String OSRAM_PAR16_50_TW_MODEL_ID = "PAR16_50_TW";

@NonNullByDefault({})
private String lightId;
private final Logger logger = LoggerFactory.getLogger(HueLightHandler.class);

private @NonNullByDefault({}) String lightId;

private @Nullable Integer lastSentColorTemp;
private @Nullable Integer lastSentBrightness;

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

// Flag to indicate whether the bulb is of type Osram par16 50 TW or not
private boolean isOsramPar16 = false;

Expand Down Expand Up @@ -134,11 +132,11 @@ private void initializeThing(@Nullable ThingStatus bridgeStatus) {
}

lightId = configLightId;
// note: this call implicitly registers our handler as a listener on
// the bridge
if (getHueClient() != null) {
// note: this call implicitly registers our handler as a listener on the bridge
HueClient bridgeHandler = getHueClient();
if (bridgeHandler != null) {
if (bridgeStatus == ThingStatus.ONLINE) {
initializeProperties();
initializeProperties(bridgeHandler.getLightById(lightId));
updateStatus(ThingStatus.ONLINE);
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
Expand All @@ -152,34 +150,31 @@ private void initializeThing(@Nullable ThingStatus bridgeStatus) {
}
}

private synchronized void initializeProperties() {
if (!propertiesInitializedSuccessfully) {
FullHueObject fullLight = getLight();
if (fullLight != null) {
Map<String, String> properties = editProperties();
String softwareVersion = fullLight.getSoftwareVersion();
if (softwareVersion != null) {
properties.put(PROPERTY_FIRMWARE_VERSION, softwareVersion);
}
String modelId = fullLight.getNormalizedModelID();
if (modelId != null) {
properties.put(PROPERTY_MODEL_ID, modelId);
String vendor = getVendor(modelId);
if (vendor != null) {
properties.put(PROPERTY_VENDOR, vendor);
}
} else {
properties.put(PROPERTY_VENDOR, fullLight.getManufacturerName());
}
properties.put(PRODUCT_NAME, fullLight.getProductName());
String uniqueID = fullLight.getUniqueID();
if (uniqueID != null) {
properties.put(UNIQUE_ID, uniqueID);
private synchronized void initializeProperties(@Nullable FullLight fullLight) {
if (!propertiesInitializedSuccessfully && fullLight != null) {
Map<String, String> properties = editProperties();
String softwareVersion = fullLight.getSoftwareVersion();
if (softwareVersion != null) {
properties.put(PROPERTY_FIRMWARE_VERSION, softwareVersion);
}
String modelId = fullLight.getNormalizedModelID();
if (modelId != null) {
properties.put(PROPERTY_MODEL_ID, modelId);
String vendor = getVendor(modelId);
if (vendor != null) {
properties.put(PROPERTY_VENDOR, vendor);
}
updateProperties(properties);
isOsramPar16 = OSRAM_PAR16_50_TW_MODEL_ID.equals(modelId);
propertiesInitializedSuccessfully = true;
} else {
properties.put(PROPERTY_VENDOR, fullLight.getManufacturerName());
}
properties.put(PRODUCT_NAME, fullLight.getProductName());
String uniqueID = fullLight.getUniqueID();
if (uniqueID != null) {
properties.put(UNIQUE_ID, uniqueID);
}
updateProperties(properties);
isOsramPar16 = OSRAM_PAR16_50_TW_MODEL_ID.equals(modelId);
propertiesInitializedSuccessfully = true;
}
}

Expand All @@ -205,27 +200,19 @@ public void dispose() {
}
}

private @Nullable FullLight getLight() {
HueClient bridgeHandler = getHueClient();
if (bridgeHandler != null) {
return bridgeHandler.getLightById(lightId);
}
return null;
}

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
handleCommand(channelUID.getId(), command, defaultFadeTime);
}

public void handleCommand(String channel, Command command, long fadeTime) {
HueClient hueBridge = getHueClient();
if (hueBridge == null) {
HueClient bridgeHandler = getHueClient();
if (bridgeHandler == null) {
logger.warn("hue bridge handler not found. Cannot handle command without bridge.");
return;
}

FullLight light = getLight();
FullLight light = bridgeHandler.getLightById(lightId);
if (light == null) {
logger.debug("hue light not known on bridge. Cannot handle command.");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
Expand Down Expand Up @@ -349,7 +336,7 @@ public void handleCommand(String channel, Command command, long fadeTime) {
if (tmpColorTemp != null) {
lastSentColorTemp = tmpColorTemp;
}
hueBridge.updateLightState(light, lightState);
bridgeHandler.updateLightState(light, lightState);
} else {
logger.warn("Command sent to an unknown channel id: {}:{}", getThing().getUID(), channel);
}
Expand Down Expand Up @@ -448,7 +435,7 @@ public void onLightStateChanged(@Nullable HueBridge bridge, FullLight fullLight)
return;
}

initializeProperties();
initializeProperties(fullLight);

lastSentColorTemp = null;
lastSentBrightness = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.types.Command;
import org.openhab.binding.hue.internal.FullHueObject;
import org.openhab.binding.hue.internal.FullSensor;
import org.openhab.binding.hue.internal.HueBridge;
import org.openhab.binding.hue.internal.SensorConfigUpdate;
Expand All @@ -55,10 +54,10 @@
@NonNullByDefault
public abstract class HueSensorHandler extends BaseThingHandler implements SensorStatusListener {

private @NonNullByDefault({}) String sensorId;

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

private @NonNullByDefault({}) String sensorId;

private boolean configInitializedSuccessfully;
private boolean propertiesInitializedSuccessfully;

Expand All @@ -81,9 +80,10 @@ private void initializeThing(@Nullable ThingStatus bridgeStatus) {
if (configSensorId != null) {
sensorId = configSensorId;
// note: this call implicitly registers our handler as a listener on the bridge
if (getHueClient() != null) {
HueClient bridgeHandler = getHueClient();
if (bridgeHandler != null) {
if (bridgeStatus == ThingStatus.ONLINE) {
initializeProperties();
initializeProperties(bridgeHandler.getSensorById(sensorId));
updateStatus(ThingStatus.ONLINE);
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
Expand All @@ -97,28 +97,25 @@ private void initializeThing(@Nullable ThingStatus bridgeStatus) {
}
}

private synchronized void initializeProperties() {
if (!propertiesInitializedSuccessfully) {
FullHueObject fullSensor = getSensor();
if (fullSensor != null) {
Map<String, String> properties = editProperties();
String softwareVersion = fullSensor.getSoftwareVersion();
if (softwareVersion != null) {
properties.put(PROPERTY_FIRMWARE_VERSION, softwareVersion);
}
String modelId = fullSensor.getNormalizedModelID();
if (modelId != null) {
properties.put(PROPERTY_MODEL_ID, modelId);
}
properties.put(PROPERTY_VENDOR, fullSensor.getManufacturerName());
properties.put(PRODUCT_NAME, fullSensor.getProductName());
String uniqueID = fullSensor.getUniqueID();
if (uniqueID != null) {
properties.put(UNIQUE_ID, uniqueID);
}
updateProperties(properties);
propertiesInitializedSuccessfully = true;
private synchronized void initializeProperties(@Nullable FullSensor fullSensor) {
if (!propertiesInitializedSuccessfully && fullSensor != null) {
Map<String, String> properties = editProperties();
String softwareVersion = fullSensor.getSoftwareVersion();
if (softwareVersion != null) {
properties.put(PROPERTY_FIRMWARE_VERSION, softwareVersion);
}
String modelId = fullSensor.getNormalizedModelID();
if (modelId != null) {
properties.put(PROPERTY_MODEL_ID, modelId);
}
properties.put(PROPERTY_VENDOR, fullSensor.getManufacturerName());
properties.put(PRODUCT_NAME, fullSensor.getProductName());
String uniqueID = fullSensor.getUniqueID();
if (uniqueID != null) {
properties.put(UNIQUE_ID, uniqueID);
}
updateProperties(properties);
propertiesInitializedSuccessfully = true;
}
}

Expand Down Expand Up @@ -166,21 +163,20 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}

public void handleCommand(String channel, Command command) {
// updateSensorState
FullSensor sensor = getSensor();
HueClient bridgeHandler = getHueClient();
if (bridgeHandler == null) {
logger.warn("hue bridge handler not found. Cannot handle command without bridge.");
return;
}

FullSensor sensor = bridgeHandler.getSensorById(sensorId);
if (sensor == null) {
logger.debug("hue sensor not known on bridge. Cannot handle command.");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"@text/offline.conf-error-wrong-sensor-id");
return;
}

HueClient hueBridge = getHueClient();
if (hueBridge == null) {
logger.warn("hue bridge handler not found. Cannot handle command without bridge.");
return;
}

StateUpdate sensorState = new StateUpdate();
switch (channel) {
case STATE_STATUS:
Expand All @@ -192,7 +188,7 @@ public void handleCommand(String channel, Command command) {
}

if (sensorState != null) {
hueBridge.updateSensorState(sensor, sensorState);
bridgeHandler.updateSensorState(sensor, sensorState);
} else {
logger.warn("Command sent to an unknown channel id: {}:{}", getThing().getUID(), channel);
}
Expand Down Expand Up @@ -235,7 +231,7 @@ public void onSensorStateChanged(@Nullable HueBridge bridge, FullSensor sensor)
return;
}

initializeProperties();
initializeProperties(sensor);

if (Boolean.TRUE.equals(sensor.getConfig().get(CONFIG_REACHABLE))) {
updateStatus(ThingStatus.ONLINE);
Expand Down

0 comments on commit 24b5aa4

Please sign in to comment.