From f8f8e899ee844ba71ab03e5174acfabb1413dfe4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Romain=20BRON=C3=88S?= <romain.brones@synchrotron-soleil.fr>
Date: Tue, 6 Aug 2024 11:39:30 +0200
Subject: [PATCH] Refactor CCN operations

---
 FofbTool/Operation.py | 128 ++++++++++++++++++++++++------------------
 FofbTool/Utils.py     |   6 +-
 2 files changed, 77 insertions(+), 57 deletions(-)

diff --git a/FofbTool/Operation.py b/FofbTool/Operation.py
index 1e22731..c33f7bb 100644
--- a/FofbTool/Operation.py
+++ b/FofbTool/Operation.py
@@ -101,114 +101,134 @@ def align_ccn(node_tangopath, comlbpif=0):
         return None
     return seqoffset
 
-def stop_ccn(nodename):
+def stop_ccn(node_tangopath, ccnif=[]):
     """
     Stop the communication with cellnode on the specified fofbnode.
 
     PARAMETERS
     ----------
-    nodename: str
-        The target fofbnode, ie 'cellnode-c09' or 'centralnode'
+    node_tangopath: str
+        The target fofbnode tango path, ie 'ans/dg/fofb-cellnode-c09'
+
+    ccnif: list(int)
+        List of the interface to stop. If empty, all the possible interface will be stopped.
 
     """
-    prx=FofbTool.Utils.get_prx_from_nodename(nodename)
-    if prx is None:
-        logger.error("Failed to stop CCN on {}".format(nodename))
-        return
 
+    try:
+        prx=tango.DeviceProxy(node_tangopath)
+        prx.ping()
+    except tango.DevFailed:
+        logger.error("Failed to obtain tango proxy or to ping to {}".format(node_tangopath))
+        return None
+
+    if len(ccnif) == 0:
+        ccnif = [int(a[7]) for a in prx.get_attribute_list() if "ccnpack" in a if "control" in a]
 
-    logger.info("Stopping CCN on {}".format(nodename))
-    nint=1
-    if 'central' in nodename:
-        nint = 4
-    for n in range(nint):
+    logger.info("Stopping CCN on {}".format(node_tangopath))
+    for n in ccnif:
         prx["ccnpack{}_control".format(n)] = False
         prx["ccnunpack{}_control".format(n)] = False
 
-def reset_ccn(nodename):
+def reset_ccn(node_tangopath, ccnif=[]):
     """
     Reset the communication with cellnode on the specified fofbnode.
 
     PARAMETERS
     ----------
-    nodename: str
-        The target fofbnode, ie 'cellnode-c09' or 'centralnode'
+    node_tangopath: str
+        The target fofbnode tango path, ie 'ans/dg/fofb-cellnode-c09'
+
+    ccnif: list(int)
+        List of the interface to stop. If empty, all the possible interface will be stopped.
 
     """
-    prx=FofbTool.Utils.get_prx_from_nodename(nodename)
-    if prx is None:
-        logger.error("Failed to reset CCN on {}".format(nodename))
-        return
 
-    logger.info("Reset CCN on {}".format(nodename))
-    nint=1
-    if 'central' in nodename:
-        nint = 4
-    for n in range(nint):
+    try:
+        prx=tango.DeviceProxy(node_tangopath)
+        prx.ping()
+    except tango.DevFailed:
+        logger.error("Failed to obtain tango proxy or to ping to {}".format(node_tangopath))
+        return None
+
+    if len(ccnif) == 0:
+        ccnif = [int(a[6]) for a in prx.get_attribute_list() if "ccneth" in a if "gt_reset" in a]
+
+    logger.info("Reset CCN on {}".format(node_tangopath))
+    for n in ccnif:
         prx["ccneth{}_reset".format(n)] = 0x60000001 # impossible to write 0xE0000001
         prx["ccneth{}_gt_reset".format(n)] = 1
 
     time.sleep(2)
 
-    for n in range(nint):
+    for n in ccnif:
         prx["ccneth{}_gt_reset".format(n)] = 0
         prx["ccneth{}_reset".format(n)] = 0
 
-    ack_ccn(nodename)
+    ack_ccn(node_tangopath, ccnif)
 
-def start_ccn(nodename):
+def start_ccn(node_tangopath, ccnif=[]):
     """
     Start the communication with cellnode on the specified fofbnode.
 
     PARAMETERS
     ----------
-    nodename: str
-        The target fofbnode, ie 'cellnode-c09' or 'centralnode'
+    node_tangopath: str
+        The target fofbnode tango path, ie 'ans/dg/fofb-cellnode-c09'
+
+    ccnif: list(int)
+        List of the interface to stop. If empty, all the possible interface will be stopped.
 
     """
-    prx=FofbTool.Utils.get_prx_from_nodename(nodename)
-    if prx is None:
-        logger.error("Failed to start CCN on {}".format(nodename))
-        return
+    try:
+        prx=tango.DeviceProxy(node_tangopath)
+        prx.ping()
+    except tango.DevFailed:
+        logger.error("Failed to obtain tango proxy or to ping to {}".format(node_tangopath))
+        return None
 
-    logger.info("Starting CCN on {}".format(nodename))
+    if len(ccnif) == 0:
+        ccnif = [int(a[7]) for a in prx.get_attribute_list() if "ccnpack" in a if "control" in a]
 
-    ack_ccn(nodename)
+    logger.info("Starting CCN on {}".format(node_tangopath))
 
-    nint=1
-    if 'central' in nodename:
-        nint = 4
-    for n in range(nint):
-        prx["ccnpack{}_control".format(n)] = True
-        prx["ccnunpack{}_control".format(n)] = True
+    ack_ccn(node_tangopath, ccnif)
 
-def ack_ccn(nodename):
+    for n in ccnif:
+        prx["ccnpack{}_control".format(n)] = 1
+        prx["ccnunpack{}_control".format(n)] = 1
+
+def ack_ccn(node_tangopath, ccnif=[]):
     """
     Start the communication with cellnode on the specified fofbnode.
 
     PARAMETERS
     ----------
-    nodename: str
-        The target fofbnode, ie 'cellnode-c09' or 'centralnode'
+    node_tangopath: str
+        The target fofbnode tango path, ie 'ans/dg/fofb-cellnode-c09'
+
+    ccnif: list(int)
+        List of the interface to stop. If empty, all the possible interface will be stopped.
 
     """
-    prx=FofbTool.Utils.get_prx_from_nodename(nodename)
-    if prx is None:
-        logger.error("Failed to ack CCN on {}".format(nodename))
-        return
+    try:
+        prx=tango.DeviceProxy(node_tangopath)
+        prx.ping()
+    except tango.DevFailed:
+        logger.error("Failed to obtain tango proxy or to ping to {}".format(node_tangopath))
+        return None
 
+    if len(ccnif) == 0:
+        ccnif = [int(a[7]) for a in prx.get_attribute_list() if "ccnpack" in a if "control" in a]
 
-    logger.info("Ack CCN error on {}".format(nodename))
-    nint=1
-    if 'central' in nodename:
-        nint = 4
-    for n in range(nint):
+    logger.info("Ack CCN error on {}".format(node_tangopath))
+    for n in ccnif:
         prx["ccnpack{}_reset_error".format(n)] = True
         prx["ccnunpack{}_reset_error".format(n)] = True
 
     time.sleep(1)
 
-    for n in range(nint):
+    for n in ccnif:
         prx["ccnpack{}_reset_error".format(n)] = False
         prx["ccnunpack{}_reset_error".format(n)] = False
 
diff --git a/FofbTool/Utils.py b/FofbTool/Utils.py
index 6c1fafb..014c345 100644
--- a/FofbTool/Utils.py
+++ b/FofbTool/Utils.py
@@ -276,8 +276,8 @@ def stop_all_ccn(force=False):
         return
 
     for n,p in FofbTool.Configuration.config["tangopath.fofbnodes"].items():
-        FofbTool.Operation.stop_ccn(n)
-        FofbTool.Operation.reset_ccn(n)
+        FofbTool.Operation.stop_ccn(p)
+        FofbTool.Operation.reset_ccn(p)
 
 
 
@@ -313,7 +313,7 @@ def start_all_ccn():
     """
 
     for n,p in FofbTool.Configuration.config["tangopath.fofbnodes"].items():
-        FofbTool.Operation.start_ccn(n)
+        FofbTool.Operation.start_ccn(p)
 
 def align_all_ccn(cellnodename, force=False):
     """
-- 
GitLab