diff --git a/include/domain_model.hrl b/include/domain_model.hrl new file mode 100644 index 0000000..3c89f80 --- /dev/null +++ b/include/domain_model.hrl @@ -0,0 +1,20 @@ +%%%------------------------------------------------------------------- +%%% @author Codex +%%% @doc +%%% 领域模型定义 +%%% @end +%%%------------------------------------------------------------------- + +-record(host_info, { + id :: integer(), + uuid :: binary(), + authorize_status :: integer(), + status :: integer() +}). + +-record(device_info, { + id :: integer(), + host_id :: integer(), + device_uuid :: binary(), + status :: integer() +}). diff --git a/src/adapters/control_api/iot_api_client.erl b/src/adapters/control_api/iot_api_client.erl index 88555f8..acc758a 100644 --- a/src/adapters/control_api/iot_api_client.erl +++ b/src/adapters/control_api/iot_api_client.erl @@ -8,6 +8,7 @@ %%%------------------------------------------------------------------- -module(iot_api_client). -author("anlicheng"). +-include("domain_model.hrl"). %% API -export([ai_event/1]). @@ -31,20 +32,30 @@ get_all_hosts() -> [] end. --spec get_host_by_uuid(UUID :: binary()) -> undefined | {ok, HostInfo :: map()}. +-spec get_host_by_uuid(UUID :: binary()) -> undefined | {ok, HostInfo :: #host_info{}}. get_host_by_uuid(UUID) when is_binary(UUID) -> case do_get("/get_host_by_uuid", [{<<"uuid">>, UUID}]) of {ok, HostInfo} -> - {ok, HostInfo}; + case host_info_record(HostInfo) of + {ok, Record} -> + {ok, Record}; + error -> + undefined + end; _ -> undefined end. --spec get_host_by_id(HostId :: integer()) -> undefined | {ok, HostInfo :: map()}. +-spec get_host_by_id(HostId :: integer()) -> undefined | {ok, HostInfo :: #host_info{}}. get_host_by_id(HostId) when is_integer(HostId) -> case do_get("/get_host_by_id", [{<<"host_id">>, integer_to_binary(HostId)}]) of {ok, HostInfo} -> - {ok, HostInfo}; + case host_info_record(HostInfo) of + {ok, Record} -> + {ok, Record}; + error -> + undefined + end; _ -> undefined end. @@ -54,15 +65,27 @@ get_host_by_id(HostId) when is_integer(HostId) -> change_host_status(UUID, NStatus) when is_binary(UUID), is_integer(NStatus) -> do_post("/change_host_status", #{<<"uuid">> => UUID, <<"new_status">> => NStatus}). --spec get_host_devices(HostId :: integer()) -> {ok, Devices :: [map()]} | {error, Reason::any()}. +-spec get_host_devices(HostId :: integer()) -> {ok, Devices :: [#device_info{}]} | {error, Reason::any()}. get_host_devices(HostId) when is_integer(HostId) -> - do_get("/get_host_devices", [{<<"host_id">>, integer_to_binary(HostId)}]). + case do_get("/get_host_devices", [{<<"host_id">>, integer_to_binary(HostId)}]) of + {ok, DeviceInfos} when is_list(DeviceInfos) -> + device_info_records(DeviceInfos); + {ok, _Other} -> + {error, invalid_device_infos}; + Error -> + Error + end. --spec get_device_by_uuid(DeviceUUID :: binary()) -> {ok, DeviceInfo :: map()} | undefined. +-spec get_device_by_uuid(DeviceUUID :: binary()) -> {ok, DeviceInfo :: #device_info{}} | undefined. get_device_by_uuid(DeviceUUID) when is_binary(DeviceUUID) -> case do_get("/get_device_by_uuid", [{<<"device_uuid">>, DeviceUUID}]) of {ok, DeviceInfo} -> - {ok, DeviceInfo}; + case device_info_record(DeviceInfo) of + {ok, Record} -> + {ok, Record}; + error -> + undefined + end; _ -> undefined end. @@ -197,3 +220,40 @@ do_get(Path, Params) when is_list(Path), is_list(Params) -> logger:warning("[iot_api_client] request url: ~p, get error is: ~p", [Url, Reason]), {error, Reason} end. + +-spec host_info_record(map()) -> {ok, #host_info{}} | error. +host_info_record(#{<<"id">> := Id, <<"uuid">> := UUID, <<"authorize_status">> := AuthorizeStatus, <<"status">> := Status}) + when is_integer(Id), is_binary(UUID), is_integer(AuthorizeStatus), is_integer(Status) -> + {ok, #host_info{ + id = Id, + uuid = UUID, + authorize_status = AuthorizeStatus, + status = Status + }}; +host_info_record(_) -> + error. + +-spec device_info_record(map()) -> {ok, #device_info{}} | error. +device_info_record(#{<<"id">> := Id, <<"host_id">> := HostId, <<"device_uuid">> := DeviceUUID, <<"status">> := Status}) + when is_integer(Id), is_integer(HostId), is_binary(DeviceUUID), is_integer(Status) -> + {ok, #device_info{ + id = Id, + host_id = HostId, + device_uuid = DeviceUUID, + status = Status + }}; +device_info_record(_) -> + error. + +-spec device_info_records([map()]) -> {ok, [#device_info{}]} | {error, invalid_device_info}. +device_info_records(DeviceInfos) -> + lists:foldr(fun(DeviceInfo, Acc) -> + case {device_info_record(DeviceInfo), Acc} of + {{ok, Record}, {ok, Records}} -> + {ok, [Record | Records]}; + {error, _} -> + {error, invalid_device_info}; + {_, Error = {error, _}} -> + Error + end + end, {ok, []}, DeviceInfos). diff --git a/src/host/iot_host.erl b/src/host/iot_host.erl index af1120f..41903b7 100644 --- a/src/host/iot_host.erl +++ b/src/host/iot_host.erl @@ -9,6 +9,7 @@ -module(iot_host). -author("aresei"). -include("iot.hrl"). +-include("domain_model.hrl"). -include("protocol.hrl"). -include("message_pb.hrl"). @@ -171,7 +172,7 @@ start_link(Name, UUID) when is_atom(Name), is_binary(UUID) -> init([UUID]) -> ok = iot_log:set_metadata(), case iot_api_client:get_host_by_uuid(UUID) of - {ok, #{<<"id">> := HostId, <<"authorize_status">> := AuthorizeStatus}} -> + {ok, #host_info{id = HostId, authorize_status = AuthorizeStatus}} -> %% 通过host_id注册别名, 可以避免通过查询数据库获取HostPid AliasName = get_alias_name(HostId), global:register_name(AliasName, self()), @@ -324,7 +325,7 @@ handle_event(cast, heartbeat, _, State = #state{heartbeat_counter = HeartbeatCou %% 没有收到心跳包,主机下线, 设备状态不变 handle_event(info, {timeout, _, heartbeat_ticker}, _, State = #state{uuid = UUID, heartbeat_counter = 0, channel_pid = ChannelPid}) -> logger:warning("[iot_host] uuid: ~p, heartbeat lost, devices will unknown", [UUID]), - {ok, #{<<"status">> := Status}} = iot_api_client:get_host_by_uuid(UUID), + {ok, #host_info{status = Status}} = iot_api_client:get_host_by_uuid(UUID), case Status of ?HOST_NOT_JOINED -> logger:debug("[iot_host] host: ~p, host_maybe_offline, host not joined, can not change to offline", [UUID]); diff --git a/src/host/iot_host_sup.erl b/src/host/iot_host_sup.erl index cfc6d51..aa22324 100644 --- a/src/host/iot_host_sup.erl +++ b/src/host/iot_host_sup.erl @@ -37,7 +37,7 @@ ensured_host_started(UUID) when is_binary(UUID) -> {ok, Pid} end. -delete_host(UUID) -> +delete_host(UUID) when is_binary(UUID) -> Id = iot_host:get_name(UUID), ok = supervisor:terminate_child(?MODULE, Id), case supervisor:delete_child(?MODULE, Id) of @@ -49,7 +49,8 @@ delete_host(UUID) -> ok end. -child_spec(UUID) -> +-spec child_spec(UUID :: binary()) -> map(). +child_spec(UUID) when is_binary(UUID) -> Id = iot_host:get_name(UUID), #{id => Id, start => {iot_host, start_link, [Id, UUID]}, diff --git a/src/simulator/simulator_api_handler.erl b/src/simulator/simulator_api_handler.erl index 4e2b376..f9be473 100644 --- a/src/simulator/simulator_api_handler.erl +++ b/src/simulator/simulator_api_handler.erl @@ -10,7 +10,7 @@ -export([handle_request/4]). --define(HOST_UUID, <<"sim-host-001">>). +-define(HOST_UUID, <<"qbxmjyzrkpntfgswaevodhluicqzxplkm">>). -define(HOST_ID, 1). -define(DEVICE_UUID, <<"sim-device-001">>). -define(DEVICE_ID, 1).