diff --git a/src/main/java/nl/stijngroenen/tradfri/device/Blind.java b/src/main/java/nl/stijngroenen/tradfri/device/Blind.java new file mode 100644 index 0000000..9846431 --- /dev/null +++ b/src/main/java/nl/stijngroenen/tradfri/device/Blind.java @@ -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; + } + +} diff --git a/src/main/java/nl/stijngroenen/tradfri/device/BlindProperties.java b/src/main/java/nl/stijngroenen/tradfri/device/BlindProperties.java new file mode 100644 index 0000000..d89b4da --- /dev/null +++ b/src/main/java/nl/stijngroenen/tradfri/device/BlindProperties.java @@ -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; + } +} diff --git a/src/main/java/nl/stijngroenen/tradfri/device/Device.java b/src/main/java/nl/stijngroenen/tradfri/device/Device.java index 319bbd2..8f3d191 100644 --- a/src/main/java/nl/stijngroenen/tradfri/device/Device.java +++ b/src/main/java/nl/stijngroenen/tradfri/device/Device.java @@ -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; } @@ -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; + } + } diff --git a/src/main/java/nl/stijngroenen/tradfri/device/DeviceType.java b/src/main/java/nl/stijngroenen/tradfri/device/DeviceType.java index fd257c1..f05f685 100644 --- a/src/main/java/nl/stijngroenen/tradfri/device/DeviceType.java +++ b/src/main/java/nl/stijngroenen/tradfri/device/DeviceType.java @@ -11,4 +11,5 @@ public enum DeviceType { PLUG, REMOTE, MOTION_SENSOR, + BLIND, } diff --git a/src/main/java/nl/stijngroenen/tradfri/device/Gateway.java b/src/main/java/nl/stijngroenen/tradfri/device/Gateway.java index 90748d7..e19ada2 100644 --- a/src/main/java/nl/stijngroenen/tradfri/device/Gateway.java +++ b/src/main/java/nl/stijngroenen/tradfri/device/Gateway.java @@ -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); } } diff --git a/src/main/java/nl/stijngroenen/tradfri/payload/DeviceRequest.java b/src/main/java/nl/stijngroenen/tradfri/payload/DeviceRequest.java index 8ff13c8..48ab858 100644 --- a/src/main/java/nl/stijngroenen/tradfri/payload/DeviceRequest.java +++ b/src/main/java/nl/stijngroenen/tradfri/payload/DeviceRequest.java @@ -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; @@ -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 @@ -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; + } } diff --git a/src/main/java/nl/stijngroenen/tradfri/payload/DeviceResponse.java b/src/main/java/nl/stijngroenen/tradfri/payload/DeviceResponse.java index e0c025e..c550df6 100644 --- a/src/main/java/nl/stijngroenen/tradfri/payload/DeviceResponse.java +++ b/src/main/java/nl/stijngroenen/tradfri/payload/DeviceResponse.java @@ -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; @@ -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 @@ -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; + } }