diff --git a/apps/dgiot_device/src/dgiot_device_channel.erl b/apps/dgiot_device/src/dgiot_device_channel.erl index 785470e9..eb0a237d 100644 --- a/apps/dgiot_device/src/dgiot_device_channel.erl +++ b/apps/dgiot_device/src/dgiot_device_channel.erl @@ -106,9 +106,9 @@ init(?TYPE, ChannelId, Args) -> }, {ok, State, []}. -handle_init(#state{env = #{<<"checktime">>:= CheckTime}} = State) -> +handle_init(State) -> erlang:send_after(300, self(), {message, <<"_Pool">>, load}), - erlang:send_after(CheckTime * 60 * 1000, self(), {message, <<"_Pool">>, check}), + erlang:send_after(3 * 60 * 1000, self(), {message, <<"_Pool">>, check}), {ok, State}. %% 通道消息处理,注意:进程池调用 diff --git a/apps/dgiot_matlab/.gitignore b/apps/dgiot_matlab/.gitignore new file mode 100644 index 00000000..0c20ff0d --- /dev/null +++ b/apps/dgiot_matlab/.gitignore @@ -0,0 +1,6 @@ +.eunit +deps +*.o +*.beam +*.plt +erl_crash.dump diff --git a/apps/dgiot_matlab/LICENSE b/apps/dgiot_matlab/LICENSE new file mode 100644 index 00000000..d9a10c0d --- /dev/null +++ b/apps/dgiot_matlab/LICENSE @@ -0,0 +1,176 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS diff --git a/apps/dgiot_matlab/Makefile b/apps/dgiot_matlab/Makefile new file mode 100644 index 00000000..1447287a --- /dev/null +++ b/apps/dgiot_matlab/Makefile @@ -0,0 +1,22 @@ +PROJECT = dgiot_matlab +PROJECT_DESCRIPTION = dgiot_matlab Plugin +PROJECT_VERSION = 1.5.4 + +CUR_BRANCH := $(shell git branch | grep -e "^*" | cut -d' ' -f 2) +BRANCH := $(if $(filter $(CUR_BRANCH), master develop), $(CUR_BRANCH), develop) + +BUILD_DEPS = emqx cuttlefish +dep_emqx = git-emqx https://github.com/emqx/emqx $(BRANCH) +dep_cuttlefish = git-emqx https://github.com/emqx/cuttlefish v2.2.1 + +DIALYZER_DIRS := ebin/ +DIALYZER_OPTS := --verbose --statistics -Werror_handling \ + -Wrace_conditions #-Wunmatched_returns + +ERLC_OPTS += +'{parse_transform, lager_transform}' + + +include erlang.mk + +app.dgiot_group:: + ./deps/cuttlefish/cuttlefish -l info -e etc/ -c etc/dgiot_matlab.conf -i priv/dgiot_matlab.schema -d data diff --git a/apps/dgiot_matlab/README.md b/apps/dgiot_matlab/README.md new file mode 100644 index 00000000..3cab723f --- /dev/null +++ b/apps/dgiot_matlab/README.md @@ -0,0 +1,5 @@ +# dgiot_matlab + 云端服务通过tcp远程连接本地计算matlab计算节点来进行分布式计算 + +# 详细步骤 + diff --git a/apps/dgiot_matlab/erlang.mk b/apps/dgiot_matlab/erlang.mk new file mode 100644 index 00000000..8930dfc4 --- /dev/null +++ b/apps/dgiot_matlab/erlang.mk @@ -0,0 +1 @@ +include ../../erlang.mk diff --git a/apps/dgiot_matlab/etc/dgiot_matlab.conf b/apps/dgiot_matlab/etc/dgiot_matlab.conf new file mode 100644 index 00000000..f704a5aa --- /dev/null +++ b/apps/dgiot_matlab/etc/dgiot_matlab.conf @@ -0,0 +1,7 @@ +##-------------------------------------------------------------------- +## dgiot_matlab Plugin +##-------------------------------------------------------------------- + +iot_dgiot_matlab.listener = 51888 + +iot_dgiot_matlab.heartbeat = 20 diff --git a/apps/dgiot_matlab/include/dgiot_matlab.hrl b/apps/dgiot_matlab/include/dgiot_matlab.hrl new file mode 100644 index 00000000..9ae6e10e --- /dev/null +++ b/apps/dgiot_matlab/include/dgiot_matlab.hrl @@ -0,0 +1,25 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2020 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. +%%-------------------------------------------------------------------- + +-define(MATLAB, <<"MATLAB">>). +-record(state, { + id, + env = #{}, + dtuaddr = <<>>, + step = login, + ref = undefined, + search = <<"quick">> +}). diff --git a/apps/dgiot_matlab/priv/dgiot_matlab.metrics b/apps/dgiot_matlab/priv/dgiot_matlab.metrics new file mode 100644 index 00000000..74427fa3 --- /dev/null +++ b/apps/dgiot_matlab/priv/dgiot_matlab.metrics @@ -0,0 +1,20 @@ +[ + { + "name": "matlab_login", + "help": "服务收到登陆报文统计", + "type": "gauge", + "labels": [] + }, + { + "name": "matlab_login_ack", + "help": "服务发送注册报文统计", + "type": "gauge", + "labels": [] + }, + { + "name": "matlab_register", + "help": "服务收到注册报文统计", + "type": "gauge", + "labels": [] + } +] diff --git a/apps/dgiot_matlab/priv/dgiot_matlab.schema b/apps/dgiot_matlab/priv/dgiot_matlab.schema new file mode 100644 index 00000000..11367ccd --- /dev/null +++ b/apps/dgiot_matlab/priv/dgiot_matlab.schema @@ -0,0 +1,10 @@ +{mapping, "iot_dgiot_matlab.listener", "dgiot_matlab.listener", [ + {default, 51888}, + {datatype, [integer, ip]} +]}. + + +{mapping, "iot_dgiot_matlab.heartbeat", "dgiot_matlab.heartbeat", [ + {default, 180}, + {datatype, integer} +]}. diff --git a/apps/dgiot_matlab/priv/swagger/swagger_matlab.json b/apps/dgiot_matlab/priv/swagger/swagger_matlab.json new file mode 100644 index 00000000..d6c7d228 --- /dev/null +++ b/apps/dgiot_matlab/priv/swagger/swagger_matlab.json @@ -0,0 +1,11 @@ +{ + "definitions": {}, + "paths": { + }, + "tags": [ + { + "description": "工业计算", + "name": "MATLAB" + } + ] +} diff --git a/apps/dgiot_matlab/rebar.config b/apps/dgiot_matlab/rebar.config new file mode 100644 index 00000000..6d6bfe1a --- /dev/null +++ b/apps/dgiot_matlab/rebar.config @@ -0,0 +1,3 @@ +{deps, [ + +]}. diff --git a/apps/dgiot_matlab/src/dgiot_matlab.app.src b/apps/dgiot_matlab/src/dgiot_matlab.app.src new file mode 100644 index 00000000..a579123f --- /dev/null +++ b/apps/dgiot_matlab/src/dgiot_matlab.app.src @@ -0,0 +1,11 @@ +{application,dgiot_matlab, + [{description,"DGIOT MATLAB"}, + {vsn,"4.0.0"}, + {registered,[]}, + {mod,{dgiot_matlab_app,[]}}, + {applications,[kernel,stdlib]}, + {env,[]}, + {modules,[]}, + {maintainers,[]}, + {licenses,["Apache 2.0"]}, + {links,[]}]}. diff --git a/apps/dgiot_matlab/src/dgiot_matlab.app.src.script b/apps/dgiot_matlab/src/dgiot_matlab.app.src.script new file mode 100644 index 00000000..d49efe8a --- /dev/null +++ b/apps/dgiot_matlab/src/dgiot_matlab.app.src.script @@ -0,0 +1,25 @@ +%%-*- mode: erlang -*- +%% .app.src.script + +RemoveLeadingV = + fun(Tag) -> + case re:run(Tag, "v\[0-9\]+\.\[0-9\]+\.*") of + nomatch -> + Tag; + {match, _} -> + %% if it is a version number prefixed by 'v' then remove the 'v' + "v" ++ Vsn = Tag, + Vsn + end + end, + +case os:getenv("EMQX_DEPS_DEFAULT_VSN") of + false -> CONFIG; % env var not defined + [] -> CONFIG; % env var set to empty string + Tag -> + [begin + AppConf0 = lists:keystore(vsn, 1, AppConf, {vsn, RemoveLeadingV(Tag)}), + {application, App, AppConf0} + end || Conf = {application, App, AppConf} <- CONFIG] +end. + diff --git a/apps/dgiot_matlab/src/dgiot_matlab.erl b/apps/dgiot_matlab/src/dgiot_matlab.erl new file mode 100644 index 00000000..811f94c9 --- /dev/null +++ b/apps/dgiot_matlab/src/dgiot_matlab.erl @@ -0,0 +1,41 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2020 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. +%%-------------------------------------------------------------------- + +%% @doc dgiot_matlab Protocol +-module(dgiot_matlab). +-include("dgiot_matlab.hrl"). +-include_lib("dgiot/include/logger.hrl"). +-export([ + create_matlab/3 +]). + +-define(APP, ?MODULE). + +%%新设备 +create_matlab(DtuAddr, ChannelId, DTUIP) -> + {ProductId, Acl, _Properties} = dgiot_data:get({matlab, ChannelId}), + Requests = #{ + <<"devaddr">> => DtuAddr, + <<"name">> => <<"MATLAB_", DtuAddr/binary>>, + <<"ip">> => DTUIP, + <<"isEnable">> => true, + <<"product">> => ProductId, + <<"ACL">> => Acl, + <<"status">> => <<"ONLINE">>, + <<"brand">> => <<"MATLAB", DtuAddr/binary>>, + <<"devModel">> => <<"MATLAB">> + }, + dgiot_device:create_device(Requests). diff --git a/apps/dgiot_matlab/src/dgiot_matlab_app.erl b/apps/dgiot_matlab/src/dgiot_matlab_app.erl new file mode 100644 index 00000000..6c066238 --- /dev/null +++ b/apps/dgiot_matlab/src/dgiot_matlab_app.erl @@ -0,0 +1,35 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2020 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. +%%-------------------------------------------------------------------- + +%% @doc dgiot_matlab Application +-module(dgiot_matlab_app). +-emqx_plugin(?MODULE). +-behaviour(application). + +%% Application callbacks +-export([start/2, stop/1]). + + +%%-------------------------------------------------------------------- +%% Application callbacks +%%-------------------------------------------------------------------- + +start(_StartType, _StartArgs) -> + {ok, Sup} = dgiot_matlab_sup:start_link(), + {ok, Sup}. + +stop(_State) -> + ok. diff --git a/apps/dgiot_matlab/src/dgiot_matlab_channel.erl b/apps/dgiot_matlab/src/dgiot_matlab_channel.erl new file mode 100644 index 00000000..ab1cd97f --- /dev/null +++ b/apps/dgiot_matlab/src/dgiot_matlab_channel.erl @@ -0,0 +1,165 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2020 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. +%%-------------------------------------------------------------------- + +%%# 用户场景 +%%从用户到设备,主要需要解决如下几个问题: +%%+ 人与设备的关系,User基于流动性,权限系统里一般不会直接绑定User与Device的权限关系,中间会通过Department(Role)来间接绑定ACL +%%+ 设备与设备的关系,设备与设备之间有可能存在真实的关系,例如DTU与Meter,也可能只有一种虚拟关系,例如Group与DTU,属于因工程需要,临时拉群 +%%+ 对具体设备来说,1、需要一个UID来表征身份;2、需要一个route来表征关系;3、需要多个tag来表征特征 +%%+ 重点讨论Meter、DTU和TCP Server的交互过程 +%% +%%| No.|名称| Meter | DTU | TCP Server | 说明 | +%%| --| ---- | ------- | ------ | ----------- |-----------| +%%|1 |连接 | | send -> [IP] | ack <-- [IP] | 必选 | +%%|2 |登陆 | | send -> [DtuAddr] | ack <-- [DtuAddr] | 可选,可用IP代替| +%%|3 |扫串口 | ack-> [485] | send <-- [search 485] | send <--[search 485] | 可选,有档案可免 | +%%|4 |扫modbus | ack-> [modbus]| send <-- [search modbus] | send <--[search modbus] |可选,有档案可免 | +%%|5 |扫表 | ack-> [Meter Addr]| send <-- [search meter] | send <--[search meter] |可选,有档案可免 | +%%|6 |抄表 | ack-> [Meter Data]| send <-- [read meter] | send <--[read meter] |必选 | +%%|7 |写表 | ack-> [Meter Control]| send <-- [write meter] | send <--[write meter] |可选 | +%%|8 |登出 | | send -> [DtuAddr] | ack <-- [DtuAddr] |可选 | +%%|9 |断开 | | send -> [IP] | do_something |必选 | +%% + +-module(dgiot_matlab_channel). +-behavior(dgiot_channelx). +-author("johnliu"). +-include_lib("dgiot/include/dgiot_socket.hrl"). +-include_lib("dgiot/include/logger.hrl"). +-include("dgiot_matlab.hrl"). +-define(TYPE, <<"MATLAB">>). +-define(MAX_BUFF_SIZE, 1024). +-define(SECS, [5, 5 * 60]). +-define(JIAOSHI, 60 * 10 * 1000). + +%% API +-export([start/2]). + +%% Channel callback +-export([init/3, handle_init/1, handle_event/3, handle_message/2, stop/3]). + +%% 注册通道类型 +-channel(?TYPE). +-channel_type(#{ + type => 1, + title => #{ + zh => <<"MATLAB采集通道"/utf8>> + }, + description => #{ + zh => <<"MATLAB采集通道"/utf8>> + } +}). +%% 注册通道参数 +-params(#{ + <<"port">> => #{ + order => 1, + type => integer, + required => true, + default => 61888, + title => #{ + zh => <<"端口"/utf8>> + }, + description => #{ + zh => <<"侦听端口"/utf8>> + } + }, + <<"search">> => #{ + order => 2, + type => enum, + required => false, + default => <<"quick"/utf8>>, + enum => [<<"nosearch">>, <<"quick">>, <<"normal">>], + title => #{ + zh => <<"搜表模式"/utf8>> + }, + description => #{ + zh => <<"搜表模式:nosearch|quick|normal"/utf8>> + } + }, + <<"ico">> => #{ + order => 102, + type => string, + required => false, + default => <<"http://dgiot-1253666439.cos.ap-shanghai-fsi.myqcloud.com/shuwa_tech/zh/product/dgiot/channel/matlab.jpg">>, + title => #{ + en => <<"channel ICO">>, + zh => <<"通道ICO"/utf8>> + }, + description => #{ + en => <<"channel ICO">>, + zh => <<"通道ICO"/utf8>> + } + } +}). + + +start(ChannelId, ChannelArgs) -> + dgiot_channelx:add(?TYPE, ChannelId, ?MODULE, ChannelArgs). + +%% 通道初始化 +init(?TYPE, ChannelId, #{ + <<"port">> := Port, + <<"product">> := Products, + <<"search">> := Search}) -> + lists:map(fun(X) -> + case X of + {ProductId, #{<<"ACL">> := Acl, <<"nodeType">> := 1}} -> + {ok, #{<<"thing">> := #{<<"properties">> := Properties}}} = dgiot_device:lookup_prod(ProductId), + dgiot_data:insert({matlab, ChannelId}, {ProductId, Acl, Properties}); + _ -> + ?LOG(info,"X ~p", [X]), + pass + end + end, Products), + dgiot_data:set_consumer(ChannelId, 20), + State = #state{ + id = ChannelId, + search = Search + }, + {ok, State, dgiot_matlab_tcp:start(Port, State)}; + +init(?TYPE, _ChannelId, _Args) -> + {ok, #{}, #{}}. + +handle_init(State) -> + {ok, State}. + +%% 通道消息处理,注意:进程池调用 +handle_event(_EventId, _Event, State) -> + {ok, State}. + +% SELECT clientid, payload, topic FROM "meter" +% SELECT clientid, disconnected_at FROM "$events/client_disconnected" WHERE username = 'dgiot' +% SELECT clientid, connected_at FROM "$events/client_connected" WHERE username = 'dgiot' +handle_message({rule, #{clientid := DtuAddr, connected_at := _ConnectedAt}, #{peername := PeerName} = _Context}, #state{id = ChannelId} = State) -> + ?LOG(error,"DtuAddr ~p PeerName ~p",[DtuAddr,PeerName] ), + DTUIP = dgiot_utils:get_ip(PeerName), + dgiot_matlab:create_matlab(DtuAddr, ChannelId, DTUIP), + {ok, State}; + +handle_message({rule, #{clientid := DevAddr, disconnected_at := _DisconnectedAt}, _Context}, State) -> + ?LOG(error,"DevAddr ~p ",[DevAddr] ), + {ok, State}; + +handle_message({rule, #{clientid := DevAddr, payload := Payload, topic := _Topic}, _Msg}, #state{id = ChannelId} = State) -> + ?LOG(error,"DevAddr ~p Payload ~p ChannelId ~p",[DevAddr,Payload,ChannelId] ), + {ok, State}; + +handle_message(_Message, State) -> + {ok, State}. + +stop(_ChannelType, _ChannelId, _State) -> + ok. diff --git a/apps/dgiot_matlab/src/dgiot_matlab_handler.erl b/apps/dgiot_matlab/src/dgiot_matlab_handler.erl new file mode 100644 index 00000000..8ddc3110 --- /dev/null +++ b/apps/dgiot_matlab/src/dgiot_matlab_handler.erl @@ -0,0 +1,90 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2020 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(dgiot_matlab_handler). +-author("johnliu"). +-behavior(dgiot_rest). +-include_lib("dgiot/include/logger.hrl"). + +%% API +-export([swagger_meter/0]). +-export([handle/4, check_auth/3]). + +%% API描述 +%% 支持二种方式导入 +%% 示例: +%% 1. Metadata为map表示的JSON, +%% dgiot_http_server:bind(<<"/meter">>, ?MODULE, [], Metadata) +%% 2. 从模块的priv/swagger/下导入 +%% dgiot_http_server:bind(<<"/swagger_meter.json">>, ?MODULE, [], priv) +swagger_meter() -> + [ + dgiot_http_server:bind(<<"/swagger_meter.json">>, ?MODULE, [], priv) + ]. + +check_auth(_OperationID, _Args, Req) -> + {true, #{}, Req}. + +%%%=================================================================== +%%% 请求处理 +%%% 如果登录, Context 内有 <<"user">>, version +%%%=================================================================== + +-spec handle(OperationID :: atom(), Args :: map(), Context :: map(), Req :: dgiot_req:req()) -> + {Status :: dgiot_req:http_status(), Body :: map()} | + {Status :: dgiot_req:http_status(), Headers :: map(), Body :: map()} | + {Status :: dgiot_req:http_status(), Headers :: map(), Body :: map(), Req :: dgiot_req:req()}. + +handle(OperationID, Args, Context, Req) -> + Headers = #{}, + case catch do_request(OperationID, Args, Context, Req) of + {ErrType, Reason} when ErrType == 'EXIT'; ErrType == error -> + Err = case is_binary(Reason) of + true -> Reason; + false -> dgiot_utils:format("~p", [Reason]) + end, + {500, Headers, #{<<"error">> => Err}}; + ok -> + ?LOG(debug,"do request: ~p, ~p ->ok ~n", [OperationID, Args]), + {200, Headers, #{}, Req}; + {ok, Res} -> + ?LOG(debug,"do request: ~p, ~p ->~p~n", [OperationID, Args, Res]), + {200, Headers, Res, Req}; + {Status, Res} -> + ?LOG(debug,"do request: ~p, ~p ->~p~n", [OperationID, Args, Res]), + {Status, Headers, Res, Req}; + {Status, NewHeaders, Res} -> + ?LOG(debug,"do request: ~p, ~p ->~p~n", [OperationID, Args, Res]), + {Status, maps:merge(Headers, NewHeaders), Res, Req} + end. + + +%%%=================================================================== +%%% 内部函数 Version:API版本 +%%%=================================================================== + +%% 服务器不支持的API接口 +do_request(_OperationId, _Args, _Context, _Req) -> + ?LOG(info,"_OperationId:~p~n", [_OperationId]), + {error, <<"Not Allowed.">>}. + + + + + + + + + diff --git a/apps/dgiot_matlab/src/dgiot_matlab_sup.erl b/apps/dgiot_matlab/src/dgiot_matlab_sup.erl new file mode 100644 index 00000000..35570172 --- /dev/null +++ b/apps/dgiot_matlab/src/dgiot_matlab_sup.erl @@ -0,0 +1,44 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2020 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. +%%-------------------------------------------------------------------- + +%% @doc dgiot_matlab supervisor +-module(dgiot_matlab_sup). + +-behaviour(supervisor). + +%% API +-export([start_link/0]). + +%% Supervisor callbacks +-export([init/1]). + +%% Helper macro for declaring children of supervisor +-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}). + +%%-------------------------------------------------------------------- +%% API functions +%%-------------------------------------------------------------------- + +start_link() -> + supervisor:start_link({local, ?MODULE}, ?MODULE, []). + +%%-------------------------------------------------------------------- +%% Supervisor callbacks +%%-------------------------------------------------------------------- + +init([]) -> + {ok, { {one_for_all, 5, 10}, []}}. + diff --git a/apps/dgiot_matlab/src/dgiot_matlab_tcp.erl b/apps/dgiot_matlab/src/dgiot_matlab_tcp.erl new file mode 100644 index 00000000..1e40c659 --- /dev/null +++ b/apps/dgiot_matlab/src/dgiot_matlab_tcp.erl @@ -0,0 +1,63 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2020 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(dgiot_matlab_tcp). +-author("johnliu"). +-include_lib("dgiot/include/dgiot_socket.hrl"). +-include_lib("dgiot/include/logger.hrl"). +-include("dgiot_matlab.hrl"). +%% API +-export([start/2]). + +%% TCP callback +-export([init/1, handle_info/2, handle_cast/2, handle_call/3, terminate/2, code_change/3]). + +start(Port, State) -> + dgiot_tcp_server:child_spec(?MODULE, dgiot_utils:to_int(Port), State). + +%% ======================= +%% tcp server start +%% {ok, State} | {stop, Reason} +init(TCPState) -> + dgiot_metrics:inc(dgiot_matlab, <<"matlab_login">>, 1), + {ok, TCPState}. + +%%设备登录报文,登陆成功后,开始搜表 +handle_info({tcp, DtuAddr}, #tcp{socket = Socket, state = #state{id = ChannelId, dtuaddr = <<>>} = State} = TCPState) -> + DTUIP = dgiot_utils:get_ip(Socket), + HexDtuAddr = dgiot_utils:binary_to_hex(DtuAddr), + dgiot_matlab:create_matlab(HexDtuAddr, ChannelId, DTUIP), + {_DtuProductId, _, _} = dgiot_data:get({matlab, ChannelId}), + ?LOG(info,"DtuAddr ~p _DtuProductId ~p",[DtuAddr,_DtuProductId]), + {noreply, TCPState#tcp{buff = <<>>, state = State#state{dtuaddr = HexDtuAddr}}}; + + +%% 异常报文丢弃 +%% {stop, TCPState} | {stop, Reason} | {ok, TCPState} | ok | stop +handle_info(_Info, TCPState) -> + {noreply, TCPState}. + +handle_call(_Msg, _From, TCPState) -> + {reply, ok, TCPState}. + +handle_cast(_Msg, TCPState) -> + {noreply, TCPState}. + +terminate(_Reason, _TCPState) -> + dgiot_metrics:dec(dgiot_matlab, <<"matlab_login">>, 1), + ok. + +code_change(_OldVsn, TCPState, _Extra) -> + {ok, TCPState}. diff --git a/apps/dgiot_matlab/test/dgiot_matlab_SUITE.erl b/apps/dgiot_matlab/test/dgiot_matlab_SUITE.erl new file mode 100644 index 00000000..39392680 --- /dev/null +++ b/apps/dgiot_matlab/test/dgiot_matlab_SUITE.erl @@ -0,0 +1,9 @@ + +-module(dgiot_matlab_SUITE). + +-compile(export_all). + +all() -> []. + +groups() -> []. + diff --git a/data/loaded_plugins.tmpl b/data/loaded_plugins.tmpl index 74ee0170..568c3337 100644 --- a/data/loaded_plugins.tmpl +++ b/data/loaded_plugins.tmpl @@ -19,6 +19,7 @@ {dgiot_topo, {{enable_plugin_dgiot_topo}}}. {dgiot_opc, {{enable_plugin_dgiot_opc}}}. {dgiot_meter, {{enable_plugin_dgiot_meter}}}. +{dgiot_matlab, {{enable_plugin_dgiot_matlab}}}. {dgiot_niisten, {{enable_plugin_dgiot_niisten}}}. {dgiot_modbus, {{enable_plugin_dgiot_modbus}}}. {dgiot_group, {{enable_plugin_dgiot_group}}}. diff --git a/rebar.config.erl b/rebar.config.erl index 1cffb873..6b2f8375 100644 --- a/rebar.config.erl +++ b/rebar.config.erl @@ -197,6 +197,7 @@ overlay_vars_rel(RelType) -> , {enable_plugin_dgiot_topo, true} , {enable_plugin_dgiot_opc, true} , {enable_plugin_dgiot_meter, true} + , {enable_plugin_dgiot_matlab, true} , {enable_plugin_dgiot_niisten, true} , {enable_plugin_dgiot_modbus, true} , {enable_plugin_dgiot_group, true} @@ -320,6 +321,7 @@ relx_plugin_apps_per_rel(cloud) -> , dgiot_topo , dgiot_opc , dgiot_meter + , dgiot_matlab , dgiot_niisten , dgiot_modbus , dgiot_group