From fdd09c12376d302a8ffc1909f350052d5a57c866 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Tue, 10 Dec 2013 15:15:45 +0000 Subject: [PATCH] Refactor MPDConnection into a normal class. --- assets/js/constructs/events.js | 1 + models/mpd_connection.rb | 42 +++++++++++++++++++++++++++----- models/song.rb | 7 ------ mpd_client.rb | 44 ++++++++++++++++++++-------------- 4 files changed, 63 insertions(+), 31 deletions(-) diff --git a/assets/js/constructs/events.js b/assets/js/constructs/events.js index c164285..43429e8 100644 --- a/assets/js/constructs/events.js +++ b/assets/js/constructs/events.js @@ -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); diff --git a/models/mpd_connection.rb b/models/mpd_connection.rb index b1f37fb..1805cad 100644 --- a/models/mpd_connection.rb +++ b/models/mpd_connection.rb @@ -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 diff --git a/models/song.rb b/models/song.rb index 331a8c3..d491ccc 100644 --- a/models/song.rb +++ b/models/song.rb @@ -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 diff --git a/mpd_client.rb b/mpd_client.rb index bc16042..415ec77 100644 --- a/mpd_client.rb +++ b/mpd_client.rb @@ -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