# 核心运行链路 ## 应用启动 1. `FastBeeApplication` 启动 Spring Boot。 2. `application.yml` 激活 profile,默认是 `prod`,本地开发通常改为 `dev`。 3. 动态数据源初始化 `master`,可选初始化 `taos`。 4. MyBatis Plus 扫描 `com.fastbee.**.domain` 和 `classpath*:mapper/**/*Mapper.xml`。 5. `boot-strap` 模块按配置启动协议服务。 6. LiteFlow 从数据库加载规则链和脚本: - 规则链表:`iot_scene` - 脚本表:`iot_script` - 应用名字段:`application_name` ## 设备上报链路 ```text 设备 -> MQTT/TCP/HTTP/CoAP/SIP 协议接入 -> 协议解码 / Topic 解析 / Session 管理 -> MessageProducer.sendPublishMsg 或其他队列入口 -> fastbee-mq 本地队列 -> fastbee-iot-data DataHandlerImpl -> 设备物模型值更新 -> 设备日志/事件日志/TDengine -> 规则引擎/场景联动/条件触发 -> WebSocket/MQTT 推送到前端 ``` 关键代码位置: | 环节 | 位置 | | --- | --- | | MQTT Broker 启动 | `fastbee-server/boot-strap/.../MQTTBootStrap.java` | | TCP 服务启动 | `fastbee-server/boot-strap/.../TCPBootStrap.java` | | 消息生产入口 | `fastbee-mq/.../MessageProducer.java` | | 上报队列 | `fastbee-mq/.../DeviceReportQueue.java` | | 上报处理 | `fastbee-iot-data/.../DataHandlerImpl.java` | | 物模型更新 | `fastbee-service/fastbee-iot-service` 下 `IDeviceService` 实现 | | 规则匹配 | `fastbee-iot-data` 的 `IRuleEngine` 和 `fastbee-iot-service` 的 `RuleProcess` | ## 服务/指令下发链路 ```text 前端/API -> DeviceMessageServiceImpl.messagePost / MqttMessagePublishImpl.funcSend -> 查询设备产品、协议、物模型和传输方式 -> MQTT: 发布到设备 Topic -> TCP: 组包后通过 SessionManager/MessageManager 写回连接 -> GB28181: 调用 SIP Gateway -> FunctionLog 记录下发结果 -> WebSocket/MQTT 推送调试消息到前端 ``` 下发时的重要分支: | 分支 | 处理 | | --- | --- | | MQTT 设备 | 使用 `PubMqttClient.publish` 发布到 `TopicsUtils.buildTopic(...)` 生成的 Topic。 | | TCP 设备 | 将指令转为协议字节,交给 `MessageManager.requestR` 对对应 Session 写出。 | | 子设备 | 先解析网关设备关系,把子设备指令映射到网关产品/设备,再设置 slave 地址。 | | 影子模式 | 不真实下发,直接构造上报数据并走 `dataHandler.reportData` 更新影子值。 | | 离线延迟下发 | `deferIfOffline=true` 时写入 `iot_device_deferred_command`,等待设备上线后补发。 | ## 规则引擎链路 规则体系有两层: | 类型 | 入口 | 数据表 | | --- | --- | --- | | 场景联动 | `IRuleEngine.ruleMatch(...)` | `iot_scene`, `iot_scene_device`, `iot_scene_script`, `iot_device_job` | | 数据桥接/脚本 | `RuleProcess.processRuleScript(...)` | `iot_script`, `bridge`, `iot_script_bridge` | LiteFlow SQL 规则源配置在 `application.yml`: ```text chainTableName: iot_scene scriptTableName: iot_script chainEnableField: enable scriptEnableField: enable ``` 设备 TCP 属性上报后,如果启用规则引擎,会额外以 `tcp/property/post` 作为桥接 Topic 调用 `RuleProcess`,用于 HTTP/MQTT/数据库桥接。 ## 数据持久化链路 | 数据 | 主要表 | | --- | --- | | 产品、分类、协议 | `iot_product`, `iot_category`, `iot_protocol` | | 设备与绑定 | `iot_device`, `iot_device_user`, `iot_device_group`, `iot_sub_gateway` | | 物模型 | `iot_things_model`, `iot_things_model_template`, 翻译表 | | 上报日志 | `iot_device_log`, `iot_event_log`, `iot_function_log`, `iot_alert_log` | | 固件升级 | `iot_firmware`, `iot_firmware_task`, `iot_firmware_task_detail` | | 规则/桥接 | `iot_scene`, `iot_script`, `bridge`, `iot_script_bridge` | | 通知 | `notify_channel`, `notify_template`, `notify_log` | | 视频 | `sip_device`, `sip_device_channel`, `media_server`, `iot_sip_relation` | TDengine 可选数据源 `taos` 默认禁用,相关日志服务在 `fastbee-iot-service` 的 `tdengine` 包和 `fastbee-iot-data` 中被调用。 ## 视频监控链路 ```text GB28181 设备/NVR -> SIP Server -> sip_device / sip_device_channel -> ZLMediaKit media_server -> 播放、PTZ、录像、回放 API -> fastbee-record 可独立承接录像相关回调/下载 ``` 视频相关 Controller 分布在 `fastbee-open-api/src/main/java/com/fastbee/controller/media` 和 `fastbee-record`。 ## 通知链路 ```text 业务事件/告警/推送配置 -> notify_template -> notify_channel -> fastbee-notify-core 具体渠道 -> notify_log ``` 物模型推送扩展表: - `iot_things_model_push_config`:某设备的物模型推送配置。 - `iot_push_model_item`:配置下需要推送的物模型项和阈值。 - `iot_push_user`:配置下的接收用户。