From 1eb246c43c082a0e209ebd575084433d689fe9d1 Mon Sep 17 00:00:00 2001 From: anlicheng Date: Thu, 15 Jun 2023 11:37:03 +0800 Subject: [PATCH] fix --- apps/iot/src/iot_host.erl | 5 +- apps/iot/src/mocker/host_mocker.erl | 73 +++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 0d6e724..71a0108 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -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]) diff --git a/apps/iot/src/mocker/host_mocker.erl b/apps/iot/src/mocker/host_mocker.erl index 5c75859..3d20a92 100644 --- a/apps/iot/src/mocker/host_mocker.erl +++ b/apps/iot/src/mocker/host_mocker.erl @@ -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(#{