-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
111 lines (79 loc) · 2.67 KB
/
test.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
107
108
109
110
111
from concurrent.futures import ThreadPoolExecutor, thread
from datetime import datetime
import time
from RedisROS.Nodes import Clock
from RedisROS import Node
NAMESPACE = "namespace"
class test_pub(Node):
def __init__(self, ref: str = "test_pub") -> None:
super().__init__(
ref=ref,
namespace=NAMESPACE
)
self.test_publisher = self.create_publisher(
msg_type=None,
topic="test_topic",
)
self.create_timer(
timer_period_sec=0.01,
callback=self.test_callback
)
def run(self):
self.spin()
# time.sleep(2)
# self.default_spin_condition.set_value(value=False, instant=True)
# test_timer.cancel()
# self.destroy_timer(timer=test_timer)
def test_callback(self):
msg = f"Hello World! |{datetime.timestamp(datetime.now())}|"
# print(f"\n-> Publishing message: {msg}")
self.test_publisher.publish(msg, instant=False)
class test_sub(Node):
def __init__(self, ref: str = "test_sub") -> None:
super().__init__(
ref=ref,
namespace=NAMESPACE
)
self.test_subscription = self.create_subscription(
msg_type=None,
topic="test_topic",
callback=self.test_callback,
qos_profile=None,
callback_group=None
)
def run(self):
self.spin()
def test_callback(self, msg):
ts = datetime.timestamp(datetime.now())
msg_ts = msg.split("|")[1]
# -> Get timestamp difference
ts_diff = ts - float(msg_ts)
# -> Convert timestamp difference to seconds
ts_diff_sec = ts_diff
print(f"-> New message: {msg}, (comm delay: {ts_diff_sec})")
from redis import Redis
from threading import Thread
from multiprocessing import Process
client = Redis()
client.flushall()
# clock_thread = Process(target=lambda: Clock(ref="Sim_clock", namespace=NAMESPACE, time_factor=10))
# clock_thread.start()
# pub_lst = []
# sub_lst = []
# for i in range(3):
# pub_lst.append(test_pub(ref=f"Publisher_{i+1}"))
# sub_lst.append(test_sub(ref=f"Subscriber_{i+1}"))
# thread_lst = []
# for i in range(10):
# thread_lst.append(Thread(target=test_pub))
# thread_lst.append(Thread(target=test_sub))
# for thread in thread_lst:
# thread.start()
test_pub1 = test_pub()
test_sub1 = test_sub()
print("Created Nodes")
pub_thread = Thread(target=test_pub1.run)
sub_thread = Thread(target=test_sub1.run)
pub_thread.start()
sub_thread.start()
print("Primed")