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 = ()=>{
<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>
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)
module ATV
class Channel
def initialize(name, db, selectors = [])
+ @name = name
@time = 0
@index = 0
@db = db
@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
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
"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
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)
@@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