-
Notifications
You must be signed in to change notification settings - Fork 0
/
ditto_grove_demo.py
97 lines (73 loc) · 2.83 KB
/
ditto_grove_demo.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
92
93
94
95
96
97
#!/usr/bin/python3
# Copyright (c) 2017 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0
#
# SPDX-License-Identifier: EPL-2.0
import json
import math
import time
import websocket
import raspberry_thing
DITTO_IP = "localhost"
DITTO_PORT = "8080"
websocketOpen = False
thing = raspberry_thing.RaspberryThing()
def on_new_trust_agent_value(trust_agent):
if websocketOpen and math.isnan(trust_agent) == False: #check if it is the same trust agent version, and what the current status is
send_modify_message(thing.create_trust_agent_change_message(trust_agent))
def on_new_illumination_value(illumination):
if websocketOpen and math.isnan(illumination) == False:
pass
# send_modify_message(thing.create_illumination_change_message(illumination))
def on_new_temperature_value(temperature, humidity):
if websocketOpen and math.isnan(temperature) == False and math.isnan(humidity) == False:
pass
# send_modify_message(thing.create_temperature_change_message(temperature, humidity))
def on_message(ws, message):
json_message = json.loads(message)
# thing.handle_websocket_message(json_message)
def send_modify_message(message):
# convert to JSON
json_message = json.dumps(message)
# send via websocket
ws.send(json_message)
def on_error(ws, error):
print('An unexpected error happened while using the Websocket connection: {}'.format(error))
def on_close(ws):
print('Websocket closed - trying to reconnect any second.')
global websocketOpen
websocketOpen = False
time.sleep(5)
start_websocket()
def on_open(ws):
print("### Websocket opened ###")
global websocketOpen
websocketOpen = True
# start listening for events and messages
ws.send("{}")
# ws.send("START-SEND-MESSAGES")
# ws.send("START-SEND-EVENTS")
def start_websocket():
print('Establishing websocket connection ...')
ws_address = "ws://" + DITTO_IP + ":" + DITTO_PORT + "/ws/2"
basic_auth = 'Authorization: Basic {}'.format(raspberry_thing.get_b64_auth())
global ws
ws = websocket.WebSocketApp(ws_address,
header=[basic_auth],
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.on_open = on_open
ws.run_forever()
if __name__ == "__main__":
# init our raspberry thing
# thing.start_polling_illumination(on_new_illumination_value)
# thing.start_polling_temperatures(on_new_temperature_value)
# start websocket
start_websocket()