Skip to content

Commit

Permalink
feat: printer
Browse files Browse the repository at this point in the history
  • Loading branch information
dawnwinterLiu committed Oct 13, 2022
1 parent aa8d242 commit a96c907
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 45 deletions.
24 changes: 15 additions & 9 deletions apps/dgiot_dlink/src/proctol/dgiot_dlink_proctol.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,21 @@ properties_report(ProductId, DevAddr, Payload) ->

%% json格式报文
firmware_report(ProductId, DevAddr, Payload) when is_map(Payload) ->
OldPayload =
case dgiot_hook:run_hook({dlink_properties_report, ProductId}, {ProductId, DevAddr, Payload}) of
{ok, [Payload1]} ->
Payload1;
_ ->
Payload
end,
NewPload = parse_payload(ProductId, OldPayload),
dgiot_task:save_td(ProductId, DevAddr, NewPload, #{});
%% OldPayload =
%% case dgiot_hook:run_hook({dlink_firmware_report, ProductId}, {ProductId, DevAddr, Payload}) of
%% {ok, [Payload1]} ->
%% Payload1;
%% _ ->
%% Payload
%% end,
%% NewPload = parse_payload(ProductId, OldPayload),
%% dgiot_task:save_td(ProductId, DevAddr, NewPload, #{});
lists:map(fun
({ChannelId, _Ctype}) ->
dgiot_channelx:do_message(ChannelId, {dlink_firmware_report, ProductId, DevAddr, Payload});
(_) ->
pass
end, dgiot_bridge:get_proctol_channel(ProductId));

%% 二进制报文
firmware_report(ProductId, DevAddr, Payload) ->
Expand Down
82 changes: 48 additions & 34 deletions apps/dgiot_dlink/src/proctol/dgiot_mqtt_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -75,38 +75,52 @@ check(#{username := _Username}, AuthResult, _) ->
description() -> "Authentication with Mnesia".

do_check(AuthResult, Password, ProductID, DeviceAddr, DeviceId, Ip) ->
case dgiot_product:lookup_prod(ProductID) of
{ok, #{<<"productSecret">> := Password} = Product} ->
case dgiot_device:lookup(DeviceId) of
{ok, _} ->
Body = #{
<<"status">> => <<"ONLINE">>},
dgiot_device:online(DeviceId),
dgiot_parse:update_object(<<"Device">>, DeviceId, Body);
_ ->
case Product of
#{<<"ACL">> := Acl, <<"name">> := Name, <<"devType">> := DevType, <<"dynamicReg">> := true} ->
Device = #{
<<"ip">> => Ip,
<<"status">> => <<"ONLINE">>,
<<"brand">> => Name,
<<"devModel">> => DevType,
<<"name">> => DeviceAddr,
<<"devaddr">> => DeviceAddr,
<<"product">> => ProductID,
<<"ACL">> => Acl
},
dgiot_device:create_device(Device);
_ ->
pass
end
end,
{stop, AuthResult#{anonymous => false, auth_result => success}};
Result =
case dgiot_product:lookup_prod(ProductID) of
{ok, #{<<"productSecret">> := Password} = Product} ->
case dgiot_device:lookup(DeviceId) of
{ok, _} ->
Body = #{
<<"status">> => <<"ONLINE">>},
dgiot_device:online(DeviceId),
dgiot_parse:update_object(<<"Device">>, DeviceId, Body);
_ ->
case Product of
#{<<"ACL">> := Acl, <<"name">> := Name, <<"devType">> := DevType, <<"dynamicReg">> := true} ->
Device = #{
<<"ip">> => Ip,
<<"status">> => <<"ONLINE">>,
<<"brand">> => Name,
<<"devModel">> => DevType,
<<"name">> => DeviceAddr,
<<"devaddr">> => DeviceAddr,
<<"product">> => ProductID,
<<"ACL">> => Acl
},
dgiot_device:create_device(Device);
_ ->
pass
end
end,
{stop, AuthResult#{anonymous => false, auth_result => success}};
_ ->
case dgiot_device:lookup(DeviceId) of
{ok, #{<<"devicesecret">> := Password}} ->
{stop, AuthResult#{anonymous => false, auth_result => success}};
_ ->
{stop, AuthResult#{anonymous => false, auth_result => password_error}}
end
end,

case Result of
{stop, #{auth_result := success}} ->
lists:map(fun
({ChannelId, _Ctype}) ->
dgiot_channelx:do_message(ChannelId, {dlink_login, do_after, ProductID, DeviceAddr, DeviceId, Ip});
(_) ->
pass
end, dgiot_bridge:get_proctol_channel(ProductID));
_ ->
case dgiot_device:lookup(DeviceId) of
{ok, #{<<"devicesecret">> := Password}} ->
{stop, AuthResult#{anonymous => false, auth_result => success}};
_ ->
{stop, AuthResult#{anonymous => false, auth_result => password_error}}
end
end.
pass
end,
Result.
2 changes: 1 addition & 1 deletion apps/dgiot_printer/src/dgiot_printer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
stop_hooks/1
]).


handle_profile({QueryData, ProductId, _State}) ->
get_topo(QueryData, ProductId).

Expand All @@ -37,6 +36,7 @@ get_topo(#{<<"profile">> := Profile} = QueryData, ProductId) ->
maps:fold(fun(K, V, Acc) ->
case maps:find(K, Profile) of
error ->

Acc ++ [V];
{ok, Text} ->
Acc ++ [V#{<<"text">> => Text}]
Expand Down
20 changes: 19 additions & 1 deletion apps/dgiot_printer/src/dgiot_printer_channel.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,27 @@ handle_init(State) ->

%% 通道消息处理,注意:进程池调用
handle_event(_EventId, Event, State) ->
?LOG(info,"channel ~p", [Event]),
?LOG(info, "channel ~p", [Event]),
{ok, State}.

handle_message({dlink_login, do_after, ProductId, Devaddr, DeviceId, _Ip}, State) ->
timer:sleep(1 * 1000),
RequestTopic = <<"$dg/device/", ProductId/binary, "/", Devaddr/binary, "/properties">>,
Payload = #{<<"cmd">> => <<"scan_printer">>, <<"data">> => #{}},
dgiot_mqtt:publish(DeviceId, RequestTopic, jsx:encode(Payload)),
{ok, State};

handle_message({dlink_firmware_report, ProductId, DevAddr, Payload}, State) ->
DeviceId = dgiot_parse_id:get_deviceid(ProductId, DevAddr),
case dgiot_parse:get_object(<<"Device">>, DeviceId) of
{ok, Device} ->
Profile = maps:get(<<"profile">>, Device, #{}),
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"profile">> => Profile#{<<"printers">> => Payload}});
_ ->
pass
end,
{ok, State};

handle_message(_Message, State) ->
{ok, State}.

Expand Down

0 comments on commit a96c907

Please sign in to comment.