From 6b1d8de3e43715136db1625850106875000889b0 Mon Sep 17 00:00:00 2001 From: anlicheng Date: Tue, 20 Jun 2023 17:56:01 +0800 Subject: [PATCH] fix host mocker --- apps/iot/src/iot_host.erl | 10 +++++++ apps/iot/src/mocker/host_mocker.erl | 45 +++++++---------------------- 2 files changed, 21 insertions(+), 34 deletions(-) diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index c647695..3c857ae 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -311,6 +311,16 @@ handle_event(cast, {handle, <>}, session, State end, {keep_state, State#state{is_answered = true}}; +handle_event(cast, {handle, <>}, 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, <>}, session, State = #state{aes = AES}) -> Info = iot_cipher_aes:decrypt(AES, Info0), case catch jiffy:decode(Info, [return_maps]) of diff --git a/apps/iot/src/mocker/host_mocker.erl b/apps/iot/src/mocker/host_mocker.erl index 031c21c..85b32c4 100644 --- a/apps/iot/src/mocker/host_mocker.erl +++ b/apps/iot/src/mocker/host_mocker.erl @@ -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, <>, 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, <>, 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, <>, 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, <>, 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, <>, 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. \ No newline at end of file