Skip to content

Commit

Permalink
Improve script to support: gateway list from file, logging to file, g…
Browse files Browse the repository at this point in the history
…eneric usage of OTAP with delay action
  • Loading branch information
jmo-wp authored and jmo-wp committed Oct 24, 2024
1 parent 87b316d commit 1657db1
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
run: echo "WHEEL_FILE=$(ls dist/*-py3-none-any.whl)" >> $GITHUB_ENV

- name: Store artifacts
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
path: ${{ env.WHEEL_FILE }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sphinx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
cd docs
make html
- uses: actions/upload-artifact@v1
- uses: actions/upload-artifact@v4
with:
name: DocumentationHTML
path: docs/_build/html/
Expand Down
131 changes: 103 additions & 28 deletions examples/example_otap.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@
import wirepas_mesh_messaging as wmm
import logging
from random import choice
import sys

otap_delay_choices = [
"10_minutes",
"30_minutes",
"1_hour",
"6_hours",
"1_day",
"2_days",
"5_days"
]

otap_delay_choices_to_internal_values = {
otap_delay_choices[0]: wmm.ProcessingDelay.DELAY_TEN_MINUTES,
otap_delay_choices[1]: wmm.ProcessingDelay.DELAY_THIRTY_MINUTES,
otap_delay_choices[2]: wmm.ProcessingDelay.DELAY_ONE_HOUR,
otap_delay_choices[3]: wmm.ProcessingDelay.DELAY_SIX_HOURS,
otap_delay_choices[4]: wmm.ProcessingDelay.DELAY_ONE_DAY,
otap_delay_choices[5]: wmm.ProcessingDelay.DELAY_TWO_DAYS,
otap_delay_choices[6]: wmm.ProcessingDelay.DELAY_FIVE_DAYS
}

if __name__ == "__main__":
parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
Expand Down Expand Up @@ -35,72 +56,126 @@

parser.add_argument('--network',
type=int,
required=True,
help="Network address concerned by scratchpad")
parser.add_argument('--file',
help="Scratcphad to use")
parser.add_argument("--gateway",

mutual_exclusive_group_1 = parser.add_mutually_exclusive_group()
mutual_exclusive_group_1.add_argument("--gateway",
type=str,
nargs='+',
help="Gateway list to use. If specified, the OTAP will be performed on given Gateway IDs",
help="""Gateway list to use (space separator between entries).
If specified, the OTAP will be performed on given Gateway IDs.
Warning: Mutually exclusive with --gateway-from-file option""",
default=None)
mutual_exclusive_group_1.add_argument('--gateway-from-file',
type=argparse.FileType('r', encoding='UTF-8'),
help="""Gateway list to use from a file (one gateway ID per line,
UTF-8 encoded with line ending with LF character).
If specified, the OTAP will be performed on given Gateway IDs.
Warning: Mutually exclusive with --gateway option""")
parser.add_argument("--otap-delay", choices=otap_delay_choices,
help="OTAP action delay (only valid for cmd 'delayed' or 'update_delay')")

# Log parameters
parser.add_argument("--log-level", default="info", type=str,
choices=["debug", "info", "warning", "error", "critical"],
help="Default to 'info'. Log level to be displayed. "
"It has to be chosen between 'debug', 'info', 'warning', 'error' and 'critical'")
# Script behavior
parser.add_argument("--strict-mode",
dest='strict_mode',
action='store_true',
help="Stop execution at first generated error on gateway/sink operation")
args = parser.parse_args()

logging.basicConfig(format='%(levelname)s %(asctime)s %(message)s', level=logging.WARNING)
logging.basicConfig(format='%(asctime)s | [%(levelname)s] %(filename)s:%(lineno)d:%(message)s', level=args.log_level.upper(),
handlers=[
logging.StreamHandler(sys.stdout),
logging.FileHandler("example_otap.log", mode="w")
]
)

logging.debug(f"Script arguments: {args}")

wni = WirepasNetworkInterface(args.host,
args.port,
args.username,
args.password,
insecure=args.insecure)

if args.network is None:
print("No network address provided")
exit()
insecure=args.insecure,
strict_mode=args.strict_mode)

if args.cmd == "delayed" or args.cmd == "update_delay":
if args.otap_delay is None:
logging.error("No value given for OTAP action with delay")
exit()
else:
delay = otap_delay_choices_to_internal_values[args.otap_delay]
delay_str = args.otap_delay.replace('_',' ')

# Get gateway list
gateways = None
if args.gateway is None:
if args.gateway_from_file is not None:
# Load gateway list from file
gateways = []
try:
for line in args.gateway_from_file:
gateways.append(line.strip('\n'))
except ValueError:
logging.error("Invalid file format. Must be UTF-8 with lines ending with LF character")
exit()
else:
gateways = args.gateway

if gateways is None:
logging.info("OTAP to be performed on all gateways under network %d" % args.network)
else:
logging.info("OTAP to be performed on %d gateways" % len(gateways))
logging.debug(f"Gateway list {gateways}")

otapHelper = WirepasOtapHelper(wni,
args.network,
args.gateway)
gateways)

if args.cmd == "update_delay":
delay = wmm.ProcessingDelay.DELAY_THIRTY_MINUTES
print("Set new delay to %s" % delay)
logging.info("Set new delay to %s" % delay_str)
if not otapHelper.set_propagate_and_process_scratchpad_to_all_sinks(delay=delay):
print("Cannot update delay")
logging.warning("Cannot update delay on all sinks")
exit()

if args.cmd == "no_otap":
print("Set target to no otap")
logging.info("Set OTAP action to <no otap> on all sinks")
if not otapHelper.set_no_otap_to_all_sinks():
print("Cannot set no otap on all sinks")
logging.warning("Cannot set OTAP action to <no otap> on all sinks")
exit()

# Optional: find a "good" sequence
current_target_seq_set = otapHelper.get_target_scratchpad_seq_list()

print("Sequences already in used: ", current_target_seq_set)
logging.info(f"Sequences already in use: {current_target_seq_set}")
# Take a sequence from 1-254 that is not in the current set
seq = choice([i for i in range(1,254) if i not in current_target_seq_set])

print("Sequence chosen: ", seq)
logging.info("Sequence chosen: %d " % seq)

if not otapHelper.load_scratchpad_to_all_sinks(args.file, seq):
print("Cannot load scratchpad to all sinks")
logging.warning("Cannot load scratchpad to all sinks")

if args.cmd == "sink_only":
print("Processing scratchpad on all sinks")
logging.info("Processing scratchpad on all sinks")
if not otapHelper.process_scratchpad_on_all_sinks():
print("Cannot process scratchpad on all sinks")
logging.warning("Cannot process scratchpad on all sinks")
elif args.cmd == "propagate_only":
print("Set propagate only")
logging.info("Set OTAP action to <propagate only> on all sinks")
if not otapHelper.set_propagate_scratchpad_to_all_sinks():
print("Cannot set propagate and process")
logging.warning("Cannot set OTAP action <propagate only> on all sinks")
elif args.cmd == "immediately":
print("Set propagate and process")
logging.info("Set OTAP action <propagate and process> on all sinks")
if not otapHelper.set_propagate_and_process_scratchpad_to_all_sinks():
print("Cannot set propagate and process only")
logging.warning("Cannot set OTAP action <propagate and process> on all sinks")
elif args.cmd == "delayed":
print("Set propagate and process with 5 days delay")
if not otapHelper.set_propagate_and_process_scratchpad_to_all_sinks(delay=wmm.ProcessingDelay.DELAY_FIVE_DAYS):
print("Cannot set propagate and process only for 5 days")


logging.info("Set OTAP action <propagate and process with %s delay> on all sinks" % delay_str)
if not otapHelper.set_propagate_and_process_scratchpad_to_all_sinks(delay=delay):
logging.warning("Cannot set OTAP action <propagate and process only with delay> on all sinks")

0 comments on commit 1657db1

Please sign in to comment.