简化数据库概念

This commit is contained in:
anlicheng 2025-05-09 22:59:20 +08:00
parent d52d779f6f
commit e7d4877d22
6 changed files with 9 additions and 54 deletions

View File

@ -33,13 +33,7 @@ get_device_by_uuid(DeviceUUID) when is_binary(DeviceUUID) ->
%% %%
-spec change_status(DeviceUUID :: binary(), Status :: integer()) -> {ok, AffectedRows :: integer()} | {error, Reason :: any()}. -spec change_status(DeviceUUID :: binary(), Status :: integer()) -> {ok, AffectedRows :: integer()} | {error, Reason :: any()}.
change_status(DeviceUUID, NStatus) when is_binary(DeviceUUID), is_integer(NStatus) -> change_status(DeviceUUID, NStatus) when is_binary(DeviceUUID), is_integer(NStatus) ->
case change_status0(DeviceUUID, NStatus) of change_status0(DeviceUUID, NStatus).
Result = {ok, _} ->
event_logs_bo:insert(?EVENT_DEVICE, DeviceUUID, NStatus),
Result;
Error ->
Error
end.
change_status0(DeviceUUID, ?DEVICE_ONLINE) when is_binary(DeviceUUID) -> change_status0(DeviceUUID, ?DEVICE_ONLINE) when is_binary(DeviceUUID) ->
Timestamp = calendar:local_time(), Timestamp = calendar:local_time(),
case mysql_pool:get_row(mysql_iot, <<"SELECT status FROM device WHERE device_uuid = ? LIMIT 1">>, [DeviceUUID]) of case mysql_pool:get_row(mysql_iot, <<"SELECT status FROM device WHERE device_uuid = ? LIMIT 1">>, [DeviceUUID]) of

View File

@ -33,13 +33,7 @@ get_host_by_id(HostId) when is_integer(HostId) ->
%% %%
-spec change_status(UUID :: binary(), Status :: integer()) -> {ok, AffectedRows :: integer()} | {error, Reason :: any()}. -spec change_status(UUID :: binary(), Status :: integer()) -> {ok, AffectedRows :: integer()} | {error, Reason :: any()}.
change_status(UUID, NStatus) when is_binary(UUID), is_integer(NStatus) -> change_status(UUID, NStatus) when is_binary(UUID), is_integer(NStatus) ->
case change_status0(UUID, NStatus) of change_status0(UUID, NStatus).
Result = {ok, _} ->
event_logs_bo:insert(?EVENT_HOST, UUID, NStatus),
Result;
Error ->
Error
end.
change_status0(UUID, ?HOST_ONLINE) when is_binary(UUID) -> change_status0(UUID, ?HOST_ONLINE) when is_binary(UUID) ->
Timestamp = calendar:local_time(), Timestamp = calendar:local_time(),
case mysql_pool:get_row(mysql_iot, <<"SELECT status FROM host WHERE uuid = ? LIMIT 1">>, [UUID]) of case mysql_pool:get_row(mysql_iot, <<"SELECT status FROM host WHERE uuid = ? LIMIT 1">>, [UUID]) of

View File

@ -1,17 +0,0 @@
%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 16. 5 2023 12:48
%%%-------------------------------------------------------------------
-module(scene_feedback).
-author("aresei").
-include("iot.hrl").
%% API
-export([insert/1]).
insert(Fields) when is_map(Fields) ->
mysql_pool:insert(mysql_iot, <<"scene_feedback">>, Fields, true).

View File

@ -1,17 +0,0 @@
%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 16. 5 2023 12:48
%%%-------------------------------------------------------------------
-module(scene_feedback_step).
-author("aresei").
-include("iot.hrl").
%% API
-export([insert/1]).
insert(Fields) when is_map(Fields) ->
mysql_pool:insert(mysql_iot, <<"scene_feedback_step">>, Fields, true).

View File

@ -31,7 +31,7 @@ handle_request("POST", "/service/set_config", _, #{<<"service_id">> := ServiceId
{ok, 200, iot_util:json_error(404, <<"set service config failed">>)} {ok, 200, iot_util:json_error(404, <<"set service config failed">>)}
end; end;
handle_request("POST", "/service/push_config", _, #{<<"host_uuid">> := HostUUID, <<"service_id">> := ServiceId}) handle_request("POST", "/service/push_config", _, #{<<"host_uuid">> := HostUUID, <<"service_id">> := ServiceId, <<"timeout">> := Timeout})
when is_binary(ServiceId), is_binary(HostUUID) -> when is_binary(ServiceId), is_binary(HostUUID) ->
lager:debug("[service_handler] push_config host_uuid: ~p, service_id: ~p", [ServiceId, HostUUID, ServiceId]), lager:debug("[service_handler] push_config host_uuid: ~p, service_id: ~p", [ServiceId, HostUUID, ServiceId]),
@ -43,7 +43,7 @@ handle_request("POST", "/service/push_config", _, #{<<"host_uuid">> := HostUUID,
undefined -> undefined ->
{ok, 200, iot_util:json_error(404, <<"host not found">>)}; {ok, 200, iot_util:json_error(404, <<"host not found">>)};
HostPid when is_pid(HostPid) -> HostPid when is_pid(HostPid) ->
case iot_host:async_service_config(HostPid, ConfigJson) of case iot_host:async_service_config(HostPid, ServiceId, ConfigJson, Timeout) of
{ok, Ref} -> {ok, Ref} ->
case iot_host:await_reply(Ref, 15000) of case iot_host:await_reply(Ref, 15000) of
{ok, #async_call_reply{code = 1}} -> {ok, #async_call_reply{code = 1}} ->

View File

@ -25,7 +25,7 @@
-export([get_metric/1, get_status/1]). -export([get_metric/1, get_status/1]).
%% %%
-export([pub/3, attach_channel/2, command/3]). -export([pub/3, attach_channel/2, command/3]).
-export([async_deploy/4, async_invoke/4, async_service_config/2, async_task_log/2, await_reply/2]). -export([async_deploy/4, async_invoke/4, async_service_config/4, async_task_log/2, await_reply/2]).
%% %%
-export([reload_device/2, delete_device/2, activate_device/3]). -export([reload_device/2, delete_device/2, activate_device/3]).
-export([heartbeat/1]). -export([heartbeat/1]).
@ -89,9 +89,10 @@ get_metric(Pid) when is_pid(Pid) ->
attach_channel(Pid, ChannelPid) when is_pid(Pid), is_pid(ChannelPid) -> attach_channel(Pid, ChannelPid) when is_pid(Pid), is_pid(ChannelPid) ->
gen_statem:call(Pid, {attach_channel, ChannelPid}). gen_statem:call(Pid, {attach_channel, ChannelPid}).
-spec async_service_config(Pid :: pid(), ConfigJson :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. -spec async_service_config(Pid :: pid(), ServiceId :: binary(), ConfigJson :: binary(), Timeout :: integer()) -> {ok, Ref :: reference()} | {error, Reason :: any()}.
async_service_config(Pid, ConfigJson) when is_pid(Pid), is_binary(ConfigJson) -> async_service_config(Pid, ServiceId, ConfigJson, Timeout) when is_pid(Pid), is_binary(ServiceId), is_binary(ConfigJson), is_integer(Timeout) ->
gen_statem:call(Pid, {async_call, self(), ?PUSH_SERVICE_CONFIG, ConfigJson}). ConfigBin = message_pb:encode_msg(#push_service_config{service_id = ServiceId, config_json = ConfigJson, timeout = Timeout}),
gen_statem:call(Pid, {async_call, self(), ?PUSH_SERVICE_CONFIG, ConfigBin}).
-spec async_deploy(Pid :: pid(), TaskId :: integer(), ServiceId :: binary(), TarUrl :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. -spec async_deploy(Pid :: pid(), TaskId :: integer(), ServiceId :: binary(), TarUrl :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}.
async_deploy(Pid, TaskId, ServiceId, TarUrl) when is_pid(Pid), is_integer(TaskId), is_binary(ServiceId), is_binary(TarUrl) -> async_deploy(Pid, TaskId, ServiceId, TarUrl) when is_pid(Pid), is_integer(TaskId), is_binary(ServiceId), is_binary(TarUrl) ->