class MagicMirror::Mirror

Public Class Methods

new() click to toggle source
# File lib/magic_mirror/mirror.rb, line 4
def initialize

end

Public Instance Methods

command_cache() click to toggle source
# File lib/magic_mirror/mirror.rb, line 55
def command_cache
  MagicMirror.command_cache
end
command_cache=(value) click to toggle source
# File lib/magic_mirror/mirror.rb, line 59
def command_cache=(value)
  MagicMirror.command_cache = value
end
init_servers!() click to toggle source
# File lib/magic_mirror/mirror.rb, line 8
def init_servers!
  Thin::Logging.silent = true
  print "\nActivating the Magic Mirror."

  @threads = []

  # start sinatra
  @threads << start_sinatra
  @threads << start_faye

  puts "\nThe Magic Mirror is Activated.  Navigate to http://127.0.0.1:4567"
  self
end
speak_into(msg) click to toggle source

sends messages through faye server to web interface

# File lib/magic_mirror/mirror.rb, line 64
def speak_into(msg)
  require 'net/http'
  channel = "/0001"

  message = {:channel => channel, :data => msg}
  uri = URI.parse("http://localhost:#{FAYE_PORT}/faye")
  begin
    Net::HTTP.post_form(uri, :message => message.to_json)
  rescue Exception => e
    $stderr.puts "failed to send message to faye server and thus webclient"
    $stderr.puts e.message
    return false
  end
  true
end
start_faye() click to toggle source
# File lib/magic_mirror/mirror.rb, line 39
def start_faye
  queue = Queue.new # allows waiting for sinatra boot

  Thread.new {
    bayeux = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)
    bayeux.listen(FAYE_PORT)
    bayeux.run!
    #run bayeux
  }

  #queue.pop # blocks until server is booted
  wait_until_faye_is_up
  print "."

end
start_sinatra() click to toggle source
# File lib/magic_mirror/mirror.rb, line 22
def start_sinatra
  queue = Queue.new # allows waiting for sinatra boot
  $stderr_backup = $stderr.dup
  $stderr.reopen("/dev/null", "w")

  Thread.new {
    SinatraSilver::App.run! do |server|
      queue.push("started") # tells caller thread to continue
    end
  }

  queue.pop # blocks until sinatra is booted
  print "."

  $stderr = $stderr_backup.dup
end
wait_until_faye_is_up() click to toggle source
# File lib/magic_mirror/mirror.rb, line 80
def wait_until_faye_is_up
  require 'net/http'
  while(true) do
    begin
      message = {:channel => "/0001", :data => "booted"}
      uri = URI.parse("http://localhost:#{FAYE_PORT}/faye")
      response = Net::HTTP.post_form(uri, :message => message.to_json)
    rescue
    end

    break if response and response.code == "200" and response.body == "[{\"channel\":\"/0001\",\"successful\":true}]"
    sleep 0.1
  end
end