Compare commits
No commits in common. "6a51ce86273b11fa3001d91d535fdc8ef0c45b33" and "dad124edb1d69e617ad962bff325e26df3486d6e" have entirely different histories.
6a51ce8627
...
dad124edb1
@ -189,13 +189,14 @@ handle_event({call, From}, dropped_message_count, _StateName, State = #state{dro
|
|||||||
{keep_state, State, [{reply, From, DroppedCount}]};
|
{keep_state, State, [{reply, From, DroppedCount}]};
|
||||||
|
|
||||||
%% 异步建立到服务器的连接
|
%% 异步建立到服务器的连接
|
||||||
handle_event(info, {timeout, _, create_transport}, ?STATE_DISCONNECTED, State = #state{next_pkt_id = PktId}) ->
|
handle_event(info, {timeout, _, create_transport}, ?STATE_DISCONNECTED, State) ->
|
||||||
case connect_socket() of
|
case connect_socket() of
|
||||||
{ok, Socket} ->
|
{ok, Socket} ->
|
||||||
AuthPacket = auth_packet(PktId),
|
{Ref, State1} = next_packet_id(State),
|
||||||
|
AuthPacket = auth_packet(Ref),
|
||||||
ok = ssl:send(Socket, AuthPacket),
|
ok = ssl:send(Socket, AuthPacket),
|
||||||
logger:debug("[efka_iot_client] send auth request, packet_id: ~p", [PktId]),
|
logger:debug("[efka_iot_client] send auth request, ref: ~p", [Ref]),
|
||||||
{next_state, ?STATE_AUTH, State#state{socket = Socket, auth_ref = PktId, next_pkt_id = PktId + 1}, [{state_timeout, 5000, auth_timeout}]};
|
{next_state, ?STATE_AUTH, State1#state{socket = Socket, auth_ref = Ref}, [{state_timeout, 5000, auth_timeout}]};
|
||||||
{error, _Reason} ->
|
{error, _Reason} ->
|
||||||
schedule_reconnect(),
|
schedule_reconnect(),
|
||||||
{keep_state, State#state{socket = undefined}}
|
{keep_state, State#state{socket = undefined}}
|
||||||
@ -451,6 +452,10 @@ decode_pb_frame(Payload, MsgName) ->
|
|||||||
{error, {bad_protobuf, MsgName, Class, Reason}}
|
{error, {bad_protobuf, MsgName, Class, Reason}}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
-spec next_packet_id(#state{}) -> {pos_integer(), #state{}}.
|
||||||
|
next_packet_id(State = #state{next_pkt_id = PacketId}) ->
|
||||||
|
{PacketId, State#state{next_pkt_id = PacketId + 1}}.
|
||||||
|
|
||||||
-spec connect_socket() -> {ok, ssl:sslsocket()} | {error, term()}.
|
-spec connect_socket() -> {ok, ssl:sslsocket()} | {error, term()}.
|
||||||
connect_socket() ->
|
connect_socket() ->
|
||||||
{ok, Props} = application:get_env(efka, iot_server),
|
{ok, Props} = application:get_env(efka, iot_server),
|
||||||
@ -551,7 +556,7 @@ handle_container_request(Ref, Request, Socket) ->
|
|||||||
decode_container_command(#'Command.Container'{action = {list, #'Command.Container.ContainerList'{}}}) ->
|
decode_container_command(#'Command.Container'{action = {list, #'Command.Container.ContainerList'{}}}) ->
|
||||||
#{<<"action">> => <<"list">>};
|
#{<<"action">> => <<"list">>};
|
||||||
decode_container_command(#'Command.Container'{action = {deploy, #'Command.Container.ContainerDeploy'{task_id = TaskId, params = Params}}}) ->
|
decode_container_command(#'Command.Container'{action = {deploy, #'Command.Container.ContainerDeploy'{task_id = TaskId, params = Params}}}) ->
|
||||||
#{<<"action">> => <<"deploy">>, <<"task_id">> => TaskId, <<"params">> => decode_json_payload(Params)};
|
#{<<"action">> => <<"deploy">>, <<"task_id">> => TaskId, <<"params">> => decode_term_payload(Params)};
|
||||||
decode_container_command(#'Command.Container'{action = {start, #'Command.Container.ContainerStart'{target = Target}}}) ->
|
decode_container_command(#'Command.Container'{action = {start, #'Command.Container.ContainerStart'{target = Target}}}) ->
|
||||||
#{<<"action">> => <<"start">>, <<"target">> => decode_container_target(Target)};
|
#{<<"action">> => <<"start">>, <<"target">> => decode_container_target(Target)};
|
||||||
decode_container_command(#'Command.Container'{action = {stop, #'Command.Container.ContainerStop'{target = Target, timeout_seconds = TimeoutSeconds}}}) ->
|
decode_container_command(#'Command.Container'{action = {stop, #'Command.Container.ContainerStop'{target = Target, timeout_seconds = TimeoutSeconds}}}) ->
|
||||||
@ -571,14 +576,14 @@ decode_container_target(#'Command.Container.ContainerTarget'{name = Name, id = I
|
|||||||
decode_container_target(undefined) ->
|
decode_container_target(undefined) ->
|
||||||
#{<<"name">> => <<>>, <<"id">> => <<>>}.
|
#{<<"name">> => <<>>, <<"id">> => <<>>}.
|
||||||
|
|
||||||
-spec decode_json_payload(iodata()) -> term().
|
-spec decode_term_payload(iodata()) -> term().
|
||||||
decode_json_payload(Payload) ->
|
decode_term_payload(Payload) ->
|
||||||
Bin = iolist_to_binary(Payload),
|
Bin = iolist_to_binary(Payload),
|
||||||
try json:decode(Bin) of
|
try binary_to_term(Bin, [safe]) of
|
||||||
Term ->
|
Term ->
|
||||||
Term
|
Term
|
||||||
catch
|
catch
|
||||||
_:_ ->
|
error:_ ->
|
||||||
Bin
|
Bin
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user