Esempio n. 1
0
            pout = pformat.format(self._ts_now(), p.pingnum, p.pong, p.pongs, packet.src, packet.dest, packet.type)
            out = "{} ({:>3}/{:>3}/{:>3}) time={:>4.0f}ms delay={:>4.0f}ms uptime={:d}s {:s}".format(
                pout,
                p.ping_size, p.pong_size, p.pong_size_max,
                rtt, delay, p.uptime_s, p.padding.encode("hex").upper())

            print(out)

        except ValueError as e:
            printred("{} pong {}".format(self._ts_now(), e.message))


if __name__ == '__main__':

    parser = ConfigArgumentParser("TosPingPong", description="Application arguments", formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument("destination", default=1, type=arg_hex2int, help="Ping destination")

    parser.add_argument("--count", default=0, type=int, help="Ping count, 0 for unlimited")
    parser.add_argument("--interval", default=1000, type=int, help="Ping interval (milliseconds)")

    parser.add_argument("--pongs", default=7, type=int, help="Pong count, >= 1")
    parser.add_argument("--delay", default=100, type=int, help="Subsequent pong delay")

    parser.add_argument("--ping-size", default=PingPacket.structsize, type=int, help="Ping size, can't be smaller than default")
    parser.add_argument("--pong-size", default=PongPacket.structsize, type=int, help="Pong size, can't be smaller than default")

    parser.add_argument("--connection", default="sf@localhost:9001")
    parser.add_argument("--address", default=0xFFFE, type=arg_hex2int, help="Local address")
def main():

    import argparse
    from argconfparse.argconfparse import ConfigArgumentParser, arg_str2bool
    parser = ConfigArgumentParser("subsdatasink", description="datasink arguments",
                                  formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument("--db-host", default="localhost")
    parser.add_argument("--db-port", default=5432)
    parser.add_argument("--db-user", default="datasink")
    parser.add_argument("--db-pass", default="datasink")

    parser.add_argument("--api-host", default="0.0.0.0")
    parser.add_argument("--api-port", type=int, default=54320)
    parser.add_argument("--api-user", default="user")
    parser.add_argument("--api-pass", default="pass")

    parser.add_argument("--logdir", default="/var/log/subsdatasink")

    parser.add_argument("--http", type=arg_str2bool, nargs="?", const=True, default=False)
    parser.add_argument("--server-crt", default="server.crt")
    parser.add_argument("--server-key", default="server.key")

    parser.add_argument("--debug", type=arg_str2bool, nargs="?", const=True, default=False,
                        help="Configure logging.basicConfig")
    parser.add_argument("--debug-server", type=arg_str2bool, nargs="?", const=True, default=False,
                        help="Run app with Flask debug server")

    args = parser.parse_args()

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)
    else:
        if not os.path.isdir(args.logdir):
            os.makedirs(args.logdir)

        ch = logging.handlers.TimedRotatingFileHandler(os.path.join(args.logdir, "subsdatasink.log"), when="W6", backupCount=8)
        ch.setLevel(logging.DEBUG)

        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

        ch.setFormatter(formatter)

        root_logger = logging.getLogger()
        root_logger.setLevel(logging.DEBUG)
        root_logger.addHandler(ch)

    if args.http:
        context = None
    else:
        import ssl
        # openssl genrsa -des3 -out server.key.secure 2048
        # openssl rsa -in server.key.secure -out server.key
        # openssl req -new -key server.key -out server.csr
        # openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

        context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
        context.load_cert_chain(args.server_crt, args.server_key)

    datasink = DataSinkPostgres(args.db_host, args.db_port, args.db_user, args.db_pass)

    app = Flask(__name__, static_url_path="")
    api = Api(app)
    api.add_resource(DataSinkFlask, '/api/v0/sink/<string:database>', endpoint='datasink',
                     resource_class_args=({args.api_user: args.api_pass}, datasink))

    if args.debug_server:
        app.run(debug=args.debug, host=args.api_host, port=args.api_port, ssl_context=context)
    else:
        if args.debug:
            from tornado.log import enable_pretty_logging
            enable_pretty_logging()
        tornadoserver(app, args.api_host, args.api_port, context)
def main():
    parser = ConfigArgumentParser("TosPingPong", description="Application arguments",
                                  formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument("--dest", default=AM_BROADCAST_ADDR, type=arg_hex2int,
                        help="Discover GUID of destination address")
    parser.add_argument("--guid", default="FFFFFFFFFFFFFFFF", type=arg_check_hex16str,
                        help="Discover address of GUID")

    parser.add_argument("--connection", default="sf@localhost:9002")
    parser.add_argument("--address", default=0xFFFE, type=arg_hex2int, help="Local address")
    parser.add_argument("--group", default=0x22, type=arg_hex2int, help="Local group")

    parser.add_argument("--response", action="store_true", default=False,
                        help="Send a response packet instead of a request")

    parser.add_argument("--debug", action="store_true", default=False)

    args = parser.parse_args()

    if args.debug:
        # todo setup moteconnection logging
        pass

    interrupted = threading.Event()

    def kbi_handler(sig, frm):
        del sig, frm
        interrupted.set()

    signal.signal(signal.SIGINT, kbi_handler)

    con = Connection()
    con.connect(args.connection, reconnect=5.0)

    disco = GUIDDisco(con, args)
    # disco.start()

    time.sleep(1)
    while not interrupted.is_set():
        disco.run()
        time.sleep(1)

    # disco.join()

    con.disconnect()
    con.join()

    print("done")
Esempio n. 4
0
                                  packet.src, packet.dest, packet.type)
            out = "{} ({:>3}/{:>3}/{:>3}) time={:>4.0f}ms delay={:>4.0f}ms uptime={:d}s {:s}".format(
                pout, p.ping_size, p.pong_size, p.pong_size_max, rtt, delay,
                p.uptime_s,
                p.padding.encode("hex").upper())

            print(out)

        except ValueError as e:
            printred("{} pong {}".format(self._ts_now(), e.message))


if __name__ == '__main__':

    parser = ConfigArgumentParser(
        "TosPingPong",
        description="Application arguments",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument("destination",
                        default=1,
                        type=arg_hex2int,
                        help="Ping destination")

    parser.add_argument("--count",
                        default=0,
                        type=int,
                        help="Ping count, 0 for unlimited")
    parser.add_argument("--interval",
                        default=1000,
                        type=int,
                        help="Ping interval (milliseconds)")
def main():

    import argparse
    from argconfparse.argconfparse import ConfigArgumentParser, arg_str2bool
    parser = ConfigArgumentParser(
        "subsdatasink",
        description="datasink arguments",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument("--db-host", default="localhost")
    parser.add_argument("--db-port", default=5432)
    parser.add_argument("--db-user", default="datasink")
    parser.add_argument("--db-pass", default="datasink")

    parser.add_argument("--api-host", default="0.0.0.0")
    parser.add_argument("--api-port", type=int, default=54320)
    parser.add_argument("--api-user", default="user")
    parser.add_argument("--api-pass", default="pass")

    parser.add_argument("--logdir", default="/var/log/subsdatasink")

    parser.add_argument("--http",
                        type=arg_str2bool,
                        nargs="?",
                        const=True,
                        default=False)
    parser.add_argument("--server-crt", default="server.crt")
    parser.add_argument("--server-key", default="server.key")

    parser.add_argument("--debug",
                        type=arg_str2bool,
                        nargs="?",
                        const=True,
                        default=False,
                        help="Configure logging.basicConfig")
    parser.add_argument("--debug-server",
                        type=arg_str2bool,
                        nargs="?",
                        const=True,
                        default=False,
                        help="Run app with Flask debug server")

    args = parser.parse_args()

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)
    else:
        if not os.path.isdir(args.logdir):
            os.makedirs(args.logdir)

        ch = logging.handlers.TimedRotatingFileHandler(os.path.join(
            args.logdir, "subsdatasink.log"),
                                                       when="W6",
                                                       backupCount=8)
        ch.setLevel(logging.DEBUG)

        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')

        ch.setFormatter(formatter)

        root_logger = logging.getLogger()
        root_logger.setLevel(logging.DEBUG)
        root_logger.addHandler(ch)

    if args.http:
        context = None
    else:
        import ssl
        # openssl genrsa -des3 -out server.key.secure 2048
        # openssl rsa -in server.key.secure -out server.key
        # openssl req -new -key server.key -out server.csr
        # openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

        context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
        context.load_cert_chain(args.server_crt, args.server_key)

    datasink = DataSinkPostgres(args.db_host, args.db_port, args.db_user,
                                args.db_pass)

    app = Flask(__name__, static_url_path="")
    api = Api(app)
    api.add_resource(DataSinkFlask,
                     '/api/v0/sink/<string:database>',
                     endpoint='datasink',
                     resource_class_args=({
                         args.api_user: args.api_pass
                     }, datasink))

    if args.debug_server:
        app.run(debug=args.debug,
                host=args.api_host,
                port=args.api_port,
                ssl_context=context)
    else:
        if args.debug:
            from tornado.log import enable_pretty_logging
            enable_pretty_logging()
        tornadoserver(app, args.api_host, args.api_port, context)