Sha256: 8a8196d8774f3f87403f834ceaebc3da2da70393802261add5bffda0284898fd

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

module MagicMirror

  class CommandCache < Array
    attr_accessor :buffer

    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

    def <<(value)
      @buffer << value

      transmit_buffer_if_ripe
      queue_buffer_to_be_transmitted

      super
    end


    def reset
      self.clear
      MagicMirror.mirror.speak_into("MagicMirror.clearCommandCache();")
      self
    end

    # what if I cached this value?...
    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

    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

    def transmit_buffer!
      MagicMirror.mirror.speak_into(@buffer.join) if @buffer.length > 0
      @buffer = []
    end

    def time_to_send_commands_through_mirror?
      @buffer.length >= @buffer_flush_point
    end

    def we_have_a_light_message_load?
       (Time.now - @last_command_at) > 0.10
    end

    # This method goes wrong because it can fire at the same time
    # magic_mirror.speak_into may be firing....
    def queue_buffer_to_be_transmitted
      @timeout_thread.wakeup if @timeout_thread.status == "sleep"
    end

    def needs_flush?
      @buffer.length >= 0
    end





  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
magic_mirror-0.1.4 lib/magic_mirror/command_cache.rb