Class: Himawari::Base
- Inherits:
-
Object
- Object
- Himawari::Base
- Defined in:
- lib/himawari/base.rb
Overview
sets up the methods for Himawari that do not have to do with accessing the internet. ie:
- it sets up all the default and customized params, builds awareness of the locally stored pics
- implements "setting" the background (copies a downloaded image into a different folder)
Direct Known Subclasses
Instance Attribute Summary collapse
-
#app_root ⇒ Object
readonly
Returns the value of attribute app_root.
-
#blacklist_wifi ⇒ Object
readonly
Returns the value of attribute blacklist_wifi.
-
#by_schedule ⇒ Object
readonly
Returns the value of attribute by_schedule.
-
#cron_action ⇒ Object
readonly
Returns the value of attribute cron_action.
-
#data_path ⇒ Object
readonly
Returns the value of attribute data_path.
-
#destination_path ⇒ Object
readonly
Returns the value of attribute destination_path.
-
#focus ⇒ Object
readonly
Returns the value of attribute focus.
-
#latest_local ⇒ Object
readonly
Returns the value of attribute latest_local.
-
#latest_remote ⇒ Object
readonly
Returns the value of attribute latest_remote.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#now ⇒ Object
readonly
Returns the value of attribute now.
-
#resolution ⇒ Object
readonly
Returns the value of attribute resolution.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
-
#work_path ⇒ Object
readonly
Returns the value of attribute work_path.
Instance Method Summary collapse
-
#background(img) ⇒ Object
sets sleeps for a bit, and then copies a selected picture to the ‘destination_path` from where the OS can use it to set a background pic.
-
#crontab(action = nil) ⇒ Object
public method exposing setting/clearing the crontab.
-
#initialize(params = {}) ⇒ Base
constructor
A new instance of Base.
-
#params_valid? ⇒ true, false
verifies the required params that user might have messed up.
-
#up_to_date? ⇒ true, false
Whether our local pics are up to date w/ himawari website.
-
#update_backgrnd ⇒ Object
useful for the “cycle through our downloaded pics and pic one for background” mode checks that we have enough pictures for a whole :day of rotation, and calls the ‘background` method with the appropriate picture’s path based on current timestamp.
Constructor Details
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/himawari/base.rb', line 28 def initialize(params = {}) init_paths(params[:workdir]) @destination_path = params[:destination] @now = Time.now.utc @focus = params[:focus] || :top @mode = params[:mode] || :day @resolution = params[:resolution] || 2 @latest_local = find_latest_local @latest_remote = nil @verbose = params[:verbose] @by_schedule = params[:by_schedule] @blacklist_wifi = params[:blacklist] || [] @cron_action = params[:cron] end |
Instance Attribute Details
#app_root ⇒ Object (readonly)
Returns the value of attribute app_root.
25 26 27 |
# File 'lib/himawari/base.rb', line 25 def app_root @app_root end |
#blacklist_wifi ⇒ Object (readonly)
Returns the value of attribute blacklist_wifi.
25 26 27 |
# File 'lib/himawari/base.rb', line 25 def blacklist_wifi @blacklist_wifi end |
#by_schedule ⇒ Object (readonly)
Returns the value of attribute by_schedule.
25 26 27 |
# File 'lib/himawari/base.rb', line 25 def by_schedule @by_schedule end |
#cron_action ⇒ Object (readonly)
Returns the value of attribute cron_action.
25 26 27 |
# File 'lib/himawari/base.rb', line 25 def cron_action @cron_action end |
#data_path ⇒ Object (readonly)
Returns the value of attribute data_path.
25 26 27 |
# File 'lib/himawari/base.rb', line 25 def data_path @data_path end |
#destination_path ⇒ Object (readonly)
Returns the value of attribute destination_path.
25 26 27 |
# File 'lib/himawari/base.rb', line 25 def destination_path @destination_path end |
#focus ⇒ Object (readonly)
Returns the value of attribute focus.
25 26 27 |
# File 'lib/himawari/base.rb', line 25 def focus @focus end |
#latest_local ⇒ Object (readonly)
Returns the value of attribute latest_local.
25 26 27 |
# File 'lib/himawari/base.rb', line 25 def latest_local @latest_local end |
#latest_remote ⇒ Object (readonly)
Returns the value of attribute latest_remote.
25 26 27 |
# File 'lib/himawari/base.rb', line 25 def latest_remote @latest_remote end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
25 26 27 |
# File 'lib/himawari/base.rb', line 25 def mode @mode end |
#now ⇒ Object (readonly)
Returns the value of attribute now.
25 26 27 |
# File 'lib/himawari/base.rb', line 25 def now @now end |
#resolution ⇒ Object (readonly)
Returns the value of attribute resolution.
25 26 27 |
# File 'lib/himawari/base.rb', line 25 def resolution @resolution end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
25 26 27 |
# File 'lib/himawari/base.rb', line 25 def verbose @verbose end |
#work_path ⇒ Object (readonly)
Returns the value of attribute work_path.
25 26 27 |
# File 'lib/himawari/base.rb', line 25 def work_path @work_path end |
Instance Method Details
#background(img) ⇒ Object
sets sleeps for a bit, and then copies a selected picture to the ‘destination_path` from where the OS can use it to set a background pic
57 58 59 60 61 62 63 64 |
# File 'lib/himawari/base.rb', line 57 def background(img) return false unless img && params_valid? cmd = "sleep 5 ; rm -f #{destination_path}/*.png ; cp #{img} #{destination_path}" # "osascript -e 'tell application \"System Events\" to tell DESKTOP to set picture to \"#{img}\"'" puts cmd if verbose system(cmd) end |
#crontab(action = nil) ⇒ Object
public method exposing setting/clearing the crontab
84 85 86 87 88 89 90 91 92 |
# File 'lib/himawari/base.rb', line 84 def crontab(action = nil) @cron_action = action if action return false unless cron_action && params_valid? cmd = '* * * * * PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin himawari ' \ "-s -m #{mode} -f #{focus} -r #{resolution} -d '#{destination_path}' -w '#{work_path}' -b #{blacklist_wifi.join(',')}" OsUtils.crontab(cmd, cron_action) cmd end |
#params_valid? ⇒ true, false
verifies the required params that user might have messed up.
96 97 98 99 100 101 102 103 104 |
# File 'lib/himawari/base.rb', line 96 def params_valid? unless %i[full top mid low].include?(focus) && %i[live day].include?(mode) && [2, 4, 8, 16, 20].include?(resolution) && blacklist_wifi.is_a?(Array) && (!destination_path || (destination_path && File.directory?(destination_path) && destination_path != '/')) puts 'Invalid params. Please double check them.' return false end true end |
#up_to_date? ⇒ true, false
Returns whether our local pics are up to date w/ himawari website.
45 46 47 48 49 50 51 52 |
# File 'lib/himawari/base.rb', line 45 def up_to_date? puts "Latest local: #{latest_local[:timestamp]}" if verbose if now - latest_local[:timestamp] < 10 * 60 puts 'Local pic is up to date; no need to go online.' if verbose return true end false end |
#update_backgrnd ⇒ Object
useful for the “cycle through our downloaded pics and pic one for background” mode checks that we have enough pictures for a whole :day of rotation, and calls the ‘background` method with the appropriate picture’s path based on current timestamp
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/himawari/base.rb', line 69 def update_backgrnd return false unless mode == :day && params_valid? # only rotate the background if we have one complete rotation of the globe saved sexy_pics = full_24hrs_available # puts sexy_pics return false unless sexy_pics i = (now.hour * 60 + now.min) / UPDATE_RATE % sexy_pics.count puts "#{i} :: #{now.hour * 60 + now.min} % #{sexy_pics.count} switching to #{sexy_pics[i]}" if verbose background(sexy_pics[i]) end |