Class: InOurTime::Tic

Inherits:
Object
  • Object
show all
Defined in:
lib/iot/iot.rb

Instance Method Summary collapse

Constructor Details

#initializeTic

Returns a new instance of Tic.



28
29
30
31
32
33
# File 'lib/iot/iot.rb', line 28

def initialize
  Thread.abort_on_exception = true
  @flag = false
  init_processes
  run
end

Instance Method Details

#endedObject



72
73
74
# File 'lib/iot/iot.rb', line 72

def ended
  timeout? :ended
end

#inc_processesObject



49
50
51
# File 'lib/iot/iot.rb', line 49

def inc_processes
  @processes.each { |process| process[:value] += 1 }
end

#init_processesObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/iot/iot.rb', line 35

def init_processes
  @processes =
    { process: {
        timeout: 5,
        value:   0 },
      playing_time: {
        timeout: 1,
        value:   0 },
      ended: {
        timeout: 1,
        value:   0 }
    }
end

#killObject



53
54
55
# File 'lib/iot/iot.rb', line 53

def kill
  Thread.kill(@th_tic) if @th_tic
end

#playing_timeObject



68
69
70
# File 'lib/iot/iot.rb', line 68

def playing_time
  timeout? :playing_time
end

#processObject



64
65
66
# File 'lib/iot/iot.rb', line 64

def process
  timeout? :process
end

#runObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/iot/iot.rb', line 76

def run
  Thread.abort_on_exception = true
  @th_tic = Thread.new do
    loop do
      sleep 0.2
      @processes[:process][:value] += 1
      @processes[:playing_time][:value] += 1
      @processes[:ended][:value] += 1
      @flag = true
    end
  end
end

#timeout?(type) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
# File 'lib/iot/iot.rb', line 57

def timeout? type
  @processes[type]
  return unless @processes[type][:value] > @processes[type][:timeout]
  @processes[type][:value] = 0
  true
end

#tocObject



89
90
91
92
93
# File 'lib/iot/iot.rb', line 89

def toc
  ret_val = @flag
  @flag = false
  ret_val
end