Exemplo n.º 1
0
class Bee2Agent(Agent):
    """Agent for the BEE2.

    Handles incoming requests.
    """
    def __init__(self):
        self.__utils = BoardUtils()

    def get(self, keys = index):
        """Get all values of parameters matching keys in keys list.

        If keys is ['index'], return a list of all available parameters.
        i.e. return a list of everything the user can get.

        Keyword arguments:
        keys -- names of parameters to get (default index)
        """
        result = []
        if keys != index:
            result += [self.__utils.readRegister(reg) for reg in keys]
            # Keep hexlification of registers separate; preserve time coupling
            for i in range(len(result)):
                if result[i] != 'Error':
                    result[i] = b2a_hex(result[i])
            #result = [b2a_hex(r) for r in result]
        else:
            result += self.__utils.listRegisters()
        return result


    def load(self, keys = index):
        """Load profiles with names which match given keys list.

        If keys is ['index'], return a list of all available profiles to load.
        i.e. return a list of everything the user can load.

        Note that one cannot load a profile on a component in use.

        Keyword arguments:
        keys -- sequence of names of profiles to load (default index)
        """
        result = []
        if keys != index:
            result += [self.__utils.loadBof(proc) for proc in keys]
        else:
            result += self.__utils.listFreeBofs()
        return result


    def set(self, keys, values):
        """Set parameters with names which match keys, with supplied values.

        Keyword arguments:
        keys -- sequence of names of parameters to set
        values -- sequence of values of parameters to set
        """
        result = []
        if len(keys) > 0 and len(keys) == len(values) \
                and keys[0] and values[0]:
            # This relies on integer rounding in the division,
            # so in this case (n/8)*8 != n for some n
            values = [a2b_hex(zfill(str(value), 8*((len(value)+7)/8)))
                      for value in values]
            result += [self.__utils.writeRegister(reg, data)
                       for reg, data in zip(keys, values)]
        else:
            self.__utils.debug('set: key or value mismatch')
            result += ['False' for i in keys]
        return result
    

    def unload(self, keys = index):
        """Unload profiles with names which match given keys list.

        If keys is ['index'], return a list of all loaded profiles.
        i.e. return a list of everything the user can unload.

        Keyword arguments:
        keys -- sequence of names of profiles to unload (default index)
        """
        result = []
        if keys != index:
            result += [self.__utils.unloadBof(proc) for proc in keys]
        else:
            result += self.__utils.listRunningBofs()
        return result


    def profiles(self, keys = index):
        """Provide information on profiles, either given or found.
        
        If keys is ['index'], return a list of info on all found profiles,
        formatted ['profile_name,s', ...] where s is state of profile.

        See the agent module's 'states' property for more information.

        Keyword arguments:
        keys -- sequence of names of profiles for info (default index)
        """
        result = []
        profs = self.__utils.listAllBofs()
        if keys != index:
            bofs = [bof[:-2] for bof in profs]
            for key in keys:
                if key in bofs:
                    result.append(profs[bofs.index(key)])
                else:
                    result.append('Error')
        else:
            result += profs
        return result