Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
simple proxy to send/receive PJON messages over Redis pub/sub channel
import time
import redis
import logging
from pjon_python import over_redis_mock_client
from pjon_python import wrapper_client
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
log = logging.getLogger('main')
log.addHandler(logging.NullHandler())
log.setLevel(logging.DEBUG)
def receive_handler_redis(payload, packet_length, packet_info):
log.info("received packet from device %s with payload: %s" % (packet_info.sender_id, payload))
def receive_forward_handler_redis(payload, packet_length, packet_info):
log.info("forwarding packet to device %s with payload: %s" % (packet_info.receiver_id, payload))
pc.send(packet_info.receiver_id, payload)
def error_handler_redis(error_code, parameter):
log.error("PJON error occurred,code: %s parameter: %s" % (error_code, parameter))
def receive_handler_serial(payload, packet_length, packet_info):
log.info("received packet from device %s with payload: %s" % (packet_info.sender_id, payload))
rc.send(packet_info.receiver_id, payload, sender_id=packet_info.sender_id)
def error_handler_serial(error_code, parameter):
log.error("PJON error occurred,code: %s parameter: %s" % (error_code, parameter))
redis_cli = redis.StrictRedis(host="127.0.0.1", port=6379)
rc = over_redis_mock_client.OverRedisClient(com_port='COMX', bus_addr=200, baud=115200, transport=redis_cli)
pc = wrapper_client.PjonPiperClient(com_port='/dev/ttyUSB1', bus_addr=200, baud=115200)
rc.set_receiver(receive_handler_redis)
rc.set_receiver_forward(receive_forward_handler_redis)
rc.set_error(error_handler_redis)
rc.start_client()
pc.set_receiver(receive_handler_serial)
pc.set_error(error_handler_serial)
pc.set_piper_stdout_watchdog(timeout_sec=3) # setting a watchdog which restarts PJON-piper client if no packet arrived for 3s
pc.start_client()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
rc.stop_client()
pc.stop_client()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment