iot_cloud/apps/iot/src/http/service_handler.erl
2025-05-08 17:53:56 +08:00

82 lines
3.8 KiB
Erlang

%%%-------------------------------------------------------------------
%%% @author licheng5
%%% @copyright (C) 2020, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 26. 4月 2020 3:36 下午
%%%-------------------------------------------------------------------
-module(service_handler).
-author("licheng5").
-include("iot.hrl").
-include("iot_tables.hrl").
-include("message_pb.hrl").
%% API
-export([handle_request/4]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% helper methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
handle_request("POST", "/service/set_config", _, #{<<"service_id">> := ServiceId, <<"config_json">> := ConfigJson, <<"last_edit_user">> := LastEditUser})
when is_binary(ServiceId), is_binary(ConfigJson), is_integer(LastEditUser) ->
lager:debug("[service_handler] set_config service_id: ~p, config_json: ~p, last_edit_user:~p", [ServiceId, ConfigJson, LastEditUser]),
case service_config_model:update(ServiceId, ConfigJson, LastEditUser) of
ok ->
{ok, 200, iot_util:json_data(<<"success">>)};
{error, Reason} ->
lager:debug("[service_handler] set_config service_id: ~p, get error: ~p", [ServiceId, Reason]),
{ok, 200, iot_util:json_error(404, <<"set service config failed">>)}
end;
handle_request("POST", "/service/push_config", _, #{<<"host_uuid">> := HostUUID, <<"service_id">> := ServiceId})
when is_binary(ServiceId), is_binary(HostUUID) ->
lager:debug("[service_handler] push_config host_uuid: ~p, service_id: ~p", [ServiceId, HostUUID, ServiceId]),
case service_config_model:get_config(ServiceId) of
error ->
{ok, 200, iot_util:json_error(404, <<"service config not found">>)};
{ok, #service_config{config_json = ConfigJson}} ->
case iot_host:get_pid(HostUUID) of
undefined ->
{ok, 200, iot_util:json_error(404, <<"host not found">>)};
HostPid ->
case iot_host:push_config(HostPid, ConfigJson) of
{ok, Ref} ->
receive
{async_response, Ref, #async_response{code = 1}} ->
{ok, 200, iot_util:json_data(<<"success">>)};
{async_response, Ref, #async_response{code = 0, message = Message}} ->
{ok, 200, iot_util:json_error(404, Message)}
after 15000 ->
{ok, 200, iot_util:json_error(404, <<"request timeout">>)}
end;
{error, Reason} when is_binary(Reason) ->
{ok, 200, iot_util:json_error(404, Reason)}
end
end
end;
handle_request("GET", "/service/get_config", #{<<"service_id">> := ServiceId}, _) when is_binary(ServiceId) ->
case service_config_model:get_config(ServiceId) of
error ->
{ok, 200, iot_util:json_error(404, <<"service config not found">>)};
{ok, Config} ->
{ok, 200, iot_util:json_data(service_config_model:as_map(Config))}
end;
%% 删除对应的主机信息
handle_request("POST", "/service/delete", _, #{<<"service_id">> := ServiceId}) when is_binary(ServiceId) ->
case service_config_model:delete(ServiceId) of
ok ->
{ok, 200, iot_util:json_data(<<"success">>)};
{error, Reason} ->
lager:debug("[service_handler] delete config of service_id: ~p, error: ~p", [ServiceId, Reason]),
{ok, 200, iot_util:json_error(404, <<"delete service config errror">>)}
end;
handle_request(_, Path, _, _) ->
Path1 = list_to_binary(Path),
{ok, 200, iot_util:json_error(-1, <<"url: ", Path1/binary, " not found">>)}.