diff --git a/apps/iot/src/database/micro_set_bo.erl b/apps/iot/src/database/micro_set_bo.erl new file mode 100644 index 0000000..6786754 --- /dev/null +++ b/apps/iot/src/database/micro_set_bo.erl @@ -0,0 +1,19 @@ +%%%------------------------------------------------------------------- +%%% @author aresei +%%% @copyright (C) 2023, +%%% @doc +%%% +%%% @end +%%% Created : 16. 5月 2023 12:48 +%%%------------------------------------------------------------------- +-module(micro_set_bo). +-author("aresei"). +-include("iot.hrl"). + +%% API +-export([change_status/4]). + +%% 修改主机的状态 +-spec change_status(HostId :: integer(), SceneId :: integer(), MircoId :: integer(), Status :: integer()) -> {ok, AffectedRows :: integer()} | {error, Reason :: any()}. +change_status(HostId, SceneId, MircoId, Status) when is_integer(HostId), is_integer(SceneId), is_integer(MircoId), is_integer(Status) -> + mysql_client:update_by(<<"UPDATE micro_set SET status = ? WHERE host_id = ? AND scene_id = ? AND micro_id = ? LIMIT 1">>, [Status, HostId, SceneId, MircoId]). \ No newline at end of file diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index ee00f18..5be92cc 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -359,8 +359,9 @@ handle_message(#{<<"method">> := <<"inform">>, <<"params">> := Info0}, State = # Info = iot_cipher_aes:decrypt(AES, base64:decode(Info0)), case catch jiffy:decode(Info, [return_maps]) of #{<<"at">> := At, <<"services">> := ServiceInforms} -> - lists:foreach(fun(Item = #{<<"name">> := Name, <<"version">> := Version, <<"version_copy">> := VersionCopy, <<"status">> := Status}) -> - SceneId = maps:get(<<"scene_id">>, Item, 0), + lists:foreach(fun(#{<<"props">> := Props, <<"name">> := Name, <<"version">> := Version, <<"version_copy">> := VersionCopy, <<"status">> := Status}) -> + %% props 主机id:场景id:微服务id + [_, SceneId, MicroId] = binary:split(Props, <<":">>, [global]), micro_inform_log:insert(#{ <<"host_id">> => HostId, <<"scene_id">> => SceneId, @@ -370,9 +371,7 @@ handle_message(#{<<"method">> := <<"inform">>, <<"params">> := Info0}, State = # <<"status">> => Status, <<"created_at">> => At }), - - %% TODO 需要更新微服务的状态 - ok + micro_set_bo:change_status(HostId, SceneId, MicroId, Status) end, ServiceInforms); Error -> lager:warning("[iot_host] inform get error: ~p", [Error])