Skip to content

Commit

Permalink
feat:get_Tabledata
Browse files Browse the repository at this point in the history
  • Loading branch information
AvantLiu committed Dec 22, 2021
1 parent cbd2983 commit b8b337f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 60 deletions.
52 changes: 11 additions & 41 deletions apps/dgiot_evidence/src/dgiot_evidence.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
post_data/2,
get_filehome/1,
test/0,
get_Tabledata/4
get_Tabledata/2
]).

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -697,45 +697,15 @@ get_attrs(Attrs, ImageUrl, Heigh, Width, ClassName, X) ->
X
end.

get_Tabledata(Parameter, Avgdatas) ->
Keys = binary:split(Parameter, <<$,>>, [global, trim]),
lists:foldl(fun(Avg, {Acc1, Num}) ->
Data =
lists:foldl(fun(Key, Acc) ->
Values = dgiot_utils:to_binary(maps:get(Key, Avg, 0)),
<<Acc/binary, ",", Values/binary>>
end, dgiot_utils:to_binary(Num), Keys),
{Acc1 ++ [Data], Num + 1}
end, {[], 1}, Avgdatas).

get_Tabledata(ParentId, SessionToken, Parameter, Samplingnumber) ->
case dgiot_tdengine:get_channel(SessionToken) of
{error, Error} ->
{error, Error};
{ok, Channel} ->
Query = #{
<<"keys">> => Parameter,
<<"starttime">> => Parameter,
<<"endtime">> => Parameter,
<<"db">> => ParentId
},
TableName = ?Table(<<"DeviceId">>),
case dgiot_tdengine:get_reportdata(Channel, TableName, Query) of
{ok, #{<<"results">> := Results}} ->
io:format("Results ~p~n", [Results]),
get_Tablelist(Parameter, Samplingnumber, Results),

{ok, #{<<"results">> => Results}};
Reason ->
Reason
end
end.


%% Query = #{<<"db">> => <<"09d0bbcf44">>,
%% <<"endtime">> => <<"1638260333000">>,
%% <<"keys">> => <<"flow,head,effect,power">>,

%% <<"starttime">> => <<"1635581932000">>}.
%% {ok, #{<<"results">> := Results}} = dgiot_tdengine:get_reportdata(<<"24b9b4bc50">>, <<"_47d9172bf1">>, Query).
get_Tablelist(Parameter, _Samplingnumber, Results) ->
Parameter = <<"flow,head,effect,power">>,
Keys = binary:split(Parameter, <<$,>>, [global, trim]),
lists:foldl(fun(Key, Acc) ->
Values =
lists:foldl(fun(Result, Acc2) ->
Value = maps:get(Key, Result, <<"0">>),
Acc2 ++ [Value]
end, [], Results),
Acc#{Key => Values}
end, #{}, Keys).
41 changes: 22 additions & 19 deletions apps/dgiot_evidence/src/handler/dgiot_evidence_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -223,25 +223,28 @@ do_request(post_generatereport, #{<<"id">> := TaskId}, #{<<"sessionToken">> := _
<<"name">> => Identifier,
<<"value">> => Value}];
<<"dynamicTable">> ->
Tabledata = [
<<"10,0,2.254,28.86,2900,0,0.266537,0,28.86,2.254,0">>,
<<"9,5.13,2.548,27.941,2900,0,0.257272,5.13,27.941,2.548,15.35">>,
<<"8,10.19,2.764,26.545,2900,0,0.242822,10.19,26.545,2.764,26.71">>,
<<"7,15.52,2.94,24.639,2900,0,0.222779,15.52,24.639,2.94,35.5">>,
<<"6,20.25,3.136,22.969,2900,0,0.204725,20.25,22.969,3.136,40.48">>,
<<"5,25.05,3.234,20.726,2900,0,0.180568,25.05,20.726,3.234,43.81">>,
<<"4,30.09,3.43,18.289,2900,0,0.153912,30.09,18.289,3.43,43.78">>,
<<"3,35.29,3.528,15.25,2900,0,0.120728,35.29,15.25,3.528,41.63">>,
<<"2,40.66,3.704,12.29,2900,0,0.087646,40.66,12.29,3.704,36.81">>,
<<"1,46.62,3.763,8.411,2900,0,0.044432,46.62,8.411,3.763,28.44">>],
%% _Avgdatas =
%% case dgiot_parse:query_object(<<"Evidence">>, #{<<"limit">> => 1, <<"where">> => #{<<"reportId">> => TaskId, <<"original.type">> => <<"avgs">>, <<"original.taskid">> => TaskId}}) of
%% {ok, #{<<"results">> := Results}} when length(Results) > 0 ->
%% [#{<<"original">> := #{<<"avgs">> := Avgs}} | _] = Results,
%% Avgs;
%% _ ->
%% <<"">>
%% end,
Avgdatas =
case dgiot_parse:query_object(<<"Evidence">>, #{<<"limit">> => 1, <<"where">> => #{<<"reportId">> => TaskId, <<"original.type">> => <<"avgs">>, <<"original.taskid">> => TaskId}}) of
{ok, #{<<"results">> := Results}} when length(Results) > 0 ->
[#{<<"original">> := #{<<"avgs">> := Avgs}} | _] = Results,
Avgs;
_ ->
<<"">>
end,
%% 采样参数
Parameter = maps:get(<<"parameter">>, Param, <<"flow,power,head,ratedspeed,pressure_in,pressure_out,conversion_flow,conversion_head,conversion_power,effect">>),
{Tabledata, _} = dgiot_evidence:get_Tabledata(Parameter, Avgdatas),
%% Tabledata = [
%% <<"10,0,2.254,28.86,2900,0,0.266537,0,28.86,2.254,0">>,
%% <<"9,5.13,2.548,27.941,2900,0,0.257272,5.13,27.941,2.548,15.35">>,
%% <<"8,10.19,2.764,26.545,2900,0,0.242822,10.19,26.545,2.764,26.71">>,
%% <<"7,15.52,2.94,24.639,2900,0,0.222779,15.52,24.639,2.94,35.5">>,
%% <<"6,20.25,3.136,22.969,2900,0,0.204725,20.25,22.969,3.136,40.48">>,
%% <<"5,25.05,3.234,20.726,2900,0,0.180568,25.05,20.726,3.234,43.81">>,
%% <<"4,30.09,3.43,18.289,2900,0,0.153912,30.09,18.289,3.43,43.78">>,
%% <<"3,35.29,3.528,15.25,2900,0,0.120728,35.29,15.25,3.528,41.63">>,
%% <<"2,40.66,3.704,12.29,2900,0,0.087646,40.66,12.29,3.704,36.81">>,
%% <<"1,46.62,3.763,8.411,2900,0,0.044432,46.62,8.411,3.763,28.44">>],
Acc ++ [#{
<<"type">> => <<"dynamicTable">>,
<<"source">> => Sources,
Expand Down

0 comments on commit b8b337f

Please sign in to comment.