Merge pull request #624 from dgiot/guo

fix:fatory
This commit is contained in:
dawnwinterLiu 2022-09-22 17:27:37 +08:00 committed by GitHub
commit ff6b8a27ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 259 additions and 319 deletions

View File

@ -99,7 +99,6 @@ handle_message({sync_parse, _Pid, 'before', put, Token, <<"Device">>, #{<<"conte
case Content of
#{<<"person">> := #{<<"type">> := Type}} ->
FlatMap = dgiot_map:flatten(Content),
io:format("~s ~p DeviceId = ~p.~n", [?FILE, ?LINE, DeviceId]),
save_data(ProductId, DeviceId, Type, FlatMap#{<<"person_sessiontoken">> => Token, <<"person_deviceid">> => DeviceId}),
{ok, State};
_ ->
@ -133,7 +132,6 @@ save_data(ProductId, DeviceId, Type, Payload) ->
{ok, NewPayload} ->
Id = maps:get(?SHEETID(Type), NewPayload, get_id(DevAddr, Type)),
Use = turn_user(NewPayload),
io:format("~s ~p Id = ~p.~n", [?FILE, ?LINE, Id]),
dgiot_task:save_td(ProductId, DevAddr, NewPayload#{<<"person_sessiontoken">> => Use,?SHEETID(Type) => Id, <<"person_sheetsid">> => Id}, #{});
_ ->
{error, <<"run_hook_failed">>}

View File

@ -1,293 +0,0 @@
%%--------------------------------------------------------------------
%% 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_factory_data).
-author("jonhl").
-include_lib("dgiot/include/logger.hrl").
-define(PRE, <<"_">>).
-define(Database(Name), <<?PRE/binary, Name/binary>>).
-define(Table(Name), <<?PRE/binary, Name/binary>>).
-define(SHEETID(SHEET), <<SHEET/binary, "_id">>).
-define(PRODUCTID, <<"ec71804a3d">>).
-define(TYPE, <<"TD">>).
-export([handle_data/1, save_data/4,add_data/4]).
handle_data([ProductId, DeviceId, Type, FlatMap]) ->
NumData = dgiot_factory_utils:turn_num(FlatMap, ProductId, Type),
save_data(ProductId, DeviceId, Type, NumData).
save_data(ProductId, DeviceId, <<"product">> = Type, Payload) ->
case dgiot_data:lookup({ProductId, ?TYPE}) of
{ok, Channel} ->
case dgiot_device_cache:lookup(DeviceId) of
{ok, #{<<"devaddr">> := DevAddr}} ->
handle_product_condition(Channel, ProductId, DeviceId, DevAddr, Type, Payload);
_ ->
{error, not_find_toal_num}
end;
_ ->
{error, not_find_channel}
end;
save_data(ProductId, DeviceId, <<"semiproduct">> = Type, Payload) ->
%% case dgiot_parse:get_object(<<"Device">>, DeviceId) of
%% {ok, #{<<"devaddr">> := DevAddr}} ->
case dgiot_device_cache:lookup(DeviceId) of
{ok, #{<<"devaddr">> := DevAddr}} ->
Id = get_id(DevAddr, Type),
dgiot_task:save_td_no_match(ProductId, DevAddr, Payload#{?SHEETID(Type) => Id}, #{});
_ ->
{error, <<"not_fin_device">>}
end;
save_data(ProductId, DeviceId, <<"quality">> = Type, Payload) ->
case dgiot_device_cache:lookup(DeviceId) of
{ok, #{<<"devaddr">> := DevAddr}} ->
Id = get_id(DevAddr, Type),
dgiot_task:save_td_no_match(ProductId, DevAddr, Payload#{?SHEETID(Type) => Id}, #{}),
handle_quality(Payload, DeviceId);
_ ->
{error, <<"not_find_device">>}
end;
save_data(ProductId, DeviceId, <<"material">>, Payload) ->
case handle_material(ProductId, DeviceId, Payload) of
{ok, Res} ->
case dgiot_parse:get_object(<<"Device">>, DeviceId) of
{ok, #{<<"devaddr">> := DevAddr}} ->
dgiot_task:save_td_no_match(ProductId, DevAddr, Res, #{});
_ ->
{error, <<"not_find_device">>}
end;
_ ->
error
end;
save_data(ProductId, DeviceId, Type, Payload) ->
case dgiot_parse:get_object(<<"Device">>, DeviceId) of
{ok, #{<<"devaddr">> := DevAddr}} ->
Id = get_id(DevAddr, Type),
dgiot_task:save_td_no_match(ProductId, DevAddr, Payload#{?SHEETID(Type) => Id}, #{});
_ ->
{error, <<"not_fin_device">>}
end.
handle_product_condition(_Channel, ProductId, DeviceId, DevAddr, Type, #{<<"product_pnumber">> := Pnumber, <<"product_subtime">> := SubTime, <<"product_condition">> := 1} = Payload) ->
case dgiot_parse:get_object(<<"Device">>, DeviceId) of
{ok, #{<<"startTime">> := LastSubTime, <<"progress">> := Progress, <<"content">> := #{<<"baseInfo">> := #{<<"Number">> := Total}}}} ->
WorkTime = (SubTime - LastSubTime) div 1000,
Id = get_id(DevAddr, Type),
UpId = string:to_upper(dgiot_utils:to_list(Id)),
dgiot_task:save_td_no_match(ProductId, DevAddr, Payload#{<<"product_mhour">> => abs(WorkTime), <<"product_id">> => UpId}, #{}),
save_progress(DeviceId, Progress, Total, Pnumber, SubTime);
_ ->
error
end;
handle_product_condition(_Channel, ProductId, DeviceId, DevAddr, _Type, #{<<"product_condition">> := 2, <<"product_id">> := Product_id, <<"product_pnumber">> := Pnumber} = Payload) ->
dgiot_task:save_td_no_match(ProductId, DevAddr, Payload#{<<"product_id">> => dgiot_utils:to_list(Product_id)}, #{}),
handle_storehouse(2, Pnumber, DeviceId);
handle_product_condition(_Channel, ProductId, DeviceId, DevAddr, _Type, #{<<"product_condition">> := 3, <<"product_id">> := Product_id, <<"product_pnumber">> := Pnumber} = Payload) ->
dgiot_task:save_td_no_match(ProductId, DevAddr, Payload#{<<"product_id">> => dgiot_utils:to_list(Product_id)}, #{}),
handle_storehouse(3, Pnumber, DeviceId);
%% handle_dingdan(DeviceId),
%% dgiot_jienuo_meter:test(Payload, DeviceId);
handle_product_condition(_, _, _, _, _, _) ->
error.
handle_material(_, DeviceId, #{<<"material_type">> := <<"picking">>, <<"material_spec">> := MaterialName, <<"material_date">> := Date} = Payload) ->
Id = get_id(DeviceId, [<<"material">>, MaterialName, Date]),
{ok, Payload#{<<"material_id">> => Id}};
handle_material(ProductId, DeviceId, #{<<"material_type">> := <<"retriving">>, <<"material_spec">> := MaterialName, <<"material_date">> := Date, <<"material_loss">> := Loss} = Payload) ->
case maps:find(<<"persion_sessiontoken">>, Payload) of
{ok, Token} ->
case dgiot_product_tdengine:get_channel(Token) of
{ok, Channel} ->
case dgiot_factory_getdata:get_work_sheet(ProductId, <<"material">>,undefined ,undefined ,Channel, DeviceId, #{<<"material_spec">> => MaterialName, <<"material_date">> => Date}, 1, 0, <<"true">>) of
{ok, {_, Res}} ->
case length(Res) of
1 ->
F = lists:nth(1, Res),
{ok, F#{<<"material_loss">> => Loss}};
_ ->
error
end;
_ ->
error
end;
_ ->
error
end;
_ ->
error
end;
handle_material(_, _, _) ->
error.
%%handle_dingdan(DeviceId) ->
%% case dgiot_parse:get_object(<<"Device">>, DeviceId) of
%% {ok, #{<<"progress">> := Progress, <<"detail">> := Detail, <<"content">> := #{<<"baseInfo">> := #{<<"Number">> := Total}}}} ->
%% case Progress >= Total of
%% true ->
%% EndTime = dgiot_datetime:format("YYYY-MM-DD HH:NN:SS"),
%% dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"realstate">> => 8, <<"detail">> => Detail#{<<"taskend">> => EndTime}});
%% _ ->
%% pass
%% end
%% end.
handle_storehouse(2, Pnumber, DeviceId) ->
case dgiot_parse:get_object(<<"Device">>, DeviceId) of
{ok, #{<<"storehouse">> := #{<<"unstored">> := Unstored, <<"stored">> := Stored}}} ->
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"storehouse">> => #{<<"unstored">> => Unstored + Pnumber, <<"stored">> => Stored}});
_ ->
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"storehouse">> => #{<<"unstored">> => Pnumber, <<"stored">> => 0}})
end;
handle_storehouse(3, Pnumber, DeviceId) ->
case dgiot_parse:get_object(<<"Device">>, DeviceId) of
{ok, #{<<"storehouse">> := #{<<"unstored">> := Unstored, <<"stored">> := Stored}}} ->
case Unstored - Pnumber < 0 of
true ->
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"storehouse">> => #{<<"unstored">> => 0, <<"stored">> => Stored + Pnumber}});
_ ->
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"storehouse">> => #{<<"unstored">> => Unstored - Pnumber, <<"stored">> => Stored + Pnumber}})
end;
_ ->
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"storehouse">> => #{<<"unstored">> => 0, <<"stored">> => 1}})
end;
handle_storehouse(_, _, _) ->
pass.
get_id(DevAddr, Type) ->
Time = dgiot_utils:to_binary(dgiot_datetime:timestamp()),
Bin = dgiot_utils:to_binary(Type),
<<ObjID:10/binary, _/binary>> = dgiot_utils:to_md5(<<Bin/binary, DevAddr/binary, Time/binary>>),
Res = string:to_upper(dgiot_utils:to_list(ObjID)),
dgiot_utils:to_binary(Res).
handle_quality(#{<<"quality_people">> := Operator, <<"quality_status">> := Status, <<"quality_quality">> := Quality, <<"quality_alarmid">> := NotificationId,
<<"quality_pnumber">> := Pnumber}, DeviceId) ->
case {Status, Quality} of
{3, 1} ->
handle_alert(NotificationId, Operator, Quality),
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"realstatus">> => 4});
{3, 0} ->
handle_alert(NotificationId, Operator, Quality),
handle_progress(DeviceId, -Pnumber),
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"realstatus">> => 2});
{5, 1} ->
handle_alert(NotificationId, Operator, Quality),
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"realstatus">> => 6});
{5, 0} ->
handle_alert(NotificationId, Operator, Quality),
handle_progress(DeviceId, -Pnumber),
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"realstatus">> => 2});
{6, 1} ->
handle_alert(NotificationId, Operator, Quality);
_ ->
{ok, ok}
end;
handle_quality(_, _) ->
error.
handle_progress(DeviceId, Pnumber) ->
case dgiot_parse:get_object(<<"Device">>, DeviceId) of
{ok, #{<<"progress">> := Progress}} ->
NewProgress = case Progress + Pnumber < 0 of
true ->
0;
false ->
Progress + Pnumber
end,
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"progress">> => NewProgress});
_ ->
pass
end.
handle_alert(NotificationId, Operator, 1) ->
case dgiot_parse:get_object(<<"Notification">>, NotificationId) of
{ok, #{<<"content">> := #{<<"alarm">> := Alarm}}} ->
dgiot_parse:update_object(<<"Notification">>, NotificationId, #{<<"status">> => 1, <<"content">> => #{<<"alarm">> => Alarm#{<<"operator">> => Operator}, <<"alertstatus">> => 2}});
_ ->
pass
end;
handle_alert(NotificationId, Operator, 0) ->
case dgiot_parse:get_object(<<"Notification">>, NotificationId) of
{ok, #{<<"content">> := #{<<"alarm">> := Alarm}}} ->
dgiot_parse:update_object(<<"Notification">>, NotificationId, #{<<"status">> => 1, <<"content">> => #{<<"alarm">> => Alarm#{<<"operator">> => Operator}}});
_ ->
pass
end.
save_progress(DeviceId, Progress, Total, Pnumber, SubTime) ->
NewProgress = case Progress + Pnumber > Total of
true ->
Total;
false ->
Progress + Pnumber
end,
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"progress">> => NewProgress, <<"startTime">> => SubTime}).
add_data(Channel, ProductId, DevAddr, Data) ->
case dgiot_parse:get_object(<<"Product">>, ProductId) of
{ok, #{<<"thing">> := Properties}} ->
case dgiot_tdengine:format_data(ProductId, DevAddr, Properties, Data) of
#{<<"db">> := DB, <<"tableName">> := Table, <<"using">> := Using, <<"tags">> := Tags, <<"fields">> := _Fields, <<"values">> := Values} ->
case dgiot_channelx:call(?TYPE, Channel, config) of
{ok, Context} ->
Tag = dgiot_utils:to_binary(Tags),
Sql = <<"INSERT INTO ", DB/binary, ".", Table/binary, " using ", Using/binary, ".", Using/binary, " TAGS (", Tag/binary, ") VALUES ", Values/binary, ";">>,
%% dgiot_tdengine_pool:run_sql(Context#{<<"channel">> => Channel}, execute_query, Sql);
Res = dgiot_tdengine_pool:run_sql(Context#{<<"channel">> => Channel}, execute_query, Sql),
Res;
_ ->
error
end;
_ ->
error
end;
_ ->
error
end.

View File

@ -17,7 +17,7 @@
%% API
-export([get_work_sheet/10]).
-export([get_ThingMap/2, thinglist2binary/1, get_history/9, get_device_list/1, get_example/2, filter_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([get_all_sheet/9]).
get_all_sheet(ProductId, Start, End, Channel, DeviceId, Where, Limit, Skip, New) ->
@ -114,7 +114,7 @@ get_work_sheet(ProductId, Type, Start, End, Channel, DeviceId, Where, Limit, Ski
{Total, Res} = filter_data(Limit, Skip, HistoryData),
MergeData = merge_data(ParseData, Res, DeviceId, ThingMap),
NamedData = dgiot_factory_utils:turn_name(MergeData, ThingMap),
{ok, {Total, NamedData}};
{ok, {Total, filter_data(NamedData)}};
_ ->
error
end;
@ -366,6 +366,13 @@ filter_data(Limit, Skip, HistoryData) ->
end,
{Total, Res}.
filter_data(Data) when is_list(Data) ->
lists:foldl(
fun(X, Acc) ->
Filtered = maps:filter(fun(_, V) -> (V /= <<"null">>) and (V/= null) end, X),
Acc ++ [Filtered]
end, [], Data).
get_device_list(ProductId) ->
case dgiot_parse:query_object(<<"Device">>, #{<<"where">> => #{<<"product">> => ProductId}}) of
{ok, #{<<"results">> := Results}} ->

View File

@ -131,7 +131,20 @@ do_request(post_worker_shift, #{<<"shift">> := Shifts} = _Args, _Context, _Body)
do_request(get_data, #{<<"productId">> := undefined} = _Args, _Context, _Body) ->
do_request(get_data, _Args#{<<"productId">> => <<"ec71804a3d">>}, _Context, _Body);
do_request(get_data, #{<<"productId">> := ProductId, <<"objectId">> := undefined, <<"type">> := Type, <<"starttime">> := Start, <<"endtime">> := End, <<"where">> := Where, <<"limit">> := Limit, <<"skip">> := Skip, <<"new">> := New} = _Args, #{<<"sessionToken">> := SessionToken} = _Context, _Body) ->
do_request(get_data, #{<<"productId">> := ProductId, <<"objectId">> := undefined, <<"type">> := Type,
<<"starttime">> := Start, <<"endtime">> := End, <<"where">> := OldWhere,
<<"limit">> := Limit, <<"skip">> := Skip, <<"new">> := New} = _Args, #{<<"sessionToken">> := SessionToken} = _Context, _Body) ->
Where = case is_map(OldWhere) of
true ->
OldWhere;
_ ->
case jsx:is_json(OldWhere) of
true ->
dgiot_bamis:format_multilayer(OldWhere);
_ ->
OldWhere
end
end,
case dgiot_product_tdengine:get_channel(SessionToken) of
{error, Error} -> {error, Error};
{ok, Channel} ->
@ -151,8 +164,20 @@ do_request(get_data, #{<<"productId">> := ProductId, <<"objectId">> := undefined
end;
do_request(get_data, #{<<"productId">> := ProductId, <<"objectId">> := DeviceId, <<"type">> := Type, <<"starttime">> := Start, <<"endtime">> := End, <<"where">> := Where, <<"limit">> := Limit, <<"skip">> := Skip, <<"new">> := New} = _Args,
do_request(get_data, #{<<"productId">> := ProductId, <<"objectId">> := DeviceId, <<"type">> := Type, <<"starttime">> := Start,
<<"endtime">> := End, <<"where">> := OldWhere, <<"limit">> := Limit, <<"skip">> := Skip, <<"new">> := New} = _Args,
#{<<"sessionToken">> := SessionToken} = _Context, _Body) ->
Where = case is_map(OldWhere) of
true ->
OldWhere;
_ ->
case jsx:is_json(OldWhere) of
true ->
dgiot_bamis:format_multilayer(OldWhere);
_ ->
OldWhere
end
end,
case dgiot_product_tdengine:get_channel(SessionToken) of
{error, Error} -> {error, Error};
{ok, Channel} ->

View File

@ -21,7 +21,6 @@
-export([get_material_record/2, post_material/2]).
-export([get_warehouse_material/3, put_warehouse_material/1]).
get_material_record(DeviceId, Depart) ->
io:format("~s ~p Depart= ~p ~n", [?FILE, ?LINE, Depart]),
case dgiot_parse:get_object(<<"Device">>, DeviceId) of
{ok, #{<<"material">> := Material}} ->
DepartMaterial = get_depart_material(Material, Depart),
@ -65,6 +64,7 @@ get_usable_material(Material) ->
post_material(DeviceId, Data) when is_list(Data) ->
io:format("~s ~p here ~n", [?FILE, ?LINE]),
case get_material_record(DeviceId, undefined) of
{ok, Material} ->
Res = lists:foldl(
@ -81,25 +81,22 @@ post_material(DeviceId, Data) when is_list(Data) ->
error
end;
post_material(DeviceId, Data) when is_map(Data) ->
io:format("~s ~p here ~n", [?FILE, ?LINE]),
case get_material_record(DeviceId, undefined) of
{ok, Material} ->
Res = hanlde_pickandretrive(Data, Material),
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"material">> => Res});
case hanlde_pickandretrive(Data, Material) of
{ok, NewMaterial} ->
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"material">> => NewMaterial})
end;
_ ->
error
end;
post_material(_, _) ->
error.
hanlde_pickandretrive(#{<<"Subitem_BOM_number">> := Name, <<"material_date">> := Date,
<<"material_people">> := People, <<"material_type">> := Type,
<<"material_number">> := Num, <<"material_batchid">> := BatchId, <<"material_weight">> := Weight,
<<"material_batch">> := #{<<"extra">> := #{<<"objectId">> := Id}}} = Data, Material) when is_list(Data) ->
<<"material_batch">> := #{<<"extra">> := #{<<"objectId">> := Id}}}, Material) ->
case maps:find(Name, Material) of
{ok, Res} ->
case Res of
@ -128,7 +125,7 @@ hanlde_pickandretrive(#{<<"Subitem_BOM_number">> := Name, <<"material_date">> :=
hanlde_pickandretrive(#{<<"Subitem_BOM_number">> := Name, <<"material_date">> := Date,
<<"material_people">> := People, <<"material_type">> := Type,
<<"material_number">> := Num, <<"material_batchid">> := BatchId, <<"material_weight">> := Weight,
<<"objectId">> := Id} = Data, Material) when is_map(Data) ->
<<"objectId">> := Id}, Material) ->
case maps:find(Name, Material) of
{ok, Res} ->
case Res of
@ -137,13 +134,15 @@ hanlde_pickandretrive(#{<<"Subitem_BOM_number">> := Name, <<"material_date">> :=
<<"picking">> ->
After = dgiot_utils:to_float(Picked) + dgiot_utils:to_float(Weight),
handle_warehouse(Id, Type, Num),
{ok, Name, maps:merge(Res, #{<<"material_picked">> => After, <<"material_pick">> => Pick ++ [#{<<"material_date">> => Date, <<"material_people">> => People, <<"material_number">> => Num,
<<"material_batchid">> => BatchId, <<"material_weight">> => Weight}]})};
NewRes = maps:merge(Res, #{<<"material_picked">> => After, <<"material_pick">> => Pick ++ [#{<<"material_date">> => Date, <<"material_people">> => People, <<"material_number">> => Num,
<<"material_batchid">> => BatchId, <<"material_weight">> => Weight}]}),
{ok, maps:merge(Material, #{Name => NewRes})};
<<"retriving">> ->
After = dgiot_utils:to_float(Picked) - dgiot_utils:to_float(Weight),
handle_warehouse(Id, Type, Num),
{ok, Name, maps:merge(Res, #{<<"material_picked">> => After, <<"material_retrive">> => Retrive ++ [#{<<"material_date">> => Date, <<"material_people">> => People, <<"material_number">> => Num,
<<"material_batchid">> => BatchId, <<"material_weight">> => Weight}]})};
NewRes = maps:merge(Res, #{<<"material_picked">> => After, <<"material_retrive">> => Retrive ++ [#{<<"material_date">> => Date, <<"material_people">> => People, <<"material_number">> => Num,
<<"material_batchid">> => BatchId, <<"material_weight">> => Weight}]}),
{ok, maps:merge(Material, #{Name => NewRes})};
_ ->
error
end;
@ -154,7 +153,7 @@ hanlde_pickandretrive(#{<<"Subitem_BOM_number">> := Name, <<"material_date">> :=
error
end;
hanlde_pickandretrive(_, _) ->
io:format("~s ~p hanlde_pickandretrive ~n", [?FILE, ?LINE]),
%% io:format("~s ~p R = ~p ~n", [?FILE, ?LINE,R]),
error.

View File

@ -22,7 +22,6 @@
-export([post_one_shift/1, get_shift_time/1]).
-export([ bits_to_workerlist/1, workerlist_to_bits/1]).
-define(DAY, 86400).
-define(SHIFT, [<<"白班"/utf8>>, <<"晚班"/utf8>>]).
-define(WORKERCALENDAR, <<"WorkerCanlendar">>).
-define(INTERVAL, 604800).
-define(ONEDAY, 86400).

View File

@ -284,3 +284,4 @@ get_zero_binary(Acc, Num) ->
_ ->
Acc
end.

View File

@ -0,0 +1,194 @@
%%--------------------------------------------------------------------
%% 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.
%%--------------------------------------------------------------------
%% https://doc.oschina.net/grpc
%% https://www.grpc.io/docs/
-module(dgiot_factory_worker).
-compile(export_all).
-compile(nowarn_export_all).
-include_lib("dgiot/include/logger.hrl").
-define(TYPE, ?FACTORY_WOKER).
%%
-params(#{
<<"shift">> => #{
order => 1,
type => string,
required => true,
default => #{<<"value">> => <<"moring">>, <<"label">> => <<"白班"/utf8>>},
enum => [
#{<<"value">> => <<"moring">>, <<"label">> => <<"白班"/utf8>>},
#{<<"value">> => <<"afernoon">>, <<"label">> => <<"晚班"/utf8>>}
],
title => #{
zh => <<"班次"/utf8>>
},
description => #{
zh => <<"班次"/utf8>>
}
},
<<"workteam">> => #{
order => 2,
type => string,
required => true,
default => <<"0000"/utf8>>,
enum => [
#{<<"value">> => <<"A">>, <<"label">> => <<"A班"/utf8>>},
#{<<"value">> => <<"B">>, <<"label">> => <<"B班"/utf8>>},
#{<<"value">> => <<"C">>, <<"label">> => <<"C班"/utf8>>}
],
title => #{
zh => <<"班组"/utf8>>
},
description => #{
zh => <<"班组"/utf8>>
}
},
<<"operatetype">> => #{
order => 3,
type => string,
required => true,
default => #{<<"value">> => <<"shift">>, <<"label">> => <<"记录排班"/utf8>>},
enum => [#{<<"value">> => <<"shift">>, <<"label">> => <<"记录排班"/utf8>>},
#{<<"value">> => <<"pay">>, <<"label">> => <<"计算工资"/utf8>>}
],
title => #{
zh => <<"操作类型"/utf8>>
},
description => #{
zh => <<"操作类型"/utf8>>
}
},
<<"address">> => #{
order => 4,
type => string,
required => true,
default => <<"0X00"/utf8>>,
title => #{
zh => <<"寄存器地址"/utf8>>
},
description => #{
zh => <<"寄存器地址:原数据地址(16进制加0X,例如:0X10,否在是10进制);8位寄存器,一个字节;16位寄存器,两个字节;32位寄存器,四个字节"/utf8>>
}
},
<<"registersnumber">> => #{
order => 5,
type => string,
required => true,
default => <<"1">>,
title => #{
zh => <<"寄存器个数"/utf8>>
},
description => #{
zh => <<"寄存器个数(多个寄存器个数)"/utf8>>
}
}
}).
start(Server) ->
Services = #{protos => [dgiot_dlink_pb],
services => #{'dgiot.Dlink' => dgiot_dlink_server}
},
{ok, _} = grpc:start_server(Server, 30051, Services, []).
stop(Server) ->
_ = grpc:stop_server(Server).
login(ClinetId) ->
SvrAddr = "http://127.0.0.1:30051",
{ok, _} = grpc_client_sup:create_channel_pool(ClinetId, SvrAddr, #{}).
logout(ClinetId) ->
_ = grpc_client_sup:stop_channel_pool(ClinetId).
send(ClinetId) ->
dgiot_dlink_client:say_hello(#{name => <<"Xiao Ming">>}, #{channel => ClinetId}).
test() ->
FileName = <<"dlink">>,
Proto = <<"Ly8gY29weSBmcm9tOiBodHRwczovL2dycGMuaW8vZG9jcy93aGF0LWlzLWdycGMvaW50cm9kdWN0aW9uLwoKCnN5bnRheCA9ICJwcm90bzMiOwoKb3B0aW9uIGphdmFfbXVsdGlwbGVfZmlsZXMgPSB0cnVlOwpvcHRpb24gamF2YV9wYWNrYWdlID0gImlvLmdycGMuZXhhbXBsZXMuZGxpbmsiOwpvcHRpb24gamF2YV9vdXRlcl9jbGFzc25hbWUgPSAiRGxpbmtQcm90byI7Cm9wdGlvbiBvYmpjX2NsYXNzX3ByZWZpeCA9ICJkbGluayI7CgpwYWNrYWdlIGRnaW90OwoKLy8gVGhlIGRsaW5rIHNlcnZpY2UgZGVmaW5pdGlvbi4Kc2VydmljZSBEbGluayB7CiAgLy8gU2VuZHMgYSBncmVldGluZwogIHJwYyBTYXlIZWxsbyAoSGVsbG9SZXF1ZXN0KSByZXR1cm5zIChIZWxsb1JlcGx5KSB7fQoKICAvLyBJZiB0aGUgcmVxdWVzdGVkIHNlcnZpY2UgaXMgdW5rbm93biwgdGhlIGNhbGwgd2lsbCBmYWlsIHdpdGggc3RhdHVzCiAgLy8gTk9UX0ZPVU5ELgogIHJwYyBDaGVjayhIZWFsdGhDaGVja1JlcXVlc3QpIHJldHVybnMgKEhlYWx0aENoZWNrUmVzcG9uc2UpOwoKICAvLyBQZXJmb3JtcyBhIHdhdGNoIGZvciB0aGUgc2VydmluZyBzdGF0dXMgb2YgdGhlIHJlcXVlc3RlZCBzZXJ2aWNlLgogIC8vIFRoZSBzZXJ2ZXIgd2lsbCBpbW1lZGlhdGVseSBzZW5kIGJhY2sgYSBtZXNzYWdlIGluZGljYXRpbmcgdGhlIGN1cnJlbnQKICAvLyBzZXJ2aW5nIHN0YXR1cy4gIEl0IHdpbGwgdGhlbiBzdWJzZXF1ZW50bHkgc2VuZCBhIG5ldyBtZXNzYWdlIHdoZW5ldmVyCiAgLy8gdGhlIHNlcnZpY2UncyBzZXJ2aW5nIHN0YXR1cyBjaGFuZ2VzLgogIC8vCiAgLy8gSWYgdGhlIHJlcXVlc3RlZCBzZXJ2aWNlIGlzIHVua25vd24gd2hlbiB0aGUgY2FsbCBpcyByZWNlaXZlZCwgdGhlCiAgLy8gc2VydmVyIHdpbGwgc2VuZCBhIG1lc3NhZ2Ugc2V0dGluZyB0aGUgc2VydmluZyBzdGF0dXMgdG8KICAvLyBTRVJWSUNFX1VOS05PV04gYnV0IHdpbGwgKm5vdCogdGVybWluYXRlIHRoZSBjYWxsLiAgSWYgYXQgc29tZQogIC8vIGZ1dHVyZSBwb2ludCwgdGhlIHNlcnZpbmcgc3RhdHVzIG9mIHRoZSBzZXJ2aWNlIGJlY29tZXMga25vd24sIHRoZQogIC8vIHNlcnZlciB3aWxsIHNlbmQgYSBuZXcgbWVzc2FnZSB3aXRoIHRoZSBzZXJ2aWNlJ3Mgc2VydmluZyBzdGF0dXMuCiAgLy8KICAvLyBJZiB0aGUgY2FsbCB0ZXJtaW5hdGVzIHdpdGggc3RhdHVzIFVOSU1QTEVNRU5URUQsIHRoZW4gY2xpZW50cwogIC8vIHNob3VsZCBhc3N1bWUgdGhpcyBtZXRob2QgaXMgbm90IHN1cHBvcnRlZCBhbmQgc2hvdWxkIG5vdCByZXRyeSB0aGUKICAvLyBjYWxsLiAgSWYgdGhlIGNhbGwgdGVybWluYXRlcyB3aXRoIGFueSBvdGhlciBzdGF0dXMgKGluY2x1ZGluZyBPSyksCiAgLy8gY2xpZW50cyBzaG91bGQgcmV0cnkgdGhlIGNhbGwgd2l0aCBhcHByb3ByaWF0ZSBleHBvbmVudGlhbCBiYWNrb2ZmLgogIHJwYyBXYXRjaChIZWFsdGhDaGVja1JlcXVlc3QpIHJldHVybnMgKHN0cmVhbSBIZWFsdGhDaGVja1Jlc3BvbnNlKTsKCn0KCi8vIFRoZSByZXF1ZXN0IG1lc3NhZ2UgY29udGFpbmluZyB0aGUgdXNlcidzIG5hbWUuCm1lc3NhZ2UgSGVsbG9SZXF1ZXN0IHsKICBzdHJpbmcgbmFtZSA9IDE7Cn0KCi8vIFRoZSByZXNwb25zZSBtZXNzYWdlIGNvbnRhaW5pbmcgdGhlIGdyZWV0aW5ncwptZXNzYWdlIEhlbGxvUmVwbHkgewogIHN0cmluZyBtZXNzYWdlID0gMTsKfQoKCm1lc3NhZ2UgSGVhbHRoQ2hlY2tSZXF1ZXN0IHsKICBzdHJpbmcgc2VydmljZSA9IDE7Cn0KCm1lc3NhZ2UgSGVhbHRoQ2hlY2tSZXNwb25zZSB7CiAgZW51bSBTZXJ2aW5nU3RhdHVzIHsKICAgIFVOS05PV04gPSAwOwogICAgU0VSVklORyA9IDE7CiAgICBOT1RfU0VSVklORyA9IDI7CiAgICBTRVJWSUNFX1VOS05PV04gPSAzOyAgLy8gVXNlZCBvbmx5IGJ5IHRoZSBXYXRjaCBtZXRob2QuCiAgfQogIFNlcnZpbmdTdGF0dXMgc3RhdHVzID0gMTsKfQ==">>,
{file, Here} = code:is_loaded(?MODULE),
ErlangDir = dgiot_httpc:url_join([filename:dirname(filename:dirname(Here)), "/priv/example/Erlang/"]),
Dir = dgiot_httpc:url_join([filename:dirname(filename:dirname(Here)), "/priv/example/Erlang/priv/"]),
Name = dgiot_utils:to_list(FileName),
?LOG(info, "~p", [Dir ++ Name ++ ".proto"]),
?LOG(info, "~p", [base64:decode(Proto)]),
case file:open(Name, [raw, write, delayed_write, binary]) of
{ok, Fd} ->
Result = file:write_file(Dir ++ Name ++ ".proto", base64:decode(Proto)),
file:close(Fd),
?LOG(info, "~p", [Result]);
_ -> pass
end,
Cmd = "cd " ++ ErlangDir ++ " && ./rebar3 compile",
?LOG(info, "~p", [Cmd]),
os:cmd(Cmd),
ok.
get_all_protocol() ->
lists:foldl(fun({_, Channel_type}, Acc) ->
App = maps:get(app, Channel_type),
Mod = maps:get(mod, Channel_type),
CType = maps:get(cType, Channel_type),
Attributes = Mod:module_info(attributes),
[format_protocol(App, CType, Channel_type, Attributes) | Acc]
end, [], list()).
format_protocol(App, CType, Channel_type, Attributes) ->
[Params] = proplists:get_value(params, Attributes, [#{}]),
Channel_type#{
cType => CType,
app => App,
params => maps:merge(#{
<<"_dlinkindex">> => #{
order => 103,
type => integer,
required => true,
default => 0,
title => #{
zh => <<"报文序号"/utf8>>
},
description => #{
zh => <<"报文序号,初始值为0"/utf8>>
}
}
}, Params)
}.
list() ->
Fun =
fun({App, Vsn, Mod}, Acc) ->
case code:is_loaded(Mod) == false of
true ->
Acc;
false ->
case [Channel || {protocol_type, [Channel]} <- Mod:module_info(attributes)] of
[] ->
Acc;
[Channel | _] ->
[{maps:get(priority, Channel, 255), Channel#{app => App, mod => Mod, vsn => Vsn}}] ++ Acc
end
end
end,
lists:sort(dgiot_plugin:check_module(Fun, [])).
get_json(Type) ->
dgiot_utils:get_JsonFile(?MODULE, <<Type/binary, ".json">>).

View File

@ -128,7 +128,16 @@ do_request(post_sendsms_deviceid, #{<<"deviceid">> := DeviceId, <<"tplid">> := T
dgiot_notification:send_sms(Mobile, TplId, Params);
%
do_request(post_warnsendsms, #{<<"objectId">> := DeviceId, <<"branchId">> := BranchId, <<"datetimes">> := DateTimes, <<"docnumber">> := Docnumber, <<"username">> := UserName, <<"workshop">> := Workshop, <<"level">> := Level} = _Args, _Context, _Req) ->
do_request(post_warnsendsms, #{<<"objectId">> := DeviceId, <<"branchId">> := BranchId, <<"datetimes">> := DateTimes, <<"docnumber">> := Docnumber,
<<"username">> := _S, <<"workshop">> := Workshop, <<"level">> := Level} = _Args, #{<<"sessionToken">> := SessionToken} = _Context, _Req) ->
%% io:format("~s ~p _Args = ~p ~n", [?FILE, ?LINE,_Args]),
UserName = case dgiot_auth:get_session(SessionToken) of
{ok, #{<<"username">> := U}} ->
U;
_ ->
error
end,
case Level of
<<"1">> ->
Warn = <<"待首检"/utf8>>,
@ -350,6 +359,7 @@ do_request(post_verify_code_action, #{<<"account">> := Account, <<"code">> := Co
%% API接口
do_request(_OperationId, _Args, _Context, _Req) ->
io:format("~s ~p Q = ~p ~n", [?FILE, ?LINE, _Args]),
{error, <<"Not Allowed.">>}.