-module(pbx_rose). -behaviour(gen_event). -export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2, code_change/3]). -export([invoke/2]). -include("opcodes.hrl"). -include("Remote-Operations-Generic-ROS-PDUs.hrl"). -define(INVOKE(Id, Op, Data), #'Invoke'{invokeId = Id, opcode = Op, argument = Data}). -define(RESULT(Id, Op, Data), #'ReturnResult'{invokeId = Id, result = #'ReturnResult_result'{opcode = Op, result = Data}}). %%% init(_Args) -> {ok, {present, 0}}. handle_event({invoke, Invoke = ?INVOKE(Id, ?STATUS, Data)}, _State) -> Status = pbx_status:decode(Data), io:format("Status: ~p~n", [Status]), case pbx_status:value(Status) of normal -> reply(Invoke, pbx_status:encode()); _ -> ok end, {ok, Id}; handle_event({invoke, ?INVOKE(Id, ?EVENT, Data)}, _State) -> {ok, Event} = pbx_event:decode(Data), io:format("Event: ~p~n", [Event]), {ok, Id}; handle_event({invoke, ?INVOKE(Id, ?ESCAPE, Data)}, _State) -> Esc = pbx_escape:decode(Data), Priv = pbx_escape:privateData(Esc), io:format("Escape: ~p~n", [Priv]), {ok, Id}; handle_event({invoke, Invoke = #'Invoke'{invokeId = Id}}, _State) -> io:format("Invoke ~p~n", [Invoke]), {ok, Id}; handle_event({returnResult, ?RESULT(Id, ?MAKECALL, Data)}, _State) -> {ok, Result} = pbx_dial:decode(Data), io:format("Makecall ~p~n", [Result]), {ok, Id}; handle_event({returnResult, ?RESULT(Id, ?BUTTON, Data)}, _State) -> {ok, Result} = pbx_button:decode(Data), io:format("Button ~p~n", [Result]), {ok, Id}; handle_event({returnResult, ?RESULT(Id, ?MONITOR, Data)}, _State) -> {ok, Result} = pbx_monitor:decode(Data), io:format("Monitor ~p~n", [Result]), {ok, Id}; handle_event({returnResult, ?RESULT(Id, ?SNAPSHOT, Data)}, _State) -> {ok, Result} = pbx_snapshot:decode(Data), io:format("Snapshot ~p~n", [Result]), {ok, Id}; handle_event({returnResult, #'ReturnResult'{invokeId = Id, result = Result}}, _State) -> io:format("Result ~p~n", [Result]), {ok, Id}; handle_event({returnError, #'ReturnError'{invokeId = Id, errcode = ?ERROR, parameter = Data}}, _State) -> {ok, Error} = error:decode(Data), io:format("Error: ~p~n", [Error]), {ok, Id}; handle_event({returnError, ReturnError = #'ReturnError'{invokeId = Id}}, _State) -> io:format("ReturnError ~p~n", [ReturnError]), {ok, Id}; handle_event({reject, #'Reject'{invokeId = Id, problem = Problem}}, _State) -> io:format("Reject ~p~n", [Problem]), {ok, Id}; handle_event(_Event, State) -> {ok, State}. handle_call(nextInvoke, State) -> {ok, next(State), next(State)}; handle_call(_Request, State) -> {ok, ok, State}. handle_info(_Info, State) -> {ok, State}. terminate(_Reason, _State) -> ok. code_change(_OldVsn, State, _Extra) -> {ok, State}. %%% next({present, N}) -> {present, N+1}. reply(Invoke = #'Invoke'{}, Data) -> pbx_conn:send(return(Invoke, Data)). return(#'Invoke'{invokeId = Id, opcode = Op}, {ok, Data}) -> Result = #'ReturnResult_result'{opcode = Op, result = Data}, ReturnResult = #'ReturnResult'{invokeId = Id, result = Result}, 'Remote-Operations-Generic-ROS-PDUs':encode('ROS', {returnResult, ReturnResult}). invoke(Op, {ok, Data}) -> Id = pbx_pdu:call(?MODULE, nextInvoke), Invoke = #'Invoke'{invokeId = Id, opcode = Op, argument = Data}, 'Remote-Operations-Generic-ROS-PDUs':encode('ROS', {invoke, Invoke}).