From 0c62781f09374f99393f31b03ef03b0653deeeaf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Romain=20Bron=C3=A8s?= <romain.brones@synchrotron-soleil.fr>
Date: Fri, 14 Jun 2024 16:02:35 +0200
Subject: [PATCH] Add Utils and CLI for FA sequence alignement

---
 FofbTool/Operation.py | 24 ++++++++++++++++++------
 FofbTool/Utils.py     | 21 +++++++++++++++++++++
 scripts/FofbTool      |  6 ++++++
 3 files changed, 45 insertions(+), 6 deletions(-)

diff --git a/FofbTool/Operation.py b/FofbTool/Operation.py
index 31ed2f5..510600c 100644
--- a/FofbTool/Operation.py
+++ b/FofbTool/Operation.py
@@ -39,8 +39,7 @@ def align_ccn(nodename):
     logger.info("Trying to detect and align FA sequences")
 
     logger.debug("Reset offsets on comlbp")
-    for n in range(4):
-        prx["comlbp{}_seqoffset".format(n)]=0
+    set_comlbp_seqoffset(nodename, 0)
 
     # Loop until two consecutives identical measures
     N=[7,3]
@@ -70,9 +69,9 @@ def align_ccn(nodename):
     if -N > 0x8000:
         N=N+0x10000
 
-    logger.info("Program sequence offset {} in {}".format(N, nodename))
-    for n in range(4):
-        prx["comlbp{}_seqoffset".format(n)]=N
+
+    set_comlbp_seqoffset(nodename, N)
+    seqoffset=N
 
     logger.debug("Perform autocheck")
     logger.debug("Latch two sequence numbers without offset")
@@ -85,7 +84,20 @@ def align_ccn(nodename):
     if not N in (-1, 1):
         logger.warning("Corrected sequence offset measured = {}, something might be wrong. Run it again".format(N))
         return False
-    return True
+    return seqoffset
+
+def set_comlbp_seqoffset(nodename, N):
+    """ Set sequence offset for all comlbp interfaces """
+
+    prx=FofbTool.Utils.get_prx_from_nodename(nodename)
+    if prx is None:
+        logger.error("Failed to align CCN on {}".format(nodename))
+        return
+
+    logger.info("Program sequence offset {} in {}".format(N, nodename))
+    for n in range(4):
+        prx["comlbp{}_seqoffset".format(n)]=N
+
 
 def stop_ccn(nodename):
     """
diff --git a/FofbTool/Utils.py b/FofbTool/Utils.py
index 7dd2066..b273b14 100644
--- a/FofbTool/Utils.py
+++ b/FofbTool/Utils.py
@@ -315,4 +315,25 @@ def start_all_ccn():
     for n,p in FofbTool.Configuration.config["tangopath.fofbnodes"].items():
         FofbTool.Operation.start_ccn(n)
 
+def align_all_ccn(cellnodename, force=False):
+    """
+    Launch alignement FA sequence for Electron and Brillance Plus.
+    """
+
+    if not check_fofbnotrunning(force):
+        logger.warning("Not running align ccn because FOFB seems to be running.")
+        return
+
+    logger.debug("Use {} for sequence alignement".format(cellnodename))
+    seqoffset = FofbTool.Operation.align_ccn(cellnodename)
+    if not seqoffset:
+        logger.error("Could not align all ccn")
+        return
+
 
+    for n,p in FofbTool.Configuration.config["tangopath.fofbnodes"].items():
+        if 'cellnode' in n:
+            try:
+                FofbTool.Operation.set_comlbp_seqoffset(n, seqoffset)
+            except (tango.DevFailed, TypeError):
+                logger.error("Could not set comlbp offset on {}".format(n))
diff --git a/scripts/FofbTool b/scripts/FofbTool
index 9dff59e..a5f5cef 100755
--- a/scripts/FofbTool
+++ b/scripts/FofbTool
@@ -62,6 +62,9 @@ if __name__ == '__main__':
             help="Configuration commands for the Fofb applications blocs."+
             " 'all' is for all com and corrector. Default is 'all'")
 
+    parser.add_argument("--align-fa", type=str, metavar="cellnode-cXX",
+            help="Start alignement process for electron and brillance plus FA sequence. The designation of the cellnode must be set as argument.")
+
     parser.add_argument("--start", action="store_true",
             help="Start command for the CCN and ComBPM applications blocks.")
     parser.add_argument("--start-combpm", action="store_true",
@@ -127,3 +130,6 @@ if __name__ == '__main__':
     if args.start or args.start_ccn:
         FofbTool.Utils.start_all_ccn()
 
+    if not args.align_fa is None:
+        FofbTool.Utils.align_all_ccn(args.align_fa, force=args.force)
+
-- 
GitLab