Skip to content

Commit

Permalink
feat: add post testpaper
Browse files Browse the repository at this point in the history
  • Loading branch information
lsxredrain committed Oct 30, 2021
1 parent 6c54a07 commit b7ad572
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 1 deletion.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [](https://github.com/dgiot/dgiot/compare/v4.3.5...v) (2021-10-21)
# [](https://github.com/dgiot/dgiot/compare/v4.3.5...v) (2021-10-30)


### Bug Fixes
Expand All @@ -8,6 +8,11 @@

### Features

* centos 7.9 ([a7e56a6](https://github.com/dgiot/dgiot/commit/a7e56a630e22db7a1e8513c96ec58f3127a6cd7c))
* device_card ([6c54a07](https://github.com/dgiot/dgiot/commit/6c54a07013fb7b9b92e5ddaf498471488b1341a7))
* go-fastdfs Api ([c4ad4e4](https://github.com/dgiot/dgiot/commit/c4ad4e4ed5be37b0b9c3abb7211746c38d26cf96))
* go-fastdfs Api ([3160613](https://github.com/dgiot/dgiot/commit/316061335e7c96bcea36d6d55da6cbc3d76931e9))
* modbus_rtu:format_value() ([af12dda](https://github.com/dgiot/dgiot/commit/af12dda71f09fe506288627bd430768d8b4bbcdc))
* rm esports ([c33dedd](https://github.com/dgiot/dgiot/commit/c33dedd1340ad0b3fbd00f4a74a6d62678a4ef76))


Expand Down
41 changes: 41 additions & 0 deletions apps/dgiot/src/utils/dgiot_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
, hash_2/4
, squotes_wrapped/1
, round/2
, split_list/5
, read/3
, read_from_csv/2
, read_csv/3
, rate/2
Expand Down Expand Up @@ -487,6 +489,45 @@ round(Num, Len) ->
N = math:pow(10, NewLen),
round(Num * N) / N.

read(Path, Fun, Acc) ->
case file:open(Path, [read]) of
{ok, IoDevice} ->
R = read_line(IoDevice, Fun, Acc),
file:close(IoDevice),
R;
{error, Reason} ->
{error, Reason}
end.
read_line(IoDevice, Fun, Acc) ->
case file:read_line(IoDevice) of
{ok, Row} ->
read_line(IoDevice, Fun, Acc ++ [Fun(Row)]);
eof ->
Acc;
{error, _Reason} ->
Acc
end.

split_list(_Start, _End, _Flag, [], Result) ->
Result;
split_list(Start, End, Flag, [Row | Acc], Result) ->
case re:run(Row, Start, [{capture, first, list}]) of
{match, [_]} ->
split_list(Start, End, true, Acc, Result);
_ ->
case re:run(Row, End, [{capture, first, list}]) of
{match, [_]} ->
Result;
_ ->
case Flag of
true ->
split_list(Start, End, Flag, Acc, Result ++ [Row]);
_ ->
split_list(Start, End, Flag, Acc, Result)
end
end
end.

read_from_csv(Path, Fun) ->
case file:open(Path, [read]) of
{ok, IoDevice} ->
Expand Down
44 changes: 44 additions & 0 deletions apps/dgiot_evidence/priv/swagger/swagger_evidence.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,50 @@
]
}
},
"/testpaper": {
"post": {
"summary": "试卷",
"description": "导入试卷",
"consumes": [
"multipart/form-data"
],
"parameters": [
{
"name": "productid",
"in": "formData",
"description": "产品ID",
"default": "3473c6de01",
"type": "string"
},
{
"name": "file",
"in": "formData",
"description": "数据文件",
"type": "file"
}
],
"responses": {
"200": {
"description": "Returns operation status"
},
"400": {
"description": "Bad Request"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"500": {
"description": "Server Internal error"
}
},
"tags": [
"Data"
]
}
},
"/import_reportTemp": {
"post": {
"summary": "导库",
Expand Down
117 changes: 117 additions & 0 deletions apps/dgiot_evidence/src/handler/dgiot_evidence_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ do_request(post_bed, _Body, #{<<"sessionToken">> := SessionToken} = _Context, _R
end,
{200, #{<<"result">> => #{<<"status">> => 0}}};

%% DB 概要: 导入试卷报告
%% OperationId:testpaper
%% 请求:POST /iotapi/testpaper
do_request(post_testpaper, #{<<"productid">> := Productid, <<"file">> := FileInfo},
#{<<"sessionToken">> := _SessionToken} = _Context, _Req) ->
{ok, get_paper(Productid, FileInfo)};

%% evidence 概要: 增加取证报告模版 描述:新增取证报告模版
%% OperationId:post_reporttemp
%% 请求:put /iotapi/reporttemp
Expand Down Expand Up @@ -263,6 +270,116 @@ do_report(Config, DevType, Name, SessionToken, FullPath, Uri) ->
[]
end.

get_paper(ProductId, FileInfo) ->
Path = maps:get(<<"fullpath">>, FileInfo),
Fun = fun(Row) ->
Map = jiffy:encode(#{<<"1">> => dgiot_utils:to_binary(Row)}),
[V | _] = maps:values(jsx:decode(Map, [{labels, binary}, return_maps])),
V
end,
List = dgiot_utils:read(Path, Fun, []),
Title = lists:nth(1, List),
DeviceId = dgiot_parse:get_deviceid(ProductId, dgiot_utils:to_md5(Title)),
Single = dgiot_utils:split_list(<<"一、单选题"/utf8>>, <<"二、多选题"/utf8>>, false, List, []),
Multiple = dgiot_utils:split_list(<<"二、多选题"/utf8>>, <<"三、判断题"/utf8>>, false, List, []),
Judge = dgiot_utils:split_list(<<"三、判断题"/utf8>>, <<"四、案例题"/utf8>>, false, List, []),
Cases = dgiot_utils:split_list(<<"四、案例题"/utf8>>, <<"四、案例题222"/utf8>>, false, List, []),
Cases1 = get_case(Cases, {<<"">>, []}, []),
{Single_question, _} = get_simple(Single, {[], #{}}),
{Multiple_question, _} = get_simple(Multiple, {[], #{}}),
{Judge_question, _} = get_simple(Judge, {[], #{}}),
Paper = Single_question ++ Multiple_question ++ Judge_question ++ Cases1,
create_device(DeviceId, ProductId, Title, Paper),
#{ <<"objectId">> => DeviceId,
<<"paper">> => Paper
}.

get_simple([], {Acc, Map}) ->
{Acc, Map};
get_simple([Row | List], {Acc, Map}) ->
case Row of
<<"A."/utf8, _Result/binary>> ->
get_simple(List, {Acc, Map#{<<"A"/utf8>> => Row}});
<<"B."/utf8, _Result/binary>> ->
get_simple(List, {Acc, Map#{<<"B"/utf8>> => Row}});
<<"C."/utf8, _Result/binary>> ->
get_simple(List, {Acc, Map#{<<"C"/utf8>> => Row}});
<<"D."/utf8, _Result/binary>> ->
get_simple(List, {Acc, Map#{<<"D"/utf8>> => Row}});
<<"E."/utf8, _Result/binary>> ->
get_simple(List, {Acc, Map#{<<"E"/utf8>> => Row}});
<<"F."/utf8, _Result/binary>> ->
get_simple(List, {Acc, Map#{<<"F"/utf8>> => Row}});
<<"答案:"/utf8, Result/binary>> ->
R = re:replace(Result, <<"\n">>, <<>>, [{return, binary}]),
get_simple(List, {Acc ++ [Map#{<<"Answer"/utf8>> => R}], #{}});
<<"答案:"/utf8, Result/binary>> ->
R = re:replace(Result, <<"\n">>, <<>>, [{return, binary}]),
get_simple(List, {Acc ++ [Map#{<<"Answer"/utf8>> => R}], #{}});
<<"\n"/utf8, _/binary>> ->
get_simple(List, {Acc, Map});
R when size(R) > 6 ->
get_simple(List, {Acc, Map#{<<"Question"/utf8>> => Row, <<"type">> => get_type(R)}});
_ ->
get_simple(List, {Acc, Map})
end.

get_type(Question) ->
%% io:format("~ts", [unicode:characters_to_list(Question)]),
case re:run(Question, <<"判断"/utf8>>, [{capture, none}]) of
match ->
<<"判断题"/utf8>>;
_ ->
case re:run(Question, <<"多选"/utf8>>, [{capture, none}]) of
match ->
<<"多选题"/utf8>>;
_ ->
<<"单选题"/utf8>>
end
end.

get_case([], {Title, Acc}, Result) ->
{Single_question, _} = get_simple(Acc, {[], #{}}),
Result ++ [#{<<"type">> => <<"材料题"/utf8>>, <<"Question"/utf8>> => Title, <<"questions"/utf8>> => Single_question}];
get_case([Row | List], {Title, Acc}, Result) ->
case re:run(Row, <<"背景材料"/utf8>>, [{capture, none}]) of
match ->
case Title of
<<"">> ->
get_case(List, {Row, Acc}, Result);
_ ->
{Single_question, _} = get_simple(Acc, {[], #{}}),
get_case(List, {Row, []}, Result ++ [#{<<"type">> => <<"材料题"/utf8>>, <<"Question"/utf8>> => Title, <<"questions"/utf8>> => Single_question}])
end;
_ ->
get_case(List, {Title, Acc ++ [Row]}, Result)
end.

create_device(DeviceId, ProductId, Devaddr, Paper) ->
case dgiot_parse:get_object(<<"Product">>, ProductId) of
{ok, #{<<"ACL">> := Acl, <<"devType">> := DevType}} ->
case dgiot_parse:get_object(<<"Device">>, DeviceId) of
{ok, #{<<"devaddr">> := _GWAddr}} ->
dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"basedata">> => #{<<"paper">> => Paper}, <<"status">> => <<"ONLINE">>});
_ ->
dgiot_device:create_device(#{
<<"devaddr">> => dgiot_utils:to_md5(Devaddr),
<<"name">> => Devaddr,
<<"isEnable">> => true,
<<"product">> => ProductId,
<<"ACL">> => Acl,
<<"status">> => <<"ONLINE">>,
<<"location">> => #{<<"__type">> => <<"GeoPoint">>, <<"longitude">> => 120.161324, <<"latitude">> => 30.262441},
<<"brand">> => DevType,
<<"devModel">> => DevType,
<<"basedata">> => #{<<"paper">> => Paper}
})
end;
Error2 ->
?LOG(info, "Error2 ~p ", [Error2]),
pass
end.

post_point(#{
<<"reportid">> := ReportId,
<<"index">> := Index,
Expand Down

0 comments on commit b7ad572

Please sign in to comment.