Skip to content

Commit

Permalink
Command: config, spec revisions
Browse files Browse the repository at this point in the history
Config, Devices, Player, Rng
  • Loading branch information
JBlackN committed Sep 16, 2015
1 parent b05bf64 commit 1f61046
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 28 deletions.
9 changes: 7 additions & 2 deletions lib/budik/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ def initialize(command, opts)

private

def config(_opts)
def config(opts)
if opts.reset
Config.instance.reset
else
Config.instance.edit
end
end

def run(opts)
Expand Down Expand Up @@ -50,7 +55,7 @@ def run_prepare(opts, sources, devices, rng, io)
end

def run_download(source, dl_method, storage)
dl_method == 'stream' ? source : storage.download(source)
dl_method == 'stream' ? source : storage.download_sources(source)
end

def run_play(source, devices, player, storage)
Expand Down
14 changes: 11 additions & 3 deletions lib/budik/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ def platform?
rpi?(os) ? :rpi : os
end

def edit(_opts)
# TODO
def edit
options_path = File.expand_path('~/.budik/options.yml')

if @options['os'] == 'windows'
system('@powershell -Command "' + options_path + '"')
else
editor = ENV['EDITOR'] ? ENV['EDITOR'] : 'vi'
system(editor + ' "' + options_path + '"')
end
end

def install
Expand All @@ -48,7 +55,8 @@ def installed?
end

def reset
# TODO
options = './config/templates/options/' + platform?.to_s + '.yml'
FileUtils.cp(options, Dir.home + '/.budik/options.yml')
end

def rpi?(os)
Expand Down
6 changes: 3 additions & 3 deletions lib/budik/devices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def initialize
storage_load(options['sources']['download'])
end

attr_reader :storage
attr_reader :tv
attr_accessor :storage, :tv

def storage_load(options)
@storage[:device] = options['device']
Expand Down Expand Up @@ -47,7 +46,8 @@ def storage_mount
end

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

Expand Down
2 changes: 1 addition & 1 deletion lib/budik/player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize
end
end

attr_accessor :player_options
attr_accessor :player, :player_options

def play(source)
if @player == 'omxplayer'
Expand Down
2 changes: 2 additions & 0 deletions lib/budik/rng.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ def initialize
@method = @options['method']
end

attr_accessor :options, :method

def generate(items)
case @method
when 'hwrng'
Expand Down
4 changes: 2 additions & 2 deletions lib/budik/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ def initialize

attr_accessor :sources, :dir, :method

def download(source = nil)
def download_sources(source = nil)
if source
source[:path].each do |path|
download_youtube(YouTubeAddy.extract_video_id(path))
end
else
@sources.each { |src| download(src) }
@sources.each { |src| download_sources(src) }
end
end

Expand Down
202 changes: 201 additions & 1 deletion spec/lib/budik/devices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,206 @@

parsed_cmd = 'udisksctl mount -b /dev/sda1'
expect(devices.storage[:mount_command]).to eq parsed_cmd
expect(devices.storage[:mount]).to eq true
expect(devices.storage[:mount]).to be true
end
end

describe Budik::Devices, '#storage_mount' do
context 'if not mounted' do
it 'calls mount command' do
devices = Budik::Devices.instance
devices.storage[:mounted] = false
mount_command = 'udisksctl mount -b /dev/sda1'

expect(devices).to receive(:system).with(mount_command)
devices.storage_mount
expect(devices.storage[:mounted]).to be true
end
end

context 'if already mounted' do
it "doesn't do anything" do
devices = Budik::Devices.instance
devices.storage[:mounted] = true
mount_command = 'udisksctl mount -b /dev/sda1'

expect(devices).not_to receive(:system).with(mount_command)
devices.storage_mount
expect(devices.storage[:mounted]).to be true
end
end

context 'if mount is disabled' do
it "doesn't do anything" do
devices = Budik::Devices.instance
devices.storage[:mounted] = nil
mount_command = 'udisksctl mount -b /dev/sda1'

expect(devices).not_to receive(:system).with(mount_command)
devices.storage_mount
expect(devices.storage[:mounted]).to be_nil
end
end
end

describe Budik::Devices, '#storage_unmount' do
context 'if mounted' do
it 'calls unmount command' do
devices = Budik::Devices.instance
devices.storage[:unmount] = true
devices.storage[:mounted] = true
unmount_command = 'udisksctl unmount -b /dev/sda1'

expect(devices).to receive(:system).with(unmount_command)
devices.storage_unmount
expect(devices.storage[:mounted]).to be false
end
end

context 'if not mounted' do
it "doesn't do anything" do
devices = Budik::Devices.instance
devices.storage[:unmount] = true
devices.storage[:mounted] = false
unmount_command = 'udisksctl unmount -b /dev/sda1'

expect(devices).not_to receive(:system).with(unmount_command)
devices.storage_unmount
expect(devices.storage[:mounted]).to be false
end
end

context 'if unmount is disabled' do
it "doesn't do anything" do
devices = Budik::Devices.instance
devices.storage[:unmount] = false
unmount_command = 'udisksctl unmount -b /dev/sda1'

expect(devices).not_to receive(:system).with(unmount_command)
devices.storage_unmount
end
end
end

describe Budik::Devices, '#storage_sleep' do
context 'if awake' do
it 'calls sleep command' do
devices = Budik::Devices.instance
devices.storage[:awake] = true
sleep_command = 'sudo hdparm -y /dev/sda'

expect(devices).to receive(:system).with(sleep_command)
devices.storage_sleep
expect(devices.storage[:awake]).to be false
end
end

context 'if already asleep' do
it "doesn't do anything" do
devices = Budik::Devices.instance
devices.storage[:awake] = false
sleep_command = 'sudo hdparm -y /dev/sda'

expect(devices).not_to receive(:system).with(sleep_command)
devices.storage_sleep
expect(devices.storage[:awake]).to be false
end
end

context 'if sleep is disabled' do
it "doesn't do anything" do
devices = Budik::Devices.instance
devices.storage[:awake] = nil
sleep_command = 'sudo hdparm -y /dev/sda'

expect(devices).not_to receive(:system).with(sleep_command)
devices.storage_sleep
expect(devices.storage[:awake]).to be_nil
end
end
end

describe Budik::Devices, '#tv_on' do
context 'if TV is off' do
it 'turns it on' do
devices = Budik::Devices.instance
devices.tv[:on] = false
devices.tv[:wait_secs_after_on] = 0
tv_on_command = 'echo "on 0" | cec-client -s'
tv_as_command = 'echo "as" | cec-client -s'

expect(devices).to receive(:system).with(tv_on_command)
expect(devices).to receive(:system).with(tv_as_command)
devices.tv_on
expect(devices.tv[:on]).to be true
end
end

context 'if TV is on' do
it "doesn't do anything" do
devices = Budik::Devices.instance
devices.tv[:on] = true
devices.tv[:wait_secs_after_on] = 0
tv_on_command = 'echo "on 0" | cec-client -s'
tv_as_command = 'echo "as" | cec-client -s'

expect(devices).not_to receive(:system).with(tv_on_command)
expect(devices).not_to receive(:system).with(tv_as_command)
devices.tv_on
expect(devices.tv[:on]).to be true
end
end

context 'if TV is not available' do
it "doesn't do anything" do
devices = Budik::Devices.instance
devices.tv[:on] = nil
devices.tv[:wait_secs_after_on] = 0
tv_on_command = 'echo "on 0" | cec-client -s'
tv_as_command = 'echo "as" | cec-client -s'

expect(devices).not_to receive(:system).with(tv_on_command)
expect(devices).not_to receive(:system).with(tv_as_command)
devices.tv_on
expect(devices.tv[:on]).to be nil
end
end
end

describe Budik::Devices, '#tv_off' do
context 'if TV is on' do
it 'turns it off' do
devices = Budik::Devices.instance
devices.tv[:on] = true
tv_off_command = 'echo "standby 0" | cec-client -s'

expect(devices).to receive(:system).with(tv_off_command)
devices.tv_off
expect(devices.tv[:on]).to be false
end
end

context 'if TV is off' do
it "doesn't do anything" do
devices = Budik::Devices.instance
devices.tv[:on] = false
tv_off_command = 'echo "standby 0" | cec-client -s'

expect(devices).not_to receive(:system).with(tv_off_command)
devices.tv_off
expect(devices.tv[:on]).to be false
end
end

context 'if TV is not available' do
it "doesn't do anything" do
devices = Budik::Devices.instance
devices.tv[:on] = nil
tv_off_command = 'echo "standby 0" | cec-client -s'

expect(devices).not_to receive(:system).with(tv_off_command)
devices.tv_off
expect(devices.tv[:on]).to be nil
end
end
end
55 changes: 51 additions & 4 deletions spec/lib/budik/player_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
require 'spec_helper'

options = Budik::Config.instance.options['player']
player = Budik::Player.instance

describe Budik::Player, '#omxplayer' do
it 'plays a source using omxplayer' do
player.player = 'omxplayer'
player.player_options = options['omxplayer']
cmd1 = player.omx_build_command('path1')
cmd2 = player.omx_build_command('path2')

expect(Open3).to receive(:popen3).with(cmd1).ordered
expect(Open3).to receive(:popen3).with(cmd2).ordered
player.omxplayer(path: %w(path1 path2))
end
end

describe Budik::Player, '#omx_build_command' do
it 'correctly builds omxplayer command' do
Budik::Config.instance.options['player']['player'] = 'omxplayer'
player = Budik::Player.instance
options['player'] = 'omxplayer'
omx_options = player.player_options
omx_options['path'] = 'omxplayer'
omx_options['default_volume'] = -2100
Expand All @@ -14,10 +29,27 @@
end
end

describe Budik::Player, '#vlc' do
it 'plays a source using vlc' do
player.player = 'vlc'
player.player_options = options['vlc']
player.player_options['wait_secs_after_run'] = 0

source = { path: %w(path1 path2) }
cmd = player.vlc_build_command(source)
player.player_options['path'].gsub!(/^"/, '').gsub!(/"$/, '')

expect(player).to receive(:spawn).with(cmd)
allow(player).to receive(:vlc_volume_control) {}
allow(player).to receive(:vlc_rc_connect) {}
allow(Process).to receive(:wait) {}
player.vlc(source)
end
end

describe Budik::Player, '#vlc_build_command' do
it 'correctly builds vlc command' do
Budik::Config.instance.options['player']['player'] = 'vlc'
player = Budik::Player.instance
options['player'] = 'vlc'
vlc_options = player.player_options
vlc_options['path'] = 'C:/Program Files (x86)/VideoLAN/VLC/vlc.exe'
vlc_options['rc_host'] = 'localhost'
Expand All @@ -32,3 +64,18 @@
expect(player.vlc_build_command(source)).to eq cmd
end
end

describe Budik::Player, '#vlc_volume_control' do
it 'lowers volume and fades back in' do
player.player = 'vlc'
player.player_options = options['vlc']
player.player_options['volume_fadein_secs'] = 0
volume = player.player_options['default_volume'] = 128
step = player.player_options['volume_step'] = 1.0

rc = instance_double('IO', puts: nil)
expect(rc).to receive(:puts).with('volume ' + volume.to_s)
expect(rc).to receive(:puts).with('volup ' + step.to_s).exactly(128).times
player.vlc_volume_control(rc)
end
end
Loading

0 comments on commit 1f61046

Please sign in to comment.