This commit is contained in:
安礼成 2023-03-06 16:39:45 +08:00
parent f593aa00de
commit 059fc6b53f
4 changed files with 208 additions and 2 deletions

View File

@ -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
}).

View File

@ -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]),

View File

@ -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

76
rebar.lock Normal file
View File

@ -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">>}]}
].