This repository has been archived by the owner on Jan 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcdb.py
59 lines (44 loc) · 1.8 KB
/
cdb.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
import json, httplib
from datetime import timedelta, datetime
from suds import WebFault
from suds.client import Client
class cdb(object):
def __init__(self, url, jsonpath, vehicleurl, vehicleapp, vehiclepassword, vehiclerefresh):
#post json position setup
self.url = url
self.jsonpath = jsonpath
self.encoder = json.JSONEncoder(indent = 4)
self.connection = httplib.HTTPConnection(self.url)
#vehicle lookup setup
self.refresh = vehiclerefresh
self.client = Client(vehicleurl)
authHeader = self.client.factory.create('AuthHeader')
authHeader.Editie = -1
authHeader.Applicatie = vehicleapp
authHeader.Wachtwoord = vehiclepassword
self.client.set_options(soapheaders=authHeader)
self.get_vehicles()
def post_position(self, dict):
timestamp = datetime.utcnow().isoformat() + 'Z'
output = {}
output['id'] = dict['imei']
output['timestamp'] = timestamp
output['latitude'] = "%f" % dict['latitude']
output['longitude'] = "%f" % dict['longitude']
output['speed'] = "%f" % dict['speed_kmh']
output['direction'] = dict['heading']
json_out = self.encoder.encode(output)
headers = {"Content-type": "application/json", "Accept": "application/json; charset=utf8"}
self.connection.request("POST", self.jsonpath, json_out, headers)
return self.connection.getresponse()
def get_vehicles(self):
vehicle_count = self.client.service.getVoertuigCount()
self.vehicles = self.client.service.getVoertuigList(0, vehicle_count)[0]
self.last_vehicle_request = datetime.now()
def get_name_from_imei(self, imei):
if (datetime.now() - self.last_vehicle_request) > timedelta(seconds=self.refresh):
self.get_vehicles()
for vehicle in self.vehicles:
if 'vt_gps_imei' in vehicle and vehicle['vt_gps_imei'] == imei:
return vehicle['vt_naam']
return "UNKNOWN GPS"