Skip to content

Commit

Permalink
feat: add grpc encode and decode
Browse files Browse the repository at this point in the history
  • Loading branch information
U-JOHNLIU\jonhl committed Jan 29, 2022
1 parent a2d6cc2 commit f46dcc5
Show file tree
Hide file tree
Showing 67 changed files with 1,194 additions and 19,549 deletions.
4 changes: 2 additions & 2 deletions apps/dgiot_device/src/dgiot_product.erl
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ delete(ProductId) ->
dgiot_data:delete(?DGIOT_PRODUCT, ProductId).

get(ProductId) ->
Keys = [<<"nodeType">>, <<"dynamicReg">>, <<"topics">>],
Keys = [<<"ACL">>, <<"status">>, <<"nodeType">>, <<"dynamicReg">>, <<"topics">>, <<"productSecret">>],
case dgiot_parse:get_object(<<"Product">>, ProductId) of
{ok, Product} ->
{ok, maps:with(Keys, Product)};
Expand Down Expand Up @@ -231,7 +231,7 @@ add_device(ProductId, DevAddr) ->
format_product(#{<<"objectId">> := ProductId} = Product) ->
Thing = maps:get(<<"thing">>, Product, #{}),
Props = maps:get(<<"properties">>, Thing, []),
Keys = [<<"ACL">>, <<"status">>, <<"nodeType">>, <<"dynamicReg">>, <<"topics">>],
Keys = [<<"ACL">>, <<"status">>, <<"nodeType">>, <<"dynamicReg">>, <<"topics">>, <<"productSecret">>],
Map = maps:with(Keys, Product),
Map#{
<<"productId">> => ProductId,
Expand Down
File renamed without changes.
File renamed without changes.
15 changes: 14 additions & 1 deletion apps/dgiot_mqtt/README.md → apps/dgiot_dlink/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
+ 设备侧topic交互采用 {productId}/{deviceAddr}的组合来唯一标识设备, deviceAddr为设备物理地址
+ 用户侧topic交互采用{deviceId}来唯一标识设备,用{userId}来唯一标识用户,deviceId为设备虚拟地址


### 鉴权设计
+ deviceId=md5("Device" + {productId} + {devAddr}).subString(10)
+ %u 表示用Username做ACL规则
Expand Down Expand Up @@ -51,4 +52,16 @@
| 事件上报 |$dg/user/{deviceId}/events|平台|用户|

## payload设计
待设计

+ 支持grpc多语言编解码

```
Dlink RunTime Third-party Decode/Encode
+========================+ +========+==========+
| Dlink | | | |
| +----------------+ | gRPC | gRPC | User's |
| | gPRC Client | ------------------> | Server | Codes |
| +----------------+ | (HTTP/2) | | |
| | | | |
+========================+ +========+==========+
```
File renamed without changes.
File renamed without changes.
File renamed without changes.
27 changes: 27 additions & 0 deletions apps/dgiot_dlink/priv/protos/dlink.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// copy from: https://grpc.io/docs/what-is-grpc/introduction/


syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";

package dgiot;

// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
string name = 1;
}

// The response message containing the greetings
message HelloReply {
string message = 1;
}
22 changes: 14 additions & 8 deletions apps/dgiot_mqtt/rebar.config → apps/dgiot_dlink/rebar.config
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
%%-*- mode: erlang -*-
{plugins,
[{grpc_plugin, {git, "https://gitee.com/fastdgiot/grpc_plugin", {tag, "v0.10.1"}}}
[rebar3_proper,
{grpc_plugin, {git, "https://gitee.com/fastdgiot/grpc_plugin.git", {tag, "v0.10.3"}}}
]}.

{deps,
[{grpc, {git, "https://gitee.com/fastdgiot/grpc-erl", {tag, "0.6.4"}}}
]}.

{grpc,
[ {type, all}
, {protos, ["priv/protos/"]}
, {out_dir, "src/"}
, {gpb_opts, [{module_name_suffix, "_pb"}]}
[{protos, ["priv/protos"]},
{gpb_opts, [{module_name_prefix, "dgiot_"},
{module_name_suffix, "_pb"}]}
]}.

{provider_hooks,
Expand All @@ -19,6 +20,7 @@
]}.

{edoc_opts, [{preprocess, true}]}.

{erl_opts, [warn_unused_vars,
warn_shadow_vars,
warn_unused_import,
Expand All @@ -29,15 +31,19 @@
{xref_checks, [undefined_function_calls, undefined_functions,
locals_not_used, deprecated_function_calls,
warnings_as_errors, deprecated_functions]}.
{xref_ignores, [emqx_exhook_pb]}.

{cover_enabled, true}.
{cover_opts, [verbose]}.
{cover_export_enabled, true}.
{cover_excl_mods, [emqx_exhook_pb,
emqx_exhook_v_1_hook_provider_bhvr,
emqx_exhook_v_1_hook_provider_client]}.

{profiles,
[{test,
[{deps,
[{emqx_ct_helpers, {git, "https://gitee.com/fastdgiot/emqx-ct-helpers", {tag, "1.2.2"}}},
{emqtt, {git, "https://gitee.com/fastdgiot/emqtt", {tag, "1.2.3"}}}]}
[{emqx_ct_helper, {git, "https://gitee.com/fastdgiot/emqx-ct-helpers", {tag, "v1.3.1"}}}
]}
]}
]}.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{application, dgiot_mqtt,
[{description, "DGIOT MQTT"},
{application, dgiot_dlink,
[{description, "DGIOT Dlink"},
{vsn, "4.3.0"},
{applications, [kernel, stdlib, grpc, dgiot]},
{modules, []},
{env, []},
{mod, {dgiot_mqtt_app, []}},
{mod, {dgiot_dlink_app, []}},
{registered, []},
{licenses, ["Apache-2.0"]},
{maintainers, ["DG-IoT Team"]},
Expand Down
40 changes: 40 additions & 0 deletions apps/dgiot_dlink/src/dgiot_dlink.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2020 EMQ 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(dgiot_dlink).

-compile(export_all).
-compile(nowarn_export_all).

start() ->
Services = #{protos => [dgiot_dlink_pb],
services => #{'Greeter' => dgiot_greeter_svr}
},
{ok, _} = grpc:start_server(server, 30051, Services, []).

stop() ->
_ = grpc:stop_server(server).

login() ->
SvrAddr = "http://127.0.0.1:30051",
{ok, _} = grpc_client_sup:create_channel_pool(channel, SvrAddr, #{}).

logout() ->
_ = grpc_client_sup:stop_channel_pool(channel).

send() ->
dgiot_greeter_client:say_hello(#{name => <<"Xiao Ming">>}, #{channel => channel}).

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
%%
%%
%% @end
-module(dgiot_mqtt_app).
-module(dgiot_dlink_app).
-emqx_plugin(auth).
-behaviour(application).

Expand All @@ -36,10 +36,10 @@
%% ===================================================================

start(_StartType, _StartArgs) ->
{ok, Sup} = dgiot_mqtt_sup:start_link(),
{ok, Sup} = dgiot_dlink_sup:start_link(),
_ = load_auth_hook(),
_ = load_acl_hook(),
%% _ = load_publish_hook(),
_ = load_publish_hook(),
{ok, Sup}.


Expand All @@ -49,7 +49,6 @@ stop(_State) ->
prep_stop(State) ->
emqx:unhook('client.authenticate', fun dgiot_mqtt_auth:check/3),
emqx:unhook('client.check_acl', fun dgiot_mqtt_acl:check_acl/5),
%% emqx:unhook('message.publish', fun dgiot_mqtt_message:on_message_publish/3),
State.

load_auth_hook() ->
Expand All @@ -58,7 +57,7 @@ load_auth_hook() ->
load_acl_hook() ->
emqx:hook('client.check_acl', fun dgiot_mqtt_acl:check_acl/5, [#{}]).

%%load_publish_hook() ->
%% emqx:hook('message.publish', fun dgiot_mqtt_message:on_message_publish/3, [#{}]).
load_publish_hook() ->
emqx:hook('message.publish', fun dgiot_mqtt_message:on_message_publish/2, [#{}]).


Loading

0 comments on commit f46dcc5

Please sign in to comment.