ManagerEvent layout and formatting changes

This commit is contained in:
Sean Maddison 2016-09-30 08:57:38 +02:00
parent 121bac20d4
commit ac07b96105

View file

@ -1,178 +1,139 @@
using System; using System;
using System.Text;
using System.Collections.Generic; using System.Collections.Generic;
namespace AsterNET.Manager.Event namespace AsterNET.Manager.Event
{ {
/// <summary> /// <summary>
/// Abstract base class for all Events that can be received from the Asterisk server.<br/> /// Abstract base class for all Events that can be received from the Asterisk server.<br/>
/// Events contain data pertaining to an event generated from within the Asterisk /// Events contain data pertaining to an event generated from within the Asterisk
/// core or an extension module.<br/> /// core or an extension module.<br/>
/// There is one conrete subclass of ManagerEvent per each supported Asterisk /// There is one conrete subclass of ManagerEvent per each supported Asterisk Event.
/// Event. ///
/// </summary> /// Channel / Privilege / UniqueId are not common to all events and should be moved to
public abstract class ManagerEvent : EventArgs, IParseSupport /// derived event classes.
{ /// </summary>
private DateTime dateReceived; public abstract class ManagerEvent : EventArgs, IParseSupport
private string privilege; {
private string server; #region Common Event Properties
private double timestamp;
private string uniqueId; /// <summary>
private string channel; /// Store all unknown (without setter) keys from manager event.<br/>
private ManagerConnection src; /// Use in default Parse method <see cref="ManagerEvent.Parse(string, string)"/>
protected Dictionary<string, string> attributes; /// </summary>
public Dictionary<string, string> Attributes { get; set; }
#region Constructors /// <summary>
public ManagerEvent() /// Get/Set the name of the channel.
{ /// </summary>
this.dateReceived = DateTime.Now; public string Channel { get; set; }
}
/// <summary>
/// Get/Set the point in time this event was received from the Asterisk server.<br/>
/// Pseudo events that are not directly received from the asterisk server
/// (for example ConnectEvent and DisconnectEvent) may return null.
/// </summary>
public DateTime DateReceived { get; set; }
/// <summary>
/// Get/Set the AMI authorization class of this event.<br/>
/// This is one or more of system, call, log, verbose, command, agent or user.
/// Multiple privileges are separated by comma.<br/>
/// Note: This property is not available from Asterisk 1.0 servers.
/// </summary>
public string Privilege { get; set; }
/// <summary>
/// Specify a server to which to send your commands (x.x.x.x or hostname).<br/>
/// This should match the server name specified in your config file's "host" entry.
/// If you do not specify a server, the proxy will pick the first one it finds -- fine in single-server configurations.
/// </summary>
public string Server { get; set; }
/// <summary>
/// The ManagerConnection the Event was sourced from.
/// </summary>
public ManagerConnection Source { get; set; }
/// <summary>
/// Returns the timestamp for this event.<br/>
/// The timestamp property is available in Asterisk since 1.4
/// if enabled in manager.conf by setting timestampevents = yes.
/// In contains the time the event was generated in seconds since the epoch.
/// </summary>
public double Timestamp { get; set; }
/// <summary>
/// Get/Set the unique id of the channel.
/// </summary>
public string UniqueId { get; set; }
#endregion
#region Constructors
/// <summary>
/// Creates a new ManagerEvent. Source already set.
/// </summary>
public ManagerEvent()
{
this.DateReceived = DateTime.Now;
}
/// <summary>
/// Creates a new ManagerEvent
/// </summary>
/// <param name="source">ManagerConnection passed through in the event.</param>
public ManagerEvent(ManagerConnection source) public ManagerEvent(ManagerConnection source)
: this() : this()
{ {
this.src = source as ManagerConnection; this.Source = source;
} }
#endregion #endregion
#region Methods
#region Attributes /// <summary>
/// <summary> /// Unknown properties parser
/// Store all unknown (without setter) keys from manager event.<br/> /// </summary>
/// Use in default Parse method <see cref="Parse(string key, string value)"/>. /// <param name="key">key name</param>
/// </summary> /// <param name="value">key value</param>
public Dictionary<string, string> Attributes /// <returns>true - value parsed, false - can't parse value</returns>
{ public virtual bool Parse(string key, string value)
get { return attributes; } {
} if (Attributes == null)
#endregion {
Attributes = new Dictionary<string, string>();
}
#region Server if (Attributes.ContainsKey(key))
/// <summary> {
/// Specify a server to which to send your commands (x.x.x.x or hostname).<br/> Attributes[key] += string.Concat(Common.LINE_SEPARATOR, value); // Key already presents, add with delimiter
/// This should match the server name specified in your config file's "host" entry. }
/// If you do not specify a server, the proxy will pick the first one it finds -- fine in single-server configurations. else
/// </summary> {
public string Server Attributes.Add(key, value);
{ }
get { return server; }
set { server = value; }
}
#endregion
#region Timestamp return true;
/// <summary> }
/// Returns the timestamp for this event.<br/>
/// The timestamp property is available in Asterisk since 1.4
/// if enabled in manager.conf by setting timestampevents = yes.
/// In contains the time the event was generated in seconds since the epoch.
/// </summary>
public double Timestamp
{
get { return this.timestamp; }
set { this.timestamp = value; }
}
#endregion
#region DateReceived /// <summary>
/// <summary> /// Unknown properties parser.
/// Get/Set the point in time this event was received from the Asterisk server.<br/> /// </summary>
/// Pseudo events that are not directly received from the asterisk server /// <param name="attributes">dictionary</param>
/// (for example ConnectEvent and DisconnectEvent) may return null. /// <returns>updated dictionary</returns>
/// </summary> public virtual Dictionary<string, string> ParseSpecial(Dictionary<string, string> attributes)
public DateTime DateReceived {
{ return attributes;
get { return this.dateReceived; } }
set { this.dateReceived = value; }
}
#endregion
#region Privilege /// <summary>
/// <summary> /// Convert all properties to string
/// Get/Set the AMI authorization class of this event.<br/> /// </summary>
/// This is one or more of system, call, log, verbose, command, agent or user. /// <returns>All event details and properties as a string</returns>
/// Multiple privileges are separated by comma.<br/>
/// Note: This property is not available from Asterisk 1.0 servers.
/// </summary>
public string Privilege
{
get { return privilege; }
set { this.privilege = value; }
}
#endregion
#region Source
/// <summary>
/// Event source.
/// </summary>
public ManagerConnection Source
{
get { return this.src; }
}
#endregion
#region UniqueId
/// <summary>
/// Get/Set the unique id of the channel.
/// </summary>
public string UniqueId
{
get { return uniqueId; }
set { this.uniqueId = value; }
}
#endregion
#region Channel
/// <summary>
/// Get/Set the name of the channel.
/// </summary>
public string Channel
{
get { return channel; }
set { this.channel = value; }
}
#endregion
#region Parse(string key, string value)
/// <summary>
/// Unknown properties parser
/// </summary>
/// <param name="key">key name</param>
/// <param name="value">key value</param>
/// <returns>true - value parsed, false - can't parse value</returns>
public virtual bool Parse(string key, string value)
{
if (attributes == null)
attributes = new Dictionary<string, string>();
if (attributes.ContainsKey(key))
// Key already presents, add with delimiter
attributes[key] += string.Concat(Common.LINE_SEPARATOR, value);
else
attributes.Add(key, value);
return true;
}
#endregion
#region ParseSpecial(Dictionary<string, string> attributes)
/// <summary>
/// Unknown properties parser
/// </summary>
/// <param name="attributes">dictionary</param>
/// <returns>updated dictionary</returns>
public virtual Dictionary<string, string> ParseSpecial(Dictionary<string, string> attributes)
{
return attributes;
}
#endregion
#region ToString()
public override string ToString() public override string ToString()
{ {
return Helper.ToString(this); return Helper.ToString(this);
} }
#endregion #endregion
} }
} }