fix
This commit is contained in:
parent
6f89774267
commit
dad21ddf02
@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([get_all_terminals/0, get_status_stat/0]).
|
-export([get_all_terminals/0, get_status_stat/0]).
|
||||||
|
-export([change_status/2, delete/1]).
|
||||||
|
|
||||||
%% 获取app信息
|
%% 获取app信息
|
||||||
get_all_terminals() ->
|
get_all_terminals() ->
|
||||||
@ -64,14 +65,14 @@ insert(Id, HostId, Name, ProductId, VendorId, Model, CellId) ->
|
|||||||
{error, Error}
|
{error, Error}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
change_status(Id, Status) ->
|
change_status(TerminalId, Status) when is_binary(TerminalId), is_integer(Status) ->
|
||||||
Fun = fun() ->
|
Fun = fun() ->
|
||||||
case mnesia:read(host, Id) of
|
case mnesia:read(terminal, TerminalId) of
|
||||||
[] ->
|
[] ->
|
||||||
mnesia:abort(<<"appinfo not found">>);
|
mnesia:abort(<<"terminal not found">>);
|
||||||
[Host] ->
|
[Terminal] ->
|
||||||
NHost = Host#host{status = Status},
|
NTerminal = Terminal#host{status = Status},
|
||||||
mnesia:write(host, NHost, write)
|
mnesia:write(terminal, NTerminal, write)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
case mnesia:transaction(Fun) of
|
case mnesia:transaction(Fun) of
|
||||||
@ -81,8 +82,51 @@ change_status(Id, Status) ->
|
|||||||
{error, Reason}
|
{error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
delete(Id) ->
|
update_metric(TerminalId, MetricName, MetricSymbol, MetricValue) when is_binary(TerminalId), is_binary(MetricName), is_binary(MetricSymbol), is_number(MetricValue) ->
|
||||||
case mnesia:transaction(fun() -> mnesia:delete(host, Id, write) end) of
|
Fun = fun() ->
|
||||||
|
case mnesia:read(terminal, TerminalId) of
|
||||||
|
[] ->
|
||||||
|
mnesia:abort(<<"terminal not found">>);
|
||||||
|
[Terminal = #terminal{metrics = Metrics}] ->
|
||||||
|
NMetrics = case lists:any(fun(#terminal_metric{symbol = Symbol}) -> Symbol =:= MetricSymbol end, Metrics) of
|
||||||
|
true ->
|
||||||
|
lists:map(fun(Metric=#terminal_metric{symbol = Symbol, serial_values = SerialValues}) ->
|
||||||
|
case Symbol =:= MetricSymbol of
|
||||||
|
true ->
|
||||||
|
NSerialValues = case queue:len(SerialValues) >= 100 of
|
||||||
|
true ->
|
||||||
|
SerialValues0 = queue:drop(SerialValues),
|
||||||
|
queue:in(MetricValue, SerialValues0);
|
||||||
|
false ->
|
||||||
|
queue:in(MetricValue, SerialValues)
|
||||||
|
end,
|
||||||
|
Metric#terminal_metric{
|
||||||
|
name = MetricName,
|
||||||
|
last_value = MetricValue,
|
||||||
|
update_ts = iot_util:current_time(),
|
||||||
|
serial_values = NSerialValues
|
||||||
|
};
|
||||||
|
false ->
|
||||||
|
Metric
|
||||||
|
end
|
||||||
|
end, Metrics);
|
||||||
|
false ->
|
||||||
|
Metrics#terminal_metric{}
|
||||||
|
end,
|
||||||
|
mnesia:write(terminal, Terminal#terminal{metrics = NMetrics}, write)
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
|
||||||
|
case mnesia:transaction(Fun) of
|
||||||
|
{atomic, ok} ->
|
||||||
|
ok;
|
||||||
|
{aborted, Reason} ->
|
||||||
|
{error, Reason}
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
delete(TerminalId) when is_binary(TerminalId) ->
|
||||||
|
case mnesia:transaction(fun() -> mnesia:delete(terminal, TerminalId, write) end) of
|
||||||
{atomic, ok} ->
|
{atomic, ok} ->
|
||||||
ok;
|
ok;
|
||||||
{aborted, Reason} ->
|
{aborted, Reason} ->
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user