def get_mqtt_source(self): template = r"""{global_line_prefix}# Get current {function_name_comment} {global_line_prefix}subscribe to tinkerforge/response/{device_name}_{device_category}/{uid}/{function_name_under} {global_line_prefix}publish '{arguments}' to tinkerforge/request/{device_name}_{device_category}/{uid}/{function_name_under} """ return template.format( global_line_prefix=global_line_prefix, device_name=self.get_device().get_name().under, device_category=self.get_device().get_category().under, uid=self.get_example().get_dummy_uid(), function_name_comment=self.get_comment_name(), function_name_under=self.get_name().under, arguments=common.wrap_non_empty( '{', ', '.join(self.get_mqtt_arguments()), '}'))
def get_csharp_source(self): templateA = r""" // Set period for {function_name_comment} callback to {period_sec_short} ({period_msec}ms) {device_name}.Set{function_name_camel}CallbackConfiguration({arguments}{period_msec}{value_has_to_change}); """ templateB = r""" // Set period for {function_name_comment} callback to {period_sec_short} ({period_msec}ms) without a threshold {device_name}.Set{function_name_camel}CallbackConfiguration({arguments}{period_msec}{value_has_to_change}, '{option_char}', {minimum_maximums}); """ templateC = r""" // Configure threshold for {function_name_comment} "{option_comment}" // with a debounce period of {period_sec_short} ({period_msec}ms) {device_name}.Set{function_name_camel}CallbackConfiguration({arguments}{period_msec}{value_has_to_change}, '{option_char}', {minimum_maximums}); """ if self.get_option_char() == None: template = templateA elif self.get_option_char() == 'x': template = templateB else: template = templateC period_msec, period_sec_short, period_sec_long = self.get_formatted_period() minimum_maximums = [] for minimum_maximum in self.get_minimum_maximums(): minimum_maximums.append(minimum_maximum.get_csharp_source()) return template.format(device_name=self.get_device().get_initial_name(), function_name_camel=self.get_name().camel, function_name_comment=self.get_comment_name(), arguments=common.wrap_non_empty('', ', '.join(self.get_csharp_arguments()), ', '), period_msec=period_msec, period_sec_short=period_sec_short, value_has_to_change=common.wrap_non_empty(', ', self.get_value_has_to_change('true', 'false', ''), ''), option_char=self.get_option_char(), option_comment=self.get_option_comment(), minimum_maximums=', '.join(minimum_maximums))
def get_delphi_source(self): template = '{comment1}{global_line_prefix} {device_name}.{function_name}{arguments};{comment2}\n' result = template.format( global_line_prefix=global_line_prefix, device_name=self.get_device().get_initial_name(), function_name=self.get_name().camel, arguments=common.wrap_non_empty( '(', ',<BP>'.join(self.get_delphi_arguments()), ')'), comment1=self.get_formatted_comment1( global_line_prefix + ' {{ {0} }}\n', '\r', '\n' + global_line_prefix + ' '), comment2=self.get_formatted_comment2(' {{ {0} }}', '')) return common.break_string(result, '{}('.format(self.get_name().camel))
def get_c_source(self): templateA = r""" // Set period for {packet_comment} callback to {period_sec_short} ({period_msec}ms){declarations} check(tf_{device_under}_set_{packet_under}_period(&{device_initial}{arguments}, {period_msec}), "set {packet_comment} period"); """ templateB = r""" // Set period for {packet_comment} callback to {period_sec_short} ({period_msec}ms) // Note: The {packet_comment} callback is only called every {period_sec_long} // if the {packet_comment} has changed since the last call!{declarations} check(tf_{device_under}_set_{packet_under}_callback_period(&{device_initial}{arguments}, {period_msec}), "set {packet_comment} callback period"); """ if self.get_device().get_name().space.startswith('IMU'): template = templateA # FIXME: special hack for IMU Brick (2.0) callback behavior and name mismatch else: template = templateB period_msec, period_sec_short, period_sec_long = self.get_formatted_period( ) arg_declarations, arguments = self.get_c_arguments() declarations = common.wrap_non_empty( '\n', '\n'.join([ '{global_line_prefix}\t{decl}'.format( global_line_prefix=global_line_prefix, decl=decl) for decl in arg_declarations ]), '') return format(template, self.get_device(), self, declarations=declarations, arguments=common.wrap_non_empty(', ', ', '.join(arguments), ''), period_msec=period_msec, period_sec_short=period_sec_short, period_sec_long=period_sec_long)
def get_csharp_function(self): template1A = r""" // Callback function for {function_name_comment} callback """ template1B = r"""{override_comment} """ template2 = r""" static void {function_name_camel}CB({device_category}{device_name} sender{parameters}) {{ {write_lines}{extra_message} }} """ override_comment = self.get_formatted_override_comment( '\t// {0}', None, '\n\t// ') if override_comment == None: template1 = template1A else: template1 = template1B parameters = [] write_lines = [] for parameter in self.get_parameters(): parameters.append(parameter.get_csharp_source()) write_lines += parameter.get_csharp_write_lines() while None in write_lines: write_lines.remove(None) if len(write_lines) > 1: write_lines.append('\t\tConsole.WriteLine("");') extra_message = self.get_formatted_extra_message( '\t\tConsole.WriteLine("{0}");') if len(extra_message) > 0 and len(write_lines) > 0: extra_message = '\n' + extra_message result = template1.format(function_name_comment=self.get_comment_name(), override_comment=override_comment) + \ template2.format(device_category=self.get_device().get_category().camel, device_name=self.get_device().get_name().camel, function_name_camel=self.get_name().camel, parameters=common.wrap_non_empty(',<BP>', ',<BP>'.join(parameters), ''), write_lines='\n'.join(write_lines).replace('\r\n\r', '\n\n').strip('\r').replace('\r', '\n'), extra_message=extra_message) return common.break_string(result, '{}CB('.format(self.get_name().camel))
def get_vbnet_subroutine(self): template1A = r""" ' Callback subroutine for {function_name_comment} callback """ template1B = r"""{override_comment} """ template2 = r""" Sub {function_name_camel}CB(ByVal sender As {device_category}{device_name}{parameters}) {write_lines}{extra_message} End Sub """ override_comment = self.get_formatted_override_comment( " ' {0}", None, "\n ' ") if override_comment == None: template1 = template1A else: template1 = template1B parameters = [] write_lines = [] for parameter in self.get_parameters(): parameters.append(parameter.get_vbnet_source()) write_lines += parameter.get_vbnet_write_lines() while None in write_lines: write_lines.remove(None) if len(write_lines) > 1: write_lines.append(' Console.WriteLine("")') extra_message = self.get_formatted_extra_message( ' Console.WriteLine("{0}")') if len(extra_message) > 0 and len(write_lines) > 0: extra_message = '\n' + extra_message result = template1.format(function_name_comment=self.get_comment_name(), override_comment=override_comment) + \ template2.format(device_category=self.get_device().get_category().camel, device_name=self.get_device().get_name().camel, function_name_camel=self.get_name().camel, parameters=common.wrap_non_empty(',<BP>', ',<BP>'.join(parameters), ''), write_lines='\n'.join(write_lines).replace('\r\n\r', '\n\n').strip('\r').replace('\r', '\n'), extra_message=extra_message) return common.break_string(result, '{}CB('.format(self.get_name().camel), continuation=' _')
def get_csharp_source(self): template = r""" // Configure threshold for {function_name_comment} "{option_comment}" {device_name}.Set{function_name_camel}CallbackThreshold({arguments}'{option_char}', {minimum_maximums}); """ minimum_maximums = [] for minimum_maximum in self.get_minimum_maximums(): minimum_maximums.append(minimum_maximum.get_csharp_source()) return template.format(device_name=self.get_device().get_initial_name(), function_name_camel=self.get_name().camel, function_name_comment=self.get_comment_name(), arguments=common.wrap_non_empty('', ', '.join(self.get_csharp_arguments()), ', '), option_char=self.get_option_char(), option_comment=self.get_option_comment(), minimum_maximums=', '.join(minimum_maximums))
def get_ruby_source(self): template = r"""# Configure threshold for {function_name_comment} "{option_comment}" {device_name}.set_{function_name_under}_callback_threshold {arguments}'{option_char}', {minimum_maximums} """ minimum_maximums = [] for minimum_maximum in self.get_minimum_maximums(): minimum_maximums.append(minimum_maximum.get_ruby_source()) return template.format(device_name=self.get_device().get_initial_name(), function_name_under=self.get_name().under, function_name_comment=self.get_comment_name(), arguments=common.wrap_non_empty('', ', '.join(self.get_ruby_arguments()), ', '), option_char=self.get_option_char(), option_comment=self.get_option_comment(), minimum_maximums=', '.join(minimum_maximums))
def get_delphi_prototype(self): template = ' procedure {function_name}CB(sender: T{device_category}{device_name}{parameters});' parameters = [] for parameter in self.get_parameters(): parameters.append(parameter.get_delphi_source()) result = template.format( device_category=self.get_device().get_category().camel, device_name=self.get_device().get_name().camel, function_name=self.get_name().camel, parameters=common.wrap_non_empty(';<BP>', ';<BP>'.join(parameters), '')) return common.break_string(result, '{}CB('.format(self.get_name().camel))
def get_ruby_source(self): template1A = r"""# Register {function_name_comment} callback """ template1B = r"""{override_comment} """ template2 = r"""{device_name_initial}.register_callback({device_category}{device_name_camel}::CALLBACK_{function_name_upper}) do{parameters} {puts}{extra_message} end """ override_comment = self.get_formatted_override_comment( '# {0}', None, '\n# ') if override_comment == None: template1 = template1A else: template1 = template1B parameters = [] puts = [] for parameter in self.get_parameters(): parameters.append(parameter.get_ruby_source()) puts += parameter.get_ruby_puts() while None in puts: puts.remove(None) if len(puts) > 1: puts.append(" puts ''") extra_message = self.get_formatted_extra_message(" puts '{0}'") if len(extra_message) > 0 and len(puts) > 0: extra_message = '\n' + extra_message result = template1.format(function_name_comment=self.get_comment_name(), override_comment=override_comment) + \ template2.format(device_category=self.get_device().get_category().camel, device_name_camel=self.get_device().get_name().camel, device_name_initial=self.get_device().get_initial_name(), function_name_under=self.get_name().under, function_name_upper=self.get_name().upper, parameters=common.wrap_non_empty(' |', ',<BP>'.join(parameters), '|'), puts='\n'.join(puts).replace('\r\n\r', '\n\n').strip('\r').replace('\r', '\n'), extra_message=extra_message) return common.break_string(result, ') do |')
def get_delphi_procedure(self): template1A = r"""{{ Callback procedure for {function_name_comment} callback }} """ template1B = r"""{override_comment} """ template2 = r"""procedure TExample.{function_name_camel}CB(sender: T{device_category}{device_name}{parameters}); begin {write_lns}{extra_message} end; """ override_comment = self.get_formatted_override_comment( '{{ {0} }}', None, '\n ') if override_comment == None: template1 = template1A else: template1 = template1B parameters = [] write_lns = [] for parameter in self.get_parameters(): parameters.append(parameter.get_delphi_source()) write_lns += parameter.get_delphi_write_lns() while None in write_lns: write_lns.remove(None) if len(write_lns) > 1: write_lns.append(" WriteLn('');") extra_message = self.get_formatted_extra_message(" WriteLn('{0}');") if len(extra_message) > 0 and len(write_lns) > 0: extra_message = '\n' + extra_message result = template1.format(function_name_comment=self.get_comment_name(), override_comment=override_comment) + \ template2.format(device_category=self.get_device().get_category().camel, device_name=self.get_device().get_name().camel, function_name_camel=self.get_name().camel, parameters=common.wrap_non_empty(';<BP>', ';<BP>'.join(parameters), ''), write_lns='\n'.join(write_lns).replace('\r\n\r', '\n\n').strip('\r').replace('\r', '\n'), extra_message=extra_message) return common.break_string(result, '{}CB('.format(self.get_name().camel))
def get_rust_functions(self, type_): functions = [] template = '.. rust:function:: {struct_name}::{func_name}({params})-> {returns}\n\n{meta_table}{desc}\n' synchronous_functions = [ "get_api_version", "get_response_expected", "set_response_expected", "set_response_expected_all" ] for packet in self.get_packets('function'): if packet.get_doc_type() != type_: continue skip = -2 if packet.has_high_level() else 0 name = packet.get_name(skip=skip).under plist = common.wrap_non_empty( ', ', packet.get_rust_parameters(high_level=True), '') returns = packet.get_rust_return_type( high_level=packet.has_high_level()) if not packet.has_high_level( ) and name not in synchronous_functions: returns = "ConvertingReceiver<" + returns + ">" if "et_response_expected" in name: params = '&mut self{}'.format(plist) else: params = '&self{}'.format(plist) meta = packet.get_formatted_element_meta( lambda element, cardinality=None: element.get_rust_type( for_doc=True, cardinality=cardinality), lambda element, index=None: element.get_name(index=index ).under, return_object='conditional', explicit_string_cardinality=True, high_level=True) meta_table = common.make_rst_meta_table(meta) desc = packet.get_rust_formatted_doc() functions.append( template.format(struct_name=self.get_rust_name(), func_name=name, params=params, returns=returns, meta_table=meta_table, desc=desc)) return ''.join(functions)
def get_tvpl_parameter_list(self): params = [] for element in self.get_elements(direction='in'): params.append('L{0}'.format(element.get_name().space.replace( ' ', '_'))) if len(params) > 1: params.insert(len(params) - 1, 'Aand') if len(params) > 0: if len(self.get_elements(direction='out')) > 0: params.insert(0, 'Awith') else: params.insert(0, 'Ato') return common.wrap_non_empty(' ', ' '.join(params), '')
def get_openhab_get_channel_type_impl(self): template = """public static @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID) {{ switch(channelTypeUID.getId()) {{ {}default: break; }} return null; }}""" case_template = """case "{channel_type_id}": return {channel_type_builder_call};""" cases = [case_template.format(channel_type_id=ct.id.camel, channel_type_builder_call=ct.get_builder_call()) for ct in self.oh.channel_types] return template.format(common.wrap_non_empty('', '\n '.join(cases), '\n'))
def get_delphi_source(self): templateA = r"""{global_line_prefix} {{ Get current {function_name_comment} }} {global_line_prefix} {variable_names} := {device_name}.{function_name_camel}{arguments}; {write_lns} """ templateB = r"""{global_line_prefix} {{ Get current {function_name_comment} }} {global_line_prefix} {device_name}.{function_name_camel}{arguments}; {write_lns} """ variable_names = [] write_lns = [] for result in self.get_results(): variable_names.append(result.get_delphi_variable_name()) write_lns += result.get_delphi_write_lns() if len(variable_names) == 1: template = templateA else: template = templateB while None in write_lns: write_lns.remove(None) if len(write_lns) > 1: write_lns.insert(0, '\b') arguments = self.get_delphi_arguments() if len(variable_names) > 1: arguments += variable_names variable_names = [] result = template.format( global_line_prefix=global_line_prefix, device_name=self.get_device().get_initial_name(), function_name_camel=self.get_name().camel, function_name_comment=self.get_comment_name(), variable_names=''.join(variable_names), write_lns='\n'.join(write_lns).replace('\b\n\r', '\n').replace( '\b', '').replace('\r\n\r', '\n\n').rstrip('\r').replace('\r', '\n'), arguments=common.wrap_non_empty('(', ',<BP>'.join(arguments), ')')) return common.break_string(result, '.{0}('.format(self.get_name().camel))
def get_ruby_source(self): template = '{comment1}{global_line_prefix}{device_name}.{function_name}{arguments}{comment2}\n' arguments = common.wrap_non_empty(' ', ',<BP>'.join(self.get_ruby_arguments()), '') indent_marker = '.{} ' if arguments.strip().startswith('('): arguments = '({0})'.format(arguments.strip()) indent_marker = '.{}(' result = template.format(global_line_prefix=global_line_prefix, device_name=self.get_device().get_initial_name(), function_name=self.get_name().under, arguments=arguments, comment1=self.get_formatted_comment1(global_line_prefix + '# {0}\n', '\r', '\n' + global_line_prefix + '# '), comment2=self.get_formatted_comment2(' # {0}', '')) return common.break_string(result, indent_marker.format(self.get_name().under), continuation=' \\')
def get_perl_subroutine(self): template1A = r"""# Callback subroutine for {function_name_comment} callback """ template1B = r"""{override_comment} """ template2 = r"""sub cb_{function_name_under} {{ {parameters}{prints}{extra_message} }} """ override_comment = self.get_formatted_override_comment( '# {0}', None, '\n# ') if override_comment == None: template1 = template1A else: template1 = template1B parameters = [] prints = [] for parameter in self.get_parameters(): parameters.append(parameter.get_perl_source()) prints += parameter.get_perl_prints() while None in prints: prints.remove(None) if len(prints) > 1: prints.append(' print "\\n";') extra_message = self.get_formatted_extra_message(' print "{0}\\n";') if len(extra_message) > 0 and len(prints) > 0: extra_message = '\n' + extra_message result = template1.format(function_name_comment=self.get_comment_name(), override_comment=override_comment) + \ template2.format(function_name_under=self.get_name().under, parameters=common.wrap_non_empty(' my (', ',<BP>'.join(parameters), ') = @_;\n\n'), prints='\n'.join(prints).replace('\r\n\r', '\n\n').strip('\r').replace('\r', '\n'), extra_message=extra_message) return common.break_string(result, 'my (')
def get_openhab_callback_impl(self): transformation_template = """ private {state_or_string} transform{camel}Callback{i}({callback_args}{device_camel}Config cfg) {{ return {transform}; }}""" cb_registration = '{predicate}this.add{camel}Listener(this.reg(({args}) -> {{if({filter}) {{{updateFn}.accept("{channel_camel}", transform{channel_camel}Callback{i}({args}{comma}cfg));}}}}, this::remove{camel}Listener));{end_predicate}' regs = [] dispose_template = '{predicate}{code}{end_predicate}' dispose_code = [] lambda_transforms = [] for c in self.oh.channels: if len(c.callbacks) == 0: continue for i, callback in enumerate(c.callbacks): elements = callback.packet.get_elements(direction='out', high_level=True) regs.append(cb_registration.format( predicate='if({}) {{\n'.format(c.predicate) if c.predicate != 'true' else '', camel=callback.packet.get_name().camel, filter=callback.filter, channel_camel=c.id.camel, args=', '.join(e.get_name().headless for e in elements), updateFn='triggerChannelFn' if c.type.is_trigger_channel else 'updateStateFn', i=i, comma=', ' if len(elements) > 0 else '', end_predicate='}' if c.predicate != 'true' else '')) packet_name = callback.packet.get_name().camel if not callback.packet.has_high_level() else callback.packet.get_name(skip=-2).camel if len(c.dispose_code) > 0: dispose_code += [dispose_template.format(predicate='if({}) {{\n'.format(c.predicate) if c.predicate != 'true' else '', code=c.dispose_code, end_predicate='}' if c.predicate != 'true' else '')] lambda_transforms.append(transformation_template.format(state_or_string='String' if c.type.is_trigger_channel else 'org.eclipse.smarthome.core.types.State', camel=c.id.camel, callback_args=common.wrap_non_empty('', ', '.join(e.get_java_type() + ' ' + e.get_name().headless for e in elements), ', '), transform=callback.transform, i=i, device_camel=self.get_category().camel + self.get_name().camel)) return (regs, dispose_code, lambda_transforms)
def get_c_functions(self, type_): functions = [] template = '.. c:function:: int tf_{device_under}_{packet_under}({params})\n\n{meta_table}{desc}\n' for packet in self.get_packets('function'): if packet.get_doc_type() != type_: continue if packet.get_name().under == 'get_api_version': continue skip = -2 if packet.has_high_level() else 0 plist = common.wrap_non_empty( ', ', packet.get_c_parameters(high_level=True), '') meta = packet.get_formatted_element_meta( lambda element, cardinality=None: element.get_c_type( 'meta', cardinality=cardinality), lambda element, index=None: element.get_c_name(index=index), output_parameter='always', prefix_elements=[ (self.get_name().under, 'TF_' + self.get_name().camel + ' *', 1, 'in') ], suffix_elements=[('e_code', 'int', 1, 'return')], stream_length_suffix='_length', high_level=True) functions.append( format(template, self, packet, skip, params=format( 'TF_{device_camel} *{device_under}{plist}', self, plist=plist), meta_table=common.make_rst_meta_table(meta), desc=packet.get_c_formatted_doc())) return ''.join(functions)
def get_c_callbacks(self): callbacks = [] template = '.. c:function:: void tf_{device_under}_register_{packet_under}_callback(TF_{device_camel} *{device_under}, TF_{device_camel}{packet_camel}Handler, void *user_data)\n{params}\n{meta_table}\n{desc}\n' param_template = { 'en': """ .. code-block:: c void handler({0}) """, 'de': """ .. code-block:: c void handler({0}) """ } for packet in self.get_packets('callback'): plist = format('TF_{device_camel} *{device_under}, ', self) + common.wrap_non_empty( '', packet.get_c_parameters(), ', ') + 'void *user_data' meta = packet.get_formatted_element_meta( lambda element, cardinality=None: element.get_c_type( 'meta', cardinality=cardinality), lambda element, index=None: element.get_c_name(index=index), prefix_elements=[(format('{device_under}', self), format('TF_{device_camel} *', self), 1, 'out')], suffix_elements=[('user_data', 'void *', 1, 'out')], stream_length_suffix='_length') callbacks.append( format(template, self, packet, params=common.select_lang(param_template).format(plist), meta_table=common.make_rst_meta_table(meta), desc=packet.get_c_formatted_doc())) return ''.join(callbacks)
def get_php_constructor(self): template = """ /** * Creates an object with the unique device ID $uid. This object can * then be added to the IP connection. * * @param string $uid */ public function __construct($uid, $ipcon) {{ parent::__construct($uid, $ipcon, self::DEVICE_IDENTIFIER, self::DEVICE_DISPLAY_NAME); $this->api_version = array({0}, {1}, {2}); """ response_expected = '' for packet in self.get_packets('function'): response_expected += ' $this->response_expected[self::FUNCTION_{0}] = self::RESPONSE_EXPECTED_{1};\n' \ .format(packet.get_name().upper, packet.get_response_expected().upper()) return template.format(*self.get_api_version()) + common.wrap_non_empty('\n', response_expected, '')
def get_ruby_source(self): template = r"""{global_line_prefix}# Get current {function_name_comment}{array_content} {global_line_prefix}{variables} = {device_name}.{function_name_under}{arguments} {puts} """ comments = [] variables = [] puts = [] for result in self.get_results(): variables.append(result.get_ruby_variable()) puts += result.get_ruby_puts() if len(variables) > 1: array_content = ' as [{0}]'.format(',<BP>'.join([variable.rstrip('_') for variable in variables])) variables = [self.get_name(skip=1).under] else: array_content = '' while None in puts: puts.remove(None) if len(puts) > 1: puts.insert(0, '\b') arguments = common.wrap_non_empty(' ', ', '.join(self.get_ruby_arguments()), '') if arguments.strip().startswith('('): arguments = '({0})'.format(arguments.strip()) result = template.format(global_line_prefix=global_line_prefix, device_name=self.get_device().get_initial_name(), function_name_under=self.get_name().under, function_name_comment=self.get_comment_name(), array_content=array_content, variables=', '.join(variables), puts='\n'.join(puts).replace('\b\n\r', '\n').replace('\b', '').replace('\r\n\r', '\n\n').rstrip('\r').replace('\r', '\n'), arguments=arguments) return common.break_string(result, '# Get current {0} as ['.format(self.get_comment_name()), indent_head='#')
def get_mqtt_source(self): callback_fn = "set_{function_name_under}_period" if self.get_device( ).get_name().space.startswith( 'IMU') else "set_{function_name_under}_callback_period" callback_fn = callback_fn.format( function_name_under=self.get_name().under) templateA = r"""# Set period for {function_name_comment} callback to {period_sec_short} ({period_msec}ms) publish '{arguments}' to tinkerforge/request/{device_name}_{device_category}/{uid}/{callback_fn} """ templateB = r"""# Set period for {function_name_comment} callback to {period_sec_short} ({period_msec}ms) # Note: The {function_name_comment} callback is only called every {period_sec_long} # if the {function_name_comment} has changed since the last call! publish '{arguments}' to tinkerforge/request/{device_name}_{device_category}/{uid}/{callback_fn} """ if self.get_device().get_name().space.startswith('IMU'): template = templateA # FIXME: special hack for IMU Brick (2.0) callback behavior and name mismatch else: template = templateB period_msec, period_sec_short, period_sec_long = self.get_formatted_period( ) args = self.get_mqtt_arguments(callback_fn) + [ '"period": ' + str(period_msec) ] return template.format( device_name=self.get_device().get_name().under, uid=self.get_example().get_dummy_uid(), device_category=self.get_device().get_category().under, callback_fn=callback_fn, function_name_comment=self.get_comment_name(), arguments=common.wrap_non_empty('{', ', '.join(args), '}'), period_msec=period_msec, period_sec_short=period_sec_short, period_sec_long=period_sec_long)
def get_labview_functions(self, type_): functions = [] template = '.. labview:function:: {0}.{1}({2}){3}\n\n{4}{5}\n' cls = self.get_labview_class_name() for packet in self.get_packets('function'): if packet.get_doc_type() != type_: continue skip = -2 if packet.has_high_level() else 0 name = packet.get_name(skip=skip).camel inputs = packet.get_labview_parameter_list('in', high_level=True) outputs = packet.get_labview_parameter_list('out', high_level=True) meta = packet.get_formatted_element_meta( lambda element, cardinality=None: element.get_labview_type( cardinality=cardinality), lambda element, index=None: element.get_name(index=index ).headless, parameter_label_override={ 'en': 'Input', 'de': 'Eingabe' }, return_label_override={ 'en': 'Output', 'de': 'Ausgabe' }, explicit_string_cardinality=True, explicit_variable_stream_cardinality=True, high_level=True) meta_table = common.make_rst_meta_table(meta) doc = packet.get_labview_formatted_doc() functions.append( template.format(cls, name, inputs, common.wrap_non_empty(' -> ', outputs, ''), meta_table, doc)) return ''.join(functions)
def get_ruby_functions(self, type_): functions = [] template = '.. rb:function:: {0}#{1}{2}{3}\n\n{4}{5}\n' cls = self.get_ruby_class_name() for packet in self.get_packets('function'): if packet.get_doc_type() != type_: continue skip = -2 if packet.has_high_level() else 0 name = packet.get_name(skip=skip).under params = packet.get_ruby_parameters(high_level=True) ret_desc = packet.get_ruby_return_desc(high_level=True) meta = packet.get_formatted_element_meta( lambda element, cardinality=None: element.get_ruby_type( cardinality=cardinality), lambda element, index=None: element.get_name(index=index ).under, return_object='conditional', return_object_label_override={ 'en': 'Return Array', 'de': 'Rückgabe-Array' }, return_object_is_array=True, explicit_string_cardinality=True, explicit_variable_stream_cardinality=True, explicit_fixed_stream_cardinality=True, explicit_common_cardinality=True, high_level=True) meta_table = common.make_rst_meta_table(meta) desc = packet.get_ruby_formatted_doc() functions.append( template.format(cls, name, common.wrap_non_empty('(', params, ')'), ret_desc, meta_table, desc)) return ''.join(functions)
def get_mqtt_source(self): callback_fn = "set_{function_name_under}_callback_threshold".format( function_name_under=self.get_name().under) template = r"""# Configure threshold for {function_name_comment} "{option_comment}" publish '{arguments}' to tinkerforge/request/{device_name}_{device_category}/{uid}/{callback_fn} """ packet = None for p in self.get_device().get_packets('function'): if callback_fn in p.get_name().under: packet = p packet_min_max_names = [ elem.get_name().under[3:] for elem in packet.get_elements(direction='in') if elem.get_name().under.startswith('min') ] minimum_maximums = [] for mm_name, minimum_maximum in zip(packet_min_max_names, self.get_minimum_maximums()): minimum_maximums.append( minimum_maximum.get_mqtt_source().format(mm_name=mm_name)) option_name_unders = {'o': 'outside', '<': 'smaller', '>': 'greater'} args = self.get_mqtt_arguments(callback_fn) + [ '"option": "' + option_name_unders[self.get_option_char()] + '"' ] + minimum_maximums return template.format( device_name=self.get_device().get_name().under, device_category=self.get_device().get_category().under, callback_fn=callback_fn, function_name_comment=self.get_comment_name(), arguments=common.wrap_non_empty('{', ', '.join(args), '}'), option_comment=self.get_option_comment(), uid=self.get_example().get_dummy_uid())
def get_python_init_method(self): template = """ def __init__(self, uid, ipcon): \"\"\" Creates an object with the unique device ID *uid* and adds it to the IP Connection *ipcon*. \"\"\" Device.__init__(self, uid, ipcon, {0}.DEVICE_IDENTIFIER, {0}.DEVICE_DISPLAY_NAME) self.api_version = ({1}, {2}, {3}) """ response_expected = '' for packet in self.get_packets('function'): response_expected += ' self.response_expected[{0}.FUNCTION_{1}] = {0}.RESPONSE_EXPECTED_{2}\n' \ .format(self.get_python_class_name(), packet.get_name().upper, packet.get_response_expected().upper()) return template.format(self.get_python_class_name(), * self.get_api_version()) + common.wrap_non_empty( '', response_expected, '\n')
def get_delphi_callbacks(self): callbacks = [] template = """.. delphi:function:: property {0}.On{1} .. code-block:: delphi procedure(sender: {0}{2}) of object; {3} {4} """ cls = self.get_delphi_class_name() for packet in self.get_packets('callback'): skip = -2 if packet.has_high_level() else 0 name = packet.get_name(skip=skip).camel params = '; '.join( packet.get_delphi_parameters('doc', high_level=True)) meta = packet.get_formatted_element_meta( lambda element, cardinality=None: element.get_delphi_type( context='meta', cardinality=cardinality), lambda element, index=None: element.get_name(index=index ).headless, prefix_elements=[('sender', cls, 1, 'out')], explicit_string_cardinality=True, high_level=True) meta_table = common.make_rst_meta_table(meta) desc = packet.get_delphi_formatted_doc() callbacks.append( template.format(cls, name, common.wrap_non_empty('; ', params, ''), meta_table, desc)) return ''.join(callbacks)
def get_mathematica_source(self): template = r"""(*Configure threshold for {function_name_comment} "{option_comment}"*) option=Tinkerforge`{device_category}{device_name_camel}`THRESHOLDUOPTIONU{option_name_upper} {device_name_initial}@Set{function_name_camel}CallbackThreshold[{arguments}option,{minimum_maximums}] """ minimum_maximums = [] for minimum_maximum in self.get_minimum_maximums(): minimum_maximums.append(minimum_maximum.get_mathematica_source()) option_name_uppers = {'o': 'OUTSIDE', '<': 'SMALLER', '>': 'GREATER'} return template.format( device_category=self.get_device().get_category().camel, device_name_camel=self.get_device().get_name().camel, device_name_initial=self.get_device().get_initial_name(), function_name_camel=self.get_name().camel, function_name_under=self.get_name().under, function_name_comment=self.get_comment_name(), option_name_upper=option_name_uppers[self.get_option_char()], option_comment=self.get_option_comment(), arguments=common.wrap_non_empty( '', ','.join(self.get_mathematica_arguments()), ','), minimum_maximums=','.join(minimum_maximums))
def get_matlab_source(self): templateA = r""" % Set period for {function_name_comment} callback to {period_sec_short} ({period_msec}ms) {device_name}.set{function_name_camel}Period({arguments}{period_msec}); """ templateB = r""" % Set period for {function_name_comment} callback to {period_sec_short} ({period_msec}ms) % Note: The {function_name_comment} callback is only called every {period_sec_long} % if the {function_name_comment} has changed since the last call! {device_name}.set{function_name_camel}CallbackPeriod({arguments}{period_msec}); """ if self.get_device().get_name().space.startswith('IMU'): template = templateA # FIXME: special hack for IMU Brick (2.0) callback behavior and name mismatch else: template = templateB period_msec, period_sec_short, period_sec_long = self.get_formatted_period() return template.format(device_name=self.get_device().get_initial_name(), function_name_camel=self.get_name().camel, function_name_comment=self.get_comment_name(), arguments=common.wrap_non_empty('', ', '.join(self.get_matlab_arguments()), ', '), period_msec=period_msec, period_sec_short=period_sec_short, period_sec_long=period_sec_long)