220 lines
6.9 KiB
C#
220 lines
6.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using KaraokePlayer.Interfaces;
|
|
using KaraokePlayer.Enums;
|
|
using FireSharp.Interfaces;
|
|
using FireSharp.Config;
|
|
using FireSharp;
|
|
|
|
namespace KaraokePlayer.Classes
|
|
{
|
|
public class FirebaseSong : ISong
|
|
{
|
|
public string FirebaseId { get; set; }
|
|
public int Id { get; set; }
|
|
public int Order { get; set; }
|
|
public string Title { get; set; }
|
|
public string Artist { get; set; }
|
|
public FileType FileType { get; set; }
|
|
public string Path { get; set; }
|
|
|
|
public string Description { get { return Artist + " - " + Title; } }
|
|
}
|
|
|
|
public class FirebaseController : IController
|
|
{
|
|
private IFirebaseConfig config = new FirebaseConfig
|
|
{
|
|
AuthSecret = "wj0ERDFZqNSysTtIXcCgCr8Itahr6pJOBeqCjvDF",
|
|
BasePath = "https://karaokecontroller.firebaseio.com/"
|
|
};
|
|
private IFirebaseClient client;
|
|
private ControllerStateChangedEventHandler StateChanged;
|
|
private ControllerSongChangedEventHandler SongChanged;
|
|
private ControllerPlayQueueChangedEventHandler PlayQueueChanged;
|
|
public FirebaseController(ControllerStateChangedEventHandler stateChanged, ControllerSongChangedEventHandler songChanged, ControllerPlayQueueChangedEventHandler playQueueChanged)
|
|
{
|
|
StateChanged = stateChanged;
|
|
SongChanged = songChanged;
|
|
PlayQueueChanged = playQueueChanged;
|
|
PlayQueue = new List<ISong>();
|
|
client = new FirebaseClient(config);
|
|
client.DeleteAsync("controller/state");
|
|
client.PushAsync("controller/state", "stop");
|
|
client.DeleteAsync("controller/playQueue");
|
|
client.DeleteAsync("controller/currentSong");
|
|
ListenToStream();
|
|
}
|
|
|
|
public int Id { get; set; }
|
|
public ISong CurrentSong { get; set; }
|
|
public List<ISong> PlayQueue { get; set; }
|
|
public void GetNextSong()
|
|
{
|
|
ISong song = PlayQueue.FirstOrDefault();
|
|
Stop();
|
|
client.DeleteAsync("controller/currentSong");
|
|
client.PushAsync("controller/currentSong", song);
|
|
}
|
|
|
|
public void PlaySong(ISong song)
|
|
{
|
|
client.DeleteAsync("controller/currentSong");
|
|
client.PushAsync("controller/currentSong", song);
|
|
}
|
|
public void AddSongToQueue(ISong song)
|
|
{
|
|
client.PushAsync("controller/playQueue", song);
|
|
}
|
|
|
|
public void RemoveSong(ISong song)
|
|
{
|
|
ISong found = PlayQueue.FirstOrDefault(s => s.Id == song.Id);
|
|
if (found != null)
|
|
{
|
|
PlayQueue.Remove(found);
|
|
client.DeleteAsync("controller/playQueue/" + ((FirebaseSong)song).FirebaseId);
|
|
}
|
|
}
|
|
public void Next()
|
|
{
|
|
client.DeleteAsync("controller/state");
|
|
client.PushAsync("controller/state", "next");
|
|
}
|
|
|
|
public void Play()
|
|
{
|
|
client.DeleteAsync("controller/state");
|
|
client.PushAsync("controller/state", "play");
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
client.DeleteAsync("controller/state");
|
|
client.PushAsync("controller/state", "stop");
|
|
}
|
|
|
|
public void Pause()
|
|
{
|
|
client.DeleteAsync("controller/state");
|
|
client.PushAsync("controller/state", "pause");
|
|
}
|
|
|
|
private async void ListenToStream()
|
|
{
|
|
await client.OnAsync("controller/currentSong",
|
|
added: (s, args, obj) =>
|
|
{
|
|
if (args.Path.Contains("Id"))
|
|
{
|
|
CurrentSongChanged();
|
|
}
|
|
},
|
|
changed: (s, args, obj) =>
|
|
{
|
|
if (args.Path.Contains("Id"))
|
|
{
|
|
CurrentSongChanged();
|
|
}
|
|
},
|
|
removed: null
|
|
);
|
|
|
|
await client.OnAsync("controller/playQueue",
|
|
added: (s, args, obj) =>
|
|
{
|
|
if (args.Path.Contains("Id"))
|
|
{
|
|
ReloadPlayQueue();
|
|
}
|
|
},
|
|
changed: (s, args, obj) =>
|
|
{
|
|
if (args.Path.Contains("Id"))
|
|
{
|
|
ReloadPlayQueue();
|
|
}
|
|
},
|
|
removed: (s, args, obj) =>
|
|
{
|
|
ReloadPlayQueue();
|
|
}
|
|
);
|
|
|
|
await client.OnAsync("controller/state",
|
|
added: (s, args, obj) =>
|
|
{
|
|
RemoteStateChanged(args.Data);
|
|
},
|
|
changed: (s, args, obj) =>
|
|
{
|
|
RemoteStateChanged(args.Data);
|
|
},
|
|
removed: null
|
|
);
|
|
}
|
|
|
|
private void ReloadPlayQueue()
|
|
{
|
|
bool autoPlay = PlayQueue.Count() == 0;
|
|
var response = client.Get("controller/playQueue");
|
|
var dict = response.ResultAs<Dictionary<string, FirebaseSong>>();
|
|
|
|
PlayQueue.Clear();
|
|
if (dict != null && dict.Count() > 0)
|
|
{
|
|
foreach (KeyValuePair<string, FirebaseSong> entry in dict)
|
|
{
|
|
entry.Value.FirebaseId = entry.Key;
|
|
}
|
|
|
|
var array = dict.Values.OrderBy(s => s.Order).ToArray();
|
|
PlayQueue.AddRange(array);
|
|
if (autoPlay)
|
|
{
|
|
GetNextSong();
|
|
}
|
|
}
|
|
if (PlayQueueChanged != null) { PlayQueueChanged(); }
|
|
PlayQueueChanged();
|
|
}
|
|
|
|
private void CurrentSongChanged()
|
|
{
|
|
var response = client.Get("controller/currentSong");
|
|
var dict = response.ResultAs<Dictionary<string, FirebaseSong>>();
|
|
if (dict != null)
|
|
{
|
|
var song = dict.Values.FirstOrDefault();
|
|
if (song != null)
|
|
{
|
|
CurrentSong = song;
|
|
SongChanged(new ControllerSongChangedEventArgs(false, song));
|
|
}
|
|
}
|
|
}
|
|
|
|
private void RemoteStateChanged(string 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));
|
|
}
|
|
|
|
}
|
|
}
|