fix service config

This commit is contained in:
anlicheng 2025-05-08 12:49:54 +08:00
parent b192d9836f
commit d1e51d635d
4 changed files with 190 additions and 1 deletions

View File

@ -0,0 +1,22 @@
%%%-------------------------------------------------------------------
%%% @author anlicheng
%%% @copyright (C) 2025, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 08. 5 2025 12:08
%%%-------------------------------------------------------------------
-author("anlicheng").
%%
-record(service_config, {
service_id :: binary(),
config_json = <<>> :: binary(),
%%
last_config_json = <<>> :: binary(),
%% id
last_edit_user :: integer(),
%% 0: , 1:
update_ts = 0,
create_ts = 0
}).

View File

@ -0,0 +1,52 @@
%%%-------------------------------------------------------------------
%%% @author licheng5
%%% @copyright (C) 2020, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 26. 4 2020 3:36
%%%-------------------------------------------------------------------
-module(service_handler).
-author("licheng5").
-include("iot.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] 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("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

@ -14,6 +14,8 @@ start(_StartType, _StartArgs) ->
io:setopts([{encoding, unicode}]),
%%
erlang:system_flag(fullsweep_after, 16),
%% mnesia数据库
start_mnesia(),
start_tcp_server(),
@ -46,4 +48,14 @@ start_tcp_server() ->
],
{ok, _} = esockd:open('iot/tcp_server', Port, TransOpts, {tcp_channel, start_link, []}),
lager:debug("[iot_app] the tcp server start at: ~p", [Port]).
lager:debug("[iot_app] the tcp server start at: ~p", [Port]).
%%
start_mnesia() ->
%%
ok = mnesia:start(),
Tables = mnesia:system_info(tables),
lager:debug("[iot_app] tables: ~p", [Tables]),
%%
not lists:member(service_config, Tables) andalso service_config_model:create_table(),
ok.

View File

@ -0,0 +1,103 @@
%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 04. 7 2023 12:31
%%%-------------------------------------------------------------------
-module(service_config_model).
-author("aresei").
-include("iot_tables.hrl").
-include_lib("stdlib/include/qlc.hrl").
-define(TAB, service_config).
%% API
-export([create_table/0]).
-export([insert/3, update/3, get_config/1, delete/1]).
-export([as_map/1]).
create_table() ->
%% id生成器
mnesia:create_table(service_config, [
{attributes, record_info(fields, service_config)},
{record_name, service_config},
{disc_copies, [node()]},
{type, ordered_set}
]).
insert(ServiceId, ConfigJson, LastEditUser) when is_binary(ServiceId), is_binary(ConfigJson), is_integer(LastEditUser) ->
ServiceConfig = #service_config{
service_id = ServiceId,
config_json = ConfigJson,
last_config_json = <<>>,
last_edit_user = LastEditUser,
create_ts = iot_util:current_time(),
update_ts = iot_util:current_time()
},
case mnesia:transaction(fun() -> mnesia:write(?TAB, ServiceConfig, write) end) of
{'atomic', ok} ->
ok;
{'aborted', Reason} ->
{error, Reason}
end.
update(ServiceId, ConfigJson, LastEditUser) when is_binary(ServiceId), is_binary(ConfigJson), is_integer(LastEditUser) ->
Fun = fun() ->
case mnesia:read(?TAB, ServiceId, write) of
[] ->
ServiceConfig = #service_config{
service_id = ServiceId,
config_json = ConfigJson,
last_config_json = <<>>,
last_edit_user = LastEditUser,
create_ts = iot_util:current_time(),
update_ts = iot_util:current_time()
},
mnesia:write(?TAB, ServiceConfig, write);
[ServiceConfig0 = #service_config{config_json = OldConfigJson}] ->
NServiceConfig = ServiceConfig0#service_config{
config_json = ConfigJson,
last_config_json = OldConfigJson,
last_edit_user = LastEditUser,
update_ts = iot_util:current_time()
},
mnesia:write(?TAB, NServiceConfig, write)
end
end,
case mnesia:transaction(Fun) of
{'atomic', ok} ->
ok;
{'aborted', Reason} ->
{error, Reason}
end.
-spec get_config(ServiceId :: any()) -> error | {ok, Config :: #service_config{}}.
get_config(ServiceId) when is_binary(ServiceId) ->
case mnesia:dirty_read(?TAB, ServiceId) of
[] ->
error;
[Config] ->
{ok, Config}
end.
-spec delete(ServiceId :: binary()) -> ok | {error, Reason :: any()}.
delete(ServiceId) when is_binary(ServiceId) ->
case mnesia:transaction(fun() -> mnesia:delete(?TAB, ServiceId, write) end) of
{'atomic', ok} ->
ok;
{'aborted', Reason} ->
{error, Reason}
end.
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,
<<"config_json">> => ConfigJson,
<<"last_config_json">> => LastConfigJson,
<<"last_edit_user">> => LastEditUser,
<<"update_ts">> => UpdateTs,
<<"create_ts">> => CreateTs
}.