Skip to content

Commit

Permalink
feat: mqtt add notification
Browse files Browse the repository at this point in the history
  • Loading branch information
dawnwinterLiu committed Apr 22, 2024
1 parent 03e0e99 commit 59d44aa
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ apps/dgiot_12184/
apps/dgiot_modbusx/
apps/dgiot_rtsp2ws/
apps/dgiot_sophon/
apps/dgiot_open62541/
node_modules/
package-lock.json
erlang_ls.config
Expand Down
2 changes: 1 addition & 1 deletion apps/dgiot/src/transport/dgiot_mqtt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ subscribe_route_key(Topics, Type, SessionToken) ->
lists:foldl(fun(X, Acc) ->
case dgiot_data:get({dlink_client, SessionToken}) of
not_find ->
pass;
dgiot_mqtt:subscribe_mgmt(SessionToken, X);
Clients ->
lists:foldl(fun(Client, _) ->
dgiot_mqtt:subscribe_mgmt(Client, X)
Expand Down
2 changes: 1 addition & 1 deletion apps/dgiot_device/src/dgiot_product.erl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ save(Product) ->
save_productSecret(ProductId),
dgiot_product_channel:save_channel(ProductId),
dgiot_product_channel:save_tdchannel(ProductId),
%% dgiot_product_channel:save_taskchannel(ProductId),
dgiot_product_channel:save_taskchannel(ProductId),
save_device_thingtype(ProductId),
save_product_identifier(ProductId),
dgiot_product_enum:save_product_enum(ProductId),
Expand Down
9 changes: 8 additions & 1 deletion apps/dgiot_dlink/src/proctol/dgiot_mqtt_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ save_client(<<Token:34/binary, _Type/binary>> = ClientId) ->
dgiot_data:insert({dlink_client, Token}, New_ClientIds)
end.

sub_notification(ClientId, Roles) ->
maps:fold(fun(RoleId, _, _) ->
dgiot_mqtt:subscribe_route_key([<<"$dg/user/dashboard/notification/", RoleId/binary, "/#">>], <<"notification">>, ClientId)
end, {}, Roles).

check(#{peerhost := PeerHost, username := <<"dgiot">>}, AuthResult, _) when PeerHost == {127, 0, 0, 1} ->
{ok, AuthResult#{anonymous => false, auth_result => success}};

Expand All @@ -57,7 +62,9 @@ check(#{username := Username}, AuthResult, _)
check(#{clientid := <<Token:34/binary, _Type/binary>> = ClientId, username := UserId, password := Token}, AuthResult, #{hash_type := _HashType}) ->
%% io:format("~s ~p UserId: ~p~n", [?FILE, ?LINE, UserId]),
case dgiot_auth:get_session(Token) of
#{<<"objectId">> := UserId} ->
#{<<"objectId">> := UserId} = User ->
%% 登录时订阅告警推送
sub_notification(ClientId, maps:get(<<"roles">>, User, #{})),
save_client(ClientId),
{stop, AuthResult#{anonymous => false, auth_result => success}};
_ ->
Expand Down
9 changes: 6 additions & 3 deletions apps/dgiot_http/src/umeng/dgiot_umeng.erl
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ sendSubscribe(_O) ->
%% 推送前端弹框
send_dashboard(#{<<"_deviceid">> := DeviceId, <<"dgiot_alarmvalue">> := Alarmvalue} = Args) ->
case dgiot_parse:get_object(<<"Device">>, DeviceId) of
{ok, #{<<"name">> := DeviceName, <<"product">> := #{<<"objectId">> := ProductId}}} ->
{ok, #{<<"ACL">> := ACL, <<"name">> := DeviceName, <<"product">> := #{<<"objectId">> := ProductId}}} ->
ProductName =
case dgiot_parse:get_object(<<"Product">>, ProductId) of
{ok, #{<<"name">> := Name1}} ->
Expand All @@ -653,8 +653,11 @@ send_dashboard(#{<<"_deviceid">> := DeviceId, <<"dgiot_alarmvalue">> := Alarmval
<<"type">> => <<"warn">>,
<<"description">> => maps:get(<<"description">>, Args, <<>>)
},
Pubtopic = <<"$dg/user/dashboard/notification/report">>,
dgiot_mqtt:publish(DeviceId, Pubtopic, dgiot_json:encode(Data));
RoleIds = dgiot_role:get_roleids(maps:keys(ACL)),
lists:foldl(fun(RoleId, _) ->
Pubtopic = <<"$dg/user/dashboard/notification/", RoleId/binary, "/report">>,
dgiot_mqtt:publish(DeviceId, Pubtopic, dgiot_json:encode(Data))
end, {}, RoleIds);
_ ->
pass
end;
Expand Down

0 comments on commit 59d44aa

Please sign in to comment.