From f6646409c8b5d7507779fb0c5f97224ec4f72468 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Mon, 21 Apr 2025 11:09:00 +0800 Subject: [PATCH] fix --- .../iot/src/http_handler/endpoint_handler.erl | 66 +++++++++++++++++++ apps/iot/src/http_handler/test_handler.erl | 31 --------- .../src/http_handler/totalizator_handler.erl | 35 ---------- 3 files changed, 66 insertions(+), 66 deletions(-) create mode 100644 apps/iot/src/http_handler/endpoint_handler.erl delete mode 100644 apps/iot/src/http_handler/test_handler.erl delete mode 100644 apps/iot/src/http_handler/totalizator_handler.erl diff --git a/apps/iot/src/http_handler/endpoint_handler.erl b/apps/iot/src/http_handler/endpoint_handler.erl new file mode 100644 index 0000000..3bc839d --- /dev/null +++ b/apps/iot/src/http_handler/endpoint_handler.erl @@ -0,0 +1,66 @@ +%%%------------------------------------------------------------------- +%%% @author licheng5 +%%% @copyright (C) 2020, +%%% @doc +%%% +%%% @end +%%% Created : 26. 4月 2020 3:36 下午 +%%%------------------------------------------------------------------- +-module(endpoint_handler). +-author("licheng5"). +-include("iot.hrl"). + +%% API +-export([handle_request/4]). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% helper methods +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +handle_request("POST", "/endpoint/create", _, #{<<"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", "/endpoint/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", "/endpoint/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">>)}. \ No newline at end of file diff --git a/apps/iot/src/http_handler/test_handler.erl b/apps/iot/src/http_handler/test_handler.erl deleted file mode 100644 index bd34652..0000000 --- a/apps/iot/src/http_handler/test_handler.erl +++ /dev/null @@ -1,31 +0,0 @@ -%%%------------------------------------------------------------------- -%%% @author licheng5 -%%% @copyright (C) 2020, -%%% @doc -%%% -%%% @end -%%% Created : 26. 4月 2020 3:36 下午 -%%%------------------------------------------------------------------- --module(test_handler). --author("licheng5"). --include("iot.hrl"). - -%% API --export([handle_request/4]). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% helper methods -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%% 重新加载对应的主机信息 -handle_request("POST", "/test/receiver", _, PostParams) -> - lager:debug("[test_handler] get post params: ~p", [PostParams]), - {ok, 200, iot_util:json_data(<<"success">>)}; - -handle_request(_, Path, _, _) -> - Path1 = list_to_binary(Path), - {ok, 200, iot_util:json_error(-1, <<"url: ", Path1/binary, " not found">>)}. - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% helper methods -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \ No newline at end of file diff --git a/apps/iot/src/http_handler/totalizator_handler.erl b/apps/iot/src/http_handler/totalizator_handler.erl deleted file mode 100644 index 03a6312..0000000 --- a/apps/iot/src/http_handler/totalizator_handler.erl +++ /dev/null @@ -1,35 +0,0 @@ -%%%------------------------------------------------------------------- -%%% @author licheng5 -%%% @copyright (C) 2020, -%%% @doc -%%% -%%% @end -%%% Created : 26. 4月 2020 3:36 下午 -%%%------------------------------------------------------------------- --module(totalizator_handler). --author("licheng5"). --include("iot.hrl"). - -%% API --export([handle_request/4]). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% helper methods -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%% 重新加载对应的主机信息 -handle_request("POST", "/totalizator/query", _, #{<<"scene_ids">> := SceneIds, <<"dates">> := Dates0}) when is_list(SceneIds), is_list(Dates0) -> - Dates = lists:map(fun(Date) -> - [Year0, Month0, Day0] = binary:split(Date, <<"-">>, [global]), - Year = binary_to_integer(Year0), - Month = binary_to_integer(Month0), - Day = binary_to_integer(Day0), - {Year, Month, Day} - end, Dates0), - - List = mnesia_totalizator:query(SceneIds, Dates), - {ok, 200, iot_util:json_data(List)}; - -handle_request(_, Path, _, _) -> - Path1 = list_to_binary(Path), - {ok, 200, iot_util:json_error(-1, <<"url: ", Path1/binary, " not found??">>)}. \ No newline at end of file