Skip to content

Commit

Permalink
Added houshold users, locationChangedThrough channel, updated Readme (o…
Browse files Browse the repository at this point in the history
…penhab#8)

* Added household users and locationChangedThrough channel
* Updated Readme
* openHAB/ESH code formatting
* fixed NPE for gender, breed, species
* minor translation fixes
* Fixed pet status polling job, Removed PetLocation - using PetStatusActivity instead, Updated Readme.md to use tabs in PaperUI control, Updated API call for PetStatus (now using only one call for all pets), some other renaming changes

Co-authored-by: Holger Eisold <[email protected]>
Co-authored-by: Rene Scherer <[email protected]>
Signed-off-by: Holger Eisold <[email protected]>
  • Loading branch information
HerzScheisse and renescherer committed Oct 22, 2020
1 parent 4ff15ce commit d430d42
Show file tree
Hide file tree
Showing 21 changed files with 409 additions and 411 deletions.
187 changes: 119 additions & 68 deletions bundles/org.openhab.binding.surepetcare/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.SocketException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Date;

import org.eclipse.jdt.annotation.NonNull;
Expand All @@ -35,7 +36,6 @@
import org.openhab.binding.surepetcare.internal.data.SurePetcareLoginCredentials;
import org.openhab.binding.surepetcare.internal.data.SurePetcareLoginResponse;
import org.openhab.binding.surepetcare.internal.data.SurePetcarePet;
import org.openhab.binding.surepetcare.internal.data.SurePetcarePetLocation;
import org.openhab.binding.surepetcare.internal.data.SurePetcarePetStatus;
import org.openhab.binding.surepetcare.internal.data.SurePetcareTag;
import org.openhab.binding.surepetcare.internal.data.SurePetcareTopology;
Expand Down Expand Up @@ -70,6 +70,7 @@ public class SurePetcareAPIHelper {
private static final String API_URL = "https://app.api.surehub.io/api";
private static final String TOPOLOGY_URL = API_URL + "/me/start";
private static final String PET_BASE_URL = API_URL + "/pet";
private static final String PET_STATUS_URL = API_URL + "/pet/?with[]=status";
private static final String DEVICE_BASE_URL = API_URL + "/device";
private static final String LOGIN_URL = API_URL + "/auth/login";

Expand Down Expand Up @@ -147,17 +148,16 @@ public synchronized void updateTopologyCache() {
}

/**
* Refreshes only the pet status (location activity and feeding information). This API call can be used more
* frequently.
* Refreshes the pet information. This API call can be used more frequently.
* Unlike for the "position" API endpoint, there is none for the "status" (activity/feeding).
* We also dont need to specify a "petId" in the call, so we just need to call the API once.
*/
public synchronized void updatePetStatus() {
try {
for (SurePetcarePet pet : topologyCache.getPets()) {
String url = PET_BASE_URL + "/" + pet.getId().toString() + "?with[]=status";
pet.setPetStatus(gson.fromJson(getDataFromApi(url), SurePetcarePetStatus.class));
}
String url = PET_STATUS_URL;
topologyCache.setPets(Arrays.asList(gson.fromJson(getDataFromApi(url), SurePetcarePet[].class)));
} catch (JsonSyntaxException | SurePetcareApiException e) {
logger.warn("Exception caught during topology cache update: {}", e.getMessage());
logger.warn("Exception caught during pet status update: {}", e.getMessage());
}
}

Expand Down Expand Up @@ -210,21 +210,6 @@ public final SurePetcareTopology getTopology() {
return topologyCache.getTagById(id);
}

/**
* Returns the location object if a pet exists with the given id, otherwise null.
*
* @param id the pet id
* @return the location of the pet with the given id
*/
public final @Nullable SurePetcarePetLocation getPetLocation(String id) {
SurePetcarePet pet = topologyCache.getPetById(id);
if (pet != null) {
return pet.getLocation();
} else {
return null;
}
}

/**
* Returns the status object if a pet exists with the given id, otherwise null.
*
Expand All @@ -243,22 +228,21 @@ public final SurePetcareTopology getTopology() {
/**
* Updates the pet location through an API call to the Sure Petcare API.
*
* @param pet the pet
* @param pet the pet
* @param newLocationId the id of the new location
* @throws SurePetcareApiException
*/
public synchronized void setPetLocation(SurePetcarePet pet, Integer newLocationId) throws SurePetcareApiException {
pet.getLocation().setPetId(pet.getId());
pet.getLocation().setWhere(newLocationId);
pet.getLocation().setSince(new Date());
pet.getPetStatus().getActivity().setWhere(newLocationId);
pet.getPetStatus().getActivity().setSince(new Date());
String url = PET_BASE_URL + "/" + pet.getId().toString() + "/position";
setDataThroughApi(url, HTTP_REQUEST_METHOD_POST, pet.getLocation());
setDataThroughApi(url, HTTP_REQUEST_METHOD_POST, pet.getPetStatus().getActivity());
}

/**
* Updates the device locking mode through an API call to the Sure Petcare API.
*
* @param device the device
* @param device the device
* @param newLockingModeId the id of the new locking mode
* @throws SurePetcareApiException
*/
Expand All @@ -279,7 +263,7 @@ public synchronized void setDeviceLockingMode(SurePetcareDevice device, Integer
/**
* Updates the device led mode through an API call to the Sure Petcare API.
*
* @param device the device
* @param device the device
* @param newLedModeId the id of the new led mode
* @throws SurePetcareApiException
*/
Expand All @@ -300,7 +284,7 @@ public synchronized void setDeviceLedMode(SurePetcareDevice device, Integer newL
/**
* Updates all curfews through an API call to the Sure Petcare API.
*
* @param device the device
* @param device the device
* @param curfewList the list of curfews
* @throws SurePetcareApiException
*/
Expand Down Expand Up @@ -402,9 +386,9 @@ private JsonElement getDataFromApi(String url) throws SurePetcareApiException {
/**
* Sends a given object as a JSON payload to the API.
*
* @param url the URL
* @param url the URL
* @param requestMethod the request method (POST, PUT etc.)
* @param payload an object used for the payload
* @param payload an object used for the payload
* @throws SurePetcareApiException
*/
private void setDataThroughApi(String url, String requestMethod, Object payload) throws SurePetcareApiException {
Expand Down Expand Up @@ -464,9 +448,9 @@ private String getResultFromApi(String url) throws SurePetcareApiException {
/**
* Uses the given request method to send a JSON string to an API.
*
* @param url the URL
* @param url the URL
* @param requestMethod the required request method (POST, PUT etc.)
* @param jsonPayload the JSON string
* @param jsonPayload the JSON string
* @throws SurePetcareApiException
*/
private void postDataThroughAPI(String url, String requestMethod, String jsonPayload)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class SurePetcareConstants {
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
public static final String REFRESH_INTERVAL_TOPOLOGY = "refresh_interval_topology";
public static final String REFRESH_INTERVAL_LOCATION = "refresh_interval_location";
public static final String REFRESH_INTERVAL_STATUS = "refresh_interval_status";

// Bridge Channel Names
public static final String BRIDGE_CHANNEL_ONLINE = "online";
Expand All @@ -58,10 +58,9 @@ public class SurePetcareConstants {
public static final String HOUSEHOLD_CHANNEL_ID = "id";
public static final String HOUSEHOLD_CHANNEL_NAME = "name";
public static final String HOUSEHOLD_CHANNEL_TIMEZONE_ID = "timezoneId";
// public static final String HOUSEHOLD_CHANNEL_TIMEZONE = "timezone";
// public static final String HOUSEHOLD_CHANNEL_TIMEZONE_UTC_OFFSET = "timezoneUTCOffset";
public static final String HOUSEHOLD_CHANNEL_CREATED_AT = "createdAt";
public static final String HOUSEHOLD_CHANNEL_UPDATED_AT = "updatedAt";
public static final String HOUSEHOLD_CHANNEL_USER_NAME = "userName";

// Device Channel Names
public static final String DEVICE_CHANNEL_ID = "id";
Expand Down Expand Up @@ -107,6 +106,7 @@ public class SurePetcareConstants {
public static final String PET_CHANNEL_PHOTO_URL = "photoURL";
public static final String PET_CHANNEL_LOCATION = "location";
public static final String PET_CHANNEL_LOCATION_CHANGED = "locationChanged";
public static final String PET_CHANNEL_LOCATION_CHANGED_THROUGH = "locationChangedThrough";
public static final String PET_CHANNEL_DATE_OF_BIRTH = "dateOfBirth";
public static final String PET_CHANNEL_WEIGHT = "weight";
public static final String PET_CHANNEL_TAG_IDENTIFIER = "tagIdentifier";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
*/
package org.openhab.binding.surepetcare.internal.data;

import java.util.Date;
import java.util.List;

import com.google.gson.annotations.SerializedName;

/**
* The {@link SurePetcareHousehold} is the Java class used as a DTO to represent a Sure Petcare Household.
Expand All @@ -21,23 +23,47 @@
*/
public class SurePetcareHousehold extends SurePetcareBaseObject {

// Commented members indicate properties returned by the API not used by the binding
public class HouseholdUsers {
public class User {
@SerializedName("id")
private Integer userId;
@SerializedName("name")
private String userName;

public Integer getUserId() {
return userId;
}

public void setUserId(Integer userId) {
this.userId = userId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}
}

@SerializedName("user")
private User user;

public class Timezone {
public Integer id;
public String name;
public String timezone;
public Integer utcOffset;
public Date createdAt;
public Date updatedAt;
public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}
}

private String name;
private String shareCode;
private Integer timezoneId;

// Timezone does seem to be included anymore
// private Timezone timezone = new Timezone();
@SerializedName("users")
private List<HouseholdUsers> householdUsers = null;

public String getName() {
return name;
Expand All @@ -63,6 +89,14 @@ public void setTimezoneId(Integer timezoneId) {
this.timezoneId = timezoneId;
}

public List<HouseholdUsers> getHouseholdUsers() {
return householdUsers;
}

public void setHouseholdUsers(List<HouseholdUsers> householdUsers) {
this.householdUsers = householdUsers;
}

@Override
public String toString() {
return "SurePetcareHousehold [id=" + id + ", name=" + name + "]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ public static PetSpecies findByTypeId(final int id) {
private Integer tagId;
private SurePetcarePhoto photo;

@SerializedName("position")
private SurePetcarePetLocation location = new SurePetcarePetLocation();
@SerializedName("status")
private SurePetcarePetStatus status = new SurePetcarePetStatus();

Expand Down Expand Up @@ -192,14 +190,6 @@ public void setPhoto(SurePetcarePhoto photo) {
this.photo = photo;
}

public SurePetcarePetLocation getLocation() {
return location;
}

public void setLocation(SurePetcarePetLocation position) {
this.location = position;
}

public SurePetcarePetStatus getPetStatus() {
return status;
}
Expand Down
Loading

0 comments on commit d430d42

Please sign in to comment.