fix: add get_channel_product

This commit is contained in:
jhonliu 2022-05-24 11:53:45 +08:00
parent 4e2196e8e7
commit fd8088afc1
3 changed files with 53 additions and 42 deletions

View File

@ -18,7 +18,7 @@
-include_lib("dgiot/include/logger.hrl").
-include_lib("dgiot/include/dgiot_socket.hrl").
-include_lib("dgiot/include/dgiot_client.hrl").
-record(child_state, {buff = <<>>, product, devaddr, hb = 30, device}).
-record(child_state, {buff = <<>>, product, hb = 30, device}).
%% tcp client callback
-define(MAX_BUFF_SIZE, 10 * 1024).
@ -30,36 +30,36 @@ init(#dclient{channel = ChannelId}) ->
not_find ->
{stop, <<"not find product">>};
ProductId ->
io:format("~s ~p ~p ~p ~n",[?FILE, ?LINE, ChannelId, ProductId]),
io:format("~s ~p ~p ~p ~n", [?FILE, ?LINE, ChannelId, ProductId]),
{ok, #child_state{product = ProductId}}
end.
handle_info(connection_ready, #dclient{child = #child_state{product = ProductId}} = Dclient) ->
handle_info(connection_ready, #dclient{child = #child_state{product = ProductId} = ChildState} = Dclient) ->
rand:seed(exs1024),
Time = erlang:round(rand:uniform() * 1 + 1) * 1000,
io:format("~s ~p ~p ~n",[?FILE, ?LINE, ProductId]),
io:format("~s ~p ~p ~n", [?FILE, ?LINE, ProductId]),
case do_cmd(ProductId, connection_ready, <<>>, Dclient) of
default ->
erlang:send_after(Time, self(), login);
_ ->
pass
end,
{noreply, Dclient};
erlang:send_after(Time, self(), login),
{noreply, ChildState};
Device ->
{noreply, ChildState#child_state{device = Device}}
end;
handle_info(#{<<"cmd">> := Cmd, <<"data">> := Data, <<"productId">> := ProductId}, Dclient) ->
handle_info(#{<<"cmd">> := Cmd, <<"data">> := Data, <<"productId">> := ProductId}, #dclient{child = ChildState} = Dclient) ->
case do_cmd(ProductId, Cmd, Data, Dclient) of
default ->
{noreply, Dclient};
Result ->
Result
{noreply, ChildState};
{Result, Device} ->
{Result, ChildState#child_state{device = Device}}
end;
handle_info(tcp_closed, #dclient{child = #child_state{product = ProductId} = ChildState} = Dclient) ->
case do_cmd(ProductId, tcp_closed, <<>>, Dclient) of
default ->
{noreply, ChildState};
Result ->
Result
{Result, Device} ->
{Result, ChildState#child_state{device = Device}}
end;
handle_info({tcp, Buff}, #dclient{child = #child_state{buff = Old, product = ProductId} = ChildState} = Dclient) ->
@ -67,10 +67,10 @@ handle_info({tcp, Buff}, #dclient{child = #child_state{buff = Old, product = Pro
case do_cmd(ProductId, tcp, Data, Dclient) of
default ->
{noreply, ChildState};
{noreply, Bin, NewChildState} ->
{noreply, NewChildState#child_state{buff = Bin}};
{stop, Reason, NewChildState} ->
{stop, Reason, NewChildState};
{noreply, Bin, Device} ->
{noreply, ChildState#child_state{buff = Bin, device = Device}};
{stop, Reason, Device} ->
{stop, Reason, ChildState#child_state{device = Device}};
Result ->
Result
end;
@ -78,7 +78,7 @@ handle_info({tcp, Buff}, #dclient{child = #child_state{buff = Old, product = Pro
handle_info({deliver, _Topic, _Msg}, #dclient{child = ChildState}) ->
{noreply, ChildState};
handle_info(login, #dclient{child = #child_state{hb = Hb} = ChildState} ) ->
handle_info(login, #dclient{child = #child_state{hb = Hb} = ChildState}) ->
erlang:send_after(Hb * 1000, self(), heartbeat),
dgiot_tcp_client:send(<<"login">>),
{noreply, ChildState};
@ -95,18 +95,18 @@ terminate(_Reason, _Dclient) ->
ok.
do_cmd(ProductId, Cmd, Data, Dclient) ->
io:format("~s ~p ~p ~n",[?FILE, ?LINE, ProductId]),
io:format("~s ~p ~p ~n", [?FILE, ?LINE, ProductId]),
case dgiot_hook:run_hook({tcp, ProductId}, [Cmd, Data, Dclient]) of
{ok, ChildState} ->
{noreply, ChildState};
{reply, ProductId, Payload, ChildState} ->
{ok, Device} ->
{noreply, Device};
{reply, ProductId, Payload, Device} ->
case dgiot_tcp_client:send(Payload) of
ok ->
ok;
{error, _Reason} ->
pass
end,
{noreply, ChildState};
{noreply, Device};
_ ->
default
end.

View File

@ -20,12 +20,13 @@
-include_lib("dgiot/include/logger.hrl").
-include_lib("dgiot_bridge/include/dgiot_bridge.hrl").
-dgiot_data("ets").
-export([init_ets/0, load_cache/0, local/1, save/1, put/1, get/1, delete/1, save_prod/2, lookup_prod/1, get_keys/1, get_control/1, save_control/1]).
-export([init_ets/0, load_cache/0, local/1, save/1, put/1, get/1, delete/1, save_prod/2, lookup_prod/1]).
-export([parse_frame/3, to_frame/2]).
-export([create_product/1, create_product/2, add_product_relation/2, delete_product_relation/1]).
-export([get_prop/1, get_props/1, get_Props/2, get_unit/1, do_td_message/1, update_properties/2, update_properties/0]).
-export([get_prop/1, get_props/1, get_props/2, get_unit/1, do_td_message/1, update_properties/2, update_properties/0]).
-export([update_topics/0, update_product_filed/1]).
-export([save_keys/1, get_keys/1, get_control/1, save_control/1, save_channel/1, get_channel/1]).
-type(result() :: any()). %% todo
init_ets() ->
dgiot_data:init(?DGIOT_PRODUCT),
@ -70,15 +71,10 @@ lookup_prod(ProductId) ->
save(Product) ->
Product1 = format_product(Product),
#{<<"productId">> := ProductId} = Product1,
%% case dgiot_data:get({ProductId, update_properties}) of
%% not_find ->
%% update_properties(ProductId, Product1),
%% dgiot_data:insert({ProductId, update_properties}, ProductId);
%% _ -> pass
%% end,
dgiot_data:insert(?DGIOT_PRODUCT, ProductId, Product1),
save_keys(ProductId),
save_control(ProductId),
save_channel(ProductId),
{ok, Product1}.
put(Product) ->
@ -103,6 +99,23 @@ get(ProductId) ->
{error, Reason}
end.
-spec save_channel(binary()) -> result().
save_channel(ProductId) ->
case lookup_prod(ProductId) of
{ok, #{<<"channel">> := #{<<"otherchannel">> := [Channel|_]}}} ->
dgiot_data:insert({channel_product, binary_to_atom(Channel)}, ProductId);
{ok, #{<<"channel">> := #{<<"otherchannel">> := Channel}}} ->
dgiot_data:insert({channel_product, binary_to_atom(Channel)}, ProductId);
_ ->
pass
end.
-spec get_channel(binary() | atom()) -> result().
get_channel(ChannelId) when is_binary(ChannelId) ->
get_channel(binary_to_atom(ChannelId));
get_channel(ChannelId) ->
dgiot_data:get({channel_product, ChannelId}).
%%
save_control(ProductId) ->
Keys =
@ -198,8 +211,6 @@ update_product_filed(_Filed) ->
pass
end.
save_keys(ProductId) ->
Keys =
case dgiot_product:lookup_prod(ProductId) of
@ -238,7 +249,7 @@ to_frame(ProductId, Msg) ->
format_product(#{<<"objectId">> := ProductId} = Product) ->
Thing = maps:get(<<"thing">>, Product, #{}),
Props = maps:get(<<"properties">>, Thing, []),
Keys = [<<"ACL">>, <<"name">>, <<"devType">>, <<"status">>, <<"content">>, <<"profile">>, <<"nodeType">>, <<"dynamicReg">>, <<"topics">>, <<"productSecret">>],
Keys = [<<"ACL">>, <<"name">>, <<"devType">>, <<"status">>, <<"channel">>, <<"content">>, <<"profile">>, <<"nodeType">>, <<"dynamicReg">>, <<"topics">>, <<"productSecret">>],
Map = maps:with(Keys, Product),
Map#{
<<"productId">> => ProductId,
@ -378,7 +389,7 @@ get_props(ProductId) ->
#{}
end.
get_Props(ProductId, <<"*">>) ->
get_props(ProductId, <<"*">>) ->
case dgiot_product:lookup_prod(ProductId) of
{ok, #{<<"thing">> := #{<<"properties">> := Props}}} ->
Props;
@ -386,10 +397,10 @@ get_Props(ProductId, <<"*">>) ->
[]
end;
get_Props(ProductId, Keys) when Keys == undefined; Keys == <<>> ->
get_Props(ProductId, <<"*">>);
get_props(ProductId, Keys) when Keys == undefined; Keys == <<>> ->
get_props(ProductId, <<"*">>);
get_Props(ProductId, Keys) ->
get_props(ProductId, Keys) ->
List =
case is_list(Keys) of
true -> Keys;

View File

@ -41,7 +41,7 @@ get_device_card(Channel, ProductId, DeviceId, Args) ->
get_card(ProductId, Results, DeviceId, Args) ->
[Result | _] = Results,
Keys = maps:get(<<"keys">>, Args, <<"*">>),
Props = dgiot_product:get_Props(ProductId, Keys),
Props = dgiot_product:get_props(ProductId, Keys),
lists:foldl(fun(X, Acc) ->
case X of
#{<<"name">> := Name, <<"identifier">> := Identifier, <<"dataForm">> := #{<<"protocol">> := Protocol}, <<"dataSource">> := DataSource, <<"dataType">> := #{<<"type">> := Typea} = DataType} ->