2008-09-16 16:32:59 +00:00
|
|
|
# PyTrackerClient.py
|
2008-09-16 16:14:30 +00:00
|
|
|
'''
|
|
|
|
author: edistar
|
|
|
|
license: GPL v3 or later
|
|
|
|
version 0.1
|
|
|
|
'''
|
|
|
|
from __future__ import with_statement
|
|
|
|
import datetime
|
|
|
|
import ecore
|
|
|
|
import e_dbus
|
|
|
|
import os
|
2008-09-17 19:53:52 +00:00
|
|
|
import sys
|
2008-09-16 16:14:30 +00:00
|
|
|
from socket import *
|
|
|
|
from dbus import SystemBus, Interface
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
|
|
|
class TrackClient:
|
2008-09-17 19:53:52 +00:00
|
|
|
|
2008-09-16 17:46:43 +00:00
|
|
|
def __init__(self, username='anonymous', passwordhash='', host='localhost', port='49152'):
|
2008-09-16 16:32:59 +00:00
|
|
|
self.InitSocket(host, port)
|
2008-09-16 16:14:30 +00:00
|
|
|
self.InitDbusStuff()
|
2008-09-16 16:32:59 +00:00
|
|
|
self.InitUserHash(username, passwordhash)
|
2008-09-16 16:14:30 +00:00
|
|
|
|
|
|
|
def InitSocket(self, host, port):
|
2008-09-17 19:53:52 +00:00
|
|
|
|
2008-09-16 16:14:30 +00:00
|
|
|
# Set the socket parameters
|
|
|
|
# e.g. host = "localhost"
|
|
|
|
# e.g. port = 49152
|
|
|
|
self.__addr = (str(host),int(port))
|
2008-09-17 19:53:52 +00:00
|
|
|
|
2008-09-16 16:14:30 +00:00
|
|
|
# Create socket
|
|
|
|
self.__UDPSock = socket(AF_INET,SOCK_DGRAM)
|
2008-09-17 19:53:52 +00:00
|
|
|
|
2008-09-16 16:14:30 +00:00
|
|
|
# Debug message:
|
|
|
|
print "UDP Socket for %s at port %s created" % (host, port)
|
|
|
|
|
2008-09-17 19:53:52 +00:00
|
|
|
|
2008-09-16 16:14:30 +00:00
|
|
|
def TransmitUDP(self, data):
|
|
|
|
if data:
|
|
|
|
self.__UDPSock.sendto(data,self.__addr)
|
2008-09-17 19:53:52 +00:00
|
|
|
|
2008-09-16 16:14:30 +00:00
|
|
|
# Debug message:
|
|
|
|
print "UDP stream %s sent" % (data)
|
|
|
|
|
|
|
|
def CloseSocket(self):
|
|
|
|
self.__UDPSock.close()
|
2008-09-17 19:53:52 +00:00
|
|
|
|
2008-09-16 16:14:30 +00:00
|
|
|
# Debug message:
|
2008-09-17 19:53:52 +00:00
|
|
|
# print "UDP Socket closed"
|
|
|
|
|
2008-09-16 16:14:30 +00:00
|
|
|
|
|
|
|
def InitDbusStuff(self):
|
2008-09-17 19:53:52 +00:00
|
|
|
|
2008-09-16 16:14:30 +00:00
|
|
|
# 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')
|
2008-09-17 19:53:52 +00:00
|
|
|
|
2008-09-16 16:14:30 +00:00
|
|
|
# request GPS from FSO (which then powers on the GPS chip)
|
|
|
|
self.usage_iface.RequestResource("GPS")
|
2008-09-17 19:53:52 +00:00
|
|
|
|
2008-09-16 16:14:30 +00:00
|
|
|
# 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')
|
2008-09-17 19:53:52 +00:00
|
|
|
|
2008-09-16 16:14:30 +00:00
|
|
|
|
|
|
|
def UpdateData(self, fields, timestamp, lat, lon, alt):
|
|
|
|
# get UTC time from gypsy timestamp
|
|
|
|
string = str(datetime.datetime.utcfromtimestamp(timestamp))
|
2008-09-17 19:53:52 +00:00
|
|
|
|
2008-09-16 16:14:30 +00:00
|
|
|
date, time = string.split()
|
|
|
|
utctime = "%sT%sZ" % (date, time)
|
2008-09-17 19:53:52 +00:00
|
|
|
|
2008-09-16 16:14:30 +00:00
|
|
|
# prepare data for sending
|
2008-09-17 19:53:52 +00:00
|
|
|
UDPData = "%s,%s,%s,%s" % (lat, lon, alt, utctime)
|
|
|
|
self.SendData(self.__username, self.__pwhash, 'TRANSMIT', UDPData)
|
|
|
|
|
2008-09-16 17:46:43 +00:00
|
|
|
# Debug message:
|
2008-09-17 19:53:52 +00:00
|
|
|
# print "Updated Data"
|
2008-09-16 16:14:30 +00:00
|
|
|
|
|
|
|
def InitUserHash(self, username, pwhash):
|
2008-09-17 19:53:52 +00:00
|
|
|
|
2008-09-16 16:14:30 +00:00
|
|
|
# set username and password globally in the class
|
|
|
|
self.__username = username
|
|
|
|
self.__pwhash = pwhash
|
|
|
|
|
|
|
|
def SendData(self, username, pwhash, action, data=""):
|
2008-09-17 19:53:52 +00:00
|
|
|
|
2008-09-16 16:14:30 +00:00
|
|
|
# put together and send data to TransmitUDP()
|
|
|
|
senddata = "%s;%s;%s;%s" % (username, pwhash, action, data)
|
|
|
|
self.TransmitUDP(senddata)
|
2008-09-17 19:53:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def StartTrack(self):
|
|
|
|
self.SendData(self.__username, self.__pwhash, action="START")
|
|
|
|
|
|
|
|
# call self.UpdatePosition() when a dbus signal "PositionChanged" comes along the system bus
|
|
|
|
self.terminator = self.pos_iface.connect_to_signal("PositionChanged", self.UpdateData)
|
|
|
|
|
|
|
|
|
|
|
|
def StopTrack(self):
|
|
|
|
self.SendData(self.__username, self.__pwhash, action="STOP")
|
2008-09-17 20:52:15 +00:00
|
|
|
|
|
|
|
# remove connect_to_signal event
|
2008-09-17 19:53:52 +00:00
|
|
|
self.terminator.remove()
|
|
|
|
self.usage_iface.ReleaseResource("GPS")
|