-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.rb
87 lines (68 loc) · 1.83 KB
/
common.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require_relative 'lirc_handler'
#require 'metar'
class Common
# event channels
attr_reader :events
# cache
attr_reader :playlist, :mpd_status
# connections
attr_reader :mpd, :lirc
attr_accessor :denon
def initialize
@events = Struct.new(:mpd_song, :mpd_status, :mpd_volume, :denon_status, :lcd_backlight, :lcd_status, :lcd_alerts, :actions).new
@events.mpd_status = EM::Channel.new
@events.mpd_volume = EM::Channel.new
@events.denon_status = EM::Channel.new
@events.lcd_backlight = EM::Channel.new
@events.lcd_status = EM::Channel.new
@events.lcd_alerts = EM::Channel.new
@events.actions = EM::Channel.new
@events.mpd_status.subscribe do |status|
@mpd_status = status
update_playlist
end
@events.mpd_volume.subscribe do |vol|
puts "vol #{vol}"
denon.volume = vol/2
end
@mpd = MPD.new
@mpd.connect
puts "Connected to MPD"
@mpd.async_idle
@lirc = EventMachine.connect_unix_domain "/var/run/lirc/lircd", LIRCHandler, self
update_status
update_playlist
end
def update_playlist
if @playlist_version != @mpd_status[:playlist]
start = Time.now
puts "Updating playlist #{@playlist_version} -> #{@mpd_status[:playlist]}"
@mpd.noidle_sync do |mpd|
@playlist = mpd.queue.to_a
end
@playlist_version = @mpd_status[:playlist]
puts "Pls updated. Took #{Time.now - start} s"
end
end
def update_status
@mpd.noidle_sync do |mpd|
@mpd_status = @mpd.status
end
end
def mpd_song
if @mpd_status[:song]
@playlist[@mpd_status[:song]]
else
nil
end
end
def lcd_backlight=(value)
@events.lcd_backlight.push value
end
def lcd_touch
@events.lcd_backlight.push nil
end
def lcd_status text
@events.lcd_status.push text
end
end