KaraokePC/FirebaseKaraoke/FirebaseController.cs
Matt Bruce 69caf72808 added in songlist uploader
Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
2016-10-02 10:38:01 -05:00

137 lines
4.5 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 PlayerState _state = PlayerState.Playing;
private IFirebaseClient _client;
private ControllerStateChangedEventHandler _stateChanged;
private ControllerSongChangedEventHandler _songChanged;
public Settings Settings { get; set; }
private string SettingsPath
{
get { return string.Format("controllers/{0}/player/settings", this.Id); }
}
private string QueuePath
{
get { return string.Format("controllers/{0}/player/queue", this.Id); }
}
private string StatePath
{
get { return string.Format("controllers/{0}/player/state/", this.Id); }
}
private string CurrentSongPath
{
get { return string.Format("controllers/{0}/player/queue/0/", this.Id); }
}
private string MostPlayedPath
{
get { return string.Format("controllers/{0}/mostplayed/", this.Id); }
}
public string Id { get; set; }
public Song CurrentSong { get; set; }
public FirebaseController(string clientId, ControllerStateChangedEventHandler stateChanged, ControllerSongChangedEventHandler songChanged)
{
Id = clientId;
_stateChanged = stateChanged;
_songChanged = songChanged;
_client = new FirebaseClient(config);
SetupListener();
Settings = _client.Get(SettingsPath).ResultAs<Settings>();
}
private void SetupListener()
{
_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
}
);
_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);
PlayerState newstate = (PlayerState)Enum.Parse(typeof(PlayerState), args.Data, true);
StateDidChange(newstate);
},
removed: null
);
}
/// <summary>
/// called by the player to end the current song in firebase
/// </summary>
public void EndSong()
{
SetState(PlayerState.Stopped);
List<QueueItem> queue = _client.Get(QueuePath).ResultAs<List<QueueItem>>();
queue.RemoveAt(0);
_client.Set(QueuePath, queue);
}
/// <summary>
/// called by the player to set the state in firebase
/// </summary>
/// <param name="state"></param>
public void SetState(PlayerState state)
{
_state = state;
_client.SetAsync(StatePath, "Playing");
}
/// <summary>
/// called by firebase to change the current song
/// </summary>
private void CurrentSongDidChange()
{
var response = _client.Get(CurrentSongPath);
var item = response.ResultAs<QueueItem>();
if (item == null) return;
if (CurrentSong == null || CurrentSong.Path != item.Song.Path)
{
CurrentSong = item.Song;
_songChanged(new ControllerSongChangedEventArgs(item.Song));
}
}
/// <summary>
/// called by firebase to change the current state
/// </summary>
/// <param name="newstate"></param>
private void StateDidChange(PlayerState newstate)
{
if (newstate == _state) return;
_state = newstate;
_stateChanged(new ControllerStateChangedEventArgs(newstate));
}
}
}