using System;
using System.Collections.Generic;
namespace AsterNET.Manager.Event
{
///
/// Abstract base class for all Events that can be received from the Asterisk server.
/// Events contain data pertaining to an event generated from within the Asterisk
/// core or an extension module.
/// There is one conrete subclass of ManagerEvent per each supported Asterisk Event.
///
/// Channel / Privilege / UniqueId are not common to all events and should be moved to
/// derived event classes.
///
public abstract class ManagerEvent : EventArgs, IParseSupport
{
#region Common Event Properties
///
/// Store all unknown (without setter) keys from manager event.
/// Use in default Parse method
///
public Dictionary Attributes { get; set; }
///
/// Get/Set the name of the channel.
///
public string Channel { get; set; }
///
/// Get/Set the point in time this event was received from the Asterisk server.
/// Pseudo events that are not directly received from the asterisk server
/// (for example ConnectEvent and DisconnectEvent) may return null.
///
public DateTime DateReceived { get; set; }
///
/// Get/Set the AMI authorization class of this event.
/// This is one or more of system, call, log, verbose, command, agent or user.
/// Multiple privileges are separated by comma.
/// Note: This property is not available from Asterisk 1.0 servers.
///
public string Privilege { get; set; }
///
/// Specify a server to which to send your commands (x.x.x.x or hostname).
/// 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.
///
public string Server { get; set; }
///
/// The ManagerConnection the Event was sourced from.
///
public ManagerConnection Source { get; set; }
///
/// Returns the timestamp for this event.
/// 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.
///
public double Timestamp { get; set; }
///
/// Get/Set the unique id of the channel.
///
public string UniqueId { get; set; }
#endregion
#region Constructors
///
/// Creates a new ManagerEvent. Source already set.
///
public ManagerEvent()
{
this.DateReceived = DateTime.Now;
}
///
/// Creates a new ManagerEvent
///
/// ManagerConnection passed through in the event.
public ManagerEvent(ManagerConnection source)
: this()
{
this.Source = source;
}
#endregion
#region Methods
///
/// Unknown properties parser
///
/// key name
/// key value
/// true - value parsed, false - can't parse value
public virtual bool Parse(string key, string value)
{
if (Attributes == null)
{
Attributes = new Dictionary();
}
if (Attributes.ContainsKey(key))
{
Attributes[key] += string.Concat(Common.LINE_SEPARATOR, value); // Key already presents, add with delimiter
}
else
{
Attributes.Add(key, value);
}
return true;
}
///
/// Unknown properties parser.
///
/// dictionary
/// updated dictionary
public virtual Dictionary ParseSpecial(Dictionary attributes)
{
return attributes;
}
///
/// Convert all properties to string
///
/// All event details and properties as a string
public override string ToString()
{
return Helper.ToString(this);
}
#endregion
}
}