From cda6d6f5a9fa5d654882051d5680aa19cab8cc1a Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Mon, 15 Aug 2011 13:37:46 -0400 Subject: [PATCH] adding packages --- .../homeautomation/Action.java | 44 ++++++++++ .../homeautomation/ActionExecutor.java | 87 +++++++++++++++++++ .../homeautomation/CalendarParser.java | 71 +++++++++++++++ 3 files changed, 202 insertions(+) create mode 100644 com/matthewhuntington/homeautomation/Action.java create mode 100644 com/matthewhuntington/homeautomation/ActionExecutor.java create mode 100644 com/matthewhuntington/homeautomation/CalendarParser.java diff --git a/com/matthewhuntington/homeautomation/Action.java b/com/matthewhuntington/homeautomation/Action.java new file mode 100644 index 0000000..df1af0d --- /dev/null +++ b/com/matthewhuntington/homeautomation/Action.java @@ -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; + } +} diff --git a/com/matthewhuntington/homeautomation/ActionExecutor.java b/com/matthewhuntington/homeautomation/ActionExecutor.java new file mode 100644 index 0000000..d7d2270 --- /dev/null +++ b/com/matthewhuntington/homeautomation/ActionExecutor.java @@ -0,0 +1,87 @@ +package com.matthewhuntington.homeautomation; + +import java.util.ArrayList; + +public class ActionExecutor +{ + protected ArrayList 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 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 = new ArrayList(); + + 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 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 getActions() + { + return actions; + } +}