feat:modbus

This commit is contained in:
dawnwinterLiu 2024-04-11 11:44:46 +08:00
parent 2725444298
commit 9f132d9e3a
2 changed files with 8 additions and 3 deletions

View File

@ -164,7 +164,8 @@ handle_message({rule, #{clientid := DevAddr, payload := Payload, topic := _Topic
handle_message(_Message, State) ->
{ok, State}.
stop(_ChannelType, _ChannelId, _State) ->
stop(_ChannelType, ChannelId, _State) ->
dgiot_data:delete({check_connection, ChannelId}),
ok.
get_app(Products) ->

View File

@ -52,6 +52,7 @@ handle_info(check_connection, #tcp{state = #state{id = ChannelId, hb = Hb}} = TC
OldTime when (Now - OldTime) > (Hb + 60) ->
%%
dgiot_bridge:control_channel(ChannelId, <<"disable">>, <<>>),
timer:sleep(500),
dgiot_bridge:control_channel(ChannelId, <<"enable">>, <<>>);
_ ->
pass
@ -66,7 +67,7 @@ handle_info({tcp, Buff}, #tcp{socket = Socket, state = #state{id = ChannelId, de
DtuAddr = dgiot_utils:binary_to_hex(Buff),
List = dgiot_utils:to_list(DtuAddr),
List1 = dgiot_utils:to_list(Buff),
erlang:send_after(120 * 1000, self(), check_connection),
erlang:send_after(600 * 1000, self(), check_connection),
case re:run(DtuAddr, Head, [{capture, first, list}]) of
{match, [Head]} when length(List) == Len ->
DeviceId = dgiot_parse_id:get_deviceid(ProductId, DtuAddr),
@ -101,6 +102,7 @@ handle_info({tcp, Buff}, #tcp{state = #state{id = ChannelId, devaddr = DtuAddr,
<<"slaveId">> => Sh * 256 + Sl,
<<"address">> => H * 256 + L}) of
{_, Things} ->
dgiot_data:insert({check_connection, ChannelId}, dgiot_datetime:now_secs()),
NewTopic = <<"$dg/thing/", DtuProductId/binary, "/", DtuAddr/binary, "/properties/report">>,
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),
@ -124,6 +126,7 @@ handle_info({tcp, Buff}, #tcp{state = #state{id = ChannelId, devaddr = DtuAddr,
<<"slaveId">> => SlaveId,
<<"address">> => Address}) of
{_, Things} ->
dgiot_data:insert({check_connection, ChannelId}, dgiot_datetime:now_secs()),
NewTopic = <<"$dg/thing/", DtuProductId/binary, "/", DtuAddr/binary, "/properties/report">>,
dgiot_bridge:send_log(ChannelId, DtuProductId, DtuAddr, "~s ~p to task ~p ~ts~n ", [?FILE, ?LINE, NewTopic, unicode:characters_to_list(dgiot_json:encode(Things))]),
DeviceId = dgiot_parse_id:get_deviceid(DtuProductId, DtuAddr),
@ -194,7 +197,8 @@ handle_call(_Msg, _From, TCPState) ->
handle_cast(_Msg, TCPState) ->
{noreply, TCPState}.
terminate(_Reason, #tcp{state = #state{devaddr = DtuAddr, product = ProductId}} = _TCPState) ->
terminate(_Reason, #tcp{state = #state{id = ChannelId, devaddr = DtuAddr, product = ProductId}} = _TCPState) ->
dgiot_data:delete({check_connection, ChannelId}),
DeviceId = dgiot_parse_id:get_deviceid(ProductId, DtuAddr),
Taskchannel = dgiot_product_channel:get_taskchannel(ProductId),
dgiot_task:del_pnque(DeviceId),