Commit 4cfa69cf authored by Elisabetta Giani's avatar Elisabetta Giani
Browse files

CT-214: Implentation of Assign/ReleaseResources.

Added transaction id for ReleaseResources.
Updated tests.
parent 3dd17527
Loading
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -694,6 +694,15 @@ class CspSubarray(SKASubarray):
                                             "ConfigureScan execution",
                                             tango.ErrSeverity.ERR)

    class ReleaseResourcesCommand(SKASubarray.ReleaseResourcesCommand):
        
        @transaction_id
        def do(self,argin):
            #TODO: Log message
            self.logger.info('Release Resource command...')
            return (ResultCode.OK, "Assign Resources command completed OK")


    class ScanCommand(SKASubarray.ScanCommand):
        def do(self, argin):
            target_device = self.target
+67 −31
Original line number Diff line number Diff line
@@ -102,12 +102,41 @@ class MidCspSubarrayBase(CspSubarray):
    class AssignResourcesCommand(CspSubarray.AssignResourcesCommand):
        #TODO: aggiungere logica riconoscimento sottoelementi
        def do(self, argin):
            """
            Stateless hook for AssignResources() command functionality.
            :param argin: The resources to be assigned.
            :type argin: string (JSON formatted)
            :return: A tuple containing a return code and a string
                message indicating status. The message is for
                information purpose only.
            :rtype: (ResultCode, str)
            :raise: ValueError exception.
            """
            super().do(argin)
            config = json.loads(argin)
            receptor_list = list(map(int, config['dish']['receptorIDList']))
            return self.addReceptors(receptor_list)
            try:
                resources_dict = json.loads(argin)
                subarray_id = resources_dict['subarrayID']
                if subarray_id != self.target.SubID:
                    msg = f"Mismatch in subarrayID. Received: {subarray_id} {self.target.SubID}"
                    self.logger.error(msg)
                    #return (ResultCode.FAILED, msg)
                receptor_list = list(map(int, resources_dict['dish']['receptorIDList']))
            except (KeyError,  json.JSONDecodeError) as error:
                msg = f"Something wrong in Json input string: {error}"
                raise ValueError(msg)
            return self._add_receptors(receptor_list)

        def _add_receptors(self, argin):
            """
            Issue the command AddReceptors to the Mid.CBF Subarray.

        def addReceptors(self, argin):
            :param argin: The list of receptors ID to assign to the subarray
            :type argin: list of integer
            :return: A tuple containing a return code and a string
                message indicating status. The message is for
                information purpose only.
            :rtype: (ResultCode, str)
            """
            device = self.target
            # check if the CbfSubarray TANGO device is already running
            # and the proxy registered
@@ -206,20 +235,33 @@ class MidCspSubarrayBase(CspSubarray):
            return receptor_to_assign
            # PROTECTED REGION END #    //  MidCspSubarrayBase.AddReceptors

        #def succeeded(self):
        #    self.logger.info("Eccomi")
        #    action = "add_receptors_succeeded"
        #    self.state_model.perform_action(action)
        #    self.logger.info("ObsState:{}".format(self.state_model.obs_state))

    class ReleaseResourcesCommand(SKASubarray.ReleaseResourcesCommand):
    class ReleaseResourcesCommand(CspSubarray.ReleaseResourcesCommand):

        def do(self, argin):
            config = json.loads(argin)
            receptor_list = list(map(int, config['receptorIDList']))
            return self.remove_receptors(receptor_list)

        def remove_receptors(self, argin):
            """
            Stateless hook for ReleaseResources() command functionality.
            :param argin: The resources to be released.
            :type argin: string (JSON formatted)
            :return: A tuple containing a return code and a string
                message indicating status. The message is for
                information purpose only.
            :rtype: (ResultCode, str)
            """
            super().do(argin)
            try: 
                resources_dict = json.loads(argin)
                subarray_id = resources_dict['subarrayID']
                if subarray_id != int(self.target.SubID):
                    msg = f"Mismatch in subarrayID. Received: {subarray_id}"
                    self.logger.error(msg)
                    return (ResultCode.FAILED, msg)
                receptor_list = list(map(int, resources_dict['dish']['receptorIDList']))
            except (KeyError,  json.JSONDecodeError, Exception) as error:
                msg = f"Something wrong in Json input string: {error}"
                return (ResultCode.FAILED, msg)
            return self._remove_receptors(receptor_list)

        def _remove_receptors(self, argin):
            device = self.target
            # check if the CspSubarray is already connected to the CbfSubarray
            if not device._is_sc_subarray_running(device.CbfSubarray):
@@ -281,18 +323,8 @@ class MidCspSubarrayBase(CspSubarray):
                device._cmd_execution_state['removereceptors'] = CmdExecState.IDLE
                return (ResultCode.STARTED, "RemoveReceptor started")
            except tango.DevFailed as tango_err:
                #tango.Except.throw_exception("Command failed",
                #                             tango_err.args[0].desc,
                #                             "RemoveReceptors",
                #                             tango.ErrSeverity.ERR)
                message = str(tango_err.args[0].desc)
            except AttributeError as attr_err:
                #log_msg = "RemoveReceptors:" + str(attr_err)
                #self.logger.error(log_msg)
                #tango.Except.throw_exception("Command failed",
                #                             str(attr_err),
                #                             "RemoveReceptors",
                #                             tango.ErrSeverity.ERR)
                message = str(attr_err)
            return (ResultCode.FAILED, message)    

@@ -301,14 +333,18 @@ class MidCspSubarrayBase(CspSubarray):
            return self.remove_all_receptors()

        def remove_all_receptors(self):
            self.logger.info("RemoveAllReceptorsCommand")
            self.logger.info("Going to remove all receptors")
            device = self.target
            try: 
                if len(device):
                    #receptors = device._get_cbfsubarray_assigned_receptors()
                    receptors = device._receptors.assigned_to_subarray(device.SubID)
                    receptors = receptors.tolist()
                    self.logger.info("assigned receptors:{}".format(receptors))
                    return device._releaseresources_cmd_obj.do('{"receptorIDList":receptors[:]}')
                    release_dict = {}
                    release_dict["dish"] = {"receptorIDList":  receptors[:]}
                    release_dict['subarrayID'] = int(device.SubID)
                    self.logger.info(release_dict)
                    return device._releaseresources_cmd_obj.do(json.dumps(release_dict))
                return (ResultCode.OK, "No receptor to remove")
            except tango.DevFailed as df:
                log_msg = ("RemoveAllReceptors failure. Reason: {} "
@@ -619,7 +655,7 @@ class MidCspSubarrayBase(CspSubarray):
        self._assignresources_cmd_obj = self.AssignResourcesCommand(*args)
        self._releaseresources_cmd_obj = self.ReleaseResourcesCommand(*args)
        self.register_command_object("AssignResources", self.AssignResourcesCommand(*args))
        self.register_command_object("ReleaseResources", self.ReleaseAllResourcesCommand(*args))
        self.register_command_object("ReleaseResources", self.ReleaseResourcesCommand(*args))
        self.register_command_object("ReleaseAllResources", self.ReleaseAllResourcesCommand(*args))

    
+5 −1
Original line number Diff line number Diff line
@@ -331,7 +331,11 @@ class TestCspSubarray(TestBase):
        receptor_to_remove.append(i)
        # Exercise the system: remove only one receptor (with a random ID)
        LOGGER.info(f"Remove one receptor from CSP subarray01")
        self.midcsp_subarray01.RemoveResources('{"receptorIDList":receptor_to_remove}')
        param = {
            'subarrayID': 1,
            'dish': {'receptorIDList': receptor_to_remove}}
        json_config = json.dumps(param)
        self.midcsp_subarray01.ReleaseResources(json_config)
        # check 
        prober_obs_state = Probe(self.midcsp_subarray01, 'obsState', ObsState.IDLE,
                                           f"Wrong CSP Subarray obsState")