-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
106 lines (83 loc) · 3.98 KB
/
main.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
98
99
100
101
102
103
104
105
106
# This software can be copied, modified and distributed freely.
# The origin must be mentioned in all copied, modified or distributed pieces:
# <link>
# 2022, GyroCube
import time
import random
from communication.messages import CubeJoinedSignal, CubeDisconnectedSignal, CubeFlippedSignal, CubeConfigIndication
from communication.mqtt_handler import MqttHandler
from testing.utils import Network
HOST_ADDRESS = "18.198.188.151"
PORT = 21883
def test_single_message():
""" Template for sending single messages """
# See message constructors in communication.messages
message = CubeFlippedSignal(cube_id=0xAA, old=1, new=5)
MqttHandler.tx_single(HOST_ADDRESS, message)
def test_cube_flipping_scenarion(number_of_cubes: int,
min_gap_between_transmissions: int,
max_gap_between_transmissions: int):
"""
Cube flipping scenaraio starts with filled network of specified size.
Cubes are periodically flipped.
:param number_of_cubes: Number of cubes in the network
:param min_gap_between_transmissions: Minimal time-gap between transmissions
:param max_gap_between_transmissions: Maximum time-gap between transmissions
"""
network = Network(size=number_of_cubes)
mqtt = MqttHandler(HOST_ADDRESS, PORT)
for _ in range(number_of_cubes):
network.join()
while True:
print("EVENT: CUBE FLIPPED")
cube = random.choice(network.network)
old_side = cube.side
cube.flip()
new_side = cube.side
mqtt.publish(CubeFlippedSignal(cube.id, old_side, new_side))
mqtt.publish(CubeConfigIndication(cube.id, cube.side, cube.config))
time.sleep(random.randint(min_gap_between_transmissions, max_gap_between_transmissions))
def test_active_scenario(number_of_cubes: int,
min_gap_between_transmissions: int,
max_gap_between_transmissions: int):
"""
Active scenaraio starts with empty network.
At some point, cube will join the network.
The cube may leave the network.
Cube is periodically flipped.
:param number_of_cubes: Max number of cubes that can join the network
:param min_gap_between_transmissions: Minimal time-gap between transmissions
:param max_gap_between_transmissions: Maximum time-gap between transmissions
"""
network = Network(size=number_of_cubes)
mqtt = MqttHandler(HOST_ADDRESS, PORT)
while True:
# Random join event
# <- CubeJoined signal to /network
# <- CubeConfigIndication signal to /config
if random.randint(1, 100) > 50 and not network.full:
print("EVENT: CUBE JOINED NETWORK")
new_cube = network.join()
mqtt.publish(CubeJoinedSignal(new_cube.id))
mqtt.publish(CubeConfigIndication(new_cube.id, new_cube.side, new_cube.config))
# Random exit event
# <- CubeDisconnected signal to /network
elif random.randint(1, 100) > 75 and not network.empty:
print("EVENT: CUBE EXITED NETWORK")
cube_id = network.exit()
mqtt.publish(CubeDisconnectedSignal(cube_id))
# Random flip event
# <- CubeFlipped signal to /cube
# <- CubeConfigIndication signal to /config
elif random.randint(1, 100) > 20 and not network.empty:
print("EVENT: CUBE FLIPPED")
cube = random.choice(network.network)
old_side = cube.side
cube.flip()
new_side = cube.side
mqtt.publish(CubeFlippedSignal(cube.id, old_side, new_side))
mqtt.publish(CubeConfigIndication(cube.id, cube.side, cube.config))
time.sleep(random.randint(min_gap_between_transmissions, max_gap_between_transmissions))
if __name__ == "__main__":
test_active_scenario(number_of_cubes=3, min_gap_between_transmissions=2, max_gap_between_transmissions=5)
#test_cube_flipping_scenarion(number_of_cubes=1, min_gap_between_transmissions=2, max_gap_between_transmissions=5)