diff --git a/applications/ecallmgr/src/ecallmgr_originate.erl b/applications/ecallmgr/src/ecallmgr_originate.erl index 4694574f682..b153f54c7fe 100644 --- a/applications/ecallmgr/src/ecallmgr_originate.erl +++ b/applications/ecallmgr/src/ecallmgr_originate.erl @@ -546,22 +546,20 @@ add_loopback('false') -> -spec originate_execute(atom(), kz_term:ne_binary(), pos_integer()) -> {'ok', kz_term:ne_binary()} | {'error', kz_term:ne_binary() | 'timeout' | 'crash'}. -originate_execute(Node, Dialstrings, Timeout) -> +originate_execute(Node, Dialstrings, _Timeout) -> lager:debug("executing originate on ~s: ~s", [Node, Dialstrings]), - case freeswitch:api(Node - ,'originate' - ,kz_term:to_list(Dialstrings) - ,Timeout * ?MILLISECONDS_IN_SECOND + 2000 - ) + case freeswitch:async_api(Node + ,'originate' + ,kz_term:to_list(Dialstrings) + ) of - {'ok', <<"+OK ", ID/binary>>} -> - UUID = kz_binary:strip(binary:replace(ID, <<"\n">>, <<>>)), + {'ok', UUID} -> Media = get('hold_media'), _Pid = kz_util:spawn(fun set_music_on_hold/3, [Node, UUID, Media]), {'ok', UUID}; - {'ok', Other} -> - lager:debug("recv other 'ok': ~s", [Other]), - {'error', kz_binary:strip(binary:replace(Other, <<"\n">>, <<>>))}; + 'ok' -> + lager:debug("recv other 'ok'"), + {'error', 'empty_response'}; {'error', Error} when is_binary(Error) -> lager:debug("error originating: ~s", [Error]), {'error', kz_binary:strip(binary:replace(Error, <<"\n">>, <<>>))}; diff --git a/applications/ecallmgr/src/freeswitch.erl b/applications/ecallmgr/src/freeswitch.erl index e7c3b647b49..17ad2da647b 100644 --- a/applications/ecallmgr/src/freeswitch.erl +++ b/applications/ecallmgr/src/freeswitch.erl @@ -41,6 +41,8 @@ ,bgapi4/5 ]). +-export([async_api/3]). + -include("ecallmgr.hrl"). -define(TIMEOUT, 5 * ?MILLISECONDS_IN_SECOND). @@ -140,3 +142,7 @@ config(Node) -> ?FS_MODULE:config(Node). {'ok', binary()} | {'error', 'timeout' | 'exception' | binary()}. bgapi4(Node, Cmd, Args, Fun, CallBackParams) -> ?FS_MODULE:bgapi4(Node, Cmd, Args, Fun, CallBackParams). + +-spec async_api(atom(), atom(), string() | binary()) -> fs_api_return(). +async_api(Node, Cmd, Args) -> ?FS_MODULE:async_api(Node, Cmd, Args). + diff --git a/applications/ecallmgr/src/mod_kazoo.erl b/applications/ecallmgr/src/mod_kazoo.erl index fa8040bab68..6b34cf5d9a0 100644 --- a/applications/ecallmgr/src/mod_kazoo.erl +++ b/applications/ecallmgr/src/mod_kazoo.erl @@ -41,6 +41,8 @@ ,bgapi4/5 ]). +-export([async_api/3]). + -include("ecallmgr.hrl"). -define(TIMEOUT, 5 * ?MILLISECONDS_IN_SECOND). @@ -133,6 +135,27 @@ fetch_reply(Node, FetchID, Section, Reply, Timeout) -> {'error', 'exception'} end. +api_result(Result, 'undefined') -> Result; +api_result(Result, Bin) -> + case kz_binary:strip_left(kz_binary:strip_right(Bin, <<"\n">>), $\s) of + <<>> when Result =:= 'error' -> {error, 'failed'}; + <<"true">> -> {Result, true}; + <<"false">> -> {Result, false}; + <<>> -> ok; + Msg -> {Result, maybe_number(Msg, byte_size(Msg))} + end. + +maybe_number(Msg, Size) + when Size < 10 -> + Float = (catch erlang:binary_to_float(Msg)), + Int = (catch erlang:binary_to_integer(Msg)), + case {is_number(Float), is_number(Int)} of + {'true', _} -> Float; + {_, 'true'} -> Int; + _ -> Msg + end; +maybe_number(Msg, _Size) -> Msg. + -spec api(atom(), kz_term:text()) -> fs_api_return(). api(Node, Cmd) -> api(Node, Cmd, ""). @@ -387,3 +410,21 @@ bgapi4(Node, Cmd, Args, Fun, CallBackParams) -> internal_fs_error(Reason) -> Error = kz_binary:strip(binary:replace(Reason, <<"\n">>, <<>>)), {'error', Error}. + +%%------------------------------------------------------------------------------ +%% @doc Make a background API call to FreeSWITCH and wait for reply. +%% @end +%%------------------------------------------------------------------------------ +-spec async_api(atom(), atom(), string() | binary()) -> freeswitch:fs_api_return(). +async_api(Node, Cmd, Args) -> + case bgapi(Node, Cmd, Args) of + {'error', _} = Error -> Error; + {'ok', JobId} -> + receive + {'bgok', JobId, <<"-ERR", Reason/binary>>} -> api_result('error', Reason); + {'bgok', JobId, <<"+OK", Result/binary>>} -> api_result('ok', Result); + {'bgok', JobId, Result} -> api_result('ok', Result); + {'bgerror', JobId, Error} -> api_result('error', Error) + end + end. +