Commit f55b8e61 authored by Elisabetta Giani's avatar Elisabetta Giani
Browse files

AT5-257: Defined decorator to check obsState value before issuing

sub-array commands.
parent d431fced
Loading
Loading
Loading
Loading
+48 −2
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ sys.path.insert(0, utils_path)

import tango
import cspcommons
from cspcommons import AdminMode, CmdExecState
from cspcommons import AdminMode, ObsState, CmdExecState
import functools

class AdminModeCheck(object):  
@@ -50,6 +50,52 @@ class AdminModeCheck(object):
            return f(*args, **kwargs)
        return admin_mode_check
    
VALID_OBS_STATE = {'abort'      : [ObsState.CONFIGURING, ObsState.SCANNING],
                   'reset'      : [ObsState.ABORTED],
                   'configscan' : [ObsState.IDLE, ObsState.READY],
                   'scan'       : [ObsState.READY],
                   'endscan'    : [ObsState.SCANNING],
                   'endsb'      : [ObsState.READY, ObsState.IDLE],
                   'addbeam'    : [ObsState.IDLE],
                   'removebeam' : [ObsState.IDLE]
                   }
class StateAndObsStateCheck(object):
    """
    Class designed to be a decorator for the CspMaster methods.
    It checks the obsMode attribute value
    """
    def __init__(self, args=False, kwargs=False):
        self._args = args
        self._kwargs = kwargs
    def __call__(self, f):
        @functools.wraps(f)
        def obs_state_check(*args, **kwargs):
            # get the  device instance
            dev_instance = args[0]
            cmd_to_execute = self._args
            # check if the sub-array State is On, otherwise throws an exception
            if dev_instance.get_state() != tango.DevState.ON:
                msg_args = (cmd_to_execute, dev_instance.get_state())
                err_msg = ("{} command can't be issued when the"
                       " State is {} ".format(*msg_args))
                tango.Except.throw_exception("Command not executable",
                                         err_msg,
                                         "StateAndObsStateCheck decorator",
                                         tango.ErrSeverity.ERR)
            # Check the obsState attribute value: valid values for the command to
            # execute are defined by VALID_OBS_STATE dictionary
            if dev_instance._obs_state in VALID_OBS_STATE[cmd_to_execute.lower()]:
                msg_args = (cmd_to_execute, ObsState(dev_instance._obs_state).name)
                err_msg = ("{} command can't be issued when the"
                       " obsState is {} ".format(*msg_args))
            
                tango.Except.throw_exception("Command not executable",
                                         err_msg,
                                         "ObsStateCheck decorator",
                                         tango.ErrSeverity.ERR)
            return f(*args, **kwargs)
        return obs_state_check

class CmdInputArgsCheck(object):
    """
    Class designed to be a decorator for the CspMaster methods.