fix: save_td

This commit is contained in:
dawnwinterLiu 2023-02-03 16:15:08 +08:00
parent 23eb3eac56
commit 19333480d0
3 changed files with 100 additions and 5 deletions

View File

@ -0,0 +1,85 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2022-2023 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 (pkcs).
-export ([
pad/1,
pad/2,
unpad/1,
unpad/2,
pkcs5_pad/1,
pkcs5_unpad/1
]).
%%pkcs5 is same as pkcs7 when set the blockSize of 8
pad(Bin) ->
pad(Bin, 8).
pkcs5_pad(Bin) ->
pad(Bin, 8).
pad(Bin,BlockSize) when is_binary(Bin)->
Diff = byte_size(Bin) rem BlockSize,
pad2(Bin, BlockSize-Diff);
pad(Str,BlockSize) when is_list(Str)->
Bin = unicode:characters_to_binary(Str),
Diff = byte_size(Bin) rem BlockSize,
pad2(Bin, BlockSize-Diff).
pad2(Bin,PaddingNum) ->
PadList = erlang:list_to_binary([PaddingNum || _ <- lists:seq(1, PaddingNum)]),
<<Bin/binary,PadList/binary>>.
pkcs5_unpad(Bin) ->
unpad(Bin,8).
unpad(<<>>) ->
<<>>;
unpad(Bin) ->
unpad(Bin,8).
unpad(<<>>,_) ->
{ok,<<>>};
unpad(Bin,BlockSize) when is_binary(Bin)->
Last = binary:last(Bin),
Size = byte_size(Bin) - Last,
RemSize = Size rem BlockSize,
unpad2(Bin,RemSize,BlockSize,Size);
unpad(Str,BlockSize) when is_list(Str)->
Bin = unicode:characters_to_binary(Str),
unpad(Bin,BlockSize).
unpad2(Bin,0,BlockSize,Size) ->
PadBin = erlang:list_to_binary([BlockSize || _ <- lists:seq(1, BlockSize)]),
unpad3(Bin,PadBin,Size);
unpad2(Bin,RemSize,BlockSize,Size) ->
PadNum = BlockSize - RemSize,
PadBin = erlang:list_to_binary([PadNum || _ <- lists:seq(1, PadNum)]),
unpad3(Bin,PadBin,Size).
unpad3(Bin,PadBin,Size) ->
case Bin of
<<Data:Size/binary,PadBin/binary>> ->
{ok,Data};
_ ->
{error,bad_padding}
end.

View File

@ -106,7 +106,8 @@ check_value(Value, ProductId, Field) ->
true ->
NewValue;
false ->
throw({error, <<Field/binary, " is not validate">>})
BinNewValue = dgiot_utils:to_binary(NewValue),
throw({error, <<Field/binary, "=", BinNewValue/binary, " is not validate">>})
end
end.
@ -158,6 +159,8 @@ check_field(Data, #{<<"identifier">> := Field, <<"dataType">> := #{<<"type">> :=
check_field(_, _) ->
undefined.
check_validate(null, _) ->
true;
check_validate(Value, #{<<"max">> := Max, <<"min">> := Min}) when is_integer(Max), is_integer(Min) ->
Value =< Max andalso Value >= Min;
check_validate(Value, #{<<"max">> := Max}) when is_integer(Max) ->
@ -200,6 +203,8 @@ get_time(V, Interval) ->
end.
get_type_value(_, null, _) ->
null;
get_type_value(Type, Value, _Specs) when Type == <<"INT">>; Type == <<"DATE">>; Type == <<"SHORT">>; Type == <<"LONG">>; Type == <<"ENUM">>, is_list(Value) ->
round(dgiot_utils:to_int(Value));
get_type_value(Type, Value, _Specs) when Type == <<"INT">>; Type == <<"DATE">>, is_float(Value) ->

View File

@ -103,7 +103,6 @@ alter_table(#{<<"tableName">> := TableName}, #{<<"channel">> := Channel} = Conte
Sql1 = <<"DESCRIBE ", Database/binary, TableName/binary, ";">>,
case dgiot_tdengine_pool:run_sql(Context, execute_query, Sql1) of
{ok, #{<<"results">> := Results}} when length(Results) > 0 ->
dgiot_tdengine:save_fields(ProductId, Results),
TdColumn =
lists:foldl(fun(Column, Acc) ->
case Column of
@ -116,7 +115,13 @@ alter_table(#{<<"tableName">> := TableName}, #{<<"channel">> := Channel} = Conte
AddSqls = dgiot_tdengine_schema:get_addSql(ProductId, TdColumn, Database, TableName),
lists:map(fun(AddSql) ->
dgiot_tdengine_pool:run_sql(Context#{<<"channel">> => Channel}, execute_query, AddSql)
end, AddSqls);
end, AddSqls),
case dgiot_tdengine_pool:run_sql(Context#{<<"channel">> => Channel}, execute_query, Sql1) of
{ok, #{<<"results">> := Results2}} ->
dgiot_tdengine:save_fields(ProductId, Results2);
_ ->
pass
end;
_ ->
pass
end.