Skip to content

Commit

Permalink
fix: tcp save device log
Browse files Browse the repository at this point in the history
  • Loading branch information
lsxredrain committed Oct 10, 2021
1 parent 868c8d0 commit ef4a240
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* add tcp channel device log ([c74b246](https://github.com/dgiot/dgiot/commit/c74b246443244f981e847bcb1f559f7ec4d2d375))
* add tcp connect info ([39a1cc9](https://github.com/dgiot/dgiot/commit/39a1cc93ecf25796eabca67aac1789258dc58037))
* add tcp metircs ([f8e19b1](https://github.com/dgiot/dgiot/commit/f8e19b1b241200b5d1324d689f4e2d9536e74a4d))
* add tcp save log ([868c8d0](https://github.com/dgiot/dgiot/commit/868c8d09e732ca3872ffe4ef5149263c2d5e97ae))
* add tcp transparent ([b1f091c](https://github.com/dgiot/dgiot/commit/b1f091cba9fa143fcc8040a6fa8848ac89412e96))
* add tdengie metrics ([c38b046](https://github.com/dgiot/dgiot/commit/c38b046f1939bf6d59a106a62b2b1f3c86a89127))
* add trace log, Modify meter channel ([d4b9fd7](https://github.com/dgiot/dgiot/commit/d4b9fd758e2ca2643bdf5c560aa32ff441fac22f))
Expand Down
13 changes: 7 additions & 6 deletions apps/dgiot_bridge/src/channel/dgiot_tcp_channel.erl
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ handle_info({deliver, _, Msg}, TCPState) ->
Payload = dgiot_mqtt:get_payload(Msg),
Topic = dgiot_mqtt:get_topic(Msg),
case binary:split(Topic, <<$/>>, [global, trim]) of
[<<"thing">>, ProductId,DevAddr,<<"tcp">>, <<"hex">>] ->
DeviceId = dgiot_parse:get_deviceid(ProductId,DevAddr),
[<<"thing">>, ProductId, DevAddr, <<"tcp">>, <<"hex">>] ->
DeviceId = dgiot_parse:get_deviceid(ProductId, DevAddr),
dgiot_device:save_log(DeviceId, Payload, ['tcp_send']),
dgiot_tcp_server:send(TCPState, dgiot_utils:hex_to_binary(dgiot_utils:trim_string(Payload))),
{noreply, TCPState};
Expand Down Expand Up @@ -214,16 +214,15 @@ handle_info({tcp, Buff}, #tcp{socket = Socket, state = #state{id = ChannelId, de
{<<>>, <<>>} ->
{noreply, TCPState#tcp{buff = <<>>}};
{_, _} ->
dgiot_device:sub_topic(DeviceId,<<"tcp/hex">>),
NewProducts = dgiot_utils:unique_1(Products ++ [NewProductId]),
dgiot_bridge:send_log(ChannelId, NewProductId, DtuAddr, "DeviceId ~p DTU revice from ~p", [DeviceId,DtuAddr]),
dgiot_bridge:send_log(ChannelId, NewProductId, DtuAddr, "DeviceId ~p DTU revice from ~p", [DeviceId, DtuAddr]),
{noreply, TCPState#tcp{buff = <<>>, register = true, clientid = DeviceId,
state = State#state{devaddr = DtuAddr, product = NewProducts, deviceId = DeviceId}}}
end
end;

handle_info({tcp, Buff}, #tcp{state = #state{id = ChannelId, product = Products, deviceId = DeviceId}} = TCPState) ->
dgiot_device:save_log(DeviceId,dgiot_utils:binary_to_hex(dgiot_utils:to_binary(Buff)),['tcp_receive']),
dgiot_device:save_log(DeviceId, dgiot_utils:binary_to_hex(Buff), ['tcp_receive']),
case decode(Buff, Products, TCPState) of
{ok, [], NewTCPState} ->
{noreply, NewTCPState#tcp{buff = <<>>}};
Expand Down Expand Up @@ -329,7 +328,6 @@ handle_frames([Frame | Frames], TCPState) ->
update_state(Env, #tcp{state = State} = TCPState) ->
TCPState#tcp{state = State#state{env = maps:without([<<"send">>], Env)}}.


create_device(DeviceId, ProductId, DTUMAC, DTUIP, Dtutype) ->
case dgiot_parse:get_object(<<"Product">>, ProductId) of
{ok, #{<<"ACL">> := Acl, <<"devType">> := DevType}} ->
Expand Down Expand Up @@ -362,12 +360,15 @@ create_device(DeviceId, ProductId, DTUMAC, DTUIP, Dtutype) ->
<<"">>
end,
?MLOG(info, #{<<"deviceid">> => DeviceId, <<"devaddr">> => DTUMAC, <<"productid">> => ProductId, <<"productname">> => Productname, <<"devicename">> => <<Dtutype/binary, DTUMAC/binary>>}, ['online']),
dgiot_device:sub_topic(DeviceId, <<"tcp/hex">>),
dgiot_device:save_log(DeviceId, dgiot_utils:binary_to_hex(DTUMAC), ['tcp_receive']),
{DeviceId, DTUMAC};
Error2 ->
?LOG(info, "Error2 ~p ", [Error2]),
{<<>>, <<>>}
end.


create_instruct(ACL, DtuProductId, DtuDevId) ->
case dgiot_product:lookup_prod(DtuProductId) of
{ok, #{<<"thing">> := #{<<"properties">> := Properties}}} ->
Expand Down
13 changes: 7 additions & 6 deletions apps/dgiot_device/src/dgiot_device.erl
Original file line number Diff line number Diff line change
Expand Up @@ -467,22 +467,23 @@ get_appname(ProductId, DevAddr) ->
<<"admin">>
end.

save_log(DeviceId,Payload,Domain) ->
save_log(DeviceId, Payload, Domain) ->
case dgiot_device:lookup(DeviceId) of
{ok,{[true,_,_,DeviceName, Devaddr, ProductId],_}} ->
{ok,{[_,_,_,DeviceName, Devaddr, ProductId],_}} ->
?MLOG(info, #{
<<"deviceid">> => DeviceId,
<<"devaddr">> => Devaddr,
<<"productid">> => ProductId,
<<"devicename">> => DeviceName,
<<"msg">> => Payload}, Domain);
_ -> pass
_ ->
pass
end.

sub_topic(DeviceId,Type) ->
case dgiot_device:lookup(DeviceId) of
{ok,{[true,_,_,_DeviceName, Devaddr, ProductId],_}} ->
Topic2 = <<"thing/", ProductId/binary,"/",Devaddr/binary, "/", Type/binary>>,
dgiot_mqtt:subscribe(Topic2);
{ok,{[_, _, _, _DeviceName, Devaddr, ProductId],_}} ->
Topic = <<"thing/", ProductId/binary,"/",Devaddr/binary, "/", Type/binary>>,
dgiot_mqtt:subscribe(Topic);
_ -> pass
end.

0 comments on commit ef4a240

Please sign in to comment.