From 059fc6b53f8fd46b7e7b3d70acd0cf070a5b7de8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E7=A4=BC=E6=88=90?= Date: Mon, 6 Mar 2023 16:39:45 +0800 Subject: [PATCH] fix --- apps/iot/include/iot.hrl | 4 + .../iot/src/http_handler/http_iot_handler.erl | 104 ++++++++++++++++++ ...ndler.erl => iot_mqtt_message_handler.erl} | 26 ++++- rebar.lock | 76 +++++++++++++ 4 files changed, 208 insertions(+), 2 deletions(-) rename apps/iot/src/mqtt_handler/{iot_message_handler.erl => iot_mqtt_message_handler.erl} (85%) create mode 100644 rebar.lock diff --git a/apps/iot/include/iot.hrl b/apps/iot/include/iot.hrl index 9374ba4..f9c1b4d 100644 --- a/apps/iot/include/iot.hrl +++ b/apps/iot/include/iot.hrl @@ -30,6 +30,8 @@ -record(memory_metric, { %% 使用量 used = 0, + %% 空余 + free = 0, %% 总量 total = 0 }). @@ -38,6 +40,8 @@ -record(disk_metric, { %% 使用量 used = 0, + %% 空余 + free = 0, %% 总量 total = 0 }). diff --git a/apps/iot/src/http_handler/http_iot_handler.erl b/apps/iot/src/http_handler/http_iot_handler.erl index c4de7a4..316d576 100644 --- a/apps/iot/src/http_handler/http_iot_handler.erl +++ b/apps/iot/src/http_handler/http_iot_handler.erl @@ -16,6 +16,110 @@ handle_request("GET", "/api/booking", _, _Params) -> {ok, 200, iot_util:json_data(<<"success">>)}; +%% 下发参数 +handle_request("POST", "/iot/send_params", _, PostParams = #{<<"host_id">> := HostId, <<"service_name">> := ServiceName, <<"params">> := Params}) -> + lager:debug("body is: ~p", [PostParams]), + + case host_model:activate(HostId) of + {error, Reason} when is_binary(Reason) -> + {ok, 200, iot_util:json_error(404, Reason)}; + {ok, #host{aes = Aes}} -> + Reply = #{ + <<"t">> => 1, + <<"b">> => #{ + <<"t_id">> => iot_util:uuid() , + <<"to">> => ServiceName, + <<"t">> => 10, + <<"m">> => Params + } + }, + EncMsg = iot_cipher_aes:encrypt(Aes, Aes, Reply), + + iot_emqtt_client:publish(<<"clients.cmd.", HostId/binary>>, EncMsg, 1), + lager:debug("enc_reply is: ~p", [EncMsg]), + + {ok, 200, iot_util:json_data(<<"success">>)} + end; + +%% 下发采集项 +handle_request("POST", "/iot/send_metrics", _, + PostParams = #{<<"host_id">> := HostId, <<"service_name">> := ServiceName, <<"metrics">> := Metrics}) -> + + lager:debug("body is: ~p", [PostParams]), + + case host_model:activate(HostId) of + {error, Reason} when is_binary(Reason) -> + {ok, 200, iot_util:json_error(404, Reason)}; + {ok, #host{aes = Aes}} -> + Reply = #{ + <<"t">> => 2, + <<"b">> => #{ + <<"t_id">> => iot_util:uuid() , + <<"to">> => ServiceName, + <<"t">> => 10, + <<"m">> => Metrics + } + }, + EncMsg = iot_cipher_aes:encrypt(Aes, Aes, Reply), + + iot_emqtt_client:publish(<<"clients.cmd.", HostId/binary>>, EncMsg, 1), + lager:debug("enc_reply is: ~p", [EncMsg]), + + {ok, 200, iot_util:json_data(<<"success">>)} + end; + +%% 下发微服务 +handle_request("POST", "/iot/send_mirco_service", _, + PostParams = #{<<"host_id">> := HostId, <<"args">> := Args}) -> + + lager:debug("body is: ~p", [PostParams]), + + case host_model:activate(HostId) of + {error, Reason} when is_binary(Reason) -> + {ok, 200, iot_util:json_error(404, Reason)}; + {ok, #host{aes = Aes}} -> + Reply = #{ + <<"t">> => 3, + <<"b">> => #{ + <<"t_id">> => iot_util:uuid() , + <<"t">> => 10, + <<"m">> => Args + } + }, + EncMsg = iot_cipher_aes:encrypt(Aes, Aes, Reply), + + iot_emqtt_client:publish(<<"clients.cmd.", HostId/binary>>, EncMsg, 1), + lager:debug("enc_reply is: ~p", [EncMsg]), + + {ok, 200, iot_util:json_data(<<"success">>)} + end; + +%% 下发数据流图 +handle_request("POST", "/iot/send_data_flow", _, + PostParams = #{<<"host_id">> := HostId, <<"args">> := Args}) -> + + lager:debug("body is: ~p", [PostParams]), + + case host_model:activate(HostId) of + {error, Reason} when is_binary(Reason) -> + {ok, 200, iot_util:json_error(404, Reason)}; + {ok, #host{aes = Aes}} -> + Reply = #{ + <<"t">> => 4, + <<"b">> => #{ + <<"t_id">> => iot_util:uuid() , + <<"t">> => 10, + <<"m">> => Args + } + }, + EncMsg = iot_cipher_aes:encrypt(Aes, Aes, Reply), + + iot_emqtt_client:publish(<<"clients.cmd.", HostId/binary>>, EncMsg, 1), + lager:debug("enc_reply is: ~p", [EncMsg]), + + {ok, 200, iot_util:json_data(<<"success">>)} + end; + %% 处理命令下发 handle_request("POST", "/iot/send_command", _, Params = #{<<"host_id">> := HostId}) -> lager:debug("body is: ~p", [Params]), diff --git a/apps/iot/src/mqtt_handler/iot_message_handler.erl b/apps/iot/src/mqtt_handler/iot_mqtt_message_handler.erl similarity index 85% rename from apps/iot/src/mqtt_handler/iot_message_handler.erl rename to apps/iot/src/mqtt_handler/iot_mqtt_message_handler.erl index 25e2449..4a3702c 100644 --- a/apps/iot/src/mqtt_handler/iot_message_handler.erl +++ b/apps/iot/src/mqtt_handler/iot_mqtt_message_handler.erl @@ -6,7 +6,7 @@ %%% @end %%% Created : 18. 2月 2023 21:39 %%%------------------------------------------------------------------- --module(iot_message_handler). +-module(iot_mqtt_message_handler). -author("aresei"). -include("iot.hrl"). @@ -107,7 +107,29 @@ handle(<<"server.data">>, #{<<"c_id">> := HostId, <<"d">> := Data}) -> end; undefined -> lager:warning("[iot_message_handler] host_id: ~p, not exists", [HostId]) -end. +end; + +%% 处理服务器的ping +handle(<<"server.ping">>, #{<<"c">> := HostId, <<"at">> := At, + <<"h">> := #{<<"fm">> := FreeMemory, <<"fd">> := FreeDisk, <<"cp">> := CpuLoad}}) -> + + case host_model:get_host(HostId) of + {ok, Host=#host{metric = Metric = #host_metric{disk = Disk, memory = Memory}}} -> + NMetric = Metric#host_metric{ + disk = Disk#disk_metric{free = FreeDisk}, + memory = Memory#memory_metric{free = FreeMemory} + }, + + case mnesia:transaction(fun() -> mnesia:write(host, Host#host{metric = NMetric}, write) end) of + {atomic, ok} -> + ok; + {error, Reason} -> + lager:warning("[iot_message_handler] host_id: ~p, ping get error: ~p", [HostId, Reason]) + end; + + undefined -> + lager:warning("[iot_message_handler] host_id: ~p, not exists", [HostId]) + end. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% helper methods diff --git a/rebar.lock b/rebar.lock new file mode 100644 index 0000000..06e78cf --- /dev/null +++ b/rebar.lock @@ -0,0 +1,76 @@ +{"1.2.0", +[{<<"certifi">>,{pkg,<<"certifi">>,<<"2.5.2">>},1}, + {<<"cowboy">>, + {git,"https://github.com/ninenines/cowboy.git", + {ref,"c998673eb009da2ea4dc0e6ef0332534cf679cc4"}}, + 0}, + {<<"cowlib">>, + {git,"https://github.com/ninenines/cowlib", + {ref,"106ba84bb04537879d8ce59321a04e0682110b91"}}, + 1}, + {<<"emqtt">>, + {git,"https://github.com/emqx/emqtt", + {ref,"55e50041cc5b3416067c120eadb8774f1d3d1f4a"}}, + 0}, + {<<"fs">>,{pkg,<<"fs">>,<<"6.1.1">>},1}, + {<<"getopt">>,{pkg,<<"getopt">>,<<"1.0.1">>},1}, + {<<"goldrush">>,{pkg,<<"goldrush">>,<<"0.1.9">>},1}, + {<<"gun">>, + {git,"https://github.com/ninenines/gun", + {ref,"e7dd9f227e46979d8073e71c683395a809b78cb4"}}, + 1}, + {<<"hackney">>, + {git,"https://github.com/benoitc/hackney.git", + {ref,"f3e9292db22c807e73f57a8422402d6b423ddf5f"}}, + 0}, + {<<"idna">>,{pkg,<<"idna">>,<<"6.0.1">>},1}, + {<<"jiffy">>, + {git,"https://github.com/davisp/jiffy.git", + {ref,"9ea1b35b6e60ba21dfd4adbd18e7916a831fd7d4"}}, + 0}, + {<<"lager">>, + {git,"https://github.com/erlang-lager/lager.git", + {ref,"459a3b2cdd9eadd29e5a7ce5c43932f5ccd6eb88"}}, + 0}, + {<<"metrics">>,{pkg,<<"metrics">>,<<"1.0.1">>},1}, + {<<"mimerl">>,{pkg,<<"mimerl">>,<<"1.2.0">>},1}, + {<<"parse_trans">>, + {git,"https://github.com/uwiger/parse_trans", + {ref,"6f3645afb43c7c57d61b54ef59aecab288ce1013"}}, + 0}, + {<<"poolboy">>, + {git,"https://github.com/devinus/poolboy.git", + {ref,"3bb48a893ff5598f7c73731ac17545206d259fac"}}, + 0}, + {<<"ranch">>, + {git,"https://github.com/ninenines/ranch", + {ref,"6bbc8431d513d9bbed7817bc1bcb3b17ef26cb35"}}, + 1}, + {<<"ssl_verify_fun">>,{pkg,<<"ssl_verify_fun">>,<<"1.1.6">>},1}, + {<<"sync">>, + {git,"https://github.com/rustyio/sync.git", + {ref,"aa27b66ccfbfc798b57288af4cf7dc386e205d68"}}, + 0}, + {<<"unicode_util_compat">>,{pkg,<<"unicode_util_compat">>,<<"0.5.0">>},2}]}. +[ +{pkg_hash,[ + {<<"certifi">>, <<"B7CFEAE9D2ED395695DD8201C57A2D019C0C43ECAF8B8BCB9320B40D6662F340">>}, + {<<"fs">>, <<"9D147B944D60CFA48A349F12D06C8EE71128F610C90870BDF9A6773206452ED0">>}, + {<<"getopt">>, <<"C73A9FA687B217F2FF79F68A3B637711BB1936E712B521D8CE466B29CBF7808A">>}, + {<<"goldrush">>, <<"F06E5D5F1277DA5C413E84D5A2924174182FB108DABB39D5EC548B27424CD106">>}, + {<<"idna">>, <<"1D038FB2E7668CE41FBF681D2C45902E52B3CB9E9C77B55334353B222C2EE50C">>}, + {<<"metrics">>, <<"25F094DEA2CDA98213CECC3AEFF09E940299D950904393B2A29D191C346A8486">>}, + {<<"mimerl">>, <<"67E2D3F571088D5CFD3E550C383094B47159F3EEE8FFA08E64106CDF5E981BE3">>}, + {<<"ssl_verify_fun">>, <<"CF344F5692C82D2CD7554F5EC8FD961548D4FD09E7D22F5B62482E5AEAEBD4B0">>}, + {<<"unicode_util_compat">>, <<"8516502659002CEC19E244EBD90D312183064BE95025A319A6C7E89F4BCCD65B">>}]}, +{pkg_hash_ext,[ + {<<"certifi">>, <<"3B3B5F36493004AC3455966991EAF6E768CE9884693D9968055AEEEB1E575040">>}, + {<<"fs">>, <<"EF94E95FFE79916860649FED80AC62B04C322B0BB70F5128144C026B4D171F8B">>}, + {<<"getopt">>, <<"53E1AB83B9CEB65C9672D3E7A35B8092E9BDC9B3EE80721471A161C10C59959C">>}, + {<<"goldrush">>, <<"99CB4128CFFCB3227581E5D4D803D5413FA643F4EB96523F77D9E6937D994CEB">>}, + {<<"idna">>, <<"A02C8A1C4FD601215BB0B0324C8A6986749F807CE35F25449EC9E69758708122">>}, + {<<"metrics">>, <<"69B09ADDDC4F74A40716AE54D140F93BEB0FB8978D8636EADED0C31B6F099F16">>}, + {<<"mimerl">>, <<"F278585650AA581986264638EBF698F8BB19DF297F66AD91B18910DFC6E19323">>}, + {<<"ssl_verify_fun">>, <<"BDB0D2471F453C88FF3908E7686F86F9BE327D065CC1EC16FA4540197EA04680">>}, + {<<"unicode_util_compat">>, <<"D48D002E15F5CC105A696CF2F1BBB3FC72B4B770A184D8420C8DB20DA2674B38">>}]} +].