Skip to content

Commit

Permalink
implemented sendAutomowerSettings
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Weger <weger.michael@gmx.net>
  • Loading branch information
MikeTheTux committed Oct 13, 2024
1 parent a38b63f commit b120aec
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerCommandAttributes;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerCommandRequest;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerListResult;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerSettings;

Check failure on line 33 in bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/bridge/AutomowerBridge.java

View workflow job for this annotation

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

The import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerSettings cannot be resolved
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerSettingsRequest;

Check failure on line 34 in bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/bridge/AutomowerBridge.java

View workflow job for this annotation

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

The import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerSettingsRequest cannot be resolved
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.Settings;
import org.openhab.binding.automower.internal.rest.exceptions.AutomowerCommunicationException;
import org.openhab.binding.automower.internal.things.AutomowerCommand;
import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
Expand Down Expand Up @@ -118,23 +121,15 @@ public void sendAutomowerCommand(String id, AutomowerCommand command, long comma
}

/**
* Sends a calendar to the automower
* Sends a calendarTask to the automower
*
* @param id The id of the mower
* @param calendar The calendar that should be sent. It is using the same json structure (start, duration, ...)
* @param workAreaId The Id of the work area this calendar belongs to (or null, if there is no work area support)
* @param calendarTask The calendar that should be sent. It is using the same json structure (start, duration, ...)
* as provided when reading the channel
* @throws AutomowerCommunicationException In case the query cannot be executed successfully
*/

/**
* Sends a calendar to the automower
*
* @param id The id of the mower
* @param calendar The calendar that should be sent. It is using the same json structure (start, duration, ...)
* as provided when reading the channel
* @throws AutomowerCommunicationException In case the query cannot be executed successfully
*/
public void sendAutomowerCalendarTask(String id, Integer workAreaId, CalendarTask calendarTask)
public void sendAutomowerCalendarTask(String id, Long workAreaId, CalendarTask calendarTask)
throws AutomowerCommunicationException {
List<CalendarTask> tasks = new ArrayList<>();
tasks.add(calendarTask);
Expand All @@ -152,4 +147,25 @@ public void sendAutomowerCalendarTask(String id, Integer workAreaId, CalendarTas

automowerApi.sendCalendar(appKey, authenticate().getAccessToken(), id, workAreaId, request);
}

/**
* Sends Settings to the automower
*
* @param id The id of the mower
* @param settings The Settings that should be sent. It is using the same json structure
* as provided when reading the channel
* @throws AutomowerCommunicationException In case the query cannot be executed successfully
*/
public void sendAutomowerSettings(String id, Settings settings) throws AutomowerCommunicationException {
MowerSettings mowerSettings = new MowerSettings();

Check failure on line 160 in bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/bridge/AutomowerBridge.java

View workflow job for this annotation

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

MowerSettings cannot be resolved to a type

Check failure on line 160 in bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/bridge/AutomowerBridge.java

View workflow job for this annotation

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

MowerSettings cannot be resolved to a type
mowerSettings.setType("settings");
mowerSettings.setAttributes(settings);

MowerSettingsRequest request = new MowerSettingsRequest();

Check failure on line 164 in bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/bridge/AutomowerBridge.java

View workflow job for this annotation

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

MowerSettingsRequest cannot be resolved to a type

Check failure on line 164 in bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/bridge/AutomowerBridge.java

View workflow job for this annotation

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

MowerSettingsRequest cannot be resolved to a type
request.setData(mowerSettings);

logger.debug("request '{}'", gson.toJson(request));

automowerApi.sendSettings(appKey, authenticate().getAccessToken(), id, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerCommandRequest;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerListResult;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerResult;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerSettingsRequest;

Check failure on line 31 in bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/rest/api/automowerconnect/AutomowerConnectApi.java

View workflow job for this annotation

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

The import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerSettingsRequest cannot be resolved
import org.openhab.binding.automower.internal.rest.exceptions.AutomowerCommunicationException;
import org.openhab.binding.automower.internal.rest.exceptions.UnauthorizedException;
import org.slf4j.Logger;
Expand Down Expand Up @@ -82,7 +83,7 @@ public void sendCommand(String appKey, String token, String id, MowerCommandRequ
checkForError(response, response.getStatus());
}

public void sendCalendar(String appKey, String token, String id, Integer workAreaId, MowerCalendardRequest calendar)
public void sendCalendar(String appKey, String token, String id, Long workAreaId, MowerCalendardRequest calendar)
throws AutomowerCommunicationException {
String url;
if (workAreaId == null) {
Expand All @@ -100,6 +101,20 @@ public void sendCalendar(String appKey, String token, String id, Integer workAre
checkForError(response, response.getStatus());
}

public void sendSettings(String appKey, String token, String id, MowerSettingsRequest settings)

Check failure on line 104 in bundles/org.openhab.binding.automower/src/main/java/org/openhab/binding/automower/internal/rest/api/automowerconnect/AutomowerConnectApi.java

View workflow job for this annotation

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

MowerSettingsRequest cannot be resolved to a type
throws AutomowerCommunicationException {
String url;
url = getBaseUrl() + "/mowers/" + id + "/settings";
final Request request = getHttpClient().newRequest(url);
request.method(HttpMethod.POST);

request.content(new StringContentProvider(gson.toJson(settings)));

ContentResponse response = executeRequest(appKey, token, request);

checkForError(response, response.getStatus());
}

private ContentResponse executeRequest(String appKey, String token, final Request request)
throws AutomowerCommunicationException {
request.timeout(10, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class CalendarTask {
private boolean friday;
private boolean saturday;
private boolean sunday;
private Integer workAreaId;
private Long workAreaId;

public short getStart() {
return start;
Expand Down Expand Up @@ -70,7 +70,7 @@ public boolean getSunday() {
return sunday;
}

public Integer getWorkAreaId() {
public Long getWorkAreaId() {
return workAreaId;
}

Expand Down Expand Up @@ -110,7 +110,7 @@ public void setSunday(boolean sunday) {
this.sunday = sunday;
}

public void setWorkAreaId(Integer workAreaId) {
public void setWorkAreaId(Long workAreaId) {
this.workAreaId = workAreaId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class WorkArea {
private long workAreaId;
private String name;
private byte cuttingHeight;
private Boolean enabled;
private boolean enabled;
private Byte progress; // Only available for EPOS mowers and systematic mowing work areas.
private Long lastTimeCompleted; // Only available for EPOS mowers and systematic mowing work areas.

Expand Down Expand Up @@ -47,11 +47,11 @@ public void setCuttingHeight(byte cuttingHeight) {
this.cuttingHeight = cuttingHeight;
}

public Boolean isEnabled() {
public boolean isEnabled() {
return enabled;
}

public void setEnabled(Boolean enabled) {
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@
import org.openhab.binding.automower.internal.bridge.AutomowerBridge;
import org.openhab.binding.automower.internal.bridge.AutomowerBridgeHandler;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.CalendarTask;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.Headlight;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.HeadlightMode;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.Mower;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.Position;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.RestrictedReason;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.Settings;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.State;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.StayOutZone;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.WorkArea;
Expand Down Expand Up @@ -112,13 +115,11 @@ public void handleCommand(ChannelUID channelUID, Command command) {
if (RefreshType.REFRESH == command) {
logger.debug("Refreshing channel '{}'", channelUID);
refreshChannels(channelUID);
/*
* } else if (CHANNEL_CALENDAR_TASKS.equals(channelUID.getId())) {
* logger.debug("Sending calendar '{}'", command);
* sendAutomowerCalendar(command.toString());
*/
} else if (CHANNEL_CALENDARTASKS.contains(channelUID.getId())) {
sendAutomowerCalendarTask(command, channelUID.getId());
} else if (channelUID.getId().equals(CHANNEL_SETTING_CUTTING_HEIGHT)
|| channelUID.getId().equals(CHANNEL_SETTING_HEADLIGHT_MODE)) {
sendAutomowerSettings(command, channelUID.getId());
} else {
AutomowerCommand.fromChannelUID(channelUID).ifPresent(commandName -> {
logger.debug("Sending command '{}'", commandName);
Expand Down Expand Up @@ -293,10 +294,10 @@ public void sendAutomowerCommand(AutomowerCommand command, long commandDurationM
}

/**
* Sends a calendar to the automower
* Sends a CalendarTask to the automower
*
* @param calendar The calendar that should be sent. It is using the same json structure (start, duration, ...)
* as provided when reading the channel
* @param command The command that should be sent. E.g. a duration in min for the Start channel
* @param channelID The triggering channel
*/
public void sendAutomowerCalendarTask(Command command, String channelID) {
String[] channelIDSplit = channelID.split("-");
Expand Down Expand Up @@ -357,6 +358,51 @@ public void sendAutomowerCalendarTask(Command command, String channelID) {
}
}

/**
* Sends Settings to the automower
*
* @param command The command that should be sent. E.g. a number for the cuttingHeight channel
* @param channelID The triggering channel
*/
public void sendAutomowerSettings(Command command, String channelID) {
String[] channelIDSplit = channelID.split("-");
int index = Integer.parseInt(channelIDSplit[0].substring("calendartasks".length())) - 1;
String param = channelIDSplit[1];
logger.debug("Sending CalendarTask '{}', index '{}', param '{}', command '{}'", channelID, index, param,
command.toString());

if (mowerState != null) {
Settings settings = mowerState.getAttributes().getSettings();

if (command instanceof DecimalType cmd) {
if (CHANNEL_SETTING_HEADLIGHT_MODE.equals(channelID)) {
settings.setCuttingHeight((byte) cmd.intValue());
}
} else if (command instanceof StringType cmd) {
if (CHANNEL_STATISTIC_CUTTING_BLADE_USAGE_TIME.equals(channelID)) {
Headlight headlight = new Headlight();
headlight.setHeadlightMode(HeadlightMode.valueOf(cmd.toString()));
settings.setHeadlight(headlight);
}
}

String id = automowerId.get();
try {
AutomowerBridge automowerBridge = getAutomowerBridge();
if (automowerBridge != null) {
automowerBridge.sendAutomowerSettings(id, settings);
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/conf-error-no-bridge");
}
} catch (AutomowerCommunicationException e) {
logger.warn("Unable to send calendar to automower: {}, Error: {}", id, e.getMessage());
}

updateAutomowerState();
}
}

private String restrictedState(RestrictedReason reason) {
return "RESTRICTED_" + reason.name();
}
Expand Down Expand Up @@ -488,7 +534,11 @@ private void updateChannelState(@Nullable Mower mower) {
updateState(CHANNEL_CALENDARTASKS.get(j++), OnOffType.from(calendarTasks.get(i).getFriday() == true));
updateState(CHANNEL_CALENDARTASKS.get(j++), OnOffType.from(calendarTasks.get(i).getSaturday() == true));
updateState(CHANNEL_CALENDARTASKS.get(j++), OnOffType.from(calendarTasks.get(i).getSunday() == true));
updateState(CHANNEL_CALENDARTASKS.get(j++), new DecimalType(calendarTasks.get(i).getWorkAreaId()));
if (calendarTasks.get(i).getWorkAreaId() == null) {
updateState(CHANNEL_CALENDARTASKS.get(j++), UnDefType.NULL);
} else {
updateState(CHANNEL_CALENDARTASKS.get(j++), new DecimalType(calendarTasks.get(i).getWorkAreaId()));
}
}
// clear remaining channels
for (; j < CHANNEL_CALENDARTASKS.size(); j++) {
Expand Down

0 comments on commit b120aec

Please sign in to comment.