On Sunday June 7 2009 16:18:48 Yogiz wrote:
> Thanks for the impressions. I really like the GPS diary idea. Perhaps
> you should release it to the public? As to alarms, try ffalarms (opkg
> install ffalarms).

I attached my code. As said, it isn't much and not very user-friendly. However, 
I was surprised how few code is needed to get the idea working.

storeLocation.py:
This script is called on the FR to store the current coordinates to 
locations.dat
E.g. storeLocation.py -t "My Home" -d "This is where I live since 5 years"
Please modify "http://myserver.org/"; to your needs.

sendLocations.sh:
All locations stored in locations.dat can be send to the server with the small 
sendLocations.sh script. I used the http-get method since there was no urllib 
module in SHR.

add.py:
On the server side, add.py receives the coordinates as cgi script and stores 
the information to locations.dat. (I should change the name, since the content 
is different to locations.dat on the FR...)

diary.kml:
This is also a cgi script written in Python which creates the kml output from 
the content of locations.dat.

Have fun :-)
Sven

Attachment: diary.kml
Description: application/vnd.google-earth.kml

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import cgi
import cgitb
cgitb.enable()

print("Content-Type: text/html")       # Header-Info  
print("")                                 # Empty line needed

form = cgi.FieldStorage()
if not (form.has_key("latitude") and form.has_key("longitude") and form.has_key("date") and form.has_key("title")):
	print "<H1>Error</H1>"
	print "Please fill in the fields."
else:
	f = open('locations.dat', 'a')
	if (form.has_key("details")):
		f.write(form["date"].value+ "|" + form["latitude"].value+ "|"+form["longitude"].value+"|"+form["title"].value+"|"+ form["details"].value+"\n" )
	else:
		f.write(form["date"].value+ "|" + form["latitude"].value+ "|"+form["longitude"].value+"|"+form["title"].value+"|"+"\n" )
	f.close()
	print "OK"

Attachment: sendLocations.sh
Description: application/shellscript

#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Logging
import logging
import sys

mainlogger = logging.getLogger("")
console = logging.StreamHandler(sys.stdout)
console_formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
console.setFormatter(console_formatter)
mainlogger.addHandler(console)

logger = logging.getLogger("")
logger.setLevel(logging.DEBUG)

import optparse
import datetime
import dbus
#import urllib

class MyOptionParser(optparse.OptionParser):
	def __init__(self):
		optparse.OptionParser.__init__(self,"usage: %prog -t title [-d \"more details\" | ... ]")
		self.add_option("-t",
										"--title",
										action="store",
										type="string",
										dest="title",
										help="Title of this location")

		self.add_option("-d",
										"--details",
										action="store",
										type="string",
										dest="details",
										help="Detailed information of this location")

		self.add_option("--longitude",
										action="store",
										type="string",
										dest="longitude",
										help="Set longitude manually")

		self.add_option("--latitude",
										action="store",
										type="string",
										dest="latitude",
										help="Set latitude manually")

		self.add_option("--date",
										action="store",
										type="string",
										dest="date",
										help="Set date manually")

		self.set_defaults(date=datetime.date.today().isoformat())


def getCoordinates():
	system_bus = dbus.SystemBus()
	gps_object = system_bus.get_object('org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
	gps_interface = dbus.Interface(gps_object, dbus_interface='org.freesmartphone.Resource')
	gps_interface.Enable()

	gps_interface = dbus.Interface(gps_object, dbus_interface='org.freedesktop.Gypsy.Position')
	position=gps_interface.GetPosition()
	print(position)
#	gps_interface = dbus.Interface(gps_object, dbus_interface='org.freesmartphone.Resource')
#	gps_interface.Disable()

	if (position[0]==0):
		logger.warning("No GPS data")
		return None, None
	return str(position[2]), str(position[3])


def saveLocation(latitude, longitude, date, title, details):
	url="http://myserver.org/add.py?";
	url=url+"date="+date
	url=url+"&latitude="+latitude
	url=url+"&longitude="+longitude
	url=url+"&title="+UrlEncode(title)
	if (details!=None):
		url=url+"&details="+UrlEncode(details)
	print(url)
	f=open("locations.dat","a")
	f.write(url+"\n")


# SHR doesn't have the urlib module, therefore I used the code snippet from
# http://blog.affien.com/archives/2005/06/25/python-url-encoding/comment-page-1/

HexCharacters = "0123456789abcdef"
def UrlEncode(s):
	r = ''
	for c in s:
		o = ord(c)
		if (o >= 48 and o <= 57) or \
			(o >= 97 and o <= 122) or \
			(o >= 65 and o <= 90) or \
			o == 36 or o == 45 or o == 95 or \
			o == 46 or o == 43 or o == 33 or \
			o == 42 or o == 39 or o == 40 or \
			o == 41 or o == 44:
			r += c
		else:
			r += '%' + CleanCharHex(c)
	return r

def CleanCharHex(c):
	o = ord(c)
	r = HexCharacters[o / 16]
	r += HexCharacters[o % 16]
	return r



if __name__ == "__main__":
	parser= MyOptionParser()
	options, args = parser.parse_args()
	if (len(args) != 0):
		parser.error("incorrect number of arguments")
		sys.exit(2)
	if (options.title==None):
		parser.error("Missing title")

	if ( (options.longitude==None and options.latitude!=None) or (options.longitude!=None and options.latitude==None)):
		parser.error("Set longitude and latitude or none of them")
	elif (options.longitude==None):
		(options.latitude, options.longitude) =getCoordinates()

	if (options.longitude==None):
		sys.exit(2)

	print("Title: "+str(options.title))
	print("Details: "+str(options.details))
	print("Longitude: "+str(options.longitude))
	print("Latitude: "+str(options.latitude))
	print("Date: "+str(options.date))
	answer=raw_input("Send this information? [Y/n] ")
	if (answer!="n"):
		saveLocation(options.latitude, options.longitude, options.date, options.title, options.details)
_______________________________________________
Openmoko community mailing list
community@lists.openmoko.org
http://lists.openmoko.org/mailman/listinfo/community

Reply via email to