Embeded Benchmark
[iotcloud.git] / version2 / src / java / light_fan_embed_benchmark / Wemo.java
diff --git a/version2/src/java/light_fan_embed_benchmark/Wemo.java b/version2/src/java/light_fan_embed_benchmark/Wemo.java
new file mode 100644 (file)
index 0000000..98b620f
--- /dev/null
@@ -0,0 +1,109 @@
+// IoT Packages
+
+//import iotcode.annotation.*;
+
+// Native Java Packages
+import java.util.Iterator;
+import javax.xml.parsers.*;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import org.w3c.dom.*;
+import org.xml.sax.SAXException;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.Semaphore;
+import java.util.List;
+import java.util.ArrayList;
+
+public class Wemo {
+
+    private IoTDeviceAddress deviceAddress = null;
+
+    public Wemo(IoTDeviceAddress _deviceAddress) {
+        deviceAddress = _deviceAddress;
+
+    }
+
+    public void turnOff() throws IOException {
+        IoTHTTP httpConnection = null;
+        try {
+            httpConnection = new IoTHTTP(deviceAddress);
+            httpConnection.setURL("/upnp/control/basicevent1");
+
+            httpConnection.openConnection();
+            httpConnection.setDoOutput(true);
+            httpConnection.setRequestMethod("POST");
+            httpConnection.setRequestProperty("Connection", "close");
+            httpConnection.setRequestProperty("Content-type", "text/xml; charset=\"utf-8\"");
+            httpConnection.setRequestProperty("SOAPACTION", "\"urn:Belkin:service:basicevent:1#SetBinaryState\"");
+            
+            httpConnection.setRequestProperty("User-Agent", "Java/1.8.0");
+            httpConnection.setRequestProperty("Host", "\"192.168.1.5:49153");
+            httpConnection.setRequestProperty("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
+
+            String reqXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:SetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\"><BinaryState>0</BinaryState></u:SetBinaryState></s:Body></s:Envelope>\n";
+
+            OutputStream reqStream = httpConnection.getOutputStream();
+            reqStream.write(reqXML.getBytes());
+
+            InputStream resStream = httpConnection.getInputStream();
+            byte[] byteBuf = new byte[10240];
+            int len = resStream.read(byteBuf);
+
+            reqStream.close();
+            resStream.close();
+
+
+        } finally {
+            if (httpConnection != null) {
+                try {
+                    httpConnection.disconnect();
+
+                } catch (Exception e) {
+                    e.printStackTrace();
+
+                }
+            }
+        }
+
+    }
+
+    public void turnOn() throws IOException {
+        IoTHTTP httpConnection = null;
+        try {
+            httpConnection = new IoTHTTP(deviceAddress);
+            httpConnection.setURL("/upnp/control/basicevent1");
+
+            httpConnection.openConnection();
+            httpConnection.setDoOutput(true);
+            httpConnection.setRequestMethod("POST");
+            httpConnection.setRequestProperty("Content-type", "text/xml; charset=\"utf-8\"");
+            httpConnection.setRequestProperty("SOAPACTION", "\"urn:Belkin:service:basicevent:1#SetBinaryState\"");
+            httpConnection.setRequestProperty("Accept", "");
+
+            String reqXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:SetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\"><BinaryState>1</BinaryState></u:SetBinaryState></s:Body></s:Envelope>\n";
+
+            OutputStream reqStream = httpConnection.getOutputStream();
+            reqStream.write(reqXML.getBytes());
+
+            InputStream resStream = httpConnection.getInputStream();
+            byte[] byteBuf = new byte[10240];
+            int len = resStream.read(byteBuf);
+
+            reqStream.close();
+            resStream.close();
+
+        } finally {
+            if (httpConnection != null) {
+                try {
+                    httpConnection.disconnect();
+
+                } catch (Exception e) {
+                    e.printStackTrace();
+
+                }
+            }
+        }
+    }
+}
\ No newline at end of file