From 7e1feb6939159232ed39c403d517bafaee04a86a Mon Sep 17 00:00:00 2001 From: Dawit Gulino 20013954 <32634333+DawitG96@users.noreply.github.com> Date: Tue, 5 Jun 2018 14:51:40 +0200 Subject: [PATCH 1/2] Added Sensor --- src/main/java/device/Sensor.java | 57 ++++++++++++++++++++-------- src/main/java/manage/AuthFITBIT.java | 5 +-- src/test/java/TestLights.java | 7 ++-- src/test/java/TestSensor.java | 29 +++++++++----- 4 files changed, 66 insertions(+), 32 deletions(-) diff --git a/src/main/java/device/Sensor.java b/src/main/java/device/Sensor.java index ece2924..572a944 100644 --- a/src/main/java/device/Sensor.java +++ b/src/main/java/device/Sensor.java @@ -12,31 +12,58 @@ public class Sensor { Logger logger = LoggerFactory.getLogger(Sensor.class); // sample RaZberry IP address - String ipAddress = "http://172.30.1.137:8083"; + public String ipAddress = "172.30.1.137"; // sample username and password - String username = "admin"; - String password = "raz4reti2"; + public String username = "admin"; + public String password = "raz4reti2"; - IZWayApi zwayApi; + public IZWayApi zwayApi; + DeviceList allZWaveDevices; + DeviceList devices; public Sensor() { - // create an instance of the Z-Way library; all the params are mandatory (we are not going to use the remote service/id) - zwayApi = new ZWayApiHttp(ipAddress, 8083, "http", username, password, 0, false, new ZWaySimpleCallback()); + this(null); } - // get all the Z-Wave devices - DeviceList allDevices = zwayApi.getDevices(); + public Sensor (Integer nodeId) { + // create an instance of the Z-Way library; all the params are mandatory (we are not going to use the remote service/id) + zwayApi = new ZWayApiHttp(ipAddress, 8083, "http", username, password, 0, false, new ZWaySimpleCallback()); - public boolean IsLowLuminescence(int Luminescence) { - for (Device dev : allDevices.getAllDevices()) { - if (dev.getDeviceType().equalsIgnoreCase("SensorMultilevel")) - if (dev.getProbeType().equalsIgnoreCase("luminescence")) + // get all the Z-Wave devices + allZWaveDevices = zwayApi.getDevices(); + + if(nodeId != null) + useNode(nodeId); + else + devices = allZWaveDevices; + } + + public void useNode(int nodeId) { + devices = new DeviceList(); + for (Device devi : allZWaveDevices.getAllDevices()) + if(devi.getNodeId() == nodeId) + devices.addDevice(devi); + } + + public int luminiscenceLevel() { + for (Device device : devices.getAllDevices()) + if (device.getMetrics().getProbeTitle().equalsIgnoreCase("luminiscence")) + return Integer.parseInt(device.getMetrics().getLevel()); + return -99; + } + + synchronized public void update(int nodeId) throws InterruptedException { + for (Device device : devices.getAllDevices()) + device.update(); + wait(10000); + } + /*public boolean IsLowLuminescence(int Luminescence) { + if (dev.getProbeType().equalsIgnoreCase("Luminescence")) if (Integer.parseInt(dev.getMetrics().getLevel()) < Luminescence) return true; else return false; - } - return false; - } + return false; + }*/ } diff --git a/src/main/java/manage/AuthFITBIT.java b/src/main/java/manage/AuthFITBIT.java index 30739df..ccc7ae0 100644 --- a/src/main/java/manage/AuthFITBIT.java +++ b/src/main/java/manage/AuthFITBIT.java @@ -1,9 +1,6 @@ package manage; -import com.google.api.client.auth.oauth2.AuthorizationCodeFlow; -import com.google.api.client.auth.oauth2.BearerToken; -import com.google.api.client.auth.oauth2.ClientParametersAuthentication; -import com.google.api.client.auth.oauth2.Credential; +import com.google.api.client.auth.oauth2.*; import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; import com.google.api.client.http.GenericUrl; diff --git a/src/test/java/TestLights.java b/src/test/java/TestLights.java index 3878584..1b8fcf3 100644 --- a/src/test/java/TestLights.java +++ b/src/test/java/TestLights.java @@ -26,16 +26,17 @@ public class TestLights { lights.turnOn(); for(int i=256; i>=0; i--) { lights.setBrightness(i); - this.wait(50); + this.wait(25); } for(int i=0; i<256; i++) { lights.setBrightness(i); - this.wait(50); + this.wait(25); } + lights.setBrightness(150); lights.colorLoop(); - this.wait(10000); // 10 sec + this.wait(20000); // 10 sec lights.turnOff(); } diff --git a/src/test/java/TestSensor.java b/src/test/java/TestSensor.java index 16fd220..8a73166 100644 --- a/src/test/java/TestSensor.java +++ b/src/test/java/TestSensor.java @@ -2,22 +2,31 @@ import device.Hue; import device.Sensor; import org.junit.Test; -import static org.junit.Assert.assertTrue; +import java.util.HashSet; +import java.util.Set; -public class TestSensor { - Sensor sensor = new Sensor(); - Hue hue = new Hue(); + public class TestSensor { + Sensor sensor = new Sensor(5); + Hue lights = new Hue(); @Test - public void firstTestSensor() { - assertTrue(sensor.IsLowLuminescence(50)); + synchronized public void firstTestSensor() throws InterruptedException { + sensor.update(5); + System.out.println(sensor.luminiscenceLevel()); } @Test - public void secondTestSensor() { - if(sensor.IsLowLuminescence(50)) { - hue.turnOn(); - hue.setBrightness(200); + public void secondTestSensor() throws InterruptedException { + sensor.update(5); + Set toRemove = new HashSet<>(); + for(String str: lights.getNameLights()) + if(!str.equals("4")) + toRemove.add(str); + lights.removeLights(toRemove); + + if(sensor.luminiscenceLevel() < 100) { + lights.turnOn(); + lights.setBrightness(200); } } } -- 2.49.1 From e8c2de7d84bd97c3b8ade6424b94b6673091da35 Mon Sep 17 00:00:00 2001 From: Dawit Gulino 20013954 <32634333+DawitG96@users.noreply.github.com> Date: Wed, 27 Jun 2018 10:31:41 +0200 Subject: [PATCH 2/2] Modified something --- src/main/java/device/Hue.java | 3 +++ src/main/java/device/Sensor.java | 12 ++++++++---- src/test/java/TestSensor.java | 28 ++++++++++++++++++++-------- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/main/java/device/Hue.java b/src/main/java/device/Hue.java index 1ee4c98..7257150 100644 --- a/src/main/java/device/Hue.java +++ b/src/main/java/device/Hue.java @@ -37,6 +37,9 @@ public class Hue { } } + public int getCurrentLuminiscence() { + return 0; + } public void turnOff() { for (String light : allLights.keySet()) { String callURL = lightsURL + light + "/state"; diff --git a/src/main/java/device/Sensor.java b/src/main/java/device/Sensor.java index 572a944..0aa2c8b 100644 --- a/src/main/java/device/Sensor.java +++ b/src/main/java/device/Sensor.java @@ -19,14 +19,17 @@ public class Sensor { public String password = "raz4reti2"; public IZWayApi zwayApi; - DeviceList allZWaveDevices; - DeviceList devices; + private DeviceList allZWaveDevices; + private DeviceList devices; + private Integer nodeId; public Sensor() { this(null); } public Sensor (Integer nodeId) { + this.nodeId = nodeId; + // create an instance of the Z-Way library; all the params are mandatory (we are not going to use the remote service/id) zwayApi = new ZWayApiHttp(ipAddress, 8083, "http", username, password, 0, false, new ZWaySimpleCallback()); @@ -53,10 +56,11 @@ public class Sensor { return -99; } - synchronized public void update(int nodeId) throws InterruptedException { + synchronized public void update(int timeout) throws InterruptedException { + //setInitialValues(); for (Device device : devices.getAllDevices()) device.update(); - wait(10000); + wait(timeout); } /*public boolean IsLowLuminescence(int Luminescence) { if (dev.getProbeType().equalsIgnoreCase("Luminescence")) diff --git a/src/test/java/TestSensor.java b/src/test/java/TestSensor.java index 8a73166..720564c 100644 --- a/src/test/java/TestSensor.java +++ b/src/test/java/TestSensor.java @@ -6,27 +6,39 @@ import java.util.HashSet; import java.util.Set; public class TestSensor { - Sensor sensor = new Sensor(5); - Hue lights = new Hue(); + Sensor sensor = new Sensor(2); + Hue lights; @Test synchronized public void firstTestSensor() throws InterruptedException { - sensor.update(5); + sensor.update(2); System.out.println(sensor.luminiscenceLevel()); } @Test - public void secondTestSensor() throws InterruptedException { - sensor.update(5); + synchronized public void secondTestSensor() throws InterruptedException { + int i=0; + lights = new Hue(); Set toRemove = new HashSet<>(); + for(String str: lights.getNameLights()) if(!str.equals("4")) toRemove.add(str); lights.removeLights(toRemove); + lights.turnOn(); - if(sensor.luminiscenceLevel() < 100) { - lights.turnOn(); - lights.setBrightness(200); + while(i<999999) { + if (sensor.luminiscenceLevel() < 200) { + lights.getCurrentLuminiscence(); + lights.setBrightness(200); + } + + if (sensor.luminiscenceLevel() > 600) { + lights.setBrightness(0); + } + System.out.println(i+"-"+sensor.luminiscenceLevel()); + sensor.update(100); + i++; } } } -- 2.49.1