mirror of
https://github.com/danbee/mpd-client
synced 2025-03-04 08:39:09 +00:00
Refactor MPDConnection into a normal class.
This commit is contained in:
parent
4263425f98
commit
fdd09c1237
@ -7,6 +7,7 @@ var Events = can.Construct.extend({
|
||||
|
||||
this.events.onmessage = function(e) {
|
||||
response = JSON.parse(e.data);
|
||||
console.log(response)
|
||||
switch (response.type) {
|
||||
case 'status':
|
||||
status.attr(response.data, true);
|
||||
|
||||
@ -1,11 +1,41 @@
|
||||
class MPDConnection
|
||||
def self.mpd
|
||||
@mpd ||= MPD.new('localhost', 6600, { callbacks: true })
|
||||
class MPDConnection < SimpleDelegator
|
||||
|
||||
attr_accessor :connections
|
||||
|
||||
def initialize(host: 'localhost', port: 6600)
|
||||
@mpd = MPD.new(host, port, { callbacks: true })
|
||||
@mpd.connect unless @mpd.connected?
|
||||
@mpd
|
||||
|
||||
@connections = []
|
||||
super(@mpd)
|
||||
end
|
||||
|
||||
def self.status
|
||||
self.mpd.status
|
||||
def queue
|
||||
current_song = status[:songid]
|
||||
super.map { |song| Song.new(song, playing: (song.id == current_song), pos: song.pos).to_h }
|
||||
end
|
||||
|
||||
def pause
|
||||
self.pause = !self.paused?
|
||||
true
|
||||
end
|
||||
|
||||
def volume(vol)
|
||||
if vol.between?(0, 100)
|
||||
self.volume = vol
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
def command(command)
|
||||
if controls.include?(command)
|
||||
send(command)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def controls
|
||||
[:play, :stop, :next, :previous, :pause]
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
require './models/mpd_connection'
|
||||
|
||||
class Song < Struct.new(:id, :artist, :album, :title, :length, :pos, :playing)
|
||||
|
||||
def initialize(song, pos: nil, playing: false)
|
||||
@ -11,9 +9,4 @@ class Song < Struct.new(:id, :artist, :album, :title, :length, :pos, :playing)
|
||||
self.pos = pos
|
||||
self.playing = playing
|
||||
end
|
||||
|
||||
def self.queue
|
||||
current_song = MPDConnection.mpd.status[:songid]
|
||||
MPDConnection.mpd.queue.map { |song| self.new(song, playing: (song.id == current_song), pos: song.pos) }
|
||||
end
|
||||
end
|
||||
|
||||
@ -8,11 +8,11 @@ require 'sass'
|
||||
require 'json'
|
||||
require 'cgi'
|
||||
|
||||
require './models/mpd_connection'
|
||||
require './models/control'
|
||||
require './models/album'
|
||||
require './models/artist'
|
||||
require './models/song'
|
||||
require './models/mpd_connection'
|
||||
|
||||
class MPDClient < Sinatra::Base
|
||||
|
||||
@ -20,6 +20,9 @@ class MPDClient < Sinatra::Base
|
||||
|
||||
set :assets_precompile, %w(app.js app.css *.png *.jpg *.svg *.eot *.ttf *.woff)
|
||||
set :assets_prefix, 'assets'
|
||||
|
||||
set mpd: MPDConnection.new
|
||||
|
||||
register Sinatra::AssetPipeline
|
||||
|
||||
register Sinatra::Namespace
|
||||
@ -28,30 +31,36 @@ class MPDClient < Sinatra::Base
|
||||
erb :index
|
||||
end
|
||||
|
||||
def self.send_status
|
||||
response = JSON({ type: 'status', data: MPDConnection.status })
|
||||
def send_status
|
||||
puts "Sending status"
|
||||
response = JSON({ type: 'status', data: status })
|
||||
settings.connections.each { |out| out << "data: #{response}\n\n" }
|
||||
end
|
||||
|
||||
def self.send_queue
|
||||
response = JSON({ type: 'queue', data: Song.queue.map(&:to_h) })
|
||||
def send_queue
|
||||
puts "Sending queue"
|
||||
response = JSON({ type: 'queue', data: queue })
|
||||
settings.connections.each { |out| out << "data: #{response}\n\n" }
|
||||
end
|
||||
|
||||
def self.send_time(elapsed, total)
|
||||
def send_time(elapsed, total)
|
||||
puts "Sending time"
|
||||
response = JSON({ type: 'time', data: [elapsed, total] })
|
||||
settings.connections.each { |out| out << "data: #{response}\n\n" }
|
||||
end
|
||||
|
||||
MPDConnection.mpd.on(:song) { |song| send_status }
|
||||
MPDConnection.mpd.on(:state) { |state| send_status }
|
||||
MPDConnection.mpd.on(:playlist) { |playlist| send_queue }
|
||||
MPDConnection.mpd.on(:time) { |elapsed, total| send_time(elapsed, total) }
|
||||
puts "Registering callbacks"
|
||||
puts settings.mpd
|
||||
settings.mpd.on(:song) { |song| send_status }
|
||||
settings.mpd.on(:state) { |state| send_status }
|
||||
settings.mpd.on(:playlist) { |playlist| send_queue }
|
||||
settings.mpd.on(:time) { |elapsed, total| send_time(elapsed, total) }
|
||||
|
||||
namespace '/api' do
|
||||
|
||||
get '/status' do
|
||||
JSON MPDConnection.status
|
||||
puts settings.mpd.object_id
|
||||
JSON settings.mpd.status
|
||||
end
|
||||
|
||||
get '/stream', provides: 'text/event-stream' do
|
||||
@ -86,24 +95,23 @@ class MPDClient < Sinatra::Base
|
||||
|
||||
get '/queue' do
|
||||
content_type 'application/json'
|
||||
JSON({ data: Song.queue.map(&:to_h) })
|
||||
JSON({ data: settings.mpd.queue })
|
||||
end
|
||||
|
||||
put '/control/play' do
|
||||
Control.play(params[:pos])
|
||||
settings.mpd.play(params[:pos])
|
||||
end
|
||||
|
||||
put '/control/:action' do
|
||||
if Control.controls.include?(params[:action].to_sym)
|
||||
Control.send(params[:action])
|
||||
else
|
||||
unless settings.mpd.command(params[:action].to_sym)
|
||||
not_found
|
||||
end
|
||||
end
|
||||
|
||||
put '/control/volume/:value' do
|
||||
content_type 'application/json'
|
||||
Control.volume(params[:value])
|
||||
unless settings.mpd.volume(params[:value].to_i)
|
||||
status 422
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user