Class: Budik::Devices
- Inherits:
-
Object
- Object
- Budik::Devices
- Includes:
- Singleton
- Defined in:
- lib/budik/devices.rb
Overview
‘Devices’ class manages display and storage devices.
Instance Attribute Summary collapse
-
#storage ⇒ Object
Returns TV and storage settings.
-
#tv ⇒ Object
Returns TV and storage settings.
Instance Method Summary collapse
-
#initialize ⇒ Devices
constructor
Loads TV and storage settings.
-
#storage_load(options) ⇒ Object
Loads storage settings.
-
#storage_mount ⇒ Object
Mounts partition if needed and if not already mounted If applicable, sets ‘mounted’ and ‘awake’ states to true.
-
#storage_parse_cmd(cmd, template, subst, state_mods = {}) ⇒ Object
Substitutes device and partition in (un)mount and sleep commands.
-
#storage_sleep ⇒ Object
Spins device down if needed and if awake If applicable, sets ‘awake’ state to false.
-
#storage_unmount ⇒ Object
Unmounts partition if needed and if mounted If applicable, sets ‘mounted’ state to false.
-
#tv_load(options) ⇒ Object
Loads TV settings if TV is available.
-
#tv_off ⇒ Object
Turns off TV if needed and if on If applicable, sets ‘on’ state to false Doesn’t work on my TV.
-
#tv_on ⇒ Object
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.
Constructor Details
Instance Attribute Details
#storage ⇒ Object
Returns TV and storage settings.
26 27 28 |
# File 'lib/budik/devices.rb', line 26 def storage @storage end |
#tv ⇒ Object
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() @storage[:device] = ['device'] @storage[:partition] = ['partition'] @storage[:dir] = ['dir'] part_sub = { '$partition': @storage[:partition] } dev_sub = { '$device': @storage[:device] } storage_parse_cmd('mount', ['mount'], part_sub, mounted: false) storage_parse_cmd('unmount', ['unmount'], part_sub, unmount: true) storage_parse_cmd('sleep', ['sleep'], dev_sub, awake: false) end |
#storage_mount ⇒ Object
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_sleep ⇒ Object
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_unmount ⇒ Object
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() if ['available'] @tv[:use_if_no_video] = ['use_if_no_video'] @tv[:wait_secs_after_on] = ['wait_secs_after_on'] @tv[:on] = false else @tv[:on] = nil end end |
#tv_off ⇒ Object
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_on ⇒ Object
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 |