Class: RadioVISGenerator::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/radiovis-generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGenerator

Returns a new instance of Generator.



5
6
7
# File 'lib/radiovis-generator.rb', line 5

def initialize
  Generator.has_dependencies?
end

Class Method Details

.has_dependencies?Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
97
# File 'lib/radiovis-generator.rb', line 87

def self.has_dependencies?
  unless self.which('inkscape')
    raise SystemCallError, "Inkscape is required to use this library.\nYou can install it on Ubuntu/Debian with 'sudo apt-get install inkscape'. (And don't worry, it works headlessly and won't install a full GUI stack).\nSpecifically I need 'inkscape' in my PATH."
  end
  unless self.which('convert')
    raise SystemCallError, "ImageMagick is required to use this library.\nYou can install it on Ubuntu/Debian with 'sudo apt-get install imagemagick'.\nSpecifically I need 'convert' in my PATH."
  end
  unless self.which('composite')
    raise SystemCallError, "ImageMagick is required to use this library.\nYou can install it on Ubuntu/Debian with 'sudo apt-get install imagemagick'.\nSpecifically I need 'composite' in my PATH."
  end
end

Instance Method Details

#run(options) ⇒ Object

Runs the generator! This does all the tying together of things and Stomp interaction. The generator accepts a hash of options:

slides - An array of Slide instances.
url - The root URL at which the pictures can be found from the outside world, ie 'http://blah.com/system/radiovis-images/'.
path - The root path where we should write the images, ie '/opt/www/website/public/system/radiovis-images'
broadcast_parameters - The broadcast parameters for your station in RadioVIS topic format, ie 'fm/ECC/PI/FREQUENCY' for FM (like 'fm/ce1/c08f/10320' for Insanity Radio 103.2 FM)
username - The Stomp username
password - The Stomp password
host - The Stomp host
port - The Stomp port

Sensible defaults are provided for Stomp, but you should set them explicitly.

Raises:

  • (ArgumentsError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
# File 'lib/radiovis-generator.rb', line 21

def run(options)
  defaults = {
    slides: [],
    url: 'http://localhost/radiovis/',
    path: '/tmp/radiovis-output',
    broadcast_parameters: 'fm/ecc/pi/freq',
    username: 'system',
    password: 'manager',
    host: 'localhost',
    port: 61613
  }
  options = defaults.merge(options)
  raise ArgumentsError, "No slide instances provided!" unless options[:slides].size > 0
  puts "Generator starting - parameters #{options.inspect}"
  begin
    conn = Stomp::Connection.open options[:username], options[:password], options[:host], options[:port], false
    while true do
      # Okay - so here's the plan.
      # We have a bunch of Slide subclasses which contain what we want.
      # Every so often we check to see if any need rendering and render whichever has the highest priority.
      slides_to_render = []
      options[:slides].each do |slide|
        slides_to_render.push(slide) if slide.changed?
      end
      # If we've got nothing that's changed, pick a redisplay instead.
      if slides_to_render.size == 0
        options[:slides].each do |slide|
          slides_to_render.push(slide) if slide.redisplay?
        end
      end
      # Pick the lowest priority.
      slides_to_render.sort_by!{|slide|slide.priority}
      slide = slides_to_render.reverse[0] rescue nil
      if slide
        # Go for it!
        puts "Rendering #{slide.inspect}"
        puts "Chosen from #{slides_to_render.inspect}"
        slide_output = slide.render(options[:path])

        # Now we go ahead and send those messages.
        image_topic = "/topic/#{options[:broadcast_parameters]}/image"
        text_topic  = "/topic/#{options[:broadcast_parameters]}/text"
        image_message = "SHOW "+options[:url]+slide_output[:image_small]
        text_message  = "TEXT "+slide_output[:text][0..127]
        puts "Publishing Stomp messages"
        puts " - #{image_topic} <= #{image_message}"
        puts " - #{text_topic} <= #{text_message}"
        conn.publish(image_topic, image_message, {'persistent'=>'false'})
        conn.publish(text_topic, text_message, {'persistent'=>'false'})
        puts "Done, waiting #{slide.display_time} seconds to let people read this slide."
        # Sleep for this long before we display anything else.
        sleep slide.display_time
      else
        puts "No slide to render, sleeping a second."
        sleep 1
      end
    end
  rescue Exception => e
    puts "Got exception #{e}!"
    conn.disconnect rescue nil
    conn = nil
    sleep 5
    retry
  end
end