Pu Zhibing
2025-04-10 0f9ecda1919beb1e8fbbca0f669ac86badf70806
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package com.ruoyi.system.controller;
 
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.dataInterchange.api.feignClient.PlaybackMsgClient;
import com.ruoyi.dataInterchange.api.feignClient.RealVideoMsgClient;
import com.ruoyi.dataInterchange.api.feignClient.UPExgMsgRealLocationClient;
import com.ruoyi.dataInterchange.api.vo.GnssDataVo;
import com.ruoyi.dataInterchange.api.vo.OrderTravelVo;
import com.ruoyi.dataInterchange.api.vo.UPPlaybackMsgStartupAckVo;
import com.ruoyi.dataInterchange.api.vo.UPRealvideoMsgStartupAckVo;
import com.ruoyi.system.api.model.Car;
import com.ruoyi.system.api.model.CarType;
import com.ruoyi.system.api.model.Driver;
import com.ruoyi.system.api.model.Enterprise;
import com.ruoyi.system.query.*;
import com.ruoyi.system.service.ICarService;
import com.ruoyi.system.service.ICarTypeService;
import com.ruoyi.system.service.IDriverService;
import com.ruoyi.system.service.IEnterpriseService;
import com.ruoyi.system.util.JavaCVStreamUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
 
/**
 * @author zhibing.pu
 * @Date 2025/3/17 11:38
 */
@RestController
@RequestMapping("/car")
public class CarController {
    
    @Value("${live.output.path}")
    private String outputPath;
    
    @Value("${live.ip}")
    private String liveIp;
    
    @Value("${live.port}")
    private Integer livePort;
    @Resource
    private ICarService carService;
    @Resource
    private IDriverService driverService;
    @Resource
    private IEnterpriseService enterpriseService;
    @Resource
    private RealVideoMsgClient realVideoMsgClient;
    @Resource
    private PlaybackMsgClient playbackMsgClient;
    @Resource
    private UPExgMsgRealLocationClient upExgMsgRealLocationClient;
    @Resource
    private ICarTypeService carTypeService;
    @Resource
    private RedisTemplate redisTemplate;
    
    @GetMapping("/getCarList")
    @ApiOperation(value = "获取车辆列表", tags = {"车辆管理"})
    public R<PageInfo<CarListResp>> getCarList(CarListReq carListReq) {
        PageInfo<CarListResp> carList = carService.getCarList(carListReq);
        return R.ok(carList);
    }
    
    
    @GetMapping("/getCarInfo/{id}")
    @ApiOperation(value = "获取车辆详情", tags = {"车辆管理"})
    @ApiImplicitParams({
            @ApiImplicitParam(value = "车辆id", name = "id", required = true)
    })
    public R<Car> getCarInfo(@PathVariable("id") Integer id) {
        Car car = carService.getById(id);
        if (null == car) {
            return R.fail("失败");
        }
        Driver driver = driverService.getOne(new LambdaQueryWrapper<Driver>().eq(Driver::getVehicleNumber, car.getVehicleNumber()).eq(Driver::getStatus, 1));
        Enterprise enterprise = enterpriseService.getById(car.getEnterpriseId());
        car.setEnterpriseName(enterprise.getName());
        if (null != driver) {
            car.setDriverName(driver.getName());
            car.setDriverPhone(driver.getPhone());
            car.setDrivingLicenseNumber(driver.getDrivingLicenceFile());
        }
        return R.ok(car);
    }
    
    
    @GetMapping("/getRealVideo/{id}")
    @ApiOperation(value = "获取实时音视频", tags = {"车辆管理"})
    @ApiImplicitParams({
            @ApiImplicitParam(value = "车辆id", name = "id", required = true)
    })
    public R<RealVideoResp> getRealVideo(@PathVariable("id") Integer id) {
        Car car = carService.getById(id);
        if (null == car) {
            return R.fail("失败");
        }
        //手动加一次状态数据,避免定时任务结束任务线程
        redisTemplate.opsForValue().set("live:" + id, true, 1, TimeUnit.MINUTES);
        Enterprise enterprise = enterpriseService.getById(car.getEnterpriseId());
        R<UPRealvideoMsgStartupAckVo> msgStartupAckVoR = realVideoMsgClient.startupRealVideo(Integer.valueOf(enterprise.getCode()), car.getVehicleNumber());
        if (200 == msgStartupAckVoR.getCode()) {
            String path = outputPath + "hls\\" + car.getVehicleNumber() + "\\live.m3u8";
            String folderPath = outputPath + "hls\\" + car.getVehicleNumber();
            FileUtil.mkParentDirs(path);
            File file = new File(path);
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            
            UPRealvideoMsgStartupAckVo data = msgStartupAckVoR.getData();
            RealVideoResp resp = new RealVideoResp();
            //执行拉流和推流
            ExecutorService executorService = new ThreadPoolExecutor(1, 1,
                    0L, TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<Runnable>());
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    JavaCVStreamUtil.push_hls(data.getUrl(), path, id, folderPath);
                }
            });
            resp.setServerIp(liveIp);
            resp.setServerPort(livePort);
            return R.ok(resp);
        }
        
        return R.fail(msgStartupAckVoR.getMsg());
    }
    
    
    @GetMapping("/closeRealVideo/{id}")
    @ApiOperation(value = "关闭实时音视频", tags = {"车辆管理"})
    @ApiImplicitParams({
            @ApiImplicitParam(value = "车辆id", name = "id", required = true)
    })
    public R closeRealVideo(@PathVariable("id") Integer id) {
        Car car = carService.getById(id);
        if (null == car) {
            return R.fail("失败");
        }
        String folderPath = outputPath + "hls\\" + car.getVehicleNumber();
        JavaCVStreamUtil.close(id, folderPath);
        return R.ok();
    }
    
    
    @GetMapping("/playDetection/{id}")
    @ApiOperation(value = "播放检测", tags = {"车辆管理"})
    @ApiImplicitParams({
            @ApiImplicitParam(value = "车辆id", name = "id", required = true)
    })
    public R playDetection(@PathVariable("id") Integer id) {
        redisTemplate.opsForValue().set("live:" + id, true, 1, TimeUnit.MINUTES);
        return R.ok();
    }
    
    
    @GetMapping("/getPlaybackVideo")
    @ApiOperation(value = "获取音视频回放", tags = {"车辆管理"})
    public R<RealVideoResp> getPlaybackVideo(PlaybackVideoReq req) {
        Car car = carService.getById(req.getId());
        if (null == car) {
            return R.fail("失败");
        }
        //手动加一次状态数据,避免定时任务结束任务线程
        redisTemplate.opsForValue().set("live:" + req.getId(), true, 1, TimeUnit.MINUTES);
        Enterprise enterprise = enterpriseService.getById(car.getEnterpriseId());
        R<UPPlaybackMsgStartupAckVo> startupAckVoR = playbackMsgClient.playbackMsgStartup(Integer.valueOf(enterprise.getCode()), car.getVehicleNumber(),
                req.getStartTime(), req.getEndTime());
        if (200 == startupAckVoR.getCode()) {
            UPPlaybackMsgStartupAckVo data = startupAckVoR.getData();
            RealVideoResp resp = new RealVideoResp();
            String path = outputPath + "hls\\" + car.getVehicleNumber() + "\\live.m3u8";
            String folderPath = outputPath + "hls\\" + car.getVehicleNumber();
            FileUtil.mkParentDirs(path);
            File file = new File(path);
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            
            //执行拉流和推流
            ExecutorService executorService = new ThreadPoolExecutor(1, 1,
                    0L, TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<Runnable>());
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    JavaCVStreamUtil.push_hls(data.getUrl(), path, req.getId(), folderPath);
                }
            });
            resp.setServerIp(liveIp);
            resp.setServerPort(livePort);
            return R.ok(resp);
        }
        return R.fail(startupAckVoR.getMsg());
    }
    
    
    @GetMapping("/playbackMsgControl")
    @ApiOperation(value = "音视频回放远程控制", tags = {"车辆管理"})
    public R playbackMsgControl(PlaybackMsgControlReq req) {
        Car car = carService.getById(req.getId());
        if (null == car) {
            return R.fail("失败");
        }
        Enterprise enterprise = enterpriseService.getById(car.getEnterpriseId());
        return playbackMsgClient.playbackMsgControl(Integer.valueOf(enterprise.getCode()), car.getVehicleNumber(),
                req.getControlType(), req.getFastTime());
    }
    
    
    @GetMapping("/getCarTravel")
    @ApiOperation(value = "获取车辆行程轨迹", tags = {"车辆管理"})
    public R<List<OrderTravelVo>> getCarTravel(CarTravelReq req) {
        Car car = carService.getOne(new LambdaQueryWrapper<Car>().eq(Car::getVehicleNumber, req.getVehicleNumber()));
        Long startTime;
        Long endTime;
        if (null != req.getStartTime() && null != req.getEndTime()) {
            startTime = req.getStartTime();
            endTime = req.getEndTime();
        } else {
            LocalDateTime now = LocalDateTime.now();
            startTime = now.minusMinutes(1).toEpochSecond(ZoneOffset.ofHours(8));
            endTime = now.toEpochSecond(ZoneOffset.ofHours(8));
        }
        R<List<OrderTravelVo>> orderTravel = upExgMsgRealLocationClient.getOrderTravel(car.getVehicleNumber(), startTime, endTime);
        return orderTravel;
    }
    
    
    @GetMapping("/getCarTypeList")
    @ApiOperation(value = "获取车辆类型列表数据", tags = {"车辆类型"})
    public R<List<CarType>> getCarTypeList(String name) {
        List<CarType> list = carTypeService.list(new LambdaQueryWrapper<CarType>().like(StringUtils.isNotEmpty(name), CarType::getName, name));
        return R.ok(list);
    }
    
    
    @GetMapping("/getCarCount")
    @ApiOperation(value = "获取各种车辆类型车辆总数", tags = {"首页"})
    public R<List<CarType>> getCarCount() {
        List<CarType> list = carTypeService.list();
        return R.ok(list);
    }
    
    
    @GetMapping("/getCarStatusCount")
    @ApiOperation(value = "获取车辆状态汇总数据和公司总数等", tags = {"首页"})
    public R<CarStatusCount> getCarStatusCount() {
        List<Car> list = carService.list();
        long online = list.isEmpty() ? 0 : list.stream().filter(s -> s.getStatus() == 1).count();
        long offline = list.isEmpty() ? 0 : list.stream().filter(s -> s.getStatus() == 3).count();
        long breakdown = list.isEmpty() ? 0 : list.stream().filter(s -> s.getStatus() == 4).count();
        long abnormal = list.isEmpty() ? 0 : list.stream().filter(s -> s.getStatus() == 2).count();
        CarStatusCount carStatusCount = new CarStatusCount();
        carStatusCount.setOffline(offline);
        carStatusCount.setOnline(online);
        carStatusCount.setBreakdown(breakdown);
        carStatusCount.setAbnormal(abnormal);
        long enterprise = enterpriseService.count();
        carStatusCount.setEnterprise(enterprise);
        carStatusCount.setCar(list.size());
        long driver = driverService.count();
        carStatusCount.setDriver(driver);
        return R.ok(carStatusCount);
    }
    
    
    @GetMapping("/getMapCarList")
    @ApiOperation(value = "获取地图司机数据", tags = {"首页"})
    public R<List<Car>> getMapCarList() {
        List<Car> list = carService.list(new LambdaQueryWrapper<Car>().eq(Car::getStatus, 1));
        for (Car car : list) {
            Long s = (null == car.getDownlineTime() ? LocalDateTime.now() : car.getDownlineTime()).toEpochSecond(ZoneOffset.ofHours(8)) - car.getOnlineTime().toEpochSecond(ZoneOffset.ofHours(8));
            car.setDrivingTime(s / 60);
            GnssDataVo gnssDataVo = (GnssDataVo) redisTemplate.opsForValue().get("location:" + car.getVehicleNumber());
            if (null != gnssDataVo) {
                car.setLongitude(new BigDecimal(gnssDataVo.getLon()).divide(new BigDecimal(1000000)).toString());
                car.setLatitude(new BigDecimal(gnssDataVo.getLat()).divide(new BigDecimal(1000000)).toString());
                car.setSpeed(gnssDataVo.getVec1());
            }
        }
        return R.ok(list);
    }
}