117 lines
3.8 KiB
C#
117 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using FireSharp.Interfaces;
|
|
using FireSharp.Config;
|
|
using FireSharp;
|
|
using Newtonsoft.Json;
|
|
using System.Configuration;
|
|
using Herse.Models;
|
|
|
|
namespace KaraokePlayer.Classes
|
|
{
|
|
public class FirebaseController
|
|
{
|
|
private IFirebaseConfig config = new FirebaseConfig
|
|
{
|
|
AuthSecret = ConfigurationManager.AppSettings["Firebase.Secret"],
|
|
BasePath = ConfigurationManager.AppSettings["Firebase.Path"]
|
|
};
|
|
private string _state = "playing";
|
|
private IFirebaseClient _client;
|
|
private ControllerStateChangedEventHandler _stateChanged;
|
|
private ControllerSongChangedEventHandler _songChanged;
|
|
|
|
private string StatePath
|
|
{
|
|
get { return string.Format("controllers/{0}/state/", 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 void SetState(PlayerState state)
|
|
{
|
|
Update(string.Format("controllers/{0}", this.Id), new { state = _state });
|
|
}
|
|
|
|
public FirebaseController(string clientId, ControllerStateChangedEventHandler stateChanged, ControllerSongChangedEventHandler songChanged)
|
|
{
|
|
Id = clientId;
|
|
_stateChanged = stateChanged;
|
|
_songChanged = songChanged;
|
|
_client = new FirebaseClient(config);
|
|
SetupListener();
|
|
}
|
|
|
|
public void PlaySong(Song song) { Update(string.Format("Controllers/{0}", this.Id), new { CurrentSong = song }); }
|
|
|
|
|
|
private void Delete(string path) { _client.DeleteAsync(path); }
|
|
|
|
private void Update(string path, object data) { _client.UpdateAsync(path, data); }
|
|
|
|
private async void SetupListener()
|
|
{
|
|
await _client.OnAsync(CurrentSongPath,
|
|
added: (s, args, obj) =>
|
|
{
|
|
Console.WriteLine("added " + args.Path + " " + args.Data);
|
|
CurrentSongDidChange();
|
|
},
|
|
removed: (s, args, obj) =>
|
|
{
|
|
//TODO: the current song was removed from the queue
|
|
}
|
|
);
|
|
|
|
await _client.OnAsync(StatePath,
|
|
added: (s, args, obj) =>
|
|
{
|
|
// do we need this? don't think so
|
|
},
|
|
changed: (s, args, obj) =>
|
|
{
|
|
Console.WriteLine("state changed " + args.Data);
|
|
StateDidChange(args.Data);
|
|
},
|
|
removed: null
|
|
);
|
|
}
|
|
|
|
private void CurrentSongDidChange()
|
|
{
|
|
var item = _client.Get(CurrentSongPath).ResultAs<QueueItem>();
|
|
if (item == null) return;
|
|
if (CurrentSong == null || CurrentSong.Path != item.Song.Path)
|
|
{
|
|
CurrentSong = item.Song;
|
|
_songChanged(new ControllerSongChangedEventArgs(item.Song));
|
|
}
|
|
}
|
|
|
|
private void StateDidChange(string state)
|
|
{
|
|
if (state == _state) return;
|
|
_state = state;
|
|
switch(state)
|
|
{
|
|
case "paused":
|
|
_stateChanged(new ControllerStateChangedEventArgs(PlayerState.Paused));
|
|
break;
|
|
case "stopped":
|
|
_stateChanged(new ControllerStateChangedEventArgs(PlayerState.Stopped));
|
|
break;
|
|
case "playing":
|
|
_stateChanged(new ControllerStateChangedEventArgs(PlayerState.Playing));
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|