Skip to content

Commit

Permalink
[hue] Refactored start, stop polling (#7537)
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes DerOetzi Ott <[email protected]>
  • Loading branch information
DerOetzi authored May 3, 2020
1 parent 0268f3a commit 2621ff1
Showing 1 changed file with 5 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -531,19 +531,8 @@ public void initialize() {

private synchronized void onUpdate() {
if (hueBridge != null) {
// start light and group polling only if a light handler or a group handler has been registered,
// otherwise stop polling
if (lightStatusListeners.isEmpty() && groupStatusListeners.isEmpty()) {
stopLightPolling();
} else {
startLightPolling();
}
// start sensor polling only if a sensor handler has been registered, otherwise stop polling
if (sensorStatusListeners.isEmpty()) {
stopSensorPolling();
} else {
startSensorPolling();
}
startLightPolling();
startSensorPolling();
}
}

Expand Down Expand Up @@ -687,8 +676,6 @@ private void handleExceptionWhileCreatingUser(Exception ex) {
public boolean registerLightStatusListener(LightStatusListener lightStatusListener) {
boolean result = lightStatusListeners.add(lightStatusListener);
if (result && hueBridge != null) {
// start light and group polling only if a light handler has been registered
startLightPolling();
// inform the listener initially about all lights and their states
for (FullLight light : lastLightStates.values()) {
lightStatusListener.onLightAdded(hueBridge, light);
Expand All @@ -699,22 +686,13 @@ public boolean registerLightStatusListener(LightStatusListener lightStatusListen

@Override
public boolean unregisterLightStatusListener(LightStatusListener lightStatusListener) {
boolean result = lightStatusListeners.remove(lightStatusListener);
if (result) {
// stop the light and group polling
if (lightStatusListeners.isEmpty() && groupStatusListeners.isEmpty()) {
stopLightPolling();
}
}
return result;
return lightStatusListeners.remove(lightStatusListener);
}

@Override
public boolean registerSensorStatusListener(SensorStatusListener sensorStatusListener) {
boolean result = sensorStatusListeners.add(sensorStatusListener);
if (result && hueBridge != null) {
// start sensor polling only if a sensor handler has been registered
startSensorPolling();
// inform the listener initially about all sensors and their states
for (FullSensor sensor : lastSensorStates.values()) {
sensorStatusListener.onSensorAdded(hueBridge, sensor);
Expand All @@ -725,22 +703,13 @@ public boolean registerSensorStatusListener(SensorStatusListener sensorStatusLis

@Override
public boolean unregisterSensorStatusListener(SensorStatusListener sensorStatusListener) {
boolean result = sensorStatusListeners.remove(sensorStatusListener);
if (result) {
// stop sensor polling
if (sensorStatusListeners.isEmpty()) {
stopSensorPolling();
}
}
return result;
return sensorStatusListeners.remove(sensorStatusListener);
}

@Override
public boolean registerGroupStatusListener(GroupStatusListener groupStatusListener) {
boolean result = groupStatusListeners.add(groupStatusListener);
if (result && hueBridge != null) {
// start light and group polling only if a group handler has been registered
startLightPolling();
// inform the listener initially about all groups and their states
for (FullGroup group : lastGroupStates.values()) {
groupStatusListener.onGroupAdded(hueBridge, group);
Expand All @@ -751,14 +720,7 @@ public boolean registerGroupStatusListener(GroupStatusListener groupStatusListen

@Override
public boolean unregisterGroupStatusListener(GroupStatusListener groupStatusListener) {
boolean result = groupStatusListeners.remove(groupStatusListener);
if (result) {
// stop the light and group polling
if (lightStatusListeners.isEmpty() && groupStatusListeners.isEmpty()) {
stopLightPolling();
}
}
return result;
return groupStatusListeners.remove(groupStatusListener);
}

@Override
Expand Down

0 comments on commit 2621ff1

Please sign in to comment.