Class: MagicMirror::CommandCache

Inherits:
Array
  • Object
show all
Defined in:
lib/magic_mirror/command_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommandCache

Returns a new instance of CommandCache.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



4
5
6
# File 'lib/magic_mirror/command_cache.rb', line 4

def buffer
  @buffer
end

Instance Method Details

#<<(value) ⇒ Object



26
27
28
29
30
31
32
33
# 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?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/magic_mirror/command_cache.rb', line 90

def needs_flush?
  @buffer.length >= 0
end

#queue_buffer_to_be_transmittedObject

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



86
87
88
# File 'lib/magic_mirror/command_cache.rb', line 86

def queue_buffer_to_be_transmitted
  @timeout_thread.wakeup if @timeout_thread.status == "sleep"
end

#resetObject



36
37
38
39
40
# 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?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/magic_mirror/command_cache.rb', line 76

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

#to_embedded_javascriptObject

what if I cached this value?…



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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!Object



71
72
73
74
# 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_ripeObject



64
65
66
67
68
69
# 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?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/magic_mirror/command_cache.rb', line 80

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