Skip to content
This repository was archived by the owner on Mar 11, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/main/java/nl/stijngroenen/tradfri/device/Blind.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package nl.stijngroenen.tradfri.device;

import nl.stijngroenen.tradfri.payload.DeviceRequest;
import nl.stijngroenen.tradfri.util.ApiEndpoint;
import nl.stijngroenen.tradfri.util.CoapClient;

public class Blind extends Device{
/**
* Construct the Device class
*
* @param name The name of the device
* @param creationDate The creation date of the device
* @param instanceId The instance id of the device
* @param deviceInfo The information of the device
* @param coapClient A CoAP client that can be used to communicate with the device using the IKEA TRÅDFRI gateway

*/
public Blind(String name, Long creationDate, Integer instanceId, DeviceInfo deviceInfo, CoapClient coapClient) {
super(name, creationDate, instanceId, deviceInfo, coapClient);
}

/**
* Set the position of Blind
* @param position The new position of Blind
* @return True if successfully updated the position, false if not
* @since 1.3.0
*/
public boolean setPosition(Float position) {
BlindProperties newProperties = new BlindProperties();
newProperties.setPosition(position);
return applyUpdate(newProperties);
}

/**
* Apply updates to the plug
* @param newProperties The new properties to apply to the plug
* @return True if successfully updated the plug, false if not
* @since 1.3.0
*/
private boolean applyUpdate(BlindProperties newProperties) {
DeviceRequest request = new DeviceRequest();
request.setBlindProperties(new BlindProperties[]{newProperties});
String response = coapClient.put(ApiEndpoint.getUri(ApiEndpoint.DEVICES, String.valueOf(getInstanceId())), request, String.class);
return response != null;
}

}
27 changes: 27 additions & 0 deletions src/main/java/nl/stijngroenen/tradfri/device/BlindProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package nl.stijngroenen.tradfri.device;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import nl.stijngroenen.tradfri.util.ApiCode;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BlindProperties extends DeviceProperties {

/**
* The position of the blind represented in a float value.
*/
@JsonProperty(ApiCode.POSITION)
@JsonFormat(shape = JsonFormat.Shape.NUMBER_FLOAT)
private Float position;

public Float getPosition() {
return position;
}

public void setPosition(Float position) {
this.position = position;
}
}
20 changes: 20 additions & 0 deletions src/main/java/nl/stijngroenen/tradfri/device/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ public DeviceType getType(){
if(isPlug()) return DeviceType.PLUG;
if(isRemote()) return DeviceType.REMOTE;
if(isMotionSensor()) return DeviceType.MOTION_SENSOR;
if(isBlind()) return DeviceType.BLIND;
return DeviceType.UNKNOWN;
}

Expand Down Expand Up @@ -314,4 +315,23 @@ public MotionSensor toMotionSensor(){
return null;
}

/**
* Check if the device is a {@link Blind}
* @return True if the device is a {@link Blind}, false if not
* @since 1.0.0
*/
public boolean isBlind(){
return this instanceof Blind;
}

/**
* Convert the device to the {@link Blind} class
* @return The device as {@link Blind}
* @since 1.0.0
*/
public Blind toBlind(){
if(isBlind()) return (Blind) this;
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public enum DeviceType {
PLUG,
REMOTE,
MOTION_SENSOR,
BLIND,
}
4 changes: 3 additions & 1 deletion src/main/java/nl/stijngroenen/tradfri/device/Gateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ public Device getDevice(int id){
return new Remote(response.getName(), response.getCreationDate(), response.getInstanceId(), response.getDeviceInfo(), coapClient);
}else if(response.getDeviceInfo().getModelName().equals("TRADFRI motion sensor")){
return new MotionSensor(response.getName(), response.getCreationDate(), response.getInstanceId(), response.getDeviceInfo(), coapClient);
}else{
}else if(response.getDeviceInfo().getModelName().contains("blind")) {
return new Blind(response.getName(), response.getCreationDate(), response.getInstanceId(), response.getDeviceInfo(), coapClient);
} else {
return new Device(response.getName(), response.getCreationDate(), response.getInstanceId(), response.getDeviceInfo(), coapClient);
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/nl/stijngroenen/tradfri/payload/DeviceRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import nl.stijngroenen.tradfri.device.BlindProperties;
import nl.stijngroenen.tradfri.device.LightProperties;
import nl.stijngroenen.tradfri.device.PlugProperties;
import nl.stijngroenen.tradfri.util.ApiCode;
Expand All @@ -42,6 +43,12 @@ public class DeviceRequest {
@JsonProperty(ApiCode.PLUG)
private PlugProperties[] plugProperties;

/**
* The new properties of the Blind (if the device is a blind)
*/
@JsonProperty(ApiCode.BLIND)
private BlindProperties[] blindProperties;

/**
* Construct the DeviceRequest class
* @since 1.0.0
Expand Down Expand Up @@ -84,4 +91,13 @@ public void setLightProperties(LightProperties[] lightProperties) {
public void setPlugProperties(PlugProperties[] plugProperties) {
this.plugProperties = plugProperties;
}

/**
* Set the new properties of the Blind (if the device is a blind)
* @param blindProperties The new properties of the blind
* @since 1.0.0
*/
public void setBlindProperties(BlindProperties[] blindProperties) {
this.blindProperties = blindProperties;
}
}
25 changes: 25 additions & 0 deletions src/main/java/nl/stijngroenen/tradfri/payload/DeviceResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import nl.stijngroenen.tradfri.device.BlindProperties;
import nl.stijngroenen.tradfri.device.DeviceInfo;
import nl.stijngroenen.tradfri.device.LightProperties;
import nl.stijngroenen.tradfri.device.PlugProperties;
Expand Down Expand Up @@ -67,6 +68,12 @@ public class DeviceResponse {
@JsonProperty(ApiCode.PLUG)
private PlugProperties[] plugProperties;

/**
* The properties of the plug (if the device is a plug)
*/
@JsonProperty(ApiCode.BLIND)
private BlindProperties[] blindProperties;

/**
* Construct the DeviceResponse class
* @since 1.0.0
Expand Down Expand Up @@ -181,4 +188,22 @@ public void setLightProperties(LightProperties[] lightProperties) {
public void setPlugProperties(PlugProperties[] plugProperties) {
this.plugProperties = plugProperties;
}

/**
* Get the properties of the blind (if the device is a blind)
* @return The properties of the Blind
* @since 1.2.0
*/
public BlindProperties[] getBlindProperties() {
return this.blindProperties;
}

/**
* Set the properties of the plug (if the device is a plug)
* @param blindProperties The new properties of the plug
* @since 1.2.0
*/
public void setBlindProperties(BlindProperties[] blindProperties) {
this.blindProperties = blindProperties;
}
}