车辆实时轨迹更新
parent
c686243244
commit
26ff678b46
|
@ -124,6 +124,15 @@ public class Car {
|
||||||
@Excel(name = "车辆状态")
|
@Excel(name = "车辆状态")
|
||||||
private int status;
|
private int status;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 电子围栏名称
|
||||||
|
*/
|
||||||
|
private String fenceName;
|
||||||
|
/**
|
||||||
|
* 车辆类型名称
|
||||||
|
*/
|
||||||
|
private String carTypeName;
|
||||||
|
|
||||||
public static Car carBuildAdd(CarRequest carRequest){
|
public static Car carBuildAdd(CarRequest carRequest){
|
||||||
return Car.builder()
|
return Car.builder()
|
||||||
.carVinId(carRequest.getCarVinId())
|
.carVinId(carRequest.getCarVinId())
|
||||||
|
|
|
@ -0,0 +1,214 @@
|
||||||
|
package com.god.base.server.common.domain;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 报文信息实体类
|
||||||
|
* VehicleMessage
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@TableName("vehicle_message")
|
||||||
|
public class VehicleMessage {
|
||||||
|
/**
|
||||||
|
* 消息标识
|
||||||
|
*/
|
||||||
|
private String identifier;
|
||||||
|
/**
|
||||||
|
* VIN码
|
||||||
|
*/
|
||||||
|
private String vin;
|
||||||
|
/**
|
||||||
|
* 经度
|
||||||
|
*/
|
||||||
|
private String longitude;
|
||||||
|
/**
|
||||||
|
* 维度
|
||||||
|
*/
|
||||||
|
private String latitude;
|
||||||
|
/**
|
||||||
|
* 车速
|
||||||
|
*/
|
||||||
|
private String speed;
|
||||||
|
/**
|
||||||
|
* 总里程
|
||||||
|
*/
|
||||||
|
private String totalDistance;
|
||||||
|
/**
|
||||||
|
* 总电压
|
||||||
|
*/
|
||||||
|
private String totalVoltage;
|
||||||
|
/**
|
||||||
|
* 总电流
|
||||||
|
*/
|
||||||
|
private String joinCurrent;
|
||||||
|
/**
|
||||||
|
* 绝缘电阻
|
||||||
|
*/
|
||||||
|
private String insulationResistance;
|
||||||
|
/**
|
||||||
|
* 档位
|
||||||
|
*/
|
||||||
|
private String gears;
|
||||||
|
/**
|
||||||
|
* 加速踏板行程值
|
||||||
|
*/
|
||||||
|
private String acceleratorPedal;
|
||||||
|
/**
|
||||||
|
* 制动踏板行程值
|
||||||
|
*/
|
||||||
|
private String brakePedal;
|
||||||
|
/**
|
||||||
|
* 燃料消耗率
|
||||||
|
*/
|
||||||
|
private String fuelRate;
|
||||||
|
/**
|
||||||
|
* 电机控制器温度
|
||||||
|
*/
|
||||||
|
private String motorControllerTemperature;
|
||||||
|
/**
|
||||||
|
* 电机转速
|
||||||
|
*/
|
||||||
|
private String motorSpeed;
|
||||||
|
/**
|
||||||
|
* 电机转矩
|
||||||
|
*/
|
||||||
|
private String motorTorque;
|
||||||
|
/**
|
||||||
|
* 电机温度
|
||||||
|
*/
|
||||||
|
private String motorTemperature;
|
||||||
|
/**
|
||||||
|
* 电机电压
|
||||||
|
*/
|
||||||
|
private String motorVoltage;
|
||||||
|
/**
|
||||||
|
* 电机电流
|
||||||
|
*/
|
||||||
|
private String motorCurrent;
|
||||||
|
/**
|
||||||
|
* 动力电池剩余电量SOC
|
||||||
|
*/
|
||||||
|
private String dumpEnergy;
|
||||||
|
/**
|
||||||
|
* 当前状态允许的最大反馈功率
|
||||||
|
*/
|
||||||
|
private String maximumFeedbackPower;
|
||||||
|
/**
|
||||||
|
* 当前状态允许最大放电功率
|
||||||
|
*/
|
||||||
|
private String maximumDischargePower;
|
||||||
|
/**
|
||||||
|
* BMS自检计数器
|
||||||
|
*/
|
||||||
|
private String selfCheckingCounter;
|
||||||
|
/**
|
||||||
|
* 动力电池充放电电流
|
||||||
|
*/
|
||||||
|
private String chargingAndDischargingCurrent;
|
||||||
|
/**
|
||||||
|
* 动力电池负载端总电压V3
|
||||||
|
*/
|
||||||
|
private String totalVoltageAtLoadEnd;
|
||||||
|
/**
|
||||||
|
* 单次最大电压
|
||||||
|
*/
|
||||||
|
private String maximumVoltage;
|
||||||
|
/**
|
||||||
|
* 单体电池最低电压
|
||||||
|
*/
|
||||||
|
private String lowestVoltageBattery;
|
||||||
|
/**
|
||||||
|
* 单体电池最高温度
|
||||||
|
*/
|
||||||
|
private String maximumTemperatureBattery;
|
||||||
|
/**
|
||||||
|
* 单体电池最低温度
|
||||||
|
*/
|
||||||
|
private String lowestTemperatureBattery;
|
||||||
|
/**
|
||||||
|
* 动力电池可用容量
|
||||||
|
*/
|
||||||
|
private String availableBatteryCapacity;
|
||||||
|
/**
|
||||||
|
* 车辆状态
|
||||||
|
*/
|
||||||
|
private Integer vehicleState;
|
||||||
|
/**
|
||||||
|
* 充电状态
|
||||||
|
*/
|
||||||
|
private Integer chargingState;
|
||||||
|
/**
|
||||||
|
* 运行状态
|
||||||
|
*/
|
||||||
|
private Integer runningStatus;
|
||||||
|
/**
|
||||||
|
* SOC
|
||||||
|
*/
|
||||||
|
private Integer socStatus;
|
||||||
|
/**
|
||||||
|
* 可充电储能装置工作状态
|
||||||
|
*/
|
||||||
|
private Integer energyStorageDeviceState;
|
||||||
|
/**
|
||||||
|
* 驱动电机状态
|
||||||
|
*/
|
||||||
|
private Integer driveMotorCondition;
|
||||||
|
/**
|
||||||
|
* 定位是否有效
|
||||||
|
*/
|
||||||
|
private Integer positioningState;
|
||||||
|
/**
|
||||||
|
* 电子防窃系统状态
|
||||||
|
*/
|
||||||
|
private Integer easStatus;
|
||||||
|
/**
|
||||||
|
* 传动系统状态
|
||||||
|
*/
|
||||||
|
private Integer ptcStatus;
|
||||||
|
/**
|
||||||
|
* 蓄电池状态
|
||||||
|
*/
|
||||||
|
private Integer epsStatus;
|
||||||
|
/**
|
||||||
|
* 防滑刹车系统状态
|
||||||
|
*/
|
||||||
|
private Integer absStatus;
|
||||||
|
/**
|
||||||
|
* 微处理器状态
|
||||||
|
*/
|
||||||
|
private Integer mcuStatus;
|
||||||
|
/**
|
||||||
|
* 动力电池加热状态
|
||||||
|
*/
|
||||||
|
private Integer batteryHeatingCondition;
|
||||||
|
/**
|
||||||
|
* 动力电池当前状态
|
||||||
|
*/
|
||||||
|
private Integer currentBatteryStatus;
|
||||||
|
/**
|
||||||
|
* 动力电池保温状态
|
||||||
|
*/
|
||||||
|
private Integer batteryInsulationStatus;
|
||||||
|
/**
|
||||||
|
* 电力交换系统状态
|
||||||
|
*/
|
||||||
|
private Integer dcdcStatus;
|
||||||
|
/**
|
||||||
|
* 充电机状态
|
||||||
|
*/
|
||||||
|
private Integer chgStatus;
|
||||||
|
/**
|
||||||
|
* 时间
|
||||||
|
*/
|
||||||
|
private Integer time;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.god.base.server.common.domainConstants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据解析端
|
||||||
|
*/
|
||||||
|
public class RedisConstant {
|
||||||
|
/**
|
||||||
|
* 实时信息
|
||||||
|
*/
|
||||||
|
public static final String CURRENT_INFO = "current_info";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当日告警数
|
||||||
|
*/
|
||||||
|
public static final String CURRENT_DAY_FAULT = "current_day_fault";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当月告警数
|
||||||
|
*/
|
||||||
|
public static final String CURRENT_MONTH_FAULT = "current_month_fault";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 车辆有关信息标识
|
||||||
|
*/
|
||||||
|
public static final String VEHICLE_INFO_TOKEN = "vehicle_info_token:";
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ package com.god.base.server.controller;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.god.base.server.common.domain.Car;
|
import com.god.base.server.common.domain.Car;
|
||||||
|
import com.god.base.server.common.domain.VehicleMessage;
|
||||||
import com.god.base.server.common.domain.request.CarRequest;
|
import com.god.base.server.common.domain.request.CarRequest;
|
||||||
import com.god.base.server.service.CarService;
|
import com.god.base.server.service.CarService;
|
||||||
import com.god.base.server.util.AopConfig;
|
import com.god.base.server.util.AopConfig;
|
||||||
|
@ -143,12 +144,21 @@ public class CarController extends BaseController {
|
||||||
util.exportExcel(response,list,"车辆信息管理");
|
util.exportExcel(response,list,"车辆信息管理");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实时轨迹
|
* 获取所有在线车辆
|
||||||
*/
|
*/
|
||||||
@PostMapping
|
@GetMapping("/onlineVehicle")
|
||||||
public Result sSgj(){
|
public Result<List<VehicleMessage>> onlineVehicle(){
|
||||||
return null;
|
return carService.onlineVehicle();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 统计车辆
|
||||||
|
*/
|
||||||
|
@GetMapping("/countData")
|
||||||
|
public Result<String> countData(){
|
||||||
|
return carService.countData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.god.base.server.controller;
|
package com.god.base.server.controller;
|
||||||
|
|
||||||
import com.god.base.server.common.domain.DrivingRecord;
|
import com.god.base.server.common.domain.DrivingRecord;
|
||||||
|
import com.god.base.server.common.domain.VehicleMessage;
|
||||||
import com.god.base.server.service.IDrivingRecordService;
|
import com.god.base.server.service.IDrivingRecordService;
|
||||||
import com.god.common.core.domain.Result;
|
import com.god.common.core.domain.Result;
|
||||||
import com.god.common.core.utils.poi.ExcelUtil;
|
import com.god.common.core.utils.poi.ExcelUtil;
|
||||||
|
@ -13,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -110,6 +112,41 @@ public class DrivingRecordController extends BaseController {
|
||||||
return Result.success(drivingRecordService.deleteDrivingRecordByIds(ids));
|
return Result.success(drivingRecordService.deleteDrivingRecordByIds(ids));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加车辆行驶记录
|
||||||
|
* @param vin
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("/strtVehocle/{vin}")
|
||||||
|
public Result startVehicle(@PathVariable("vin") String vin){
|
||||||
|
DrivingRecord drivingRecord = new DrivingRecord();
|
||||||
|
drivingRecord.setVin(vin);
|
||||||
|
drivingRecord.setStartTime(new Date());
|
||||||
|
drivingRecordService.insertDrivingRecord(drivingRecord);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取单条记录
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/findOne/{id}")
|
||||||
|
public Result<DrivingRecord> findOne(@PathVariable("id") Long id){
|
||||||
|
DrivingRecord drivingRecord = drivingRecordService.selectDrivingRecordById(id);
|
||||||
|
return Result.success(drivingRecord);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 回显历史轨迹
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("trackHistory/{id}")
|
||||||
|
public Result<List<VehicleMessage>> trackHistory(@PathVariable Long id){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,4 +50,10 @@ public interface CarMapper extends BaseMapper<Car> {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
List<Car> selectVehicleInfoList(CarRequest carRequest);
|
List<Car> selectVehicleInfoList(CarRequest carRequest);
|
||||||
|
|
||||||
|
List<String> getOnlineCarIds();
|
||||||
|
|
||||||
|
Integer getOnlineCarCount();
|
||||||
|
|
||||||
|
Integer getUnOnlineCount();
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.god.base.server.service;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import com.god.base.server.common.domain.Car;
|
import com.god.base.server.common.domain.Car;
|
||||||
|
import com.god.base.server.common.domain.VehicleMessage;
|
||||||
import com.god.base.server.common.domain.request.CarRequest;
|
import com.god.base.server.common.domain.request.CarRequest;
|
||||||
import com.god.common.core.domain.Result;
|
import com.god.common.core.domain.Result;
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
@ -18,6 +19,15 @@ public interface CarService extends IService<Car> {
|
||||||
|
|
||||||
|
|
||||||
List<Car> selectCarInfoList(CarRequest carRequest);
|
List<Car> selectCarInfoList(CarRequest carRequest);
|
||||||
|
|
||||||
|
Result<List<VehicleMessage>> onlineVehicle();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 响应信息主题
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Result<String> countData();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,17 +3,21 @@ package com.god.base.server.service.impl;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.god.base.server.common.domain.Car;
|
import com.god.base.server.common.domain.Car;
|
||||||
|
import com.god.base.server.common.domain.VehicleMessage;
|
||||||
import com.god.base.server.common.domain.request.CarRequest;
|
import com.god.base.server.common.domain.request.CarRequest;
|
||||||
|
import com.god.base.server.common.domainConstants.RedisConstant;
|
||||||
import com.god.base.server.mapper.CarMapper;
|
import com.god.base.server.mapper.CarMapper;
|
||||||
import com.god.base.server.service.CarService;
|
import com.god.base.server.service.CarService;
|
||||||
import com.god.common.core.domain.Result;
|
import com.god.common.core.domain.Result;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 车辆管理业务层
|
* 车辆管理业务层
|
||||||
|
@ -25,6 +29,9 @@ public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarSe
|
||||||
@Autowired
|
@Autowired
|
||||||
private CarMapper carMapper;
|
private CarMapper carMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate redisTemplate;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 车辆信息
|
* 车辆信息
|
||||||
|
@ -129,5 +136,28 @@ public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarSe
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取所有在线车辆
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Result<List<VehicleMessage>> onlineVehicle() {
|
||||||
|
List<String> ids = carMapper.getOnlineCarIds();
|
||||||
|
List<VehicleMessage> messages = ids.stream().map(id -> {
|
||||||
|
VehicleMessage message = (VehicleMessage) redisTemplate.opsForValue().get(RedisConstant.CURRENT_INFO + ":" + id);
|
||||||
|
return message;
|
||||||
|
}).collect(Collectors.toList());Collectors.toList();
|
||||||
|
return Result.success(messages);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Result<String> countData() {
|
||||||
|
Integer day = (Integer) redisTemplate.opsForValue().get(RedisConstant.VEHICLE_INFO_TOKEN+RedisConstant.CURRENT_DAY_FAULT);
|
||||||
|
Integer month = (Integer) redisTemplate.opsForValue().get(RedisConstant.VEHICLE_INFO_TOKEN+RedisConstant.CURRENT_MONTH_FAULT);
|
||||||
|
Integer onlineCount = carMapper.getOnlineCarCount();
|
||||||
|
Integer unOnlineCount = carMapper.getUnOnlineCount();
|
||||||
|
return Result.success(day + ":" + month + ":" + onlineCount + ":" + unOnlineCount );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
//package com.god.base.server.webSocket.config;
|
||||||
|
//import org.springframework.context.annotation.Bean;
|
||||||
|
//import org.springframework.context.annotation.Configuration;
|
||||||
|
//import org.springframework.web.server.ServerWebInputException;
|
||||||
|
//
|
||||||
|
///**
|
||||||
|
// * WebSocketConfig
|
||||||
|
// * 开始webSocket
|
||||||
|
// * WenHao.Sao
|
||||||
|
// */
|
||||||
|
//@Configuration
|
||||||
|
//public class WebSocketConfig {
|
||||||
|
//
|
||||||
|
// @Bean
|
||||||
|
// public ServerEndpointExporter serverEndpointExporter(){
|
||||||
|
// return new ServerEndpointExporter();
|
||||||
|
// }
|
||||||
|
//}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.god.base.server.webSocket.controller;
|
||||||
|
|
||||||
|
import com.god.base.server.webSocket.handler.SocketBatchHandler;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.websocket.OnOpen;
|
||||||
|
import javax.websocket.Session;
|
||||||
|
import javax.websocket.server.ServerEndpoint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName WebSocketBatchSever
|
||||||
|
* @Author WenHao.Sao
|
||||||
|
*/
|
||||||
|
@ServerEndpoint("/car/batch")
|
||||||
|
@Component
|
||||||
|
@Log4j2
|
||||||
|
public class WebSocketBatchSever {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接建立成功方法
|
||||||
|
*/
|
||||||
|
@OnOpen
|
||||||
|
public void onOpen(Session session){
|
||||||
|
log.info("车辆大屏新连接" + session.getId());
|
||||||
|
SocketBatchHandler.sessions.add(session);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.god.base.server.webSocket.controller;
|
||||||
|
import com.god.base.server.webSocket.entity.SocketData;
|
||||||
|
import com.god.base.server.webSocket.handler.SocketHandler;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import javax.websocket.OnOpen;
|
||||||
|
import javax.websocket.Session;
|
||||||
|
import javax.websocket.server.ServerEndpoint;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WebSocketServer服务类
|
||||||
|
*/
|
||||||
|
@ServerEndpoint("/vehicle/realTime/{vin}")
|
||||||
|
@Component
|
||||||
|
@Log4j2
|
||||||
|
public class WebSocketServer {
|
||||||
|
/**
|
||||||
|
* 连接建立成功调用的方法
|
||||||
|
*/
|
||||||
|
@OnOpen
|
||||||
|
public void onOpen(Session session , @PathVariable("vin") String vin){
|
||||||
|
log.info("车辆实时轨迹,新连接:{}" , session.getId());
|
||||||
|
SocketData socketData = new SocketData();
|
||||||
|
socketData.setVin(vin);
|
||||||
|
socketData.setSession(session);
|
||||||
|
SocketHandler.socketList.add(socketData);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.god.base.server.webSocket.entity;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.websocket.Session;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName SocketData
|
||||||
|
* @Author WenHao.Sao
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class SocketData {
|
||||||
|
/**
|
||||||
|
* vin
|
||||||
|
*/
|
||||||
|
private String vin;
|
||||||
|
/**
|
||||||
|
*连接对象
|
||||||
|
*/
|
||||||
|
private Session session;
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.god.base.server.webSocket.entity;
|
||||||
|
|
||||||
|
import com.god.base.server.common.domain.VehicleMessage;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName VehicleAllData
|
||||||
|
* @Author WenHao.Sao
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class VehicleAllData {
|
||||||
|
private List<VehicleMessage> messages;
|
||||||
|
|
||||||
|
private String info;
|
||||||
|
}
|
|
@ -0,0 +1,57 @@
|
||||||
|
package com.god.base.server.webSocket.handler;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.god.base.server.common.domain.VehicleMessage;
|
||||||
|
import com.god.base.server.common.domainConstants.RedisConstant;
|
||||||
|
import com.god.base.server.mapper.CarMapper;
|
||||||
|
import com.god.base.server.webSocket.entity.VehicleAllData;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.websocket.Session;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author swh的mac
|
||||||
|
* @Date 2023/11/20 14:37
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
@Component
|
||||||
|
public class SocketBatchHandler {
|
||||||
|
public static CopyOnWriteArrayList<Session> sessions = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CarMapper carMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate redisTemplate;
|
||||||
|
|
||||||
|
@Scheduled(cron = "0/1 * * * * ?")
|
||||||
|
public void sendMessageFlush(){
|
||||||
|
for (Session session : sessions) {
|
||||||
|
List<String> ids = carMapper.getOnlineCarIds();
|
||||||
|
List<VehicleMessage> messages = ids.stream().map(id -> {
|
||||||
|
VehicleMessage message = (VehicleMessage) redisTemplate.opsForValue().get(RedisConstant.CURRENT_INFO + ":" + id);
|
||||||
|
return message;
|
||||||
|
}).collect(Collectors.toList());
|
||||||
|
Integer day = (Integer) redisTemplate.opsForValue().get(RedisConstant.VEHICLE_INFO_TOKEN+RedisConstant.CURRENT_DAY_FAULT);
|
||||||
|
Integer month = (Integer) redisTemplate.opsForValue().get(RedisConstant.VEHICLE_INFO_TOKEN+RedisConstant.CURRENT_MONTH_FAULT);
|
||||||
|
Integer onlineCount = carMapper.getOnlineCarCount();
|
||||||
|
Integer unOnlineCount = carMapper.getUnOnlineCount();
|
||||||
|
VehicleAllData vehicleAllData = new VehicleAllData();
|
||||||
|
vehicleAllData.setMessages(messages);
|
||||||
|
vehicleAllData.setInfo(day+":"+month+":"+onlineCount+":"+unOnlineCount);
|
||||||
|
try {
|
||||||
|
session.getBasicRemote().sendText(JSON.toJSONString(vehicleAllData));
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.info("{}:连接中断",session.getId());
|
||||||
|
sessions.remove(session);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
package com.god.base.server.webSocket.handler;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.god.base.server.common.domain.VehicleMessage;
|
||||||
|
import com.god.base.server.common.domainConstants.RedisConstant;
|
||||||
|
import com.god.base.server.webSocket.entity.SocketData;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.websocket.Session;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author swh的mac
|
||||||
|
* @Date 2023/11/20 15:51
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
@Component
|
||||||
|
public class SocketHandler {
|
||||||
|
//存放每个客户端对应的MyWebSocket
|
||||||
|
public static CopyOnWriteArrayList<SocketData> socketList = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate redisTemplate;
|
||||||
|
|
||||||
|
@Scheduled(cron = "0/1 * * * * ?")
|
||||||
|
public void sendMessageFlush(){
|
||||||
|
for (SocketData socketData : socketList) {
|
||||||
|
Session session = socketData.getSession();
|
||||||
|
VehicleMessage msg = (VehicleMessage) redisTemplate.opsForValue().get(RedisConstant.CURRENT_INFO + ":" + socketData.getVin());
|
||||||
|
try {
|
||||||
|
session.getBasicRemote().sendText(JSON.toJSONString(msg));
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.info("{}:连接中断",socketData.getSession().getId());
|
||||||
|
socketList.remove(socketData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -40,4 +40,19 @@
|
||||||
</where>
|
</where>
|
||||||
|
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<select id="getOnlineCarIds" resultType="java.lang.String">
|
||||||
|
select vin
|
||||||
|
from car where vehicle_status = 1
|
||||||
|
</select>
|
||||||
|
|
||||||
|
|
||||||
|
<select id="getOnlineCarCount" resultType="java.lang.Integer">
|
||||||
|
select count(*)
|
||||||
|
from t_car where status = 1
|
||||||
|
</select>
|
||||||
|
<select id="getUnOnlineCount" resultType="java.lang.Integer">
|
||||||
|
select count(*)
|
||||||
|
from t_car
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|
Loading…
Reference in New Issue