Class: Budik::Devices

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/budik/devices.rb

Overview

‘Devices’ class manages display and storage devices.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDevices

Loads TV and storage settings.



16
17
18
19
20
21
22
23
# File 'lib/budik/devices.rb', line 16

def initialize
  options = Config.instance.options
  @tv = {}
  @storage = { mounted: nil, awake: nil, unmount: false }

  tv_load(options['tv'])
  storage_load(options['sources']['download'])
end

Instance Attribute Details

#storageObject

Returns TV and storage settings.



26
27
28
# File 'lib/budik/devices.rb', line 26

def storage
  @storage
end

#tvObject

Returns TV and storage settings.



26
27
28
# File 'lib/budik/devices.rb', line 26

def tv
  @tv
end

Instance Method Details

#storage_load(options) ⇒ Object

Loads storage settings.

  • Args:

    • options -> Storage options (Hash).



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/budik/devices.rb', line 33

def storage_load(options)
  @storage[:device] = options['device']
  @storage[:partition] = options['partition']
  @storage[:dir] = options['dir']

  part_sub = { '$partition': @storage[:partition] }
  dev_sub = { '$device': @storage[:device] }

  storage_parse_cmd('mount', options['mount'], part_sub, mounted: false)
  storage_parse_cmd('unmount', options['unmount'], part_sub, unmount: true)
  storage_parse_cmd('sleep', options['sleep'], dev_sub, awake: false)
end

#storage_mountObject

Mounts partition if needed and if not already mounted If applicable, sets ‘mounted’ and ‘awake’ states to true



75
76
77
78
79
80
81
82
# File 'lib/budik/devices.rb', line 75

def storage_mount
  unless @storage[:mounted].nil? || @storage[:mounted] == true
    system(@storage[:mount_command])
  end

  @storage[:mounted] = true unless @storage[:mounted].nil?
  @storage[:awake] = true unless @storage[:awake].nil?
end

#storage_parse_cmd(cmd, template, subst, state_mods = {}) ⇒ Object

Substitutes device and partition in (un)mount and sleep commands.

Example

cmd = ‘sleep’ template = ‘sudo hdparm -y $device’ subst = { ‘$device’: ‘/dev/sda’ } state_mods = { awake: false }

Parsed command: ‘sudo hdparm -y /dev/sda’ State ‘awake’ set to false

  • Args:

    • cmd -> Command (‘mount’, ‘unmount’ or ‘sleep’).

    • template -> Command template (String).

    • subst -> Variable to substitute (Hash, variable: value).

    • state_mods -> State modifiers (Hash).



64
65
66
67
68
69
70
71
# File 'lib/budik/devices.rb', line 64

def storage_parse_cmd(cmd, template, subst, state_mods = {})
  return if template.empty?

  cmd = (cmd + '_command').to_sym
  var, val = subst.first
  @storage[cmd] = template.gsub(var.to_s, val)
  state_mods.each { |state, setting| @storage[state] = setting }
end

#storage_sleepObject

Spins device down if needed and if awake If applicable, sets ‘awake’ state to false



97
98
99
100
101
102
103
104
105
# File 'lib/budik/devices.rb', line 97

def storage_sleep
  sleep_check = @storage[:awake].nil? || @storage[:awake] == false

  unless sleep_check || @storage[:mounted] == true
    system(@storage[:sleep_command])
  end

  @storage[:awake] = false unless @storage[:awake].nil?
end

#storage_unmountObject

Unmounts partition if needed and if mounted If applicable, sets ‘mounted’ state to false



86
87
88
89
90
91
92
93
# File 'lib/budik/devices.rb', line 86

def storage_unmount
  unmount = !@storage[:unmount]
  unless unmount || @storage[:mounted].nil? || @storage[:mounted] == false
    system(@storage[:unmount_command])
  end

  @storage[:mounted] = false unless @storage[:mounted].nil?
end

#tv_load(options) ⇒ Object

Loads TV settings if TV is available.

  • Args:

    • options -> TV options (Hash).



112
113
114
115
116
117
118
119
120
# File 'lib/budik/devices.rb', line 112

def tv_load(options)
  if options['available']
    @tv[:use_if_no_video] = options['use_if_no_video']
    @tv[:wait_secs_after_on] = options['wait_secs_after_on']
    @tv[:on] = false
  else
    @tv[:on] = nil
  end
end

#tv_offObject

Turns off TV if needed and if on If applicable, sets ‘on’ state to false Doesn’t work on my TV



138
139
140
141
142
143
144
# File 'lib/budik/devices.rb', line 138

def tv_off
  unless @tv[:on].nil? || @tv[:on] == false
    system('echo "standby 0" | cec-client -s >/dev/null')
  end

  @tv[:on] = false unless @tv[:on].nil?
end

#tv_onObject

Turns on TV if needed and if not already on Gives TV time to turn on, then sets active HDMI as active source If applicable, sets ‘on’ state to true



125
126
127
128
129
130
131
132
133
# File 'lib/budik/devices.rb', line 125

def tv_on
  unless @tv[:on].nil? || @tv[:on] == true
    system('echo "on 0" | cec-client -s >/dev/null')
    sleep(@tv[:wait_secs_after_on]) unless @tv[:wait_secs_after_on].nil?
    system('echo "as" | cec-client -s >/dev/null')
  end

  @tv[:on] = true unless @tv[:on].nil?
end