-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensors.py
80 lines (72 loc) · 2.15 KB
/
sensors.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
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import json
import requests
import sys
import random
global eprijs
global wprijs
def on_connect(client, userdata, flags, rc):
print("Connected with result code " +str(rc))
def on_message(client, userdata, msg):
# print(msg.topic + " " + str(msg.payload))
try:
data = json.loads(msg.payload.decode("utf-8"))
print(data)
except:
print("An error has occured!")
def get_prices():
global eprijs
global wprijs
response = requests.get("https://af-voorbeeldexamendaan.azurewebsites.net/api/v1/prices")
print(response.json())
for item in response.json():
if item["type"] == "water":
wprijs = item["itemPrice"]
else:
eprijs = item["itemPrice"]
def menu(keuze):
global eprijs
global wprijs
aantal = 0
if keuze == 9:
sys.exit()
elif keuze == 1:
aantal = random.randrange(100)
prijs = aantal * wprijs
payload = {
"Sensor": "water",
"Amount": aantal,
"Price": round(prijs, 2),
"EMail": email
}
publish.single("/daandewilde", json.dumps(payload).encode("utf-8") , hostname="13.81.105.139")
elif keuze == 2:
aantal = random.randrange(100)
prijs = aantal * eprijs
payload = {
"Sensor": "electricity",
"Amount": aantal,
"Price": round(prijs, 2),
"EMail": email
}
publish.single("/daandewilde", json.dumps(payload).encode("utf-8") , hostname="13.81.105.139")
def run():
keuze = 0
while keuze != 9:
print("Maak uw keuze:")
print("1. Test Water Sensor")
print("2. Test Electriciteits Sensor")
print("9. Exit")
keuze = int(input())
menu(keuze)
if __name__ == '__main__':
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("13.81.105.139", 1883, 60)
client.subscribe("/daandewilde", qos=0)
client.loop_start()
email = input("Geef je e-mail in om verder te gaan:\n")
get_prices()
run()