From 64925b492778a296591abdcb02e8b70495843ffc Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sun, 24 Dec 2023 22:26:52 -0500 Subject: [PATCH] started breaking up ruby logic into classes and modules --- client.js | 2 +- serve-videos | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/client.js b/client.js index d6589d5..ce9bc18 100644 --- a/client.js +++ b/client.js @@ -12,7 +12,7 @@ const Client = ((self = {})=>{ }; ws.onclose = ()=>{ - self.connect(); + self.connect(onmsg); }; }; diff --git a/serve-videos b/serve-videos index e576a09..1b2f70d 100755 --- a/serve-videos +++ b/serve-videos @@ -19,7 +19,8 @@ $channels = [ 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 @@ -74,6 +75,88 @@ def program_changed() 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}" -- 2.55.0