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,5 +1,4 @@
using System; using System;
using System.Text;
using System.Collections.Generic; using System.Collections.Generic;
namespace AsterNET.Manager.Event namespace AsterNET.Manager.Event
@ -8,134 +7,90 @@ namespace AsterNET.Manager.Event
/// 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. ///
/// Channel / Privilege / UniqueId are not common to all events and should be moved to
/// derived event classes.
/// </summary> /// </summary>
public abstract class ManagerEvent : EventArgs, IParseSupport public abstract class ManagerEvent : EventArgs, IParseSupport
{ {
private DateTime dateReceived; #region Common Event Properties
private string privilege;
private string server;
private double timestamp;
private string uniqueId;
private string channel;
private ManagerConnection src;
protected Dictionary<string, string> attributes;
#region Constructors
public ManagerEvent()
{
this.dateReceived = DateTime.Now;
}
public ManagerEvent(ManagerConnection source)
: this()
{
this.src = source as ManagerConnection;
}
#endregion
#region Attributes
/// <summary> /// <summary>
/// Store all unknown (without setter) keys from manager event.<br/> /// Store all unknown (without setter) keys from manager event.<br/>
/// Use in default Parse method <see cref="Parse(string key, string value)"/>. /// Use in default Parse method <see cref="ManagerEvent.Parse(string, string)"/>
/// </summary> /// </summary>
public Dictionary<string, string> Attributes public Dictionary<string, string> Attributes { get; set; }
{
get { return attributes; }
}
#endregion
#region Server
/// <summary> /// <summary>
/// Specify a server to which to send your commands (x.x.x.x or hostname).<br/> /// Get/Set the name of the channel.
/// 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> /// </summary>
public string Server public string Channel { get; set; }
{
get { return server; }
set { server = value; }
}
#endregion
#region Timestamp
/// <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>
/// Get/Set the point in time this event was received from the Asterisk server.<br/> /// 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 /// Pseudo events that are not directly received from the asterisk server
/// (for example ConnectEvent and DisconnectEvent) may return null. /// (for example ConnectEvent and DisconnectEvent) may return null.
/// </summary> /// </summary>
public DateTime DateReceived public DateTime DateReceived { get; set; }
{
get { return this.dateReceived; }
set { this.dateReceived = value; }
}
#endregion
#region Privilege
/// <summary> /// <summary>
/// Get/Set the AMI authorization class of this event.<br/> /// Get/Set the AMI authorization class of this event.<br/>
/// This is one or more of system, call, log, verbose, command, agent or user. /// This is one or more of system, call, log, verbose, command, agent or user.
/// Multiple privileges are separated by comma.<br/> /// Multiple privileges are separated by comma.<br/>
/// Note: This property is not available from Asterisk 1.0 servers. /// Note: This property is not available from Asterisk 1.0 servers.
/// </summary> /// </summary>
public string Privilege public string Privilege { get; set; }
{
get { return privilege; }
set { this.privilege = value; }
}
#endregion
#region Source
/// <summary> /// <summary>
/// Event source. /// 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> /// </summary>
public ManagerConnection Source public string Server { get; set; }
{
get { return this.src; } /// <summary>
} /// The ManagerConnection the Event was sourced from.
#endregion /// </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; }
#region UniqueId
/// <summary> /// <summary>
/// Get/Set the unique id of the channel. /// Get/Set the unique id of the channel.
/// </summary> /// </summary>
public string UniqueId public string UniqueId { get; set; }
{
get { return uniqueId; }
set { this.uniqueId = value; }
}
#endregion #endregion
#region Channel #region Constructors
/// <summary> /// <summary>
/// Get/Set the name of the channel. /// Creates a new ManagerEvent. Source already set.
/// </summary> /// </summary>
public string Channel public ManagerEvent()
{ {
get { return channel; } this.DateReceived = DateTime.Now;
set { this.channel = value; } }
/// <summary>
/// Creates a new ManagerEvent
/// </summary>
/// <param name="source">ManagerConnection passed through in the event.</param>
public ManagerEvent(ManagerConnection source)
: this()
{
this.Source = source;
} }
#endregion #endregion
#region Methods
#region Parse(string key, string value)
/// <summary> /// <summary>
/// Unknown properties parser /// Unknown properties parser
/// </summary> /// </summary>
@ -144,21 +99,25 @@ namespace AsterNET.Manager.Event
/// <returns>true - value parsed, false - can't parse value</returns> /// <returns>true - value parsed, false - can't parse value</returns>
public virtual bool Parse(string key, string value) public virtual bool Parse(string key, string value)
{ {
if (attributes == null) if (Attributes == null)
attributes = new Dictionary<string, string>(); {
Attributes = new Dictionary<string, string>();
}
if (attributes.ContainsKey(key)) if (Attributes.ContainsKey(key))
// Key already presents, add with delimiter {
attributes[key] += string.Concat(Common.LINE_SEPARATOR, value); Attributes[key] += string.Concat(Common.LINE_SEPARATOR, value); // Key already presents, add with delimiter
}
else else
attributes.Add(key, value); {
Attributes.Add(key, value);
}
return true; return true;
} }
#endregion
#region ParseSpecial(Dictionary<string, string> attributes)
/// <summary> /// <summary>
/// Unknown properties parser /// Unknown properties parser.
/// </summary> /// </summary>
/// <param name="attributes">dictionary</param> /// <param name="attributes">dictionary</param>
/// <returns>updated dictionary</returns> /// <returns>updated dictionary</returns>
@ -166,9 +125,11 @@ namespace AsterNET.Manager.Event
{ {
return attributes; return attributes;
} }
#endregion
#region ToString() /// <summary>
/// Convert all properties to string
/// </summary>
/// <returns>All event details and properties as a string</returns>
public override string ToString() public override string ToString()
{ {
return Helper.ToString(this); return Helper.ToString(this);