]> git.mdlowis.com Git - proto/atv.git/commitdiff
added save and restor of state of channel
authorMike Lowis <mike.lowis@gentex.com>
Thu, 14 Mar 2024 18:47:19 +0000 (14:47 -0400)
committerMike Lowis <mike.lowis@gentex.com>
Thu, 14 Mar 2024 18:47:19 +0000 (14:47 -0400)
atv/lib/atv/channel.rb
atv/lib/atv/player.rb

index 461b096acf9d49207337dae2cd10b754883b882d..35cfe9d68993f0a528bc76e75c8ff54ae8f7e95e 100644 (file)
@@ -4,15 +4,12 @@ module ATV
       @name = name
       @db = db
       @selectors = selectors
-
       @time = 0
       @index = 0
       @queue = []
       @current = files[@index] if files.length > 0
     end
 
-
-
     def update(playing)
       return false if !@current || items.length == 0
       @time += 1 if playing
@@ -45,6 +42,28 @@ module ATV
       }
     end
 
+    def export_state()
+      {
+        "name"    => @name,
+        "time"    => @time,
+        "index"   => @index,
+        "queue"   => @queue,
+        "current" => @current
+      }
+    end
+
+    def import_state(state)
+      # "name" is ignored so we ensure the config value is used.
+      if @name == state["name"] then
+        @time    = state["time"]
+        @index   = state["index"]
+        @queue   = state["queue"]
+        @current = state["current"]
+      else
+        puts "Rejecting channel state import: #{@name} != #{state["name"]}"
+      end
+    end
+
     def items()
 #      pp filedata[:tree]
       { "items" => filedata[:tree] }
index f1c269f95e4f202b332217395c69f7134a4f91de..d22ad019452b3d7c1b32e83ce76a299d77fcc8d2 100644 (file)
@@ -5,13 +5,23 @@ module ATV
       @channel = 0
       @channels = channels
       @playing = true
+      if File.exist? "/tmp/atv.json"
+        data = JSON.parse(File.read("/tmp/atv.json"))
+        data.each_with_index {|e, i| channels[i].import_state e }
+      end
     end
 
     def update()
+      export_data = []
       updated = false
       @channels.each do |c|
         updated ||= c.update(@playing)
+        export_data << c.export_state
+      end
+      File.open("/tmp/atv.json", "wb") do |f|
+        f.write JSON.dump(export_data)
       end
+      pp export_data
       updated
     end