forked from jolth/Report-GPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreportgpsservi.py
66 lines (50 loc) · 1.77 KB
/
reportgpsservi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Jorge A. Toro [[email protected]]
#
import sys, os
import socket
import threading
import time
#FILE='/tmp/gps.log'
FILE='gps.log'
def createFile(File):
""" Create file of Log """
with open(File, 'w') as f:
if f.tell() == 0:
print >> f, 'ID'.center(8), 'IP,Port'.center(24), 'Date'.center(12), 'Time'.center(10), 'Event'.center(7), 'Latitude'.center(10), 'Length'.center(12), 'Geocoding'.center(36)
print >> f, ('-'*6).ljust(8), ('-'*22).ljust(24), ('-'*10).ljust(12), ('-'*8).ljust(10), ('-'*6).ljust(6), ('-'*10).ljust(10), ('-'*10).ljust(12), ('-'*34).ljust(36)
return True
class Device(threading.Thread):
""" """
endfile = 0
def __init__(self, data, address, lock):
threading.Thread.__init__(self)
self.data, self.address = data, address
self.lock = lock
def run(self):
self.lock.acquire(True)
with open(FILE, 'a+') as f:
f.seek(self.__class__.endfile)
#print >> f, f.tell()
#print >> f, time.asctime() + ': ' + repr(self.address)
print >> f, ('None').ljust(8), (repr(self.address)).ljust(26), (time.strftime('%D')).ljust(12), (time.strftime("%H:%M:%S")).ljust(10), \
('None').ljust(6), ('None').ljust(10), ('None').ljust(12), ('None').ljust(36)
#print >> f, self.data
self.__class__.endfile = f.tell()
#f.close()
self.lock.release()
if __name__ == "__main__":
#if os.path.exists(FILE) or createFile(FILE):
if createFile(FILE):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((socket.gethostbyname(socket.gethostname()), 59000))
lock = threading.Lock()
while 1:
try:
data, address = sock.recvfrom(4096)
d = Device(data, address, lock)
d.start()
except KeyboardInterrupt:
sys.stderr.write("Exit, KeyboardInterrupt\n")
break