Class: Himawari::Base

Inherits:
Object
  • Object
show all
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

Download

Instance Attribute Summary collapse

Instance Method Summary collapse

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_rootObject (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_wifiObject (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_scheduleObject (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_actionObject (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_pathObject (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_pathObject (readonly)

Returns the value of attribute destination_path.



25
26
27
# File 'lib/himawari/base.rb', line 25

def destination_path
  @destination_path
end

#focusObject (readonly)

Returns the value of attribute focus.



25
26
27
# File 'lib/himawari/base.rb', line 25

def focus
  @focus
end

#latest_localObject (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_remoteObject (readonly)

Returns the value of attribute latest_remote.



25
26
27
# File 'lib/himawari/base.rb', line 25

def latest_remote
  @latest_remote
end

#modeObject (readonly)

Returns the value of attribute mode.



25
26
27
# File 'lib/himawari/base.rb', line 25

def mode
  @mode
end

#nowObject (readonly)

Returns the value of attribute now.



25
26
27
# File 'lib/himawari/base.rb', line 25

def now
  @now
end

#resolutionObject (readonly)

Returns the value of attribute resolution.



25
26
27
# File 'lib/himawari/base.rb', line 25

def resolution
  @resolution
end

#verboseObject (readonly)

Returns the value of attribute verbose.



25
26
27
# File 'lib/himawari/base.rb', line 25

def verbose
  @verbose
end

#work_pathObject (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

Parameters:

  • img (String)

    full path to the picture



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

Parameters:

  • action (Symbol) (defaults to: nil)

    should we set the cron, or clear it?



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.

Returns:

  • (true, false)

    depending on the validity of the parameters supplied



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.

Returns:

  • (true, false)

    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_backgrndObject

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