Skip to content

Instantly share code, notes, and snippets.

@Andrewcpu
Created August 14, 2018 20:28
Show Gist options
  • Save Andrewcpu/20134b8313019594aef9ba0656a431f6 to your computer and use it in GitHub Desktop.
Save Andrewcpu/20134b8313019594aef9ba0656a431f6 to your computer and use it in GitHub Desktop.
package lights;
public class Light {
private String location;
private int port;
private String controlMethod;
public Light(String location, int port, String controlMethod) {
this.location = location;
this.port = port;
this.controlMethod = controlMethod;
}
public void toggle() throws Exception{
}
public void on() throws Exception{
}
public void off() throws Exception{
}
public boolean getState() throws Exception{
return false;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getControlMethod() {
return controlMethod;
}
public void setControlMethod(String controlMethod) {
this.controlMethod = controlMethod;
}
public String getFullURL(){
return "http://" + location + ":" + port + controlMethod;
}
public static Light createFromIPPortType(String ip, int port, LightType type){
Light light = null;
switch (type){
case WEMO:
light = new WemoSocket(ip,port);
break;
}
return light;
}
}
package lights;
public enum LightType {
WEMO;
}
package lights;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class WemoSocket extends Light {
public static String controlMethod = "/upnp/control/basicevent1";
public WemoSocket(String location, int port) {
super(location, port, controlMethod);
}
@Override
public void toggle() throws Exception{
URL obj = new URL(getFullURL());
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
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 = "<?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>" + (!getState() ? "1" : "0") + "</BinaryState>" +
"</u:SetBinaryState>" +
"</s:Body>" +
"</s:Envelope>";
String urlParameters = body;
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) {
System.out.print(inputLine);
}
in.close();
}
@Override
public void on() throws Exception{
URL obj = new URL(getFullURL());
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
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 = "<?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>";
String urlParameters = body;
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) {
// System.out.print(inputLine);
}
in.close();
}
@Override
public void off() throws Exception{
URL obj = new URL(getFullURL());
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
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 = "<?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>";
String urlParameters = body;
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) {
// System.out.print(inputLine);
}
in.close();
}
@Override
public boolean getState() throws Exception {
URL obj = new URL(getFullURL());
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
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 = "<?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;
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);
}
in.close();
return response.toString().contains(">1<"); //>1< only appears if its on
}
}
import lights.LightType;
import lights.WemoSocket;
import management.Group;
import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static List<Group> groups = new ArrayList<>();
public static void main(String[] args) throws Exception{
if(args.length < 1) System.out.println("Invalid program arguments");
loadGroups();
if(args[0].equalsIgnoreCase("group")){ //group
Group group = getGroup(args[1]);
List<String> actions = new ArrayList<>();
for(int i = 2; i<args.length; i++){
actions.add(args[i]);
}
for(String action: actions){
if(action.split(":").length == 2){
if(action.split(":")[0].equalsIgnoreCase("sleep")){
try {
Thread.sleep(Integer.parseInt(action.split(":")[1]));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
continue;
}
if(action.equalsIgnoreCase("off")){
group.off();
}
else if(action.equalsIgnoreCase("on")){
group.on();
}
else if(action.equalsIgnoreCase("strobe")){
boolean on = true;
while(true){
if(on)
group.off();
else
group.on();
on = !on;
}
}
else if(action.equalsIgnoreCase("togglequick")){
group.toggle();
Thread.sleep(150);
group.toggle();
Thread.sleep(150);
group.toggle();
Thread.sleep(150);
group.toggle();
}
else if(action.equalsIgnoreCase("toggle")){
group.toggle();
}
}
}
else{ // individual
String ip = args[1];
int port = Integer.parseInt(args[2]);
LightType type = LightType.valueOf(args[3]);
List<String> actions = new ArrayList<>();
for (int i = 4; i < args.length; i++) {
actions.add(args[i]);
}
for (String action : actions) {
if (action.split(":").length == 2) {
if (action.split(":")[0].equalsIgnoreCase("sleep")) {
try {
Thread.sleep(Integer.parseInt(action.split(":")[1]));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
continue;
}
try {
switch (type) {
case WEMO:
WemoSocket socket = new WemoSocket(ip, port);
if (action.equalsIgnoreCase("on")) {
socket.on();
} else if (action.equalsIgnoreCase("off")) {
socket.off();
} else if (action.equalsIgnoreCase("toggle")) {
socket.toggle();
} else if (action.equalsIgnoreCase("strobe")) {
boolean state = false;
while (true) {
if (state)
socket.off();
else
socket.on();
state = !state;
}
} else if (action.equalsIgnoreCase("togglequick")) {
socket.toggle();
Thread.sleep(150);
socket.toggle();
Thread.sleep(150);
socket.toggle();
Thread.sleep(150);
socket.toggle();
Thread.sleep(150);
} else {
System.out.println("Invalid command.");
}
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static Group getGroup(String name){
for(Group group : groups){
if(group.getName().equalsIgnoreCase(name)){
return group;
}
}
return null;
}
public static void loadGroups() throws Exception{
File file =
new File( "groups.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
String groupName = sc.nextLine();
Group group = new Group(groupName);
group.sync(true);
groups.add(group);
System.out.println("Loaded " + groupName);
}
}
}
package management;
import lights.Light;
import lights.LightType;
import lights.WemoSocket;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Group {
private List<Light> lights = new ArrayList<>();
private String name;
public Group(String name){
this.name = name;
try {
loadGroup();
} catch (Exception e) {
e.printStackTrace();
}
}
private void loadGroup() throws Exception{
File file =
new File(name + ".txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
// System.out.println(sc.nextLine());
String[] args = sc.nextLine().split(" ");
String ip = args[0];
int port = Integer.parseInt(args[1]);
LightType type = LightType.valueOf(args[2]);
switch (type){
case WEMO:
lights.add((WemoSocket)Light.createFromIPPortType(ip,port,type));
break;
}
}
}
public void on() throws Exception{
for(Light light : lights){
light.on();
}
}
public void off() throws Exception{
for(Light light : lights){
light.off();
}
}
public void toggle() throws Exception{
for(Light light : lights){
light.toggle();
}
}
public void sync(boolean on) throws Exception{
for(Light light : lights){
if(on)
light.on();
else
light.off();
}
}
public List<Light> getLights() {
return lights;
}
public void setLights(List<Light> lights) {
this.lights = lights;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package management;
import javax.swing.*;
public class LightGUI extends JFrame {
public LightGUI(){
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment