Class: HumbleRPi

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

Instance Method Summary collapse

Constructor Details

#initialize(device_name: 'rpi', sps_address: nil, sps_port: 59000, plugins: {}, group_id: 'root') ⇒ HumbleRPi

Returns a new instance of HumbleRPi.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/humble_rpi.rb', line 20

def initialize(device_name: 'rpi', sps_address: nil, sps_port: 59000, \
                                            plugins: {}, group_id: 'root')

  @device_name, @sps_address, @sps_port = device_name, sps_address, sps_port
  @group_id = group_id
  
  @publisher, @subscriber = sps_address ?  initialize_sps() \
                                              :  [DummyNotifier.new, nil]    

  @plugins = initialize_plugins(plugins || [])    
  
  at_exit do
    
    @plugins.each do |x|
      if x.respond_to? :on_exit then
        puts 'stopping ' + x.inspect
        Thread.new { x.on_exit() }
      end
    end
    
  end

end

Instance Method Details

#ontopic(topic, msg) ⇒ Object

triggered from a sps-sub callback



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/humble_rpi.rb', line 46

def ontopic(topic, msg)

  component = topic[/\w+$/]

  method_name = "on_#{component}_message".to_sym

  @plugins.each do |x|      

    if x.respond_to? method_name then
      x.method(method_name).call(msg)
    end

  end
end

#startObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/humble_rpi.rb', line 61

def start()

  @plugins.each do |x|
    
    if x.respond_to? :on_start then
      
      puts 'starting ' + x.inspect
      
      Thread.new do  
        
        begin
          x.on_start() 
        rescue
          puts 'problem with ' + x.to_s
          puts 'retryng ...'
          retry
        end
        
      end
      
    end
  end
      
  if @subscriber then
          
    Thread.new do
      sp = SPSSubPing.new host: @sps_address, port: @sps_port, \
                 identifier: "HumbleRPi/%s/%s" % [@group_id, @device_name]
      sp.start
    end      

    subtopics = %w(output do)
    topics = subtopics\
        .map {|x| "HumbleRPi/%s/%s/%s/#" % [@group_id, @device_name, x]}\
                                                                 .join(' | ')
    @subscriber.subscribe topic: topics
    
  else
    loop while true
  end
  
end