-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber.py
87 lines (70 loc) · 2.21 KB
/
subscriber.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
#
# Weather update client
# Connects SUB socket to tcp://localhost:5556
# Collects weather updates and finds avg temp in zipcode
#
import sys
import zmq
import threading
import time
from queue import Queue
buffer = Queue()
class printThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while True:
if not buffer.empty():
ax, ay, az, gx, gy ,gz, mx, my, mz = buffer.get()
print("%s %s %s %s %s %s %s %s %s" % (ax, ay, az, gx, gy ,gz, mx, my, mz))
class readThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
# Socket to talk to server
self.context = zmq.Context()
self.sub = self.context.socket(zmq.SUB)
self.sub.connect("tcp://localhost:5556")
# Set socket options to subscribe
self.sub.setsockopt_string(zmq.SUBSCRIBE, "imu")
def run(self):
while True:
string = self.sub.recv_string()
t, ax, ay, az, gx, gy ,gz, mx, my, mz = string.split()
buffer.put((ax, ay, az, gx, gy ,gz, mx, my, mz))
class countingThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.counter = 0
def run(self):
while True:
print("Counting %i" %(self.counter))
self.counter += 1
time.sleep(1)
rt = readThread()
# rt2 = readThread()
pt = printThread()
ot = countingThread()
rt.start()
# rt2.start()
pt.start()
ot.start()
rt.join()
# rt2.join()
pt.join()
ot.join()
# Socket to talk to server
# context = zmq.Context()
# socket = context.socket(zmq.SUB)
# socket.connect("tcp://localhost:5556")
# # Subscribe to zipcode, default is NYC, 10001
# zip_filter = sys.argv[1] if len(sys.argv) > 1 else "10001"
# # Python 2 - ascii bytes to unicode str
# if isinstance(zip_filter, bytes):
# zip_filter = zip_filter.decode('ascii')
# zip_filter = "1"
# socket.setsockopt_string(zmq.SUBSCRIBE, zip_filter)
# Process 5 updates
# while True:
# string = socket.recv_string()
# t, ax, ay, az, gx, gy ,gz, mx, my, mz = string.split()
# print("%s %s %s %s %s %s %s %s %s" % (ax, ay, az, gx, gy ,gz, mx, my, mz))