class Himawari::Base

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)

Attributes

app_root[R]
blacklist_wifi[R]
by_schedule[R]
cron_action[R]
data_path[R]
destination_path[R]
focus[R]
latest_local[R]
latest_remote[R]
mode[R]
now[R]
resolution[R]
verbose[R]
work_path[R]

Public Class Methods

new(params = {}) click to toggle source
# 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

Public Instance Methods

background(img) click to toggle source

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 @param img [String] full path to the picture

# 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) click to toggle source

public method exposing setting/clearing the crontab @param action [Symbol] should we set the cron, or clear it?

# 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?() click to toggle source

verifies the required params that user might have messed up. @return [true, false] depending on the validity of the parameters supplied

# 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?() click to toggle source

@return [true, false] whether our local pics are up to date w/ himawari website

# 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() click to toggle source

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

# 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

Private Instance Methods

find_latest_local() click to toggle source

scans the `workdir` and @return [DateTime] of the most recent picture saved in `workdir`

# File lib/himawari/base.rb, line 141
def find_latest_local
  # we can't select by timestamp, because those could be messed up. Instead, just search for "latest" by filename
  # latest_pic = `ls -t #{data_path}/h_* | head -1`
  twodays_ago = now - 86_400 * 2
  latest = { timestamp: nil }
  failsafe = { filename: nil, timestamp: Time.new(twodays_ago.year, twodays_ago.month, twodays_ago.day, 0, 0, 0, '+00:00') }

  Dir["#{data_path}/h_*.png"].each do |pic|
    stamp = Time.parse("#{pic[0..-5].insert(-3, ':')}:00+00:00")
    `touch -t #{stamp.strftime('%Y%m%d%H%M.%S')} #{pic}`
    latest = { filename: pic, timestamp: stamp } if !latest[:timestamp] || latest[:timestamp] < stamp
  end

  latest[:timestamp] && latest[:timestamp] > twodays_ago ? latest : failsafe
end
full_24hrs_available() click to toggle source

@return [Array <String>] of pic_names to choose a `background` from, or

false if we do not have enough pictures for a full 24hr rotation
# File lib/himawari/base.rb, line 110
def full_24hrs_available
  prev_pic = ''
  sexy_pics = []

  Dir["#{data_path}/h_*.png"].sort { |x, y| y <=> x }.each do |pic|
    pic_time = pic[-8, 4]
    return sexy_pics if sexy_pics.count.positive? && pic_time < LOCAL_MIDNIGHT && LOCAL_MIDNIGHT <= prev_pic

    sexy_pics.unshift(pic) if sexy_pics.count.positive? || pic_time < LOCAL_MIDNIGHT && LOCAL_MIDNIGHT <= prev_pic
    prev_pic = pic_time
  end
  false
end
init_paths(workdir) click to toggle source

relative to the location of the script: Pathname.new(File.expand_path('../', __FILE__)) relative to the current working dir: Dir.pwd sets the paths for the script @param workdir [String] is taken from the param that the user supplied (or defaults to pwd) (it's where we want to save the images from himawari) the directory will be created if it doesn't exist yet

# File lib/himawari/base.rb, line 130
def init_paths(workdir)
  @app_root = workdir && File.directory?(workdir) ? workdir : Dir.pwd
  @work_path = @app_root
  @data_path = "#{@app_root}/data"

  @app_root = Pathname.new(File.expand_path(__dir__))
  Dir.mkdir(@data_path) unless File.exist?(@data_path)
end