This commit is contained in:
anlicheng 2025-05-20 16:09:20 +08:00
parent a9ef277402
commit 96b76f2b09
3 changed files with 66 additions and 40 deletions

View File

@ -65,7 +65,7 @@
-define(PUSH_SERVICE_CONFIG, 16#04). -define(PUSH_SERVICE_CONFIG, 16#04).
-define(PUSH_INVOKE, 16#05). -define(PUSH_INVOKE, 16#05).
-define(PUSH_TASK_LOG, 16#05). -define(PUSH_TASK_LOG, 16#06).
%% %%
-record(kv, { -record(kv, {

View File

@ -13,48 +13,43 @@
%% API %% API
-export([handle_request/4]). -export([handle_request/4]).
%% %% config.json,
handle_request("POST", "/service/set_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", "/service/push_config", _, handle_request("POST", "/service/push_config", _,
PostParams = #{<<"uuid">> := UUID, <<"service_id">> := ServiceId, <<"config_json">> := ConfigJson, <<"timeout">> := Timeout0}) #{<<"uuid">> := UUID, <<"service_id">> := ServiceId, <<"last_edit_user">> := LastEditUser, <<"config_json">> := ConfigJson, <<"timeout">> := Timeout0})
when is_binary(UUID), is_binary(ServiceId), is_binary(ConfigJson), is_integer(Timeout0) -> when is_binary(UUID), is_binary(ServiceId), is_binary(ConfigJson), is_integer(Timeout0) ->
lager:debug("[http_host_handler] async_service_config body is: ~p", [PostParams]), %% ConfigJson是否是合法的json字符串
case iot_host:get_pid(UUID) of case iot_util:is_json(ConfigJson) of
undefined -> true ->
{ok, 200, iot_util:json_error(404, <<"host not found">>)}; case iot_host:get_pid(UUID) of
Pid when is_pid(Pid) -> undefined ->
Timeout = Timeout0 * 1000, {ok, 200, iot_util:json_error(-1, <<"host not found">>)};
case iot_host:async_service_config(Pid, ServiceId, ConfigJson, Timeout) of Pid when is_pid(Pid) ->
{ok, Ref} -> Timeout = Timeout0 * 1000,
case iot_host:await_reply(Ref, Timeout) of case iot_host:async_service_config(Pid, ServiceId, ConfigJson, Timeout) of
{ok, Result} -> {ok, Ref} ->
{ok, 200, iot_util:json_data(Result)}; case iot_host:await_reply(Ref, Timeout) of
{error, Reason} -> {ok, Result} ->
{ok, 200, iot_util:json_error(400, Reason)} %%
end; case service_config_model:update(ServiceId, UUID, ConfigJson, LastEditUser) of
{error, Reason} when is_binary(Reason) -> ok ->
{ok, 200, iot_util:json_error(400, Reason)} {ok, 200, iot_util:json_data(Result)};
end {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;
{error, Reason} ->
{ok, 200, iot_util:json_error(-1, Reason)}
end;
{error, Reason} when is_binary(Reason) ->
{ok, 200, iot_util:json_error(-1, Reason)}
end
end;
false ->
{ok, 200, iot_util:json_error(-1, <<"config is invalid json">>)}
end; end;
%%
handle_request("GET", "/service/get_config", #{<<"service_id">> := ServiceId}, _) when is_binary(ServiceId) -> handle_request("GET", "/service/get_config", #{<<"service_id">> := ServiceId}, _) when is_binary(ServiceId) ->
case service_config_model:get_config(ServiceId) of case service_config_model:get_config(ServiceId) of
error -> error ->
@ -100,7 +95,7 @@ handle_request("POST", "/service/start", _, #{<<"uuid">> := UUID, <<"service_id"
undefined -> undefined ->
{ok, 200, iot_util:json_error(404, <<"host not found">>)}; {ok, 200, iot_util:json_error(404, <<"host not found">>)};
Pid when is_pid(Pid) -> Pid when is_pid(Pid) ->
case iot_host:deploy_service(Pid, TaskId, ServiceId, TarUrl) of case iot_host:start_service(Pid, ServiceId) of
{ok, Ref} -> {ok, Ref} ->
case iot_host:await_reply(Ref, 5000) of case iot_host:await_reply(Ref, 5000) of
{ok, Result} -> {ok, Result} ->
@ -113,6 +108,26 @@ handle_request("POST", "/service/start", _, #{<<"uuid">> := UUID, <<"service_id"
end end
end; end;
%%
handle_request("POST", "/service/stop", _, #{<<"uuid">> := UUID, <<"service_id">> := ServiceId}) when is_binary(UUID), is_binary(ServiceId) ->
case iot_host:get_pid(UUID) of
undefined ->
{ok, 200, iot_util:json_error(404, <<"host not found">>)};
Pid when is_pid(Pid) ->
case iot_host:stop_service(Pid, ServiceId) of
{ok, Ref} ->
case iot_host:await_reply(Ref, 5000) of
{ok, Result} ->
{ok, 200, iot_util:json_data(Result)};
{error, Reason} ->
{ok, 200, iot_util:json_error(400, Reason)}
end;
{error, Reason} when is_binary(Reason) ->
{ok, 200, iot_util:json_error(400, Reason)}
end
end;
%% , json
handle_request("POST", "/service/invoke", _, #{<<"uuid">> := UUID, <<"service_id">> := ServiceId, <<"payload">> := Payload, <<"timeout">> := Timeout0}) handle_request("POST", "/service/invoke", _, #{<<"uuid">> := UUID, <<"service_id">> := ServiceId, <<"payload">> := Payload, <<"timeout">> := Timeout0})
when is_binary(UUID), is_binary(ServiceId), is_binary(Payload), is_integer(Timeout0) -> when is_binary(UUID), is_binary(ServiceId), is_binary(Payload), is_integer(Timeout0) ->

View File

@ -12,9 +12,20 @@
%% API %% API
-export([timestamp/0, number_format/2, current_time/0, timestamp_of_seconds/0, float_to_binary/2, int_format/2, file_uri/1]). -export([timestamp/0, number_format/2, current_time/0, timestamp_of_seconds/0, float_to_binary/2, int_format/2, file_uri/1]).
-export([step/3, chunks/2, rand_bytes/1, uuid/0, md5/1, parse_mapper/1]). -export([step/3, chunks/2, rand_bytes/1, uuid/0, md5/1, parse_mapper/1]).
-export([json_data/1, json_error/2]). -export([json_data/1, json_error/2, is_json/1]).
-export([queue_limited_in/3, assert_call/2, assert/2]). -export([queue_limited_in/3, assert_call/2, assert/2]).
-spec is_json(Json :: term()) -> boolean().
is_json(Json) when is_binary(Json) ->
case catch jiffy:decode(Json, [return_maps]) of
Result when is_list(Result) orelse is_map(Result) ->
true;
_ ->
false
end;
is_json(_Json) ->
false.
%% %%
timestamp() -> timestamp() ->
{Mega, Seconds, Micro} = os:timestamp(), {Mega, Seconds, Micro} = os:timestamp(),