question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to send [Action: ExtensionStateList] with AsterNET?

See original GitHub issue

I’m working with AsterNET and C#, I need to get the status of all the extensions, specifically the result of Action: ExtensionStateList but the library doesn’t have this action, I’m trying to create it but I can’t get it. I hope someone can guide me. Attached is the code.

ResponseEvents re;
try
{
   re = manager.SendEventGeneratingAction(new ExtensionStateListAction());
}
catch (EventTimeoutException e)
{
   re = e.PartialResult;
}
foreach (ManagerEvent e in re.Events)
{
   foreach (KeyValuePair<string, string> d in e.Attributes)
   {
       Console.WriteLine(e);
   }
}
using System;
using AsterNET.Manager.Event;
namespace AsterNET.Manager.Action
{
    public class ExtensionStateListAction : ManagerActionEvent
    {

        public override string Action
        {
            get { return "ExtensionStateList"; }
        }

        public override Type ActionCompleteEventClass()
        {
            return typeof (ExtensionStateListCompleteEvent);
        }
    }
}

namespace AsterNET.Manager.Event
{
    public class ExtensionStateListCompleteEvent : ResponseEvent
    {
        private int listItems;

        public int ListItems
        {
            get { return this.listItems; }
            set { this.listItems = value; }
        }

        public ExtensionStateListCompleteEvent(ManagerConnection source) 
            : base(source)
        {
        }
    }
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
Deantwocommented, Apr 3, 2020

You have created the ExtensionStateListAction action and the ExtensionStateListCompleteEvent event.

Aren’t you perhaps missing the ExtensionStateEvent event?

The Asterisk documentation: https://wiki.asterisk.org/wiki/display/AST/Asterisk+13+ManagerAction_ExtensionStateList

Description This will list out all known extension states in a sequence of ExtensionStatus events. When finished, a ExtensionStateListComplete event will be emitted.

I have never used the SendEventGeneratingAction(...) method, I normally just catch the events manually. And maybe that is your issue, you are expecting ExtensionStateListCompleteEvent to be a ResponseEvent when it isn’t.

Try simply subscribing to the events, send the action like a normal action, and see what you get.

0reactions
Deantwocommented, Apr 6, 2020

Here is an untested example created using some code I wrote for a similar thing.

private Dictionary<string, List<ExtensionStateListAction>> _extensionStateListActions = new Dictionary<string, List<ExtensionStateListAction>>();
private int _actionIdNum = 0;

public void ExtensionStateList()
{
    if (_ami is null)
        throw new Exception("Cannot send command while not connected.");

    string actionId = $"Action{++_actionIdNum}";
    _extensionStateListActions.Add(actionId, new List<ExtensionStateEvent>());

    ManagerResponse extensionStateListResponse = null;
    try
    {
        ExtensionStateListAction extensionStateListAction = new ExtensionStateListAction()
        {
            ActionId = actionId
        };
        extensionStateListResponse = _ami.SendAction(extensionStateListAction);
        if (extensionStateListResponse.Response == "Error")
        {
            if (extensionStateListResponse.Message == "Action Filtered")
                throw new Exception("AmiActionFilteredException");
            else
                throw new Exception($"AmiActionFailedException: {extensionStateListResponse.Message}.");
        }
    }
    catch (AsterNET.Manager.TimeoutException ex)
    {
        throw new Exception("AmiActionTimeoutException", ex);
    }
    catch (SystemException ex)
    {
        throw new Exception("AmiConnectionLostException", ex);
    }
    catch (Exception ex)
    {
        throw;
    }
}

private void ExtensionStateEvent(object sender, ExtensionStateEvent e)
{
    if (e == null)
        throw new ArgumentNullException(nameof(e));

    if (_extensionStateListActions.ContainsKey(e.ActionId))
    {
        _extensionStateListActions[e.ActionId].Add(e);
    }
    else
        throw new Exception($"Unexpected ExtensionState Event, ActionId: {e.ActionId}");
}

private void ExtensionStateListCompleteEvent(object sender, ExtensionStateListCompleteEvent e)
{
    if (e == null)
        throw new ArgumentNullException(nameof(e));

    if (_extensionStateListActions.ContainsKey(e.ActionId))
    {
        // Fire an AmiExtensionStateListComplete event.
        ExtensionStateListEventArgs args = new ExtensionStateListEventArgs();
        args.ExtensionStateList = _extensionStateListActions[e.ActionId];
        OnAmiExtensionStateListComplete(args);

        // Finished, delete _extensionStateListActions entry.
        _extensionStateListActions.Remove(e.ActionId);
    }
    else
        throw new Exception($"Unexpected ExtensionStateListComplete Event, ActionId: {e.ActionId}");
}

public event EventHandler<ExtensionStateListEventArgs> AmiExtensionStateListComplete;

private void OnAmiExtensionStateListComplete(ExtensionStateListEventArgs e)
{
    AmiExtensionStateListComplete?.Invoke(this, e);
}

Make a simple ExtensionStateListEventArgs class with a public List<ExtensionStateListAction> ExtensionStateList { get; set; } property.

Again this is asynchronous. You call the void ExtensionStateList() method, and then wait for the AmiExtensionStateListComplete event to be raised. I don’t have a good idea about how to make it synchronous, but it shouldn’t be impossible to make it wait or something.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to send action with AsterNET, C#? - ...
I'm working with AsterNET and C#, I need to get the status of all the extensions, specifically the result of Action: ExtensionStateList but ......
Read more >
Newest 'asternet' Questions
I'm working with AsterNET and C#, I need to get the status of all the extensions, specifically the result of Action: ExtensionStateList but...
Read more >
Asterisk 13 ManagerAction_ExtensionStateList
Syntax. Action: ExtensionStateList ActionID: <value>. Arguments. ActionID - ActionID for this transaction. Will ...
Read more >
ManagerConnection.SendAction Method (ManagerAction)
Send Action with default timeout. ... Assembly: AsterNET (in AsterNET.dll) Version: 1.0.0.0 (1.0.0.0). Syntax. C#. VB. C++. F#. Copy.
Read more >
AsterNET.Manager.Action Namespace
The CommandAction sends a command line interface (CLI) command to the asterisk server. For a list of supported commands type help on asterisk's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found