fix api
This commit is contained in:
parent
939262d467
commit
78093a6b91
20
include/domain_model.hrl
Normal file
20
include/domain_model.hrl
Normal file
@ -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()
|
||||||
|
}).
|
||||||
@ -8,6 +8,7 @@
|
|||||||
%%%-------------------------------------------------------------------
|
%%%-------------------------------------------------------------------
|
||||||
-module(iot_api_client).
|
-module(iot_api_client).
|
||||||
-author("anlicheng").
|
-author("anlicheng").
|
||||||
|
-include("domain_model.hrl").
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([ai_event/1]).
|
-export([ai_event/1]).
|
||||||
@ -31,20 +32,30 @@ get_all_hosts() ->
|
|||||||
[]
|
[]
|
||||||
end.
|
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) ->
|
get_host_by_uuid(UUID) when is_binary(UUID) ->
|
||||||
case do_get("/get_host_by_uuid", [{<<"uuid">>, UUID}]) of
|
case do_get("/get_host_by_uuid", [{<<"uuid">>, UUID}]) of
|
||||||
{ok, HostInfo} ->
|
{ok, HostInfo} ->
|
||||||
{ok, HostInfo};
|
case host_info_record(HostInfo) of
|
||||||
|
{ok, Record} ->
|
||||||
|
{ok, Record};
|
||||||
|
error ->
|
||||||
|
undefined
|
||||||
|
end;
|
||||||
_ ->
|
_ ->
|
||||||
undefined
|
undefined
|
||||||
end.
|
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) ->
|
get_host_by_id(HostId) when is_integer(HostId) ->
|
||||||
case do_get("/get_host_by_id", [{<<"host_id">>, integer_to_binary(HostId)}]) of
|
case do_get("/get_host_by_id", [{<<"host_id">>, integer_to_binary(HostId)}]) of
|
||||||
{ok, HostInfo} ->
|
{ok, HostInfo} ->
|
||||||
{ok, HostInfo};
|
case host_info_record(HostInfo) of
|
||||||
|
{ok, Record} ->
|
||||||
|
{ok, Record};
|
||||||
|
error ->
|
||||||
|
undefined
|
||||||
|
end;
|
||||||
_ ->
|
_ ->
|
||||||
undefined
|
undefined
|
||||||
end.
|
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) ->
|
change_host_status(UUID, NStatus) when is_binary(UUID), is_integer(NStatus) ->
|
||||||
do_post("/change_host_status", #{<<"uuid">> => UUID, <<"new_status">> => 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) ->
|
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) ->
|
get_device_by_uuid(DeviceUUID) when is_binary(DeviceUUID) ->
|
||||||
case do_get("/get_device_by_uuid", [{<<"device_uuid">>, DeviceUUID}]) of
|
case do_get("/get_device_by_uuid", [{<<"device_uuid">>, DeviceUUID}]) of
|
||||||
{ok, DeviceInfo} ->
|
{ok, DeviceInfo} ->
|
||||||
{ok, DeviceInfo};
|
case device_info_record(DeviceInfo) of
|
||||||
|
{ok, Record} ->
|
||||||
|
{ok, Record};
|
||||||
|
error ->
|
||||||
|
undefined
|
||||||
|
end;
|
||||||
_ ->
|
_ ->
|
||||||
undefined
|
undefined
|
||||||
end.
|
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]),
|
logger:warning("[iot_api_client] request url: ~p, get error is: ~p", [Url, Reason]),
|
||||||
{error, Reason}
|
{error, Reason}
|
||||||
end.
|
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).
|
||||||
|
|||||||
@ -9,6 +9,7 @@
|
|||||||
-module(iot_host).
|
-module(iot_host).
|
||||||
-author("aresei").
|
-author("aresei").
|
||||||
-include("iot.hrl").
|
-include("iot.hrl").
|
||||||
|
-include("domain_model.hrl").
|
||||||
-include("protocol.hrl").
|
-include("protocol.hrl").
|
||||||
-include("message_pb.hrl").
|
-include("message_pb.hrl").
|
||||||
|
|
||||||
@ -171,7 +172,7 @@ start_link(Name, UUID) when is_atom(Name), is_binary(UUID) ->
|
|||||||
init([UUID]) ->
|
init([UUID]) ->
|
||||||
ok = iot_log:set_metadata(),
|
ok = iot_log:set_metadata(),
|
||||||
case iot_api_client:get_host_by_uuid(UUID) of
|
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
|
%% 通过host_id注册别名, 可以避免通过查询数据库获取HostPid
|
||||||
AliasName = get_alias_name(HostId),
|
AliasName = get_alias_name(HostId),
|
||||||
global:register_name(AliasName, self()),
|
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}) ->
|
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]),
|
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
|
case Status of
|
||||||
?HOST_NOT_JOINED ->
|
?HOST_NOT_JOINED ->
|
||||||
logger:debug("[iot_host] host: ~p, host_maybe_offline, host not joined, can not change to offline", [UUID]);
|
logger:debug("[iot_host] host: ~p, host_maybe_offline, host not joined, can not change to offline", [UUID]);
|
||||||
|
|||||||
@ -37,7 +37,7 @@ ensured_host_started(UUID) when is_binary(UUID) ->
|
|||||||
{ok, Pid}
|
{ok, Pid}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
delete_host(UUID) ->
|
delete_host(UUID) when is_binary(UUID) ->
|
||||||
Id = iot_host:get_name(UUID),
|
Id = iot_host:get_name(UUID),
|
||||||
ok = supervisor:terminate_child(?MODULE, Id),
|
ok = supervisor:terminate_child(?MODULE, Id),
|
||||||
case supervisor:delete_child(?MODULE, Id) of
|
case supervisor:delete_child(?MODULE, Id) of
|
||||||
@ -49,7 +49,8 @@ delete_host(UUID) ->
|
|||||||
ok
|
ok
|
||||||
end.
|
end.
|
||||||
|
|
||||||
child_spec(UUID) ->
|
-spec child_spec(UUID :: binary()) -> map().
|
||||||
|
child_spec(UUID) when is_binary(UUID) ->
|
||||||
Id = iot_host:get_name(UUID),
|
Id = iot_host:get_name(UUID),
|
||||||
#{id => Id,
|
#{id => Id,
|
||||||
start => {iot_host, start_link, [Id, UUID]},
|
start => {iot_host, start_link, [Id, UUID]},
|
||||||
|
|||||||
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
-export([handle_request/4]).
|
-export([handle_request/4]).
|
||||||
|
|
||||||
-define(HOST_UUID, <<"sim-host-001">>).
|
-define(HOST_UUID, <<"qbxmjyzrkpntfgswaevodhluicqzxplkm">>).
|
||||||
-define(HOST_ID, 1).
|
-define(HOST_ID, 1).
|
||||||
-define(DEVICE_UUID, <<"sim-device-001">>).
|
-define(DEVICE_UUID, <<"sim-device-001">>).
|
||||||
-define(DEVICE_ID, 1).
|
-define(DEVICE_ID, 1).
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user