Wednesday, October 5, 2011

New Home? time to play with some automation!

So Yeah, i know i don't write half as often as i'd like but hey! probably no one is reading my blog so...meh!

[caption id="" align="alignleft" width="181" caption="Tasker Task"][/caption]

I wanted to write today about a little automation i made, which i'm rather proud of. 2 months ago, i moved in with my girlfriend and now that i have a pretty powerful laptop that i use only rarely, i decided to have some fun with it, so i set it up as a little file/sharing/downloading/multi-purpose server. I no longer have a download restriction, so i download a lot of stuff on torrent. The only problem is that i don't want these torrent to be using all my bandwidth while i'm at home (the place where i live has a really shitty internet, even slower than 1 Mbps :( ). I use a lot of automation on my phone with Tasker, and i actually really like it, i set-up all kind of stuff like
automatically muting my phone while i'm in class (it checks on my calendar to know if i'm in a class!). Well anyway i thought to use the same kind of automation software for my computer and integrate the two!(yeah i know, i'm a programmer, i could just program the damn thing, but i like to hack around)

So i started by installing Eventghost, a software that can monitor all kind of events on your computer, from mouse move to software opening and... HTTP Requests!! :D. I needed my phone to communicate with my computer, so i thought i would probably need some network component and, HTTP is pretty simple so i settled with that. At first, since i don't have a static ip address at home, i wanted Eventghost to send to my phone the renewed ip address, it however turned out impossible since Evenghost doesn't permit to receive information from the GET requests, it justs create an event. Also, it would have required to have a permanent webserver on my phone (or something like that) and that would've probably drained a lot of battery. Anyway i started by creating the action to do when the HTTP request arrived, Eventghost would just open uTorrent, select all the torrent (really just typing CTRL + A) and then start or stop all the torrent. quite simple really!(i later found a more robust way to do it, more on that later).

So, now that everything was set-up to start and stop the torrents when some event was triggered, i went on my phone and created the trigger with Tasker. My tasker already has profile to know when i'm at home or not (i use the wi-fi SSID to know) so all i had to do was trigger an HTTP get request when i got home that would stop the torrent, and one that would start it when i left, this part was really easy. I also added the same kind of rule for when i went to sleep, my torrents would start when i went to sleep(or when i plugged my phone in) and stop when i woke up (when i unplugged my phone). So, there i had it, automatically starting and stopping torrents :D.

Now, fast-forward 2 weeks after-that, my setup doesn't work because my dynamic ip changed, and in the meantime i have had the change to discover another (REALLLY NIIIICCEE) automation software (which was developed at MIT) named Sikuli, so i decided to update the way i did things a little. I started with the part where Eventghost used to Start/Stop the torrents, i wrote a really small script to do it with Sikuli:

[caption id="" align="alignleft" width="371" caption="Sikuli Script"]Sikuli Script[/caption]

So this script does the exact same thing my event ghost actions did but it's a lot simpler and it uses image recognition to navigate (instead of me specifying the coordinates) so it's a lot more stable!I also had to change the way Eventghost  handled the event so that it would instead call the script generated by Sikuli.The other modification i did was to use dyndns to map a dynamic ip address to a static domain name.

[caption id="" align="alignright" width="225" caption="Evenghost final event"]Evenghost final event[/caption]

No comments:

Post a Comment