feat: (dgiot物模型[新增事件,服务,标签],生成dgiot sql模板)

This commit is contained in:
dgiot 2022-03-09 15:43:34 +08:00
parent f3450e41b3
commit 39ffa25960
5 changed files with 240 additions and 134 deletions

View File

@ -41,15 +41,14 @@
+ 中国Linux / Unix / Mac / windowsビルド
```bash
cd dgiot_dashboard
cd dgiot-dashboard
git pull
yarn dgiot:install
yarn build
pnpm install
pnpm build
cd ../dgiot
git pull
rm ./apps/dgiot_api/priv/www -rf
cp ../dgiot_dashboard/dist/ ./apps/dgiot_api/priv/www -rf
cp ../dgiot_dashboard/swagger ./apps/dgiot_api/priv/www/ -rf
cp ../dgiot-dashboard/dist/ ./apps/dgiot_api/priv/www -rf
make run
cd ./apps/dgiot_api/priv/www
wget https://dgiot-release-1306147891.cos.ap-nanjing.myqcloud.com/v4.4.0/dgiot_dashboard.tar.gz &> /dev/null

View File

@ -41,15 +41,14 @@
+ Китайская Linux / Unix / Mac / Windows Build
```bash
cd dgiot_dashboard
cd dgiot-dashboard
git pull
yarn dgiot:install
yarn build
pnpm install
pnpm build
cd ../dgiot
git pull
rm ./apps/dgiot_api/priv/www -rf
cp ../dgiot_dashboard/dist/ ./apps/dgiot_api/priv/www -rf
cp ../dgiot_dashboard/swagger ./apps/dgiot_api/priv/www/ -rf
cp ../dgiot-dashboard/dist/ ./apps/dgiot_api/priv/www -rf
make run
cd ./apps/dgiot_api/priv/www
wget https://dgiot-release-1306147891.cos.ap-nanjing.myqcloud.com/v4.4.0/dgiot_dashboard.tar.gz &> /dev/null

View File

@ -413,6 +413,46 @@
}
},
"/thing": {
"get": {
"summary": "查询物模型",
"description": "查询物模型",
"parameters": [
{
"name": "productid",
"in": "query",
"type": "string",
"default": "8ecadfebe1",
"description": "产品id"
},
{
"name": "moduleType",
"in": "query",
"type": "string",
"default": "properties",
"description": "属性类型"
}
],
"responses": {
"200": {
"description": "Returns success"
},
"400": {
"description": "Bad Request"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"500": {
"description": "Server Internal error"
}
},
"tags": [
"Product"
]
},
"post": {
"summary": "添加物模型",
"description": "添加物模型",
@ -518,6 +558,7 @@
"description": "物模型单项指标",
"type": "object",
"example": {
"moduleType": "properties",
"name": "电压",
"index": 11,
"isshow": true,
@ -605,6 +646,7 @@
"name": "电压",
"index": 11,
"isshow": true,
"moduleType": "properties",
"dataForm": {
"data": "1",
"rate": 1,

View File

@ -163,107 +163,34 @@ do_request(post_graphql, Body, #{<<"sessionToken">> := SessionToken} = _Context,
end;
%% Thing : :
%% OperationId:get_thing
%% :GET /iotapi/get_thing
do_request(get_thing, #{<<"productid">> := ProductId, <<"moduleType">> := ModuleType},
#{<<"sessionToken">> := SessionToken} = _Context, _Req) ->
getThing(ProductId, ModuleType, SessionToken);
%% Thing : :
%% OperationId:post_thing
%% :PUT /iotapi/post_thing
do_request(post_thing, #{<<"productid">> := ProductId, <<"item">> := Item} = _Body,
#{<<"sessionToken">> := SessionToken} = _Context, _Req) ->
#{<<"identifier">> := Identifier} = Item,
case dgiot_parse:get_object(<<"Product">>, ProductId, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"thing">> := Thing}} ->
Properties = maps:get(<<"properties">>, Thing, []),
{Ids, NewProperties} =
lists:foldl(fun(X, {Ids1, Acc}) ->
case X of
#{<<"identifier">> := Identifier} ->
{Ids1 ++ [Identifier], Acc};
_ ->
{Ids1, Acc ++ [X]}
end
end, {[], [Item]}, Properties),
case length(Ids) == 0 of
true ->
{_, R} = dgiot_parse:update_object(<<"Product">>, ProductId,
#{<<"thing">> => Thing#{<<"properties">> => NewProperties}},
[{"X-Parse-Session-Token", SessionToken}], [{from, rest}]),
{ok, R#{<<"code">> => 200}};
false ->
{ok, #{<<"code">> => 204, <<"msg">> => <<Identifier/binary, " already existed">>}}
end;
Error ->
{error, Error}
end;
postThing(ProductId, Item, SessionToken);
%% Thing : :
%% OperationId:put_thing
%% :PUT /iotapi/put_thing
do_request(put_thing, #{<<"productid">> := ProductId, <<"item">> := Item} = _Body,
#{<<"sessionToken">> := SessionToken} = _Context, _Req) ->
#{<<"identifier">> := Identifier} = Item,
case dgiot_parse:get_object(<<"Product">>, ProductId, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"thing">> := Thing}} ->
#{<<"properties">> := Properties} = Thing,
NewProperties =
lists:foldl(fun(X, Acc) ->
case X of
#{<<"identifier">> := Identifier} ->
Acc ++ [Item];
_ ->
Acc ++ [X]
end
end, [], Properties),
{_, R} = dgiot_parse:update_object(<<"Product">>, ProductId,
#{<<"thing">> => Thing#{<<"properties">> => NewProperties}},
[{"X-Parse-Session-Token", SessionToken}], [{from, rest}]),
{ok, R#{<<"code">> => 200}};
Error ->
{error, Error}
end;
putTing(ProductId, Item, SessionToken);
%% Thing : :
%% OperationId:put_thing
%% :PUT /iotapi/put_thing
do_request(delete_thing, #{<<"productid">> := ProductId, <<"item">> := Item} = _Body,
#{<<"sessionToken">> := SessionToken} = _Context, _Req) ->
#{<<"identifier">> := Identifier} = Item,
case dgiot_parse:get_object(<<"Product">>, ProductId, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"thing">> := Thing}} ->
#{<<"properties">> := Properties} = Thing,
{Ids, NewProperties} =
lists:foldl(fun(X, {Ids1, Acc}) ->
case X of
#{<<"identifier">> := Identifier} ->
{Ids1, Acc};
#{<<"identifier">> := Identifier1, <<"dataForm">> := #{<<"collection">> := Collection}} ->
case binary:match(Collection, [Identifier]) of
nomatch ->
{Ids1, Acc ++ [X]};
_ ->
case Ids1 of
[] ->
{Ids1 ++ [Identifier1], Acc};
_ ->
{Ids1 ++ [<<",", Identifier1/binary>>], Acc}
end
end;
_ ->
{Ids1, Acc}
end
end, {[], []}, Properties),
case length(Ids) == 0 of
true ->
{_, R} = dgiot_parse:update_object(<<"Product">>, ProductId,
#{<<"thing">> => Thing#{<<"properties">> => NewProperties}},
[{"X-Parse-Session-Token", SessionToken}], [{from, rest}]),
{ok, R#{<<"code">> => 200}};
false ->
BinIds = dgiot_utils:to_binary(Ids),
{ok, #{<<"code">> => 204, <<"msg">> => <<BinIds/binary, " use ", Identifier/binary>>}}
end;
Error ->
{error, Error}
end;
deleteThing(ProductId, SessionToken, Item);
%% Product : :json文件导库
@ -637,3 +564,131 @@ get_json_from_zip(FileInfo) ->
{error, Reason}
end.
%% Thing : :
%% OperationId:get_thing
%% :GET /iotapi/get_thing
getThing(ProductId, ModuleType, SessionToken) ->
case dgiot_parse:get_object(<<"Product">>, ProductId, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"thing">> := Thing}} ->
Modules = maps:get(ModuleType, Thing, []),
{_, Maps} =
lists:foldl(fun(X, {Num, Acc}) ->
case X of
#{<<"updateAt">> := UpdateAt} ->
BinUpdateAt = dgiot_utils:to_binary(UpdateAt),
{Num, Acc#{BinUpdateAt => X}};
_ ->
BinUpdateAt = dgiot_utils:to_binary(1577854035000 + Num),
{Num + 1, Acc#{BinUpdateAt => X}}
end
end, {0, #{}}, Modules),
Keys = maps:keys(Maps),
NewModules =
lists:foldl(fun(X, Acc) ->
Acc ++ [maps:get(X, Maps)]
end, [], lists:sort(Keys)),
{ok, NewModules};
Error ->
{error, Error}
end.
%% Thing : :
%% OperationId:post_thing
%% :POST /iotapi/post_thing
postThing(ProductId, Item, SessionToken) ->
case dgiot_parse:get_object(<<"Product">>, ProductId, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"thing">> := Thing}} ->
ModuleType = maps:get(<<"moduleType">>, Item, <<"properties">>),
Properties = maps:get(<<"properties">>, Thing, []),
#{<<"identifier">> := Identifier} = Item,
{Ids, NewProperties} =
lists:foldl(fun(X, {Ids1, Acc}) ->
case X of
#{<<"identifier">> := Identifier} ->
{Ids1 ++ [Identifier], Acc};
_ ->
{Ids1, Acc ++ [X]}
end
end, {[], [Item]}, Properties),
case length(Ids) == 0 of
true ->
{_, R} = dgiot_parse:update_object(<<"Product">>, ProductId,
#{<<"thing">> => Thing#{ModuleType => NewProperties}},
[{"X-Parse-Session-Token", SessionToken}], [{from, rest}]),
{ok, R#{<<"code">> => 200}};
false ->
{ok, #{<<"code">> => 204, <<"msg">> => <<Identifier/binary, " already existed">>}}
end;
Error ->
{error, Error}
end.
%% Thing : :
%% OperationId:put_thing
%% :PUT /iotapi/put_thing
putTing(ProductId, Item, SessionToken) ->
ModuleType = maps:get(<<"moduleType">>, Item, <<"properties">>),
#{<<"identifier">> := Identifier} = Item,
case dgiot_parse:get_object(<<"Product">>, ProductId, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"thing">> := Thing}} ->
Properties = maps:get(ModuleType, Thing, []),
NewProperties =
lists:foldl(fun(X, Acc) ->
case X of
#{<<"identifier">> := Identifier} ->
Acc ++ [Item];
_ ->
Acc ++ [X]
end
end, [], Properties),
{_, R} = dgiot_parse:update_object(<<"Product">>, ProductId,
#{<<"thing">> => Thing#{ModuleType => NewProperties}},
[{"X-Parse-Session-Token", SessionToken}], [{from, rest}]),
{ok, R#{<<"code">> => 200}};
Error ->
{error, Error}
end.
%% Thing : :
%% OperationId:delete_thing
%% :DELETE /iotapi/delete_thing
deleteThing(ProductId, SessionToken, Item) ->
#{<<"identifier">> := Identifier} = Item,
ModuleType = maps:get(<<"moduleType">>, Item, <<"properties">>),
case dgiot_parse:get_object(<<"Product">>, ProductId, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"thing">> := Thing}} ->
#{<<"properties">> := Properties} = Thing,
{Ids, NewProperties} =
lists:foldl(fun(X, {Ids1, Acc}) ->
case X of
#{<<"identifier">> := Identifier} ->
{Ids1, Acc};
#{<<"identifier">> := Identifier1, <<"dataForm">> := #{<<"collection">> := Collection}} ->
case binary:match(Collection, [Identifier]) of
nomatch ->
{Ids1, Acc ++ [X]};
_ ->
case Ids1 of
[] ->
{Ids1 ++ [Identifier1], Acc};
_ ->
{Ids1 ++ [<<",", Identifier1/binary>>], Acc}
end
end;
_ ->
{Ids1, Acc}
end
end, {[], []}, Properties),
case length(Ids) == 0 of
true ->
{_, R} = dgiot_parse:update_object(<<"Product">>, ProductId,
#{<<"thing">> => Thing#{ModuleType => NewProperties}},
[{"X-Parse-Session-Token", SessionToken}], [{from, rest}]),
{ok, R#{<<"code">> => 200}};
false ->
BinIds = dgiot_utils:to_binary(Ids),
{ok, #{<<"code">> => 204, <<"msg">> => <<BinIds/binary, " use ", Identifier/binary>>}}
end;
Error ->
{error, Error}
end.

View File

@ -30,7 +30,7 @@
%% API
-export([swagger_rule/0]).
-export([handle/4, sysc_rules/0, save_rule_to_dict/2, device_sql/3]).
-export([handle/4, sysc_rules/0, save_rule_to_dict/2, device_sql/4]).
%% API描述
%%
@ -171,20 +171,8 @@ do_request(get_actions, _Args, _Context, _Req) ->
%% Rule : sql参数
%% OperationId:post_rulesql
%% :POST /iotapi/rulesql
do_request(post_rulesql, #{<<"productid">> := ProductId, <<"devaddr">> := DevAddr, <<"trigger">> := Trigger} = Params,
_Context, _Req) ->
io:format("Params: ~p~n", [Params]),
case Trigger of
<<"device">> ->
device_sql(ProductId, DevAddr, {});
%%
<<"cron">> ->
{ok, #{<<"productid">> => ProductId, <<"devaddr">> => DevAddr,
<<"message">> => <<"cron触发,暂不处理"/utf8>>, <<"cron">> => maps:get(<<"cron">>, Params)}};
_ ->
{ok, #{<<"productid">> => ProductId, <<"devaddr">> => DevAddr,
<<"message">> => <<"其他触发方式,暂未处理该逻辑"/utf8>>, <<"trigger">> => Trigger}}
end;
do_request(post_rulesql, #{<<"select">> := Select, <<"from">> := From, <<"where">> := Where, <<"method">> := Method}, _Context, _Req) ->
device_sql(Select, From, Where, Method);
do_request(get_actions_id, #{<<"id">> := RuleID}, _Context, _Req) ->
@ -217,35 +205,58 @@ do_request(get_resource_types, _Args, _Context, _Req) ->
do_request(_OperationId, _Args, _Context, _Req) ->
{error, <<"Not Allowed.">>}.
%%getSelect(#{<<"mqttEvent">> := MqttEvent,<<"mqttEvent">> := MqttEvent}->Select) ->
%% sql模板
%% ProductId id
%% DeviceId id
%% Rule sql的规则
device_sql(ProductId, Devaddr, _Rule) ->
%% sql参数
%% Data5 = <<"filename\"", "\r\n", "\r\n", FileName/binary, "\r\n">>,
%% Select
%% From
%% Where
device_sql(Select, From, Where, _Method) ->
%%
%% sql Where参数
%% WhereTpl = [#{<<"identifier">> => <<"flow">>, <<"operator">> => <<"==">>, <<"value">> => <<"test">>},
%% #{<<"identifier">> => <<"temp">>, <<"operator">> => <<"==">>, <<"value">> => <<"test">>},
%% #{<<"identifier">> => <<"flow">>, <<"operator">> => <<"==">>, <<"value">> => <<"test">>}],
%% Select = <<"sql">>,
SelectTpl = lists:foldl(fun(X, Acc) ->
case X of
#{<<"identifier">> := Id, <<"operator">> := Op, <<"value">> := Value} ->
case Acc of
<<"">> ->
<<Id/binary, " ", Op/binary, " ", Value/binary>>;
_ ->
<<Acc/binary, " , ", Id/binary, " ", Op/binary, " ", Value/binary>>
end;
_ -> Acc
end
end, <<"">>, Select),
TopicTpl = case From of
#{<<"productid">> :=ProductId,<<"devaddr">> := Devaddr} ->
<<" thing/", ProductId/binary, Devaddr/binary, "/#", "\r\n">>;
_ -> <<" thing/", "/test/#", "\r\n">>
end,
WhereSql = lists:foldl(fun(X, Acc) ->
case X of
#{<<"identifier">> := Id, <<"operator">> := Op, <<"value">> := Value} ->
case Acc of
<<"">> ->
<<Id/binary, " ", Op/binary, " ", Value/binary>>;
_ ->
<<Acc/binary, " , ", Id/binary, " ", Op/binary, " ", Value/binary>>
end;
_ -> Acc
end
end, <<"">>, Where),
DefaultSql = <<"SELECT\"", "\r\n",
"payload.msg as msg,clientid,", "\r\n",
ProductId/binary, "' as productid FROM'", "\r\n",
"thing/", ProductId/binary, Devaddr/binary, "/#", "\r\n",
"WHERE", "\r\n",
"msg = hello", ProductId/binary, ""
" payload.msg as msg,clientid,", "\r\n ",
SelectTpl/binary,
TopicTpl/binary,
" WHERE", "\r\n",
WhereSql/binary
>>,
MetaRawsql = DefaultSql,
%% case dgiot_parse:get_object(<<"Product">>, ProductId) of
%% {ok, #{<<"objectId">> := ProductId}} ->
%% case DeviceId of
%% %% device # tpoic则设置为通配符
%% <<"#">> ->
%% DefaultSql;
%% _ -> %% device 0 #
%%
%% end;
%% _ ->
%% DefaultSql
%% end,
{ok, #{<<"template">> => MetaRawsql}}.
{ok, #{<<"template">> => DefaultSql}}.
%% cron表达式生成sql模板