Class: AlarMPD
- Inherits:
-
Object
- Object
- AlarMPD
- Defined in:
- lib/alarmpd.rb
Defined Under Namespace
Classes: Alarm
Constant Summary collapse
- H_SECS =
60 * 60
- D_SECS =
H_SECS * 24
- TIME_REGEXP =
'([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])'
- DAY_REGEXP =
"(#{Date::DAYNAMES.join('|')})"
Instance Attribute Summary collapse
-
#fade_in ⇒ Object
Returns the value of attribute fade_in.
-
#interval ⇒ Object
Returns the value of attribute interval.
-
#latest_alarm ⇒ Object
readonly
Returns the value of attribute latest_alarm.
-
#mpd ⇒ Object
readonly
Returns the value of attribute mpd.
Class Method Summary collapse
Instance Method Summary collapse
- #alarm_job ⇒ Object
- #connect ⇒ Object
- #fade_in? ⇒ Boolean
- #fade_in_job(job) ⇒ Object
- #fade_interval=(interval) ⇒ Object
-
#initialize(host, port) ⇒ AlarMPD
constructor
A new instance of AlarMPD.
- #next_alarm ⇒ Object
- #refresh ⇒ Object
- #run ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(host, port) ⇒ AlarMPD
Returns a new instance of AlarMPD.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/alarmpd.rb', line 18 def initialize(host, port) @mpd = MPD.new host, port @scheduler = Rufus::Scheduler.new if defined?(RSpec) @scheduler.pause else @mpd.connect end @fade_interval = 10 @interval = 20 @latest_alarm = nil @fade_in = false @alarm_job = nil end |
Instance Attribute Details
#fade_in ⇒ Object
Returns the value of attribute fade_in.
16 17 18 |
# File 'lib/alarmpd.rb', line 16 def fade_in @fade_in end |
#interval ⇒ Object
Returns the value of attribute interval.
16 17 18 |
# File 'lib/alarmpd.rb', line 16 def interval @interval end |
#latest_alarm ⇒ Object (readonly)
Returns the value of attribute latest_alarm.
15 16 17 |
# File 'lib/alarmpd.rb', line 15 def latest_alarm @latest_alarm end |
#mpd ⇒ Object (readonly)
Returns the value of attribute mpd.
15 16 17 |
# File 'lib/alarmpd.rb', line 15 def mpd @mpd end |
Class Method Details
.parse_entry(playlist_entry) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/alarmpd.rb', line 90 def self.parse_entry(playlist_entry) match = playlist_entry.match "^#{DAY_REGEXP} #{TIME_REGEXP}$" if match time = AlarMPD.prepare_time(match[1], match[2].to_i, match[3].to_i) end end |
.prepare_time(day, hour, minute) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/alarmpd.rb', line 97 def self.prepare_time(day, hour, minute) wday = Date::DAYNAMES.index(day) current_wday = Time.now.wday offset = if wday < current_wday 7 - (current_wday - wday) else wday - current_wday end offset_secs = offset * D_SECS + H_SECS * hour + minute * 60 d = Date.today.to_time + offset_secs d = d + D_SECS * 7 if d < Time.now d end |
Instance Method Details
#alarm_job ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/alarmpd.rb', line 50 def alarm_job p = @mpd.playlists.find {|p| p.name == @latest_alarm.name} offset = @mpd.queue.count p.load if fade_in? @mpd.volume = 0 @scheduler.every(@fade_interval, method(:fade_in_job)) end @mpd.play offset end |
#connect ⇒ Object
33 34 35 |
# File 'lib/alarmpd.rb', line 33 def connect @mpd.connect end |
#fade_in? ⇒ Boolean
46 47 48 |
# File 'lib/alarmpd.rb', line 46 def fade_in? @fade_in end |
#fade_in_job(job) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/alarmpd.rb', line 63 def fade_in_job(job) if @mpd.volume == 100 job.unschedule if @mpd.volume == 100 else @mpd.volume = @mpd.volume + 1 end end |
#fade_interval=(interval) ⇒ Object
41 42 43 44 |
# File 'lib/alarmpd.rb', line 41 def fade_interval=(interval) @fade_interval = interval @fade_in = interval != 0 end |
#next_alarm ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/alarmpd.rb', line 80 def next_alarm alarms = [] @mpd.playlists.each do |playlist| time = AlarMPD.parse_entry(playlist.name) alarms << Alarm.new(playlist.name, time) if time end alarms = alarms.sort_by &:time if alarms.any? alarms.first end |
#refresh ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/alarmpd.rb', line 71 def refresh new_alarm = next_alarm if new_alarm && (@latest_alarm.nil? || new_alarm.time != @latest_alarm.time) @latest_alarm = new_alarm @alarm_job.unschedule if @alarm_job @alarm_job = @scheduler.schedule_at(new_alarm.time, method(:alarm_job)) end end |
#run ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'lib/alarmpd.rb', line 114 def run @running = true while @running do refresh sleep @interval end @alarm_job.unschedule if @alarm_job end |
#stop ⇒ Object
37 38 39 |
# File 'lib/alarmpd.rb', line 37 def stop @running = false end |