Pu Zhibing
2025-03-24 87c2f1ec41e244dfdd4722884fcdf285a1fe9b41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.ruoyi.dataInterchange.controller;
 
import com.ruoyi.common.core.domain.R;
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.time.LocalDateTime;
 
/**
 * 实时音视频控制器
 *
 * @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<UPRealvideoMsgStartupAck> startupRealVideo(@RequestParam("inferiorPlatformId") Integer inferiorPlatformId, @RequestParam("vehicleNo") String vehicleNo) {
        R realVideo = downRealvideoMsgStartupService.startupRealVideo(inferiorPlatformId, vehicleNo);
        if (realVideo.getCode() != 200) {
            return realVideo;
        }
        //发送成功,定时任务获取响应结果
        int num = 0;
        LocalDateTime now = LocalDateTime.now();
        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 || num >= 30) {
                return R.ok(realvideoMsgStartupAck);
            }
        }
    }
    
    
    /**
     * 主动请求停止实时音视频
     *
     * @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;
        LocalDateTime now = LocalDateTime.now();
        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("远程响应失败");
            }
        }
    }
}