From 604d68c975b1ea275a0332dabaa9ebd2f236cb30 Mon Sep 17 00:00:00 2001 From: edistar Date: Tue, 16 Sep 2008 14:41:51 +0000 Subject: [PATCH] This is the first pre-alpha code of the PyTracker project The PyTracker project seeks to create a simple tracking app, not with all the bloat TangoGPS creates (which is not needed) git-svn-id: http://www.neo1973-germany.de/svn@150 46df4e5c-bc4e-4628-a0fc-830ba316316d --- PyTracker/PyTracker.py | 81 ++++++++++++++++++++++++++++++++++++++++++ PyTracker/client.py | 27 ++++++++++++++ PyTracker/server.py | 79 ++++++++++++++++++++++++++++++++++++++++ PyTracker/tracking.txt | 16 +++++++++ 4 files changed, 203 insertions(+) create mode 100755 PyTracker/PyTracker.py create mode 100644 PyTracker/client.py create mode 100644 PyTracker/server.py create mode 100644 PyTracker/tracking.txt diff --git a/PyTracker/PyTracker.py b/PyTracker/PyTracker.py new file mode 100755 index 0000000..568353b --- /dev/null +++ b/PyTracker/PyTracker.py @@ -0,0 +1,81 @@ +#!/usr/bin/python +''' +authors: edistar +license: gpl v2 or later +version 0.2 +''' + +from __future__ import with_statement +import time +import datetime +import ecore +import e_dbus +import os +from dbus import SystemBus, Interface +from optparse import OptionParser + +class Main: + + def __init__(self): + + self.GetPath() + +# get FSO Usage proxy/iface up to request GPS + self.systembus=systembus = SystemBus(mainloop=e_dbus.DBusEcoreMainLoop()) + self.usage_proxy = self.systembus.get_object('org.freesmartphone.ousaged', '/org/freesmartphone/Usage') + self.usage_iface = Interface(self.usage_proxy, 'org.freesmartphone.Usage') + +# Request GPS from FSO (which then powers on the GPS chip) + self.usage_iface.RequestResource("GPS") + +#get gypsy proxy/iface up + self.ogpsd_proxy = self.systembus.get_object('org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy') + self.course_iface = Interface(self.ogpsd_proxy, 'org.freedesktop.Gypsy.Course') + self.pos_iface = Interface(self.ogpsd_proxy, 'org.freedesktop.Gypsy.Position') + +# call self.UpdatePosition() when a dbus signal "PositionChanged" comes along the system bus + self.pos_iface.connect_to_signal("PositionChanged", self.UpdatePosition) + + + def UpdatePosition(self, fields, timestamp, lat, lon, alt): + +# get UTC time from gypsy timestamp + string = str(datetime.datetime.utcfromtimestamp(timestamp)) + date, time = string.split() + utctime = "%sT%sZ" % (date, time) + +# write data to file + s ="%s,%s,%s,%s\n" % (lat, lon, alt, utctime) + with open(self.trackfile,'a') as file: + file.write(s) + +#debugging output: + print s + + + def Parser(self): + +# parse command line options + self.parser=OptionParser("usage foo bar") + self.parser.add_option("-f", "--file", "-o", "-l", action = "store", dest = "trackfile") + (self.options, self.args) = self.parser.parse_args() + + + def GetPath(self): + trackpath = '.' + suffix = 'track' + self.Parser() + +# raises an Exception if the path doesn't exist! :) + path, rubbish, suffix = self.trackfile.options.rpartition("/") + + if not os.path.exists(path): + raise Exception("path does not exist!") + +# sets trackfile path and filename + self.trackfile = "%s/%s-%s" % (trackpath, time.strftime(), suffix) + + + +test=Main() +ecore.main_loop_begin() diff --git a/PyTracker/client.py b/PyTracker/client.py new file mode 100644 index 0000000..3494edc --- /dev/null +++ b/PyTracker/client.py @@ -0,0 +1,27 @@ +# Client program + +from socket import * + +# Set the socket parameters +host = "localhost" +port = 49152 +buf = 1024 +addr = (host,port) + +# Create socket +UDPSock = socket(AF_INET,SOCK_DGRAM) + +def_msg = "===Enter message to send to server==="; +print "\n",def_msg + +# Send messages +while (1): + data = raw_input(">> ") + if not data: + break + else: + UDPSock.sendto(data,addr) + print "Sending message", data + +# Close socket +UDPSock.close() diff --git a/PyTracker/server.py b/PyTracker/server.py new file mode 100644 index 0000000..038d29a --- /dev/null +++ b/PyTracker/server.py @@ -0,0 +1,79 @@ +from __future__ import with_statement +import time +from socket import * + +class TrackServer: + def __init__(self): + self.InitHashdb("hashfile.txt") + self.InitTrackDict() + + def InitHashdb(self, hashfile): + self.hashdb=[] + with open(hashfile, "r") as file: + for line in file: + if line: + self.hashdb.append((line.split()[0], \ + line.split()[1])) + + def InitTrackDict(self): + self.TrackDict={} + for data in self.hashdb: + self.TrackDict[data[0]] = "" + + def VerifyUser(self, username, password_hash): + for data in self.hashdb: + if data[0] == username and data[1] == password_hash: + return 1 + return 0 + + + def Parser(self, stuff): +# Parses the complete data sent to UDP port + try: + username, password_hash, action, data = stuff.split() +# Verifies the user and password + if self.VerifyUser(username, password_hash): + + if action == "START": + self.NewTrack(username) + if action == "STOP": + self.CloseTrack(username) + if action == "TRANSMIT": + self.AddToTrack(username, data) + print "Action", action,"received" + except: + print "Something went wrong.." + + def NewTrack(self, username): + self.TrackDict[username] = "/home/edistar/Openmoko/projects/tracking/data/" + username + time.strftime("%Y%m%d%H%M%S") + print "Created track", self.TrackDict[username] + + def CloseTrack(self, username): + self.TrackDict[username] = "" + print "Closed track", self.TrackDict[username] + + def AddToTrack(self, username, data): + with open(self.TrackDict[username], "a") as trackfile: + trackfile.write(data + "\n") + print "Successfully added data to track", self.TrackDict[username] + +instance=TrackServer() + +# Set the socket parameters +host = "" +port = 49152 +buf = 1024 +addr = (host,port) + +# Create socket and bind to address +UDPSock = socket(AF_INET,SOCK_DGRAM) +UDPSock.bind(addr) + +# Create instance of TrackServer +instance = TrackServer() + +#Receive messages +while 1: + data,addr = UDPSock.recvfrom(buf) + print "Following data received:", data + instance.Parser(data) diff --git a/PyTracker/tracking.txt b/PyTracker/tracking.txt new file mode 100644 index 0000000..9cfa575 --- /dev/null +++ b/PyTracker/tracking.txt @@ -0,0 +1,16 @@ +ONLY WAY OF DATA TRANSFER: +CLIENT --> SERVER + +PASSWORD_HASH is in md5 +Structure: + +(USERNAME,PASSWORD_HASH,ACTION,DATA) + +ACTION == ("START" | "TRANSMIT" | "STOP") + +DATA == ("" | POSITION, TIME/DATE | "") + +POSITION == (LAT, LON, HEIGHT) + +Only one track may be open for each user.. so if the user requests START and +another track is open it gets closed immediately