package com.ruoyi.dataInterchange.controller;
|
|
import com.ruoyi.common.core.domain.R;
|
import com.ruoyi.common.core.utils.bean.BeanUtils;
|
import com.ruoyi.dataInterchange.api.vo.UPRealvideoMsgStartupAckVo;
|
import com.ruoyi.dataInterchange.dao.UPRealvideoMsgEndAckDao;
|
import com.ruoyi.dataInterchange.dao.UPRealvideoMsgStartupAckDao;
|
import com.ruoyi.dataInterchange.model.UPRealvideoMsgEndAck;
|
import com.ruoyi.dataInterchange.model.UPRealvideoMsgStartupAck;
|
import com.ruoyi.dataInterchange.server.DOWNRealvideoMsgEndService;
|
import com.ruoyi.dataInterchange.server.DOWNRealvideoMsgStartupService;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RestController;
|
|
import javax.annotation.Resource;
|
import java.io.UnsupportedEncodingException;
|
import java.net.URLEncoder;
|
import java.time.LocalDateTime;
|
import java.time.ZoneOffset;
|
|
/**
|
* 实时音视频控制器
|
*
|
* @author zhibing.pu
|
* @Date 2025/3/21 11:11
|
*/
|
@RestController
|
@RequestMapping("/realVideoMsg")
|
public class RealVideoMsgController {
|
|
@Resource
|
private DOWNRealvideoMsgStartupService downRealvideoMsgStartupService;
|
|
@Resource
|
private UPRealvideoMsgStartupAckDao upRealvideoMsgStartupAckDao;
|
|
@Resource
|
private DOWNRealvideoMsgEndService downRealvideoMsgEndService;
|
|
@Resource
|
private UPRealvideoMsgEndAckDao upRealvideoMsgEndAckDao;
|
|
|
/**
|
* 发起实时音视频请求
|
*
|
* @param vehicleNo 车牌号
|
* @return
|
*/
|
@PostMapping("/startupRealVideo")
|
public R<UPRealvideoMsgStartupAckVo> startupRealVideo(@RequestParam("inferiorPlatformId") Integer inferiorPlatformId, @RequestParam("vehicleNo") String vehicleNo) {
|
R<String> realVideo = downRealvideoMsgStartupService.startupRealVideo(inferiorPlatformId, vehicleNo);
|
if (realVideo.getCode() != 200) {
|
return R.fail(realVideo.getMsg());
|
}
|
//发送成功,定时任务获取响应结果
|
int num = 0;
|
long now = LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8));
|
while (true) {
|
UPRealvideoMsgStartupAck realvideoMsgStartupAck = upRealvideoMsgStartupAckDao.findByInferiorPlatformIdAndVehicleNoAndCreateTimeAfter(inferiorPlatformId, vehicleNo, now);
|
if (null == realvideoMsgStartupAck) {
|
try {
|
Thread.sleep(1000L);
|
} catch (InterruptedException e) {
|
throw new RuntimeException(e);
|
}
|
num++;
|
continue;
|
}
|
if (null != realvideoMsgStartupAck) {
|
int result = realvideoMsgStartupAck.getResult();
|
switch (result){
|
case 0:
|
UPRealvideoMsgStartupAckVo vo = new UPRealvideoMsgStartupAckVo();
|
BeanUtils.copyProperties(realvideoMsgStartupAck, vo);
|
String encode = null;
|
try {
|
encode = URLEncoder.encode(vehicleNo, "UTF-8");
|
} catch (UnsupportedEncodingException e) {
|
throw new RuntimeException(e);
|
}
|
vo.setUrl("http://" + vo.getServerIP() + ":" + vo.getServerPort() + "/" + encode + "." + vo.getVehicleColor() + ".1.0." + realVideo.getData());
|
return R.ok(vo);
|
case 1:
|
return R.fail("发起实时音视频失败,可能是车辆离线导致");
|
case 2:
|
return R.fail("不支持实时音视频");
|
case 3:
|
return R.fail("实时音视频会话结束");
|
case 4:
|
return R.fail("实时音视频口令错误");
|
case 5:
|
return R.fail("实时音视频不满足跨域条件");
|
default:
|
return R.fail("发起实时音视频失败,可能是车辆离线导致");
|
}
|
}
|
if (num >= 30) {
|
return R.fail("发起实时音视频失败,可能是车辆离线导致");
|
}
|
}
|
}
|
|
|
/**
|
* 主动请求停止实时音视频
|
*
|
* @param vehicleNo 车牌号
|
* @return
|
*/
|
@PostMapping("/stopRealVideo")
|
public R<?> stopRealVideo(@RequestParam("inferiorPlatformId") Integer inferiorPlatformId, @RequestParam("vehicleNo") String vehicleNo) {
|
R r = downRealvideoMsgEndService.stopRealVideo(inferiorPlatformId, vehicleNo);
|
if (r.getCode() != 200) {
|
return r;
|
}
|
//发送成功,定时任务获取响应结果
|
int num = 0;
|
Long now = LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8));
|
while (true) {
|
UPRealvideoMsgEndAck upRealvideoMsgEndAck = upRealvideoMsgEndAckDao.findByInferiorPlatformIdAndVehicleNoAndCreateTimeAfter(inferiorPlatformId, vehicleNo, now);
|
if (null == upRealvideoMsgEndAck) {
|
try {
|
Thread.sleep(1000L);
|
} catch (InterruptedException e) {
|
throw new RuntimeException(e);
|
}
|
num++;
|
continue;
|
}
|
if (null != upRealvideoMsgEndAck) {
|
return R.ok(upRealvideoMsgEndAck.getResult());
|
}
|
if (num >= 30) {
|
return R.fail("远程响应失败");
|
}
|
}
|
}
|
}
|