Created
August 12, 2018 03:05
-
-
Save Andrewcpu/e251ddf0303e3b664e06209fd7cc2944 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.net.ssl.HttpsURLConnection; | |
import java.io.*; | |
import java.net.*; | |
public class Main { | |
public static void main(String args[]) throws Exception { | |
/* create byte arrays to hold our send and response data */ | |
// HTTPBuilder http = new HTTPBuilder("http://192.168.1.120:49153/upnp/control/basicevent1") | |
// http.request(Method.POST, ContentType.XML) { | |
// body = '<?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>' | |
// headers.'SOAPACTION' = "\"urn:Belkin:service:basicevent:1#SetBinaryState\"" | |
// headers.'Content-Type' = "text/xml; charset=\"utf-8\"" | |
// headers.'Accept' = "" | |
// } | |
// socket = new Socket("192.168.1.254",49153); | |
// | |
// socket.setKeepAlive(true); | |
//// | |
//// | |
//// sendBytes(hexStringToByteArray("0000 60 38 e0 ee c5 b1 70 8b cd be e4 40 08 00 45 00\n" + | |
//// "0010 00 28 77 e8 40 00 80 06 00 00 c0 a8 01 12 c0 a8\n" + | |
//// "0020 01 97 eb ae c0 01 13 0b b1 fb 43 27 3f 93 50 10\n" + | |
//// "0030 00 ff 84 14 00 00\n")); | |
// | |
//// sendBytes(hexStringToByteArray("6038e0eec5b1708bcdbee44008004500002803e2400080060000c0a80112c0a80197efcbc0012e7806d4d90a90dc5010080584140000")); | |
// sendBytes(hexStringToByteArray("01005e7ffffab4f1dab4bc8f080045000091153c40000111b1d8c0a801a5effffffa1f48076c007d3e334d2d534541524348202a20485454502f312e310d0a53543a2075726e3a42656c6b696e3a736572766963653a62617369636576656e743a310d0a4d583a20320d0a4d414e3a2022737364703a646973636f766572220d0a484f53543a203233392e3235352e3235352e3235303a313930300d0a0d0a")); | |
// | |
// PrintWriter writer = null; | |
// BufferedReader bufferedReader = null; | |
// try{ | |
// | |
//// writer = new PrintWriter(socket.getOutputStream(), true); | |
//// writer.println(command); | |
//// writer.flush(); | |
// | |
// bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
// String str; | |
// while ((str = bufferedReader.readLine()) != null) { | |
// System.out.println(str); | |
// } | |
// }catch (Exception ex){ex.printStackTrace();} | |
// | |
// | |
// toggleLight(false); | |
if(args[0].equalsIgnoreCase("true")){ | |
toggleLight(true); | |
} | |
else if(args[0].equalsIgnoreCase("false")){ | |
toggleLight(false); | |
} | |
else if (args[0].equalsIgnoreCase("strobe")) { | |
boolean on = false; | |
while(true){ | |
Thread.sleep(100); | |
toggleLight(on); | |
on = !on; | |
} | |
} | |
else if(args[0].equalsIgnoreCase("toggle")){ | |
toggleLight(!getSwitchState()); | |
} | |
else if(args[0].equalsIgnoreCase("togglequick")){ | |
toggleLight(!getSwitchState()); | |
Thread.sleep(500); | |
toggleLight(!getSwitchState()); | |
Thread.sleep(500); | |
toggleLight(!getSwitchState()); | |
Thread.sleep(500); | |
toggleLight(!getSwitchState()); | |
} | |
// System.out.println(getSwitchState()); | |
// toggleLight(false); | |
} | |
public static boolean getSwitchState() throws Exception{ | |
String url = "http://192.168.1.254:49153/upnp/control/basicevent1"; | |
URL obj = new URL(url); | |
HttpURLConnection con = (HttpURLConnection) obj.openConnection(); | |
//add reuqest header | |
con.setRequestMethod("POST"); | |
con.setRequestProperty("User-Agent", "Mozilla / 5.0"); | |
con.setRequestProperty("SOAPACTION","\"urn:Belkin:service:basicevent:1#GetBinaryState\""); | |
con.setRequestProperty("Content-Type","text/xml; charset=\"utf-8\""); | |
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); | |
String body = ""; | |
body = "<?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:GetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\">" + | |
"<BinaryState>1</BinaryState>" + | |
"</u:GetBinaryState>" + | |
"</s:Body>" + | |
"</s:Envelope>"; | |
String urlParameters = body; | |
// Send post request | |
con.setDoOutput(true); | |
DataOutputStream wr = new DataOutputStream(con.getOutputStream()); | |
wr.writeBytes(urlParameters); | |
wr.flush(); | |
wr.close(); | |
BufferedReader in = new BufferedReader( | |
new InputStreamReader(con.getInputStream())); | |
String inputLine; | |
StringBuffer response = new StringBuffer(); | |
while ((inputLine = in.readLine()) != null) { | |
response.append(inputLine.toString()); | |
} | |
in.close(); | |
//print result | |
String[] split= response.toString().split(" "); | |
//System.out.println(split[0]); | |
// System.out.println(response.toString()); | |
// if(split[1].charAt(0) == '0') return false; | |
return response.toString().contains(">1<"); | |
} | |
public static void toggleLight(boolean on) throws Exception{ | |
String url = "http://192.168.1.254:49153/upnp/control/basicevent1"; | |
URL obj = new URL(url); | |
HttpURLConnection con = (HttpURLConnection) obj.openConnection(); | |
//add reuqest header | |
con.setRequestMethod("POST"); | |
con.setRequestProperty("User-Agent", "Mozilla / 5.0"); | |
con.setRequestProperty("SOAPACTION","\"urn:Belkin:service:basicevent:1#SetBinaryState\""); | |
con.setRequestProperty("Content-Type","text/xml; charset=\"utf-8\""); | |
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); | |
String body = ""; | |
body = "<?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>" + (on ? "1" : "0") + "</BinaryState>" + | |
"</u:SetBinaryState>" + | |
"</s:Body>" + | |
"</s:Envelope>"; | |
String urlParameters = body; | |
// Send post request | |
con.setDoOutput(true); | |
DataOutputStream wr = new DataOutputStream(con.getOutputStream()); | |
wr.writeBytes(urlParameters); | |
wr.flush(); | |
wr.close(); | |
int responseCode = con.getResponseCode(); | |
System.out.println("\nSending 'POST' request to URL : " + url); | |
System.out.println("Post parameters : " + urlParameters); | |
System.out.println("Response Code : " + responseCode); | |
BufferedReader in = new BufferedReader( | |
new InputStreamReader(con.getInputStream())); | |
String inputLine; | |
StringBuffer response = new StringBuffer(); | |
while ((inputLine = in.readLine()) != null) { | |
System.out.print(inputLine); | |
} | |
in.close(); | |
//print result | |
System.out.println(response.toString()); | |
} | |
public static void test() throws Exception{ | |
byte[] sendData = new byte[1024]; | |
byte[] receiveData = new byte[1024]; | |
/* our M-SEARCH data as a byte array */ | |
String MSEARCH = "M-SEARCH * HTTP/1.1\n" + | |
"ST: urn:Belkin:service:basicevent:1\n" + | |
"MX: 2\n" + | |
"MAN: \"ssdp:discover\"\n" + | |
"HOST: 239.255.255.250:1900\n" + | |
"\n"; | |
sendData = MSEARCH.getBytes(); | |
/* create a packet from our data destined for 239.255.255.250:1900 */ | |
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName("239.255.255.250"), 1900); | |
/* send packet to the socket we're creating */ | |
DatagramSocket clientSocket = new DatagramSocket(); | |
clientSocket.send(sendPacket); | |
/* recieve response and store in our receivePacket */ | |
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); | |
clientSocket.receive(receivePacket); | |
/* get the response as a string */ | |
String response = new String(receivePacket.getData()); | |
/* print the response */ | |
System.out.println(response); | |
/* close the socket */ | |
clientSocket.close(); | |
} | |
private static Socket socket; | |
public static void sendBytes(byte[] myByteArray) throws IOException { | |
sendBytes(myByteArray, 0, myByteArray.length); | |
} | |
public static void sendBytes(byte[] myByteArray, int start, int len) throws IOException { | |
if (len < 0) | |
throw new IllegalArgumentException("Negative length not allowed"); | |
if (start < 0 || start >= myByteArray.length) | |
throw new IndexOutOfBoundsException("Out of bounds: " + start); | |
// Other checks if needed. | |
// May be better to save the streams in the support class; | |
// just like the socket variable. | |
OutputStream out = socket.getOutputStream(); | |
DataOutputStream dos = new DataOutputStream(out); | |
dos.writeInt(len); | |
if (len > 0) { | |
dos.write(myByteArray, start, len); | |
} | |
} | |
public static byte[] hexStringToByteArray(String s) { | |
int len = s.length(); | |
byte[] data = new byte[len / 2]; | |
for (int i = 0; i < len; i += 2) { | |
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) | |
+ Character.digit(s.charAt(i+1), 16)); | |
} | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment