fix host mocker

This commit is contained in:
anlicheng 2023-06-20 17:56:01 +08:00
parent 50ee83feba
commit 6b1d8de3e4
2 changed files with 21 additions and 34 deletions

View File

@ -311,6 +311,16 @@ handle_event(cast, {handle, <<?METHOD_INFORM:8, Info0/binary>>}, session, State
end,
{keep_state, State#state{is_answered = true}};
handle_event(cast, {handle, <<?METHOD_FEEDBACK_STEP:8, Info0/binary>>}, session, State = #state{aes = AES}) ->
Info = iot_cipher_aes:decrypt(AES, Info0),
case catch jiffy:decode(Info, [return_maps]) of
Data when is_map(Data) ->
lager:warning("[iot_host] feedback_step info: ~p", [Data]);
Other ->
lager:warning("[iot_host] feedback_step error: ~p", [Other])
end,
{keep_state, State};
handle_event(cast, {handle, <<?METHOD_FEEDBACK_RESULT:8, Info0/binary>>}, session, State = #state{aes = AES}) ->
Info = iot_cipher_aes:decrypt(AES, Info0),
case catch jiffy:decode(Info, [return_maps]) of

View File

@ -8,6 +8,7 @@
%%%-------------------------------------------------------------------
-module(host_mocker).
-author("aresei").
-include("iot.hrl").
-behaviour(gen_server).
@ -157,7 +158,7 @@ handle_info({disconnect, ReasonCode, Properties}, State = #state{}) ->
{stop, disconnected, State};
%% json反序列需要在host进程进行
handle_info({publish, #{payload := Payload, qos := Qos, topic := FromTopic}},
State = #state{topic = Topic, pub_key = PubKey, pri_key = PrivateKey, aes = Aes0}) ->
State = #state{topic = Topic, pri_key = PrivateKey, aes = Aes0}) ->
lager:debug("[host_mocker] Recv a publish packet: ~p, qos: ~p, from topic: ~p", [Payload, Qos, FromTopic]),
case Payload of
@ -250,14 +251,7 @@ handle_info({puback, Packet = #{packet_id := _PacketId}}, State = #state{}) ->
%% iot的会话
handle_info(create_session, State = #state{topic = Topic, pub_key = PubKey}) ->
Req = jiffy:encode(#{
<<"method">> => <<"create_session">>,
<<"params">> => #{
<<"pub_key">> => PubKey
}
}, [force_utf8]),
{ok, Ref} = iot_mqtt_publisher:publish(Topic, Req, 1),
{ok, Ref} = iot_mqtt_publisher:publish(Topic, <<?METHOD_CREATE_SESSION:8, PubKey/binary>>, 1),
receive
{ok, Ref, PacketId} ->
lager:debug("[host_mocker] send create_session success, packet_id: ~p", [PacketId]);
@ -288,13 +282,9 @@ handle_info({timeout, _, data_ticker}, State = #state{aes = Aes, topic = Topic})
],
Info = jiffy:encode(InfoList),
EncInfo = iot_cipher_aes:encrypt(Aes, Info),
Msg = jiffy:encode(#{
<<"method">> => <<"data">>,
<<"params">> => base64:encode(iot_cipher_aes:encrypt(Aes, Info))
}, [force_utf8]),
{ok, Ref} = iot_mqtt_publisher:publish(Topic, Msg, 1),
{ok, Ref} = iot_mqtt_publisher:publish(Topic, <<?METHOD_DATA:8, EncInfo/binary>>, 1),
receive
{ok, Ref, PacketId} ->
lager:debug("[host_mocker] send data success, packet_id: ~p", [PacketId]);
@ -327,12 +317,9 @@ handle_info({timeout, _, inform_ticker}, State = #state{aes = Aes, topic = Topic
]
}, [force_utf8]),
Msg = jiffy:encode(#{
<<"method">> => <<"inform">>,
<<"params">> => base64:encode(iot_cipher_aes:encrypt(Aes, Info))
}, [force_utf8]),
EncInfo = iot_cipher_aes:encrypt(Aes, Info),
{ok, Ref} = iot_mqtt_publisher:publish(Topic, Msg, 1),
{ok, Ref} = iot_mqtt_publisher:publish(Topic, <<?METHOD_INFORM:8, EncInfo/binary>>, 1),
receive
{ok, Ref, PacketId} ->
lager:debug("[host_mocker] send inform success, packet_id: ~p", [PacketId]);
@ -352,13 +339,9 @@ handle_info({timeout, _, feedback_ticker}, State = #state{aes = Aes, topic = Top
<<"error">> => <<"">>,
<<"time">> => iot_util:current_time()
}, [force_utf8]),
EncInfo = iot_cipher_aes:encrypt(Aes, Info),
Msg = jiffy:encode(#{
<<"method">> => <<"feedback_result">>,
<<"params">> => base64:encode(iot_cipher_aes:encrypt(Aes, Info))
}, [force_utf8]),
{ok, Ref} = iot_mqtt_publisher:publish(Topic, Msg, 1),
{ok, Ref} = iot_mqtt_publisher:publish(Topic, <<?METHOD_FEEDBACK_RESULT:8, EncInfo/binary>>, 1),
receive
{ok, Ref, PacketId} ->
lager:debug("[host_mocker] send inform success, packet_id: ~p", [PacketId]);
@ -393,12 +376,7 @@ handle_info({timeout, _, ping_ticker}, State = #state{aes = Aes, topic = Topic})
Data = iot_cipher_aes:encrypt(Aes, Metric),
Msg = jiffy:encode(#{
<<"method">> => <<"ping">>,
<<"params">> => base64:encode(Data)
}, [force_utf8]),
{ok, Ref} = iot_mqtt_publisher:publish(Topic, Msg, 1),
{ok, Ref} = iot_mqtt_publisher:publish(Topic, <<?METHOD_PING:8, Data/binary>>, 1),
receive
{ok, Ref, PacketId} ->
lager:debug("[host_mocker] send ping success, packet_id: ~p", [PacketId]);
@ -432,5 +410,4 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
handle_command(Type, Info, State) ->
lager:debug("[host_mocker] command type: ~p, is: ~p", [Type, Info]),
State.
State.