From 905ddbcecd3e37cbf17a56f2f6b6cc3c50d94493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E7=A4=BC=E6=88=90?= Date: Thu, 23 Feb 2023 17:37:45 +0800 Subject: [PATCH] fix --- apps/iot/src/iot_message_handler.erl | 71 +++++++++++++--------------- apps/iot/src/iot_util.erl | 7 +-- apps/iot/src/model/host_model.erl | 2 +- 3 files changed, 37 insertions(+), 43 deletions(-) diff --git a/apps/iot/src/iot_message_handler.erl b/apps/iot/src/iot_message_handler.erl index e5edd9d..20408be 100644 --- a/apps/iot/src/iot_message_handler.erl +++ b/apps/iot/src/iot_message_handler.erl @@ -79,46 +79,39 @@ handle(<<"server.register">>, Msg = #{<<"c_id">> := ClientId, <<"r">> := PubKey, end; handle(<<"server.data">>, #{<<"c_id">> := HostId, <<"d">> := Data}) -> - {ok, Host = #host{aes = Aes}} = host_model:get_host(HostId), - Services = microservice_model:get_services(HostId), - - PlainData = iot_cipher_aes:decrypt(Aes, Aes, Data), - case jiffy:decode(PlainData, [return_maps]) of - Infos when is_list(Infos) -> - lager:debug("the data is: ~p", [Infos]), - - %% 一次可能提前多组数据 - lists:foreach(fun(#{<<"service_name">> := ServiceName, <<"data">> := Items}) -> - case lists:search(fun(#service{name = Name}) -> Name =:= ServiceName end, Services) of - {value, Service=#service{metrics = Metrics}} -> - %% 更新数据 - NMetrics = lists:foldl(fun(MetricData, MetricsAcc) -> append_metric(MetricsAcc, MetricData) end, Metrics, Items), - - - - - ok; - false -> - lager:warning("[iot_message_handler] host_id: ~p, not found service_name: ~p", [HostId, ServiceName]) - - end - - - - end, Infos), - - - - - - ok; - _ -> - lager:debug("the metric is invalid json") - end, - - - ok. + case host_model:get_host(HostId) of + {ok, #host{aes = Aes}} -> + Services = microservice_model:get_services(HostId), + PlainData = iot_cipher_aes:decrypt(Aes, Aes, Data), + case jiffy:decode(PlainData, [return_maps]) of + Infos when is_list(Infos) -> + lager:debug("[iot_message_handler] the data is: ~p", [Infos]), + %% 一次可能提前多组数据 + lists:foreach(fun(#{<<"service_name">> := ServiceName, <<"data">> := Items}) -> + case lists:search(fun(#service{name = Name}) -> Name =:= ServiceName end, Services) of + {value, #service{service_id = ServiceId, metrics = Metrics}} -> + %% 更新数据 + NMetrics = lists:foldl(fun(MetricData, MetricsAcc) -> append_metric(MetricsAcc, MetricData) end, Metrics, Items), + case service_model:update_metric(ServiceId, NMetrics) of + ok -> + lager:debug("[iot_message_handler] update metrics success"); + {ok, Reason} -> + lager:debug("[iot_message_handler] update metrics error: ~p", [Reason]) + end; + false -> + lager:warning("[iot_message_handler] host_id: ~p, not found service_name: ~p", [HostId, ServiceName]) + end + end, Infos); + _ -> + lager:debug("[iot_message_handler] the metric is invalid json") + end; + undefined -> + lager:warning("[iot_message_handler] host_id: ~p, not exists", [HostId]) +end. +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% helper methods +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% append_metric(Metrics, MetricData) when is_map(MetricData) -> Name = maps:get(<<"name">>, MetricData, <<"">>), diff --git a/apps/iot/src/iot_util.erl b/apps/iot/src/iot_util.erl index 41390d6..ec01914 100644 --- a/apps/iot/src/iot_util.erl +++ b/apps/iot/src/iot_util.erl @@ -67,10 +67,11 @@ json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) uuid() -> rand_bytes(16). -rand_bytes(Size) when is_integer(Size) -> - Bytes = crypto:strong_rand_bytes(Size), +rand_bytes(Size) when is_integer(Size), Size > 0 -> + Size1 = erlang:ceil(Size / 2), + Bytes = crypto:strong_rand_bytes(Size1), S = lists:flatten([integer_to_list(E, 16) || <> <= Bytes]), - string:to_lower(S). + lists:sublist(string:to_lower(S), 1, Size). queue_limited_in(Item, Q, Num) when is_integer(Num) -> case queue:len(Q) >= Num of diff --git a/apps/iot/src/model/host_model.erl b/apps/iot/src/model/host_model.erl index 75f91df..f515767 100644 --- a/apps/iot/src/model/host_model.erl +++ b/apps/iot/src/model/host_model.erl @@ -24,7 +24,7 @@ get_host(HostId) when is_binary(HostId) -> %% 获取app信息 -spec get_hosts(Start :: integer(), Limit :: integer()) -> - {ok, Items :: list(), Stat :: maps()} | + {ok, Items :: list(), Stat :: maps:map()} | {error, Reason :: any()}. get_hosts(Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 -> Fun = fun() ->