-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosmtrack_fuer_Doku.py
57 lines (52 loc) · 1.67 KB
/
osmtrack_fuer_Doku.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 3 2016
Last Edit on Fri Oct 6 2016
@author: bjoern
"""
import paho.mqtt.publish as mqtt
import os, json, cgi, cgitb
import datetime
from wsgiref.simple_server import make_server
# osmupload ist der Handler der bei eingehenden Calls auf Port 8001 aufegrufen wird
def osmupload(env, start_response):
userid ={'username':"angeben",'password':"angeben"}
topic="owntracks/bjoern/osmand"
hostname="localhost"
port=8883
data={
"_type" : "location",
"alt" : 0,
"lat" : 34.533333,
"lon" : 69.166666,
"tid" : "bp",
"tst" : 0
}
#Zuweisung der aktuellen Daten
htmldata = cgi.FieldStorage(environ=env)
data['lat']=htmldata.getvalue('lat')
data['lon']=htmldata.getvalue('lon')
data['alt']=htmldata.getvalue('alt')
data['tst']=int(long((htmldata.getvalue('tst')))/1000)-int(datetime.datetime.utcfromtimestamp(0).strftime('%s'))
if (data['lat']!=None) and (data['lon']!=None):
#Json als String formatieren
datastr=json.dumps(data)
print datastr
mqtt.single(topic,payload=datastr,auth=userid,port=port,hostname=hostname)
status = '200 OK' # HTTP Status
headers = [('Content-type', 'text/plain; charset=utf-8')] # HTTP Headers
start_response(status, headers)
return []
else:
status = '500' # HTTP Status
headers = [('Content-type', 'text/plain; charset=utf-8')] # HTTP Headers
start_response(status, headers)
return []
#hier wird der Server erzeugt und der Handler angegeben
httpd=make_server('',8001,osmupload)
print("Serving on port 8001...")
#enbles CGI Debugging
cgitb.enable()
# Serve until process is killed
httpd.serve_forever()