1. 在运营区外X秒后断电

This commit is contained in:
邱贞招 2024-05-30 11:48:16 +08:00
parent f9086a50ea
commit d9e8887a60
3 changed files with 161 additions and 116 deletions

View File

@ -32,6 +32,10 @@ import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* 接收硬件参数
@ -66,6 +70,9 @@ public class ReceiveController {
@Autowired
private IEtOrderService etOrderService;
@Autowired
private ScheduledExecutorService scheduledExecutorService;
/**
* 功能描述第三方平台数据接收<p>
@ -125,133 +132,162 @@ public class ReceiveController {
lat = new BigDecimal(doubles[0]).setScale(4, RoundingMode.HALF_UP);
lon = new BigDecimal(doubles[1]).setScale(4, RoundingMode.HALF_UP);
log.info("转换后的GCJ02经纬度" + lon + "---" + lat);
device.setLatitude(lat.toString());
device.setLongitude(lon.toString());
Integer bat = value.getBat();
BigDecimal divide = new BigDecimal(bat).divide(new BigDecimal(10));
log.info("保存电压:" + divide);
device.setVoltage(divide.toString());//电压
// 根据电压计算续航里程
EtModel model = etModelService.selectEtModelByModelId(device.getModelId());
Integer remainingMileage = CommonUtil.getRemainingMileage(device.getVoltage(), model.getFullVoltage(), model.getLowVoltage(), model.getFullEndurance());
Integer electricQuantity = CommonUtil.getElectricQuantity(device.getVoltage(), model.getFullVoltage(), model.getLowVoltage());//电量百分百
device.setRemainingMileage(remainingMileage);
device.setRemainingPower(electricQuantity.toString());
int i = asDeviceService.updateAsDeviceBySn(device);
if(i>0){
log.info("更新定位成功==========================>" +logEntry.getDevName());
/** 2. 判断是否在禁行区内
* 如果在 根据配置禁行区内断电配置进行断电
**/
boolean noRidingArea = asDeviceService.isNoRidingArea(device.getSn(), device.getAreaId());
if(noRidingArea){
String noRidingOutage = area.getNoRidingOutage();
//发送播报指令
asDeviceService.sendCommand(device.getMac(), Token.getToken(),IotConstants.COMMAND_PLAY3,"禁行区内播报");
if(noRidingOutage.equals("1")){//禁行区内断电
//发送断电命令
asDeviceService.sendCommand(device.getMac(), Token.getToken(),IotConstants.COMMAND_QLOSE,"禁行区内断电");
log.info("禁行区内发送断电命令:" +logEntry.getDevName());
if(BigDecimal.ZERO.compareTo(lon) != 0 && BigDecimal.ZERO.compareTo(lat) != 0){
device.setLatitude(lat.toString());
device.setLongitude(lon.toString());
Integer bat = value.getBat();
BigDecimal divide = new BigDecimal(bat).divide(new BigDecimal(10));
log.info("保存电压:" + divide);
device.setVoltage(divide.toString());//电压
// 根据电压计算续航里程
EtModel model = etModelService.selectEtModelByModelId(device.getModelId());
Integer remainingMileage = CommonUtil.getRemainingMileage(device.getVoltage(), model.getFullVoltage(), model.getLowVoltage(), model.getFullEndurance());
Integer electricQuantity = CommonUtil.getElectricQuantity(device.getVoltage(), model.getFullVoltage(), model.getLowVoltage());//电量百分百
device.setRemainingMileage(remainingMileage);
device.setRemainingPower(electricQuantity.toString());
int i = asDeviceService.updateAsDeviceBySn(device);
if(i>0){
log.info("更新定位成功==========================>" +logEntry.getDevName());
/** 2. 判断是否在禁行区内
* 如果在 根据配置禁行区内断电配置进行断电
**/
boolean noRidingArea = asDeviceService.isNoRidingArea(device.getSn(), device.getAreaId());
if(noRidingArea){
String noRidingOutage = area.getNoRidingOutage();
//发送播报指令
asDeviceService.sendCommand(device.getMac(), Token.getToken(),IotConstants.COMMAND_PLAY3,"禁行区内播报");
if(noRidingOutage.equals("1") && !device.getIsAreaOutOutage().equals("1")){//禁行区内断电
//x秒后发送断电命令
scheduledExecutorService.schedule(() -> {
try {
asDeviceService.sendCommand(device.getMac(), Token.getToken(),IotConstants.COMMAND_QLOSE,"超出营运区断电");
device.setIsAreaOutOutage("0");
asDeviceService.updateAsDevice(device);
log.info("禁行区内发送断电命令:" +logEntry.getDevName());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
}, area.getOutage(), TimeUnit.SECONDS);
device.setIsAreaOutOutage("1");
asDeviceService.updateAsDevice(device);
}
}
}
/** 3.超出运营区外断电*/
boolean isAreaZone = asDeviceService.isAreaZone(device.getSn(), area);
if(isAreaZone){
String areaOutOutage = area.getAreaOutOutage();
//发送超出营运区播报指令
asDeviceService.sendCommand(device.getMac(), Token.getToken(),IotConstants.COMMAND_PLAY3,"超出营运区播报");
if(areaOutOutage.equals("1")){//超出营运区断电
//发送断电命令
asDeviceService.sendCommand(device.getMac(), Token.getToken(),IotConstants.COMMAND_QLOSE,"超出营运区断电");
log.info("超出营运区发送断电命令:" +logEntry.getDevName());
/** 3.超出运营区外断电*/
boolean isAreaZone = asDeviceService.isAreaZone(device.getSn(), area);
if(isAreaZone){
String areaOutOutage = area.getAreaOutOutage();
//发送超出营运区播报指令
asDeviceService.sendCommand(device.getMac(), Token.getToken(),IotConstants.COMMAND_PLAY3,"超出营运区播报");
if(areaOutOutage.equals("1") && !device.getIsAreaOutOutage().equals("1")){//超出营运区断电
//x秒后发送断电命令
scheduledExecutorService.schedule(() -> {
try {
asDeviceService.sendCommand(device.getMac(), Token.getToken(),IotConstants.COMMAND_QLOSE,"超出营运区断电");
device.setIsAreaOutOutage("0");
asDeviceService.updateAsDevice(device);
log.info("超出营运区发送断电命令:" +logEntry.getDevName());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
}, area.getOutage(), TimeUnit.SECONDS);
device.setIsAreaOutOutage("1");
asDeviceService.updateAsDevice(device);
}
}
}
/** 4.行程线路添加更新订单中的trip_route字段 */
Integer status = value.getStatus();
if(status == 1){//上电运行
log.info("上电运行:" +logEntry.getDevName());
if(BigDecimal.ZERO.compareTo(lon) != 0 && BigDecimal.ZERO.compareTo(lat) != 0){
//获取当前正在骑行中的订单
EtOrder etOrder = etOrderService.getCurrentOrder(device.getSn());
if(ObjectUtil.isNotNull(etOrder)){
JSONArray jsonArray;
if(StrUtil.isNotBlank(etOrder.getTripRouteStr())){
jsonArray = JSON.parseArray(etOrder.getTripRouteStr());
}else{
jsonArray = new JSONArray();
}
JSONArray newPoint = new JSONArray();
newPoint.add(lon);
newPoint.add(lat);
/** 4.行程线路添加更新订单中的trip_route字段 */
Integer status = value.getStatus();
if(status == 1){//上电运行
log.info("上电运行:" +logEntry.getDevName());
if(BigDecimal.ZERO.compareTo(lon) != 0 && BigDecimal.ZERO.compareTo(lat) != 0){
//获取当前正在骑行中的订单
EtOrder etOrder = etOrderService.getCurrentOrder(device.getSn());
if(ObjectUtil.isNotNull(etOrder)){
JSONArray jsonArray;
if(StrUtil.isNotBlank(etOrder.getTripRouteStr())){
jsonArray = JSON.parseArray(etOrder.getTripRouteStr());
}else{
jsonArray = new JSONArray();
}
JSONArray newPoint = new JSONArray();
newPoint.add(lon);
newPoint.add(lat);
// 优化轨迹如果获取到的定位与最后一个定位相同则不添加
if(jsonArray.size() > 0){
JSONArray lastPoint = (JSONArray) jsonArray.get(jsonArray.size() - 1);
if(lastPoint.get(0).equals(lon) && lastPoint.get(1).equals(lat)){
log.info("获取到的定位与最后一个定位相同,不添加线路");
// 优化轨迹如果获取到的定位与最后一个定位相同则不添加
if(jsonArray.size() > 0){
JSONArray lastPoint = (JSONArray) jsonArray.get(jsonArray.size() - 1);
if(lastPoint.get(0).equals(lon) && lastPoint.get(1).equals(lat)){
log.info("获取到的定位与最后一个定位相同,不添加线路");
}else{
jsonArray.add(newPoint);
jsonArray.toJSONString();
}
}else{
jsonArray.add(newPoint);
jsonArray.toJSONString();
}
}else{
jsonArray.add(newPoint);
jsonArray.toJSONString();
}
log.info("更新行程jsonArray" +jsonArray.toJSONString());
etOrder.setTripRouteStr(jsonArray.toJSONString());
Geometry geometry = GeoUtils.toGeometryByLinearRing(jsonArray.toJSONString());
String wkt = GeoUtils.wkt(geometry);
etOrder.setTripRoute(wkt);
int updateEtOrder = etOrderService.updateEtOrder(etOrder);
if (updateEtOrder > 0) {
log.info("更新行程线路成功==========================>" +logEntry.getDevName());
}else{
log.info("更新行程线路失败==========================>" +logEntry.getDevName());
log.info("更新行程jsonArray" +jsonArray.toJSONString());
etOrder.setTripRouteStr(jsonArray.toJSONString());
Geometry geometry = GeoUtils.toGeometryByLinearRing(jsonArray.toJSONString());
String wkt = GeoUtils.wkt(geometry);
etOrder.setTripRoute(wkt);
int updateEtOrder = etOrderService.updateEtOrder(etOrder);
if (updateEtOrder > 0) {
log.info("更新行程线路成功==========================>" +logEntry.getDevName());
}else{
log.info("更新行程线路失败==========================>" +logEntry.getDevName());
}
}
}
}
}
/** 5.低于电量(%)不得骑行,声音播报 */
if(electricQuantity <= model.getLowBatteryReminder()){
//发送低电量播报指令
/** 5.低于电量(%)不得骑行,声音播报 */
if(electricQuantity <= model.getLowBatteryReminder()){
//发送低电量播报指令
// asDeviceService.sendCommand(device.getMac(), Token.getToken(),IotConstants.COMMAND_PLAY6,"低电量播报");
log.info("低电量播报:" +logEntry.getDevName());
}
/** 6.低电量 生成换电工单*/
Integer replacementOrder = area.getAutoReplacementOrder();
Boolean aBoolean = etAdminOrderService.checkOrderUnique(device.getSn());
if (Integer.parseInt(device.getRemainingPower()) < replacementOrder && !aBoolean) {
/** 生成换电工单 */
EtAdminOrder adminOrder = new EtAdminOrder();
adminOrder.setDeviceMac(device.getMac());
adminOrder.setSn(device.getSn());
adminOrder.setBeforeElectric(Integer.parseInt(device.getRemainingPower()));
adminOrder.setOrderNo(IdUtils.randomUUID2());
adminOrder.setStatus(ServiceConstants.REPLACEMENT_ELECTRICITY_STATUS_UNDER_WAY);
adminOrder.setIsEffective(ServiceConstants.REPLACEMENT_ELECTRICITY_IS_EFFECTIVE_YES);
SysUser sysUser = sysUserService.selectUserByUserName("wx");//获取用户名为wx的维修人员
adminOrder.setAdminId(sysUser.getUserId());
adminOrder.setAdminName(sysUser.getUserName());
adminOrder.setLatitude(device.getLatitude());
adminOrder.setLongitude(device.getLongitude());
adminOrder.setType("2");
String location = device.getLongitude() + ","+device.getLatitude();
adminOrder.setAddress(CommonUtil.getAddressByGeo(location));
int i1 = etAdminOrderService.insertEtAdminOrder(adminOrder);
if (i1 > 0) {
log.info("生成换电工单成功");
}else{
throw new ServiceException("生成换电工单失败");
log.info("低电量播报:" +logEntry.getDevName());
}
/** 改变车辆状态 */
device.setStatus(ServiceConstants.VEHICLE_STATUS_IN_OFFLINE);
if (asDeviceService.updateAsDevice(device) > 0) {
log.info("车辆状态改成换电中");
}else{
throw new ServiceException("车辆状态更新失败");
/** 6.低电量 生成换电工单*/
Integer replacementOrder = area.getAutoReplacementOrder();
Boolean aBoolean = etAdminOrderService.checkOrderUnique(device.getSn());
if (Integer.parseInt(device.getRemainingPower()) < replacementOrder && !aBoolean) {
/** 生成换电工单 */
EtAdminOrder adminOrder = new EtAdminOrder();
adminOrder.setDeviceMac(device.getMac());
adminOrder.setSn(device.getSn());
adminOrder.setBeforeElectric(Integer.parseInt(device.getRemainingPower()));
adminOrder.setOrderNo(IdUtils.randomUUID2());
adminOrder.setStatus(ServiceConstants.REPLACEMENT_ELECTRICITY_STATUS_UNDER_WAY);
adminOrder.setIsEffective(ServiceConstants.REPLACEMENT_ELECTRICITY_IS_EFFECTIVE_YES);
SysUser sysUser = sysUserService.selectUserByUserName("wx");//获取用户名为wx的维修人员
adminOrder.setAdminId(sysUser.getUserId());
adminOrder.setAdminName(sysUser.getUserName());
adminOrder.setLatitude(device.getLatitude());
adminOrder.setLongitude(device.getLongitude());
adminOrder.setType("2");
String location = device.getLongitude() + ","+device.getLatitude();
adminOrder.setAddress(CommonUtil.getAddressByGeo(location));
int i1 = etAdminOrderService.insertEtAdminOrder(adminOrder);
if (i1 > 0) {
log.info("生成换电工单成功");
}else{
throw new ServiceException("生成换电工单失败");
}
/** 改变车辆状态 */
device.setStatus(ServiceConstants.VEHICLE_STATUS_IN_OFFLINE);
if (asDeviceService.updateAsDevice(device) > 0) {
log.info("车辆状态改成换电中");
}else{
throw new ServiceException("车辆状态更新失败");
}
}
}
// /** TODO 7.运营边界判断 几米判断? 3 播报运营边界 play2@ 是否靠近运营区边界*/
// boolean isCloseToTheBoundary = asDeviceService.isCloseToTheBoundary(device.getSn(), area);
// if(isCloseToTheBoundary){
@ -259,8 +295,9 @@ public class ReceiveController {
// asDeviceService.sendCommand(device.getMac(), Token.getToken(),IotConstants.COMMAND_PLAY2,"靠近营运边界播报");
// }
}else{
log.info("更新定位失败:" +logEntry.getDevName());
}else{
log.info("更新定位失败:" +logEntry.getDevName());
}
}
}else{
log.info("未找到车辆对象:" +logEntry.getDevName());

View File

@ -136,6 +136,9 @@ public class AsDevice implements Serializable{
/** 车牌号 */
private String vehicleNum;
/** 是否发送过断电指令 */
private String isAreaOutOutage;
/** 正在进行中的订单 */
@TableField(exist = false)
private List<EtOrder> etOrders;

View File

@ -29,10 +29,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="qrcode" column="qrcode" />
<result property="longitude" column="longitude" />
<result property="latitude" column="latitude" />
<result property="isAreaOutOutage" column="is_area_out_outage" />
</resultMap>
<sql id="selectAsDeviceVo">
select device_id, picture, device_name, mac, sn, model_id, vehicle_num, area_id, activation_time, online_status, create_by, create_time, update_by, update_time, last_time, remark, status, lock_status, location, remaining_power, voltage, qrcode, longitude, latitude from et_device
select device_id, picture, device_name, mac, sn, model_id, vehicle_num, area_id, activation_time, online_status, create_by, create_time, update_by, update_time, last_time, remark, status, lock_status, location, remaining_power, voltage, qrcode, longitude, latitude, is_area_out_outage from et_device
</sql>
<select id="selectAsDeviceList" parameterType="AsDevice" resultMap="AsDeviceResult">
@ -124,6 +125,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="qrcode != null">qrcode,</if>
<if test="longitude != null">longitude,</if>
<if test="latitude != null">latitude,</if>
<if test="isAreaOutOutage != null">is_area_out_outage,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="picture != null">#{picture},</if>
@ -149,6 +151,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="qrcode != null">#{qrcode},</if>
<if test="longitude != null">#{longitude},</if>
<if test="latitude != null">#{latitude},</if>
<if test="isAreaOutOutage != null">#{isAreaOutOutage},</if>
</trim>
</insert>
@ -178,6 +181,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="qrcode != null">qrcode = #{qrcode},</if>
<if test="longitude != null">longitude = #{longitude},</if>
<if test="latitude != null">latitude = #{latitude},</if>
<if test="isAreaOutOutage != null">is_area_out_outage = #{isAreaOutOutage},</if>
</trim>
where device_id = #{deviceId}
</update>
@ -208,6 +212,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="qrcode != null">qrcode = #{qrcode},</if>
<if test="longitude != null">longitude = #{longitude},</if>
<if test="latitude != null">latitude = #{latitude},</if>
<if test="is_area_out_outage != null">is_area_out_outage = #{is_area_out_outage},</if>
</trim>
where sn = #{sn}
</update>