diff --git a/src/main/kotlin/com/hisense/dahua_video/consts/EventBusAddress.kt b/src/main/kotlin/com/hisense/dahua_video/consts/EventBusAddress.kt index ec2ad63..1f08579 100644 --- a/src/main/kotlin/com/hisense/dahua_video/consts/EventBusAddress.kt +++ b/src/main/kotlin/com/hisense/dahua_video/consts/EventBusAddress.kt @@ -80,6 +80,11 @@ enum class EventBusAddress(val address: String) { */ SYS_DEVICE_CHANNEL_SCREENSHOT("MSG://EVENT/BUS/SQL/SYS/SYS_DEVICE_CHANNEL_SCREENSHOT"), + /** + * 通道录制视频 + */ + SYS_DEVICE_CHANNEL_RECORD_VIDEO("MSG://EVENT/BUS/SQL/SYS/SYS_DEVICE_CHANNEL_RECORD_VIDEO"), + /** * 获取在线设备通道 */ diff --git a/src/main/kotlin/com/hisense/dahua_video/controller/MonitorController.kt b/src/main/kotlin/com/hisense/dahua_video/controller/MonitorController.kt index b08cf72..59d28b1 100644 --- a/src/main/kotlin/com/hisense/dahua_video/controller/MonitorController.kt +++ b/src/main/kotlin/com/hisense/dahua_video/controller/MonitorController.kt @@ -116,4 +116,47 @@ class MonitorController( } } + /** + * 录制视频 + */ + fun recordVideo(requestHandler: RoutingContext) { + val userId = requestHandler.get("userId") // 用户id + val queryParams = requestHandler.queryParams() + val channelId = queryParams.get("channelId") + val subType = queryParams.get("subType") + val scheme = queryParams.get("scheme") + val duration = queryParams.get("duration") + event!!.request(EventBusAddress.SYS_MONITOR_USER_ALLMONITORUSER_TOKEN.address, JsonObject()) { + if (it.succeeded()) { + val token = it.result().body().stream().filter { index -> + index as JsonObject + index.getInteger("id") == userId + }.findFirst() + token.ifPresent { tokenInfo -> + tokenInfo as JsonObject + tokenInfo.put("channelId", channelId) + tokenInfo.put("subType", subType) + tokenInfo.put("scheme", MonitorScheme.values()[scheme.toInt()].scheme) + tokenInfo.put("duration", duration.toLong()) + event!!.request( + EventBusAddress.SYS_DEVICE_CHANNEL_RECORD_VIDEO.address, + tokenInfo + ) { previewUrl -> + if (previewUrl.succeeded()) { + requestHandler + .response() + .putHeader("content-type", "application/json") + .end(previewUrl.result().body().encode()) + } else { + requestHandler + .response() + .putHeader("content-type", "application/json") + .end(JsonObject().encode()) + } + } + } + } + } + } + } diff --git a/src/main/kotlin/com/hisense/dahua_video/dao/ScreenshotVideoDao.kt b/src/main/kotlin/com/hisense/dahua_video/dao/ScreenshotVideoDao.kt index c86536b..3307a84 100644 --- a/src/main/kotlin/com/hisense/dahua_video/dao/ScreenshotVideoDao.kt +++ b/src/main/kotlin/com/hisense/dahua_video/dao/ScreenshotVideoDao.kt @@ -1,9 +1,20 @@ package com.hisense.dahua_video.dao +import com.hisense.dahua_video.consts.EventBusAddress +import com.hisense.dahua_video.modules.device.DeviceChannel +import com.hisense.dahua_video.modules.media.ScreenshotVideo import io.vertx.core.Vertx import io.vertx.core.eventbus.EventBus import io.vertx.core.impl.logging.Logger import io.vertx.core.impl.logging.LoggerFactory +import io.vertx.core.json.JsonObject +import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq +import org.jetbrains.exposed.sql.StdOutSqlLogger +import org.jetbrains.exposed.sql.addLogger +import org.jetbrains.exposed.sql.insert +import org.jetbrains.exposed.sql.select +import org.jetbrains.exposed.sql.transactions.transaction +import java.time.LocalDateTime /** * 截取短视频功能dao @@ -14,9 +25,43 @@ class ScreenshotVideoDao(event_: EventBus?, vertx: Vertx) { private var event: EventBus? = null private var vertx: Vertx? = null + private val deviceChannel = DeviceChannel() + private val screenshotVideo = ScreenshotVideo() + init { this.event = event_ this.vertx = vertx + saveVideo() + } + + /** + * 保存视频记录 + */ + private fun saveVideo() { + /** + * 只处理本地事件 + */ + event!!.localConsumer(EventBusAddress.SYS_DEVICE_CHANNEL_SCREENSHOT_VIDEO_SAVE.address) + ?.handler { replay -> + val message = replay.body() + logger.info("saveVideo:" + message.encode()) + val channelId = transaction { + addLogger(StdOutSqlLogger) + deviceChannel.slice(deviceChannel.id).select { deviceChannel.channelNo eq message.getString("channelNo") } + .withDistinct().map { it[deviceChannel.id] }.firstOrNull()?.value + } + transaction { + addLogger(StdOutSqlLogger) + screenshotVideo.insert { + it[screenshotVideo.channelId] = channelId!!.toInt() + it[screenshotVideo.channelNo] = message.getString("channelNo") + it[screenshotVideo.createTime] = LocalDateTime.now() + it[screenshotVideo.filePath] = message.getString("filePath") + it[screenshotVideo.fileSize] = message.getLong("fileSize") + it[screenshotVideo.duration] = message.getLong("duration") + } + } + } } diff --git a/src/main/kotlin/com/hisense/dahua_video/util/ScreenshotUtil.kt b/src/main/kotlin/com/hisense/dahua_video/util/ScreenshotUtil.kt index 2a07dc8..c8eb2f4 100644 --- a/src/main/kotlin/com/hisense/dahua_video/util/ScreenshotUtil.kt +++ b/src/main/kotlin/com/hisense/dahua_video/util/ScreenshotUtil.kt @@ -283,7 +283,7 @@ class ScreenshotUtil(vertx: Vertx) { return result } - event!!.consumer(EventBusAddress.SYS_DEVICE_CHANNEL_SCREENSHOT_VIDEO_SAVE.address)?.handler { replay -> + event!!.consumer(EventBusAddress.SYS_DEVICE_CHANNEL_RECORD_VIDEO.address)?.handler { replay -> val message = replay.body() val port = message.getString("monitorDomain").substringAfterLast(":").toInt() var domain = message.getString("monitorDomain").substringBeforeLast(":").substringAfterLast("/") diff --git a/src/main/kotlin/com/hisense/dahua_video/verticle/WebAPIVerticle.kt b/src/main/kotlin/com/hisense/dahua_video/verticle/WebAPIVerticle.kt index 80bc80d..8990be9 100644 --- a/src/main/kotlin/com/hisense/dahua_video/verticle/WebAPIVerticle.kt +++ b/src/main/kotlin/com/hisense/dahua_video/verticle/WebAPIVerticle.kt @@ -88,6 +88,7 @@ class WebAPIVerticle : CoroutineVerticle() { router.route(HttpMethod.POST, "/admin/monitor_user/sign_up").handler(monitorUserController::sign_up) // 第三方平台注册表单 router.route(HttpMethod.GET, "/admin/realmonitorUrl").handler(monitorController::realmonitorUrl) // 获取预览地址 - router.route(HttpMethod.GET, "/admin/screenshot").handler(monitorController::screenshot) + router.route(HttpMethod.GET, "/admin/screenshot").handler(monitorController::screenshot) // 截图 + router.route(HttpMethod.GET, "/admin/record").handler(monitorController::recordVideo) // 截取短视频 } }