diff --git a/apps/iot/src/database/device_bo.erl b/apps/iot/src/database/device_bo.erl index af8ad7e..b877a6c 100644 --- a/apps/iot/src/database/device_bo.erl +++ b/apps/iot/src/database/device_bo.erl @@ -17,14 +17,9 @@ get_all_devices() -> mysql_pool:get_all(mysql_iot, <<"SELECT * FROM device WHERE device_uuid != ''">>). --spec get_host_devices(HostId :: integer()) -> {ok, Devices :: [binary()]} | {error, Reason::any()}. +-spec get_host_devices(HostId :: integer()) -> {ok, Devices :: [map()]} | {error, Reason::any()}. get_host_devices(HostId) when is_integer(HostId) -> - case mysql_pool:get_all(mysql_iot, <<"SELECT device_uuid FROM device WHERE host_id = ? AND device_uuid != ''">>, [HostId]) of - {ok, Devices} -> - {ok, lists:map(fun(#{<<"device_uuid">> := DeviceUUID}) -> DeviceUUID end, Devices)}; - {error, Reason} -> - {error, Reason} - end. + mysql_pool:get_all(mysql_iot, <<"SELECT device_uuid FROM device WHERE host_id = ? AND device_uuid != ''">>, [HostId]). -spec get_device_by_uuid(DeviceUUID :: binary()) -> {ok, DeviceInfo :: map()} | undefined. get_device_by_uuid(DeviceUUID) when is_binary(DeviceUUID) -> diff --git a/apps/iot/src/iot_device.erl b/apps/iot/src/iot_device.erl index f6af9c3..6296372 100644 --- a/apps/iot/src/iot_device.erl +++ b/apps/iot/src/iot_device.erl @@ -30,7 +30,7 @@ %%% API %%%=================================================================== --spec new(DeviceUUID :: binary()) -> error | {ok, Device :: #device{}}. +-spec new(DeviceInfo :: binary() | map()) -> error | {ok, Device :: #device{}}. new(DeviceUUID) when is_binary(DeviceUUID) -> case device_bo:get_device_by_uuid(DeviceUUID) of {ok, #{<<"device_uuid">> := DeviceUUID, <<"authorize_status">> := AuthorizeStatus, <<"status">> := Status}} -> @@ -38,7 +38,9 @@ new(DeviceUUID) when is_binary(DeviceUUID) -> undefined -> lager:warning("[iot_device] device uuid: ~p, loaded from mysql failed", [DeviceUUID]), error - end. + end; +new(#{<<"device_uuid">> := DeviceUUID, <<"authorize_status">> := AuthorizeStatus, <<"status">> := Status}) -> + {ok, #device{device_uuid = DeviceUUID, status = Status, auth_state = auth_state(AuthorizeStatus)}}. -spec is_activated(Device :: #device{}) -> boolean(). is_activated(#device{auth_state = AuthState}) -> diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index eea40d5..cf39329 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -185,15 +185,15 @@ init([UUID]) -> end, %% 加载设备信息 - {ok, DeviceItems} = device_bo:get_host_devices(HostId), - Devices = lists:flatmap(fun(DeviceUUID) -> - case iot_device:new(DeviceUUID) of + {ok, DeviceInfos} = device_bo:get_host_devices(HostId), + Devices = lists:filtermap(fun(DeviceInfo = #{<<"device_uuid">> := DeviceUUID}) -> + case iot_device:new(DeviceInfo) of error -> - []; + false; {ok, Device} -> - [{DeviceUUID, Device}] + {true, {DeviceUUID, Device}} end - end, DeviceItems), + end, DeviceInfos), {ok, StateName, #state{host_id = HostId, uuid = UUID, device_map = maps:from_list(Devices), has_session = false}}; undefined -> diff --git a/TODO.md b/todo_docs/mysql.md similarity index 100% rename from TODO.md rename to todo_docs/mysql.md diff --git a/todo_docs/todo.md b/todo_docs/todo.md new file mode 100644 index 0000000..8e1759a --- /dev/null +++ b/todo_docs/todo.md @@ -0,0 +1,26 @@ +## 整体架构 + +## 通讯模式 + 1. 微服务(tcp + json) => efka(ssl + protobuf) => iot + 2. ssl的加解密通过nginx反向代理实现, 利用nginx提交数据的加解密效率 + +## 数据模式 + +## 已完成 + 1. 微服务的部署/启动/停止功能 + 2. 微服务和efka之间基于tcp的通讯逻辑规范已经形成 + 3. 云服务离线的时候,efka对微服务产生的文件的缓存逻辑已经实现 + 4. 微服务部署的日志相关的逻辑,管理后台可以通过taskId获取部署的所有相关日志 + +## 待完成 + 1. 微服务和efka之间增加基于websocket的通讯 + json的格式, => 基于文档可以不用为微服务提供sdk + 2. 多租户 + +## 实现修改 + 1. 将配置项目和采集项目合并成一个配置项目,并通过json格式管理 + 2. 数据传输中需要对通过aes加密,修改为在链接层面全部数据加密传输(基于efka端基于ssl,云端基于nginx反向代理) + 3. 取消对数据上传中数据格式的限制,整个数据链路中对数据的约束为二进制格式;这样可以兼容所有的数据类型(efka,iot其实不关心数据负载;只负责转发和路由) + 4. todo 微服务启动的时候通过 register方法建立到efka的关联;返回的时候直接将 config_json 返回;避免服务启动时需要手动获取 +## 新增加 + 1. pub/sub机制,微服务可以通过topic监听自己关注的消息(将一对一和一对多的消息机制统一) + 2. 增加了endpoint,支持将数据路由到http|kafka|mqtt; 增加路由机制,数据上报的时候通过route_key路由到对应的endpoint \ No newline at end of file