255 lines
7.4 KiB
C#
255 lines
7.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using KaraokePlayer.Enums;
|
|
using FireSharp.Interfaces;
|
|
using FireSharp.Config;
|
|
using FireSharp;
|
|
using Newtonsoft.Json;
|
|
using System.Configuration;
|
|
|
|
namespace KaraokePlayer.Classes
|
|
{
|
|
|
|
public class Singer
|
|
{
|
|
[JsonProperty("name")]
|
|
public string Name { get; set; }
|
|
}
|
|
|
|
public class Song
|
|
{
|
|
[JsonProperty("title")]
|
|
public string Title { get; set; }
|
|
|
|
[JsonProperty("artist")]
|
|
public string Artist { get; set; }
|
|
|
|
[JsonProperty("genre")]
|
|
public string Genre { get; set; }
|
|
|
|
[JsonProperty("year")]
|
|
public int Year { get; set; }
|
|
|
|
[JsonProperty("path")]
|
|
public string Path { get; set; }
|
|
|
|
[JsonIgnore]
|
|
public FileType FileType
|
|
{
|
|
get
|
|
{
|
|
switch (Path.Substring(Path.Length - 3, 3).ToLower())
|
|
{
|
|
case "cdg":
|
|
case "mp3":
|
|
return FileType.CDG;
|
|
case "mp4":
|
|
return FileType.MP4;
|
|
case "zip":
|
|
return FileType.ZIP;
|
|
default:
|
|
throw new Exception("file type not handled");
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public class QueueItem
|
|
{
|
|
[JsonProperty("singer")]
|
|
public Singer Singer { get; set; }
|
|
|
|
[JsonProperty("song")]
|
|
public Song Song { get; set; }
|
|
}
|
|
|
|
public class FirebaseController
|
|
{
|
|
private IFirebaseConfig config = new FirebaseConfig
|
|
{
|
|
AuthSecret = ConfigurationManager.AppSettings["Firebase.Secret"],
|
|
BasePath = ConfigurationManager.AppSettings["Firebase.Path"]
|
|
};
|
|
private string _state = "stop";
|
|
private IFirebaseClient _client;
|
|
private ControllerStateChangedEventHandler _stateChanged;
|
|
private ControllerSongChangedEventHandler _songChanged;
|
|
private ControllerPlayQueueChangedEventHandler _playQueueChanged;
|
|
|
|
private string StatePath
|
|
{
|
|
get { return string.Format("controllers/{0}/state/", this.Id); }
|
|
}
|
|
private string PlayQueuePath
|
|
{
|
|
get { return string.Format("controllers/{0}/queue/", this.Id); }
|
|
}
|
|
private string CurrentSongPath
|
|
{
|
|
get { return string.Format("controllers/{0}/queue/0/", this.Id); }
|
|
}
|
|
|
|
public string Id { get; set; }
|
|
public Song CurrentSong { get; set; }
|
|
public List<Song> PlayQueue { get; set; }
|
|
|
|
public string State {
|
|
get { return State; }
|
|
set {
|
|
_state = value;
|
|
Update(string.Format("controllers/{0}", this.Id), new { state = _state });
|
|
}
|
|
}
|
|
|
|
public FirebaseController(string clientId, ControllerStateChangedEventHandler stateChanged, ControllerSongChangedEventHandler songChanged, ControllerPlayQueueChangedEventHandler playQueueChanged)
|
|
{
|
|
Id = clientId;
|
|
_stateChanged = stateChanged;
|
|
_songChanged = songChanged;
|
|
_playQueueChanged = playQueueChanged;
|
|
PlayQueue = new List<Song>();
|
|
_client = new FirebaseClient(config);
|
|
Reset();
|
|
SetupListener();
|
|
}
|
|
|
|
public void NextSong()
|
|
{
|
|
Song song = PlayQueue.FirstOrDefault();
|
|
Stop();
|
|
if(song != null) { PlaySong(song); }
|
|
}
|
|
|
|
public void PlaySong(Song song) { Update(string.Format("Controllers/{0}", this.Id), new { CurrentSong = song }); }
|
|
|
|
public void Next() { this.State = "Next"; }
|
|
|
|
public void Play() { this.State = "Play"; }
|
|
|
|
public void Stop() { this.State = "Stop"; }
|
|
|
|
public void Pause() { this.State = "Pause"; }
|
|
|
|
private void Delete(string path) { _client.DeleteAsync(path); }
|
|
|
|
private void Update(string path, object data) { _client.UpdateAsync(path, data); }
|
|
|
|
private void Reset()
|
|
{
|
|
Stop();
|
|
Delete(PlayQueuePath);
|
|
Delete(CurrentSongPath);
|
|
}
|
|
|
|
private async void SetupListener()
|
|
{
|
|
await _client.OnAsync(CurrentSongPath,
|
|
added: (s, args, obj) =>
|
|
{
|
|
if (args.Path == "/singer/name")
|
|
{
|
|
Console.WriteLine("added " + args.Path + " " + args.Data);
|
|
CurrentSongDidChange();
|
|
}
|
|
},
|
|
removed: (s, args, obj) =>
|
|
{
|
|
//TODO: the current song was removed from the queue
|
|
if (args.Path == "/singer")
|
|
{
|
|
Console.WriteLine("removed " + args.Path);
|
|
}
|
|
}
|
|
);
|
|
/*
|
|
await _client.OnAsync(PlayQueuePath,
|
|
added: (s, args, obj) =>
|
|
{
|
|
Console.WriteLine(args.Path);
|
|
if (args.Path.Contains("Id"))
|
|
{
|
|
PlayQueueDidChange();
|
|
}
|
|
},
|
|
changed: (s, args, obj) =>
|
|
{
|
|
if (args.Path.Contains("Id"))
|
|
{
|
|
PlayQueueDidChange();
|
|
}
|
|
},
|
|
removed: (s, args, obj) =>
|
|
{
|
|
PlayQueueDidChange();
|
|
}
|
|
);
|
|
|
|
await _client.OnAsync(StatePath,
|
|
added: (s, args, obj) =>
|
|
{
|
|
StateDidChange(args.Data);
|
|
},
|
|
changed: (s, args, obj) =>
|
|
{
|
|
StateDidChange(args.Data);
|
|
},
|
|
removed: null
|
|
);
|
|
*/
|
|
}
|
|
|
|
private void PlayQueueDidChange()
|
|
{
|
|
bool autoPlay = PlayQueue.Count() == 0;
|
|
var response = _client.Get(PlayQueuePath);
|
|
var dict = response.ResultAs<Dictionary<string, Song>>();
|
|
|
|
PlayQueue.Clear();
|
|
if (dict != null && dict.Count() > 0)
|
|
{
|
|
var array = dict.Values.ToArray();
|
|
PlayQueue.AddRange(array);
|
|
if (autoPlay)
|
|
{
|
|
NextSong();
|
|
}
|
|
}
|
|
if (_playQueueChanged != null) { _playQueueChanged(); }
|
|
}
|
|
|
|
private void CurrentSongDidChange()
|
|
{
|
|
var response = _client.Get(CurrentSongPath);
|
|
var item = response.ResultAs<QueueItem>();
|
|
if (item != null)
|
|
{
|
|
CurrentSong = item.Song;
|
|
_songChanged(new ControllerSongChangedEventArgs(item.Song));
|
|
}
|
|
}
|
|
|
|
private void StateDidChange(string state)
|
|
{
|
|
_state = state;
|
|
PlayerState s = PlayerState.Play;
|
|
if (state.ToLower() == "pause")
|
|
{
|
|
s = PlayerState.Pause;
|
|
}
|
|
else if (state.ToLower() == "stop")
|
|
{
|
|
s = PlayerState.Stop;
|
|
}
|
|
else if (state == "next")
|
|
{
|
|
s = PlayerState.Next;
|
|
}
|
|
//_stateChanged(new ControllerStateChangedEventArgs(s));
|
|
}
|
|
|
|
}
|
|
}
|