Skip to content

Commit

Permalink
[orbitbhyve] next bunch of fixes related to code review
Browse files Browse the repository at this point in the history
Signed-off-by: Ondrej Pecta <opecta@gmail.com>
  • Loading branch information
octa22 committed Jul 26, 2021
1 parent a6ec3c9 commit 74993c3
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 34 deletions.
2 changes: 1 addition & 1 deletion bundles/org.openhab.binding.orbitbhyve/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Beside the dynamic channels each sprinkler thing provides these standard channel
|----------------|-------------|--------------------------------------------------------------------|
| mode | String | This channel represents the mode of sprinkler device (auto/manual) |
| next_start | DateTime | This channel represents the start time of the next watering |
| rain_delay | Number | This channel manages the current rain delay in hours |
| rain_delay | Number:Time | This channel manages the current rain delay in hours |
| watering_time | Number:Time | This channel manages the manual zone watering time in minutes |
| control | Switch | This channel controls the sprinkler (ON/OFF) |
| smart_watering | Switch | This channel controls the smart watering (ON/OFF) |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import static org.openhab.binding.orbitbhyve.internal.OrbitBhyveBindingConstants.THING_TYPE_BRIDGE;
import static org.openhab.binding.orbitbhyve.internal.OrbitBhyveBindingConstants.THING_TYPE_SPRINKLER;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -48,7 +47,7 @@
public class OrbitBhyveHandlerFactory extends BaseThingHandlerFactory {

private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = new HashSet<>(
Arrays.asList(THING_TYPE_BRIDGE, THING_TYPE_SPRINKLER));
Set.of(THING_TYPE_BRIDGE, THING_TYPE_SPRINKLER));

/**
* the shared http client
Expand All @@ -58,7 +57,7 @@ public class OrbitBhyveHandlerFactory extends BaseThingHandlerFactory {
/**
* the shared web socket client
*/
private @NonNullByDefault({}) WebSocketClient webSocketClient;
private WebSocketClient webSocketClient;

@Activate
public OrbitBhyveHandlerFactory(@Reference HttpClientFactory httpClientFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ public class OrbitBhyveBridgeHandler extends ConfigStatusBridgeHandler {

private @Nullable ScheduledFuture<?> future = null;

private @Nullable ScheduledFuture<?> statusFuture = null;

private @Nullable Session session;

private @Nullable String sessionToken = null;
Expand Down Expand Up @@ -130,10 +128,6 @@ public void dispose() {
if (localFuture != null) {
localFuture.cancel(true);
}
localFuture = statusFuture;
if (localFuture != null) {
localFuture.cancel(true);
}
closeSession();
super.dispose();
}
Expand Down Expand Up @@ -186,6 +180,7 @@ private synchronized void ping() {
try {
logger.debug("Sending ping");
localSession.getRemote().sendString("{\"event\":\"ping\"}");
updateAllStatuses();
} catch (IOException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Error sending ping to a web socket");
Expand Down Expand Up @@ -290,10 +285,10 @@ private void processEvent(OrbitBhyveSocketEvent event) {
if (ch != null) {
updateState(ch.getUID(), "off".equals(event.getMode()) ? OnOffType.OFF : OnOffType.ON);
}
deferredStatusUpdate(event.getDeviceId());
updateDeviceStatus(event.getDeviceId());
break;
case "rain_delay":
deferredStatusUpdate(event.getDeviceId());
updateDeviceStatus(event.getDeviceId());
break;
case "skip_active_station":
disableZones(event.getDeviceId());
Expand All @@ -302,33 +297,42 @@ private void processEvent(OrbitBhyveSocketEvent event) {
OrbitBhyveProgram program = gson.fromJson(event.getProgram(), OrbitBhyveProgram.class);
if (program != null) {
updateDeviceProgramStatus(program);
deferredStatusUpdate(program.getDeviceId());
updateDeviceStatus(program.getDeviceId());
}
break;
default:
logger.debug("Received event: {}", event.getEvent());
}
}

private void deferredStatusUpdate(String deviceId) {
ScheduledFuture<?> localFuture = statusFuture;
if (localFuture != null) {
localFuture.cancel(true);
private void updateAllStatuses() {
List<OrbitBhyveDevice> devices = getDevices();
for (Thing th : getThing().getThings()) {
String deviceId = th.getUID().getId();
OrbitBhyveSprinklerHandler handler = (OrbitBhyveSprinklerHandler) th.getHandler();
for (OrbitBhyveDevice device : devices) {
if (deviceId.equals(th.getUID().getId())) {
updateDeviceStatus(device, handler);
}
}
}
}

private void updateDeviceStatus(@Nullable OrbitBhyveDevice device, @Nullable OrbitBhyveSprinklerHandler handler) {
if (device != null && handler != null) {
handler.setDeviceOnline(device.isConnected());
handler.updateDeviceStatus(device.getStatus());
handler.updateSmartWatering(device.getWaterSenseMode());
return;
}
statusFuture = scheduler.schedule(() -> updateDeviceStatus(deviceId), 3, TimeUnit.SECONDS);
}

private void updateDeviceStatus(String deviceId) {
for (Thing th : getThing().getThings()) {
if (deviceId.equals(th.getUID().getId())) {
OrbitBhyveSprinklerHandler handler = (OrbitBhyveSprinklerHandler) th.getHandler();
OrbitBhyveDevice device = getDevice(deviceId);
if (device != null && handler != null) {
handler.setDeviceOnline(device.isConnected());
handler.updateDeviceStatus(device.getStatus());
handler.updateSmartWatering(device.getWaterSenseMode());
return;
}
updateDeviceStatus(device, handler);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
import org.openhab.binding.orbitbhyve.internal.model.OrbitBhyveDeviceStatus;
import org.openhab.binding.orbitbhyve.internal.model.OrbitBhyveProgram;
import org.openhab.binding.orbitbhyve.internal.model.OrbitBhyveZone;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.types.*;
import org.openhab.core.library.unit.Units;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
Expand Down Expand Up @@ -79,9 +77,12 @@ public void handleCommand(ChannelUID channelUID, Command command) {
handler.stopWatering(deviceId);
return;
}
if (CHANNEL_WATERING_TIME.equals(channelUID.getId()) && command instanceof DecimalType) {
wateringTime = ((DecimalType) command).intValue();
updateState(CHANNEL_WATERING_TIME, (DecimalType) command);
if (CHANNEL_WATERING_TIME.equals(channelUID.getId()) && command instanceof QuantityType) {
final QuantityType<?> value = ((QuantityType<?>) command).toUnit(Units.MINUTE);
if (value != null) {
wateringTime = value.intValue();
updateState(CHANNEL_WATERING_TIME, new DecimalType(wateringTime));
}
return;
}
if (channelUID.getId().startsWith("zone")) {
Expand All @@ -107,7 +108,11 @@ public void handleCommand(ChannelUID channelUID, Command command) {
return;
}
if (CHANNEL_RAIN_DELAY.equals(channelUID.getId()) && command instanceof DecimalType) {
handler.setRainDelay(deviceId, ((DecimalType) command).intValue());
final QuantityType<?> value = ((QuantityType<?>) command).toUnit(Units.HOUR);
if (value != null) {
handler.setRainDelay(deviceId, value.intValue());
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</parameter>
<parameter name="refresh" type="integer" required="false" min="10">
<label>Refresh</label>
<description>Specifies the refresh time in seconds for polling events from Orbit cloud</description>
<description>Specifies the refresh time in seconds for polling data from Orbit cloud</description>
<default>30</default>
</parameter>
</config-description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<state readOnly="true"></state>
</channel-type>
<channel-type id="rain_delay">
<item-type>Number</item-type>
<item-type>Number:Time</item-type>
<label>Rain Delay</label>
<description>Channel representing rain delay in hours</description>
<state pattern="%d h"></state>
Expand Down

0 comments on commit 74993c3

Please sign in to comment.