Exemplo n.º 1
0
def get_param(node_name, name, default, params_glob):
    """ Gets a parameter from a given node """

    if params_glob and not any(
            fnmatch.fnmatch(str(name), glob) for glob in params_glob):
        # If the glob list is not empty and there are no glob matches,
        # stop the attempt to get the parameter.
        return
    # If the glob list is empty (i.e. false) or the parameter matches
    # one of the glob strings, continue to get the parameter.
    if default is not "":
        try:
            default = loads(default)
        except ValueError:
            pass  # Keep default without modifications.

    node_name = get_absolute_node_name(node_name)
    with param_server_lock:
        try:
            # call_get_parameters will fail if node does not exist.
            response = call_get_parameters(node=_node,
                                           node_name=node_name,
                                           parameter_names=[name])
            pvalue = response.values[0]
            # if type is 0 (parameter not set), the next line will raise an exception
            # and return value shall go to default.
            value = getattr(pvalue, _parameter_type_mapping[pvalue.type])
        except:
            # If either the node or the parameter does not exist, return default.
            value = default

    return dumps(value)
Exemplo n.º 2
0
    def main(self, *, args):  # noqa: D102
        with NodeStrategy(args) as node:
            node_names = get_node_names(
                node=node, include_hidden_nodes=args.include_hidden_nodes)

        if args.node_name not in [n.full_name for n in node_names]:
            return 'Node not found'

        with DirectNode(args) as node:
            response = call_get_parameters(node=node,
                                           node_name=args.node_name,
                                           parameter_names=[args.name])

            assert len(response.values) <= 1

            # requested parameter not set
            if not response.values:
                return 'Parameter not set'

            # extract type specific value
            pvalue = response.values[0]
            if pvalue.type == ParameterType.PARAMETER_BOOL:
                label = 'Boolean value is:'
                value = pvalue.bool_value
            elif pvalue.type == ParameterType.PARAMETER_INTEGER:
                label = 'Integer value is:'
                value = pvalue.integer_value
            elif pvalue.type == ParameterType.PARAMETER_DOUBLE:
                label = 'Double value is:'
                value = pvalue.double_value
            elif pvalue.type == ParameterType.PARAMETER_STRING:
                label = 'String value is:'
                value = pvalue.string_value
            elif pvalue.type == ParameterType.PARAMETER_BYTE_ARRAY:
                label = 'Byte values are:'
                value = pvalue.byte_array_value
            elif pvalue.type == ParameterType.PARAMETER_BOOL_ARRAY:
                label = 'Boolean values are:'
                value = pvalue.bool_array_value
            elif pvalue.type == ParameterType.PARAMETER_INTEGER_ARRAY:
                label = 'Integer values are:'
                value = pvalue.integer_array_value
            elif pvalue.type == ParameterType.PARAMETER_DOUBLE_ARRAY:
                label = 'Double values are:'
                value = pvalue.double_array_value
            elif pvalue.type == ParameterType.PARAMETER_STRING_ARRAY:
                label = 'String values are:'
                value = pvalue.string_array_value
            elif pvalue.type == ParameterType.PARAMETER_NOT_SET:
                label = 'Parameter not set.'
                value = None
            else:
                return "Unknown parameter type '{pvalue.type}'" \
                    .format_map(locals())

            # output response
            if not args.hide_type:
                print(label, value) if value is not None else print(label)
            else:
                print(value)
Exemplo n.º 3
0
    def get_parameter_value(node, node_name, param):
        response = call_get_parameters(node=node,
                                       node_name=node_name,
                                       parameter_names=[param])

        # requested parameter not set
        if not response.values:
            return '# Parameter not set'

        # extract type specific value
        return get_value(parameter_value=response.values[0])
Exemplo n.º 4
0
    def get_parameter_values(node, node_name, params):
        response = call_get_parameters(
            node=node, node_name=node_name,
            parameter_names=params)

        # requested parameter not set
        if not response.values:
            return '# Parameter not set'

        # extract type specific value
        return [get_value(parameter_value=i) for i in response.values]
Exemplo n.º 5
0
def has_param(node_name, name, params_glob):
    """ Checks whether a given node has a parameter or not """

    if params_glob and not any(
            fnmatch.fnmatch(str(name), glob) for glob in params_glob):
        # If the glob list is not empty and there are no glob matches,
        # stop the attempt to set the parameter.
        return False
    # If the glob list is empty (i.e. false) or the parameter matches
    # one of the glob strings, check whether the parameter exists.
    node_name = get_absolute_node_name(node_name)
    with param_server_lock:
        try:
            response = call_get_parameters(node=_node,
                                           node_name=node_name,
                                           parameter_names=[name])
        except:
            return False

    return response.values[0].type > 0 and response.values[0].type < len(
        _parameter_type_mapping)