Skip to content

Commit

Permalink
fix:factory_worker
Browse files Browse the repository at this point in the history
  • Loading branch information
shifttttttt committed Oct 19, 2022
1 parent fed3395 commit d69fc91
Show file tree
Hide file tree
Showing 6 changed files with 357 additions and 65 deletions.
66 changes: 65 additions & 1 deletion apps/dgiot_factory/priv/swagger/swagger_factory.json
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@
},
"/shift_time": {
"get": {
"description": "用户树",
"description": "获取班次默认信息",
"security": [],
"parameters": [
{
Expand Down Expand Up @@ -678,6 +678,70 @@
"type": "string",
"default": "fastdfs",
"description": "车间"
},
{
"name": "product",
"in": "query",
"type": "string",
"description": "人员产品id"
}
],
"responses": {
"200": {
"description": "Returns operation status"
},
"400": {
"description": "Bad Request"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"500": {
"description": "Server Internal error"
}
},
"tags": [
"jienuo"
]
}
},
"/duplicate_shift": {
"put": {
"security": [],
"summary": "复制排班",
"description": "复制排班",
"consumes": [
"*"
],
"parameters": [
{
"in": "body",
"name": "user",
"description": "用户信息",
"required": true,
"schema": {
"type": "object",
"properties": {
"product": {
"description": "人员产品id",
"type": "string",
"example": "38e6d7b035"
},
"sink_date": {
"description": "目标日期",
"type": "string",
"example": "1666022400"
},
"where": {
"description": "复制条件",
"type": "string",
"example": "worker_date = 1664812800"
}
}
}
}
],
"responses": {
Expand Down
80 changes: 75 additions & 5 deletions apps/dgiot_factory/src/dgiot_factory_channel.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@
-define(TYPE, <<"FACTORY">>).
-define(SHEETID(SHEET), <<SHEET/binary, "_id">>).
-define(MAX_BUFF_SIZE, 1024).
-define(WORKERCATEGORY, <<"bf6cbee357">>).
-record(state, {id, mod, product, env = #{}}).
%% API
-export([start/2]).

%% 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, init_worker_device/3]).

%% 注册通道类型
-channel_type(#{
cType => ?TYPE,
type => ?BACKEND_CHL,
type => ?PROTOCOL_CHL,
priority => 2,
title => #{
zh => <<"数字工厂通道"/utf8>>
Expand Down Expand Up @@ -68,13 +69,32 @@ start(ChannelId, ChannelArgs) ->
dgiot_channelx:add(?TYPE, ChannelId, ?MODULE, ChannelArgs).

%% 通道初始化
init(?TYPE, ChannelId, Args) ->
init(?TYPE, ChannelId, #{<<"product">> := Products} = Args) ->
State = #state{
id = ChannelId,
env = Args
},
lists:map(
fun
({ProductId, _}) ->
case dgiot_product:get(ProductId) of
{ok, #{<<"devType">> := DevType, <<"name">> := Name}} ->
TempProductId = get_wokrer_id(Name, DevType),
case TempProductId of
ProductId ->
dgiot_data:insert({ChannelId, worker}, ProductId);
_ ->
pass
end;
_ ->
pass
end
end, Products),
dgiot_parse_hook:subscribe(<<"Device/*">>, put, ChannelId, [<<"content">>]),
dgiot_parse_hook:subscribe(<<"Device/*">>, delete, ChannelId),
dgiot_parse_hook:subscribe(<<"_User/*">>, post, ChannelId),
dgiot_parse_hook:subscribe(<<"_User/*">>, put, ChannelId),
dgiot_parse_hook:subscribe(<<"_User/*">>, delete, ChannelId),
{ok, State, []}.

handle_init(State) ->
Expand All @@ -86,7 +106,23 @@ handle_event(_EventId, Event, State) ->
{ok, State}.


handle_message({sync_parse, _Pid, 'after', put, _Token, <<"_User">>, #{<<"objectId">> := UserId} = _QueryData},
#state{id = ChannelId} = State) ->
io:format("~s ~p ChannelId =~p ~n", [?FILE, ?LINE, ChannelId]),
case dgiot_data:get({ChannelId, worker}) of
not_find ->
pass;
ProductId ->
case dgiot_parse:get_object(<<"_User">>, UserId) of
{ok, #{<<"username">> := WorkerNum, <<"nick">> :=WorkerName }} ->
init_worker_device(ProductId, WorkerNum, WorkerName);
_ ->
pass
end
end,
{ok, State};
handle_message({sync_parse, _Pid, 'before', put, Token, <<"Device">>, #{<<"content">> := Content, <<"id">> := TaskDeviceId} = _QueryData}, State) ->
io:format("~s ~p TaskDeviceId =~p ~n", [?FILE, ?LINE, TaskDeviceId]),
case dgiot_device_cache:lookup(TaskDeviceId) of
{ok, #{<<"productid">> := TaskProductId}} ->
case Content of
Expand Down Expand Up @@ -238,7 +274,7 @@ process_roll_dev(TaskProductId, TaskDeviceId, OrderName, SessionToken, FlatMap)

get_roll_dev_id(ProductId, FlatMap) ->
BatchProductId = get_sub_product(ProductId),
case maps:find(<<"person_sheetsid">>, FlatMap) of
case maps:find(<<"sheetsid">>, maps:get(<<"person">>,FlatMap,#{})) of
{ok, BatchDeviceId} ->
case dgiot_device:lookup(BatchDeviceId) of
{ok, #{<<"devaddr">> := BatchAddr}} ->
Expand Down Expand Up @@ -280,9 +316,43 @@ save2parse(BatchProductId, BatchDeviceId, ALlData) ->
_ ->
ALlData
end,
dgiot_parse:update_object(<<"Device">>, BatchDeviceId, #{<<"content">> => Content}).
dgiot_parse:update_object(<<"Device">>, BatchDeviceId, #{<<"content">> => Content}).

save2td(BatchProductId, BatchAddr, Data) ->
FlatternData = dgiot_map:flatten(Data),
NumData = dgiot_factory_utils:turn_num(FlatternData, BatchProductId),
dgiot_task:save_td(BatchProductId, BatchAddr, NumData, #{}).


get_wokrer_id(Name, DevType) ->
dgiot_parse_id:get_productid(?WORKERCATEGORY, DevType, Name).

init_worker_device(ProductId, WorkerNum, WorkerName) ->
case dgiot_product:get(ProductId) of
{ok, Product} ->
case Product of
#{<<"ACL">> := Acl, <<"name">> := Name, <<"devType">> := DevType, <<"dynamicReg">> := true} ->
Device = #{
<<"status">> => <<"ONLINE">>,
<<"brand">> => Name,
<<"devModel">> => DevType,
<<"name">> => WorkerName,
<<"devaddr">> => WorkerNum,
<<"product">> => ProductId,
<<"ACL">> => Acl
},

dgiot_device:create_device(Device),
AllData = #{<<"worker_validate">> => true,
<<"worker_num">> => WorkerNum,
<<"worker_date">> => 0,
<<"worker_name">> => WorkerName,
<<"product">> => ProductId},
NumData = dgiot_factory_utils:turn_num(AllData,ProductId),
dgiot_task:save_td_no_match(ProductId, WorkerNum, NumData, #{});
_ ->
pass
end;
_ ->
error
end.
2 changes: 1 addition & 1 deletion apps/dgiot_factory/src/dgiot_factory_data.erl
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ order(undefined) ->
order(Order) ->
<<" ", Order/binary>>.
limit_skip(undefined, undefined) ->
<<" limit 10 ">>;
<<" ">>;
limit_skip(Limit, Skip) ->
BinLimit = dgiot_utils:to_binary(Limit),
BinSkip = dgiot_utils:to_binary(Skip),
Expand Down
22 changes: 18 additions & 4 deletions apps/dgiot_factory/src/dgiot_factory_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ do_request(post_worker_shift, #{<<"shift">> := Shifts} = _Args, _Context, _Body)
{ok, #{<<"status">> => 0, msg => <<"修改成功"/utf8>>, <<"data">> => #{}}};

do_request(put_worker_shift, _Args, _Context, _Body) ->
io:format("~s ~p _Args = ~p ~n", [?FILE, ?LINE, _Args]),
case dgiot_factory_worker:put_worker_shift(_Args) of
{ok, _} ->
{ok, #{<<"status">> => 0, msg => <<"修改成功"/utf8>>, <<"data">> => #{}}};
Expand Down Expand Up @@ -226,18 +225,33 @@ do_request(get_useablematerial, #{<<"id">> := Id} = _Arg, _Context, _Req) ->
_ ->
{error, <<"not find useable material">>}
end;
do_request(get_workshop_worker, #{<<"WorkShop">> := WorkShop} = _Args, _Context, _Body) ->
do_request(get_workshop_worker, #{<<"WorkShop">> := WorkShop, <<"product">> := ProductId} = _Args, _Context, _Body) ->
%% {Res,_} = dgiot_jienuo_utils:get_shift(WorkShop),
Res = dgiot_factory_worker:get_work_shop_workers(WorkShop),
Res = dgiot_factory_worker:get_work_shop_workers(WorkShop, ProductId),
%% io:format("~s ~p Res= ~ts ~n", [?FILE, ?LINE, unicode:characters_to_list(jiffy:encode(Res))]),
{ok, #{<<"status">> => 0, msg => <<"操作成功"/utf8>>, <<"data">> => #{<<"options">> => Res}}};

do_request(get_new_worker_num, #{<<"product">> := ProductId} = _Arg, _Context, _Req) ->
Num = dgiot_factory_worker:get_new_workernum(ProductId),
{ok, #{<<"status">> => 0, msg => <<"操作成功"/utf8>>, <<"data">> => Num}};
{ok, #{<<"status">> => 0, msg => <<"操作成功"/utf8>>, <<"data">> => dgiot_utils:to_binary(Num)}};
do_request(get_shift_time, #{<<"product">> := ProductId, <<"shift">> := Shift} = _Arg, _Context, _Req) ->
Res = dgiot_factory_worker:get_shift_time(ProductId, Shift),
{ok, #{<<"status">> => 0, msg => <<"操作成功"/utf8>>, <<"data">> => Res}};
do_request(put_duplicate_shift, #{<<"product">> := ProductId, <<"sink_date">> := SinkDate, <<"where">> := Where} = _Arg,
#{<<"sessionToken">> := SessionToken} = _Context, _Req) ->
case dgiot_product_tdengine:get_channel(SessionToken) of
{error, Error} ->
{error, Error};
{ok, Channel} ->
case dgiot_factory_worker:duplicate_shift(SinkDate,Where, ProductId, Channel) of
{ok, Res} ->
{ok, #{<<"status">> => 0, msg => <<"操作成功"/utf8>>, <<"data">> => Res}};
_ ->

{error, <<"failed_duplicate">>}

end
end;
%% 服务器不支持的API接口
do_request(_OperationId, _Args, _Context, _Req) ->
io:format("~s ~p _Args = ~p ~n", [?FILE, ?LINE, _Args]),
Expand Down
Loading

0 comments on commit d69fc91

Please sign in to comment.