feat: api(update_properties)

This commit is contained in:
h7ml 2022-05-17 20:34:58 +08:00
parent 349f784df5
commit 71e6377b9c

View File

@ -23,7 +23,7 @@
-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, get_keys/1, get_control/1, save_control/1]).
-export([parse_frame/3, to_frame/2]). -export([parse_frame/3, to_frame/2]).
-export([create_product/1, create_product/2, add_product_relation/2, delete_product_relation/1]). -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]). -export([get_prop/1, get_props/1, get_Props/2, get_unit/1, do_td_message/1, update_properties/2, update_properties/0]).
init_ets() -> init_ets() ->
dgiot_data:init(?DGIOT_PRODUCT), dgiot_data:init(?DGIOT_PRODUCT),
@ -128,6 +128,7 @@ get_control(ProductId) ->
end. end.
update_properties(ProductId, Product) -> update_properties(ProductId, Product) ->
io:format("~s ~p ProductId = ~p.~n", [?FILE, ?LINE, ProductId]),
PropertiesTpl = dgiot_dlink:get_json(<<"properties_tpl">>), PropertiesTpl = dgiot_dlink:get_json(<<"properties_tpl">>),
case dgiot_product:lookup_prod(ProductId) of case dgiot_product:lookup_prod(ProductId) of
{ok, #{<<"thing">> := #{<<"properties">> := Props} = Thing}} -> {ok, #{<<"thing">> := #{<<"properties">> := Props} = Thing}} ->
@ -140,12 +141,28 @@ update_properties(ProductId, Product) ->
NewThing = Thing#{ NewThing = Thing#{
<<"properties">> => NewProperties <<"properties">> => NewProperties
}, },
dgiot_parse:update_object(<<"Product">>, ProductId, #{<<"thing">> => NewThing}), Message = dgiot_parse:update_object(<<"Product">>, ProductId, #{<<"thing">> => NewThing}),
io:format("~s ~p Message = ~p.~n", [?FILE, ?LINE, Message]),
dgiot_data:insert(?DGIOT_PRODUCT, ProductId, Product#{<<"thing">> => NewThing}); dgiot_data:insert(?DGIOT_PRODUCT, ProductId, Product#{<<"thing">> => NewThing});
_Error -> _Error ->
[] []
end. end.
update_properties() ->
case dgiot_parse:query_object(<<"Product">>, #{<<"skip">> => 0}) of
{ok, #{<<"results">> := Results}} ->
lists:foldl(fun(X, _Acc) ->
case X of
#{<<"objectId">> := ProductId} ->
update_properties(ProductId, X);
_ ->
pass
end
end, #{}, Results);
_ ->
pass
end.
save_keys(ProductId) -> save_keys(ProductId) ->
Keys = Keys =
case dgiot_product:lookup_prod(ProductId) of case dgiot_product:lookup_prod(ProductId) of