feat: parse_auth

This commit is contained in:
dawnwinterLiu 2023-04-14 12:22:34 +08:00
parent de71892ad3
commit 0dbc2cca60
12 changed files with 193 additions and 113 deletions

View File

@ -397,14 +397,21 @@ notification(DeviceId, Status, Longitude, Latitude, IsEnable, Now) ->
end,
Address =
case dgiot_data:get(?DGIOT_LOCATION_ADDRESS, DeviceId) of
not_find ->
get_address(DeviceId, Longitude, Latitude);
Addr ->
Addr
Addr when size(Addr) > 0 ->
Addr;
_ ->
get_address(DeviceId, Longitude, Latitude)
end,
dgiot_mqtt:publish(DeviceId, Topic, jsx:encode(#{
PubData =
case size(Address) of
0 ->
#{};
_ ->
#{<<"address">> => Address}
end,
dgiot_mqtt:publish(DeviceId, Topic, jsx:encode(PubData#{
DeviceId => #{
<<"status">> => NewStatus, <<"isEnable">> => IsEnable, <<"lastOnlineTime">> => Now, <<"address">> => Address,
<<"status">> => NewStatus, <<"isEnable">> => IsEnable, <<"lastOnlineTime">> => Now,
<<"location">> => #{<<"longitude">> => Longitude, <<"latitude">> => Latitude}
}}));
_ ->

View File

@ -1,3 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCgjpVEZJTdmbzm
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCgjpVEZJTdmbzm
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCgjpVEZJTdmbzm
rid/yxITsCE4NB3NNxX7p1ob2Sd7s+O5cni7kl5scmkfTtHAb4hNEnHKZrrrVnQT
x8GSGyotuQa/O13Ek6f9uk1tGPdSPi2cKoRed9oUb6mnUkwMT5dcKbdyoCP4Yq7D
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCgjpVEZJTdmbzm
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCgjpVEZJTdmbzm
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCgjpVEZJTdmbzm
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCgjpVEZJTdmbzm
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCgjpVEZJTdmbzm
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCgjpVEZJTdmbzm
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCgjpVEZJTdmbzm
J2mxLi1OVZ3JQrojNBskbJHqkRINV2ND2cO3XRVqNXJU8vc0M0Gl1lEIluuD6nd3
naTkWG1Ir9sDZNk/sokdYf3Hv5IVPcKuPlQnFHEyHQrxUHWguaxQxPLVHhMdgKV0
4P1uDNCW2KepoRh0YtMj+LirfQKBgQDF4enNVc9ytcAw4VCMNd7EOcN3ODW5NWcD
rid/yxITsCE4NB3NNxX7p1ob2Sd7s+O5cni7kl5scmkfTtHAb4hNEnHKZrrrVnQT
x8GSGyotuQa/O13Ek6f9uk1tGPdSPi2cKoRed9oUb6mnUkwMT5dcKbdyoCP4Yq7D
u8fjq0y+iQKBgAPerJjxuVZMsJOi6PmE3wue58/ZCzXi/GZAU4N2XrNoA4WLEvLG
2tcC8M8vaRIohN9wbrHRCdBB0vkr2U1NLJZIg3i4NgdhKJ8EL4PJoJXJHuqgZPEk
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCgjpVEZJTdmbzm
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCgjpVEZJTdmbzm
G9x4hRJwG6VLRq+aPuBpz+ZZAxV9ztQy/1akB0p+Lyja/gGX0HaEncAv6+P3uDaM
yDwCKtqLRfnWujwaREd+fSmnskGELn83GEfUDYECgYBzN07EV6DAF51luwfo/83w
0/bPO2L32ezmS9f6yLkRdMeFJOGkgy0dRewpk9M5kQTkFpdz/RlkQ1tPJxf2IApC
AADFM+GKYm3EwuBoXFIwNQq25852Pi7cF6Z/dTP2nMvomAat35P4OPf6UT5Rq2xP
91h+FCct7r8wCvGvEoy6Gw==
-----END PRIVATE KEY-----

View File

@ -334,7 +334,13 @@ get_baidu_gps(LonDeg, LatDeg, Lonoffset, Latoffset) ->
%%http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding-abroad
%<<"http://api.map.baidu.com/reverse_geocoding/v3/?ak=0twrgSghSF1Q6zUvvYdUu2KFhFiGdbm5&output=json&coordtype=wgs84ll&location=25.368078,111.064845">>.
get_baidu_addr(LonDeg, LatDeg) ->
AppKey = dgiot_utils:to_binary(application:get_env(dgiot_http, baidumap_appkey, <<"">>)),
AppKey =
case dgiot_data:get(dgiot_configuration, baidu_sak) of
not_find ->
dgiot_utils:to_binary(application:get_env(dgiot_http, baidumap_appkey, <<"">>));
Ak ->
Ak
end,
get_baidu_addr(AppKey, "wgs84ll", LonDeg, LatDeg).
get_baidu_addr(AK, Coordtype, Lng, Lat) ->

View File

@ -35,9 +35,6 @@ init_ets() ->
dgiot_data:init(?CONFIGURATION),
dgiot_data:init(?NOTIFICATION).
%
send_verification_code(NationCode, Key) ->
case catch dgiot_cache:get(<<Key/binary, "wait">>) of
@ -134,7 +131,7 @@ send_sms(NationCode, Mobile, TplId, Params, AppId, AppKey, Sign, Ext) ->
case jsx:decode(ResBody, [{labels, binary}, return_maps]) of
#{<<"result">> := 0, <<"errmsg">> := <<"OK">>} = Result ->
{ok, Result#{<<"code">> => 200}};
#{<<"errmsg">> := ErrMsg, <<"result">> := Code} = ErrResult->
#{<<"errmsg">> := ErrMsg, <<"result">> := Code} = ErrResult ->
{ok, ErrResult#{<<"code">> => Code, <<"error">> => ErrMsg}}
end;
{Err, Reason} when Err == error; Err == 'EXIT' ->
@ -403,22 +400,14 @@ save_configuration() ->
DictId = dgiot_parse_id:get_dictid(<<"dgiotconfiguration">>, <<"configuration">>, <<"configuration">>, <<"dgiotconfiguration">>),
case dgiot_parse:get_object(<<"Dict">>, DictId) of
{ok, #{<<"data">> := Data}} ->
Sms = maps:get(<<"sms">>, Data, #{}),
Sms_appid = maps:get(<<"appid">>, Sms, <<"">>),
Sms_appkey = maps:get(<<"appkey">>, Sms, <<"">>),
Sms_sign = maps:get(<<"sign">>, Sms, <<"">>),
Verify_Code_Tplid = maps:get(<<"verify_code_tplid">>, Sms, <<"1715928">>),
dgiot_data:insert(?CONFIGURATION, sms_appid, Sms_appid),
dgiot_data:insert(?CONFIGURATION, sms_appkey, Sms_appkey),
dgiot_data:insert(?CONFIGURATION, sms_sign, Sms_sign),
dgiot_data:insert(?CONFIGURATION, sms_verify_code_tplid, Verify_Code_Tplid),
Mail = maps:get(<<"mail">>, Data, #{}),
Mail_username = maps:get(<<"username">>, Mail, <<"">>),
Mail_password = maps:get(<<"password">>, Mail, <<"">>),
Mail_smtp = maps:get(<<"smtp">>, Mail, <<"">>),
dgiot_data:insert(?CONFIGURATION, mail_username, Mail_username),
dgiot_data:insert(?CONFIGURATION, mail_password, Mail_password),
dgiot_data:insert(?CONFIGURATION, mail_smtp, Mail_smtp);
maps:fold(fun
(Key, Value, _) when is_map(Value) ->
maps:fold(fun(Key1, Value1, _) ->
dgiot_data:insert(?CONFIGURATION, dgiot_utils:to_atom(<<Key/binary, "_", Key1/binary>>), Value1)
end, #{}, Value);
(_, _, _) ->
pass
end, #{}, Data);
_ ->
pass
end.

View File

@ -46,7 +46,7 @@
get_children/1,
getuser/1
]).
-export([create_user/2, delete_user/2, put_user/2, disableusere/3, check_roles/1]).
-export([create_user/2, delete_user/2, put_user/2, get_user/2, disableusere/3, check_roles/1]).
-export([login_by_account/2, login_by_token/2, login_by_mail_phone/1, do_login/1]).
-export([create_user_for_app/1, get_token/1, set_cookies/3, add_acl/5]).
-export([get_usersession/1, put_usersession/2, del_usersession/1]).
@ -343,7 +343,7 @@ refresh_session(Token) ->
get_roleuser(Filter, SessionToken) ->
_IncludeChild = maps:get(<<"include">>, Filter, false),
IncludeChild = maps:get(<<"include">>, Filter, false),
%% io:format("~s ~p Filter ~p IncludeChild ~p ~n", [?FILE, ?LINE, Filter, IncludeChild]),
case dgiot_parse:query_object(<<"_Role">>, maps:without([<<"include">>, <<"userfilter">>], Filter),
[{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
@ -359,13 +359,25 @@ get_roleuser(Filter, SessionToken) ->
_ ->
maps:without([<<"phone">>], UserWhere)
end,
UsersQuery =
UserFilter#{<<"where">> => NewUserWhere#{<<"$relatedTo">> => #{
<<"object">> => #{
<<"__type">> => <<"Pointer">>,
<<"className">> => <<"_Role">>,
<<"objectId">> => RoleId}, <<"key">> => <<"users">>},
<<"tag">> => #{<<"$exists">> => true}}},
ChildRoleIds =
case IncludeChild of
true ->
dgiot_role:get_childrole(RoleId);
false ->
[RoleId]
end,
UsersQuery = UserFilter#{
<<"where">> => NewUserWhere#{
<<"tag">> => #{<<"$exists">> => true},
<<"$relatedTo">> => #{
<<"key">> => <<"users">>,
<<"object">> => #{
<<"__type">> => <<"Pointer">>,
<<"className">> => <<"_Role">>,
<<"objectId">> => #{<<"$in">> => ChildRoleIds}
}
}
}},
case dgiot_parse:query_object(<<"_User">>, UsersQuery) of
{ok, #{<<"results">> := Results} = UserResults} ->
NewResults =
@ -863,29 +875,54 @@ create_user(#{<<"username">> := UserName, <<"department">> := RoleId} = Body, Se
%%[{"X-Parse-Session-Token", Session}], [{from, rest}]
delete_user(#{<<"username">> := UserName, <<"department">> := RoleId}, _SessionToken) ->
ChildRoleIds = dgiot_role:get_childrole(RoleId),
UserIds =
lists:foldl(fun(ChildRoleId, Acc) ->
Acc ++ dgiot_parse_auth:get_UserIds(ChildRoleId)
end, [], ChildRoleIds),
UsersQuery = #{<<"where">> => #{<<"objectId">> => #{<<"$in">> => UserIds}}},
UsersQuery = #{
<<"where">> => #{
<<"username">> => UserName,
<<"$relatedTo">> => #{
<<"key">> => <<"users">>,
<<"object">> => #{
<<"__type">> => <<"Pointer">>,
<<"className">> => <<"_Role">>,
<<"objectId">> => #{<<"$in">> => ChildRoleIds}}
}}},
case dgiot_parse:query_object(<<"_User">>, UsersQuery) of
{ok, #{<<"results">> := Results}} ->
R =
lists:map(fun(#{<<"username">> := Name, <<"objectId">> := ObjectId}) ->
case Name of
UserName ->
DelR = dgiot_parse:del_object(<<"_User">>, ObjectId),
dgiot_parse_auth:del_User_Role(ObjectId, RoleId),
#{<<"msg">> => DelR};
_ ->
#{<<"msg">> => <<"error">>}
end
end, Results),
{ok, #{<<"result">> => R}};
{ok, #{<<"results">> := [#{<<"username">> := UserName, <<"objectId">> := ObjectId} | _]}} ->
DelR = dgiot_parse:del_object(<<"_User">>, ObjectId),
dgiot_parse_auth:del_User_Role(ObjectId, RoleId),
{ok, #{<<"msg">> => DelR}};
_ ->
{error, <<"token fail">>}
{error, #{<<"msg">> => <<"token fail">>}}
end.
%%
%%[{"X-Parse-Session-Token", Session}], [{from, rest}]
get_user(#{<<"id">> := UserId}, SessionToken) ->
case dgiot_auth:get_session(dgiot_utils:to_binary(SessionToken)) of
#{<<"roles">> := Roles} ->
R =
maps:fold(fun(RoleId, _, _) ->
ChildRoleIds = dgiot_role:get_childrole(RoleId),
lists:foldl(fun(ChildRoleId, Acc) ->
UsersQuery = #{<<"where">> => #{
<<"objectId">> => UserId,
<<"$relatedTo">> => #{
<<"key">> => <<"users">>,
<<"object">> => #{
<<"__type">> => <<"Pointer">>,
<<"className">> => <<"_Role">>,
<<"objectId">> => ChildRoleId}}}},
case dgiot_parse:query_object(<<"_User">>, UsersQuery) of
{ok, #{<<"results">> := Results}} ->
Acc ++ Results;
_ ->
Acc
end
end, [], ChildRoleIds)
end, #{}, Roles),
{ok, #{<<"data">> => R, <<"msg">> => <<"success">>, <<"status">> => 0}};
_ ->
{ok, #{<<"msg">> => <<"token fail">>}}
end.
%%
%%[{"X-Parse-Session-Token", Session}], [{from, rest}]
@ -897,13 +934,13 @@ put_user(#{<<"username">> := UserName} = Body, SessionToken) ->
ChildRoleIds = dgiot_role:get_childrole(RoleId),
ChildUsers =
lists:foldl(fun(ChildRoleId, Acc) ->
%% Acc ++ dgiot_parse_auth:get_UserIds(ChildRoleId)
UsersQuery = #{<<"where">> => #{<<"$relatedTo">> =>
#{<<"object">> => #{
<<"__type">> => <<"Pointer">>,
<<"className">> => <<"_Role">>,
<<"objectId">> => ChildRoleId},
<<"key">> => <<"users">>}}},
UsersQuery = #{<<"where">> => #{
<<"$relatedTo">> => #{
<<"key">> => <<"users">>,
<<"object">> => #{
<<"__type">> => <<"Pointer">>,
<<"className">> => <<"_Role">>,
<<"objectId">> => ChildRoleId}}}},
case dgiot_parse:query_object(<<"_User">>, UsersQuery) of
{ok, #{<<"results">> := Results}} ->
Acc ++ Results;

View File

@ -128,25 +128,26 @@ get_newwhere(Header, Where) ->
Map = dgiot_json:decode(Where),
case dgiot_auth:get_session(SessionToken) of
#{<<"roles">> := Roles} ->
ViewIds =
RoleIds =
maps:fold(fun(RoleId, Role, Acc) ->
case maps:find(<<"level">>, Role) of
{ok, Level} when Level < 3 ->
Acc ++ [true];
_ ->
case dgiot_role:get_role_views(RoleId) of
not_find ->
Acc;
Ids ->
Acc ++ Ids
end
Acc ++ [RoleId]
end
end, [], Roles),
case lists:member(true, ViewIds) of
case lists:member(true, RoleIds) of
true ->
Where;
_ ->
dgiot_json:encode(Map#{<<"objectId">> => #{<<"$in">> => ViewIds}})
dgiot_json:encode(Map#{<<"$relatedTo">> => #{
<<"object">> =>
#{<<"__type">> => <<"Pointer">>,
<<"className">> => <<"_Role">>,
<<"objectId">> => #{<<"$in">> => RoleIds}},
<<"key">> => <<"views">>
}})
end;
_ ->
Where
@ -423,7 +424,7 @@ do_request_after(Method0, Path, Header, NewQueryData, ResBody, Options) ->
method(Method0, atom)
end,
{match, PathList} = re:run(Path, <<"([^/]+)">>, [global, {capture, all_but_first, binary}]),
%% io:format("~s ~p ~p ~p ~n",[?FILE, ?LINE, Path, NewQueryData]),
%% io:format("~s ~p ~p ~p ~n",[?FILE, ?LINE, Path, NewQueryData]),
dgiot_parse_hook:do_request_hook('after', lists:concat(PathList), Method, dgiot_parse:get_token(Header), NewQueryData, ResBody).

View File

@ -144,19 +144,14 @@ do_request(post_user, #{<<"username">> := _UserName, <<"password">> := _Password
{error, Error} -> {500, Error}
end;
do_request(delete_user, #{<<"username">> := UserName} = Body, #{<<"sessionToken">> := SessionToken}, _Req) ->
DefaultUsers = ?DEFUser,
case lists:member(UserName, DefaultUsers) of
true ->
{ok, #{<<"code">> => 401, <<"msg">> => <<"dgiot_admin PROHIBITED DELETE">>}};
_ ->
case dgiot_parse_auth:delete_user(Body, SessionToken) of
{ok, _Data} ->
dgiot_role:load_user(),
dgiot_parse_auth:load_roleuser(),
{200, #{<<"result">> => true}};
{error, Error} -> {error, Error}
end
do_request(delete_user, Body, #{<<"sessionToken">> := SessionToken}, _Req) ->
case dgiot_parse_auth:delete_user(Body, SessionToken) of
{ok, Data} ->
dgiot_role:load_user(),
dgiot_parse_auth:load_roleuser(),
{200, Data};
{error, Error} ->
{error, Error}
end;
do_request(put_user, #{<<"username">> := UserName} = Body, #{<<"sessionToken">> := SessionToken}, _Req) ->
@ -174,6 +169,15 @@ do_request(put_user, #{<<"username">> := UserName} = Body, #{<<"sessionToken">>
end
end;
do_request(get_users_id, Body, #{<<"sessionToken">> := SessionToken}, _Req) ->
case dgiot_parse_auth:get_user(Body, SessionToken) of
{ok, Data} ->
dgiot_role:load_user(),
dgiot_parse_auth:load_roleuser(),
{200, Data};
{error, Error} -> {500, Error}
end;
%% IoTDevice :
%% OperationId: get_disableuser
%% Disuserid
@ -268,6 +272,7 @@ do_request(get_usertree, _Arg, #{<<"sessionToken">> := SessionToken} = _Context,
%% API接口
do_request(_OperationId, _Args, _Context, _Req) ->
?LOG(info, "_Args ~p", [_Args]),
%% io:format("~s ~p _OperationId = ~p.~n", [?FILE, ?LINE, _OperationId]),
%% io:format("~s ~p _Args = ~p.~n", [?FILE, ?LINE, _Args]),
{error, <<"Not Allowed.">>}.

View File

@ -155,23 +155,16 @@ get_control(Round, Data, Control) ->
get_storage(ProductId, Calculated) ->
case dgiot_product:lookup_prod(ProductId) of
{ok, #{<<"thing">> := #{<<"properties">> := Props}}} ->
lists:foldl(fun(X, Acc) ->
case Acc of
error ->
Acc;
_ ->
case X of
#{<<"isstorage">> := true, <<"identifier">> := Identifier} ->
lists:foldl(fun
(#{<<"isstorage">> := true, <<"identifier">> := Identifier}, Acc) ->
case maps:find(Identifier, Calculated) of
{ok, Value} ->
Acc#{Identifier => Value};
_ ->
Acc
end;
_ ->
(_, Acc) ->
Acc
end
end
end, #{}, Props);
_Error ->
Calculated
@ -327,7 +320,7 @@ save_td(ProductId, DevAddr, Ack, _AppData) ->
DeviceId = dgiot_parse_id:get_deviceid(ProductId, DevAddr),
Interval = dgiot_product:get_interval(ProductId),
%%
CacheData = merge_cache_data(DeviceId, Ack, Interval),
CacheData = dgiot_task:merge_cache_data(DeviceId, Ack, Interval),
%%
Collection = dgiot_task:get_collection(ProductId, [], CacheData),
%%

View File

@ -110,7 +110,6 @@ handle_info(read, State) ->
%% ACK消息触发进行新的指令发送
handle_info({dclient_ack, Topic, Payload}, #dclient{channel = ChannelId, userdata = Usedata} = State) ->
dgiot_metrics:inc(dgiot_task, <<"task_recv">>, 1),
%% io:format("~s ~p Topic ~p Payload ~p ",[?FILE, ?LINE,Topic, Payload]),
case binary:split(Topic, <<$/>>, [global, trim]) of
[<<"$dg">>, <<"thing">>, ProductId, DevAddr, <<"properties">>, <<"report">>] ->
dgiot_bridge:send_log(dgiot_utils:to_binary(ChannelId), ProductId, DevAddr, "~s ~p recv => ~p ~ts ", [?FILE, ?LINE, Topic, unicode:characters_to_list(jsx:encode(Payload))]),

View File

@ -44,12 +44,12 @@ insert_sql(#{<<"driver">> := <<"WS">>, <<"ws_pid">> := ConnPid, <<"ws_ref">> :=
case gun:await(ConnPid, StreamRef) of
{ws, {text, Data}} ->
case catch dgiot_json:decode(Data, [return_maps]) of
#{<<"code">> := 0} = R ->
#{<<"code">> := _} = R ->
case maps:get(<<"channel">>, Context, <<"">>) of
<<"">> ->
?LOG(debug, "Execute (~ts) ", [unicode:characters_to_list(Sql)]);
ChannelId ->
dgiot_bridge:send_log(ChannelId, "Execute (~ts)", [unicode:characters_to_list(Sql)])
dgiot_bridge:send_log(ChannelId, "Execute (~ts) ~p", [unicode:characters_to_list(Sql), jsx:encode(R)])
end,
{ok, R};
_ ->

View File

@ -19,7 +19,7 @@
-include("dgiot_tdengine.hrl").
-include_lib("dgiot/include/logger.hrl").
-export([add_field/4, get_field/1, check_fields/2, check_fields/3, get_time/2, check_value/3]).
-export([add_field/4, get_field/1, check_fields/2, check_fields/3, get_time/2, check_value/3, get_field_type/1]).
add_field(#{<<"type">> := <<"enum">>}, Database, TableName, LowerIdentifier) ->
<<"ALTER TABLE ", Database/binary, TableName/binary, " ADD COLUMN ", LowerIdentifier/binary, " INT;">>;
@ -212,9 +212,9 @@ get_time(V, Interval) ->
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));
round(dgiot_utils:to_int(Value));
get_type_value(Type, Value, _Specs) when Type == <<"INT">>; Type == <<"DATE">>, is_float(Value) ->
round(Value);
round(Value);
get_type_value(Type, Value, _Specs) when Type == <<"INT">>; Type == <<"DATE">> ->
Value;
get_type_value(Type, Value, Specs) when Type == <<"FLOAT">>; Type == <<"DOUBLE">> ->
@ -237,3 +237,23 @@ get_type_value(<<"IMAGE">>, Value, _Specs) ->
round(dgiot_utils:to_int(Value));
get_type_value(_, Value, _Specs) ->
Value.
get_field_type(<<"enum">>) ->
<<"int">>;
get_field_type(<<"file">>) ->
<<"nchar">>;
get_field_type(<<"text">>) ->
<<"nchar">>;
get_field_type(<<"url">>) ->
<<"nchar">>;
get_field_type(<<"geopoint">>) ->
<<"nchar">>;
get_field_type(<<"image">>) ->
<<"bigint;">>;
get_field_type(<<"date">>) ->
<<"timestamp">>;
get_field_type(<<"long">>) ->
<<"bigint">>;
get_field_type(Type) ->
list_to_binary(string:to_lower(binary_to_list(Type))).

View File

@ -104,15 +104,13 @@ alter_table(#{<<"tableName">> := TableName}, #{<<"channel">> := Channel} = Conte
case dgiot_tdengine_pool:run_sql(Context, execute_query, Sql1) of
{ok, #{<<"results">> := Results}} when length(Results) > 0 ->
TdColumn =
lists:foldl(fun(Column, Acc) ->
case Column of
#{<<"Field">> := Identifier, <<"Type">> := Type} ->
Acc#{Identifier => list_to_binary(string:to_lower(binary_to_list(Type)))};
#{<<"field">> := Identifier, <<"type">> := Type} ->
Acc#{Identifier => list_to_binary(string:to_lower(binary_to_list(Type)))};
_ ->
Acc
end
lists:foldl(fun
(#{<<"Field">> := Identifier, <<"Type">> := Type}, Acc) ->
Acc#{Identifier => list_to_binary(string:to_lower(binary_to_list(Type)))};
(#{<<"field">> := Identifier, <<"type">> := Type}, Acc) ->
Acc#{Identifier => list_to_binary(string:to_lower(binary_to_list(Type)))};
(_, Acc) ->
Acc
end, #{}, Results),
AddSqls = dgiot_tdengine_schema:get_addSql(ProductId, TdColumn, Database, TableName),
lists:map(fun(AddSql) ->
@ -135,7 +133,7 @@ get_addSql(ProductId, TdColumn, Database, TableName) ->
case Prop of
#{<<"dataType">> := #{<<"type">> := Type} = DataType, <<"identifier">> := Identifier, <<"isstorage">> := true} ->
LowerIdentifier = list_to_binary(string:to_lower(binary_to_list(Identifier))),
LowerType = list_to_binary(string:to_lower(binary_to_list(Type))),
LowerType = dgiot_tdengine_field:get_field_type(Type),
case maps:find(LowerIdentifier, TdColumn) of
error ->
Acc ++ [dgiot_tdengine_field:add_field(DataType, Database, TableName, LowerIdentifier)];