remove devie
This commit is contained in:
parent
25bb8b0514
commit
2a138da787
@ -1,50 +0,0 @@
|
|||||||
%%%-------------------------------------------------------------------
|
|
||||||
%%% @author licheng5
|
|
||||||
%%% @copyright (C) 2020, <COMPANY>
|
|
||||||
%%% @doc
|
|
||||||
%%%
|
|
||||||
%%% @end
|
|
||||||
%%% Created : 26. 4月 2020 3:36 下午
|
|
||||||
%%%-------------------------------------------------------------------
|
|
||||||
-module(device_handler).
|
|
||||||
-author("licheng5").
|
|
||||||
-include("iot.hrl").
|
|
||||||
|
|
||||||
%% API
|
|
||||||
-export([handle_request/4]).
|
|
||||||
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
||||||
%% helper methods
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
||||||
|
|
||||||
%% 重新加载对应的主机信息
|
|
||||||
handle_request("POST", "/device/reload", _, #{<<"host_id">> := HostId, <<"device_uuid">> := DeviceUUID}) when is_integer(HostId), is_binary(DeviceUUID) ->
|
|
||||||
lager:debug("[device_handler] host_id: ~p, will reload device uuid: ~p", [HostId, DeviceUUID]),
|
|
||||||
AliasName = iot_host:get_alias_name(HostId),
|
|
||||||
case global:whereis_name(AliasName) of
|
|
||||||
undefined ->
|
|
||||||
{ok, 200, iot_util:json_error(404, <<"reload device failed">>)};
|
|
||||||
HostPid when is_pid(HostPid) ->
|
|
||||||
case iot_host:reload_device(HostPid, DeviceUUID) of
|
|
||||||
ok ->
|
|
||||||
{ok, 200, iot_util:json_data(<<"success">>)};
|
|
||||||
{error, Reason} ->
|
|
||||||
lager:debug("[device_handler] reload device: ~p, get error: ~p", [DeviceUUID, Reason]),
|
|
||||||
{ok, 200, iot_util:json_error(404, <<"reload device failed">>)}
|
|
||||||
end
|
|
||||||
end;
|
|
||||||
|
|
||||||
%% 删除对应的主机信息
|
|
||||||
handle_request("POST", "/device/delete", _, #{<<"host_id">> := HostId, <<"device_uuid">> := DeviceUUID}) when is_integer(HostId), is_binary(DeviceUUID) ->
|
|
||||||
AliasName = iot_host:get_alias_name(HostId),
|
|
||||||
case global:whereis_name(AliasName) of
|
|
||||||
undefined ->
|
|
||||||
{ok, 200, iot_util:json_error(404, <<"delete device failed">>)};
|
|
||||||
HostPid when is_pid(HostPid) ->
|
|
||||||
ok = iot_host:delete_device(HostPid, DeviceUUID),
|
|
||||||
{ok, 200, iot_util:json_data(<<"success">>)}
|
|
||||||
end;
|
|
||||||
|
|
||||||
handle_request(_, Path, _, _) ->
|
|
||||||
Path1 = list_to_binary(Path),
|
|
||||||
{ok, 200, iot_util:json_error(-1, <<"url: ", Path1/binary, " not found">>)}.
|
|
||||||
@ -49,7 +49,7 @@ start_http_server() ->
|
|||||||
{'_', [
|
{'_', [
|
||||||
{"/host/[...]", http_protocol, [host_handler]},
|
{"/host/[...]", http_protocol, [host_handler]},
|
||||||
{"/container/[...]", http_protocol, [container_handler]},
|
{"/container/[...]", http_protocol, [container_handler]},
|
||||||
{"/device/[...]", http_protocol, [device_handler]},
|
{"/endpoint/[...]", http_protocol, [endpoint_handler]},
|
||||||
{"/event_stream", event_stream_handler, []}
|
{"/event_stream", event_stream_handler, []}
|
||||||
]}
|
]}
|
||||||
]),
|
]),
|
||||||
|
|||||||
@ -1,73 +0,0 @@
|
|||||||
%%%-------------------------------------------------------------------
|
|
||||||
%%% @copyright (C) 2023, <COMPANY>
|
|
||||||
%%% @doc
|
|
||||||
%%%
|
|
||||||
%%% @end
|
|
||||||
%%% Created : 14. 8月 2023 11:40
|
|
||||||
%%%-------------------------------------------------------------------
|
|
||||||
-module(iot_device).
|
|
||||||
-author("aresei").
|
|
||||||
-include("iot.hrl").
|
|
||||||
|
|
||||||
%% API
|
|
||||||
-export([new/1, change_status/2, reload/1]).
|
|
||||||
|
|
||||||
%% 终端是否授权
|
|
||||||
-define(DEVICE_AUTH_DENIED, 0).
|
|
||||||
-define(DEVICE_AUTH_AUTHED, 1).
|
|
||||||
|
|
||||||
%% 状态
|
|
||||||
-define(STATE_DENIED, denied).
|
|
||||||
-define(STATE_ACTIVATED, activated).
|
|
||||||
|
|
||||||
-record(device, {
|
|
||||||
device_uuid :: binary(),
|
|
||||||
status = ?DEVICE_OFFLINE
|
|
||||||
}).
|
|
||||||
|
|
||||||
%%%===================================================================
|
|
||||||
%%% API
|
|
||||||
%%%===================================================================
|
|
||||||
|
|
||||||
-spec new(DeviceInfo :: binary() | map()) -> error | {ok, Device :: #device{}}.
|
|
||||||
new(DeviceUUID) when is_binary(DeviceUUID) ->
|
|
||||||
case iot_api:get_device_by_uuid(DeviceUUID) of
|
|
||||||
{ok, #{<<"device_uuid">> := DeviceUUID, <<"status">> := Status}} ->
|
|
||||||
{ok, #device{device_uuid = DeviceUUID, status = Status}};
|
|
||||||
undefined ->
|
|
||||||
lager:warning("[iot_device] device uuid: ~p, loaded from mysql failed", [DeviceUUID]),
|
|
||||||
error
|
|
||||||
end;
|
|
||||||
new(#{<<"device_uuid">> := DeviceUUID, <<"status">> := Status}) ->
|
|
||||||
{ok, #device{device_uuid = DeviceUUID, status = Status}}.
|
|
||||||
|
|
||||||
-spec change_status(Device :: #device{}, NewStatus :: integer()) -> NDevice :: #device{}.
|
|
||||||
change_status(Device = #device{status = Status}, NewStatus) when is_integer(NewStatus), Status =:= NewStatus ->
|
|
||||||
Device;
|
|
||||||
change_status(Device = #device{device_uuid = DeviceUUID}, ?DEVICE_ONLINE) ->
|
|
||||||
iot_api:change_device_status(DeviceUUID, ?DEVICE_ONLINE),
|
|
||||||
Device#device{status = ?DEVICE_ONLINE};
|
|
||||||
change_status(Device = #device{device_uuid = DeviceUUID}, ?DEVICE_OFFLINE) ->
|
|
||||||
{ok, #{<<"status">> := Status}} = iot_api:get_device_by_uuid(DeviceUUID),
|
|
||||||
case Status of
|
|
||||||
?DEVICE_NOT_JOINED ->
|
|
||||||
lager:debug("[iot_device] device: ~p, device_maybe_offline, not joined, can not change to offline", [DeviceUUID]),
|
|
||||||
Device#device{status = ?DEVICE_NOT_JOINED};
|
|
||||||
?DEVICE_OFFLINE ->
|
|
||||||
lager:debug("[iot_device] device: ~p, device_maybe_offline, is offline, do nothing", [DeviceUUID]),
|
|
||||||
Device#device{status = ?DEVICE_OFFLINE};
|
|
||||||
?DEVICE_ONLINE ->
|
|
||||||
iot_api:change_device_status(DeviceUUID, ?DEVICE_OFFLINE),
|
|
||||||
Device#device{status = ?DEVICE_OFFLINE}
|
|
||||||
end.
|
|
||||||
|
|
||||||
-spec reload(Device :: #device{}) -> error | {ok, NDevice :: #device{}}.
|
|
||||||
reload(Device = #device{device_uuid = DeviceUUID}) ->
|
|
||||||
lager:debug("[iot_device] will reload: ~p", [DeviceUUID]),
|
|
||||||
case iot_api:get_device_by_uuid(DeviceUUID) of
|
|
||||||
{ok, #{<<"status">> := Status}} ->
|
|
||||||
{ok, Device#device{device_uuid = DeviceUUID, status = Status}};
|
|
||||||
undefined ->
|
|
||||||
lager:warning("[iot_device] device uuid: ~p, loaded from mysql failed", [DeviceUUID]),
|
|
||||||
error
|
|
||||||
end.
|
|
||||||
@ -26,8 +26,6 @@
|
|||||||
%% 通讯相关
|
%% 通讯相关
|
||||||
-export([pub/3, attach_channel/2, command/3]).
|
-export([pub/3, attach_channel/2, command/3]).
|
||||||
-export([deploy_container/3, start_container/2, stop_container/2, remove_container/2, kill_container/2, config_container/3, get_containers/1, await_reply/2]).
|
-export([deploy_container/3, start_container/2, stop_container/2, remove_container/2, kill_container/2, config_container/3, get_containers/1, await_reply/2]).
|
||||||
%% 设备管理
|
|
||||||
-export([reload_device/2, delete_device/2]).
|
|
||||||
-export([heartbeat/1]).
|
-export([heartbeat/1]).
|
||||||
|
|
||||||
%% gen_statem callbacks
|
%% gen_statem callbacks
|
||||||
@ -42,10 +40,6 @@
|
|||||||
heartbeat_counter = 0 :: integer(),
|
heartbeat_counter = 0 :: integer(),
|
||||||
%% websocket相关
|
%% websocket相关
|
||||||
channel_pid :: undefined | pid(),
|
channel_pid :: undefined | pid(),
|
||||||
|
|
||||||
%% 设备的关系, #{device_uuid => Device}
|
|
||||||
device_map = #{},
|
|
||||||
|
|
||||||
%% 主机的相关信息
|
%% 主机的相关信息
|
||||||
metrics = #{} :: map()
|
metrics = #{} :: map()
|
||||||
}).
|
}).
|
||||||
@ -150,15 +144,6 @@ pub(Pid, Topic, Content) when is_pid(Pid), is_binary(Topic), is_binary(Content)
|
|||||||
command(Pid, CommandType, Command) when is_pid(Pid), is_integer(CommandType), is_binary(Command) ->
|
command(Pid, CommandType, Command) when is_pid(Pid), is_integer(CommandType), is_binary(Command) ->
|
||||||
gen_statem:call(Pid, {command, CommandType, Command}).
|
gen_statem:call(Pid, {command, CommandType, Command}).
|
||||||
|
|
||||||
%% 设备管理相关
|
|
||||||
-spec reload_device(Pid :: pid(), DeviceUUID :: binary()) -> ok | {error, Reason :: any()}.
|
|
||||||
reload_device(Pid, DeviceUUID) when is_pid(Pid), is_binary(DeviceUUID) ->
|
|
||||||
gen_statem:call(Pid, {reload_device, DeviceUUID}).
|
|
||||||
|
|
||||||
-spec delete_device(Pid :: pid(), DeviceUUID :: binary()) -> ok.
|
|
||||||
delete_device(Pid, DeviceUUID) when is_pid(Pid), is_binary(DeviceUUID) ->
|
|
||||||
gen_statem:call(Pid, {delete_device, DeviceUUID}).
|
|
||||||
|
|
||||||
-spec heartbeat(Pid :: pid()) -> no_return().
|
-spec heartbeat(Pid :: pid()) -> no_return().
|
||||||
heartbeat(undefined) ->
|
heartbeat(undefined) ->
|
||||||
ok;
|
ok;
|
||||||
@ -193,19 +178,7 @@ init([UUID]) ->
|
|||||||
true -> ?STATE_ACTIVATED;
|
true -> ?STATE_ACTIVATED;
|
||||||
false -> ?STATE_DENIED
|
false -> ?STATE_DENIED
|
||||||
end,
|
end,
|
||||||
|
{ok, StateName, #state{host_id = HostId, uuid = UUID, has_session = false}};
|
||||||
%% 加载设备信息
|
|
||||||
{ok, DeviceInfos} = iot_api:get_host_devices(HostId),
|
|
||||||
Devices = lists:filtermap(fun(DeviceInfo = #{<<"device_uuid">> := DeviceUUID}) ->
|
|
||||||
case iot_device:new(DeviceInfo) of
|
|
||||||
error ->
|
|
||||||
false;
|
|
||||||
{ok, Device} ->
|
|
||||||
{true, {DeviceUUID, Device}}
|
|
||||||
end
|
|
||||||
end, DeviceInfos),
|
|
||||||
|
|
||||||
{ok, StateName, #state{host_id = HostId, uuid = UUID, device_map = maps:from_list(Devices), has_session = false}};
|
|
||||||
undefined ->
|
undefined ->
|
||||||
lager:warning("[iot_host] host uuid: ~p, loaded from mysql failed", [UUID]),
|
lager:warning("[iot_host] host uuid: ~p, loaded from mysql failed", [UUID]),
|
||||||
ignore
|
ignore
|
||||||
@ -318,39 +291,13 @@ handle_event({call, From}, {attach_channel, _}, _, State = #state{uuid = UUID, c
|
|||||||
lager:notice("[iot_host] attach_channel host_id uuid: ~p, old channel exists: ~p", [UUID, OldChannelPid]),
|
lager:notice("[iot_host] attach_channel host_id uuid: ~p, old channel exists: ~p", [UUID, OldChannelPid]),
|
||||||
{keep_state, State, [{reply, From, {error, <<"channel existed">>}}]};
|
{keep_state, State, [{reply, From, {error, <<"channel existed">>}}]};
|
||||||
|
|
||||||
%% 重新加载设备信息
|
%% 数据分发
|
||||||
handle_event({call, From}, {reload_device, DeviceUUID}, _, State = #state{device_map = DeviceMap}) ->
|
|
||||||
case maps:find(DeviceUUID, DeviceMap) of
|
|
||||||
error ->
|
|
||||||
{keep_state, State, [{reply, From, {error, <<"device not found">>}}]};
|
|
||||||
{ok, Device} ->
|
|
||||||
case iot_device:reload(Device) of
|
|
||||||
error ->
|
|
||||||
{keep_state, State#state{device_map = maps:remove(Device, DeviceMap)}, [{reply, From, {error, <<"reload device error">>}}]};
|
|
||||||
{ok, NDevice} ->
|
|
||||||
{keep_state, State#state{device_map = maps:put(DeviceUUID, NDevice, DeviceMap)}, [{reply, From, ok}]}
|
|
||||||
end
|
|
||||||
end;
|
|
||||||
|
|
||||||
%% 删除设备
|
|
||||||
handle_event({call, From}, {delete_device, DeviceUUID}, _, State = #state{device_map = DeviceMap}) ->
|
|
||||||
{keep_state, State#state{device_map = maps:remove(DeviceUUID, DeviceMap)}, [{reply, From, ok}]};
|
|
||||||
|
|
||||||
%% todo
|
|
||||||
handle_event(cast, {handle, {data, #data{service_id = ServiceId, device_uuid = DeviceUUID, route_key = RouteKey0, metric = Metric}}}, ?STATE_ACTIVATED,
|
handle_event(cast, {handle, {data, #data{service_id = ServiceId, device_uuid = DeviceUUID, route_key = RouteKey0, metric = Metric}}}, ?STATE_ACTIVATED,
|
||||||
State = #state{uuid = UUID, has_session = true, device_map = DeviceMap}) ->
|
State = #state{uuid = UUID, has_session = true}) ->
|
||||||
|
|
||||||
lager:debug("[iot_host] metric_data host: ~p, service_id: ~p, device_uuid: ~p, route_key: ~p, metric: ~p", [UUID, ServiceId, DeviceUUID, RouteKey0, Metric]),
|
lager:debug("[iot_host] metric_data host: ~p, service_id: ~p, device_uuid: ~p, route_key: ~p, metric: ~p", [UUID, ServiceId, DeviceUUID, RouteKey0, Metric]),
|
||||||
case maps:find(DeviceUUID, DeviceMap) of
|
RouteKey = get_route_key(RouteKey0),
|
||||||
error ->
|
endpoint_subscription:publish(RouteKey, ServiceId, Metric),
|
||||||
lager:warning("[iot_host] host uuid: ~p, device uuid: ~p not found, metric: ~p", [UUID, DeviceUUID, Metric]),
|
{keep_state, State};
|
||||||
{keep_state, State};
|
|
||||||
{ok, Device} ->
|
|
||||||
RouteKey = get_route_key(RouteKey0),
|
|
||||||
endpoint_subscription:publish(RouteKey, ServiceId, Metric),
|
|
||||||
NDevice = iot_device:change_status(Device, ?DEVICE_ONLINE),
|
|
||||||
{keep_state, State#state{device_map = maps:put(DeviceUUID, NDevice, DeviceMap)}}
|
|
||||||
end;
|
|
||||||
|
|
||||||
%% ping的数据是通过aes加密后的,因此需要在有会话的情况下才行
|
%% ping的数据是通过aes加密后的,因此需要在有会话的情况下才行
|
||||||
handle_event(cast, {handle, {ping, Metrics}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, has_session = true}) ->
|
handle_event(cast, {handle, {ping, Metrics}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, has_session = true}) ->
|
||||||
@ -359,9 +306,6 @@ handle_event(cast, {handle, {ping, Metrics}}, ?STATE_ACTIVATED, State = #state{u
|
|||||||
|
|
||||||
handle_event(cast, {handle, {event, #event{service_id = ServiceId, event_type = EventType, params = Params}}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, has_session = true}) ->
|
handle_event(cast, {handle, {event, #event{service_id = ServiceId, event_type = EventType, params = Params}}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, has_session = true}) ->
|
||||||
lager:debug("[iot_host] event uuid: ~p, service_id: ~p, event_type: ~p, params: ~p", [UUID, ServiceId, EventType, Params]),
|
lager:debug("[iot_host] event uuid: ~p, service_id: ~p, event_type: ~p, params: ~p", [UUID, ServiceId, EventType, Params]),
|
||||||
%DevicePid = iot_device:get_pid(DeviceUUID),
|
|
||||||
%iot_device:change_status(DevicePid, Status),
|
|
||||||
|
|
||||||
{keep_state, State};
|
{keep_state, State};
|
||||||
|
|
||||||
%% 心跳机制
|
%% 心跳机制
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user