mirror of
https://gitee.com/dgiiot/dgiot.git
synced 2024-11-29 18:57:41 +08:00
feat: add log
This commit is contained in:
parent
9d8d21a02a
commit
aa9913e8aa
@ -25,7 +25,7 @@
|
||||
-export([parse_cache_Device/1, sync_parse/1, get/2, post/1, post/2, put/1, save/1, save/2, lookup/1, lookup/2, delete/1, delete/2]).
|
||||
-export([save_profile/1, get_profile/1, get_profile/2, get_online/1, online/1, offline/1, offline_child/1, enable/1, disable/1]).
|
||||
-export([put_color/3, get_color/2, put_location/3, get_location/1, get_address/3, get_productid/1]).
|
||||
-export([get_acl/1, get_readonly_acl/1, save_log/3, get_url/1, get_appname/1]).
|
||||
-export([get_acl/1, get_readonly_acl/1, save_log/3, get_url/1, get_appname/1, save_log/4]).
|
||||
|
||||
parse_cache_Device(Skip) ->
|
||||
dgiot_device_cache:parse_cache_Device(Skip).
|
||||
@ -178,9 +178,9 @@ create_device(#{<<"status">> := Status, <<"brand">> := Brand, <<"devModel">> :=
|
||||
<<"devaddr">> := DevAddr, <<"product">> := ProductId, <<"ACL">> := Acl} = Device) ->
|
||||
DeviceId = maps:get(<<"objectId">>, Device, dgiot_parse_id:get_deviceid(ProductId, DevAddr)),
|
||||
case dgiot_parsex:get_object(<<"Device">>, DeviceId) of
|
||||
{ok, #{<<"ip">> := Ip} = Result} ->
|
||||
{ok, Result} ->
|
||||
Body = #{
|
||||
<<"ip">> => maps:get(<<"ip">>, Device, Ip),
|
||||
<<"ip">> => maps:get(<<"ip">>, Device, maps:get(<<"ip">>, Result, <<>>)),
|
||||
<<"status">> => Status},
|
||||
dgiot_parsex:update_object(<<"Device">>, DeviceId, Body),
|
||||
dgiot_device:put(#{<<"objectId">> => DeviceId, <<"status">> => Status}),
|
||||
@ -226,6 +226,7 @@ create_device(#{<<"status">> := Status, <<"brand">> := Brand, <<"devModel">> :=
|
||||
|
||||
create_device(ProductId, DeviceAddr, Ip) ->
|
||||
DeviceId = dgiot_parse_id:get_deviceid(ProductId, DeviceAddr),
|
||||
dgiot_device:save_log(ProductId, DeviceAddr, DeviceAddr, <<"online">>),
|
||||
case dgiot_device:lookup(DeviceId) of
|
||||
{ok, _} ->
|
||||
Body = #{<<"status">> => <<"ONLINE">>},
|
||||
@ -338,3 +339,14 @@ get_readonly_acl(DeviceId) ->
|
||||
_ ->
|
||||
#{}
|
||||
end.
|
||||
|
||||
|
||||
save_log(ProductId, DevAddr, Data, Domain) ->
|
||||
dgiot_parsex:create_object(<<"Log">>, #{
|
||||
<<"deviceid">> => dgiot_parse_id:get_deviceid(ProductId, DevAddr)
|
||||
, <<"productid">> => ProductId
|
||||
, <<"msg">> => dgiot_json:encode(#{<<"data">> => Data})
|
||||
, <<"domain">> => [Domain]
|
||||
, <<"devaddr">> => DevAddr
|
||||
, <<"time">> => dgiot_datetime:nowstamp() * 1000
|
||||
}).
|
||||
|
@ -101,6 +101,7 @@ properties_report(ProductId, DevAddr, Payload) when is_map(Payload) ->
|
||||
end,
|
||||
NewPload = parse_payload(ProductId, OldPayload),
|
||||
dgiot_device_profile:publish(ProductId, DevAddr, NewPload),
|
||||
dgiot_device:save_log(ProductId, DevAddr, NewPload, <<"reportProperty">>),
|
||||
dgiot_task:save_td(ProductId, DevAddr, NewPload, #{});
|
||||
|
||||
%% 二进制报文
|
||||
|
@ -23,6 +23,7 @@
|
||||
on_client_disconnected(#{clientid := <<ProductID:10/binary, "_", DeviceAddr/binary>>, username := ProductID}, _ReasonCode, #{disconnected_at := _DisconnectedAt}, _State) ->
|
||||
DeviceId = dgiot_parse_id:get_deviceid(ProductID, DeviceAddr),
|
||||
dgiot_device:offline(DeviceId),
|
||||
dgiot_device:save_log(ProductID, DeviceAddr, DeviceAddr, <<"offline">>),
|
||||
io:format("~s ~p ProductID = ~p DeviceAddr ~p DeviceId ~p ~n", [?FILE, ?LINE, ProductID, DeviceAddr, DeviceId]),
|
||||
ok;
|
||||
|
||||
|
@ -67,6 +67,7 @@ create_dtu(DtuAddr, ChannelId, DTUIP) ->
|
||||
<<"brand">> => <<"DTU", DtuAddr/binary>>,
|
||||
<<"devModel">> => <<"DTU_">>
|
||||
},
|
||||
dgiot_device:save_log(ProductId, DtuAddr, DtuAddr, <<"online">>),
|
||||
dgiot_device:create_device(Requests);
|
||||
_ ->
|
||||
pass
|
||||
@ -147,6 +148,7 @@ create_meter4G(DevAddr, ChannelId, DTUIP) ->
|
||||
<<"brand">> => <<"Concentrator", DevAddr/binary>>,
|
||||
<<"devModel">> => <<"Concentrator">>
|
||||
},
|
||||
dgiot_device:save_log(ProductId, DevAddr, DevAddr, <<"online">>),
|
||||
dgiot_device:create_device(Requests),
|
||||
dgiot_task:save_pnque(ProductId, DevAddr, ProductId, DevAddr);
|
||||
_ ->
|
||||
|
@ -92,6 +92,7 @@ handle_info({tcp, Buff}, #tcp{socket = Socket, state = #state{id = ChannelId, dt
|
||||
dgiot_mqtt:subscribe(Topic2),
|
||||
{NewRef, NewStep} = {undefined, read_meter},
|
||||
DtuId = dgiot_parse_id:get_deviceid(DtuProductId, DtuAddr),
|
||||
dgiot_device:save_log(ProductId, DtuAddr, DtuAddr, <<"online">>),
|
||||
case Search of
|
||||
<<"nosearch">> ->
|
||||
lists:map(fun(X) ->
|
||||
@ -192,7 +193,6 @@ handle_info({tcp, Buff}, #tcp{socket = Socket, clientid = DtuId, state = #state{
|
||||
case Frames of
|
||||
[#{<<"con">> := 1, <<"frame">> := Frame} | _] ->
|
||||
dgiot_bridge:send_log(ChannelId, "~s ~p DLT376 response: ~p ", [?FILE, ?LINE, dgiot_utils:binary_to_hex(Frame)]),
|
||||
%% io:format("~s ~p DLT376 send = ~p.~n", [?FILE, ?LINE, dgiot_utils:binary_to_hex(Buff)]),
|
||||
dgiot_tcp_server:send(TCPState, Frame); %%回复确认
|
||||
[#{<<"afn">> := 16#0A, <<"di">> := <<16#00, 16#00, 16#02, 16#01>>} | _] ->
|
||||
dlt376_decoder:process_message(Frames, ChannelId, DTUIP, DtuId); %%注册或更新电表信
|
||||
@ -208,6 +208,7 @@ handle_info({tcp, Buff}, #tcp{socket = Socket, clientid = DtuId, state = #state{
|
||||
dgiot_bridge:send_log(ChannelId, ProductId, DevAddr, "~s ~p Dtu to task ~p ~ts ", [?FILE, ?LINE, Topic, unicode:characters_to_list(dgiot_json:encode(Value))]),
|
||||
timer:sleep(1 * 1000),
|
||||
lists:foldl(fun(#{<<"productid">> := ChildProductId, <<"addr">> := ChildDevAddr, <<"value">> := ChildValue}, _Acc) ->
|
||||
dgiot_device:save_log(ChildProductId, ChildDevAddr, ChildValue, <<"reportProperty">>),
|
||||
ChildTopic = dgiot_meter:send_task(ChildProductId, ChildDevAddr, DtuId, ChildValue),
|
||||
dgiot_bridge:send_log(ChannelId, ProductId, DevAddr, "~s ~p DtuChild to task ~p ~ts ", [?FILE, ?LINE, ChildTopic, unicode:characters_to_list(dgiot_json:encode(ChildValue))]),
|
||||
timer:sleep(1 * 1000)
|
||||
@ -215,6 +216,7 @@ handle_info({tcp, Buff}, #tcp{socket = Socket, clientid = DtuId, state = #state{
|
||||
% 抄表数据返回
|
||||
#{<<"productid">> := ProductId, <<"addr">> := Addr, <<"value">> := Value} ->
|
||||
DevAddr = dgiot_utils:binary_to_hex(Addr),
|
||||
dgiot_device:save_log(ProductId, DevAddr, Value, <<"reportProperty">>),
|
||||
Topic = dgiot_meter:send_task(ProductId, DevAddr, DtuId, Value),
|
||||
dgiot_bridge:send_log(ChannelId, ProductId, DevAddr, "~s ~p Dtu to task ~p ~ts ", [?FILE, ?LINE, Topic, unicode:characters_to_list(dgiot_json:encode(Value))]);
|
||||
Error ->
|
||||
@ -235,6 +237,7 @@ handle_info({tcp, Buff}, #tcp{socket = Socket, clientid = DtuId, state = #state{
|
||||
% 抄表数据返回
|
||||
#{<<"productid">> := ProductId, <<"addr">> := Addr, <<"value">> := Value} ->
|
||||
DevAddr = dgiot_utils:binary_to_hex(Addr),
|
||||
dgiot_device:save_log(ProductId, DevAddr, Value, <<"reportProperty">>),
|
||||
Topic = dgiot_meter:send_task(ProductId, DevAddr, DtuId, Value),
|
||||
dgiot_bridge:send_log(ChannelId, ProductId, DevAddr, "~s ~p to task ~p ~ts ", [?FILE, ?LINE, Topic, unicode:characters_to_list(dgiot_json:encode(Value))]);
|
||||
Error ->
|
||||
@ -269,12 +272,14 @@ handle_info({deliver, _Topic, Msg}, #tcp{state = #state{id = ChannelId, protocol
|
||||
DataSource = jsx:decode(dgiot_mqtt:get_payload(Msg), [{labels, binary}, return_maps]),
|
||||
Payload1 = dgiot_meter:to_frame(#{<<"devaddr">> => DevAddr, <<"dataSource">> => DataSource}),
|
||||
dgiot_bridge:send_log(ChannelId, ProductId, DevAddr, " ~s ~p DLT376 send to DevAddr ~p => ~p", [?FILE, ?LINE, DevAddr, dgiot_utils:binary_to_hex(Payload1)]),
|
||||
dgiot_device:save_log(ProductId, DevAddr, dgiot_utils:binary_to_hex(Payload1), <<"readProperty">>),
|
||||
dgiot_tcp_server:send(TCPState, Payload1);
|
||||
{Da, Dtuaddr} ->
|
||||
DA = dgiot_utils:binary_to_hex(dlt376_decoder:pn_to_da(dgiot_utils:to_int(Da))),
|
||||
DataSource = jsx:decode(dgiot_mqtt:get_payload(Msg), [{labels, binary}, return_maps]),
|
||||
Payload1 = dgiot_meter:to_frame(#{<<"protocol">> => Protocol, <<"devaddr">> => Dtuaddr, <<"dataSource">> => DataSource#{<<"da">> => DA}}),
|
||||
dgiot_bridge:send_log(ChannelId, ProductId, DevAddr, " ~s ~p DLT376 send to DevAddr ~p => ~p", [?FILE, ?LINE, DevAddr, dgiot_utils:binary_to_hex(Payload1)]),
|
||||
dgiot_device:save_log(ProductId, DevAddr, dgiot_utils:binary_to_hex(Payload1), <<"readProperty">>),
|
||||
dgiot_tcp_server:send(TCPState, Payload1)
|
||||
end;
|
||||
?DLT645 ->
|
||||
@ -286,6 +291,7 @@ handle_info({deliver, _Topic, Msg}, #tcp{state = #state{id = ChannelId, protocol
|
||||
},
|
||||
Payload1 = dgiot_meter:to_frame(ThingData),
|
||||
dgiot_bridge:send_log(ChannelId, "~s ~p DLT645 to dev => ~p", [?FILE, ?LINE, dgiot_utils:binary_to_hex(Payload1)]),
|
||||
dgiot_device:save_log(ProductId, DevAddr, dgiot_utils:binary_to_hex(Payload1), <<"readProperty">>),
|
||||
dgiot_tcp_server:send(TCPState, Payload1)
|
||||
end,
|
||||
{noreply, TCPState};
|
||||
@ -295,11 +301,13 @@ handle_info({deliver, _Topic, Msg}, #tcp{state = #state{id = ChannelId, protocol
|
||||
?DLT376 ->
|
||||
Payload2 = dlt376_decoder:frame_write_param(#{<<"concentrator">> => DevAddr, <<"payload">> => ProfilePayload}),
|
||||
dgiot_bridge:send_log(ChannelId, " ~s ~p DLT376(下发) send to DevAddr ~p => ~p", [?FILE, ?LINE, DevAddr, dgiot_utils:binary_to_hex(Payload2)]),
|
||||
dgiot_device:save_log(ProductId, DevAddr, dgiot_utils:binary_to_hex(Payload2), <<"device_operationlog">>),
|
||||
dgiot_tcp_server:send(TCPState, Payload2);
|
||||
?DLT645 ->
|
||||
%% dgiot_umeng:send_message_to3D(ProductId, DevAddr, ProfilePayload),
|
||||
Payload1 = dlt645_decoder:frame_write_param(#{<<"meter">> => DevAddr, <<"payload">> => ProfilePayload}),
|
||||
?LOG(info, "DLT645 Payload1 :~p ~n~n", [dgiot_utils:binary_to_hex(Payload1)]),
|
||||
dgiot_device:save_log(ProductId, DevAddr, dgiot_utils:binary_to_hex(Payload1), <<"device_operationlog">>),
|
||||
dgiot_tcp_server:send(TCPState, Payload1)
|
||||
end,
|
||||
{noreply, TCPState#tcp{state = State#state{env = #{product => ProductId, devaddr => DevAddr}}}};
|
||||
@ -328,6 +336,7 @@ terminate(_Reason, #tcp{state = #state{dtuaddr = DtuAddr, product = ProductId}}
|
||||
DeviceId = dgiot_parse_id:get_deviceid(ProductId, DtuAddr),
|
||||
Taskchannel = dgiot_product_channel:get_taskchannel(ProductId),
|
||||
dgiot_task:del_pnque(DeviceId),
|
||||
dgiot_device:save_log(ProductId, DtuAddr, DtuAddr, <<"unregister">>),
|
||||
dgiot_client:stop(Taskchannel, DeviceId),
|
||||
ok;
|
||||
|
||||
|
@ -58,6 +58,7 @@ handle_info({tcp, Buff}, #tcp{socket = Socket, state = #state{id = ChannelId, de
|
||||
{match, [Head]} when length(List) == Len ->
|
||||
DeviceId = dgiot_parse_id:get_deviceid(ProductId, DtuAddr),
|
||||
create_device(DeviceId, ProductId, DtuAddr, DTUIP, Dtutype),
|
||||
dgiot_device:save_log(ProductId, DtuAddr, DtuAddr, <<"online">>),
|
||||
dgiot_bridge:send_log(ChannelId, ProductId, DtuAddr, "~s ~p DTU login DtuAddr:~p", [?FILE, ?LINE, DtuAddr]),
|
||||
Topic = <<"$dg/device/", ProductId/binary, "/", DtuAddr/binary, "/profile">>,
|
||||
dgiot_mqtt:subscribe(Topic),
|
||||
@ -67,6 +68,7 @@ handle_info({tcp, Buff}, #tcp{socket = Socket, state = #state{id = ChannelId, de
|
||||
{match, [Head]} when length(List1) == Len ->
|
||||
DeviceId = dgiot_parse_id:get_deviceid(ProductId, Buff),
|
||||
create_device(DeviceId, ProductId, Buff, DTUIP, Dtutype),
|
||||
dgiot_device:save_log(ProductId, Buff, Buff, <<"online">>),
|
||||
Topic = <<"$dg/device/", ProductId/binary, "/", Buff/binary, "/profile">>,
|
||||
dgiot_bridge:send_log(ChannelId, ProductId, Buff, "~s ~p DTU login DtuAddr:~p", [?FILE, ?LINE, Buff]),
|
||||
dgiot_mqtt:subscribe(Topic),
|
||||
@ -81,6 +83,7 @@ handle_info({tcp, Buff}, #tcp{state = #state{id = ChannelId, devaddr = DtuAddr,
|
||||
dgiot_bridge:send_log(ChannelId, ProductId, DtuAddr, "~p ~s ~p DTU ~p recv ~p", [dgiot_datetime:format("YYYY-MM-DD HH:NN:SS"), ?FILE, ?LINE, DtuAddr, dgiot_utils:binary_to_hex(Buff)]),
|
||||
<<H:8, L:8>> = dgiot_utils:hex_to_binary(modbus_rtu:is16(Di)),
|
||||
<<Sh:8, Sl:8>> = dgiot_utils:hex_to_binary(modbus_rtu:is16(Pn)),
|
||||
dgiot_device:save_log(ProductId, DtuAddr, dgiot_utils:binary_to_hex(Buff), <<"tcp_receive">>),
|
||||
case modbus_rtu:parse_frame(Buff, #{}, #{
|
||||
<<"dtuproduct">> => ProductId,
|
||||
<<"channel">> => ChannelId,
|
||||
@ -92,6 +95,7 @@ handle_info({tcp, Buff}, #tcp{state = #state{id = ChannelId, devaddr = DtuAddr,
|
||||
dgiot_bridge:send_log(ChannelId, ProductId, DtuAddr, "~s ~p to task ~p ~ts ", [?FILE, ?LINE, NewTopic, unicode:characters_to_list(dgiot_json:encode(Things))]),
|
||||
DeviceId = dgiot_parse_id:get_deviceid(ProductId, DtuAddr),
|
||||
Taskchannel = dgiot_product_channel:get_taskchannel(ProductId),
|
||||
dgiot_device:save_log(ProductId, DtuAddr, Things, <<"reportProperty">>),
|
||||
dgiot_client:send(Taskchannel, DeviceId, NewTopic, Things);
|
||||
Other ->
|
||||
?LOG(info, "Other ~p", [Other]),
|
||||
@ -102,6 +106,7 @@ handle_info({tcp, Buff}, #tcp{state = #state{id = ChannelId, devaddr = DtuAddr,
|
||||
%% 主动上报 Buff = <<"01 03 0000 000C45CF 0103184BC73E373AB53E361BFD3E4100000000000000000000000021AC">>.
|
||||
handle_info({tcp, Buff}, #tcp{state = #state{id = ChannelId, devaddr = DtuAddr, env = <<>>, product = DtuProductId} = State} = TCPState) ->
|
||||
dgiot_bridge:send_log(ChannelId, DtuProductId, DtuAddr, "~p ~s ~p DTU ~p recv ~p", [dgiot_datetime:format("YYYY-MM-DD HH:NN:SS"), ?FILE, ?LINE, DtuAddr, dgiot_utils:binary_to_hex(Buff)]),
|
||||
dgiot_device:save_log(DtuProductId, DtuAddr, dgiot_utils:binary_to_hex(Buff), <<"other">>),
|
||||
case modbus_rtu:dealwith(Buff) of
|
||||
{ok, #{<<"buff">> := NewBuff, <<"slaveId">> := SlaveId, <<"address">> := Address}} ->
|
||||
case modbus_rtu:parse_frame(NewBuff, #{}, #{
|
||||
@ -137,6 +142,7 @@ handle_info({deliver, _, Msg}, #tcp{state = #state{id = ChannelId} = State} = TC
|
||||
Payloads = modbus_rtu:set_params(ProfilePayload, ProductId, DevAddr),
|
||||
lists:map(fun(X) ->
|
||||
timer:sleep(100),
|
||||
dgiot_device:save_log(ProductId, DevAddr, dgiot_utils:binary_to_hex(X), <<"device_operationlog">>),
|
||||
dgiot_tcp_server:send(TCPState, X)
|
||||
end, Payloads),
|
||||
{noreply, TCPState};
|
||||
@ -145,6 +151,7 @@ handle_info({deliver, _, Msg}, #tcp{state = #state{id = ChannelId} = State} = TC
|
||||
#{<<"_dgiotTaskFreq">> := Freq, <<"slaveid">> := SlaveId, <<"address">> := Address} = DataSource ->
|
||||
Data = modbus_rtu:to_frame(DataSource),
|
||||
%% io:format("~s ~p Data = ~p.~n", [?FILE, ?LINE, dgiot_utils:to_hex(Data)]),
|
||||
dgiot_device:save_log(ProductId, DevAddr, dgiot_utils:binary_to_hex(Data), <<"readProperty">>),
|
||||
dgiot_bridge:send_log(ChannelId, ProductId, DevAddr, "Channel sends ~p to DTU ~p", [dgiot_utils:binary_to_hex(Data), DevAddr]),
|
||||
dgiot_tcp_server:send(TCPState, Data),
|
||||
{noreply, TCPState#tcp{state = State#state{hb = Freq, env = #{product => ProductId, pn => SlaveId, di => Address}}}};
|
||||
@ -163,6 +170,7 @@ handle_info({deliver, _, Msg}, #tcp{state = #state{id = ChannelId} = State} = TC
|
||||
Payloads = modbus_rtu:set_params(ProfilePayload, ProductId, DevAddr),
|
||||
lists:map(fun(X) ->
|
||||
timer:sleep(100),
|
||||
dgiot_device:save_log(ProductId, DevAddr, dgiot_utils:binary_to_hex(X), <<"device_operationlog">>),
|
||||
dgiot_tcp_server:send(TCPState, X)
|
||||
end, Payloads),
|
||||
{noreply, TCPState};
|
||||
@ -186,6 +194,7 @@ terminate(_Reason, #tcp{state = #state{id = _ChannelId, devaddr = DtuAddr, produ
|
||||
DeviceId = dgiot_parse_id:get_deviceid(ProductId, DtuAddr),
|
||||
Taskchannel = dgiot_product_channel:get_taskchannel(ProductId),
|
||||
dgiot_task:del_pnque(DeviceId),
|
||||
dgiot_device:save_log(ProductId, DtuAddr, DtuAddr, <<"offline">>),
|
||||
dgiot_client:stop(Taskchannel, DeviceId),
|
||||
ok;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user