-module(pbx_rose). -behaviour(gen_event). -export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2, code_change/3]). -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(Id, ?STATUS, Data)}, _State) -> Status = pbx_status:decode(Data), Value = pbx_status:value(Status), io:format("Status: ~p~n", [Value]), case Value of normal -> pbx_pdu:notify({status, ?STATUS, 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({invoke, Op, {ok, Data}}, State) -> NewState = next(State), Invoke = #'Invoke'{invokeId = NewState, opcode = Op, argument = Data}, Reply = 'Remote-Operations-Generic-ROS-PDUs':encode('ROS', {invoke, Invoke}), pbx_conn:send(Reply), {ok, NewState}; handle_event({status, Op, {ok, Data}}, State) -> Result = #'ReturnResult_result'{opcode = Op, result = Data}, ReturnResult = #'ReturnResult'{invokeId = State, result = Result}, Reply = 'Remote-Operations-Generic-ROS-PDUs':encode('ROS', {returnResult, ReturnResult}), pbx_conn:send(Reply), {ok, State}; handle_event(_Event, State) -> {ok, State}. handle_call({nextInvoke}, State) -> NewState = next(State), {ok, NewState, NewState}; 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}.