Class: Subduino::ArdPS
- Inherits:
-
Object
- Object
- Subduino::ArdPS
- Defined in:
- lib/subduino/ard_ps.rb
Class Method Summary collapse
-
.read ⇒ Object
Read PubSub.
-
.redis ⇒ Object
Redis PubSub.
-
.stop! ⇒ Object
Fatality.
-
.write(msg) ⇒ Object
Write PubSub.
Class Method Details
.read ⇒ Object
Read PubSub
Join ‘subduino’ channel and send messages to I/O
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/subduino/ard_ps.rb', line 20 def read return Log.warn "[PubSub] Not started..." unless redis @psthread = Thread.new do begin redis.subscribe('subduino') do |on| on.subscribe {|klass, num_subs| Log.info "[PubSub] Subscribed to #{klass} (#{num_subs} subscriptions)" } on. do |klass, msg| Log.info "[PubSub] #{klass} - #{msg}" ArdIO.write msg # @redis.unsubscribe if msg == 'exit' end on.unsubscribe {|klass, num_subs| Log.info "[PubSub] Unsubscribed to #{klass} (#{num_subs} subscriptions)" } end rescue => e Log.error "[PubSub] Error #{e}" Log.error e.backtrace.join("\n") exit 1 end end end |
.redis ⇒ Object
Redis PubSub
Direct access to the Redis instance.
11 12 13 |
# File 'lib/subduino/ard_ps.rb', line 11 def redis @redis ||= Redis.new(:timeout => 0) end |
.stop! ⇒ Object
Fatality
55 56 57 58 59 |
# File 'lib/subduino/ard_ps.rb', line 55 def stop! Thread.kill @psthread redis.disconnect if redis Log.info "[PubSub] K.I.A" end |
.write(msg) ⇒ Object
Write PubSub
Write a message to the ‘subduino’ PubSub channel.
47 48 49 |
# File 'lib/subduino/ard_ps.rb', line 47 def write(msg) redis.publish('subduino', msg) end |