Skip to content

Commit 145ec35

Browse files
committed
#2 Code Structure Optimization
1 parent 8ae51ee commit 145ec35

File tree

2 files changed

+4
-43
lines changed

2 files changed

+4
-43
lines changed

python/infinite_sense_core/message.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@ def pub(self, topic: str, metadata: str):
4343
except zmq.ZMQError as e:
4444
print(f"[ERROR] Publish error: {e}")
4545

46-
def pub_struct(self, topic: str, data: bytes):
47-
try:
48-
with self.lock:
49-
self.publisher.send_string(+topic, zmq.SNDMORE)
50-
self.publisher.send(data, zmq.DONTWAIT)
51-
except zmq.ZMQError as e:
52-
print(f"[ERROR] Publish struct error: {e}")
53-
5446
def sub(self, topic: str, callback):
5547
def run():
5648
try:
@@ -62,30 +54,11 @@ def run():
6254
data_msg = subscriber.recv_string()
6355
if topic_msg != topic:
6456
continue
65-
callback(data_msg)
57+
callback(data_msg.encode(), len(data_msg))
6658
except zmq.ZMQError as e:
6759
print(f"[ERROR] Exception in Sub thread for topic [{topic}]: {e}")
6860

6961
thread = threading.Thread(target=run, daemon=True)
7062
thread.start()
7163
self.sub_threads.append(thread)
7264

73-
def sub_struct(self, topic: str, callback):
74-
def run():
75-
try:
76-
context = zmq.Context()
77-
subscriber = context.socket(zmq.SUB)
78-
subscriber.connect(self.endpoint)
79-
subscriber.setsockopt_string(zmq.SUBSCRIBE, topic)
80-
while True:
81-
topic_msg = subscriber.recv_string()
82-
data_msg = subscriber.recv()
83-
if topic_msg != topic:
84-
continue
85-
callback(data_msg)
86-
except zmq.ZMQError as e:
87-
print(f"[ERROR] Exception in SubStruct for topic [{topic}]: {e}")
88-
89-
thread = threading.Thread(target=run, daemon=True)
90-
thread.start()
91-
self.sub_threads.append(thread)

python/main.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
import logging
22
import time
3-
import threading
43
from infinite_sense_core.infinit_sense import Synchronizer
4+
from infinite_sense_core.message import Messenger
55

6-
class Messenger:
7-
@staticmethod
8-
def get_instance():
9-
return Messenger()
10-
11-
def sub_struct(self, topic, callback):
12-
def run():
13-
while True:
14-
time.sleep(1)
15-
fake_data = b"fake binary data"
16-
callback(fake_data, len(fake_data))
17-
threading.Thread(target=run, daemon=True).start()
186

197
class MockSensor:
208
def initialization(self):
@@ -52,8 +40,8 @@ def main():
5240
synchronizer.start()
5341

5442
messenger = Messenger.get_instance()
55-
messenger.sub_struct("imu_1", imu_callback)
56-
messenger.sub_struct("cam_1", image_callback)
43+
messenger.sub("imu_1", imu_callback)
44+
messenger.sub("cam_1", image_callback)
5745

5846
logging.info("Synchronizer is running. Press Ctrl+C to exit.")
5947

0 commit comments

Comments
 (0)