d020b75052dad189feebfaac07d18a86ba9d2dfa
[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
31     public void turnOff() throws IOException {
32         IoTHTTP httpConnection = null;
33         try {
34             httpConnection = new IoTHTTP(deviceAddress);
35             httpConnection.setURL("/upnp/control/basicevent1");
36
37             httpConnection.openConnection();
38             httpConnection.setDoOutput(true);
39             httpConnection.setRequestMethod("POST");
40             httpConnection.setRequestProperty("Content-type", "text/xml; charset=\"utf-8\"");
41             httpConnection.setRequestProperty("SOAPACTION", "\"urn:Belkin:service:basicevent:1#SetBinaryState\"");
42             httpConnection.setRequestProperty("Accept", "");
43
44             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";
45
46             OutputStream reqStream = httpConnection.getOutputStream();
47             reqStream.write(reqXML.getBytes());
48
49             InputStream resStream = httpConnection.getInputStream();
50             byte[] byteBuf = new byte[10240];
51             int len = resStream.read(byteBuf);
52
53             reqStream.close();
54             resStream.close();
55
56
57         } finally {
58             if (httpConnection != null) {
59                 try {
60                     httpConnection.disconnect();
61
62                 } catch (Exception e) {
63                     e.printStackTrace();
64
65                 }
66             }
67         }
68
69     }
70
71     public void turnOn() throws IOException {
72         IoTHTTP httpConnection = null;
73         try {
74             httpConnection = new IoTHTTP(deviceAddress);
75             httpConnection.setURL("/upnp/control/basicevent1");
76
77             httpConnection.openConnection();
78             httpConnection.setDoOutput(true);
79             httpConnection.setRequestMethod("POST");
80             httpConnection.setRequestProperty("Content-type", "text/xml; charset=\"utf-8\"");
81             httpConnection.setRequestProperty("SOAPACTION", "\"urn:Belkin:service:basicevent:1#SetBinaryState\"");
82             httpConnection.setRequestProperty("Accept", "");
83
84             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";
85
86             OutputStream reqStream = httpConnection.getOutputStream();
87             reqStream.write(reqXML.getBytes());
88
89             InputStream resStream = httpConnection.getInputStream();
90             byte[] byteBuf = new byte[10240];
91             int len = resStream.read(byteBuf);
92
93             reqStream.close();
94             resStream.close();
95
96         } finally {
97             if (httpConnection != null) {
98                 try {
99                     httpConnection.disconnect();
100
101                 } catch (Exception e) {
102                     e.printStackTrace();
103
104                 }
105             }
106         }
107     }
108 }