From 328563b7e6e17f466ef48c07cf3535aac99f8e3a Mon Sep 17 00:00:00 2001 From: Bryant Jones Date: Tue, 12 Aug 2025 12:36:16 -0400 Subject: [PATCH] Docs: Add TC10 .py example --- docs/icsneopy/examples.rst | 37 +--------------- examples/python/tc10/tc10.py | 85 ++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 35 deletions(-) create mode 100644 examples/python/tc10/tc10.py diff --git a/docs/icsneopy/examples.rst b/docs/icsneopy/examples.rst index 82aed15..5cae90c 100644 --- a/docs/icsneopy/examples.rst +++ b/docs/icsneopy/examples.rst @@ -92,42 +92,9 @@ Monitor Ethernet Status TC10 ==== -.. code-block:: python +:download:`Download example <../../examples/python/tc10/tc10.py>` - import icsneopy - import time - - devices: list[icsneopy.Device] = icsneopy.find_all_devices() - - device: icsneopy.Device = devices[0] - - print(f"using {device} for TC10") - - device.open() - - netid = icsneopy.Network.NetID.AE_01 - - if device.supports_tc10(): - # initial - status = device.get_tc10_status(netid) - print(f"initial status: wake: {status.wakeStatus}, sleep: {status.sleepStatus}") - time.sleep(1) - - # sleep - device.request_tc10_sleep(netid) - print("waiting 1s for sleep to occur") - time.sleep(1) - status = device.get_tc10_status(netid) - print(f"post sleep status: wake: {status.wakeStatus}, sleep: {status.sleepStatus}") - - # wake - device.request_tc10_wake(netid) - print("waiting 1s for wake to occur") - time.sleep(1) - status = device.get_tc10_status(netid) - print(f"post wake status: wake: {status.wakeStatus}, sleep: {status.sleepStatus}") - else: - print(f"{device} does not support TC10") +.. literalinclude:: ../../examples/python/tc10/tc10.py DoIP Ethernet Activation ======================== diff --git a/examples/python/tc10/tc10.py b/examples/python/tc10/tc10.py new file mode 100644 index 0000000..bf6f487 --- /dev/null +++ b/examples/python/tc10/tc10.py @@ -0,0 +1,85 @@ +import icsneopy +import argparse + + +def main(): + parser = get_parser() + args = parser.parse_args() + run_test(args) + + +def find_device(serial: str) -> icsneopy.Device: + devices = icsneopy.find_all_devices() + for d in devices: + if d.get_serial() == serial: + print(f"opening device {serial}") + return d + return None + + +def run_test(args): + # find the device + d = find_device(args.serial) + if d is None: + print(f"error: unable to find device {args.serial}") + exit(1) + + # open the device + if not d.open(): + print(f"error: unable to open device {args.serial}") + exit(1) + + # check if TC10 is supported + if not d.supports_tc10(): + print(f"error: device does not support TC10 {args.serial}") + exit(1) + + # send the request on all networks + for n in args.networks: + net = getattr(icsneopy.Network.NetID, n) + if args.send_wake: + print(f"requesting TC10 wake on network {net}") + if not d.request_tc10_wake(net): + print(f"error: unable to send TC10 wake on device {args.serial}") + exit(1) + elif args.send_sleep: + print(f"requesting TC10 sleep on network {net}") + if not d.request_tc10_sleep(net): + print(f"error: unable to send TC10 sleep on device {args.serial}") + exit(1) + + # close the device + print(f"closing device {args.serial}") + d.close() + + +def get_parser(): + parser = argparse.ArgumentParser(description="TC10 wake request") + parser.add_argument( + "serial", + help="The serial number of the device", + ) + parser.add_argument( + "--networks", + nargs="+", + help="List of icsneopy networks to use. Multiple networks accepted, e.g. '--networks ETHERNET_01 AE_01'", + required=True, + ), + group = parser.add_mutually_exclusive_group(required=True) + group.add_argument( + "--send-wake", + help="Trigger TC10 wake on the selected networks", + action="store_true", + default=False, + ) + group.add_argument( + "--send-sleep", + help="Trigger TC10 sleep on the selected networks", + action="store_true", + default=False, + ) + return parser + + +if __name__ == "__main__": + main()