Skip to content

Commit

Permalink
fix:factory
Browse files Browse the repository at this point in the history
  • Loading branch information
shifttttttt committed Oct 16, 2022
1 parent 40faa17 commit ce0150d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
39 changes: 23 additions & 16 deletions apps/dgiot_factory/src/dgiot_factory_channel.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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/7, get_card_data/2]).
-export([get_sub_product/1,get_new_acl/2]).
-export([get_sub_product/1, get_new_acl/2]).

%% 注册通道类型
-channel_type(#{
Expand Down Expand Up @@ -133,9 +133,9 @@ handle_data(_TaskProductId, TaskDeviceId, BatchProductId, BatchDeviceId, BatchAd
NewPayLoad = run_factory_hook(_TaskProductId, TaskDeviceId, BatchProductId, BatchDeviceId, PersonType, NewData),
dgiot_data:insert(?FACTORY_ORDER, {BatchProductId, BatchDeviceId, PersonType}, NewPayLoad),
OldData = get_card_data(BatchProductId, BatchDeviceId),
ALlData = maps:merge(OldData, NewPayLoad),
save2parse(BatchDeviceId, ALlData),
save2td(BatchProductId, BatchAddr, ALlData).
ALlData = dgiot_map:merge(OldData, NewPayLoad),
save2parse(BatchProductId, BatchDeviceId, ALlData),
save2td(BatchProductId, BatchAddr, NewPayLoad).
get_card_data(BatchProductId, BatchDeviceId) ->
DevcieTypeList = dgiot_product:get_devicetype(BatchProductId) -- [<<"quality">>],
lists:foldl(
Expand All @@ -144,12 +144,12 @@ get_card_data(BatchProductId, BatchDeviceId) ->
not_find ->
Acc;
Res ->
maps:merge(Acc, Res)
dgiot_map:merge(Acc, Res)
end
end, #{}, DevcieTypeList).

process_data(Content, PersonType, Token, TaskDeviceId) ->
FlatMap = dgiot_map:flatten(Content),
process_data(FlatMap, PersonType, Token, TaskDeviceId) ->
%% FlatMap = dgiot_map:flatten(Content),
%% io:format("~s ~p status =~p ~n", [?FILE, ?LINE,maps:get(<<"quality_status">>,FlatMap,0)]),
case dgiot_parse:get_object(<<"Device">>, TaskDeviceId) of
{ok, #{<<"name">> := OrderName, <<"product">> := #{<<"objectId">> := TaskProductId}}} ->
Expand All @@ -162,7 +162,9 @@ process_data(Content, PersonType, Token, TaskDeviceId) ->
end.

init_data(_TaskProductId, TaskDeviceId, BatchDeviceId, FlatMap, _PersonType, Token) ->
maps:merge(FlatMap, #{<<"person_sessiontoken">> => Token, <<"person_deviceid">> => TaskDeviceId, <<"person_sheetsid">> => BatchDeviceId}).
%% maps:merge(FlatMap, #{<<"person_sessiontoken">> => Token, <<"person_deviceid">> => TaskDeviceId, <<"person_sheetsid">> => BatchDeviceId}).
dgiot_map:merge(FlatMap, #{<<"person">> => #{<<"sessiontoken">> => Token, <<"deviceid">> => TaskDeviceId, <<"sheetsid">> => BatchDeviceId}}).

after_handle(ProductId, DevAddr, Payload, _Type) ->
Use = turn_user(Payload),
dgiot_task:save_td(ProductId, DevAddr, Payload#{<<"person_sessiontoken">> => Use}, #{}).
Expand Down Expand Up @@ -240,7 +242,6 @@ get_roll_dev_id(ProductId, FlatMap) ->
{ok, BatchDeviceId} ->
case dgiot_device:lookup(BatchDeviceId) of
{ok, #{<<"devaddr">> := BatchAddr}} ->

{BatchProductId, BatchDeviceId, BatchAddr};
_ ->
error
Expand All @@ -263,19 +264,25 @@ get_new_acl(SessionToken, Acl) ->
Acc
end
end, [], Roles),
NewRoleList = Acl ++ (UserRoleList --Acl ),
NewRoleList = Acl ++ (UserRoleList --Acl),
lists:foldl(
fun(X, Acc) ->
maps:merge(Acc,#{dgiot_utils:to_binary(X) => #{<<"read">> => true, <<"write">> => true}})
maps:merge(Acc, #{dgiot_utils:to_binary(X) => #{<<"read">> => true, <<"write">> => true}})
end, #{}, NewRoleList);
Err -> {400, Err}
end.

save2parse(BatchDeviceId, ALlData) ->
NameData = dgiot_factory_utils:turn_name(ALlData, BatchDeviceId),
Content = dgiot_map:unflatten(NameData),

save2parse(BatchProductId, BatchDeviceId, ALlData) ->
Content = case dgiot_hook:run_hook({factory, BatchProductId, beforeParse}, [ALlData]) of
{ok, [{ok, Res}]} ->
Res;
_ ->
ALlData
end,
dgiot_parse:update_object(<<"Device">>, BatchDeviceId, #{<<"content">> => Content}).

save2td(BatchProductId, BatchAddr, ALlData) ->
NumData = dgiot_factory_utils:turn_num(ALlData, BatchProductId),
save2td(BatchProductId, BatchAddr, Data) ->
FlatternData = dgiot_map:flatten(Data),
NumData = dgiot_factory_utils:turn_num(FlatternData, BatchProductId),
dgiot_task:save_td(BatchProductId, BatchAddr, NumData, #{}).
18 changes: 12 additions & 6 deletions apps/dgiot_factory/src/dgiot_factory_data.erl
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ filter_data(Data) when is_map(Data) ->
%% 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) ->
get_history_data(ProductId, DeviceId, Type, Function, FunctionMap, Group, Having, Where, Order, Channel, Limit, Skip) ->
DB = dgiot_tdengine_select:format_db(?Database(ProductId)),
TableName = case DeviceId of
undefined ->
Expand All @@ -593,22 +593,28 @@ get_history_data(ProductId, DeviceId, Type, Function, FunctionMap, Group,Having,
Select = select(ProductId, Type, Function, FunctionMap),
From = <<DB/binary, TableName/binary>>,
GROPU = group(Group),
Have =have(Having),
Have = have(Having),
WHERE = where(Where),
ORDER = order(Order),
%% LimitAndSkip = limit_skip(Limit, Skip),
case dgiot_tdengine:transaction(Channel,
fun(Context) ->
Sql = <<"SELECT ", Select/binary, " FROM ", From/binary, WHERE/binary,GROPU/binary,Have/binary, ORDER/binary, ";">>,
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
{ok, #{<<"results">> := HistoryData}} ->
NamedData = dgiot_factory_utils:turn_name(HistoryData, ProductId),
{Total, Res} = filter_data(Limit, Skip, NamedData),
{ok, {Total, filter_data(Res)}};
{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}};
_ ->
error

Expand Down Expand Up @@ -659,7 +665,7 @@ select(ProductId, Type, undefined, _) ->
end, <<"">>, ThingList),
Res;

select(ProductId, Type, Function, FunctiongMap1) ->
select(ProductId, Type, Function, FunctiongMap1) ->
ThingList = get_thing_list(ProductId, Type),
FunctiongMap = dgiot_utils:to_map(FunctiongMap1),
<<" , ", Res/binary>> =
Expand Down
2 changes: 0 additions & 2 deletions apps/dgiot_factory/src/dgiot_factory_material.erl
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,6 @@ get_warehouse_material(_, _, _, _) ->


put_warehouse_material(#{<<"objectId">> := Id} = Record) ->


case dgiot_parse:get_object(<<"Device">>, Id) of
{ok, #{<<"content">> := Content}} ->
case maps:find(<<"isEnable">>, Record) of
Expand Down
6 changes: 4 additions & 2 deletions apps/dgiot_factory/src/dgiot_factory_shift.erl
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ bits_to_workerlist(Bits) ->
Res.


format_worker(Worker) ->
format_worker(Worker) when is_binary(Worker) ->
WorkerList = re:split(Worker, <<" ">>),
lists:foldl(
fun(X, Acc) ->
Expand All @@ -340,7 +340,9 @@ format_worker(Worker) ->
%% io:format("~s ~p Name = ~p.~n", [?FILE, ?LINE, Name]),
<<Acc/binary, " ", Name/binary>>
end
end, <<"">>, WorkerList).
end, <<"">>, WorkerList);
format_worker(Worker) ->
Worker.


update_cache(#{<<"device">> := Dev, <<"date">> := Date, <<"shift">> := Shift, <<"worker">> := Worker}) ->
Expand Down

0 comments on commit ce0150d

Please sign in to comment.