def secs_since_midnight()
now = Time.now
- midnight = Time.new(now.year, now.month, now.day, 0, 0, 0)
+ midnight = Time.new(
+ now.year, now.month, now.day, 0, 0, 0)
(now - midnight)
end
changed
end
+
+class ATVPlayer
+ def initialize(channels)
+ @db = JSON.parse(File.read("index.json"))
+ @now_playing = nil
+ @channel = 0
+ @channels = channels
+ end
+
+ def secs_since_midnight()
+ now = Time.now
+ midnight = Time.new(
+ now.year, now.month, now.day, 0, 0, 0)
+ (now - midnight)
+ end
+
+ def next_show(channel)
+ cfg = @channels[channel]
+ path = cfg[:files][cfg[:curr]]
+ cfg[:curr] += 1
+ if cfg[:curr] >= cfg[:files].length
+ cfg[:files].shuffle
+ cfg[:curr] = 0
+ end
+ (path ? @db[path].merge({"path"=>path}) : nil)
+ end
+
+ def send_program()
+ @now_playing = @channels[@channel][:play]
+ Iodine.publish(:atv,
+ JSON.dump(@now_playing.merge({ "cmd" => "play" })))
+ end
+
+ def program_changed()
+ changed = false
+ time = secs_since_midnight
+ @channels.each_with_index do |chan, i|
+ next if not chan[:play][:curr]
+ end_time = chan[:play][:start_time] + chan[:play][:curr]["duration"]
+ if end_time <= time then
+ changed = true
+ chan[:play][:chan] = i
+ chan[:play][:curr] = chan[:play][:next]
+ chan[:play][:next] = next_show(i)
+ chan[:play][:start_time] = time
+ end
+ end
+ changed
+ end
+
+ def skip()
+ cfg = @channels[@channel]
+ cfg[:play][:start_time] = secs_since_midnight()
+ cfg[:play][:curr] = cfg[:play][:next]
+ cfg[:play][:next] = next_show(@channel)
+ send_program()
+ end
+
+ def play()
+ end
+
+ def pause()
+ end
+
+ def chan_prev()
+ @channel -= 1
+ if @channel < 0
+ @channel = @channels.length - 1
+ end
+ send_program()
+ end
+
+ def chan_next()
+ @channel += 1
+ if @channel >= @channels.length
+ @channel = 0
+ end
+ send_program()
+ end
+end
+
+
module ATV
def on_open(client)
puts "connect: #{client}"