Skip to content

Commit

Permalink
feta: modbusc heart
Browse files Browse the repository at this point in the history
  • Loading branch information
dawnwinterLiu committed May 27, 2024
1 parent 59892a0 commit 054aa9e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
20 changes: 17 additions & 3 deletions apps/dgiot_modbus/src/dgiot_modbusc_channel.erl
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ init(?TYPE, ChannelId, #{
maxaddr => MaxAddr}},
%% dgiot_client:add_clock(ChannelId, Start_time, End_time),
dgiot_client:add_clock(ChannelId, dgiot_datetime:now_secs() - 5000, dgiot_datetime:now_secs() + 300000),
{ok, #state{id = ChannelId, env = #{size => Size}}, dgiot_client:register(ChannelId, tcp_client_sup, NewArgs)}.
{ok, #state{id = ChannelId, env = #{size => Size, filename => FileName, freq => Freq}}, dgiot_client:register(ChannelId, tcp_client_sup, NewArgs)}.

handle_init(State) ->
{ok, State}.
Expand All @@ -239,10 +239,24 @@ handle_event(_EventId, Event, State) ->
io:format("~s ~p Event = ~p.~n", [?FILE, ?LINE, Event]),
{ok, State}.

handle_message(start_client, #state{id = ChannelId, env = #{size := Size}} = State) ->
handle_message(check_connection, #state{id = ChannelId, env = #{filename := FileName, freq := Freq}} = Dclient) ->
Now = dgiot_datetime:now_secs(),
case dgiot_data:get({check_connection, ChannelId, FileName}) of
OldTime when (Now - OldTime) > Freq ->
dgiot_client:stop(ChannelId, FileName),
dgiot_client:start(ChannelId, FileName);
_ ->
pass
end,
erlang:send_after(Freq * 1200, self(), check_connection),
{noreply, Dclient};

handle_message(start_client, #state{id = ChannelId, env = #{size := _Size, filename := FileName}} = State) ->
case dgiot_data:get({start_client, ChannelId}) of
not_find ->
[dgiot_client:start(ChannelId, dgiot_utils:to_binary(I)) || I <- lists:seq(1, Size)],
%% [dgiot_client:start(ChannelId, dgiot_utils:to_binary(I)) || I <- lists:seq(1, Size)],
dgiot_client:start(ChannelId, FileName),
erlang:send_after(30 * 1000, self(), check_connection),
dgiot_data:insert({start_client, ChannelId}, ChannelId);
_ ->
pass
Expand Down
1 change: 1 addition & 0 deletions apps/dgiot_modbus/src/dgiot_modbusc_tcp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ handle_info({tcp, Buff}, #dclient{channel = ChannelId,
EndData = <<OldData/binary, Data/binary>>,
%% io:format("~s ~p EndData = ~p.~n", [?FILE, ?LINE, dgiot_utils:binary_to_hex(EndData)]),
AllData = modbus_tcp:parse_frame(StartAddr, FileName, EndData, MinAddr),
dgiot_data:insert({check_connection, ChannelId, FileName}, dgiot_datetime:now_secs()),
dgiot_bridge:send_log(dgiot_utils:to_binary(ChannelId), "~p recv data ~ts => ~p", [dgiot_datetime:format("YYYY-MM-DD HH:NN:SS"), unicode:characters_to_list(dgiot_json:encode(AllData)), dgiot_utils:binary_to_hex(EndData)]),
erlang:send_after(Freq * 1000, self(), read),
{noreply, Dclient#dclient{child = ChildState#{di => StartAddr, data => <<>>}}};
Expand Down
9 changes: 9 additions & 0 deletions apps/dgiot_modbus/src/modbus/modbus_rtu.erl
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ is16(<<"0X", Data/binary>>) when size(Data) > 4 ->
is16(<<"0X", Data/binary>>) ->
<<"00", Data/binary>>;

is16(<<"0x", Data/binary>>) when size(Data) == 4 ->
Data;

is16(<<"0x", Data/binary>>) when size(Data) > 4 ->
Data;

is16(<<"0x", Data/binary>>) ->
<<"00", Data/binary>>;

is16(<<"00", Data/binary>>) ->
is16(Data);

Expand Down
7 changes: 0 additions & 7 deletions apps/dgiot_modbus/src/modbus/modbus_tcp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,6 @@ parse_frame(StartAddr, FileName, Data, MinAddr) ->
NewAllData =
maps:fold(fun
(_, {ProductId1, Devaddr1, Ack}, Ncc) ->
%% DeviceId = dgiot_parse_id:get_deviceid(ProductId1, Devaddr1),
%% NewAck = change_data(ProductId1, Ack),
CacheAck = dgiot_task:merge_cache_data(ProductId1, Ack, -1),
dgiot_task:save_cache_data(ProductId1, CacheAck),
Now = dgiot_datetime:now_secs(),
Expand All @@ -367,14 +365,9 @@ parse_frame(StartAddr, FileName, Data, MinAddr) ->
<<Acc/binary, Len/binary, BinK/binary, BinV/binary, ",">>
end, <<>>, CacheAck),
Shard_data = modbus_tcp:shard_data(BinData, Calculated),

dgiot_device:save(ProductId1, Devaddr1),
Sql = dgiot_tdengine:format_sql(ProductId1, Devaddr1, [Shard_data#{<<"createdat">> => Now * 1000}]),
dgiot_tdengine_adapter:save_sql(ProductId1, Sql),
%% ChannelId = dgiot_parse_id:get_channelid(<<"2">>, <<"DGIOTTOPO">>, <<"TOPO组态通道"/utf8>>),
%% dgiot_channelx:do_message(ChannelId, {topo_thing, ProductId1, DeviceId, Shard_data}),
%% RealData = dgiot_device_card:get_card(ProductId1, [Shard_data#{<<"createdat">> => Now * 1000}], DeviceId, #{}, dgiot_data:get({shard_storage, ProductId1})),
%% dgiot_data:insert({last_data, DeviceId}, RealData),
Ncc#{Devaddr1 => Calculated};
_ ->
Ncc
Expand Down

0 comments on commit 054aa9e

Please sign in to comment.