feat: dg/dashboard

This commit is contained in:
AvantLiu 2022-03-09 20:36:48 +08:00
parent 87333971c2
commit db709fa9c8
5 changed files with 60 additions and 53 deletions

View File

@ -102,10 +102,9 @@ do_check(#{clientid := ClientID} = _ClientInfo, subscribe, <<"$dg/channel/", Dev
%% "$dg/dashboard/{dashboardId}/{productId}/{deviceId}"
do_check(#{clientid := ClientID} = _ClientInfo, subscribe, <<"$dg/dashboard/", DeviceInfo/binary>> = Topic) ->
io:format("~s ~p Topic: ~p~n", [?FILE, ?LINE, Topic]),
[ProuctID, Devaddr | _] = binary:split(DeviceInfo, <<"/">>, [global]),
DeviceID = dgiot_parse:get_deviceid(ProuctID, Devaddr),
case ClientID == DeviceID of
true ->
[DashboardId | _] = binary:split(DeviceInfo, <<"/">>, [global]),
case dgiot_parse:get_object(<<"View">>, DashboardId, [{"X-Parse-Session-Token", ClientID}], [{from, rest}]) of
{ok, _} ->
allow;
_ ->
deny

View File

@ -14,5 +14,5 @@
%% limitations under the License.
%%--------------------------------------------------------------------
-record(task, {oldque = [], newque = [], freq = 0, heart = 0, sessiontoken = <<>>}).
-record(task, {oldque = [], newque = [], freq = 0, heart = 0, dashboardId = <<>>, sessiontoken = <<>>}).

View File

@ -190,6 +190,14 @@
"post": {
"description": "启动任务",
"parameters": [
{
"name": "dashboardId",
"description": "大屏ID",
"in": "query",
"required": true,
"type": "string",
"default": "32511dbfe5"
},
{
"in": "body",
"name": "data",

View File

@ -54,21 +54,22 @@ stop(#{<<"sessionToken">> := SessionToken}) ->
_Reason ->
ok
end.
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
init([#{<<"data">> := Que, <<"sessionToken">> := SessionToken}]) ->
init([#{<<"data">> := Que, <<"dashboardId">> := DashboardId, <<"sessionToken">> := SessionToken}]) ->
dgiot_data:insert({dashboard, SessionToken}, self()),
case length(Que) of
0 ->
erlang:send_after(300, self(), stop);
_ ->
Topic = <<"dashboard/", SessionToken/binary, "/heart">>,
dgiot_mqtt:subscribe(Topic),
erlang:send_after(30 * 1000, self(), heart),
%% Topic = <<"dashboard/", SessionToken/binary, "/heart">>,
%% dgiot_mqtt:subscribe(Topic),
%% erlang:send_after(30 * 1000, self(), heart),
erlang:send_after(100, self(), retry)
end,
{ok, #task{oldque = Que, newque = Que, freq = 1, sessiontoken = SessionToken}};
{ok, #task{oldque = Que, newque = Que, freq = 1, dashboardId = DashboardId, sessiontoken = SessionToken}};
init(A) ->
?LOG(info, "A ~p ", [A]).
@ -92,16 +93,13 @@ handle_info(retry, #task{newque = Que} = State) when length(Que) == 0 ->
erlang:garbage_collect(self()),
{stop, normal, State};
%%
handle_info(retry, State) ->
{noreply, send_msg(State)};
%%
handle_info(heart, #task{heart = Heart} = State) when Heart < 4 ->
erlang:send_after(30 * 1000, self(), heart),
{noreply, State#task{heart = Heart + 1}};
%%
handle_info(heart, State) ->
{stop, normal, State};

View File

@ -31,7 +31,7 @@ post_dashboard(Args, #{<<"sessionToken">> := SessionToken} = _Context) ->
%%182,229,153,168,49,50,51,52,...>>,
%%<<"objectId">> => <<"5c413c7040">>,
do_task(#{<<"dataType">> := <<"map">>, <<"vuekey">> := Vuekey, <<"table">> := Table, <<"query">> := Query}, #task{sessiontoken = SessionToken}) ->
do_task(#{<<"dataType">> := <<"map">>, <<"vuekey">> := Vuekey, <<"table">> := Table, <<"query">> := Query}, #task{dashboardId = DashboardId, sessiontoken = SessionToken}) ->
case dgiot_parse:query_object(<<"Device">>, Query, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"results">> := Results}} ->
NewResult =
@ -47,7 +47,7 @@ do_task(#{<<"dataType">> := <<"map">>, <<"vuekey">> := Vuekey, <<"table">> := Ta
Acc
end
end, [], Results),
Topic = <<"dashboard/", SessionToken/binary, "/post">>,
Topic = <<"$dg/dashboard/", DashboardId/binary, "/report">>,
Base64 = base64:encode(jsx:encode(#{<<"dataType">> => <<"map">>, <<"vuekey">> => Vuekey, <<"table">> => Table, <<"value">> => NewResult})),
dgiot_mqtt:publish(self(), Topic, Base64);
_ ->
@ -55,47 +55,49 @@ do_task(#{<<"dataType">> := <<"map">>, <<"vuekey">> := Vuekey, <<"table">> := Ta
end;
%% dgiot_parse:query_object(<<"Device">>, #{<<"keys">> => [<<"count(*)">>],<<"order">> => <<"-updatedAt">>, <<"limit">> => 10, <<"skip">> => 0}, [{"X-Parse-Session-Token", <<"r:3bcb41395765d5affefd549a0bcc6f0f">>}], [{from, rest}])
do_task(#{<<"dataType">> := <<"card">>, <<"vuekey">> := Vuekey, <<"table">> := Table, <<"query">> := Query}, #task{sessiontoken = SessionToken}) ->
do_task(#{<<"dataType">> := <<"card">>, <<"vuekey">> := Vuekey, <<"table">> := Table, <<"query">> := Query}, #task{dashboardId = DashboardId, sessiontoken = SessionToken}) ->
case dgiot_parse:query_object(Table, Query, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"count">> := Count, <<"results">> := Results} = NewResults} ->
case Table of
<<"Product">> ->
Products =
lists:foldl(fun(X, Acc) ->
case X of
#{<<"objectId">> := ObjectId} ->
DeviceChild = getDevice(ObjectId, SessionToken),
Acc ++ [X#{<<"deviceChild">> => DeviceChild}];
Base64 =
case Table of
<<"Product">> ->
Products =
lists:foldl(fun(X, Acc) ->
case X of
#{<<"objectId">> := ObjectId} ->
DeviceChild = getDevice(ObjectId, SessionToken),
Acc ++ [X#{<<"deviceChild">> => DeviceChild}];
_ ->
Acc
end
end, [], Results),
base64:encode(jsx:encode(#{<<"dataType">> => <<"card">>, <<"vuekey">> => Vuekey, <<"table">> => Table, <<"value">> => #{<<"count">> => Count, <<"results">> => Products}}));
<<"ChartStatus">> ->
OnlineCount =
case dgiot_parse:query_object(<<"Device">>, #{<<"keys">> => [<<"count(*)">>], <<"where">> => #{<<"status">> => <<"ONLINE">>}}, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"count">> := OnlineCount1}} ->
OnlineCount1;
_ ->
Acc
end
end, [], Results),
Topic = <<"dashboard/", SessionToken/binary, "/post">>,
Base64 = base64:encode(jsx:encode(#{<<"dataType">> => <<"card">>, <<"vuekey">> => Vuekey, <<"table">> => Table, <<"value">> => #{<<"count">> => Count, <<"results">> => Products}})),
dgiot_mqtt:publish(self(), Topic, Base64);
<<"ChartStatus">> ->
OnlineCount =
case dgiot_parse:query_object(<<"Device">>, #{<<"keys">> => [<<"count(*)">>], <<"where">> => #{<<"status">> => <<"ONLINE">>}}, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"count">> := OnlineCount1}} ->
OnlineCount1;
_ ->
0
end,
OfflineCount =
case dgiot_parse:query_object(<<"Device">>, #{<<"keys">> => [<<"count(*)">>], <<"where">> => #{<<"status">> => <<"OFFLINE">>}}, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"count">> := OfflineCount2}} ->
OfflineCount2;
_ ->
0
end,
Topic = <<"dashboard/", SessionToken/binary, "/post">>,
Base64 = base64:encode(jsx:encode(#{<<"dataType">> => <<"card">>, <<"vuekey">> => Vuekey, <<"table">> => Table, <<"value">> => #{<<"chartData">> => #{<<"columns">> => [<<"状态"/utf8>>, <<"数量"/utf8>>], <<"rows">> => [#{<<"状态"/utf8>> => <<"在线"/utf8>>, <<"数量"/utf8>> => OnlineCount}, #{<<"状态"/utf8>> => <<"离线"/utf8>>, <<"数量"/utf8>> => OfflineCount}]}}})),
dgiot_mqtt:publish(self(), Topic, Base64);
_ ->
Topic = <<"dashboard/", SessionToken/binary, "/post">>,
Base64 = base64:encode(jsx:encode(#{<<"dataType">> => <<"card">>, <<"vuekey">> => Vuekey, <<"table">> => Table, <<"value">> => NewResults})),
dgiot_mqtt:publish(self(), Topic, Base64)
end;
0
end,
OfflineCount =
case dgiot_parse:query_object(<<"Device">>, #{<<"keys">> => [<<"count(*)">>], <<"where">> => #{<<"status">> => <<"OFFLINE">>}}, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"count">> := OfflineCount2}} ->
OfflineCount2;
_ ->
0
end,
base64:encode(jsx:encode(#{<<"dataType">> => <<"card">>, <<"vuekey">> => Vuekey, <<"table">> => Table,
<<"value">> => #{<<"chartData">> => #{
<<"columns">> => [<<"状态"/utf8>>, <<"数量"/utf8>>],
<<"rows">> => [
#{<<"状态"/utf8>> => <<"在线"/utf8>>, <<"数量"/utf8>> => OnlineCount},
#{<<"状态"/utf8>> => <<"离线"/utf8>>, <<"数量"/utf8>> => OfflineCount}]}}}));
_ ->
base64:encode(jsx:encode(#{<<"dataType">> => <<"card">>, <<"vuekey">> => Vuekey, <<"table">> => Table, <<"value">> => NewResults}))
end,
Topic = <<"$dg/dashboard/", DashboardId/binary, "/post">>,
dgiot_mqtt:publish(self(), Topic, Base64);
_ ->
pass
end;