forked from arenaxr/arena-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
box-runner-step.py
80 lines (66 loc) · 1.99 KB
/
box-runner-step.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 json
import random
import time
import numpy
import paho.mqtt.client as paho
broker = "oz.andrew.cmu.edu"
object_name = "cube_2"
object_path = "realm/s/render/"
# define callback
def on_message(client, userdata, message):
time.sleep(1)
print("received message =", str(message.payload.decode("utf-8")))
client = paho.Client("client-001")
######Bind function to callback
client.on_message = on_message
#####
print("connecting to broker ", broker)
client.connect(broker)
# connect
client.loop_start() # start loop to process received messages
# client.subscribe("house/bulb1")#subscribe
print("publishing intial box")
MESSAGE = {
"object_id": "cube_2",
"action": "create",
"data": {"object_type": "cube", "color": "#AA0000"},
}
client.publish("realm/s/cube_2", json.dumps(MESSAGE))
while True:
z = 1.0
color = "#%06x" % random.randint(0, 0xFFFFFF)
# cube_str = object_name + ",{},{},{},0,0,0,0,1,1,1,{},on"
# Walk out
for x in numpy.arange(0.0, 20.0, 0.2):
y = 1
z += random.random() - 0.5
# pos_str = "x:{}; y:{}; z:{};"
MESSAGE = {
"object_id": object_name,
"action": "create",
"data": {
"object_type": "cube",
"position": {"x": x, "y": y, "z": z},
"color": color,
},
}
client.publish(object_path + object_name, json.dumps(MESSAGE))
time.sleep(0.1)
# Walk back
for x in numpy.arange(20.0, 0.0, -0.2):
y = 1
z -= random.random() - 0.5
# pos_str = "x:{}; y:{}; z:{};"
MESSAGE = {
"object_id": object_name,
"action": "create",
"data": {
"object_type": "cube",
"position": {"x": x, "y": y, "z": z},
"color": color,
},
}
client.publish(object_path + object_name, json.dumps(MESSAGE))
time.sleep(0.1)
client.disconnect() # disconnect
client.loop_stop() # stop loop