]> git.mdlowis.com Git - proto/atv.git/commitdiff
added scaffolding for queue management
authorMike Lowis <mike.lowis@gentex.com>
Tue, 16 Jan 2024 21:11:19 +0000 (16:11 -0500)
committerMike Lowis <mike.lowis@gentex.com>
Tue, 16 Jan 2024 21:11:19 +0000 (16:11 -0500)
atv/assets/client.js
atv/assets/control.html
atv/bin/atv
atv/lib/atv/channel.rb
atv/lib/atv/player.rb
atv/lib/atv/server.rb

index 66ebb7b14ef6c0c43bd8eea4801aca5713058490..b4844fbd6ccf796182d4a7396935edc18894a863 100644 (file)
@@ -8,9 +8,17 @@ const Client = ((self = {})=>{
         ws = new WebSocket(
             "ws://" + window.location.host + ":3000");
 
+        ws.onopen = (event)=>{
+          send("get_items", {});
+        }
+
         ws.onmessage = (event)=>{
-            self.state = JSON.parse(event.data);
-            self.onmessage(self.state);
+            cmd = JSON.parse(event.data);
+            if (cmd.cmd !== "items")
+            {
+                self.state = cmd;
+            }
+            self.onmessage(cmd);
         };
 
         ws.onclose = ()=>{
index c4a2f5d68764cd8dea39cae75f83beabb8d99de8..e4deb3c4b8ebc583658cc9d137e616f8f1596d0c 100644 (file)
   <input type="button" value="Skip" onclick="javscript:Client.skip()"/>
   <input type="button" value="Chan +" onclick="javscript:Client.chan_next()"/>
   <input type="button" value="Chan -" onclick="javscript:Client.chan_prev()"/>
+  <input type="button" value="Queue" onclick="javscript:Client.chan_prev()"/>
 </div>
 <script type="text/javascript" src="client.js"></script>
 <script>
 (()=>{
     Client.connect((data)=>{
         console.log(data);
-        currVid.innerText = data["curr"]["path"]
-        nextVid.innerText = data["next"]["path"]
-        playBtn.value = (data["playing"] ? "Pause" : "Play");
+        if (cmd.cmd !== "items")
+        {
+            currVid.innerText = data["curr"]["path"]
+            nextVid.innerText = data["next"]["path"]
+            playBtn.value = (data["playing"] ? "Pause" : "Play");
+        }
+        else
+        {
+            console.log("received items");
+            console.log(data);
+        }
     });
 })();
 </script>
index 008d8c82eee4cb1491a99d71177e6bfabe836fd9..26aa8a42a1924aff8b49977a7c72545392af24c4 100755 (executable)
@@ -20,7 +20,9 @@ end
 db = ATV::Database.new(ATV_ROOT)
 channels = [
   ATV::Channel.new('Everything', db,
-    ["Movies", "Shorts", "Shows"])
+    ["Movies", "Shorts", "Shows"]),
+  ATV::Channel.new('Christmas', db,
+    ["Christmas"]),
 ]
 player = ATV::Player.new(db, channels)
 ATV::Server.start(player)
index 1f007813fd708a748b375267ff02477ce7fb1718..ef618fffe4b0b9f7da26417e28de196daec06190 100644 (file)
@@ -1,6 +1,7 @@
 module ATV
   class Channel
     def initialize(name, db, selectors = [])
+      @name = name
       @time = 0
       @index = 0
       @db = db
@@ -8,15 +9,17 @@ module ATV
       @items = selectors.map do |sel|
         files.select {|f| f["path"].start_with? sel }
       end.flatten.shuffle
+      @item_map = @items.map{|e| [e["path"], e]}.to_h
+      @queue = []
+      @current = @items[@index] if @items.length > 0
     end
 
     def update(playing)
-      return false if @items.length == 0
+      return false if !@current || @items.length == 0
       @time += 1 if playing
-      item = @items[@index]
-      if @time >= item["duration"] then
+      if @time >= @current["duration"] then
         next_vid()
-        pp @items[@index]
+        pp @current
         true
       else
         false
@@ -24,20 +27,35 @@ module ATV
     end
 
     def next_vid()
-      @index += 1
-      if @index >= @items.length
-        @index = 0
-      end
       @time = 0
+      if @queue.length > 0
+        @current = @queue.shift
+      else
+        @index = ((@index+1) >= @items.length ? 0 : (@index+1))
+        @current = @items[@index]
+      end
     end
 
     def state()
        next_index = ((@index+1) >= @items.length ? 0 : (@index+1))
+       next_item = (@queue.length > 0 ? @queue.first : @items[next_index])
       {
         "time" => @time,
-        "curr" => @items[@index],
-        "next" => @items[next_index]
+        "curr" => @current,
+        "next" => next_item
       }
     end
+
+    def items()
+      { "items" => @items }
+    end
+
+    def queue()
+      @queue
+    end
+
+    def enqueue(path)
+      @queue << @item_map[path] if @item_map[path]
+    end
   end
 end
index feb937a5faab006466dc48b97c74f82d91d3eac0..f1c269f95e4f202b332217395c69f7134a4f91de 100644 (file)
@@ -46,5 +46,17 @@ module ATV
           "playing" => @playing
       })
     end
+
+    def items()
+      @channels[@channel].items
+    end
+
+    def queue()
+      @channels[@channel].queue
+    end
+
+    def enqueue(item)
+      @channels[@channel].enqueue(item)
+    end
   end
 end
index 86f672056aa790cf312f3b1414f24df3dc112ac1..d8b771479a6a7e24634a1aad7291ab590e1b7675 100644 (file)
@@ -30,15 +30,17 @@ module ATV
       Iodine.publish(:atv, JSON.dump(obj))
     end
 
-    def client_send(client, obj)
-      puts "To #{client} #{JSON.dump(obj)}"
-      client.write JSON.dump(obj)
+    def client_send(client, cmd, obj)
+      obj = obj.merge({"cmd" => cmd})
+      json = JSON.dump(obj)
+      puts "To #{client} #{json}"
+      client.write json
     end
 
     def on_open(client)
       puts "connect: #{client}"
       client.subscribe :atv
-      client_send(client, @@player.state.merge({"cmd" => "play"}))
+      client_send(client, "play", @@player.state)
     end
 
     def on_close(client)
@@ -72,45 +74,9 @@ module ATV
       @@player.chan_prev
     end
 
-#    def send(client, data)
-#      client.write JSON.dump(data)
-#    end
-#
-#    Cmds = {
-#      "skip" => lambda do |data|
-#        cfg = $channels[$channel]
-#        cfg[:play][:start_time] = secs_since_midnight()
-#        cfg[:play][:curr] = cfg[:play][:next]
-#        cfg[:play][:next] = next_show($channel)
-#        update_program()
-#      end,
-#
-#      "play" => lambda do |data|
-#        pp data
-#      end,
-#
-#      "pause" => lambda do |data|
-#        pp data
-#      end,
-#
-#      "chan_prev" => lambda do |data|
-#        $channel -= 1
-#        if $channel < 0
-#          $channel = $channels.length - 1
-#        end
-#        puts $channel
-#        update_program()
-#      end,
-#
-#      "chan_next" => lambda do |data|
-#        $channel += 1
-#        if $channel >= $channels.length
-#          $channel = 0
-#        end
-#        puts $channel
-#        update_program()
-#      end,
-#    }
+    def on_get_items(client, data)
+      client_send(client, "items", @@player.items)
+    end
 
     extend self
   end