using System;
using PepperDash.Core;
using PepperDash.Core.Logging;
using PepperDash.Essentials.AppServer.Messengers;
using PepperDash.Essentials.Core.DeviceTypeInterfaces;
namespace PepperDash.Essentials.RoomBridges
{
///
/// Base class for a Mobile Control Bridge that's used to control a room
///
public abstract class MobileControlBridgeBase : MessengerBase, IMobileControlRoomMessenger
{
///
/// Triggered when the user Code changes
///
public event EventHandler UserCodeChanged;
///
/// Triggered when a user should be prompted for the new code
///
public event EventHandler UserPromptedForCode;
///
/// Triggered when a client joins to control this room
///
public event EventHandler ClientJoined;
///
/// Triggered when the App URL for this room changes
///
public event EventHandler AppUrlChanged;
///
/// Gets or sets the Parent
///
public IMobileControl Parent { get; private set; }
///
/// Gets or sets the AppUrl
///
public string AppUrl { get; private set; }
///
/// Gets or sets the UserCode
///
public string UserCode { get; private set; }
///
/// Gets or sets the QrCodeUrl
///
public string QrCodeUrl { get; protected set; }
///
/// Gets or sets the QrCodeChecksum
///
public string QrCodeChecksum { get; protected set; }
///
/// Gets or sets the McServerUrl
///
public string McServerUrl { get; private set; }
///
/// Room Name
///
public abstract string RoomName { get; }
///
/// Room key
///
public abstract string RoomKey { get; }
///
/// Create an instance of the class
///
/// The unique key for this bridge
/// The message path for this bridge
protected MobileControlBridgeBase(string key, string messagePath)
: base(key, messagePath)
{
}
///
/// Create an instance of the class
///
/// The unique key for this bridge
/// The message path for this bridge
/// The device associated with this bridge
protected MobileControlBridgeBase(string key, string messagePath, IKeyName device)
: base(key, messagePath, device)
{
}
///
/// Set the parent. Does nothing else. Override to add functionality such
/// as adding actions to parent
///
///
///
/// AddParent method
///
public virtual void AddParent(IMobileControl parent)
{
Parent = parent;
McServerUrl = Parent.ClientAppUrl;
}
///
/// Sets the UserCode on the bridge object. Called from controller. A changed code will
/// fire method UserCodeChange. Override that to handle changes
///
///
///
/// SetUserCode method
///
public void SetUserCode(string code)
{
var changed = UserCode != code;
UserCode = code;
if (changed)
{
UserCodeChange();
}
}
///
/// Sets the UserCode on the bridge object. Called from controller. A changed code will
/// fire method UserCodeChange. Override that to handle changes
///
///
/// Checksum of the QR code. Used for Cisco codec branding command
public void SetUserCode(string code, string qrChecksum)
{
QrCodeChecksum = qrChecksum;
SetUserCode(code);
}
///
/// Update the App Url with the provided URL
///
/// The new App URL
public virtual void UpdateAppUrl(string url)
{
AppUrl = url;
var handler = AppUrlChanged;
if (handler == null) return;
handler(this, new EventArgs());
}
///
/// Empty method in base class. Override this to add functionality
/// when code changes
///
protected virtual void UserCodeChange()
{
this.LogDebug("Server user code changed: {userCode}", UserCode);
var qrUrl = string.Format($"{Parent.Host}/api/rooms/{Parent.SystemUuid}/{RoomKey}/qr?x={new Random().Next()}");
QrCodeUrl = qrUrl;
this.LogDebug("Server user code changed: {userCode} - {qrCodeUrl}", UserCode, qrUrl);
OnUserCodeChanged();
}
///
/// Trigger the UserCodeChanged event
///
protected void OnUserCodeChanged()
{
UserCodeChanged?.Invoke(this, new EventArgs());
}
///
/// Trigger the UserPromptedForCode event
///
protected void OnUserPromptedForCode()
{
UserPromptedForCode?.Invoke(this, new EventArgs());
}
///
/// Trigger the ClientJoined event
///
protected void OnClientJoined()
{
ClientJoined?.Invoke(this, new EventArgs());
}
}
}