import org.openhab.binding.sleepiq.api.model.SleepersResponse;
import org.openhab.binding.sleepiq.internal.GsonProvider;
-public class SleepIQImpl extends AbstractClient implements SleepIQ
-{
+public class SleepIQImpl extends AbstractClient implements SleepIQ {
protected static final String PARAM_KEY = "_k";
protected static final String DATA_BED_ID = "bedId";
private volatile LoginInfo loginInfo;
- public SleepIQImpl(Configuration config)
- {
+ private final ClientBuilder clientBuilder;
+
+ public SleepIQImpl(Configuration config, ClientBuilder clientBuilder) {
this.config = config;
+ this.clientBuilder = clientBuilder;
}
@Override
- public LoginInfo login() throws LoginException
- {
- if (loginInfo == null)
- {
- synchronized (this)
- {
- if (loginInfo == null)
- {
- Response response = getClient().target(config.getBaseUri())
- .path(Endpoints.login())
- .request(MediaType.APPLICATION_JSON_TYPE)
- .put(Entity.json(new LoginRequest().withLogin(config.getUsername())
- .withPassword(config.getPassword())));
-
- if (isUnauthorized(response))
- {
+ public LoginInfo login() throws LoginException {
+ if (loginInfo == null) {
+ synchronized (this) {
+ if (loginInfo == null) {
+ Response response = getClient().target(config.getBaseUri()).path(Endpoints.login())
+ .request(MediaType.APPLICATION_JSON_TYPE).put(Entity.json(new LoginRequest()
+ .withLogin(config.getUsername()).withPassword(config.getPassword())));
+
+ if (isUnauthorized(response)) {
throw new UnauthorizedException(response.readEntity(Failure.class));
}
- if (!Status.Family.SUCCESSFUL.equals(response.getStatusInfo().getFamily()))
- {
+ if (!Status.Family.SUCCESSFUL.equals(response.getStatusInfo().getFamily())) {
throw new LoginException(response.readEntity(Failure.class));
}
// add the received cookies to all future requests
- getClient().register(new ClientRequestFilter()
- {
+ getClient().register(new ClientRequestFilter() {
@Override
- public void filter(ClientRequestContext requestContext) throws IOException
- {
- List<Object> cookies = response.getCookies()
- .values()
- .stream()
- .map(newCookie -> newCookie.toCookie())
- .collect(Collectors.toList());
+ public void filter(ClientRequestContext requestContext) throws IOException {
+ List<Object> cookies = response.getCookies().values().stream()
+ .map(newCookie -> newCookie.toCookie()).collect(Collectors.toList());
requestContext.getHeaders().put("Cookie", cookies);
}
});
}
@Override
- public List<Bed> getBeds()
- {
+ public List<Bed> getBeds() {
return getSessionResponse(this::getBedsResponse).readEntity(BedsResponse.class).getBeds();
}
- protected Response getBedsResponse(Map<String, Object> data) throws LoginException
- {
+ protected Response getBedsResponse(Map<String, Object> data) throws LoginException {
LoginInfo login = login();
- return getClient().target(config.getBaseUri())
- .path(Endpoints.bed())
- .queryParam(PARAM_KEY, login.getKey())
- .request(MediaType.APPLICATION_JSON_TYPE)
- .get();
+ return getClient().target(config.getBaseUri()).path(Endpoints.bed()).queryParam(PARAM_KEY, login.getKey())
+ .request(MediaType.APPLICATION_JSON_TYPE).get();
}
@Override
- public List<Sleeper> getSleepers()
- {
- return getSessionResponse(this::getSleepersResponse).readEntity(SleepersResponse.class)
- .getSleepers();
+ public List<Sleeper> getSleepers() {
+ return getSessionResponse(this::getSleepersResponse).readEntity(SleepersResponse.class).getSleepers();
}
- protected Response getSleepersResponse(Map<String, Object> data) throws LoginException
- {
+ protected Response getSleepersResponse(Map<String, Object> data) throws LoginException {
LoginInfo login = login();
- return getClient().target(config.getBaseUri())
- .path(Endpoints.sleeper())
- .queryParam(PARAM_KEY, login.getKey())
- .request(MediaType.APPLICATION_JSON_TYPE)
- .get();
+ return getClient().target(config.getBaseUri()).path(Endpoints.sleeper()).queryParam(PARAM_KEY, login.getKey())
+ .request(MediaType.APPLICATION_JSON_TYPE).get();
}
@Override
- public FamilyStatus getFamilyStatus()
- {
+ public FamilyStatus getFamilyStatus() {
return getSessionResponse(this::getFamilyStatusResponse).readEntity(FamilyStatus.class);
}
- protected Response getFamilyStatusResponse(Map<String, Object> data) throws LoginException
- {
+ protected Response getFamilyStatusResponse(Map<String, Object> data) throws LoginException {
LoginInfo login = login();
- return getClient().target(config.getBaseUri())
- .path(Endpoints.bed())
- .path(Endpoints.familyStatus())
- .queryParam(PARAM_KEY, login.getKey())
- .request(MediaType.APPLICATION_JSON_TYPE)
- .get();
+ return getClient().target(config.getBaseUri()).path(Endpoints.bed()).path(Endpoints.familyStatus())
+ .queryParam(PARAM_KEY, login.getKey()).request(MediaType.APPLICATION_JSON_TYPE).get();
}
@Override
- public PauseMode getPauseMode(String bedId) throws BedNotFoundException
- {
+ public PauseMode getPauseMode(String bedId) throws BedNotFoundException {
Map<String, Object> data = new HashMap<>();
data.put(DATA_BED_ID, bedId);
Response response = getSessionResponse(this::getPauseModeResponse, data);
- if (!Status.Family.SUCCESSFUL.equals(response.getStatusInfo().getFamily()))
- {
+ if (!Status.Family.SUCCESSFUL.equals(response.getStatusInfo().getFamily())) {
throw new BedNotFoundException(response.readEntity(Failure.class));
}
return response.readEntity(PauseMode.class);
}
- protected Response getPauseModeResponse(Map<String, Object> data) throws LoginException
- {
+ protected Response getPauseModeResponse(Map<String, Object> data) throws LoginException {
LoginInfo login = login();
- return getClient().target(config.getBaseUri())
- .path(Endpoints.bed())
- .path(data.get(DATA_BED_ID).toString())
- .path(Endpoints.pauseMode())
- .queryParam(PARAM_KEY, login.getKey())
- .request(MediaType.APPLICATION_JSON_TYPE)
- .get();
+ return getClient().target(config.getBaseUri()).path(Endpoints.bed()).path(data.get(DATA_BED_ID).toString())
+ .path(Endpoints.pauseMode()).queryParam(PARAM_KEY, login.getKey())
+ .request(MediaType.APPLICATION_JSON_TYPE).get();
}
- protected boolean isUnauthorized(Response response)
- {
+ protected boolean isUnauthorized(Response response) {
return Status.UNAUTHORIZED.getStatusCode() == response.getStatusInfo().getStatusCode();
}
- protected synchronized void resetLogin()
- {
+ protected synchronized void resetLogin() {
loginInfo = null;
}
- protected Response getSessionResponse(Request request)
- {
+ protected Response getSessionResponse(Request request) {
return getSessionResponse(request, Collections.emptyMap());
}
- protected Response getSessionResponse(Request request, Map<String, Object> data)
- {
- try
- {
+ protected Response getSessionResponse(Request request, Map<String, Object> data) {
+ try {
Response response = request.execute(data);
- if (isUnauthorized(response))
- {
+ if (isUnauthorized(response)) {
// session timed out
response.close();
resetLogin();
}
return response;
- }
- catch (LoginException e)
- {
+ } catch (LoginException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
@Override
- protected Client createClient()
- {
- ClientBuilder builder = ClientBuilder.newBuilder();
-
+ protected Client createClient() {
// setup Gson (de)serialization
GsonProvider<Object> gsonProvider = new GsonProvider<>(getGson());
- builder.register(gsonProvider);
+ clientBuilder.register(gsonProvider);
// turn on logging if requested
- if (config.isLogging())
- {
- builder.register(new LoggingFilter(Logger.getLogger(SleepIQImpl.class.getName()),
- true));
+ if (config.isLogging()) {
+ clientBuilder.register(new LoggingFilter(Logger.getLogger(SleepIQImpl.class.getName()), true));
}
- return builder.build();
+ return clientBuilder.build();
}
@FunctionalInterface
- public static interface Request
- {
+ public static interface Request {
public Response execute(Map<String, Object> data) throws LoginException;
}
}
package org.openhab.binding.sleepiq.api.model;
import java.time.Duration;
-import java.time.format.DateTimeParseException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-public class TimeSince
-{
+public class TimeSince {
private static final Pattern PATTERN = Pattern.compile("(([0-9]+) d )?([0-9]{2}):([0-9]{2}):([0-9]{2})",
- Pattern.CASE_INSENSITIVE);
+ Pattern.CASE_INSENSITIVE);
private Duration duration;
- public Duration getDuration()
- {
+ public Duration getDuration() {
return duration;
}
- public void setDuration(Duration duration)
- {
+ public void setDuration(Duration duration) {
this.duration = duration == null ? null : duration.abs();
}
- public TimeSince withDuration(long days, long hours, long minutes, long seconds)
- {
- return withDuration(Duration.ofSeconds(TimeUnit.DAYS.toSeconds(days)
- + TimeUnit.HOURS.toSeconds(hours)
- + TimeUnit.MINUTES.toSeconds(minutes)
- + seconds));
+ public TimeSince withDuration(long days, long hours, long minutes, long seconds) {
+ return withDuration(Duration.ofSeconds(TimeUnit.DAYS.toSeconds(days) + TimeUnit.HOURS.toSeconds(hours)
+ + TimeUnit.MINUTES.toSeconds(minutes) + seconds));
}
- public TimeSince withDuration(Duration duration)
- {
+ public TimeSince withDuration(Duration duration) {
setDuration(duration);
return this;
}
@Override
- public int hashCode()
- {
+ public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((duration == null) ? 0 : duration.hashCode());
}
@Override
- public boolean equals(Object obj)
- {
- if (this == obj)
- {
+ public boolean equals(Object obj) {
+ if (this == obj) {
return true;
}
- if (obj == null)
- {
+ if (obj == null) {
return false;
}
- if (!(obj instanceof TimeSince))
- {
+ if (!(obj instanceof TimeSince)) {
return false;
}
- TimeSince other = (TimeSince)obj;
- if (duration == null)
- {
- if (other.duration != null)
- {
+ TimeSince other = (TimeSince) obj;
+ if (duration == null) {
+ if (other.duration != null) {
return false;
}
- }
- else if (!duration.equals(other.duration))
- {
+ } else if (!duration.equals(other.duration)) {
return false;
}
return true;
}
@Override
- public String toString()
- {
+ public String toString() {
long totalDays = duration.toDays();
long totalHours = duration.toHours();
long totalMinutes = duration.toMinutes();
long minutes = totalMinutes - TimeUnit.HOURS.toMinutes(totalHours);
long seconds = totalSeconds - TimeUnit.MINUTES.toSeconds(totalMinutes);
- if (totalDays > 0)
- {
+ if (totalDays > 0) {
return String.format("%d d %02d:%02d:%02d", totalDays, hours, minutes, seconds);
}
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}
- public static TimeSince parse(CharSequence text)
- {
+ public static TimeSince parse(CharSequence text) {
Objects.requireNonNull(text, "text");
Matcher matcher = PATTERN.matcher(text);
- if (!matcher.matches())
- {
- throw new DateTimeParseException("Text cannot be parsed", text, 0);
+ if (!matcher.matches()) {
+ return new TimeSince().withDuration(Duration.ZERO);
}
String dayMatch = matcher.group(2);
String secondMatch = matcher.group(5);
StringBuilder sb = new StringBuilder("P");
- if (dayMatch != null)
- {
+ if (dayMatch != null) {
sb.append(dayMatch).append('D');
}
- sb.append('T')
- .append(hourMatch)
- .append('H')
- .append(minuteMatch)
- .append('M')
- .append(secondMatch)
- .append('S');
+ sb.append('T').append(hourMatch).append('H').append(minuteMatch).append('M').append(secondMatch).append('S');
return new TimeSince().withDuration(Duration.parse(sb.toString()));
}