From 839537628a3452cce970467b764acf285c48cd1d 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 13:51:34 +0200
Subject: [PATCH] refactor comlbp operation

---
 FofbTool/Operation.py | 55 +++++++++++++++++++++++--------------------
 FofbTool/Utils.py     |  6 ++---
 2 files changed, 33 insertions(+), 28 deletions(-)

diff --git a/FofbTool/Operation.py b/FofbTool/Operation.py
index 952197f..ec039b4 100644
--- a/FofbTool/Operation.py
+++ b/FofbTool/Operation.py
@@ -10,7 +10,6 @@ import tango
 import logging
 import numpy as np
 import time
-import FofbTool.Utils
 
 
 # Get the module logger
@@ -315,64 +314,70 @@ def ack_combpm(node_tangopath):
 #    OPERATIONS ON COMLBP
 ###################################################################################################
 
-def stop_comlbp(cellnodename):
+def stop_comlbp(node_tangopath):
     """
     Stop the communication with LBP on the specified cellnode.
 
     PARAMETERS
     ----------
-    cellnodename: str
-        The target fofbnode, ie 'cellnode-c09'
+    node_tangopath: str
+        The target fofbnode tango path, ie 'ans/dg/fofb-cellnode-c09'
 
     """
-    prx=FofbTool.Utils.get_prx_from_nodename(cellnodename)
-    if prx is None:
-        logger.error("Failed to stop ComLBP on {}".format(p))
-        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("Stopping ComLBP on {}".format(cellnodename))
+    logger.info("Stopping ComLBP on {}".format(node_tangopath))
     for n in range(4):
         prx["comlbp{}_control".format(n)] = 1
     time.sleep(1)
     for n in range(4):
         prx["comlbp{}_control".format(n)] = 0
 
-def start_comlbp(cellnodename):
+def start_comlbp(node_tangopath):
     """
     Start the communication with LBP on the specified cellnode.
 
     PARAMETERS
     ----------
-    cellnodename: str
-        The target fofbnode, ie 'cellnode-c09'
+    node_tangopath: str
+        The target fofbnode tango path, ie 'ans/dg/fofb-cellnode-c09'
 
     """
-    prx=FofbTool.Utils.get_prx_from_nodename(cellnodename)
-    if prx is None:
-        logger.error("Failed to start ComLBP on {}".format(p))
-        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 ComLBP on {}".format(cellnodename))
+    logger.info("Starting ComLBP on {}".format(node_tangopath))
     for n in range(4):
         prx["comlbp{}_control".format(n)] = 0x10
 
 
-def reset_comlbp(cellnodename):
+def reset_comlbp(node_tangopath):
     """
     Reset the communication with LBP on the specified cellnode.
 
     PARAMETERS
     ----------
-    cellnodename: str
-        The target fofbnode, ie 'cellnode-c09'
+    node_tangopath: str
+        The target fofbnode tango path, ie 'ans/dg/fofb-cellnode-c09'
 
     """
-    prx=FofbTool.Utils.get_prx_from_nodename(cellnodename)
-    if prx is None:
-        logger.error("Failed to reset ComLBP on {}".format(p))
-        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("Reset ComLBP on {}".format(cellnodename))
+    logger.info("Reset ComLBP on {}".format(node_tangopath))
     for n in range(4):
         prx["comlbp{}_control".format(n)] = 0x3
 
diff --git a/FofbTool/Utils.py b/FofbTool/Utils.py
index a91ea31..edba76a 100644
--- a/FofbTool/Utils.py
+++ b/FofbTool/Utils.py
@@ -258,8 +258,8 @@ def stop_all_comlbp(force=False):
     for n,p in FofbTool.Configuration.config["tangopath.fofbnodes"].items():
         if 'cellnode' in n:
             try:
-                FofbTool.Operation.reset_comlbp(n)
-                FofbTool.Operation.stop_comlbp(n)
+                FofbTool.Operation.reset_comlbp(p)
+                FofbTool.Operation.stop_comlbp(p)
             except (tango.DevFailed, TypeError):
                 logger.error("Could not stop cpmlbp on {}".format(n))
 
@@ -300,7 +300,7 @@ def start_all_comlbp():
     for n,p in FofbTool.Configuration.config["tangopath.fofbnodes"].items():
         if 'cellnode' in n:
             try:
-                FofbTool.Operation.start_comlbp(n)
+                FofbTool.Operation.start_comlbp(p)
             except (tango.DevFailed, TypeError):
                 logger.error("Could not start LBP on {}".format(n))
 
-- 
GitLab