Skip to content

Commit

Permalink
developed cli tool for dnp3dnp3
Browse files Browse the repository at this point in the history
  • Loading branch information
kefeimo committed Dec 8, 2022
1 parent c3051c7 commit 76ee8ea
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 17 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ ms(1666217819745) INFO server - Accepted connection from: 127.0.0.1
```


> **_NOTE:_** Use `python -m dnp3demo -h` to see demo options
```
$ python -m dnp3demo -h
Basic dnp3 use case demo
optional arguments:
-h, --help show this help message and exit
-d sec, --duration sec
Configure demo duration (in seconds.)
-rm, --run-master-station
Run a standalone master station.
-ro, --run-outstation
Run a standalone master station.
-dg, --demo-get-point
Demo get point workflow.
-ds, --demo-set-point
Demo set point workflow.
```

## For Developers

pydnp3 is a thin wrapper around opendnp3 classes. Documentation for the opendnp3
Expand Down
59 changes: 46 additions & 13 deletions src/dnp3demo/__main__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@
from dnp3demo import data_retrieval_demo
from dnp3demo import data_retrieval_demo, control_workflow_demo, \
data_retrieval_demo_master, data_retrieval_demo_outstation
import argparse


def main():
"""Read the Real Python article feed"""

# If an article ID is given, then show the article
# if len(sys.argv) > 1:
# article = feed.get_article(sys.argv[1])
# viewer.show(article)
#
# # If no ID is given, then show a list of all articles
# else:
# site = feed.get_site()
# titles = feed.get_titles()
# viewer.show_list(site, titles)
data_retrieval_demo.main()
# Initialize parser
parser = argparse.ArgumentParser(
prog="dnp3demo",
description="Basic dnp3 use case demo",
# epilog="Thanks for using %(prog)s! :)",
)

# Adding optional argument
parser.add_argument("-d", "--duration", action="store",
help="Configure demo duration (in seconds.)",
type=int,
default=60,
metavar="sec")

# use exclusive group, choose among scripts to run,
# by default run --demo-get-point

group = parser.add_mutually_exclusive_group(required=False)
group.description = "some description"
group.add_argument("-rm", "--run-master-station", action="store_true",
help="Run a standalone master station.", )
group.add_argument("-ro", "--run-outstation", action="store_true",
help="Run a standalone master station.")
group.add_argument("-dg", "--demo-get-point", action="store_true",
help="Demo get point workflow.")
group.add_argument("-ds", "--demo-set-point", action="store_true",
help="Demo set point workflow.")

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

# choose among the following scripts to run
if args.demo_set_point:
control_workflow_demo.main()
elif args.run_master_station:
data_retrieval_demo_master.main(duration=args.duration)
elif args.run_outstation:
data_retrieval_demo_outstation.main(duration=args.duration)
else: # run as default
data_retrieval_demo.main()


if __name__ == "__main__":
main()
print("============")
print("End of Demo.")
print("============")
11 changes: 9 additions & 2 deletions src/dnp3demo/data_retrieval_demo_master.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import datetime
from time import sleep
import time


stdout_stream = logging.StreamHandler(sys.stdout)
stdout_stream.setFormatter(logging.Formatter('%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s'))
Expand All @@ -19,15 +21,20 @@

# logging.basicConfig(filename='demo.log', level=logging.DEBUG)

def main():

def main(duration=300):
master_application = MyMasterNew()
master_application.start()
_log.debug('Initialization complete. Master Station in command loop.')
# outstation_application = MyOutStationNew()
# _log.debug('Initialization complete. OutStation in command loop.')

start = time.time()
end = time.time()

count = 0
while count < 20:
while count < 1000 and (end - start) < duration:
end = time.time()
sleep(3) # Note: hard-coded, master station query every 1 sec.

count += 1
Expand Down
10 changes: 8 additions & 2 deletions src/dnp3demo/data_retrieval_demo_outstation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from dnp3_python.dnp3station.outstation_new import MyOutStationNew

from time import sleep
import time

import datetime

Expand All @@ -21,7 +22,7 @@
# _log.setLevel(logging.ERROR)


def main():
def main(duration=300):
# cmd_interface_master = MasterCmdNew()
# master_application = MyMasterNew(log_handler=MyLogger(),
# listener=AppChannelListener(),
Expand All @@ -34,7 +35,12 @@ def main():
_log.debug('Initialization complete. OutStation in command loop.')

count = 0
while count < 20:
start = time.time()
end = time.time()

count = 0
while count < 1000 and (end - start) < duration:
end = time.time()
sleep(5) # Note: hard-coded, master station query every 1 sec.

count += 1
Expand Down

0 comments on commit 76ee8ea

Please sign in to comment.