diff --git a/apps/dgiot_bamis/src/dgiot_bamis.erl b/apps/dgiot_bamis/src/dgiot_bamis.erl index b25dbd5c..e56a5234 100644 --- a/apps/dgiot_bamis/src/dgiot_bamis.erl +++ b/apps/dgiot_bamis/src/dgiot_bamis.erl @@ -153,11 +153,11 @@ delete({'after', Data}) -> %%excludeKeys Exclude specific fields from the returned query %%include Use on Pointer columns to return the full object %%*/ -format(#{<<"orderBy">> := OrderBy} = Data) -> +format(#{<<"orderBy">> := OrderBy} = Data) when byte_size(OrderBy) >0-> NewData = maps:without([<<"orderBy">>], Data), format(NewData#{<<"order">> => [OrderBy]}); -format(#{<<"order">> := [Order | _], <<"orderDir">> := OrderDir} = Data) -> +format(#{<<"order">> := [Order | _], <<"orderDir">> := OrderDir} = Data) when byte_size(Order) >0 -> NewData = maps:without([<<"orderDir">>], Data), NewOrder = case OrderDir of diff --git a/apps/dgiot_factory/src/dgiot_factory_channel.erl b/apps/dgiot_factory/src/dgiot_factory_channel.erl index 9a1b29f2..324de087 100644 --- a/apps/dgiot_factory/src/dgiot_factory_channel.erl +++ b/apps/dgiot_factory/src/dgiot_factory_channel.erl @@ -31,7 +31,7 @@ %% Channel callback -export([init/3, handle_init/1, handle_event/3, handle_message/2, stop/3]). --export([get_id/2, after_handle/4, handle_data/8, get_card_data/2]). +-export([get_id/2, after_handle/4, handle_data/8]). -export([get_sub_product/1, get_new_acl/2, init_worker_device/3]). -export([get_roll_dev_id/2]). %% 注册通道类型 @@ -186,22 +186,15 @@ handle_data(TaskProductId, TaskDeviceId, BatchProductId, BatchDeviceId, BatchAdd io:format("~s ~p BatchDeviceId = ~p ~n", [?FILE, ?LINE, BatchDeviceId]), NewPayLoad = run_factory_hook(TaskProductId, TaskDeviceId, BatchProductId, BatchDeviceId, PersonType, NewData, ChannelId), dgiot_data:insert(?FACTORY_ORDER, {BatchProductId, BatchDeviceId, PersonType}, NewPayLoad), - OldData = get_card_data(BatchProductId, BatchDeviceId), + record_device_log(BatchProductId, BatchDeviceId, NewPayLoad), + OldData =dgiot_factory_utils: get_card_data(BatchProductId, BatchDeviceId), +%% io:format("~s ~p keys = ~p.~n", [?FILE, ?LINE, maps:keys(OldData)]), ALlData = dgiot_map:merge(OldData, NewPayLoad), dgiot_factory_statis:do_statis(TaskProductId, TaskDeviceId, PersonType, NewPayLoad), save2parse(BatchProductId, BatchDeviceId, ALlData), dgiot_factory_utils:save2td(BatchProductId, BatchAddr, ALlData). -get_card_data(BatchProductId, BatchDeviceId) -> - DevcieTypeList = dgiot_product:get_devicetype(BatchProductId) -- [<<"quality">>], - lists:foldl( - fun(DeviceType, Acc) -> - case dgiot_data:get(?FACTORY_ORDER, {BatchProductId, BatchDeviceId, DeviceType}) of - not_find -> - Acc; - Res -> - dgiot_map:merge(Acc, maps:without([<<"quality">>], Res)) - end - end, #{}, DevcieTypeList). + + process_data(FlatMap, PersonType, Token, TaskDeviceId) -> case dgiot_parse:get_object(<<"Device">>, TaskDeviceId) of @@ -395,3 +388,40 @@ init_worker_device(ProductId, WorkerNum, WorkerName) -> end. + +record_device_log(TaskProductId, TaskDeviceId, AllContent) -> + ACL = case dgiot_device_cache:lookup(TaskDeviceId) of + {ok, #{<<"acl">> := AclList}} -> + lists:foldl( + fun(Role, Acc) -> + Acc#{Role => #{ + <<"read">> => true, + <<"write">> => false}} + end, #{}, AclList); + _ -> + #{<<"*">> => #{<<"read">> => true}} + end, + Product = #{ + <<"__type">> => <<"Pointer">>, + <<"className">> => <<"Product">>, + <<"objectId">> => TaskProductId}, + Device = #{ + <<"__type">> => <<"Pointer">>, + <<"className">> => <<"Device">>, + <<"objectId">> => TaskDeviceId}, + + Devaddr = dgiot_utils:to_binary(dgiot_datetime:nowstamp()), + LogId = dgiot_parse_id:get_devicelogid(TaskDeviceId, Devaddr), + DeviceLog = #{ + <<"objectId">> => LogId, + <<"devaddr">> => Devaddr, + <<"ACL">> => ACL, + <<"createtime">> => dgiot_utils:to_binary(dgiot_datetime:nowstamp()), + <<"data">> => AllContent, + <<"product">> => Product, + <<"device">> => Device + }, + _R = dgiot_parse:create_object(<<"Devicelog">>, DeviceLog), + io:format("~s ~p _R ~p ~n ", [?FILE, ?LINE, _R]). + + diff --git a/apps/dgiot_factory/src/dgiot_factory_data.erl b/apps/dgiot_factory/src/dgiot_factory_data.erl index 0e337310..8bbc920a 100644 --- a/apps/dgiot_factory/src/dgiot_factory_data.erl +++ b/apps/dgiot_factory/src/dgiot_factory_data.erl @@ -17,261 +17,6 @@ -export([get_history_data/12, filter_data/1, filter_data/3]). -export([select/4, get_thing_list/2, limit_skip/2]). -export([kill_null/1]). -%% API -%%-export([ -%% get_card_data/2, -%% get_card_data/3, -%% get_td_sheet/10 -%% get_cache_data/3 -%%]). -%%-export([get_ThingMap/2, thinglist2binary/1, get_history/9, get_device_list/1, get_example/2, filter_data/3, filter_data/1]). -%%-export([merge_data/1]). - -%%get_card_data(BatchProductId, BatchDeviceId) -> -%% DevcieTypeList = dgiot_product:get_devicetype(BatchProductId), -%% lists:foldl( -%% fun(DeviceType, Acc) -> -%% case dgiot_data:get(?FACTORY_ORDER, {BatchProductId, BatchDeviceId, DeviceType}) of -%% not_find -> -%% Acc; -%% Res -> -%% maps:merge(Acc, Res) -%% end -%% end, #{}, DevcieTypeList). - -%%get_card_data(ProductId, RollId, DeviceType) -> -%% case dgiot_data:get(?FACTORY_ORDER, {ProductId, RollId, DeviceType}) of -%% not_find -> -%% get_td_last(ProductId, RollId, DeviceType); -%% Data -> -%% Data -%% end. - - -%%get_td_last(ProductId, Type, DeviceId) -> -%% case dgiot_product:get_device_thing(ProductId, Type) of -%% not_find -> -%% error; -%% ThingMap -> -%% case get_history(Channel, ProductId, DeviceId, ThingMap, Type) of -%% {ok, #{<<"results">> := HistoryData}} -> -%% NamedData = dgiot_product_enum:turn_name(HistoryData, ThingMap), -%%%% {Total, Res} = filter_data(Limit, Skip, NamedData), -%% {ok, {Total, filter_data(Res)}}; -%% -%% _ -> -%% error -%% end -%% end. - -%% -%% -%%get_td_sheet(ProductId, Type, Start, End, Channel, DeviceId, Where, Limit, Skip, New) -> -%% case dgiot_product:get_device_thing(ProductId, Type) of -%% not_find -> -%% error; -%% ThingMap -> -%% case get_history(Channel, ProductId, DeviceId, ThingMap, Where, Start, End, Type, New) of -%% {ok, #{<<"results">> := HistoryData}} -> -%% NamedData = dgiot_product_enum:turn_name(HistoryData, ThingMap), -%% {Total, Res} = filter_data(Limit, Skip, NamedData), -%% {ok, {Total, filter_data(Res)}}; -%% -%% _ -> -%% error -%% end -%% end. - -%%get_td_sheet(ProductId, Type, Start, End, Channel, DeviceId, Where, Limit, Skip, New) -> -%% case dgiot_product:get_device_thing(ProductId, Type) of -%% not_find -> -%% error; -%% ThingMap -> -%% case get_history(Channel, ProductId, DeviceId, ThingMap, Where, Start, End, Type, New) of -%% {ok, #{<<"results">> := HistoryData}} -> -%% NamedData = dgiot_product_enum:turn_name(HistoryData, ThingMap), -%% {Total, Res} = filter_data(Limit, Skip, NamedData), -%% {ok, {Total, filter_data(Res)}}; -%% -%% _ -> -%% error -%% end -%% end. -%% -%%merge_data(Data) -> -%% ReversList = lists:reverse(Data), -%% {Order, MapData} = lists:foldl( -%% fun(X, {List, Map}) -> -%% case maps:find(<<"person_sheetsid">>, X) of -%% {ok, Id} -> -%% case lists:member(Id, List) of -%% true -> -%%%% io:format("~s ~p List=~p ~n", [?FILE, ?LINE,List]), -%%%% io:format("~s ~p Map=~p ~n", [?FILE, ?LINE,Map]), -%% Old = maps:get(Id, Map, #{}), -%% New = maps:merge(Old, filter_data(X)), -%% {List, Map#{Id => New}}; -%% _ -> -%%%% io:format("~s ~p List=~p ~n", [?FILE, ?LINE,List]), -%%%% io:format("~s ~p Map=~p ~n", [?FILE, ?LINE,Map]), -%% {List ++ [Id], Map#{Id => filter_data(X)}} -%% end; -%% _ -> -%% {List, Map} -%% end -%% end, {[], #{}}, ReversList), -%% lists:foldl( -%% fun(X, Acc) -> -%% case maps:find(X, MapData) of -%% {ok, Res} -> -%% Acc ++ [Res]; -%% _ -> -%% Acc -%% end -%% end, [], lists:reverse(Order)). -%% -%%get_ThingMap(<<"all">>, ProductId) -> -%% case dgiot_product:get(<<"Product">>, ProductId) of -%% {ok, #{<<"thing">> := #{<<"properties">> := Properties}}} -> -%% ThingList = lists:foldl( -%% fun(X, Acc) -> -%% case X of -%% #{<<"isstorage">> := true, <<"identifier">> := Identifier, <<"dataType">> := #{<<"type">> := DateType}} -> -%% Acc#{Identifier => DateType}; -%% _ -> -%% Acc -%% end -%% end, #{}, Properties), -%% -%% case maps:size(ThingList) of -%% 0 -> -%% {error, not_find_thing}; -%% _ -> -%% {ok, ThingList} -%% end; -%% _ -> -%% {error, not_find_thing} -%% end; -%%get_ThingMap(Type, ProductId) -> -%% case dgiot_parse:get_object(<<"Product">>, ProductId) of -%% {ok, #{<<"thing">> := #{<<"properties">> := Properties}}} -> -%% ThingList = lists:foldl( -%% fun(X, Acc) -> -%% case X of -%% #{<<"devicetype">> := ?PERSON, <<"isstorage">> := true, <<"identifier">> := Identifier, <<"dataType">> := #{<<"type">> := DateType}} -> -%% Acc#{Identifier => DateType}; -%% #{<<"devicetype">> := Type, <<"isstorage">> := true, <<"identifier">> := Identifier, <<"dataType">> := #{<<"type">> := DateType}} -> -%% Acc#{Identifier => DateType}; -%% _ -> -%% Acc -%% end -%% end, #{}, Properties), -%% -%% case maps:size(ThingList) of -%% 0 -> -%% {error, not_find_thing}; -%% _ -> -%% {ok, ThingList} -%% end; -%% _ -> -%% {error, not_find_thing} -%% end. -%% -%%thinglist2binary(_ThingList) -> -%%%% Str = lists:foldl( -%%%% fun(X, Acc) -> -%%%% Acc ++ " ," ++ dgiot_utils:to_list(X) -%%%% end, [], ThingList), -%%%% dgiot_utils:to_binary(lists:nthtail(2, Str)) -%% <<" * ">>. -%% -%%get_history(Channel, ProductId, DeviceId, ThingMap, Where, Start, End, Type, New) -> -%% case dgiot_data:get({tdengine_os, Channel}) of -%% <<"windows">> -> -%% {error, wrong_td_platform}; -%% _ -> -%% DB = dgiot_tdengine_select:format_db(?Database(ProductId)), -%% TableName = case DeviceId of -%% undefined -> -%% ?Table(ProductId); -%% _ -> -%% ?Table(DeviceId) -%% end, -%% -%% Quality = case dgiot_product:get_device_thing(ProductId, <<"quality">>) of -%% not_find -> -%% #{}; -%% Res -> -%% Res -%% end, -%% Person = case dgiot_product:get_device_thing(ProductId, <<"person">>) of -%% not_find -> -%% #{}; -%% Res1 -> -%% Res1 -%% end, -%% AllThngMap = maps:merge(ThingMap, maps:merge(Quality, Person)), -%% List = maps:keys(AllThngMap) ++ [<<"createdat">>], -%% Select = thinglist2binary(List), -%% From = get_from(New, List, DB, TableName), -%%%% io:format("~s ~p From = ~ts ~n", [?FILE, ?LINE, From]), -%% WHERE = get_where(Where, AllThngMap, Type, ProductId, Start, End), -%%%% io:format("~s ~p WHERE = ~ts ~n", [?FILE, ?LINE, WHERE]), -%%%% LimitSkip = get_limit_skip(Limit, Skip), -%% Order = <<" ORDER BY createdat DESC ">>, -%% dgiot_tdengine:transaction(Channel, -%% fun(Context) -> -%% Sql = <<"SELECT ", Select/binary, " FROM ", From/binary, WHERE/binary, Order/binary, ";">>, -%% dgiot_tdengine_pool:run_sql(Context#{<<"channel">> => Channel}, execute_query, Sql) -%% end) -%% -%% end. - -%%get_limit_skip(undefined, undefined) -> -%% <<" limit 10 ">>; -%%get_limit_skip(Limit, Skip) -> -%% BinLimit = dgiot_utils:to_binary(Limit), -%% BinSkip = dgiot_utils:to_binary(Skip), -%% <<" limit ", BinLimit/binary, " offset ", BinSkip/binary>>. -%%(Where, ThingMap, Type, ProductId, Start, End) -%% -%%get_where(Where, _ProductId) when is_map(Where) -> -%% case maps:size(Where) /= 0 of -%% true -> -%% <<" and ", Where1/binary>> = maps:fold( -%% fun(K, Map, Acc) -> -%% BinOption = dgiot_utils:to_binary(lists:nth(1, maps:keys(Map))), -%% V = lists:nth(1, maps:values(Map)), -%% Type = maps:get(K, ThingMap), -%% BinV = format_value(Type, V), -%% <> -%% end, <<"">>, maps:remove(<<"product">>, Where)), -%% case get_time(Start, End) of -%% error -> -%% <<"where ", Where1/binary>>; -%% Time -> -%% <<" where ", Where1/binary, " and ", Time/binary>> -%% end; -%% _ -> -%% case get_time(Start, End) of -%% error -> -%% <<" ">>; -%% Time -> -%% <<" where ", Time/binary>> -%% end -%% end. -%%get_time(undefined, undefined) -> -%% error; -%%get_time(undefined, End) -> -%% BinEnd = dgiot_utils:to_binary(End * 1000), -%% <<" createdat < ", BinEnd/binary, " ">>; -%%get_time(Start, undefined) -> -%% BinStart = dgiot_utils:to_binary(Start * 1000), -%% <<" createdat > ", BinStart/binary, " ">>; -%%get_time(Start, End) -> -%% BinStart = dgiot_utils:to_binary(Start * 1000), -%% BinEnd = dgiot_utils:to_binary(End * 1000), -%% <<" createdat > ", BinStart/binary, " and createdat < ", BinEnd/binary, " ">>. filter_data(undefined, _, HistoryData) -> Total = length(HistoryData), @@ -301,285 +46,6 @@ filter_data(Data) when is_list(Data) -> filter_data(Data) when is_map(Data) -> maps:filter(fun(_, V) -> (V /= <<"null">>) and (V /= null) end, Data). -%% -%%get_device_list(ProductId) -> -%% case dgiot_parse:query_object(<<"Device">>, #{<<"where">> => #{<<"product">> => ProductId}}) of -%% {ok, #{<<"results">> := Results}} -> -%% Res = lists:foldl( -%% fun(X, Acc) -> -%% case X of #{<<"objectId">> := DevId} -> -%% Acc ++ [DevId]; -%% _ -> -%% Acc -%% end -%% end, [], Results), -%% {ok, Res}; -%% _ -> -%% error -%% end. -%% - - -%%get_from(<<"true">>, List, DB, TableName) -> -%% DevcieTypeList = dgiot_product:get_devicetype(ProductId), -%% List = lists:foldl( -%% fun(DeviceType, Acc) -> -%% case dgiot_product:get_device_thing(ProductId, DeviceType) of -%% not_find -> -%% Acc; -%% ThingMap -> -%% Acc ++ maps:keys(ThingMap) -%% end -%% end, [], DevcieTypeList), -%% io:format("~s ~p List = ~p ~n", [?FILE, ?LINE, length(List)]), -%% Last = lists:foldl( -%% fun(X, Acc) -> -%% L = dgiot_utils:to_list(X), -%% Acc ++ " ,last(" ++ L ++ " ) as " ++ L -%% end, [], List), -%% Select = dgiot_utils:to_binary(lists:nthtail(2, Last)), -%% Group = <<"person_sheetsid">>, -%% <<"( select ", Select/binary, " from ", DB/binary, TableName/binary, " group by ", Group/binary, " ) ">>; -%% -%%get_from(_, _, DB, TableName) -> -%% <>. - -%% -%% -%% -%%get_example(Type, ProductId) -> -%% Map = case get_ThingMap(Type, ProductId) of -%% {ok, Res} -> -%% maps:fold( -%% fun(K, V, Acc) -> -%% case V of -%% <<"text">> -> -%% Acc#{K => <<"text">>}; -%% <<"enum">> -> -%% Acc#{K => <<"text">>}; -%% _ -> -%% Acc#{K => 2.55} -%% end -%% end, #{}, Res) -%% end, -%% io:format("~ts ~n", [unicode:characters_to_list(jsx:encode(Map))]). -%% -%% -%% -%%format_value(<<"text">>, V) -> -%% <<"\"", V/binary, "\"">>; -%%format_value(_, V) -> -%% dgiot_utils:to_binary(V). - - -%%get_work_sheet(ProductId, ProcessType, Start, End, Channel, DeviceId, Where, Limit, Skip, New) -> -%% case get_cache_data(ProductId, DeviceId, ProcessType) of -%% {ok, Res} -> -%% {ok, Res}; -%% _ -> -%% get_td_sheet(ProductId, ProcessType, Start, End, Channel, DeviceId, Where, Limit, Skip, New) -%% end. -%% -%%get_cache_data(ProductId, DeviceId, <<"quality">>) -> -%% case dgiot_data:get(?FACTORY_QUALITY, {ProductId, DeviceId, '-'}) of -%% {ok, Res} -> -%% Res; -%% _ -> -%% error -%% end; -%%get_cache_data(ProductId, DeviceId, Type) -> -%% case dgiot_data:get(?FACTORY_QUALITY, {ProductId, DeviceId, Type}) of -%% {ok, Res} -> -%% Res; -%% _ -> -%% error -%% end. -%% -%%get_cache_data(<<"all">>, ProductId, Start, End, _, DeviceId, Where, Limit, Skip, _) -> -%% Dev = case DeviceId of -%% undefined -> -%% '_'; -%% _ -> -%% DeviceId -%% -%% end, -%% DeviceTypeList = dgiot_product:get_devicetype(ProductId), -%% AllData = lists:foldl( -%% fun(DevType, Acc) -> -%% case dgiot_data:match(?FACTORY_ORDER, {{ProductId, Dev, '_', DevType}, '$1'}) of -%% {ok, Res} -> -%% Acc ++ Res; -%% _ -> -%% Acc -%% end -%% end, [], DeviceTypeList), -%% MergedData = merge_data(AllData), -%% FiltedData = filter_cache_data(MergedData, Start, End, Where), -%% {Total, Result} = filter_data(Limit, Skip, FiltedData), -%% {ok, {Total, Result}}; -%% -%%get_cache_data(ProductId, Type, Start, End, _, DeviceId, Where, Limit, Skip, <<"true">>) -> -%% io:format("~s ~p Type = ~p ~n", [?FILE, ?LINE, Type]), -%% Dev = case DeviceId of -%% undefined -> -%% '_'; -%% _ -> -%% DeviceId -%% -%% end, -%% case dgiot_data:match(?FACTORY_ORDER, {{ProductId, Dev, '_', Type}, '$1'}) of -%% {ok, [Res]} -> -%%%% io:format("~s ~p length = ~p ~n", [?FILE, ?LINE,length(Res)]), -%% FiltedData = filter_cache_data(Res, Start, End, Where), -%%%% io:format("~s ~p length = ~p ~n", [?FILE, ?LINE,length(FiltedData)]), -%% {Total, Result} = filter_data(Limit, Skip, FiltedData), -%%%% io:format("~s ~p length = ~p ~n", [?FILE, ?LINE,length(Result)]), -%% {ok, {Total, Result}}; -%% _ -> -%% error -%% end; -%%get_cache_data(_, _, _, _, _, _, _, _, _, _) -> -%% error. -%% -%%filter_cache_data(Data, undefined, _, Where) -> -%% lists:foldl( -%% fun([X, _], Acc) -> -%% case Where of -%% undefined -> -%% Acc ++ [X]; -%% _ -> -%% case maps:merge(X, Where) == X of -%% true -> -%% Acc ++ [X]; -%% _ -> -%% Acc -%% end -%% end -%% end, [], Data); -%%filter_cache_data(Data, Start, End, Where) -> -%% lists:foldl( -%% fun([X, Date], Acc) -> -%% case (Date > End) and (Date < Start) of -%% true -> -%% case maps:merge(X, Where) == X of -%% true -> -%% Acc ++ [X]; -%% _ -> -%% Acc -%% end; -%% _ -> -%% Acc -%% end -%% end, [], Data). - - -%%merge_data(ParseData, HistoryData, DeviceList, _ThingMap) when is_list(DeviceList) -> -%% lists:foldl( -%% fun(X, Acc) -> -%% DeviceId = maps:get(<<"person_deviceid">>, X, <<"">>), -%% case maps:find(DeviceId, ParseData) of -%% {ok, Parse} -> -%% Acc ++ [maps:merge(Parse, X)]; -%% _ -> -%% Acc -%% end -%% end, [], HistoryData); -%% -%%merge_data(ParseData, HistoryData, _, _ThingMap) -> -%% lists:foldl( -%% fun(X, Acc) -> -%% Acc ++ [maps:merge(ParseData, X)] -%% end, [], HistoryData). -%% -%% -%%search_parse(DeviceList, Parse, Type) when is_list(DeviceList) -> -%% Res = lists:foldl( -%% fun(X, Acc) -> -%% case search_parse(X, Parse, Type) of -%% {ok, Res} -> -%% Acc#{X => Res}; -%% _ -> -%% Acc -%% end -%% end, #{}, DeviceList), -%% {ok, Res}; -%% -%%search_parse(DeviceId, undefined, Type) -> -%% case dgiot_parse:get_object(<<"Device">>, DeviceId) of -%% {ok, #{<<"content">> := #{Type := Data}}} -> -%% FlatternMap = dgiot_map:flatten(#{Type => Data}), -%% {ok, FlatternMap#{<<"objectId">> => DeviceId}}; -%% _ -> -%% error -%% end; -%% -%%search_parse(DeviceId, Parse, Type) -> -%% case maps:size(Parse) of -%% 0 -> -%% search_parse(DeviceId, undefined, Type); -%% Num -> -%% case dgiot_parse:get_object(<<"Device">>, DeviceId) of -%% {ok, #{<<"content">> := Content}} -> -%% FlatMap = dgiot_map:flatten(Content), -%% MatchNum = maps:fold( -%% fun(K, V, Acc) -> -%% case maps:find(K, FlatMap) of -%% {ok, V} -> -%% Acc + 1; -%% _ -> -%% Acc -%% end -%% -%% end, 0, Parse), -%% case MatchNum of -%% Num -> -%% Data = maps:get(<>, Content), -%% FlatternMap = dgiot_map:flatten(#{Type => Data}), -%% {ok, FlatternMap#{<<"objectId">> => DeviceId}}; -%% _ -> -%% error -%% end -%% end -%% end. -%% -%%filter_where(undefined, ProductId, Type) -> -%% case get_ThingMap(Type, ProductId) of -%% {ok, ThingMap} -> -%% {undefined, undefined, ThingMap}; -%% _ -> -%% error -%% end; -%%filter_where(Where, ProductId, Type) -> -%% MapWhere = case is_map(Where) of -%% true -> -%% dgiot_product_enum:turn_num(Where, ProductId, Type); -%% _ -> -%% dgiot_product_enum:turn_num(jsx:decode(Where), ProductId, Type) -%% end, -%% case get_ThingMap(Type, ProductId) of -%% {ok, ThingMap} -> -%% {Parse, Td} = maps:fold( -%% fun(K, V, {Parse, Td}) -> -%% case maps:is_key(K, ThingMap) of -%% true -> -%% {Parse, Td#{K => V}}; -%% _ -> -%% {Parse#{K => V}, Td} -%% -%% end -%% end, {#{}, #{}}, MapWhere), -%% -%% TdWithPerson = case maps:is_key(<<"person">>, MapWhere) of -%% true -> -%% Td#{<<"person">> => maps:get(<<"person">>, MapWhere)}; -%% false -> -%% Td -%% end, -%% {Parse, TdWithPerson, ThingMap}; -%% _ -> -%% error -%% end. -%%get_td_sheet(ProductId, Type, Start, End, Channel, DeviceId, Where, Limit, Skip, New) get_history_data(ProductId, DeviceId, Type, Function, FunctionMap, Group, Having, Where, Order, Channel, Limit, Skip) -> DB = dgiot_tdengine:get_database(Channel, ProductId), @@ -597,30 +63,48 @@ get_history_data(ProductId, DeviceId, Type, Function, FunctionMap, Group, Having Have = have(Having), WHERE = where(Where), ORDER = order(Order), -%% LimitAndSkip = limit_skip(Limit, Skip), + LimitAndSkip = limit_skip(Limit, Skip), + LimitAndSkip = limit_skip(Limit, Skip), + Sql = <<"SELECT ", Select/binary, " FROM ", From/binary, WHERE/binary, GROPU/binary, Have/binary, ORDER/binary, LimitAndSkip/binary, ";">>, + CountSql = <<"SELECT count(*) ", " FROM ", From/binary, WHERE/binary, GROPU/binary, Have/binary, ORDER/binary, ";">>, + case run_data_sql(Channel,Sql) of + {ok,Data}-> + case run_count_sql(Channel,CountSql) of + {ok,Total} -> + {ok,{Total,Data}}; + _-> + {ok,{null,Data}} + end; + _-> + error + end . + + + + + + +run_data_sql(Channel,Sql) -> case dgiot_tdengine:transaction(Channel, fun(Context) -> - Sql = <<"SELECT ", Select/binary, " FROM ", From/binary, WHERE/binary, GROPU/binary, Have/binary, ORDER/binary, ";">>, - io:format("~s ~p Sql = ~p ~n", [?FILE, ?LINE, Sql]), - dgiot_tdengine_pool:run_sql(Context#{<<"channel">> => Channel}, execute_query, Sql) - end) - of + end) of {ok, #{<<"results">> := HistoryData}} -> - NamedData = dgiot_product_enum:turn_name(HistoryData, ProductId), - {Total, FileredRes} = filter_data(Limit, Skip, NamedData), - Data = case dgiot_hook:run_hook({factory, ProductId, afterTd}, [FileredRes]) of - {ok, [{ok, AfteRes}]} -> - AfteRes; - _ -> - FileredRes - end, - {ok, {Total, Data}}; - _ -> + {ok,HistoryData}; + _-> error - end. +run_count_sql(Channel,Sql) -> + case dgiot_tdengine:transaction(Channel, + fun(Context) -> + dgiot_tdengine_pool:run_sql(Context#{<<"channel">> => Channel}, execute_query, Sql) + end) of + {ok, #{<<"results">> := [#{<<"count(*)">> :=Total}]}} -> + {ok,Total}; + _-> + error + end. group(undefined) -> <<" ">>; diff --git a/apps/dgiot_factory/src/dgiot_factory_screen.erl b/apps/dgiot_factory/src/dgiot_factory_screen.erl index d34c38d9..4a588f2c 100644 --- a/apps/dgiot_factory/src/dgiot_factory_screen.erl +++ b/apps/dgiot_factory/src/dgiot_factory_screen.erl @@ -25,7 +25,7 @@ -define(KEY, <<"key">>). -export([get_screen/2]). --export([format_data/3,turn_echart/3]). +-export([format_data/3, turn_echart/3]). -export([init_screen_data/0, updata_screen_data/3, format_bar/1]). @@ -117,7 +117,7 @@ turn_echart(<<"total">>, #{<<"data">> := Data}, FormatedData) -> Acc#{Key => 0} end, #{}, maps:keys(Data)), - maps:fold( + Map = maps:fold( fun(_, DataMap, OldAcc) -> maps:fold( fun(Key, OldSum, Acc) -> @@ -133,8 +133,19 @@ turn_echart(<<"total">>, #{<<"data">> := Data}, FormatedData) -> Acc#{Key => OldSum + Num} end, #{}, OldAcc) - end, InitModel, FormatedData); + end, InitModel, FormatedData), +%% io:format("~s ~p Map = ~p. ~n", [?FILE, ?LINE, Map]), + case maps:get(<<"statis_produced">>, Map, 0) of + 0 -> + Map#{<<"statis_percent">> => 0}; + Ptatis_produced -> + Qualified = maps:get(<<"statis_qualitified">>, Map, 0), +%% io:format("~s ~p Qualified = ~p. ~n", [?FILE, ?LINE, Qualified]), + NewMap = maps:merge(Map, #{<<"statis_percent">> => dgiot_factory_utils:float(Qualified / Ptatis_produced, 2)}), +%% io:format("~s ~p NewMap = ~p. ~n", [?FILE, ?LINE, NewMap]), + NewMap + end; turn_echart(<<"bar">>, #{<<"model">> := #{<<"series">> := SeriesMod} = Model}, FormatedData) -> {XAxis, NewSeries} = maps:fold( fun(X, Data, {Xlist, SeriesAcc}) -> @@ -142,7 +153,6 @@ turn_echart(<<"bar">>, #{<<"model">> := #{<<"series">> := SeriesMod} = Model}, F fun(#{?KEY := Key} = OneSeries, Acc) -> OldList = maps:get(<<"data">>, OneSeries, []), NewValue = maps:get(Key, Data, 0), - io:format("~s ~p NewValue = ~p. ~n", [?FILE, ?LINE, NewValue]), NewList = OldList ++ [NewValue], Acc ++ [OneSeries#{<<"data">> => NewList}]; (S, Acc) -> diff --git a/apps/dgiot_factory/src/dgiot_factory_statis.erl b/apps/dgiot_factory/src/dgiot_factory_statis.erl index e38ef722..72850f9c 100644 --- a/apps/dgiot_factory/src/dgiot_factory_statis.erl +++ b/apps/dgiot_factory/src/dgiot_factory_statis.erl @@ -17,7 +17,7 @@ do_statis(TaskProductId, TaskDeviceId, PersonType, Payload) -> NewData = run_statis_hook(TaskProductId, TaskDeviceId, PersonType, Payload, DefaultData), dgiot_data:insert(?FACTORYSTATIS, {TaskProductId, TaskDeviceId}, NewData), update2parse(TaskDeviceId, NewData), - io:format("~s ~p NewData = ~p. ~n", [?FILE, ?LINE, NewData]), +%% io:format("~s ~p NewData = ~p. ~n", [?FILE, ?LINE, NewData]), dgiot_factory_utils:save2td(TaskDeviceId, NewData). get_old_data(TaskProductId, TaskDeviceId) -> @@ -61,10 +61,9 @@ get_default_data(<<"quality">>, #{<<"quality">> := #{<<"type">> := Process, <<"q get_default_data(Process, Payload, OldData) -> ProducedNum = maps:get(<<"num">>, maps:get(Process, Payload, #{}), 0), - OldProduced = maps:get(<<"produced">>, maps:get(<<"statis">>, OldData, #{}), 0), - NewProduced = ProducedNum + OldProduced, + OrderInfo = get_order_info(Process, Payload), - dgiot_map:merge(OldData, #{<<"order">> => OrderInfo, <<"statis">> => #{<<"produced">> => NewProduced}}). + dgiot_map:merge(OldData, #{<<"order">> => OrderInfo, <<"statis">> => #{<<"produced">> => ProducedNum}}). %%记录生产计划信息,计划号,产成品编码,工序 get_order_info(Process, Payload) -> diff --git a/apps/dgiot_factory/src/dgiot_factory_utils.erl b/apps/dgiot_factory/src/dgiot_factory_utils.erl index dccdfed0..11b923c1 100644 --- a/apps/dgiot_factory/src/dgiot_factory_utils.erl +++ b/apps/dgiot_factory/src/dgiot_factory_utils.erl @@ -26,7 +26,7 @@ -export([get_json_file/1, unflatten_map/1, flatten_map/1,merge_map/2]). -export([save2td/3, save2td/2]). -export([kill_undefined/1]). - +-export([float/2,get_card_data/2,get_cache_data/3,keep_decimal/2]). @@ -266,10 +266,11 @@ get_sum(BatchList) when is_list(BatchList) -> get_sum(_) -> 0. + batch_create_worker(ProductId, WorkerList, InitNum) -> lists:foldl( fun(Worker, Num) -> - dgiot_factory_channel:init_worker_device(ProductId, Num, Worker), + dgiot_factory_channel:init_worker_device(ProductId, Num, dgiot_utils:to_binary( Worker)), Num + 1 end, InitNum, WorkerList). @@ -486,3 +487,49 @@ kill_undefined(Arg) when is_map(Arg) -> end, #{}, Arg); kill_undefined(Arg) -> Arg. +float(Number, X) -> + N = math:pow(10,X), + round(Number*N)/N. + + + +get_card_data(BatchProductId, BatchDeviceId) -> + DevcieTypeList = dgiot_product:get_devicetype(BatchProductId), + lists:foldl( + fun(DeviceType, Acc) -> + Res = case get_cache_data(BatchProductId, BatchDeviceId, DeviceType) of + {ok, R} -> + R; + _ -> + #{} + end, + dgiot_map:merge(Acc, Res) + end, #{}, DevcieTypeList). +get_cache_data(BatchProductId, BatchDeviceId, DeviceType) -> + case dgiot_data:get(?FACTORY_ORDER, {BatchProductId, BatchDeviceId, DeviceType}) of + not_find -> + case dgiot_parse:query_object(<<"Devicelog">>, #{<<"where">> => #{<<"data.person.sheetsid">> => BatchDeviceId, <<"data.person.type">> => DeviceType}, <<"order">> => <<"-createdAt">>, <<"limit">> => 1}) of + {ok, #{<<"results">> := [#{<<"data">>:= Data}]}} -> + {ok,maps:without([<<"quality">>],Data)}; + _R -> +%% io:format("~s ~p _R ~p~n", [?FILE, ?LINE, _R]), + error + end; + Res -> + {ok,maps:without([<<"quality">>],Res)} + end. + + +keep_decimal(Float, Num) when is_float(Float) -> + F=fun(_, Base1) -> + Base1*10 + end, + Base = lists:foldl(F, 10, lists:seq(1, Num)), + Float2 = Float*Base, + Int = erlang:trunc(Float2), + Rest = Int rem 10, + Keep = Int div 10, + Add = erlang:round((Rest/10)), + (Keep+Add)*10/Base; +keep_decimal(Float, _Num) -> + Float. diff --git a/apps/dgiot_factory/src/dgiot_material_channel.erl b/apps/dgiot_factory/src/dgiot_material_channel.erl index f01b1554..6804cfc9 100644 --- a/apps/dgiot_factory/src/dgiot_material_channel.erl +++ b/apps/dgiot_factory/src/dgiot_material_channel.erl @@ -107,15 +107,15 @@ handle_message({sync_parse, _Pid, 'before', put, _Token, <<"Device">>, #{<<"id"> end, dgiot_parse_hook:publish(_Pid, NewQueryData), {ok, State}; -handle_message({sync_parse, _Pid, 'after', post, _Token, <<"Device">>, #{<<"objectId">> := DeviceId} = QueryData}, State) -> +handle_message({sync_parse, _Pid, 'after', post, _Token, <<"Device">>, #{<<"basedata">> := BaseData,<<"objectId">> := DeviceId} = QueryData}, State) -> %% case dgiot_device:lookup(DeviceId) of %% {ok, #{<<"productid">> := ProductId}} -> case dgiot_parse:get_object(<<"Device">>, DeviceId) of {ok, #{<<"product">> := #{<<"objectId">> := ProductId}}} -> case catch dgiot_hook:run_hook({sync_parse, 'after', post, ProductId}, {QueryData, ProductId, State}) of - {ok, [Res]} -> - io:format("~s ~p DeviceId = ~p ~n", [?FILE, ?LINE, DeviceId]), - Res; + {ok, [{ok,Res}]} -> + NewBaseData = dgiot_map:merge(BaseData,Res), + dgiot_parse:update_object(<<"Device">>,DeviceId,#{<<"basedata">> => NewBaseData}); _ -> QueryData end;