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) {
|
this.events.onmessage = function(e) {
|
||||||
response = JSON.parse(e.data);
|
response = JSON.parse(e.data);
|
||||||
|
console.log(response)
|
||||||
switch (response.type) {
|
switch (response.type) {
|
||||||
case 'status':
|
case 'status':
|
||||||
status.attr(response.data, true);
|
status.attr(response.data, true);
|
||||||
|
|||||||
@ -1,11 +1,41 @@
|
|||||||
class MPDConnection
|
class MPDConnection < SimpleDelegator
|
||||||
def self.mpd
|
|
||||||
@mpd ||= MPD.new('localhost', 6600, { callbacks: true })
|
attr_accessor :connections
|
||||||
|
|
||||||
|
def initialize(host: 'localhost', port: 6600)
|
||||||
|
@mpd = MPD.new(host, port, { callbacks: true })
|
||||||
@mpd.connect unless @mpd.connected?
|
@mpd.connect unless @mpd.connected?
|
||||||
@mpd
|
|
||||||
|
@connections = []
|
||||||
|
super(@mpd)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.status
|
def queue
|
||||||
self.mpd.status
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
require './models/mpd_connection'
|
|
||||||
|
|
||||||
class Song < Struct.new(:id, :artist, :album, :title, :length, :pos, :playing)
|
class Song < Struct.new(:id, :artist, :album, :title, :length, :pos, :playing)
|
||||||
|
|
||||||
def initialize(song, pos: nil, playing: false)
|
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.pos = pos
|
||||||
self.playing = playing
|
self.playing = playing
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@ -8,11 +8,11 @@ require 'sass'
|
|||||||
require 'json'
|
require 'json'
|
||||||
require 'cgi'
|
require 'cgi'
|
||||||
|
|
||||||
require './models/mpd_connection'
|
|
||||||
require './models/control'
|
require './models/control'
|
||||||
require './models/album'
|
require './models/album'
|
||||||
require './models/artist'
|
require './models/artist'
|
||||||
require './models/song'
|
require './models/song'
|
||||||
|
require './models/mpd_connection'
|
||||||
|
|
||||||
class MPDClient < Sinatra::Base
|
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_precompile, %w(app.js app.css *.png *.jpg *.svg *.eot *.ttf *.woff)
|
||||||
set :assets_prefix, 'assets'
|
set :assets_prefix, 'assets'
|
||||||
|
|
||||||
|
set mpd: MPDConnection.new
|
||||||
|
|
||||||
register Sinatra::AssetPipeline
|
register Sinatra::AssetPipeline
|
||||||
|
|
||||||
register Sinatra::Namespace
|
register Sinatra::Namespace
|
||||||
@ -28,30 +31,36 @@ class MPDClient < Sinatra::Base
|
|||||||
erb :index
|
erb :index
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.send_status
|
def send_status
|
||||||
response = JSON({ type: 'status', data: MPDConnection.status })
|
puts "Sending status"
|
||||||
|
response = JSON({ type: 'status', data: status })
|
||||||
settings.connections.each { |out| out << "data: #{response}\n\n" }
|
settings.connections.each { |out| out << "data: #{response}\n\n" }
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.send_queue
|
def send_queue
|
||||||
response = JSON({ type: 'queue', data: Song.queue.map(&:to_h) })
|
puts "Sending queue"
|
||||||
|
response = JSON({ type: 'queue', data: queue })
|
||||||
settings.connections.each { |out| out << "data: #{response}\n\n" }
|
settings.connections.each { |out| out << "data: #{response}\n\n" }
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.send_time(elapsed, total)
|
def send_time(elapsed, total)
|
||||||
|
puts "Sending time"
|
||||||
response = JSON({ type: 'time', data: [elapsed, total] })
|
response = JSON({ type: 'time', data: [elapsed, total] })
|
||||||
settings.connections.each { |out| out << "data: #{response}\n\n" }
|
settings.connections.each { |out| out << "data: #{response}\n\n" }
|
||||||
end
|
end
|
||||||
|
|
||||||
MPDConnection.mpd.on(:song) { |song| send_status }
|
puts "Registering callbacks"
|
||||||
MPDConnection.mpd.on(:state) { |state| send_status }
|
puts settings.mpd
|
||||||
MPDConnection.mpd.on(:playlist) { |playlist| send_queue }
|
settings.mpd.on(:song) { |song| send_status }
|
||||||
MPDConnection.mpd.on(:time) { |elapsed, total| send_time(elapsed, total) }
|
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
|
namespace '/api' do
|
||||||
|
|
||||||
get '/status' do
|
get '/status' do
|
||||||
JSON MPDConnection.status
|
puts settings.mpd.object_id
|
||||||
|
JSON settings.mpd.status
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/stream', provides: 'text/event-stream' do
|
get '/stream', provides: 'text/event-stream' do
|
||||||
@ -86,24 +95,23 @@ class MPDClient < Sinatra::Base
|
|||||||
|
|
||||||
get '/queue' do
|
get '/queue' do
|
||||||
content_type 'application/json'
|
content_type 'application/json'
|
||||||
JSON({ data: Song.queue.map(&:to_h) })
|
JSON({ data: settings.mpd.queue })
|
||||||
end
|
end
|
||||||
|
|
||||||
put '/control/play' do
|
put '/control/play' do
|
||||||
Control.play(params[:pos])
|
settings.mpd.play(params[:pos])
|
||||||
end
|
end
|
||||||
|
|
||||||
put '/control/:action' do
|
put '/control/:action' do
|
||||||
if Control.controls.include?(params[:action].to_sym)
|
unless settings.mpd.command(params[:action].to_sym)
|
||||||
Control.send(params[:action])
|
|
||||||
else
|
|
||||||
not_found
|
not_found
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
put '/control/volume/:value' do
|
put '/control/volume/:value' do
|
||||||
content_type 'application/json'
|
unless settings.mpd.volume(params[:value].to_i)
|
||||||
Control.volume(params[:value])
|
status 422
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user