DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
amc_butler.py
Go to the documentation of this file.
1#!/usr/bin/env python
2"""
3Created on: 05/06/2025 11:16
4
5Author: Shyam Bhuller, Alessandro Thea
6
7Description: Check the status of an AMC or multiple AMCs to ensure they can be reached.
8"""
9
10import subprocess
11import time
12import sh
13import click
14from rich import print
15
16import tdemodules
17
18def ping(host):
19 return subprocess.call(['ping', "-c", '1', host]) == 0
20
21class Commands():
22 def __init__(self, controllers : dict):
23 self.controllerscontrollers = controllers
24
25 def stop(self):
26 for k, v in self.controllerscontrollers.items():
27 print(f"starting: {k}")
28 v.card_stop()
29 return
30
31 def start(self):
32 for k, v in self.controllerscontrollers.items():
33 print(f"starting: {k}")
34 v.card_start()
35 return
36
37 def status(self):
38 # Constantly get the status of the AMCs to check whether they can be accessed.
40 print("[yellow]Press ctrl-C to exit.[/yellow]")
41 ti = 10
42 t = time.time()
43 while True:
44 for k, v in self.controllerscontrollers.items():
45 print(f"Reading status of AMC with IP: {k})")
46 v.card_status()
47 print(f"[cyan]Sleeping {ti}s[/cyan]")
48 time.sleep(ti)
49 print(f"-- {time.time() - t:.2g} s elapsed")
50 return
51
52
53@click.command()
54@click.argument('crate_ip', type=str)
55@click.option('-a', '--amcs', type=int, multiple=True, default=[i for i in range(10)])
56@click.option('-c', 'cmd', type=click.Choice(['arping', 'status', 'start', 'stop']), default=None)
57def main(crate_ip, amcs, cmd):
58
59 # get list of amcs from the command arguments
60 try:
61 sh.ping(["-c", '1', crate_ip])
62 print(f"[green]uTCA crate {crate_ip} is active[/green]")
63 except sh.ErrorReturnCode as e:
64 print(f"[red]Could not ping uTCA Crate at IP: {crate_ip}[/red]")
65 exit(1)
66
67 crate_subnet = crate_ip.rsplit(".", 1)[0]
68
69 amc_ips = { i: crate_subnet + f".{i + 1}" for i in amcs}
70
71 if cmd=='arping':
72 # Arping the crate, just for completeness
73 try:
74 r = sh.arping(["-c", '1', crate_ip])
75 print(f"- [green]uTCA crate {crate_ip} responded to arping[/green]")
76 print(r)
77 except sh.ErrorReturnCode as e:
78 print(f"- [red]Could not arping uTCA Crate at IP: {crate_ip}[/red]")
79
80 # Arping the AMCs
81 for amc, amc_ip in amc_ips.items():
82 try:
83 r = sh.arping(["-c", '1', amc_ip])
84 print(f"- [green]AMC {amc} ({amc_ip}) responded to arping[/green]")
85 print(r)
86
87 except sh.ErrorReturnCode as e:
88 print(f"- [red]Could not arping AMC {amc} at IP: {amc_ip}[/red]")
89
90 # Arping the NIC
91 nic_ip = crate_subnet+'.129'
92 try:
93 r = sh.arping(["-c", '1', nic_ip])
94 print(f"- [green]NIC data sink {nic_ip} responded to arping[/green]")
95 print(r)
96 except sh.ErrorReturnCode as e:
97 print(f"- [red]Could not arping NIC data at IP: {nic_ip}[/red]")
98
99 else:
100 if cmd:
101 controllers = { amc_ip:tdemodules.AMCController(amc_ip, 54321 + (i + 1)) for i,amc_ip in amc_ips.items() }
102 print(controllers)
103 cmds = Commands(controllers)
104 getattr(cmds, cmd)()
105 else:
106 print("no command was provided.")
107
108 return
109
110if __name__ == "__main__":
111
112 main()
__init__(self, dict controllers)
Definition amc_butler.py:22
main(crate_ip, amcs, cmd)
Definition amc_butler.py:57
ping(host)
Definition amc_butler.py:18