tralfamadore.com
get unstuck, in time

Tuesday, August 26, 2008

Changelog for Locatable 0.3


I'm just about to submit the new version of Locatable. Here are the changes:

Locatable
  • More accurate GPS readings. When retrieving a location, wait until the accuracy is within the specified range, or 20 seconds, whichever comes first (i.e. if you set it to "Best", it'll spend the full 20 seconds).
  • New option "Expire after" specifies how stale of a location is allowed to be sent to web sites. Default is 10 minutes. If you set this to "each request", Locatable will pop up every time a web site wants to read your location.
  • New option "Ask permission" defines if and how many times you'll be prompted to allow a site to see your location. Default is to always ask. Set this to "Twice per site" to mimic the behaviour of iPhone native apps.
Relocatable
  • Relocatable is now a daemon process. You can control how often and how fast it runs by editing /System/Library/LaunchDaemons/com.tralfamadore.locatable.plist.
  • You can also run it from the command line (it now lives in /usr/libexec/relocatable), which gives you the following options:

~ root# /usr/libexec/relocatable/Relocatable -h
Usage: Relocatable [-v] [-t SECONDS] [-d SECONDS] [-e CMD]
-d SECONDS run as a daemon, delay specified seconds between fixes
-e CMD execute given program (with args) after each location fix,
can include @lat@, @long@, and @hacc@ tokens
-t SECONDS spend specified seconds waiting for a fix, default 30
-v turn on verbose logging
Let me know if you encounter any issues.

Update (30 Aug 08): A few people had questions about getting the command execution piece to work. Here's an example — the important bit is to wrap the whole command in single quotes, otherwise the shell gets confused.

First, if Relocatable is already running as a daemon (it will be by default), you need to stop it:

~ root# launchctl unload /System/Library/LaunchDaemons/com.tralfamadore.locatable.plist


Once that's done, you can run Relocatable as a one-off from the command line:

~ root# /usr/libexec/relocatable/Relocatable -v \
> -e 'curl "http://my.site.com/savepos.pl?lat=@lat@&long=@long@&acc=@hacc@"'
Opened LBS database for read...
Started updates...
newLocation: <+51.xxxxxxxxx, -0.xxxxxxxx> +/- 93.21m @ 2008-08-30 13:17:44 +0100
newLocation: <+51.xxxxxxxxx, -0.xxxxxxxx> +/- 93.21m @ 2008-08-30 13:18:29 +0100
Stopped updates
Opened LBS database for write...
* About to connect() to my.site.com port 80 (#0)
* Trying xxx.xxx.xxx.xxx... connected
* Connected to my.site.com (xxx.xxx.xxx.xxx) port 80 (#0)
> GET /savepos.pl?lat=51.xxxxxxxxxx&lng=-0.xxxxxxxxxxxx&acc=93.21 HTTP/1.1
> User-Agent: curl/7.17.1 (arm-apple-darwin9) libcurl/7.17.1 OpenSSL/0.9.8g zlib/1.2.3


Note the use of the single quotes (to bracket the command passed to Relocatable) and double quotes (to bracket the URL, so it can contain characters that would otherwise confuse the shell, like the ampersand).

The same rules regarding quoting apply to editing the daemon plist. Once you're happy with your settings and have edited the plist to your satisfaction, remember to start it back up:

~ root# launchctl load /System/Library/LaunchDaemons/com.tralfamadore.locatable.plist

Labels: , , , ,

19 Comments:

Blogger Evan said...

Hi

I am unable to get the -e option work with Relocatable. Are you able to provide a LaunchDaemon usage example including arguments?

August 29, 2008 9:02 PM  
Blogger Wesley said...

I added an example to the post. It's all about quoting your arguments.

August 30, 2008 5:44 AM  
Anonymous Anonymous said...

I'm trying to use the relocatable launchdaemon to execute a curl command. At the moment I have:
[string]-e[/string]
[string]'curl "http://example.com/add.php?lat=@lat@&long=@long@"'[/string]

nb: I am using square brackets because blogger doesn't permit angle brackets. I have angle brackets in the plist.

I can see via top that Relocatable is running and has a small CPU time measurement, which is increasing each delay period. If I unload the plist, this instance is removed from top. Additionally it terminates locationd. From my weblogs, there's no attempt to load add.php off my site when operating the plist version. I have set permissions on the plist to 744. Any other permissions aborts its loading because of dubious permissions.

Executing /usr/libexec/relocatable/Relocatable -d 30 -t 10 -e 'curl "http://example.com/add.php?lat=@lat@&long=@long@"' -v works flawlessly. If the launchd version has been previously loaded, no prompt is given for access to location information.

So, at the moment: plist doesn't invoke Relocatable the way I thought it would, but running it in a terminal works great.

Any thoughts on how to get the auto one to curl properly?

August 31, 2008 6:23 AM  
Anonymous Anonymous said...

I am the anonymous commentor above. I removed the single quotation marks around the curl "URL" command and it's now functional. The single quotation is required when executing it once-off, but not inside the plist.

August 31, 2008 7:39 AM  
Blogger Martinez said...

I succesfull used your instructions to post a google maps links on ur own twitter site.

I created a file name /bin/tweet with the following contents

#! /bin/sh
curl --basic --user user:password --data status="`/usr/libexec/relocatableRelocatable -t 15 -e 'date && echo mijn locatie http://maps.google.com/maps?q=@lat@@long@'`" http://twitter.com/statuses/update.xml

(only 2 lines). Please change user and password with ur twitter user/passwd

Be sure the script (named Tweet) runs as a daemon.

Martijn

September 1, 2008 3:13 PM  
Blogger mayson14 said...

ive edited com.tralfamadore.locatable.plist
to use the -e argument to pass lat and long to an aspx application ive created, but the web app is never accessed from the iphone... here is the .plist:

[?xml version="1.0" encoding="UTF-8"?]
[!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"]
[plist version="1.0"]
[dict]
[key]KeepAlive[/key]
[true/]
[key]Label[/key]
[string]com.tralfamadore.locatable[/string]
[key]Nice[/key]
[integer]20[/integer]
[key]ProgramArguments[/key]
[array]
[string]/usr/libexec/relocatable/Relocatable[/string]
[string]-d[/string]
[string]30[/string]
[string]-t[/string]
[string]30[/string]
[string]-e[/string]
[string] curl "http://www.myserver.com/storegps.aspx?lat=@lat@&long=@long@"[/string]
[/array]
[key]RunAtLoad[/key]
[true/]
[/dict]
[/plist]

replaced angled brackets for post.. any help would be much appreciated.

September 9, 2008 2:44 AM  
Blogger mayson14 said...

Re: Post Above- I woke up with a server full of entries.. Guess it works!

September 9, 2008 10:10 AM  
Anonymous Anonymous said...

Hi everybody!

I had the same problem with the ' ... but also with the & !
The launchctl doesn't load a file which contains & in it!
Do you have the same problem?

So now my curl seems to be called every XX minutes. However sometimes the curl process seems to be stucked! When I do a ps I see it but it never ends.

Does anybody have the same problem?
Franck

September 28, 2008 3:58 AM  
Blogger Wesley said...

@Anonymous above -- the plist is an XML file so presumably you need to escape the & as &amp;.

September 28, 2008 6:17 AM  
Anonymous iTs mE said...

Hey Wesley.

I would like to use Relocatable and Twitter the way that "martinez" mentioned.

But Relocatable does not work for me. When trying to stop the Daemon via Terminal i get the message "Error unloading com.tralfamadore.locatable"

By trying to lauch Relocatable i get: "Socket is not connected".

Is it possible that you msg me via ICQ on this? My Number is 244382874. I think there is a litte mistake i can`t find on my own. Thx a lot in advance.

excuse my bad english...i am german.

September 30, 2008 10:55 AM  
Anonymous selfix said...

Cheers Wesley!

Thanks for sharing Relocatable with the world. It was the best hello-world like application for me. I guess one of those things that actually pulled me into iPhone development.

I had one quirk though: when trying binary version of Relocatable from .deb: I'd get an error:
246110287.734,CLClientHandleDaemonDataRegistration: server did not accept client registration 2
Got error #1, no location read

But when I compiled it from sources it worked flawlessly.

I'll probably modify it to fit my needs: run as a deamon and log bicycle tracks to a database.

Thanks again!

October 19, 2008 5:07 AM  
Blogger Wesley said...

@selfix: when you say using it from the .deb, do you mean the version installed with Locatable via the Bigboss repository? I haven't seen any problems so just curious what might be the issue.

October 19, 2008 5:27 AM  
Anonymous selfix said...

The one that came from bigboss yes. I did not try running it as a daemon, but what was strange that when run from command line it didn't prompt me to allow access to gps (and probably did not get it later). The version I compiled (unchanged) worked as expected. Even the file size is the same, but the binary is different. I have firmware 2.0.1 on 3G.

October 19, 2008 7:09 AM  
Blogger Martin Cleaver said...

A different approach than sending updates over the air using CURL is to extract information out of the SqLite database after it has synced.

What a cool little app. Thanks Wesley!

October 21, 2008 4:17 PM  
Blogger Martin Cleaver said...

Further to my comment about syncing later... it looks like it keeps clearing out the database?

I've had the app on all day, but the database looks like it is empty?

localhost:/ mobile$ sqlite3 /private/var/mobile/Library/WebKit/Databases/http_lbs.tralfamadore.com_0/0000000000000001.db

sqlite> .dump
BEGIN TRANSACTION;
CREATE TABLE __WebKitDatabaseInfoTable__ (key TEXT NOT NULL ON CONFLICT FAIL UNIQUE ON CONFLICT REPLACE,value TEXT NOT NULL ON CONFLICT FAIL);
INSERT INTO "__WebKitDatabaseInfoTable__" VALUES('WebKitDatabaseVersionKey','1.0');
CREATE TABLE location (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, tag TEXT NOT NULL, latitude DECIMAL(30,27) NOT NULL, longitude DECIMAL(30,27) NOT NULL, last_update TIMESTAMP);
DELETE FROM sqlite_sequence;
INSERT INTO "sqlite_sequence" VALUES('preferences',1);
CREATE TABLE preferences (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, domain TEXT, accuracy INTEGER NOT NULL, interval INTEGER NOT NULL, enabled INTEGER NOT NULL DEFAULT 1, ask INTEGER NOT NULL DEFAULT 1);
INSERT INTO "preferences" VALUES(1,NULL,1000,600,1,1);
COMMIT;

I'd have expected more INSERT statements.

Thanks again,
Martin.

October 22, 2008 6:56 PM  
Blogger flashman said...

I've installed Locatable but Relocatable won't run from command line. I've navigated to /usr/libexec/relocatable and I see Relocatable there, but get this error:

-sh: Relocatable: command not found

Nobody else has commented on this so I assume I'm doing something wrong. iPhone has been restarted and I've tried this both with com.tralfamadore.locatable.plist loaded and unloaded.

November 10, 2008 3:18 AM  
Blogger Wesley said...

@flashman -- I'm guessing you need to do "./Relocatable" as the libexec directory is not in the shell's executable PATH.

November 10, 2008 3:40 AM  
Anonymous PacmanII said...

I want to use relocatable to locate my iPhone in the case of lost or if it's been stolen. So I implemented the procedure described above and my iPhone posts it's current location via curl to a script on my webserver every 10 min.

The problem is, that this only works when the iPhone is in use. If I turn it off (not really "off", just standby mode) the position isn't updated anymore until I put it on again. So I have lacks in the tracking log on my webserver of about 2-3 hours if I haven't used it for this time. Does anyone else has that problem? And how to solve it?

November 10, 2008 6:28 AM  
Anonymous PacmanII said...

Just as an addional information: With Insomnia switched "on" relocatable works fine. Seems that the power management of the iPhone turns off the GPS receiver when in standby. Insomnia prohibits this but makes the battery going empty within hours. Maybe this can be changed by implementing a wakeup in locatable. For more information visit the Insomnia website: http://code.google.com/p/iphone-insomnia/

"However if you would like your application to do something periodically a much better approach would be to schedule a wake up, and allow it to sleep in between times, to conserve battery power like so:

NSCalendarDate *nextWake = [[NSCalendarDate calendarDate] dateByAddingYears:0 months:0 days:0 hours:0 minutes:15 seconds:0];
CPSchedulePowerUpAtDate((CFDateRef)nextWake);

Then you need to cancel the suspend power notifications while you are doing your work, so the phone stays on while it is working. If you don't do this then it turns off again 15 seconds later interfering with what you need done. This last part is in the Insomnia source code on this site."

November 11, 2008 11:52 PM  

Post a Comment

<< Home

Copyright (C) 2001-2008 Tralfamadore.com