This commit is contained in:
anlicheng 2025-05-09 23:44:38 +08:00
parent c92ec214f5
commit 6070a3a822
5 changed files with 47 additions and 88 deletions

View File

@ -11,6 +11,7 @@
%%
-record(service_config, {
service_id :: binary(),
host_uuid :: binary(),
config_json = <<>> :: binary(),
%%
last_config_json = <<>> :: binary(),

View File

@ -67,8 +67,26 @@ handle_request("POST", "/host/delete", _, #{<<"uuid">> := UUID}) when is_binary(
{ok, 200, iot_util:json_error(404, <<"error">>)}
end;
%%
handle_request("POST", "/host/set_service_config", _, #{<<"uuid">> := UUID, <<"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_service_config service_id: ~p, config_json: ~p, last_edit_user:~p", [ServiceId, ConfigJson, LastEditUser]),
case iot_host:get_pid(UUID) of
undefined ->
{ok, 200, iot_util:json_error(404, <<"host not found">>)};
Pid when is_pid(Pid) ->
case service_config_model:update(ServiceId, UUID, 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(-1, <<"set service config failed">>)}
end
end;
%% config.json
handle_request("POST", "/host/async_service_config", _,
handle_request("POST", "/host/push_service_config", _,
PostParams = #{<<"uuid">> := UUID, <<"service_id">> := ServiceId, <<"config_json">> := ConfigJson, <<"timeout">> := Timeout0})
when is_binary(UUID), is_binary(ServiceId), is_binary(ConfigJson), is_integer(Timeout0) ->
@ -91,6 +109,24 @@ handle_request("POST", "/host/async_service_config", _,
end
end;
handle_request("GET", "/host/get_service_config", #{<<"service_id">> := ServiceId}, _) when is_binary(ServiceId) ->
case service_config_model:get_config(ServiceId) of
error ->
{ok, 200, iot_util:json_error(-1, <<"service config not found">>)};
{ok, Config} ->
{ok, 200, iot_util:json_data(service_config_model:as_map(Config))}
end;
%%
handle_request("POST", "/host/delete_service_config", _, #{<<"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(-1, <<"delete service config errror">>)}
end;
handle_request("POST", "/host/async_deploy", _, #{<<"uuid">> := UUID, <<"task_id">> := TaskId, <<"service_id">> := ServiceId, <<"tar_url">> := TarUrl})
when is_binary(UUID), is_integer(TaskId), is_binary(ServiceId), is_binary(TarUrl) ->

View File

@ -23,8 +23,7 @@ start() ->
Dispatcher = cowboy_router:compile([
{'_', [
{"/host/[...]", http_protocol, [host_handler]},
{"/device/[...]", http_protocol, [device_handler]},
{"/service/[...]", http_protocol, [service_handler]}
{"/device/[...]", http_protocol, [device_handler]}
]}
]),

View File

@ -1,82 +0,0 @@
%%%-------------------------------------------------------------------
%%% @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, <<"timeout">> := Timeout})
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 when is_pid(HostPid) ->
case iot_host:async_service_config(HostPid, ServiceId, ConfigJson, Timeout) of
{ok, Ref} ->
case iot_host:await_reply(Ref, 15000) of
{ok, #async_call_reply{code = 1}} ->
{ok, 200, iot_util:json_data(<<"success">>)};
{ok, #async_call_reply{code = 0, message = Message}} ->
{ok, 200, iot_util:json_error(404, Message)};
{error, timeout} ->
{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">>)}.

View File

@ -15,7 +15,7 @@
%% API
-export([create_table/0]).
-export([insert/3, update/3, get_config/1, delete/1]).
-export([insert/4, update/4, get_config/1, delete/1]).
-export([as_map/1]).
create_table() ->
@ -27,9 +27,11 @@ create_table() ->
{type, ordered_set}
]).
insert(ServiceId, ConfigJson, LastEditUser) when is_binary(ServiceId), is_binary(ConfigJson), is_integer(LastEditUser) ->
-spec insert(ServiceId :: binary(), HostUUID :: binary(), ConfigJson :: binary(), LastEditUser :: integer()) -> ok | {error, Reason :: term()}.
insert(ServiceId, HostUUID, ConfigJson, LastEditUser) when is_binary(ServiceId), is_binary(HostUUID), is_binary(ConfigJson), is_integer(LastEditUser) ->
ServiceConfig = #service_config{
service_id = ServiceId,
host_uuid = HostUUID,
config_json = ConfigJson,
last_config_json = <<>>,
last_edit_user = LastEditUser,
@ -43,12 +45,14 @@ insert(ServiceId, ConfigJson, LastEditUser) when is_binary(ServiceId), is_binary
{error, Reason}
end.
update(ServiceId, ConfigJson, LastEditUser) when is_binary(ServiceId), is_binary(ConfigJson), is_integer(LastEditUser) ->
-spec update(ServiceId :: binary(), HostUUID :: binary(), ConfigJson :: binary(), LastEditUser :: integer()) -> ok | {error, Reason :: term()}.
update(ServiceId, HostUUID, ConfigJson, LastEditUser) when is_binary(ServiceId), is_binary(HostUUID), is_binary(ConfigJson), is_integer(LastEditUser) ->
Fun = fun() ->
case mnesia:read(?TAB, ServiceId, write) of
[] ->
ServiceConfig = #service_config{
service_id = ServiceId,
host_uuid = HostUUID,
config_json = ConfigJson,
last_config_json = <<>>,
last_edit_user = LastEditUser,
@ -92,6 +96,7 @@ delete(ServiceId) when is_binary(ServiceId) ->
{error, Reason}
end.
-spec as_map(ServiceConfig :: #service_config{}) -> map().
as_map(#service_config{service_id = ServiceId, config_json = ConfigJson, last_config_json = LastConfigJson, last_edit_user = LastEditUser, update_ts = UpdateTs, create_ts = CreateTs}) ->
#{
<<"service_id">> => ServiceId,