iot/apps/iot/src/iot_mock.erl
2023-04-17 15:53:34 +08:00

145 lines
4.2 KiB
Erlang

%%%-------------------------------------------------------------------
%%% @author licheng5
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 14. 2月 2023 20:32
%%%-------------------------------------------------------------------
-module(iot_mock).
-author("licheng5").
-include("iot.hrl").
%% API
-export([insert_hosts/0, insert_services/1, insert_terminals/1, insert_routers/0]).
-export([start_router/1]).
-export([rsa_encode/1]).
-export([start_issue/0]).
start_issue() ->
iot_issue_sup:start_issue(#issue{
issue_id = 1,
name = <<"issue 1">>,
uid = 1234,
deploy_type = 1,
assoc_id = 1,
hosts = [<<"host1">>, <<"host2">>],
timeout = 6,
create_ts = 0,
status = 0
}).
insert_hosts() ->
lists:foreach(fun(Id0) ->
%Id0 = iot_util:rand_bytes(16),
Host = #host{
host_id = integer_to_binary(Id0),
name = <<"N1000_0001">>,
model = <<"N1000">>,
cell_id = rand:uniform(100),
status = 1
},
host_model:add_host(Host)
end, lists:seq(1, 1)).
insert_services(HostId) ->
lists:foreach(fun(Id0) ->
Q0 = queue:new(),
Q = queue:in({1234, 21}, Q0),
Service = #service{
service_id = integer_to_binary(Id0),
host_id = HostId,
name = <<"Service_0001">>,
category = <<"电器"/utf8>>,
%% 应用对象
apply_object = <<"暂时不清楚"/utf8>>,
%% 版本
version = <<"v1.1">>,
%% 执行次数
execute_count = 10,
%% 部署时间
deploy_ts = 0,
metrics = [#service_metric{symbol = <<"X10">>, name = <<"温度"/utf8>>, last_value = 10, unit = <<"E">>, queue = Q}],
status = 0
},
service_model:add_service(Service)
end, lists:seq(1, 100)).
insert_terminals(HostId) ->
lists:foreach(fun(Id0) ->
Terminal = #terminal{
terminal_id = integer_to_binary(Id0),
host_id = HostId,
name = <<"Service_0001">>,
code = <<"1234">>,
access_protocol = <<"TCP/IP">>,
product_id = 12,
vendor_id = 13,
model = <<"1345">>,
cell_id = 12,
status = 0
},
terminal_model:add_terminal(Terminal)
end, lists:seq(1, 100)).
insert_routers() ->
lists:foreach(fun(Id0) ->
R = #router{
router_id = Id0,
name = <<"计费电表"/utf8>>,
rule = <<"测试规则"/utf8>>,
endpoint = #http_endpoint{url = <<"http://127.0.0.1:8080/data">>},
status = 1
},
router_model:add_router(R)
end, lists:seq(1, 100)).
start_router(Id0) when is_integer(Id0) ->
R = #router{
router_id = Id0,
name = <<"计费电表"/utf8>>,
rule = <<"测试规则"/utf8>>,
endpoint = #http_endpoint{url = <<"http://127.0.0.1:8080/data">>},
status = 1
},
router_model:add_router(R),
iot_router_sup:start_new_router(R).
rsa_encode(Data) when is_binary(Data) ->
%% 读取相关配置
PublicPemFile = "/tmp/keys/public.pem",
%% 私钥保存解析后的
{ok, PubBin} = file:read_file(PublicPemFile),
lager:debug("pub bin is: ~p", [PubBin]),
[Pub] = public_key:pem_decode(PubBin),
lager:debug("pub pem bin is: ~p", [Pub]),
PubKey = public_key:pem_entry_decode(Pub),
lager:debug("the public key is: ~p", [PubKey]),
EncData = public_key:encrypt_public(Data, PubKey),
lager:debug("enc data is: ~p", [EncData]),
rsa_decode(EncData),
ok.
rsa_decode(EncData) when is_binary(EncData) ->
%% 读取相关配置
PublicPemFile = "/tmp/keys/pri.pem",
%% 私钥保存解析后的
{ok, PubBin} = file:read_file(PublicPemFile),
lager:debug("pub bin is: ~p", [PubBin]),
[Pub] = public_key:pem_decode(PubBin),
lager:debug("pub pem bin is: ~p", [Pub]),
PubKey = public_key:pem_entry_decode(Pub),
lager:debug("the public key is: ~p", [PubKey]),
PlainData = public_key:decrypt_private(EncData, PubKey),
lager:debug("plain data is: ~p", [PlainData]),
ok.