-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeo.py
91 lines (67 loc) · 2 KB
/
geo.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from geojson import Feature, Point, Polygon
import json
import sys
import paho.mqtt.client as mqtt
import hmac
hostname = "iot.eclipse.org" # Sandbox broker
port = 1883 # Default port for unencrypted MQTT
topic="TOPIC1"
topic2="TOPIC2"
def on_connect(client, userdata, rc):
# Successful connection is '0'
print("Connection result: " + str(rc))
if rc == 0:
# Subscribe to topics
client.subscribe(topic)
client.subscribe(topic2)
def on_message(client, userdata, message):
#print("Received message on %s: %s (QoS = %s)" %
#(message.topic, message.payload.decode("utf-8"), str(message.qos)))
recv=message.payload.decode("utf-8")
hash=recv.split('_')[0]
to_hash=recv.split('_')[1]
digest_maker = hmac.new('PASSWORD')
digest_maker.update(to_hash)
digest = digest_maker.hexdigest()
if(digest==hash):
print "ok"
packet=json.loads(to_hash)
print(hash)
print(packet)
prop=packet["properties"]
if(message.topic=="TOPIC1"):
coord=tuple(packet["coordinates"])
my_feature = Feature(geometry=Point(coord),properties=prop)
fle='protei.geojson'
elif(message.topic=="TOPIC2"):
cord_list=[packet["coordinates"]]+[[[0,0]]]
coord=tuple(cord_list)
my_feature = Feature(geometry=Polygon(coord),properties=prop)
fle='neighborhoods.geojson'
with open(fle) as f:
data = json.load(f)
data['features'].append(my_feature)
with open(fle, 'w') as f:
json.dump(data, f)
else:
print "Data integrity check failed"
print "Hash value: %s"%(hash)
print "Computer Value: %s"%(digest)
def on_disconnect(client, userdata, rc):
if rc != 0:
print("Disconnected unexpectedly")
# Initialize client instance
client = mqtt.Client()
# Bind events to functions
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
# Connect to the specified broker
client.connect(hostname, port=port)
# Network loop runs in the background to listen to the events
client.loop_forever()
'''22.28397,114.11993
22.28814,114.12705
22.28876,114.09847
22.282231,114.129151
'''