feat: topo Setting parameters

This commit is contained in:
AvantLiu 2022-02-18 11:47:52 +08:00
parent 0a69b735fe
commit 95368fafd5
5 changed files with 52 additions and 11 deletions

View File

@ -235,6 +235,8 @@ handle_info(Info, State) ->
{stop, Reason, State};
{Err, Reason} when Err == 'EXIT'; Err == error ->
?LOG(error, "do_message, ~p,~p", [Info, Reason]),
{noreply, State};
_->
{noreply, State}
end.

View File

@ -450,19 +450,23 @@ modbus_tcp_decoder(ProductId, TransactionId, Address, Data, Acc1) ->
case X of
#{<<"identifier">> := Identifier,
<<"dataForm">> := #{
<<"strategy">> := Strategy,
<<"slaveid">> := OldSlaveid,
<<"address">> := OldAddress,
<<"protocol">> := <<"modbus">>
}} ->
}} when Strategy =/= <<"计算值"/utf8>> ->
<<H:8, L:8>> = dgiot_utils:hex_to_binary(modbus_rtu:is16(OldSlaveid)),
<<Sh:8, Sl:8>> = dgiot_utils:hex_to_binary(modbus_rtu:is16(OldAddress)),
NewSlaveid = H * 256 + L,
NewAddress = Sh * 256 + Sl,
case {TransactionId, Address} of
{NewSlaveid, NewAddress} ->
%% case format_value(Data, X, Props) of
case format_value(Data, X) of
{Value, _Rest} ->
Acc#{Identifier => Value};
{map, Value, _Rest} ->
maps:merge(Acc, Value);
_ -> Acc
end;
_ ->
@ -552,8 +556,8 @@ format_value(Buff, #{<<"dataForm">> := #{
<<"originaltype">> := <<"bit">>
}}) ->
IntLen = dgiot_utils:to_int(Len),
Size = max(2, IntLen) * 2,
<<Value:Size/binary, Rest/binary>> = dgiot_utils:binary_to_hex(Buff),
Size = max(2, IntLen) * 8,
<<Value:Size, Rest/binary>> = Buff,
{Value, Rest};
format_value(Buff, #{<<"dataForm">> := #{

View File

@ -105,7 +105,8 @@
-export([
test_graphql/0,
subscribe/2,
send_msg/3
send_msg/3,
send_msg/4
]).
@ -121,8 +122,8 @@ subscribe(Table, Method) ->
[_, Data, _Body] ->
dgiot_parse:send_msg(Table, Method, Data),
{ok, Data};
[_, _ObjectId, Data, _Body] ->
dgiot_parse:send_msg(Table, Method, Data),
[_, ObjectId, Data, _Body] ->
dgiot_parse:send_msg(Table, Method, Data, ObjectId),
{ok, Data};
_ ->
{ok, []}
@ -142,6 +143,18 @@ send_msg(Table, Method, Args) ->
end, [], dgiot_data:get({sub, Table, Method})),
dgiot_data:insert({sub, Table, Method}, Pids).
send_msg(Table, Method, Args, ObjectId) ->
Pids = lists:foldl(fun(Pid, Acc) ->
case is_process_alive(Pid) of
true ->
Pid ! {sync_parse, Args, ObjectId},
Acc ++ [Pid];
false ->
Acc
end
end, [], dgiot_data:get({sub, Table, Method})),
dgiot_data:insert({sub, Table, Method}, Pids).
get_shapeid(DeviceId, Identifier) ->
<<ShapeId:10/binary, _/binary>> = dgiot_utils:to_md5(<<DeviceId/binary, Identifier/binary, "dgiottopo">>),
ShapeId.

View File

@ -109,8 +109,31 @@ handle_message({check_profie, Args}, State) ->
erlang:send_after(1000 * 30, self(), {message, <<"_Pool">>, check_profile}),
{ok, State};
handle_message({sync_parse, Args, ObjectId}, State) ->
case jsx:decode(Args, [{labels, binary}, return_maps]) of
#{<<"profile">> := _Profile, <<"devaddr">> := _Devaddr, <<"product">> := #{<<"objectId">> := _ProductId}} ->
handle_message({sync_parse, Args}, State);
#{<<"profile">> := Profile} ->
io:format("~s ~p ObjectId = ~p.~n", [?FILE, ?LINE, ObjectId]),
case dgiot_device:lookup(dgiot_utils:to_binary(ObjectId)) of
{ok, #{<<"devaddr">> := Devaddr, <<"productid">> := ProductId}} ->
NewArgs = jsx:encode(#{<<"profile">> => Profile, <<"devaddr">> => Devaddr, <<"product">> => #{<<"objectId">> => ProductId}}),
handle_message({sync_parse, NewArgs}, State);
_ ->
case dgiot_parse:get_object(<<"Device">>, ObjectId) of
{ok, Device} ->
NewArgs = jsx:encode(Device#{<<"profile">> => Profile}),
handle_message({sync_parse, NewArgs}, State);
_ ->
pass
end
end;
_ ->
pass
end;
handle_message({sync_parse, Args}, State) ->
%% ?LOG(info, "Args ~p", [jsx:decode(Args, [{labels, binary}, return_maps])]),
io:format("~s ~p Args = ~p.~n", [?FILE, ?LINE, jsx:decode(Args, [{labels, binary}, return_maps])]),
case jsx:decode(Args, [{labels, binary}, return_maps]) of
#{<<"profile">> := Profile, <<"devaddr">> := Devaddr, <<"product">> := #{<<"objectId">> := ProductId}} = Arg ->
Sessiontoken = maps:get(<<"sessiontoken">>, Arg, <<"">>),
@ -150,7 +173,6 @@ handle_message({sync_parse, Args}, State) ->
pass
end,
{ok, State};
handle_message(_Message, State) ->
%% ?LOG(info, "_Message ~p", [_Message]),
{ok, State}.
@ -164,7 +186,7 @@ stop(ChannelType, ChannelId, #state{env = #{<<"product">> := ProductId, <<"args"
get_modifyprofile(DeviceId, Profile) ->
case dgiot_data:get(dgiot_profile, <<"b3de3bd7a9">>) of
case dgiot_data:get(?PROFILE, DeviceId) of
not_find ->
dgiot_data:insert(?PROFILE, DeviceId, Profile),
Profile;

View File

@ -257,10 +257,10 @@ handle_message(_Message, State) ->
?LOG(info, "_Message ~p", [_Message]),
{ok, State}.
stop(ChannelType, ChannelId, #state{env = #{<<"product">> := ProductId, <<"args">> := Args}} = _State) ->
stop(_ChannelType, ChannelId, #state{env = #{<<"product">> := ProductId, <<"args">> := Args}} = _State) ->
spawn(fun() ->
dgiot_task:stop(Args#{<<"product">> => ProductId, <<"channel">> => ChannelId})
end),
?LOG(warning, "channel stop ~p,~p", [ChannelType, ChannelId]),
%% ?LOG(warning, "channel stop ~p,~p", [ChannelType, ChannelId]),
ok.