]> git.mdlowis.com Git - proto/atv.git/commitdiff
started breaking up ruby logic into classes and modules
authorMichael D. Lowis <mike@mdlowis.com>
Mon, 25 Dec 2023 03:26:52 +0000 (22:26 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Mon, 25 Dec 2023 03:26:52 +0000 (22:26 -0500)
client.js
serve-videos

index d6589d5a6e9d4bcbe4f85df40a7b91210e36f99c..ce9bc18442419e39001f91917cca1928806a9697 100644 (file)
--- a/client.js
+++ b/client.js
@@ -12,7 +12,7 @@ const Client = ((self = {})=>{
         };
 
         ws.onclose = ()=>{
-            self.connect();
+            self.connect(onmsg);
         };
     };
 
index e576a09d3947d4c6d9f5c83077f00c1b2bdc6b84..1b2f70d3de43e1c323d0e44eb503da74254b869b 100755 (executable)
@@ -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}"