Skip to content

Commit

Permalink
Merge pull request #14 from HerzScheisse/feature/surepetcare_binding
Browse files Browse the repository at this point in the history
Added Location Time Offset Channel, Added sitemap example to Readme
  • Loading branch information
renescherer authored Nov 5, 2019
2 parents 6413145 + ba35456 commit fc678cb
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 118 deletions.
2 changes: 1 addition & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
/bundles/org.openhab.binding.sonyprojector/ @lolodomo
/bundles/org.openhab.binding.spotify/ @Hilbrand
/bundles/org.openhab.binding.squeezebox/ @digitaldan @mhilbush
/bundles/org.openhab.binding.surepetcare/ @renescherer
/bundles/org.openhab.binding.surepetcare/ @renescherer @HerzScheisse
/bundles/org.openhab.binding.synopanalyzer/ @clinique
/bundles/org.openhab.binding.systeminfo/ @svilenvul
/bundles/org.openhab.binding.tado/ @dfrommi
Expand Down
196 changes: 126 additions & 70 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 @@ -7,6 +7,7 @@ String UR_1e_Species "Pet Species [%s]" (dgP
Image UR_1e_Photo "Pet Photo" (dgPet) {channel="surepetcare:pet:bridge1:12345:photo"}
String UR_1e_TagIdentifier "Pet Tag Identifier [%s]" (dgPet) {channel="surepetcare:pet:bridge1:12345:tagIdentifier"}
String UR_1e_Location "Pet Location [%s]" (dgPet) {channel="surepetcare:pet:bridge1:12345:location"}
String UR_1e_LocationTimeoffset"Pet Switch Location [%s]" (gCats) {channel="surepetcare:pet:bridge1:12345:locationTimeoffset"}
DateTime UR_1e_LocationChanged "Pet Loc. Updated [%1$ta. %1$tH:%1$tM]" (dgPet) {channel="surepetcare:pet:bridge1:12345:locationChanged"}
String UR_1e_LocationThrough "Pet Entered / Left through [%s]" (dgPet) {channel="surepetcare:pet:bridge1:12345:locationChangedThrough"}
Number:Mass UR_1e_Weight "Pet Weight [%.1f %unit%]" (dgPet) {channel="surepetcare:pet:bridge1:12345:weight"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,21 +220,22 @@ 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 {
public synchronized void setPetLocation(SurePetcarePet pet, Integer newLocationId, Date newSince)
throws SurePetcareApiException {
pet.getPetStatus().getActivity().setWhere(newLocationId);
pet.getPetStatus().getActivity().setSince(new Date());
pet.getPetStatus().getActivity().setSince(newSince);
String url = PET_BASE_URL + "/" + pet.getId().toString() + "/position";
setDataThroughApi(url, HttpMethod.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 @@ -256,7 +257,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 @@ -278,7 +279,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 @@ -324,7 +325,7 @@ public final Integer getDeviceId() {
* Returns a unique device id used during the authentication process with the Sure Petcare API. The id is derived
* from the local MAC address or hostname provided as arguments
*
* @param interfaces a list of interface of this host
* @param interfaces a list of interface of this host
* @param localHostAddress the ip address of the localhost
* @return a unique device id
*/
Expand Down Expand Up @@ -396,9 +397,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, HttpMethod method, Object payload) throws SurePetcareApiException {
Expand Down Expand Up @@ -448,8 +449,8 @@ 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 method the required request method (POST, PUT etc.)
* @param url the URL
* @param method the required request method (POST, PUT etc.)
* @param jsonPayload the JSON string
* @throws SurePetcareApiException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public class SurePetcareConstants {
public static final String PET_CHANNEL_PHOTO = "photo";
public static final String PET_CHANNEL_LOCATION = "location";
public static final String PET_CHANNEL_LOCATION_CHANGED = "locationChanged";
public static final String PET_CHANNEL_LOCATION_TIMEOFFSET = "locationTimeoffset";
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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public void initialize() {

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
logger.debug("BaseObjectHandler handleCommand called with command: {}", command);
if (command instanceof RefreshType) {
updateThingCache.getValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ public void dispose() {

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
logger.debug("SurePetcareBridgeHandler handleCommand called with command: {}", command);
if (command instanceof RefreshType) {
updateState(BRIDGE_CHANNEL_ONLINE, OnOffType.from(petcareAPI.isOnline()));
updateState(BRIDGE_CHANNEL_REFRESH, OnOffType.OFF);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public SurePetcareDeviceHandler(Thing thing, SurePetcareAPIHelper petcareAPI) {

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
logger.debug("DeviceHandler handleCommand called with command: {}", command.toFullString());

if (command instanceof RefreshType) {
synchronized (petcareAPI) {
updateThingCache.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import static org.openhab.binding.surepetcare.internal.SurePetcareConstants.*;

import java.time.ZonedDateTime;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import javax.measure.quantity.Mass;

Expand Down Expand Up @@ -48,7 +50,7 @@
* The {@link SurePetcarePetHandler} is responsible for handling the things created to represent Sure Petcare pets.
*
* @author Rene Scherer - Initial Contribution
* @author Holger Eisold - Added pet feeder status
* @author Holger Eisold - Added pet feeder status, location time offset
*/
@NonNullByDefault
public class SurePetcarePetHandler extends SurePetcareBaseObjectHandler {
Expand All @@ -65,8 +67,6 @@ public SurePetcarePetHandler(Thing thing, SurePetcareAPIHelper petcareAPI) {

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
logger.debug("PetHandler handleCommand called with command: {}", command);

if (command instanceof RefreshType) {
updateThingCache.getValue();
} else {
Expand All @@ -78,19 +78,64 @@ public void handleCommand(ChannelUID channelUID, Command command) {
SurePetcarePet pet = petcareAPI.getPet(thing.getUID().getId());
if (pet != null) {
String newLocationIdStr = ((StringType) command).toString();
Integer newLocationId = Integer.valueOf(newLocationIdStr);
// Only update if location has changed. (Needed for Group:Switch item)
if ((pet.getPetStatus().getActivity().getWhere().equals(newLocationId))
|| newLocationId.equals(0)) {
logger.debug("Location has not changed, skip pet id: {} with loc id: {}",
pet.getId(), newLocationId);
} else {
try {
logger.debug("Received new location: {}", newLocationId);
petcareAPI.setPetLocation(pet, newLocationId, new Date());
updateState(PET_CHANNEL_LOCATION,
new StringType(pet.getPetStatus().getActivity().getWhere().toString()));
updateState(PET_CHANNEL_LOCATION_CHANGED, new DateTimeType(
pet.getPetStatus().getActivity().getLocationChanged()));
} catch (NumberFormatException e) {
logger.warn("Invalid location id: {}, ignoring command", newLocationIdStr);
} catch (SurePetcareApiException e) {
logger.warn("Error from SurePetcare API. Can't update location {} for pet {}",
newLocationIdStr, pet);
}
}
}
}
}
break;
case PET_CHANNEL_LOCATION_TIMEOFFSET:
logger.debug("Received location time offset update command: {}", command.toFullString());
if (command instanceof StringType) {
synchronized (petcareAPI) {
SurePetcarePet pet = petcareAPI.getPet(thing.getUID().getId());
if (pet != null) {
String commandIdStr = ((StringType) command).toString();
try {
Integer newLocationId = Integer.valueOf(newLocationIdStr);
logger.debug("Received new location: {}", newLocationId);
petcareAPI.setPetLocation(pet, newLocationId);
Integer commandId = Integer.valueOf(commandIdStr);
Integer currentLocation = pet.getPetStatus().getActivity().getWhere();
logger.debug("Received new location: {}", currentLocation == 1 ? 2 : 1);
// We set the location to the opposite state.
// We also set location to INSIDE (1) if currentLocation is Unknown (0)
if (commandId == 10) {
petcareAPI.setPetLocation(pet, currentLocation == 1 ? 2 : 1, new Date(
System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(commandId)));
} else if (commandId == 30) {
petcareAPI.setPetLocation(pet, currentLocation == 1 ? 2 : 1, new Date(
System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(commandId)));
} else if (commandId == 60) {
petcareAPI.setPetLocation(pet, currentLocation == 1 ? 2 : 1, new Date(
System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(commandId)));
}
updateState(PET_CHANNEL_LOCATION,
new StringType(pet.getPetStatus().getActivity().getWhere().toString()));
updateState(PET_CHANNEL_LOCATION_CHANGED,
new DateTimeType(pet.getPetStatus().getActivity().getLocationChanged()));
updateState(PET_CHANNEL_LOCATION_TIMEOFFSET, UnDefType.UNDEF);
} catch (NumberFormatException e) {
logger.warn("Invalid location id: {}, ignoring command", newLocationIdStr);
logger.warn("Invalid location id: {}, ignoring command", commandIdStr);
} catch (SurePetcareApiException e) {
logger.warn("Error from SurePetcare API. Can't update location {} for pet {}",
newLocationIdStr, pet);
commandIdStr, pet);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ channel-type.surepetcare.tagIdentifierType.description = The unique identifier o
channel-type.surepetcare.locationChangedType.label = Last Location Change Time
channel-type.surepetcare.locationChangedType.description = The time when the pet location has last changed.

channel-type.surepetcare.locationTimeoffsetType.label = Location Change Time Offset
channel-type.surepetcare.locationTimeoffsetType.description = Time-Command to set the pet location with a time offset. (10, 30 or 60 minutes ago).
channel-type.surepetcare.locationTimeoffsetType.state.option.10 = 10 mins ago
channel-type.surepetcare.locationTimeoffsetType.state.option.30 = 30 mins ago
channel-type.surepetcare.locationTimeoffsetType.state.option.60 = 60 mins ago

channel-type.surepetcare.locationChangedThroughType.label = Last Location Changed By
channel-type.surepetcare.locationChangedThroughType.description = Indicates by which user or device the last location change was made.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ channel-type.surepetcare.tagIdentifierType.description = Zeigt den Tag des Haust
channel-type.surepetcare.locationChangedType.label = Standort Zeit
channel-type.surepetcare.locationChangedType.description = Zeigt die Zeit des letzten Standortwechsels an.

channel-type.surepetcare.locationTimeoffsetType.label = Standort Wechsel Zeitversatz
channel-type.surepetcare.locationTimeoffsetType.description = Änderung des Haustier Standortes mit einem Zeitversatz. (vor 10, 30 oder 60 Minuten).
channel-type.surepetcare.locationTimeoffsetType.state.option.10 = vor 10 min
channel-type.surepetcare.locationTimeoffsetType.state.option.30 = vor 30 min
channel-type.surepetcare.locationTimeoffsetType.state.option.60 = vor 60 min

channel-type.surepetcare.locationChangedThroughType.label = Standort geändert durch
channel-type.surepetcare.locationChangedThroughType.description = Zeigt an durch welchen Benutzer oder welches Gerät die letzte Standort-Änderung durchgeführt wurde.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<item-type>Switch</item-type>
<label>Refresh</label>
<description>Triggers a cache refresh of everything (devices, pets etc.).</description>
<state readOnly="false" pattern="%s"></state>
<state readOnly="false" pattern="%s" />
</channel-type>

</thing:thing-descriptions>
Loading

0 comments on commit fc678cb

Please sign in to comment.