class MagicMirror::CommandCache

Attributes

buffer[RW]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/magic_mirror/command_cache.rb, line 6
def initialize
  @buffer = []
  @buffer_flush_point = 500
  @last_command_at = Time.now

  @mutex = Mutex.new

  @timeout_thread = Thread.new {
    while true
      @mutex.synchronize {
        MagicMirror.command_cache.transmit_buffer!
      }
      Thread.stop
      sleep 0.2
    end
  }

  super
end

Public Instance Methods

<<(value) click to toggle source
Calls superclass method
# File lib/magic_mirror/command_cache.rb, line 26
def <<(value)
  @buffer << value

  transmit_buffer_if_ripe
  queue_buffer_to_be_transmitted

  super
end
needs_flush?() click to toggle source
# File lib/magic_mirror/command_cache.rb, line 90
def needs_flush?
  @buffer.length >= 0
end
queue_buffer_to_be_transmitted() click to toggle source

This method goes wrong because it can fire at the same time magic_mirror.speak_into may be firing.…

# File lib/magic_mirror/command_cache.rb, line 86
def queue_buffer_to_be_transmitted
  @timeout_thread.wakeup if @timeout_thread.status == "sleep"
end
reset() click to toggle source
# File lib/magic_mirror/command_cache.rb, line 36
def reset
  self.clear
  MagicMirror.mirror.speak_into("MagicMirror.clearCommandCache();")
  self
end
time_to_send_commands_through_mirror?() click to toggle source
# File lib/magic_mirror/command_cache.rb, line 76
def time_to_send_commands_through_mirror?
  @buffer.length >= @buffer_flush_point
end
to_embedded_javascript() click to toggle source

what if I cached this value?…

# File lib/magic_mirror/command_cache.rb, line 43
def to_embedded_javascript
  string = ""

  string += "<script>"

  time_offset = 40
  self.each_slice(100) do |a|
    string += "setTimeout(function(){"
    string += "z(#{a.to_json});"
    if time_offset == 40
      string += "}, #{0});"
      time_offset+=5
    else
      string += "}, #{time_offset+=5});"
    end
  end

  string += "</script>"
  string
end
transmit_buffer!() click to toggle source
# File lib/magic_mirror/command_cache.rb, line 71
def transmit_buffer!
  MagicMirror.mirror.speak_into(@buffer.join) if @buffer.length > 0
  @buffer = []
end
transmit_buffer_if_ripe() click to toggle source
# File lib/magic_mirror/command_cache.rb, line 64
def transmit_buffer_if_ripe
  if time_to_send_commands_through_mirror? or we_have_a_light_message_load?
    transmit_buffer!
  end
  @last_command_at = Time.now
end
we_have_a_light_message_load?() click to toggle source
# File lib/magic_mirror/command_cache.rb, line 80
def we_have_a_light_message_load?
   (Time.now - @last_command_at) > 0.10
end