Class: LibComponent::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/openplacos/libcomponent.rb

Overview

Entry point of LibComponent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv_ = ARGV) {|_self| ... } ⇒ Component

Please provide ARGV in argument

Yields:

  • (_self)

Yield Parameters:



233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/openplacos/libcomponent.rb', line 233

def initialize(argv_ = ARGV)
  @argv        = argv_
  @description = ""
  @bus         = nil
  @main        = nil
  @inputs      = Array.new
  @outputs     = Array.new
  @parser      = Parser.new
  @parser.option(:introspect, "Return introspection of the component",{})
  @parser.option(:debug, "debug flag")
  yield self if block_given?      
  @options = @parser.process!(@argv)
  @name = @options[:name].downcase
end

Instance Attribute Details

#busObject (readonly)

Returns the value of attribute bus.



230
231
232
# File 'lib/openplacos/libcomponent.rb', line 230

def bus
  @bus
end

#mainObject (readonly)

Returns the value of attribute main.



230
231
232
# File 'lib/openplacos/libcomponent.rb', line 230

def main
  @main
end

#nameObject (readonly)

Returns the value of attribute name.



230
231
232
# File 'lib/openplacos/libcomponent.rb', line 230

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



230
231
232
# File 'lib/openplacos/libcomponent.rb', line 230

def options
  @options
end

Instance Method Details

#<<(pin_) ⇒ Object

Push pin to component



270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/openplacos/libcomponent.rb', line 270

def <<(pin_)
  if pin_.kind_of?(Input)
    @inputs << pin_
  elsif pin_.kind_of?(Output)
    @outputs << pin_
  elsif pin_.kind_of?(Array) 
    # push an array of pin
    pin_.each { |p|
      self << p
    }
  end
  pin_.set_component(self)
end

#default_name(name_) ⇒ Object

Default name for identiying your component



260
261
262
# File 'lib/openplacos/libcomponent.rb', line 260

def default_name(name_)
  @parser.option(:name,"Dbus name of the composant", :default => name_)
end

#description(desc_) ⇒ Object

provide a string describing your component



249
250
251
252
# File 'lib/openplacos/libcomponent.rb', line 249

def description(desc_)
  @description   = desc_
  @parser.banner = desc_
end

#get_input_iface(object_name_, iface_name_) ⇒ Object



356
357
358
359
360
361
# File 'lib/openplacos/libcomponent.rb', line 356

def get_input_iface(object_name_,iface_name_)
  @inputs.each { |input|
    return input if (input.name==object_name_) and (input.interface == iface_name_)
  }
  return nil
end

#get_output_iface(object_name_, iface_name_) ⇒ Object



377
378
379
380
381
382
# File 'lib/openplacos/libcomponent.rb', line 377

def get_output_iface(object_name_,iface_name_)
  @outputs.each { |output|
    return output if (output.name==object_name_) and (output.interface == iface_name_)
  }
  return nil
end

#introspectObject

Parse inputs and outputs to communicate with server



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/openplacos/libcomponent.rb', line 337

def introspect
  inputs_h = Hash.new
  outputs_h = Hash.new
  
  #call all inputs introspect and merge values
  @inputs.each { |input|
    inputs_h.merge!(input.introspect) { |key, old, new| old.merge(new) }
  }
  #call all outputs introspect and merge values
  @outputs.each { |output|
    outputs_h.merge!(output.introspect) { |key, old, new| old.merge(new) }
  }
  
  res = Hash.new
  res["input"] = {"pin" => inputs_h}
  res["output"] = {"pin" => outputs_h}
  return res
end

#on_quit(&block) ⇒ Object

Event style for quit method



330
331
332
333
334
# File 'lib/openplacos/libcomponent.rb', line 330

def on_quit(&block)
  self.singleton_class.instance_eval {
    define_method(:quit , &block)
  }
end

#option(*args_) ⇒ Object

define an option in command line (micro-optparse syntaxe)



265
266
267
# File 'lib/openplacos/libcomponent.rb', line 265

def option(*args_)
  @parser.option(*args_)
end

print function please use this method rather than puts



286
287
288
289
290
# File 'lib/openplacos/libcomponent.rb', line 286

def print_debug(arg_)
  if !@options[:introspect]
    puts arg_
  end
end

#quit_callbackObject

Method called when signal “quit” from server is raised



385
386
387
388
# File 'lib/openplacos/libcomponent.rb', line 385

def quit_callback
  self.quit if self.respond_to?(:quit)
  @main.quit if !@main.nil?
end

#runObject

Let’s rock! Run the component



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/openplacos/libcomponent.rb', line 293

def run
  intro = introspect
  if @options[:introspect]
    print intro.to_yaml
  else
    @bus = create_bus
    
    #create dbus input pins
    dbusinputs = LibComponent::DbusInput.create_dbusinputs_from_introspect(intro["input"]["pin"],self)
    name = "org.openplacos.components.#{@name.downcase}"
    if (@bus.proxy.ListNames[0].member?(name))
      LibError.quit_server(255, "#{name} already exists")
    end
    @service = @bus.request_service(name)
    dbusinputs.each { |pin|
      @service.export(pin)
    }
    
    #create and connect output pins
    if options[:debug]
      @dbusoutputs = LibComponent::DebugOutput.create_dbusoutputs_from_introspect(intro["output"]["pin"],self)
    else
      @dbusoutputs = LibComponent::DbusOutput.create_dbusoutputs_from_introspect(intro["output"]["pin"],self)
      @servicesignal = Servicesignal.new(@bus, self) # listen for service signal from server
    end
    
    Signal.trap('INT') do 
      self.quit_callback
    end
            
    @main = DBus::Main.new
    @main << @bus
    @main.run
  end
end

#set_input(object_name_, input_) ⇒ Object



363
364
365
366
367
368
# File 'lib/openplacos/libcomponent.rb', line 363

def set_input(object_name_, input_)
  @inputs.each { |input|
    input.input= input_ if (input.name==object_name_) 
  }
  return nil     
end

#set_last_iface_init(object_name_, iface_name_) ⇒ Object



370
371
372
373
374
375
# File 'lib/openplacos/libcomponent.rb', line 370

def set_last_iface_init(object_name_, iface_name_)
  @inputs.each { |input|
    input.last_iface_init = iface_name_ if (input.name==object_name_) 
  }
  return nil     
end

#version(version_) ⇒ Object

Set version of your component



255
256
257
# File 'lib/openplacos/libcomponent.rb', line 255

def version(version_)
  @parser.version = version_
end