@ -10,7 +10,7 @@ Next you want a way to tell your sever to activate/deactivate certain appliances
The advantage to this process is that since Google Calendar is the link between you and your server/X10 setup at home, you can essentially write programs that also use the Google Calendar API or just use existing tools to communicate with your home setup. You can use the calendar on your smart phone, or you can use Google's calendar website. You can also share the calendar with anyone else with whom you live so that they can control your devices too.
The advantage to this process is that since Google Calendar is the link between you and your server/X10 setup at home, you can essentially write programs that also use the Google Calendar API or just use existing tools to communicate with your home setup. You can use the calendar on your smart phone, or you can use Google's calendar website. You can also share the calendar with anyone else with whom you live so that they can control your devices too.
@ -50,6 +50,7 @@ This was probably the lengthiest part of the process for me. If you take [this c
### HomeAutomation.java
### HomeAutomation.java
```java
import java.io.FileInputStream;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.Properties;
@ -77,6 +78,7 @@ This was probably the lengthiest part of the process for me. If you take [this c
}
}
}
}
}
}
```
Here's the main file. This starts off by loading the properties file which will have values for which calendar to use and the user/password used to authenticate. The `login.properties` file should look something like this (and should eventually be stored in the same dir as the compiled code):
Here's the main file. This starts off by loading the properties file which will have values for which calendar to use and the user/password used to authenticate. The `login.properties` file should look something like this (and should eventually be stored in the same dir as the compiled code):
@ -90,6 +92,7 @@ Once the properties have been loaded, the `main` function creates a `CalendarPar
### CalendarParser
### CalendarParser
```java
package com.matthewhuntington.homeautomation;
package com.matthewhuntington.homeautomation;
import java.net.URL;
import java.net.URL;
@ -161,13 +164,13 @@ Once the properties have been loaded, the `main` function creates a `CalendarPar
return actions;
return actions;
}
}
}
}
```
The calendar parser is a little more involved. I'm using the [Google Data Java Client Library](http://code.google.com/apis/gdata/articles/java_client_lib.html) to deal with converting Google's XML services to java objects. Basically everything before the large `for` block gets the calendar data from Google. The first `for` block goes through and gets the events for this calendar. An event can have multiple start/top times associated with it (e.g. if it's repeated weekly), so the next `for` block goes through each pair of start/stop times associated with an event to see if that event should be evaluated. It does this by checking if the start time is before the current system time and if the end time is after the current system time. Because the program uses system time, you'll have to make sure that the system time on your server is in sync with Google's time.
The calendar parser is a little more involved. I'm using the [Google Data Java Client Library](http://code.google.com/apis/gdata/articles/java_client_lib.html) to deal with converting Google's XML services to java objects. Basically everything before the large `for` block gets the calendar data from Google. The first `for` block goes through and gets the events for this calendar. An event can have multiple start/top times associated with it (e.g. if it's repeated weekly), so the next `for` block goes through each pair of start/stop times associated with an event to see if that event should be evaluated. It does this by checking if the start time is before the current system time and if the end time is after the current system time. Because the program uses system time, you'll have to make sure that the system time on your server is in sync with Google's time.
@ -212,6 +215,7 @@ The calendar parser is a little more involved. I'm using the [Google Data Java C
return value;
return value;
}
}
}
}
```
The `Action` class is very simple. Its objects act as a form of communication between the `CalendarParser` and the `ActionExecutor`. The `CalendarParser` knows how to create them, and the `ActionExecutor` knows how to interpret them.
The `Action` class is very simple. Its objects act as a form of communication between the `CalendarParser` and the `ActionExecutor`. The `CalendarParser` knows how to create them, and the `ActionExecutor` knows how to interpret them.
@ -219,6 +223,7 @@ The class consists of a few members (which are set in the constructor) and their
### ActionExecutor.java
### ActionExecutor.java
```java
package com.matthewhuntington.homeautomation;
package com.matthewhuntington.homeautomation;
import java.util.ArrayList;
import java.util.ArrayList;
@ -306,6 +311,7 @@ The class consists of a few members (which are set in the constructor) and their
}
}
}
}
}
}
```
This last class is much simpler than it appears. Basically all it does is just format a string and then in `protected void call(String command)` it executes `Runtime.getRuntime().exec(command);` on that string. Almost all of the other functions and members serve only to make the formatting process more clear and extendable. The `public void execute(Action action)` function takes an action and based on its values calls the appropriate function which begins the formatting process. This second function then calls the `protected void call(String command)` function.
This last class is much simpler than it appears. Basically all it does is just format a string and then in `protected void call(String command)` it executes `Runtime.getRuntime().exec(command);` on that string. Almost all of the other functions and members serve only to make the formatting process more clear and extendable. The `public void execute(Action action)` function takes an action and based on its values calls the appropriate function which begins the formatting process. This second function then calls the `protected void call(String command)` function.
@ -326,7 +332,3 @@ Note that I've redirected all the output to a log file that I've created for the
Now you've completed your setup and are ready to test it. Log on to your Google Calendar and create an event in the calendar that you specified in the properties file. Remember that the title of the event must only be of the form House[Unit] (e.g. A2, B3, C, D4, etc). Remember that the time at which this executes will only be as accurate as you set it to be in your cron job. If you have problems, make sure your server's system clock is accurate. If it works, congrats! If you have a smart phone that syncs with your google calendar, try that. Try sharing your calendar with a housemate or maybe using the google API to write more programs for whatever device you want. One thing to note is that X10 sells a wide variety of products including HVAC automation. I haven't tried it yet, but I get the feeling that you can use this same process to control your heating, air-conditioning, etc.
Now you've completed your setup and are ready to test it. Log on to your Google Calendar and create an event in the calendar that you specified in the properties file. Remember that the title of the event must only be of the form House[Unit] (e.g. A2, B3, C, D4, etc). Remember that the time at which this executes will only be as accurate as you set it to be in your cron job. If you have problems, make sure your server's system clock is accurate. If it works, congrats! If you have a smart phone that syncs with your google calendar, try that. Try sharing your calendar with a housemate or maybe using the google API to write more programs for whatever device you want. One thing to note is that X10 sells a wide variety of products including HVAC automation. I haven't tried it yet, but I get the feeling that you can use this same process to control your heating, air-conditioning, etc.