Skip to content

Commit

Permalink
polished cmd parse input
Browse files Browse the repository at this point in the history
  • Loading branch information
kefeimo committed Nov 4, 2024
1 parent d33ca95 commit 28f4f83
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 45 deletions.
12 changes: 6 additions & 6 deletions src/dnp3_python/dnp3station/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,12 +697,12 @@ class MasterApplication:

def __init__(
self,
master_ip: str | None = "0.0.0.0",
outstation_ip: str | None = "127.0.0.1",
port: int | None = 20000,
master_id: int | None = 2,
outstation_id: int | None = 1,
concurrency_hint: int | None = 1,
master_ip: str = "0.0.0.0",
outstation_ip: str = "127.0.0.1",
port: int = 20000,
master_id: int = 2,
outstation_id: int = 1,
concurrency_hint: int = 1,
log_handler=asiodnp3.ConsoleLogger().Create(),
listener=asiodnp3.PrintingChannelListener().Create(),
soe_handler=SOEHandler(),
Expand Down
10 changes: 5 additions & 5 deletions src/dnp3_python/dnp3station/outstation.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,11 +550,11 @@ class OutStationApplication:

def __init__(
self,
outstation_ip: str | None = "0.0.0.0",
port: int | None = 20000,
master_id: int | None = 2,
outstation_id: int | None = 1,
concurrency_hint: int | None = 1,
outstation_ip: str = "0.0.0.0",
port: int = 20000,
master_id: int = 2,
outstation_id: int = 1,
concurrency_hint: int = 1,
channel_log_level=opendnp3.levels.NORMAL,
outstation_log_level=opendnp3.levels.NORMAL,
db_sizes: opendnp3.DatabaseSizes = None,
Expand Down
27 changes: 14 additions & 13 deletions src/dnp3demo/run_master.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,39 @@ def input_prompt(display_str=None, prefix="", menu_indicator="") -> str:
def setup_args(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
# Adding optional argument
parser.add_argument(
"--master-ip=",
"--master-ip",
action="store",
default="0.0.0.0",
type=str,
metavar="<IP>",
help="master ip, default: 0.0.0.0",
)
parser.add_argument(
"--outstation-ip=",
"--outstation-ip",
action="store",
default="127.0.0.1",
type=str,
metavar="<IP>",
help="outstation ip, default: 127.0.0.1",
)
parser.add_argument(
"--port=",
"--port",
action="store",
default=20000,
type=int,
metavar="<PORT>",
help="port, default: 20000",
)
parser.add_argument(
"--master-id=",
"--master-id",
action="store",
default=2,
type=int,
metavar="<ID>",
help="master id, default: 2",
)
parser.add_argument(
"--outstation-id=",
"--outstation-id",
action="store",
default=1,
type=int,
Expand Down Expand Up @@ -98,18 +98,19 @@ def main(parser=None, *args, **kwargs):
parser = setup_args(parser)

# Read arguments from command line
args = parser.parse_args()
parse_args = parser.parse_args()

# dict to store args.Namespace
d_args = vars(args)
print(__name__, d_args)
# d_args = vars(parse_args)
# print(__name__, d_args)
print(f"{parse_args=}")
# print(args.__dir__())
master_application = MasterApplication(
master_ip=d_args.get("master_ip="),
outstation_ip=d_args.get("outstation_ip="),
port=d_args.get("port="),
master_id=d_args.get("master_id="),
outstation_id=d_args.get("outstation_id="),
master_ip=parse_args.master_ip,
outstation_ip=parse_args.outstation_ip,
port=parse_args.port,
master_id=parse_args.master_id,
outstation_id=parse_args.outstation_id,
# channel_log_level=opendnp3.levels.ALL_COMMS,
# master_log_level=opendnp3.levels.ALL_COMMS
# soe_handler=SOEHandler(soehandler_log_level=logging.DEBUG)
Expand Down
46 changes: 25 additions & 21 deletions src/dnp3demo/run_outstation.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,66 +36,69 @@ def setup_args(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
# parser.add_argument("-mip", "--master-ip", action="store", default="0.0.0.0", type=str,
# metavar="<IP>")
parser.add_argument(
"--outstation-ip=",
"--outstation-ip",
action="store",
default="0.0.0.0",
type=str,
metavar="<IP>",
help="outstation ip, default: 0.0.0.0",
)
parser.add_argument(
"--port=",
"--port",
action="store",
default=20000,
type=int,
metavar="<PORT>",
help="port, default: 20000",
)
parser.add_argument(
"--master-id=",
"--master-id",
action="store",
default=2,
type=int,
metavar="<ID>",
help="master id, default: 2",
)
parser.add_argument(
"--outstation-id=",
"--outstation-id",
action="store",
default=1,
type=int,
metavar="<ID>",
help="master id, default: 1",
)
parser.add_argument(
"--init-random", action="store_true", help="if appears, init with random values"
"--init-random",
action="store_true",
default=False,
help="if appears, init with random values, default: False",
)
parser.add_argument(
"--n-ai=",
"--n-ai",
action="store",
default=5,
type=int,
metavar="<num>",
help="number of AnalogInput, default: 5",
)
parser.add_argument(
"--n-ao=",
"--n-ao",
action="store",
default=5,
type=int,
metavar="<num>",
help="number of AnalogOutput, default: 5",
)
parser.add_argument(
"--n-bi=",
"--n-bi",
action="store",
default=5,
type=int,
metavar="<num>",
help="number of BinaryInput, default: 5",
)
parser.add_argument(
"--n-bo=",
"--n-bo",
action="store",
default=5,
type=int,
Expand Down Expand Up @@ -132,11 +135,12 @@ def main(parser=None, *args, **kwargs):
parser = setup_args(parser)

# Read arguments from command line
args = parser.parse_args()
parse_args: argparse.Namespace = parser.parse_args()

# dict to store args.Namespace
d_args = vars(args)
print(__name__, d_args)
# d_args = vars(parse_args)
# print(__name__, f"{d_args=}")
print(f"{parse_args=}")

# db_sizes = opendnp3.DatabaseSizes.AllTypes(count=5)
# db_sizes = opendnp3.DatabaseSizes(
Expand All @@ -152,18 +156,18 @@ def main(parser=None, *args, **kwargs):

outstation_application = OutStationApplication(
# masterstation_ip_str=args.master_ip,
outstation_ip=d_args.get("outstation_ip="),
port=d_args.get("port="),
master_id=d_args.get("master_id="),
outstation_id=d_args.get("outstation_id="),
outstation_ip=parse_args.outstation_ip,
port=parse_args.port,
master_id=parse_args.master_id,
outstation_id=parse_args.outstation_id,
# db_sizes=db_sizes,
# channel_log_level=opendnp3.levels.ALL_COMMS,
# master_log_level=opendnp3.levels.ALL_COMMS
# soe_handler=SOEHandler(soehandler_log_level=logging.DEBUG)
numAnalog=d_args.get("n_ai="),
numAnalogOutputStatus=d_args.get("n_ao="),
numBinary=d_args.get("n_bi="),
numBinaryOutputStatus=d_args.get("n_bo="),
numAnalog=parse_args.n_ai,
numAnalogOutputStatus=parse_args.n_ao,
numBinary=parse_args.n_bi,
numBinaryOutputStatus=parse_args.n_bo,
)
_log.info("Connection Config", outstation_application.get_config())
outstation_application.start()
Expand All @@ -176,7 +180,7 @@ def main(parser=None, *args, **kwargs):

# Additional init for demo purposes
# if d_args.get("init_random")==True, init with random values
if d_args.get("init_random"):
if parse_args.init_random:
outstation_application.update_db_with_random()

count = 0
Expand Down

0 comments on commit 28f4f83

Please sign in to comment.