fix:factory

This commit is contained in:
guo 2022-11-10 19:02:56 +08:00
parent 723bacf366
commit 57170c4c05
8 changed files with 433 additions and 35 deletions

View File

@ -25,7 +25,7 @@
-export([parse_cache_Device/1, sync_parse/1, get/2, post/1, post/2, put/1, save/1, save/2, lookup/1, lookup/2, delete/1, delete/2]).
-export([save_profile/1, get_profile/1, get_profile/2, get_online/1, online/1, offline/1, offline_child/1, enable/1, disable/1]).
-export([put_location/3, get_location/1, get_address/3]).
-export([get_acl/1, save_log/3, get_url/1, get_appname/1]).
-export([get_acl/1,get_readonly_acl/1, save_log/3, get_url/1, get_appname/1]).
parse_cache_Device(_ClasseName) ->
dgiot_device_cache:parse_cache_Device(_ClasseName).
@ -339,3 +339,16 @@ save_log(DeviceId, Payload, Domain) ->
pass
end.
get_readonly_acl(DeviceId) ->
case dgiot_device_cache:lookup(DeviceId) of
{ok, #{<<"acl">> := AclList}} ->
lists:foldl(
fun(Role, Acc) ->
Acc#{Role => #{
<<"read">> => true,
<<"write">> => false}}
end, #{}, AclList);
_ ->
#{}
end.

View File

@ -764,12 +764,106 @@
"factory"
]
}
},
"/sumpick": {
"put": {
"summary": "获取总数",
"security": [],
"description": "获取总数",
"parameters": [
{
"in": "query",
"name": "BatchList",
"description": "list",
"type": "string",
"required": true
}
],
"consumes": [
"application/json"
],
"responses": {
"200": {
"description": "Returns operation status"
},
"400": {
"description": "Bad Request"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"500": {
"description": "Server Internal error"
}
},
"tags": [
"factory"
]
}
},
"/materialApply": {
"put": {
"summary": "获取总数",
"security": [],
"description": "获取总数",
"parameters": [
{
"description": "更新设备管理的ID",
"in": "path",
"name": "id",
"required": true,
"type": "string"
},
{
"in": "body",
"name": "body",
"description": "日历",
"required": true,
"schema": {
"type": "object",
"properties": {
"basedata": {
"description": "basedata",
"type": "string"
}
}
}
}
],
"consumes": [
"application/json"
],
"responses": {
"200": {
"description": "Returns operation status"
},
"400": {
"description": "Bad Request"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"500": {
"description": "Server Internal error"
}
},
"tags": [
"factory"
]
}
}
},
"tags": [
{
"name": "factory",
"description": "数字工厂"
"description": "factory"
}
]
}

View File

@ -244,7 +244,7 @@ do_request(post_duplicate_shift, #{<<"product">> := ProductId, <<"sink_date">> :
{error, Error} ->
{error, Error};
{ok, Channel} ->
case dgiot_factory_worker:duplicate_shift(SinkDate,Where, ProductId, Channel) of
case dgiot_factory_worker:duplicate_shift(SinkDate, Where, ProductId, Channel) of
{ok, Res} ->
{ok, #{<<"status">> => 0, msg => <<"操作成功"/utf8>>, <<"data">> => Res}};
_ ->
@ -253,6 +253,12 @@ do_request(post_duplicate_shift, #{<<"product">> := ProductId, <<"sink_date">> :
end
end;
do_request(put_sumpick, #{<<"BatchList">> := BatchList} = _Arg, _Context, _Req) ->
Sum = dgiot_factory_utils:get_sum(BatchList),
{ok, #{<<"status">> => 0, msg => <<"操作成功"/utf8>>, <<"data">> => #{<<"SumPick">> => Sum}}};
do_request(put_materialapply_id, #{<<"id">> := DeviceId,<<"basedata">> := BaseData} = _Arg, _Context, _Req) ->
dgiot_material_channel:handle_material_apply(DeviceId,BaseData),
{ok, #{<<"status">> => 0, msg => <<"操作成功"/utf8>>, <<"data">> => #{}}};
%% API接口
do_request(_OperationId, _Args, _Context, _Req) ->
io:format("~s ~p _Args = ~p ~n", [?FILE, ?LINE, _Args]),

View File

@ -20,15 +20,16 @@
%% API
-export([get_material_record/2, post_material/2]).
-export([get_warehouse_material/4, put_warehouse_material/1]).
-export([get_usable_material/1]).
get_material_record(DeviceId, Depart) ->
case dgiot_parse:get_object(<<"Device">>, DeviceId) of
{ok, #{<<"basedata">> := #{<<"material">> := Material}}} ->
DepartMaterial = get_depart_material(Material, Depart),
{ok, get_usable_material(DepartMaterial)};
{ok, DepartMaterial};
_ ->
case dgiot_hook:run_hook({factory, get_material}, [DeviceId]) of
{ok, [{ok, Material}]} ->
{ok, get_usable_material(Material)};
{ok, Material};
_ ->
error
end
@ -139,16 +140,16 @@ handle_warehouse(BatchId, Type, Number) ->
NewWeight = dgiot_utils:to_float(Number),
Num = case Type of
<<"picking">> ->
NewWeight;
<<"retriving">> ->
-NewWeight;
<<"retriving">> ->
NewWeight;
_ ->
0
end,
io:format("~s ~p BatchId = ~p ~n", [?FILE, ?LINE, BatchId]),
case dgiot_parse:get_object(<<"Device">>, BatchId) of
{ok, #{<<"content">> := #{<<"FQty">> := FQty} = Res}} ->
Remaining = maps:get(<<"Remaining">>, Res, 0),
Remaining = maps:get(<<"Remaining">>, Res, FQty),
NewRemaining = case Remaining + Num > FQty of
true ->
FQty;

View File

@ -21,6 +21,7 @@
-export([get_usertree/2, getalluser/1, clear_cache/2]).
-export([get_zero_list/1, get_zero_binary/1]).
-export([fix_model/1, get_worker/1, get_children/1, check_workteam/1]).
-export([get_sum/1]).
fix_model(ID) ->
{ok, #{<<"thing">> := Model}} = dgiot_parse:get_object(<<"Product">>, ID),
@ -268,3 +269,15 @@ clear_cache(BatchProduct, BatchDeviceId) ->
fun(Type, _) ->
dgiot_data:delete(?FACTORY_ORDER, {BatchProduct, BatchDeviceId, Type})
end, [], DeviceTypeList).
get_sum(BatchList) when is_list(BatchList)->
lists:foldl(
fun
(#{<<"PickNum">> := PickNum},Acc)->
Acc + dgiot_utils:to_int(PickNum);
(_,Acc)->
Acc
end,0,BatchList);
get_sum(_) ->
0.

View File

@ -389,3 +389,45 @@ format_worker(Worker) ->
Worker.
%%record_worker_info(BatchProductId, BatchDeviceId, #{<<"quality">> := #{<<"type">> := Type}} = Payload, ChannelId, WorkerList, WorkTime, Num) ->
%% Quality = maps:get(<<"quality">>, maps:get(<<"quality">>, Payload, #{}), <<"合格"/utf8>>),
%% TypeData = case dgiot_data:get(?FACTORY_ORDER, {BatchProductId, BatchDeviceId, Type}) of
%% not_find ->
%% #{};
%% R ->
%% R
%% end,
%% OrderId = maps:get(<<"ordername">>, maps:get(<<"person">>, TypeData, #{}), <<"null">>),
%% _WorkShop = maps:get(<<"workshop">>, maps:get(Type, TypeData, #{}), <<"null">>),
%% Spec = maps:get(<<"spec">>, maps:get(Type, TypeData, #{}), <<"">>),
%% RollNum = maps:get(<<"rollnum">>, maps:get(<<"person">>, TypeData, #{}), <<"null">>),
%% lists:foldl(
%% fun(Worker, _) ->
%% case dgiot_data:get({ChannelId, worker}) of
%% not_find ->
%% pass;
%% ProductId ->
%% WorkerData = case dgiot_data:get(?WORKER, Worker) of
%% not_find ->
%% #{};
%% N ->
%% lists:nth(1, N)
%% end,
%% ManufacData = #{
%%%% <<"manufac_type">> => dgiot_utils:to_binary(Type),
%% <<"manufac_quality">> => Quality,
%% <<"manufac_orderid">> => OrderId,
%% <<"manufac_spec">> => Spec,
%% <<"manufac_num">> => Num,
%% <<"manufac_worktime">> => WorkTime,
%% <<"manufac_batchid">> => BatchDeviceId,
%% <<"manufac_rollnum">> => RollNum,
%% <<"manufac_type">> => dgiot_utils:to_binary(Type),
%% <<"base_source">> => <<"质检数据"/utf8>>
%% },
%% NumData = dgiot_product_enum:turn_num(maps:merge(WorkerData, ManufacData), ProductId),
%% dgiot_task:save_td(ProductId, Worker, NumData, #{})
%% end
%%
%% end, [], WorkerList);

View File

@ -0,0 +1,241 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2020-2021 DGIOT Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
-module(dgiot_material_channel).
-behavior(dgiot_channelx).
-author("kenneth").
-include_lib("dgiot/include/dgiot_socket.hrl").
-include_lib("dgiot/include/logger.hrl").
-include_lib("dgiot_bridge/include/dgiot_bridge.hrl").
-include("dgiot_factory.hrl").
-define(TYPE, <<"MATERIAL">>).
-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([handle_material_apply/2]).
-export([handle_apply_form/2]).
%%
-channel_type(#{
cType => ?TYPE,
type => ?PROTOCOL_CHL,
priority => 2,
title => #{
zh => <<"物料申请通道"/utf8>>
},
description => #{
zh => <<"物料申请通道"/utf8>>
}
}).
%%
-params(#{
<<"ico">> => #{
order => 102,
type => string,
required => false,
default => <<"/dgiot_file/shuwa_tech/zh/product/dgiot/channel/factory.png">>,
title => #{
en => <<"channel ICO">>,
zh => <<"通道ICO"/utf8>>
},
description => #{
en => <<"channel ICO">>,
zh => <<"通道ICO"/utf8>>
}
}
}).
start(ChannelId, ChannelArgs) ->
dgiot_channelx:add(?TYPE, ChannelId, ?MODULE, ChannelArgs).
%%
init(?TYPE, ChannelId, #{<<"product">> := _Products} = Args) ->
State = #state{
id = ChannelId,
env = Args
},
dgiot_parse_hook:subscribe(<<"Device/*">>, put, ChannelId, [<<"basedata">>]),
dgiot_parse_hook:subscribe(<<"Device/*">>, post, ChannelId),
{ok, State, []}.
handle_init(State) ->
{ok, State}.
%% ,
handle_event(_EventId, Event, State) ->
?LOG(info, "Channel ~p", [Event]),
{ok, State}.
%%{sync_parse, self(), Type, Method, Token, Class, Data}
handle_message({sync_parse, _Pid, 'before', put, _Token, <<"Device">>, #{<<"id">> := DeviceId} = QueryData}, State) ->
io:format("~s ~p DeviceId = ~p ~n", [?FILE, ?LINE, DeviceId]),
NewQueryData =
case dgiot_device:lookup(DeviceId) of
{ok, #{<<"productid">> := ProductId}} ->
case catch dgiot_hook:run_hook({sync_parse, before, put, ProductId}, {QueryData, ProductId, State}) of
{ok, [Res]} ->
%% io:format("~s ~p Res = ~ts.~n", [?FILE, ?LINE, unicode:characters_to_list(jsx:encode(Res))]),
Res;
_ ->
QueryData
end;
_->
pass
end,
dgiot_parse_hook:publish(_Pid, NewQueryData),
{ok, State};
%%handle_message({sync_parse, _Pid, 'before', put, _Token, <<"Device">>, #{<<"basedata">> := _BaseData,
%% <<"objectId">> := DeviceId}},
%% State) ->
%% io:format("~s ~p DeviceId = ~p ~n", [?FILE, ?LINE, DeviceId]),
%% handle_apply_form(#{a => a}, DeviceId),
%% {ok, State};
%%handle_message({sync_parse, _Pid, 'after', put, _Token, <<"Device">>, _QueryData},
%% State) ->
%%%% io:format("~s ~p Message = ~p ~n", [?FILE, ?LINE, Message]),
%% {ok, State};
handle_message({_, _Pid, _, _, _Token, <<"Device">>, _QueryData} = Message, State) ->
%% io:format("~s ~p Message = ~p ~n", [?FILE, ?LINE, Message]),
%% io:format("~s ~p QueryData= ~ts ~n", [?FILE, ?LINE, unicode:characters_to_list(jiffy:encode(_QueryData))]),
?LOG(debug, "channel ~p", [Message]),
{ok, State}.
stop(ChannelType, ChannelId, _State) ->
?LOG(warning, "Channel[~p,~p] stop", [ChannelType, ChannelId]),
ok.
handle_apply_form(#{<<"Status">> := 21, <<"SumPick">> := -1, <<"PickList">> := BatchList} = BaseData, DeviceId) when is_list(BatchList) ->
{NewList, Sum} = lists:foldl(
fun
(#{<<"PickNum">> := PickNum, <<"objectId">> := MaterialId} = Batch, {List, Sum}) ->
case dgiot_parse:get_object(<<"Device">>, MaterialId) of
{ok, #{<<"content">> := #{<<"FQty">> := FQty} = MaterialContent}} ->
UnConfirm = maps:get(<<"UnConform">>, MaterialContent, 0),
NewUnConfirm = UnConfirm + PickNum,
Remaining = maps:get(<<"Remaining">>, MaterialContent, FQty),
NewRemaining = Remaining - dgiot_utils:to_int(PickNum),
NewContent = MaterialContent#{<<"Remaining">> => NewRemaining, <<"unConform">> => NewUnConfirm},
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"content">> => NewContent}),
{List ++ [Batch], Sum + PickNum};
_ ->
io:format("~s ~p MaterialId = ~p ~n", [?FILE, ?LINE, MaterialId]),
{List, Sum}
end;
(_, Acc) ->
Acc
end, {[], 0}, BatchList),
NewBaseData = dgiot_map:merge(BaseData, #{<<"BatchList">> => NewList, <<"SumPick">> => Sum}),
io:format("~s ~p SumPick = ~p ~n", [?FILE, ?LINE, Sum]),
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"basedata">> => NewBaseData});
%%handle_apply_form(#{<<"Status">> := 0, <<"PickList">> := BatchList} = BaseData, DeviceId) ->
%% lists:foldl(
%% fun
%% (#{<<"PickNum">> := PickNum, <<"objectId">> := MaterialId} = Batch, {List, Sum}) ->
%% case dgiot_parse:get_object(<<"Device">>, MaterialId) of
%% {ok, #{<<"content">> := #{<<"FQty">> := FQty} = MaterialContent}} ->
%% UnConfirm = maps:get(<<"UnConform">>, MaterialContent, 0),
%% NewUnConfirm = UnConfirm - PickNum,
%% Remaining = maps:get(<<"Remaining">>, MaterialContent, FQty),
%% NewRemaining = Remaining - dgiot_utils:to_int(PickNum),
%% NewContent = MaterialContent#{<<"Remaining">> => NewRemaining, <<"unConform">> => NewUnConfirm},
%% dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"content">> => NewContent}),
%% {List ++ [Batch], Sum + PickNum};
%% _ ->
%% io:format("~s ~p MaterialId = ~p ~n", [?FILE, ?LINE, MaterialId]),
%% {List, Sum}
%% end;
%% (_, Acc) ->
%% Acc
%% end, {[], 0}, BatchList);
handle_apply_form(_, _) ->
pass.
handle_material_apply(DeviceId, #{<<"Status">> := <<"21">>, <<"PickList">> := BatchList} = BaseData) when is_list(BatchList) ->
{NewList, Sum} = lists:foldl(
fun
(#{<<"PickNum">> := PickNum, <<"objectId">> := MaterialId} = Batch, {List, Sum}) ->
case dgiot_parse:get_object(<<"Device">>, MaterialId) of
{ok, #{<<"content">> := #{<<"FQty">> := FQty} = MaterialContent}} ->
io:format("~s ~p PickNum = ~p ~n", [?FILE, ?LINE, PickNum]),
UnConfirm = maps:get(<<"UnConform">>, MaterialContent, 0),
NewUnConfirm = UnConfirm + PickNum,
Remaining = maps:get(<<"Remaining">>, MaterialContent, FQty),
NewRemaining = Remaining - dgiot_utils:to_int(PickNum),
NewContent = MaterialContent#{<<"Remaining">> => NewRemaining, <<"unConform">> => NewUnConfirm},
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"content">> => NewContent}),
{List ++ [Batch], Sum + PickNum};
_ ->
io:format("~s ~p MaterialId = ~p ~n", [?FILE, ?LINE, MaterialId]),
{List, Sum}
end;
(_, Acc) ->
Acc
end, {[], 0}, BatchList),
NewBaseData = dgiot_map:merge(BaseData, #{<<"PickList">> => NewList, <<"SumPick">> => Sum}),
io:format("~s ~p SumPick = ~p ~n", [?FILE, ?LINE, Sum]),
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"basedata">> => NewBaseData});
handle_material_apply(DeviceId, #{<<"Status">> := <<"11">>, <<"PickList">> := BatchList} = BaseData) ->
lists:foldl(
fun
(#{<<"PickNum">> := PickNum, <<"objectId">> := MaterialId}, _) ->
case dgiot_parse:get_object(<<"Device">>, MaterialId) of
{ok, #{<<"content">> := #{<<"FQty">> := FQty} = MaterialContent}} ->
UnConfirm = maps:get(<<"UnConform">>, MaterialContent, 0),
NewUnConfirm = UnConfirm - PickNum,
Remaining = maps:get(<<"Remaining">>, MaterialContent, FQty),
NewRemaining = Remaining + dgiot_utils:to_int(PickNum),
NewContent = MaterialContent#{<<"Remaining">> => NewRemaining, <<"unConform">> => NewUnConfirm},
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"content">> => NewContent});
_ ->
pass
end;
(_, _) ->
pass
end, [], BatchList),
NewBaseData = dgiot_map:merge(BaseData, #{<<"BatchList">> => [], <<"SumPick">> => -1}),
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"basedata">> => NewBaseData});
handle_material_apply(DeviceId, #{<<"Status">> := 0, <<"PickList">> := BatchList, <<"SumPick">> := _SumPick} = BaseData) ->
lists:foldl(
fun
(#{<<"PickNum">> := PickNum, <<"objectId">> := MaterialId}, _) ->
case dgiot_parse:get_object(<<"Device">>, MaterialId) of
{ok, #{<<"content">> := MaterialContent}} ->
UnConfirm = maps:get(<<"UnConform">>, MaterialContent, 0),
NewUnConfirm = UnConfirm - PickNum,
NewContent = MaterialContent#{<<"unConform">> => NewUnConfirm},
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"content">> => NewContent});
_ ->
pass
end;
(_, _) ->
pass
end, [], BatchList),
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"basedata">> => BaseData}),
dgiot_hook:run_hook();
handle_material_apply(_, BaseData) ->
io:format("~s ~p BaseData = ~p ~n", [?FILE, ?LINE, BaseData]),
pass.

View File

@ -320,32 +320,10 @@ get_id(OperationID) ->
%% todo json的 merge修改
do_put(<<"put">>, Token, <<"/iotapi/classes/", Tail/binary>>, #{<<"id">> := Id} = Args) ->
[ClassName | _] = re:split(Tail, <<"/">>),
%% io:format("~s ~p put Id = ~p ~n", [?FILE, ?LINE, Id]),
notify('before', put, Token, ClassName, Id, Args),
%% io:format("~s ~p put Id = ~p ~n", [?FILE, ?LINE, Id]),
NewArgs = receive_put(Args),
case dgiot_parse:get_object(ClassName, Id) of
{ok, Class} ->
Keys = maps:keys(NewArgs),
dgiot_map:merge(maps:with([Keys], Class), maps:without([<<"id">>], NewArgs));
_ ->
maps:without([<<"id">>], NewArgs)
end;
do_put_(Id,Args,Token,Tail);
%% amis iotapi
do_put(<<"put">>, Token, <<"/iotapi/amis/", Tail/binary>>, #{<<"id">> := Id} = Args) ->
[ClassName | _] = re:split(Tail, <<"/">>),
notify('before', put, Token, ClassName, Id, Args),
case dgiot_parse:get_object(ClassName, Id) of
{ok, Class} ->
Keys = maps:keys(Args),
Res = dgiot_map:merge(maps:with(Keys, Class), maps:without([<<"id">>], Args)),
%% io:format("~s ~p put Res = ~p ~n", [?FILE, ?LINE, Res]),
Res;
_ ->
Args
end;
do_put_(Id,Args,Token,Tail);
do_put(_, _Token, _ClassName, Args) ->
%% io:format("~s ~p put Args = ~p ~n", [?FILE, ?LINE, Args]),
@ -355,13 +333,23 @@ do_put(_, _Token, _ClassName, Args) ->
receive_put(ResBody) ->
receive
{sync_parse, NewResBody} when is_map(NewResBody) ->
io:format("~s ~p ~p ~n", [?FILE, ?LINE, length(maps:to_list(NewResBody))]),
{ok, jsx:encode(NewResBody)};
{ok,NewResBody};
{sync_parse, NewResBody} ->
io:format("~s ~p ~p ~n", [?FILE, ?LINE, NewResBody]),
{ok, NewResBody};
{error} ->
{ok, ResBody}
after 500 -> %% 5
after 2000 -> %% 2
{ok, ResBody}
end.
do_put_(Id,Args,Token,Tail)->
[ClassName | _] = re:split(Tail, <<"/">>),
notify('before', put, Token, ClassName, Id, Args),
{ok,NewArgs} = receive_put(Args),
case dgiot_parse:get_object(ClassName, Id) of
{ok, Class} ->
Keys = maps:keys(NewArgs),
dgiot_map:merge(maps:with([Keys], Class), maps:without([<<"id">>], NewArgs));
_ ->
maps:without([<<"id">>], NewArgs)
end.