This commit is contained in:
AvantLiu 2021-07-26 12:21:26 +08:00
parent 006a046c82
commit a100a25b9d
5 changed files with 18 additions and 20 deletions

View File

@ -235,22 +235,22 @@ get_wechat_map(SessionToken) ->
get_device_info(Deviceid, SessionToken) ->
case dgiot_parse:get_object(<<"Device">>, Deviceid, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"product">> := #{<<"objectId">> := ProductId}, <<"basedata">> := Basedata} = Result} ->
NewParams =
{NewParams, ProductName} =
case dgiot_parse:get_object(<<"Product">>, ProductId) of
{ok, #{<<"config">> := #{<<"basedate">> := #{<<"params">> := Params}}}} ->
lists:foldl(fun(Param, Acc) ->
{ok, #{<<"config">> := #{<<"basedate">> := #{<<"params">> := Params}}, <<"name">> := Productname}} ->
lists:foldl(fun(Param, {Acc, _Acc1}) ->
Identifier = maps:get(<<"identifier">>, Param),
case maps:find(Identifier, Basedata) of
error ->
Acc;
{Acc, Productname};
{ok, Value} ->
Acc ++ [Param#{<<"value">> => Value}]
{Acc ++ [Param#{<<"value">> => Value}], Productname}
end
end, [], Params);
end, {[], <<>>}, Params);
_ ->
[]
{[], <<" ">>}
end,
{ok, Result#{<<"params">> => NewParams}};
{ok, Result#{<<"params">> => NewParams, <<"productname">> => ProductName}};
_ ->
{error, []}
end.

View File

@ -42,7 +42,7 @@
-define(FC_READ_IREGS, 16#04). %
-define(FC_WRITE_COIL, 16#05). %线
-define(FC_WRITE_HREG, 16#06). %
-define(FC_WRITE_HREG, 16#06). %
-define(FC_WRITE_COILS, 16#0f). %线
-define(FC_WRITE_HREGS, 16#10). %

View File

@ -66,14 +66,14 @@ handle_info({tcp, Buff}, #tcp{socket = Socket, state = #state{id = ChannelId, de
{DevId1, Devaddr1} ->
{DevId1, Devaddr1}
end,
Topic = <<"profile/", ProductId/binary, "/", DtuAddr/binary>>,
Topic = <<"profile/", ProductId/binary, "/", Devaddr/binary>>,
dgiot_mqtt:subscribe(Topic),
{noreply, TCPState#tcp{buff = <<>>, state = State#state{devaddr = Devaddr, deviceId = DevId}}};
_Error ->
case re:run(Buff, Head, [{capture, first, list}]) of
{match, [Head]} when length(List1) == Len ->
create_device(DeviceId, ProductId, Buff, DTUIP, Dtutype),
Topic = <<"profile/", ProductId/binary, "/", DtuAddr/binary>>,
Topic = <<"profile/", ProductId/binary, "/", Buff/binary>>,
dgiot_mqtt:subscribe(Topic),
{noreply, TCPState#tcp{buff = <<>>, state = State#state{devaddr = Buff}}};
Error1 ->
@ -103,19 +103,18 @@ handle_info({tcp, Buff}, #tcp{state = #state{id = ChannelId, devaddr = DtuAddr,
end,
{noreply, TCPState#tcp{buff = <<>>, state = State#state{env = <<>>}}};
handle_info({deliver, Topic, Msg}, #tcp{state = #state{id = ChannelId} = State} = TCPState) ->
?LOG(info, "Msg ~p", [Msg]),
handle_info({deliver, _, Msg}, #tcp{state = #state{id = ChannelId} = State} = TCPState) ->
Payload = dgiot_mqtt:get_payload(Msg),
Topic = dgiot_mqtt:get_topic(Msg),
case jsx:is_json(Payload) of
true ->
case binary:split(dgiot_mqtt:get_topic(Msg), <<$/>>, [global, trim]) of
case binary:split(Topic, <<$/>>, [global, trim]) of
[<<"profile">>, ProductId, _DtuAddr] ->
%%
case Payload of
#{<<"_dgiotprotocol">> := <<"modbus">>} ->
Payloads = modbus_rtu:set_params(maps:without([<<"_dgiotprotocol">>], Payload), ProductId),
lists:map(fun(X) ->
?LOG(info, "X ~p", [X]),
dgiot_tcp_server:send(TCPState, X)
end, Payloads);
_ ->
@ -175,12 +174,11 @@ handle_info({deliver, Topic, Msg}, #tcp{state = #state{id = ChannelId} = State}
false ->
case binary:split(Topic, <<$/>>, [global, trim]) of
[<<"profile">>, ProductId, _DtuAddr] ->
%%
%%
case Payload of
#{<<"_dgiotprotocol">> := <<"modbus">>} ->
Payloads = modbus_rtu:set_params(maps:without([<<"_dgiotprotocol">>], Payload), ProductId),
lists:map(fun(X) ->
?LOG(info, "X ~p", [X]),
dgiot_tcp_server:send(TCPState, X)
end, Payloads);
_ ->

View File

@ -137,8 +137,8 @@ set_params(Basedata, ProductId) ->
<<"readHregs">> -> ?FC_READ_HREGS;
<<"readIregs">> -> ?FC_READ_IREGS;
<<"writeCoil">> -> ?FC_WRITE_COIL;
<<"writeHreg">> -> ?FC_WRITE_COILS; %%线
<<"writeCoils">> -> ?FC_WRITE_HREG;
<<"writeHreg">> -> ?FC_WRITE_HREG;
<<"writeCoils">> -> ?FC_WRITE_COILS; %%线
<<"writeHregs">> -> ?FC_WRITE_HREGS; %%
_ -> ?FC_READ_HREGS
end,

View File

@ -6,7 +6,7 @@ parse.delete_field = ACL,objectId,updatedAt,createdAt
##--------------------------------------------------------------------
## parse config
##--------------------------------------------------------------------
parse.parse_server = http://prod.iotn2n.com:1337
parse.parse_server = http://47.105.106.54:1337
parse.parse_path = /parse/
parse.parse_appid = 1uqZbbdd_JMyQ45YLsUzYezMRPerMa80
parse.parse_master_key = PADbN7p973quWLngikp6XvrDbL97u_vM