feat: rule, install.sh

This commit is contained in:
dawnwinterLiu 2022-09-27 17:34:11 +08:00
parent 7de9b2715c
commit fb6df0d156
2 changed files with 41 additions and 26 deletions

View File

@ -30,7 +30,7 @@
%% API
-export([swagger_rule/0]).
-export([handle/4, sysc_rules/0, save_rule_to_dict/2, device_sql/4, create_rules/5]).
-export([handle/4, sysc_rules/0, save_rule_to_dict/3, device_sql/4, create_rules/6]).
%% API描述
%%
@ -104,7 +104,7 @@ do_request(get_rule_id, #{<<"id">> := RuleID}, _Context, _Req) ->
%% OperationId:put_rules_id
%% :PUT /iotapi/rule/:{id}
do_request(put_rule_id, #{<<"id">> := RuleID} = Params, _Context, _Req) ->
save_rule_to_dict(RuleID, Params),
save_rule_to_dict(RuleID, Params, #{}),
emqx_rule_engine_api:update_rule(#{id => RuleID}, maps:to_list(maps:without([<<"id">>], Params)));
%% Rule : :
@ -139,7 +139,7 @@ do_request(post_rules, Params, _Context, _Req) ->
R = emqx_rule_engine_api:create_rule(#{}, maps:to_list(Params#{<<"actions">> => NewActions})),
case R of
{ok, #{data := #{id := RuleID}}} ->
save_rule_to_dict(RuleID, Params#{<<"actions">> => NewActions});
save_rule_to_dict(RuleID, Params#{<<"actions">> => NewActions}, #{});
_ -> pass
end,
R;
@ -174,10 +174,10 @@ do_request(get_actions, _Args, _Context, _Req) ->
%%do_request(post_rulesql, #{<<"select">> := Select, <<"from">> := From, <<"where">> := Where, <<"method">> := Method}, _Context, _Req) ->
%% device_sql(Select, From, Where, Method);
do_request(post_rulesql, #{<<"trigger">> := Trigger, <<"ruleid">> := Ruleid, <<"productid">> := ProductId, <<"description">> := Description}, _Context, _Req) ->
do_request(post_rulesql, #{<<"trigger">> := Trigger, <<"ruleid">> := Ruleid, <<"productid">> := ProductId, <<"description">> := Description} = Args, _Context, _Req) ->
%% device_sql(Select, From, Where, Method);
%% sql_tpl(Trigger, Condition, Action, Ruleid, Description);
sql_tpl(Trigger, Ruleid, ProductId, Description);
sql_tpl(Trigger, Ruleid, ProductId, Description, Args);
do_request(get_actions_id, #{<<"id">> := RuleID}, _Context, _Req) ->
@ -220,7 +220,7 @@ do_request(_OperationId, _Args, _Context, _Req) ->
%%doc-api https://help.aliyun.com/document_detail/73736.htm?spm=a2c4g.11186623.0.0.353d7a48CvEOwF#concept-ap3-lql-b2b
%% SELECT payload.electricity as electricity FROM "$dg/alarm/94656917ab/157d0ff60f/#" where electricity > 20
sql_tpl(Trigger, Ruleid, ProductId, Description) ->
sql_tpl(Trigger, Ruleid, ProductId, Description, Args) ->
SELECT = generateSelect(Trigger),
FROM = generateFrom(ProductId, Trigger),
WHERE = generateWhere(Trigger),
@ -233,7 +233,7 @@ sql_tpl(Trigger, Ruleid, ProductId, Description) ->
" \"", FROM/binary, "\"", "\r\n",
"WHERE", "\r\n ",
WHERE/binary>>,
create_rules(Ruleid, ChannelId, Description, DefaultSql, FROM),
create_rules(Ruleid, ChannelId, Description, DefaultSql, FROM, Args),
{ok, #{<<"template">> => DefaultSql}}.
%% sql模板
@ -282,10 +282,7 @@ device_sql(Select, From, Where, _Method) ->
{ok, #{<<"template">> => DefaultSql}}.
%% cron表达式生成sql模板
save_rule_to_dict(RuleID, Params) ->
save_rule_to_dict(RuleID, Params, Args) ->
Rule =
case emqx_rule_engine_api:show_rule(#{id => RuleID}, []) of
{ok, #{message := <<"Not Found">>}} ->
@ -301,7 +298,7 @@ save_rule_to_dict(RuleID, Params) ->
<<"title">> => <<"Rule">>,
<<"key">> => RuleID,
<<"type">> => <<"ruleengine">>,
<<"data">> => #{<<"rule">> => jsx:encode(Rule)}
<<"data">> => #{<<"args">> => Args, <<"rule">> => jsx:encode(Rule)}
},
dgiot_data:insert(?DGIOT_RUlES, Dict),
ObjectId = dgiot_parse_id:get_dictid(RuleID, <<"ruleengine">>, <<"Rule">>, <<"Rule">>),
@ -498,7 +495,7 @@ generateWhere(Trigger) ->
end
end, <<"">>, maps:get(<<"items">>, Trigger, [])).
create_rules(RuleID, ChannelId, Description, Rawsql, Target_topic) ->
create_rules(RuleID, ChannelId, Description, Rawsql, Target_topic, Args) ->
emqx_rule_engine_api:create_resource(#{},
[
{<<"id">>, <<"resource:", ChannelId/binary>>},
@ -530,14 +527,14 @@ create_rules(RuleID, ChannelId, Description, Rawsql, Target_topic) ->
ObjectId = dgiot_parse_id:get_dictid(RuleID, <<"ruleengine">>, <<"Rule">>, <<"Rule">>),
case dgiot_parse:get_object(<<"Dict">>, ObjectId) of
{ok, _} ->
dgiot_rule_handler:save_rule_to_dict(RuleID, Params);
dgiot_rule_handler:save_rule_to_dict(RuleID, Params, Args);
_ ->
R = emqx_rule_engine_api:create_rule(#{}, maps:to_list(Params)),
case R of
{ok, #{data := #{id := EmqxRuleId}}} ->
dgiot_data:delete(?DGIOT_RUlES, EmqxRuleId),
emqx_rule_engine_api:delete_rule(#{id => EmqxRuleId}, []),
dgiot_rule_handler:save_rule_to_dict(RuleID, Params);
dgiot_rule_handler:save_rule_to_dict(RuleID, Params, Args);
_ -> pass
end
end.

View File

@ -4,7 +4,7 @@
export PATH=$PATH:/usr/local/bin
function help() {
echo "Usage: `basename $0` -v [single | cluster | devops | ci] -s [dgiot_n] -p [your_dgiot_plugin] -d [your_domain_name] -m [dgiotmd5] -e [pg_eip] -a [pg_auth]"
echo "Usage: `basename $0` -v [single | cluster | devops | ci] -s [dgiot_n] -p [your_dgiot_plugin] -d [your_domain_name] -m [dgiotmd5] -e [pg_eip] -a [pg_auth] -n [islanip]"
exit 0
}
@ -160,6 +160,10 @@ function get_processor() {
function get_wanip() {
ping -c2 baidu.com &> /dev/null
wlanip=`curl whatismyip.akamai.com`
echo -e "`date +%F_%T` $LINENO: ${GREEN} islanip=${islanip} ${NC}"
if [ "${islanip}" == "true" ]; then
wlanip=${lanip}
fi
echo -e "`date +%F_%T` $LINENO: ${GREEN} wlanip: ${wlanip}${NC}"
}
@ -515,8 +519,11 @@ function deploy_postgres() {
}
function restore_parse_data() {
### 下载dgiot_parse_server初始数据p
wget ${fileserver}/parse_4.0.sql.tar.gz -O ${install_dir}/dgiot_pg_writer/parse_4.0.sql.tar.gz &> /dev/null
### 下载dgiot_parse_server初始数据
if [ ! -f ${script_dir}/parse_4.0.sql.tar.gz ]; then
wget ${fileserver}/parse_4.0.sql.tar.gz -O ${script_dir}/parse_4.0.sql.tar.gz &> /dev/null
fi
cp ${script_dir}/parse_4.0.sql.tar.gz ${install_dir}/dgiot_pg_writer/
cd ${install_dir}/dgiot_pg_writer/
tar xvf parse_4.0.sql.tar.gz &> /dev/null
@ -542,7 +549,7 @@ function install_parse_server() {
fi
###下载dgiot_parse_server软件
if [ ! -f ${install_dir}/dgiot_parse_server.tar.gz ]; then
if [ ! -f ${script_dir}/dgiot_parse_server.tar.gz ]; then
wget ${fileserver}/dgiot_parse_server.tar.gz -O ${script_dir}/dgiot_parse_server.tar.gz &> /dev/null
fi
@ -646,7 +653,7 @@ function deploy_tdengine_server() {
systemctl start taosadapter
${csudo} systemctl enable taosadapter &> /dev/null
echo -e "`date +%F_%T` $LINENO: ${GREEN} tdengine_server start success${NC}"
install_dgiot_tdengine_mqtt
# install_dgiot_tdengine_mqtt
}
## 3.3 部署dgiot_tdengine_mqtt桥接服务
@ -654,7 +661,7 @@ function install_dgiot_tdengine_mqtt() {
## 3.2.setup mosquitto
### 1.2.5 下载mosquitto
if [ ! -f ${script_dir}/mosquitto-1.6.7.tar.gz ]; then
wget ${fileserver}/mosquitto-1.6.7.tar.gz -O ${script_dir}/mosquitto-1.6.7.tar.gz &> /dev/null
wget ${fileserver}/mosquitto-1.6.7.tar.gz -O ${script_dir}/mosquitto-1.6.7.tar.gz &> /dev/null
fi
if [ -d ${script_dir}/mosquitto-1.6.7/ ]; then
rm ${script_dir}/mosquitto-1.6.7/ -rf
@ -663,7 +670,7 @@ function install_dgiot_tdengine_mqtt() {
tar xvf mosquitto-1.6.7.tar.gz &> /dev/null
cd ${script_dir}/mosquitto-1.6.7
if [ -f /usr/lib/libmosquitto.so.1 ]; then
rm /usr/lib/libmosquitto.so.1 -rf
rm /usr/lib/libmosquitto.so.1 -rf
fi
make -j${processor} &> /dev/null
@ -673,9 +680,12 @@ function install_dgiot_tdengine_mqtt() {
cd ${script_dir}/
rm ${script_dir}/mosquitto-1.6.7/ -rf
#wget ${fileserver}/dgiot_tdengine_mqtt -O /usr/sbin/dgiot_tdengine_mqtt &> /dev/null
rm ${script_dir}/dgiot_tdengine_mqtt/ -rf
#rm ${script_dir}/dgiot_tdengine_mqtt/ -rf
rm /usr/sbin/dgiot_tdengine_mqtt -rf
git clone https://gitee.com/dgiiot/dgiot_tdengine_mqtt.git &> /dev/null
if [ -d ${script_dir}/dgiot_tdengine_mqtt/ ]; then
echo -e "`date +%F_%T` $LINENO: ${GREEN} git clone dgiot_tdengine_mqtt ${NC}"
git clone https://gitee.com/dgiiot/dgiot_tdengine_mqtt.git &> /dev/null
fi
cd ${script_dir}/dgiot_tdengine_mqtt/c/
make &> /dev/null
cp ${script_dir}/dgiot_tdengine_mqtt/c/dgiot_tdengine_mqtt /usr/sbin/dgiot_tdengine_mqtt
@ -884,7 +894,11 @@ function install_dgiot() {
fi
if [ ! -f ${install_dir}/go_fastdfs/files/package/${software}.tar.gz ]; then
wget ${fileserver}/${software}.tar.gz -O ${install_dir}/go_fastdfs/files/package/${software}.tar.gz &> /dev/null
if [ ! -f ${script_dir}/${software}.tar.gz ]; then
wget ${fileserver}/${software}.tar.gz -O ${install_dir}/go_fastdfs/files/package/${software}.tar.gz &> /dev/null
else
cp ${script_dir}/${software}.tar.gz ${install_dir}/go_fastdfs/files/package/
fi
fi
cd ${install_dir}/go_fastdfs/files/package/
tar xf ${software}.tar.gz
@ -1491,8 +1505,9 @@ plugin="dgiot" # [dgiot | dgiot_your_plugin]
dgiotmd5="5def10e33d5f5b7fabd362249a58f36b" # [dgiotmd5]
pg_eip="changeyourip" # [datanode_eip]
pg_auth='changeyourpassword' # [pg_auth]
islanip="false" # [islanip]
while getopts "v:s:p:m:d:e:a:" arg
while getopts "v:s:p:m:d:e:a:n:" arg
do
case $arg in
v)
@ -1529,6 +1544,9 @@ do
echo -e "`date +%F_%T` $LINENO: ${GREEN} pg_auth=$OPTARG${NC}"
pg_auth=$(echo $OPTARG)
;;
n)
islanip=$(echo $OPTARG)
;;
?) #unknow option
help
;;