This commit is contained in:
anlicheng 2023-06-15 11:37:03 +08:00
parent aa05d062b6
commit 1eb246c43c
2 changed files with 77 additions and 1 deletions

View File

@ -324,7 +324,10 @@ handle_message(#{<<"method">> := <<"inform">>, <<"params">> := Info0}, State = #
<<"version_copy">> => VersionCopy,
<<"status">> => Status,
<<"created_at">> => At
})
}),
%% TODO
ok
end, ServiceInforms);
Error ->
lager:warning("[iot_host] inform error: ~p", [Error])

View File

@ -192,6 +192,11 @@ handle_info({publish, #{payload := Payload, qos := Qos, topic := FromTopic}},
%%
% erlang:start_timer(?TICKER_INTERVAL + 1000, self(), data_ticker),
%% inform上传
erlang:start_timer(?TICKER_INTERVAL, self(), inform_ticker),
erlang:start_timer(?TICKER_INTERVAL, self(), feedback_ticker),
{noreply, State#state{aes = Aes}};
_ ->
lager:debug("[host_mocker] auth failed")
@ -259,6 +264,74 @@ handle_info({timeout, _, data_ticker}, State = #state{aes = Aes, topic = Topic})
{noreply, State};
%% inform信息
handle_info({timeout, _, inform_ticker}, State = #state{aes = Aes, topic = Topic}) ->
Info = jiffy:encode(#{
<<"at">> => iot_util:current_time(),
<<"services">> => [
#{
<<"scene_id">> => 1,
<<"name">> => <<"测试微服务"/utf8>>,
<<"version">> => <<"V1.0">>,
<<"version_copy">> => <<"CopyV1.0">>,
<<"status">> => 1
},
#{
<<"scene_id">> => 1,
<<"name">> => <<"水表"/utf8>>,
<<"version">> => <<"V1.0">>,
<<"version_copy">> => <<"CopyV1.0">>,
<<"status">> => 1
}
]
}, [force_utf8]),
Msg = jiffy:encode(#{
<<"method">> => <<"inform">>,
<<"params">> => base64:encode(iot_cipher_aes:encrypt(Aes, Info))
}, [force_utf8]),
{ok, Ref} = iot_mqtt_publisher:publish(Topic, Msg, 1),
receive
{ok, Ref, PacketId} ->
lager:debug("[host_mocker] send inform success, packet_id: ~p", [PacketId]);
{error, Reason} ->
lager:debug("[host_mocker] send inform failed, reason: ~p", [Reason])
end,
erlang:start_timer(?TICKER_INTERVAL + 2000 + rand:uniform(5000), self(), data_ticker),
{noreply, State};
%% feedback_result信息
handle_info({timeout, _, feedback_ticker}, State = #state{aes = Aes, topic = Topic}) ->
Info = jiffy:encode(#{
<<"task_id">> => 1,
<<"task_type">> => 2,
<<"code">> => 200,
<<"reason">> => <<"ok">>,
<<"error">> => <<"">>,
<<"created_at">> => iot_util:current_time()
}, [force_utf8]),
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),
receive
{ok, Ref, PacketId} ->
lager:debug("[host_mocker] send inform success, packet_id: ~p", [PacketId]);
{error, Reason} ->
lager:debug("[host_mocker] send inform failed, reason: ~p", [Reason])
end,
erlang:start_timer(?TICKER_INTERVAL + 2000 + rand:uniform(5000), self(), data_ticker),
{noreply, State};
%% ping逻辑
handle_info({timeout, _, ping_ticker}, State = #state{aes = Aes, topic = Topic}) ->
Metric = jiffy:encode(#{