adding packages

master
Matt Huntington 15 years ago
parent 016a82ee69
commit cda6d6f5a9

@ -0,0 +1,44 @@
package com.matthewhuntington.homeautomation;
public class Action
{
protected int socket;
protected char house;
protected boolean apply_to_all_sockets;
protected int value;
public Action(char house, int value)
{
this.house=house;
this.apply_to_all_sockets=true;
this.value=value;
}
public Action(char house, int socket, int value)
{
this.house=house;
this.socket=socket;
this.apply_to_all_sockets=false;
this.value=value;
}
public int getSocket()
{
return socket;
}
public char getHouse()
{
return house;
}
public boolean getApplyToAllSockets()
{
return apply_to_all_sockets;
}
public int getValue()
{
return value;
}
}

@ -0,0 +1,87 @@
package com.matthewhuntington.homeautomation;
import java.util.ArrayList;
public class ActionExecutor
{
protected ArrayList<Action> actions;
protected static final String program_name = "br";
protected static final String port_param = "--port=";
protected static final String house_param = "--house=";
protected static final String on_param = "--on=";
protected static final String off_param = "--off=";
protected static final String on = "--ON";
protected static final String off = "--OFF";
protected static final String default_port = "/dev/ttyS0";
protected static final String default_command_start = program_name + " " + port_param + default_port + " " + house_param;
public ActionExecutor(ArrayList<Action> actions)
{
this.actions = actions;
}
protected void call(String command)
{
try
{
Runtime.getRuntime().exec(command);
//System.out.println(command);
}
catch(Exception e)
{
System.out.println("Could not execute runtime command");
e.printStackTrace();
}
}
public void activate(char house, int socket)
{
String command = default_command_start + house + " " + on_param + socket;
call(command);
}
public void activate(char house)
{
String command = default_command_start + house + " " + on;
call(command);
}
public void deactivate(char house, int socket)
{
String command = default_command_start + house + " " + off_param + socket;
call(command);
}
public void deactivate(char house)
{
String command = default_command_start + house + " " + off;
call(command);
}
public void execute(Action action)
{
if(action.getApplyToAllSockets())
{
if(action.getValue()==1)
activate(action.getHouse());
else if(action.getValue()==0)
deactivate(action.getHouse());
}
else
{
if(action.getValue()==1)
activate(action.getHouse(), action.getSocket());
else if(action.getValue()==0)
deactivate(action.getHouse(), action.getSocket());
}
}
public void executeAll()
{
for(int i=0; i<actions.size(); i++)
{
execute(actions.get(i));
}
}
}

@ -0,0 +1,71 @@
package com.matthewhuntington.homeautomation;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.google.gdata.client.calendar.CalendarQuery;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import com.google.gdata.data.extensions.When;
public class CalendarParser
{
protected ArrayList<Action> actions = new ArrayList<Action>();
public CalendarParser(String id, String user, String password)
{
CalendarService myService = new CalendarService("Home Automation");
URL feedUrl = null;
CalendarEventFeed resultFeed = null;
try
{
feedUrl = new URL("https://www.google.com/calendar/feeds/" + id + "/private/full");
CalendarQuery myQuery = new CalendarQuery(feedUrl);
myService.setUserCredentials(user, password);
resultFeed = myService.query(myQuery, CalendarEventFeed.class);
}
catch (Exception e)
{
System.out.println("Could not get Calendar data from Google.");
e.printStackTrace();
return;
}
Date now = new Date();
for (int i = 0; i < resultFeed.getEntries().size(); i++)
{
CalendarEventEntry entry = resultFeed.getEntries().get(i);
List<When> times = entry.getTimes();
for (int j = 0; j < times.size(); j++)
{
Date start = new Date(times.get(j).getStartTime().getValue());
Date end = new Date(times.get(j).getEndTime().getValue());
if (now.after(start) && now.before(end))
{
String command = entry.getTitle().getPlainText();
char house = command.charAt(0);
if (command.length() > 1)
{
int socket = Integer.parseInt(command.substring(1));
Action new_action = new Action(house, socket, 1);
actions.add( new_action );
}
else
{
actions.add( new Action(house, 1));
}
}
}
}
}
public ArrayList<Action> getActions()
{
return actions;
}
}
Loading…
Cancel
Save