#!python

"""AER PubSuber.

Because PubSuber mainly serves as a processing unit,
so we support only custom implementation.

Author: Yuhuang Hu
Email : duguyue100@gmail.com
"""

from __future__ import print_function, absolute_import

import argparse
import json

from pyaer.utils import expandpath, import_custom_module
from pyaer.utils import parse_custom_args

parser = argparse.ArgumentParser()
parser.add_argument("--url", type=str,
                    default="tcp://127.0.0.1",
                    help="AER Publisher URL")

parser.add_argument("--pub_port", type=int,
                    default=5100,
                    help="the port that connects this publisher")
parser.add_argument("--pub_topic", type=str,
                    default="device",
                    help="publish topic name for the publisher")
parser.add_argument("--pub_name", type=str,
                    default="",
                    help="Name of the publisher")

parser.add_argument("--sub_port", type=int,
                    default=5099,
                    help="the port that connects this subscriber")
parser.add_argument("--sub_topic", type=str,
                    default="",
                    help="subscriber topic name for the subscriber")
parser.add_argument("--sub_name", type=str,
                    default="",
                    help="Name of the subscriber")

parser.add_argument("--custom_pubsuber", type=expandpath,
                    default="",
                    help="path to the custom PubSuber class")
parser.add_argument("--custom_class", type=str,
                    default="",
                    help="custom publisher class name")

args, custom_args = parser.parse_known_args()

custom_args_dict = parse_custom_args(custom_args)

# print all options
print("="*50)
print(json.dumps(
    {**args.__dict__, **custom_args_dict},
    indent=4, sort_keys=True))
print("="*50)


# define publisher
# use custom publisher
CustomPubSuber = import_custom_module(args.custom_pubsuber, args.custom_class)
pubsuber = CustomPubSuber(
    url=args.url,
    pub_port=args.pub_port, pub_topic=args.pub_topic, pub_name=args.pub_name,
    sub_port=args.sub_port, sub_topic=args.sub_topic, sub_name=args.sub_name,
    **custom_args_dict)
pubsuber.logger.info("Use custom PubSuber {}".format(args.custom_class))

# Start sending data
pubsuber.run()
