puzhibing
7 天以前 35b0088fa28dab8a28758dcbcc056620c1ea5e2c
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
package com.ruoyi.system.service.impl;
 
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.dataInterchange.api.feignClient.UPExgMsgRegisterClient;
import com.ruoyi.dataInterchange.api.model.enums.VehicleColorEnum;
import com.ruoyi.dataInterchange.api.vo.GnssDataVo;
import com.ruoyi.dataInterchange.api.vo.UPExgMsgRegisterVo;
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.mapper.CarMapper;
import com.ruoyi.system.query.CarListReq;
import com.ruoyi.system.query.CarListResp;
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 org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
 
/**
 * @author zhibing.pu
 * @Date 2025/3/17 11:38
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements ICarService {
    
    @Value("${live.hls.output-path}")
    private String hlsOutputPath;
    
    @Resource
    private UPExgMsgRegisterClient upExgMsgRegisterClient;
    
    @Resource
    private IEnterpriseService enterpriseService;
    
    @Resource
    private IDriverService driverService;
    
    @Resource
    private ICarTypeService carTypeService;
    
    @Resource
    private RedisTemplate redisTemplate;
    
    
    /**
     * 定时任务存储最新的车辆数据
     */
    @Override
    public void taskSaveNewCar() {
        Car car = this.getOne(new LambdaQueryWrapper<Car>().orderByDesc(Car::getCreateTime).last(" limit 0, 1"));
        Long startTime = -1L;
        if (null != car) {
            startTime = car.getCreateTime().toEpochSecond(ZoneOffset.ofHours(8));
        }
        List<UPExgMsgRegisterVo> list = upExgMsgRegisterClient.getUPExgMsgRegisterListIsAfterCreateTime(startTime).getData();
        if (null == list || list.isEmpty()) {
            return;
        }
        List<Enterprise> list1 = enterpriseService.list();
        List<Driver> driverList = driverService.list(new LambdaQueryWrapper<Driver>().eq(Driver::getStatus, 1));
        List<Car> carList = new ArrayList<>();
        for (UPExgMsgRegisterVo vo : list) {
            car = new Car();
            car.setVehicleNumber(vo.getVehicleNo());
            car.setLicensePlateColor(VehicleColorEnum.getName(vo.getVehicleColor()));
            Enterprise enterprise = list1.stream().filter(s -> s.getCode().equals(vo.getInferiorPlatformId().toString())).findFirst().get();
            car.setEnterpriseId(enterprise.getId());
            car.setOperateType(enterprise.getOperationType());
            Optional<Driver> optional = driverList.stream().filter(s -> s.getVehicleNumber().equals(vo.getVehicleNo())).findFirst();
            if (optional.isPresent()) {
                car.setDriverId(optional.get().getId());
            }
            car.setGpsModel(vo.getTerminalModelType());
            car.setGpsImei(vo.getTerminalSIMCode());
            car.setLongitude(vo.getLongitude());
            car.setLatitude(vo.getLatitude());
            car.setCreateTime(LocalDateTime.now());
            carList.add(car);
        }
        if (carList.size() > 0) {
            this.saveBatch(carList);
            //更新车辆类型统计数据
            List<CarType> carTypeList = carTypeService.list();
            List<Car> cars = this.list();
            for (CarType carType : carTypeList) {
                long count = cars.stream().filter(s -> s.getOperateType().equals(carType.getName())).count();
                carType.setCarNum(count);
            }
            carTypeService.updateBatchById(carTypeList);
        }
    }
    
    
    /**
     * 定时保存车辆司机id
     */
    @Override
    public void taskSaveDriverId() {
        List<Driver> driverList = driverService.list(new LambdaQueryWrapper<Driver>().eq(Driver::getStatus, 1));
        List<Car> list = this.list(new LambdaQueryWrapper<Car>().isNull(Car::getDriverId));
        for (Car car : list) {
            Optional<Driver> optional = driverList.stream().filter(s -> s.getVehicleNumber().equals(car.getVehicleNumber())).findFirst();
            if (optional.isPresent()) {
                car.setDriverId(optional.get().getId());
                this.updateById(car);
            }
        }
    }
    
    /**
     * 获取车辆列表数据
     *
     * @param carListReq
     * @return
     */
    @Override
    public PageInfo<CarListResp> getCarList(CarListReq carListReq) {
        PageInfo<CarListResp> pageInfo = new PageInfo<>(carListReq.getPageCurr(), carListReq.getPageSize());
        return this.baseMapper.getCarList(pageInfo, carListReq);
    }
    
    
    /**
     * 定时任务修改车辆状态
     */
    @Override
    public void taskUpdateCarStatus() {
        List<Car> list = this.list();
        for (Car car : list) {
            LambdaUpdateWrapper<Car> updateWrapper = new LambdaUpdateWrapper<>();
            GnssDataVo gnssDataVo = (GnssDataVo) redisTemplate.opsForValue().get("location:" + car.getVehicleNumber());
            if (null == gnssDataVo) {
                if (car.getStatus() == 1) {
                    updateWrapper.set(Car::getDownlineTime, LocalDateTime.now());
                }
                updateWrapper.set(Car::getStatus, 3);
                updateWrapper.eq(Car::getId, car.getId());
                this.update(updateWrapper);
            } else {
                String dateTime = gnssDataVo.getDate() + " " + gnssDataVo.getTime();
                LocalDateTime localDateTime = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                long second = localDateTime.toEpochSecond(ZoneOffset.ofHours(8));
                if (System.currentTimeMillis() / 1000 > second + 60) {
                    if (car.getStatus() == 1) {
                        updateWrapper.set(Car::getDownlineTime, LocalDateTime.now());
                    }
                    updateWrapper.set(Car::getStatus, 3);
                    updateWrapper.eq(Car::getId, car.getId());
                    this.update(updateWrapper);
                } else {
                    if (car.getStatus() != 1) {
                        updateWrapper.set(Car::getDownlineTime, null);
                        updateWrapper.set(Car::getOnlineTime, LocalDateTime.now());
                    }
                    updateWrapper.set(Car::getStatus, 1);
                    updateWrapper.eq(Car::getId, car.getId());
                    this.update(updateWrapper);
                }
            }
        }
    }
    
    
    /**
     * 检测视频播放,清除没有播放的视频流
     */
    @Override
    public void taskPlayDetection(Integer deviceNumber) {
        Car car = this.getById(deviceNumber);
        try {
            ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
            executorService.scheduleWithFixedDelay(new Runnable() {
                @Override
                public void run() {
                    if(!redisTemplate.hasKey("live:" + car.getId())){
                        String folderPath = hlsOutputPath + "hls\\" + car.getVehicleNumber();
                        JavaCVStreamUtil.close(car.getId(), folderPath);
                    }
                }}, 1, 1, TimeUnit.MINUTES);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    
    
    
    public void taskGetCarDeviceId(){
        Object o = redisTemplate.opsForValue().get("chuzu:token");
        String access_Token = "";
        if(null == o){
            HttpRequest get = HttpUtil.createGet("http://116.169.59.170:9986/MediaAPI/Token?token=shehongchuzu");
            HttpResponse execute = get.execute();
            String body = execute.body();
            Integer expires_in = JSON.parseObject(body).getInteger("Expires_In");
            access_Token = JSON.parseObject(body).getString("Access_Token");
            redisTemplate.opsForValue().set("chuzu:token", access_Token, expires_in, TimeUnit.SECONDS);
        }
        //获取车辆数据
        List<Car> cars = this.list(new QueryWrapper<Car>().eq("enterprise_id", 5));
        HttpRequest get = HttpUtil.createGet("http://116.169.59.170:9986/MediaAPI/BaseInfo/GetVehicleInfo?accessToken=" + access_Token + "&tenantNo=射洪出租");
        HttpResponse execute = get.execute();
        String body = execute.body();
        JSONObject jsonObject = JSON.parseObject(body);
        Integer resultState = jsonObject.getInteger("ResultState");
        if(null != resultState && 1 == resultState){
            JSONArray resultData = jsonObject.getJSONArray("ResultData");
            for (int i = 0; i < resultData.size(); i++) {
                JSONObject jsonObject1 = resultData.getJSONObject(i);
                String vehicleNo = jsonObject1.getString("VehicleNo");
                String Deviceid = jsonObject1.getString("Deviceid");
                cars.stream().filter(s->s.getVehicleNumber().equals(vehicleNo)).findFirst().ifPresent(car -> {
                    car.setDeviceId(Deviceid);
                    this.updateById(car);
                });
            }
        }
    }
 
}