Embeded Benchmark
[iotcloud.git] / version2 / src / java / light_fan_benchmark / Wemo.java
1 // IoT Packages
2
3 //import iotcode.annotation.*;
4
5 // Native Java Packages
6 import java.util.Iterator;
7 import javax.xml.parsers.*;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10 import java.io.IOException;
11 import java.nio.charset.StandardCharsets;
12 import org.w3c.dom.*;
13 import org.xml.sax.SAXException;
14 import java.util.concurrent.atomic.AtomicBoolean;
15 import java.util.concurrent.Semaphore;
16 import java.util.List;
17 import java.util.ArrayList;
18
19 public class Wemo {
20
21     private IoTDeviceAddress deviceAddress = null;
22
23     public Wemo(IoTDeviceAddress _deviceAddress) {
24         deviceAddress = _deviceAddress;
25
26     }
27
28
29
30     public void turnOff() throws IOException {
31         IoTHTTP httpConnection = null;
32         try {
33             httpConnection = new IoTHTTP(deviceAddress);
34             httpConnection.setURL("/upnp/control/basicevent1");
35
36             httpConnection.openConnection();
37             httpConnection.setDoOutput(true);
38             httpConnection.setRequestMethod("POST");
39             httpConnection.setRequestProperty("Connection", "close");
40             httpConnection.setRequestProperty("Content-type", "text/xml; charset=\"utf-8\"");
41             httpConnection.setRequestProperty("SOAPACTION", "\"urn:Belkin:service:basicevent:1#SetBinaryState\"");
42             
43             httpConnection.setRequestProperty("User-Agent", "Java/1.8.0");
44             httpConnection.setRequestProperty("Host", "\"192.168.1.5:49153");
45             httpConnection.setRequestProperty("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
46
47             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";
48
49             OutputStream reqStream = httpConnection.getOutputStream();
50             reqStream.write(reqXML.getBytes());
51
52             InputStream resStream = httpConnection.getInputStream();
53             byte[] byteBuf = new byte[10240];
54             int len = resStream.read(byteBuf);
55
56             reqStream.close();
57             resStream.close();
58
59
60         } finally {
61             if (httpConnection != null) {
62                 try {
63                     httpConnection.disconnect();
64
65                 } catch (Exception e) {
66                     e.printStackTrace();
67
68                 }
69             }
70         }
71
72     }
73
74     public void turnOn() throws IOException {
75         IoTHTTP httpConnection = null;
76         try {
77             httpConnection = new IoTHTTP(deviceAddress);
78             httpConnection.setURL("/upnp/control/basicevent1");
79
80             httpConnection.openConnection();
81             httpConnection.setDoOutput(true);
82             httpConnection.setRequestMethod("POST");
83             httpConnection.setRequestProperty("Content-type", "text/xml; charset=\"utf-8\"");
84             httpConnection.setRequestProperty("SOAPACTION", "\"urn:Belkin:service:basicevent:1#SetBinaryState\"");
85             httpConnection.setRequestProperty("Accept", "");
86
87             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";
88
89             OutputStream reqStream = httpConnection.getOutputStream();
90             reqStream.write(reqXML.getBytes());
91
92             InputStream resStream = httpConnection.getInputStream();
93             byte[] byteBuf = new byte[10240];
94             int len = resStream.read(byteBuf);
95
96             reqStream.close();
97             resStream.close();
98
99         } finally {
100             if (httpConnection != null) {
101                 try {
102                     httpConnection.disconnect();
103
104                 } catch (Exception e) {
105                     e.printStackTrace();
106
107                 }
108             }
109         }
110     }
111 }