kopia lustrzana https://github.com/ogre/pizero_tracker
improved test zmq client
rodzic
4029e56e22
commit
d0407b0056
|
@ -17,5 +17,7 @@ dtoverlay=i2c-gpio,i2c_gpio_sda=2,i2c_gpio_scl=3
|
|||
|
||||
|
||||
dependencies
|
||||
|
||||
boost program_options
|
||||
|
||||
https://github.com/zeromq/cppzmq/
|
|
@ -1,25 +1,83 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
from __future__ import print_function
|
||||
import time
|
||||
import json
|
||||
import traceback
|
||||
from pprint import pprint
|
||||
import zmq
|
||||
|
||||
def main():
|
||||
|
||||
def process_reply(msg):
|
||||
if "dynamics" in msg or "nmea" in msg:
|
||||
msg = msg.replace("'", '"')
|
||||
try:
|
||||
data = json.loads(msg)
|
||||
return data
|
||||
except:
|
||||
print(traceback.format_exc())
|
||||
# time.sleep(1)
|
||||
|
||||
|
||||
def main2():
|
||||
REQUEST_TIMEOUT = 3000
|
||||
REQUEST_RETRIES = 1e12
|
||||
SERVER_ENDPOINT = "tcp://192.168.1.22:6666"
|
||||
|
||||
context = zmq.Context(1)
|
||||
socket = context.socket(zmq.REQ)
|
||||
socket.connect( 'tcp://192.168.1.22:6666' )
|
||||
|
||||
query_msgs = ['nmea', 'temp', 'invalid']
|
||||
print("Connecting to server")
|
||||
client = context.socket(zmq.REQ)
|
||||
client.connect(SERVER_ENDPOINT)
|
||||
|
||||
while True:
|
||||
poll = zmq.Poller()
|
||||
poll.register(client, zmq.POLLIN)
|
||||
|
||||
query_msgs = ['nmea', 'dynamics', 'invalid']
|
||||
|
||||
retries_left = REQUEST_RETRIES
|
||||
while retries_left:
|
||||
time.sleep(1)
|
||||
for qm in query_msgs:
|
||||
socket.send(qm)
|
||||
msg = socket.recv()
|
||||
print msg
|
||||
time.sleep(1)
|
||||
print("\n\nSending (%s)" % qm)
|
||||
client.send(qm)
|
||||
|
||||
expect_reply = True
|
||||
while expect_reply:
|
||||
socks = dict(poll.poll(REQUEST_TIMEOUT))
|
||||
if socks.get(client) == zmq.POLLIN:
|
||||
reply = client.recv()
|
||||
if reply:
|
||||
print("OK")
|
||||
pprint( process_reply(reply) )
|
||||
expect_reply = False
|
||||
else:
|
||||
break
|
||||
else:
|
||||
print("No response from server, retrying")
|
||||
# Socket is confused. Close and remove it.
|
||||
client.setsockopt(zmq.LINGER, 0)
|
||||
client.close()
|
||||
poll.unregister(client)
|
||||
retries_left -= 1
|
||||
if retries_left == 0:
|
||||
print("Server seems to be offline, abandoning")
|
||||
break
|
||||
print("Reconnecting and resending (%s)" % qm)
|
||||
# Create new connection
|
||||
client = context.socket(zmq.REQ)
|
||||
client.connect(SERVER_ENDPOINT)
|
||||
poll.register(client, zmq.POLLIN)
|
||||
client.send(qm)
|
||||
|
||||
context.term()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
while(1):
|
||||
try:
|
||||
main2()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
except:
|
||||
print(traceback.format_exc())
|
||||
|
||||
# while(1): pass
|
||||
|
|
Ładowanie…
Reference in New Issue