feat: api(Dlink topicåˆ

This commit is contained in:
dgiot 2022-03-10 19:41:27 +08:00
parent 7e67842820
commit 29d34bf59a
5 changed files with 124 additions and 21 deletions

14
.pnpm-debug.log Normal file
View File

@ -0,0 +1,14 @@
{
"0 debug pnpm:scope": {
"selected": 1
},
"1 error pnpm": {
"code": "ERR_PNPM_NO_IMPORTER_MANIFEST_FOUND",
"err": {
"name": "pnpm",
"message": "No package.json (or package.yaml, or package.json5) was found in \"D:\\msys64\\home\\h7ml\\dgiot\\dgiot\".",
"code": "ERR_PNPM_NO_IMPORTER_MANIFEST_FOUND",
"stack": "pnpm: No package.json (or package.yaml, or package.json5) was found in \"D:\\msys64\\home\\h7ml\\dgiot\\dgiot\".\n at readProjectManifest (D:\\software\\nodejs\\node_global\\node_modules\\pnpm\\dist\\pnpm.cjs:19808:13)\n at async Object.readProjectManifestOnly (D:\\software\\nodejs\\node_global\\node_modules\\pnpm\\dist\\pnpm.cjs:19812:28)\n at async readProjectManifestOnly (D:\\software\\nodejs\\node_global\\node_modules\\pnpm\\dist\\pnpm.cjs:43711:24)\n at async Object.handler (D:\\software\\nodejs\\node_global\\node_modules\\pnpm\\dist\\pnpm.cjs:177889:24)\n at async D:\\software\\nodejs\\node_global\\node_modules\\pnpm\\dist\\pnpm.cjs:182165:21\n at async run (D:\\software\\nodejs\\node_global\\node_modules\\pnpm\\dist\\pnpm.cjs:182139:34)\n at async runPnpm (D:\\software\\nodejs\\node_global\\node_modules\\pnpm\\dist\\pnpm.cjs:182358:5)\n at async D:\\software\\nodejs\\node_global\\node_modules\\pnpm\\dist\\pnpm.cjs:182350:7"
}
}
}

View File

@ -220,23 +220,13 @@ device_sql(Select, From, Where, _Method) ->
%% #{<<"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),
SelectTpl = getSelect(Select, <<"">>),
TopicTpl = case From of
#{<<"productid">> :=ProductId,<<"devaddr">> := Devaddr} ->
<<" thing/", ProductId/binary, Devaddr/binary, "/#", "\r\n">>;
_ -> <<" thing/", "/test/#", "\r\n">>
end,
#{<<"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} ->
@ -252,7 +242,8 @@ device_sql(Select, From, Where, _Method) ->
DefaultSql = <<"SELECT\"", "\r\n",
" payload.msg as msg,clientid,", "\r\n ",
SelectTpl/binary,
TopicTpl/binary,
" FROM ", "\r\n ",
TopicTpl/binary,
" WHERE", "\r\n",
WhereSql/binary
>>,
@ -368,3 +359,27 @@ sysc_rules() ->
pass
end
end.
%%
%%
getSelect(#{<<"payload">> := Payload} = Select, Acc1) ->
NewAcc = lists:foldl(fun(Id, Acc) ->
case Acc of
<<"">> ->
<<"payload.", Id/binary, " as ", Id/binary>>;
_ ->
<<Acc/binary, " , ", "payload.", Id/binary, " as ", Id/binary>>
end
end, Acc1, Payload),
getSelect(maps:without([<<"payload">>], Select), NewAcc);
getSelect(Select, Acc1) ->
lists:foldl(fun(Id, Acc) ->
case Acc of
<<"">> ->
<<Id/binary>>;
_ ->
<<Acc/binary, " , ", Id/binary>>
end
end, Acc1, lists:flatten(maps:values(Select))).

View File

@ -2,11 +2,11 @@
"tags": [
{
"name": "Dlink",
"description": "Dlink解码器"
"description": "Dlink协议"
},
{
"name": "Protocol",
"description": "Dlink协议"
"description": "Dlink解码器"
}
],
"definitions": {
@ -40,6 +40,34 @@
"Protocol"
]
}
},
"/topic": {
"get": {
"summary": "获取topic列表",
"description": "获取topic列表",
"parameters": [
],
"responses": {
"200": {
"description": "Returns operation status"
},
"400": {
"description": "Bad Request"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"500": {
"description": "Server Internal error"
}
},
"tags": [
"Dlink"
]
}
}
}
}

View File

@ -141,3 +141,39 @@ list() ->
end
end,
lists:sort(dgiot_plugin:check_module(Fun, [])).
getTopic()->
#{
<<"basic">> => [
#{
<<"topic">> => <<"$dg/user/${deviceid}/post">>,
<<"type">> => <<"sub">>,
<<"desc">> => <<"设备属性上报"/utf8>>,
<<"isdef">> => true,
<<"category">> => <<"OTA 升级"/utf8>>
},
#{
<<"topic">> => <<"$dg/thing/${deviceid}/">>,
<<"type">> => <<"pub">>,
<<"desc">> => <<"消息下发"/utf8>>,
<<"isdef">> => true,
<<"category">> => <<"OTA 升级"/utf8>>
}
],
<<"thing">> => [
#{
<<"topic">> => <<"$dg/user/${deviceid}/post">>,
<<"type">> => <<"sub'">>,
<<"desc">> => <<"设备属性上报"/utf8>>,
<<"isdef">> => true,
<<"category">> => <<"属性上报"/utf8>>
},
#{
<<"topic">> => <<"$dg/thing/${deviceid}/">>,
<<"type">> => <<"pub">>,
<<"desc">> => <<"消息下发"/utf8>>,
<<"isdef">> => true,
<<"category">> => <<"属性上报"/utf8>>
}
]
}.

View File

@ -70,13 +70,23 @@ handle(OperationID, Args, Context, Req) ->
%%%===================================================================
%%% Version:API版本
%%%===================================================================
%% API接口
%% Proctol : :json文件导库
%% Proctol : Dlink协议列表
%% OperationId:protocol
%% :GET /iotapi/protocol
do_request(get_protocol, _Body, #{<<"sessionToken">> := _SessionToken} = _Context, _Req) ->
Protocols = dgiot_dlink:get_all_protocol(),
{200, Protocols};
%%%===================================================================
%%% Version:API版本
%%%===================================================================
%% Proctol : Dlink topic列表
%% OperationId:topic
%% :GET /iotapi/topic
do_request(get_topic, _Body, #{<<"sessionToken">> := _SessionToken} = _Context, _Req) ->
Topics = dgiot_dlink:getTopic(),
{200, Topics};
do_request(_OperationId, _Args, _Context, _Req) ->
{error, <<"Not Allowed.">>}.