Commit af0a53a5 by 林永健

[+] 处理实时数据上传消息

1 parent 0e9ab5ef
......@@ -39,28 +39,28 @@ public class ChargingGun implements Serializable {
* 枪编号(1byte:BCD)
*/
@Column(name = "gun_number")
private String gunNumber;
private Byte gunNumber;
/**
* 状态
* @see cn.com.glsx.evbusiness.modules.enums.GunStatusEnums
*/
@Column(name = "gun_status")
private Integer gunStatus;
private Byte gunStatus;
/**
* 归位状态
* @see cn.com.glsx.evbusiness.modules.enums.GunReturnStatusEnums
*/
@Column(name = "if_return")
private byte ifReturn;
private Byte ifReturn;
/**
* 插枪状态
* @see cn.com.glsx.evbusiness.modules.enums.GunInUseStatusEnums
*/
@Column(name = "if_charging")
private byte ifCharging;
private Byte ifCharging;
/**
* 枪线编码 BIN 码
......@@ -70,18 +70,11 @@ public class ChargingGun implements Serializable {
private String gunWireNumber;
/**
* SOC BIN 码
* 待机置零;交流桩置零
*/
@Column(name = "soc")
private byte soc;
/**
* 故障类型
* @see cn.com.glsx.evbusiness.modules.enums.HardwareFaultEnums
*/
@Column(name = "hardware_fault")
private String hardwareFault;
private Short hardwareFault;
/**
* 创建人
......
package cn.com.glsx.evbusiness.modules.entity;
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.*;
......@@ -9,26 +10,26 @@ public class ChargingOrderMonitorinfo {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private Double voltage;
private BigDecimal voltage;
private Double current;
private BigDecimal current;
/**
* 累计时间
* 累计时间,单位分钟
*/
@Column(name = "acc_time")
private Date accTime;
private Integer accTime;
/**
* 度数
*/
private Double degrees;
private BigDecimal degrees;
/**
* 已冲金额
*/
@Column(name = "charged_amount")
private Double chargedAmount;
private BigDecimal chargedAmount;
/**
* 订单号
......@@ -60,28 +61,28 @@ public class ChargingOrderMonitorinfo {
/**
* @return voltage
*/
public Double getVoltage() {
public BigDecimal getVoltage() {
return voltage;
}
/**
* @param voltage
*/
public void setVoltage(Double voltage) {
public void setVoltage(BigDecimal voltage) {
this.voltage = voltage;
}
/**
* @return current
*/
public Double getCurrent() {
public BigDecimal getCurrent() {
return current;
}
/**
* @param current
*/
public void setCurrent(Double current) {
public void setCurrent(BigDecimal current) {
this.current = current;
}
......@@ -90,7 +91,7 @@ public class ChargingOrderMonitorinfo {
*
* @return acc_time - 累计时间
*/
public Date getAccTime() {
public Integer getAccTime() {
return accTime;
}
......@@ -99,7 +100,7 @@ public class ChargingOrderMonitorinfo {
*
* @param accTime 累计时间
*/
public void setAccTime(Date accTime) {
public void setAccTime(Integer accTime) {
this.accTime = accTime;
}
......@@ -108,7 +109,7 @@ public class ChargingOrderMonitorinfo {
*
* @return degrees - 度数
*/
public Double getDegrees() {
public BigDecimal getDegrees() {
return degrees;
}
......@@ -117,7 +118,7 @@ public class ChargingOrderMonitorinfo {
*
* @param degrees 度数
*/
public void setDegrees(Double degrees) {
public void setDegrees(BigDecimal degrees) {
this.degrees = degrees;
}
......@@ -126,7 +127,7 @@ public class ChargingOrderMonitorinfo {
*
* @return charged_amount - 已冲金额
*/
public Double getChargedAmount() {
public BigDecimal getChargedAmount() {
return chargedAmount;
}
......@@ -135,7 +136,7 @@ public class ChargingOrderMonitorinfo {
*
* @param chargedAmount 已冲金额
*/
public void setChargedAmount(Double chargedAmount) {
public void setChargedAmount(BigDecimal chargedAmount) {
this.chargedAmount = chargedAmount;
}
......
......@@ -12,10 +12,10 @@ import lombok.Getter;
@AllArgsConstructor
@Getter
public enum GunInUseStatusEnums {
NO(0, "否"),
YES(1, "是");
NO((byte) 0, "否"),
YES((byte) 1, "是");
private Integer code;
private Byte code;
private String msg;
}
......@@ -12,11 +12,11 @@ import lombok.Getter;
@AllArgsConstructor
@Getter
public enum GunReturnStatusEnums {
NO(0, "否"),
YES(1, "是"),
UNKNOWN(2, "未知");
NO((byte) 0, "否"),
YES((byte) 1, "是"),
UNKNOWN((byte) 2, "未知");
private Integer code;
private Byte code;
private String msg;
}
......@@ -12,12 +12,12 @@ import lombok.Getter;
@AllArgsConstructor
@Getter
public enum GunStatusEnums {
ONLINE(0, "离线"),
FAULT(1, "故障"),
IDLE(2, "空闲"),
CHARGING(3, "充电中");
ONLINE((byte) 0, "离线"),
FAULT((byte) 1, "故障"),
IDLE((byte) 2, "空闲"),
CHARGING((byte) 3, "充电中");
private Integer code;
private Byte code;
private String msg;
}
......@@ -12,21 +12,21 @@ import lombok.Getter;
@AllArgsConstructor
@Getter
public enum HardwareFaultEnums {
BIT1(1, "急停按钮动作故障"),
BIT2(3, "无可用整流模块"),
BIT3(7, "出风口温度过高"),
BIT4(15, "交流防雷故障"),
BIT5(31, "交直流模块DC20通信中断"),
BIT6(63, "绝缘检测模块FC08通信中断"),
BIT7(127, "电度表通信中断"),
BIT8(255, "读卡器通信中断"),
BIT9(511, "RC10通信中断"),
BIT10(1023, "风扇调速板故障"),
BIT11(2047, "直流熔断器故障"),
BIT12(4095, "高压接触器故障"),
BIT13(8191, "门打开");
BIT1((short) 1, "急停按钮动作故障"),
BIT2((short) 3, "无可用整流模块"),
BIT3((short) 7, "出风口温度过高"),
BIT4((short) 15, "交流防雷故障"),
BIT5((short) 31, "交直流模块DC20通信中断"),
BIT6((short) 63, "绝缘检测模块FC08通信中断"),
BIT7((short) 127, "电度表通信中断"),
BIT8((short) 255, "读卡器通信中断"),
BIT9((short) 511, "RC10通信中断"),
BIT10((short) 1023, "风扇调速板故障"),
BIT11((short) 2047, "直流熔断器故障"),
BIT12((short) 4095, "高压接触器故障"),
BIT13((short) 8191, "门打开");
private Integer code;
private String msg;
private Short code;
private String msg;
}
......@@ -4,10 +4,15 @@ import cn.com.glsx.evbusiness.modules.entity.ChargingGun;
import cn.com.glsx.evbusiness.modules.model.ChargingGunSearch;
import com.glsx.plat.mybatis.mapper.CommonBaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface ChargingGunMapper extends CommonBaseMapper<ChargingGun> {
List<ChargingGun> page(ChargingGunSearch search);
@Select("select * from charging_gun where pileId = #{pileId} and gun_number = #{gunNumber}")
ChargingGun findByPileIdAndNumber(@Param("pileId") String pileId, @Param("gunNumber") byte gunNumber);
}
\ No newline at end of file
......@@ -6,6 +6,8 @@ import lombok.ToString;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;
/**
* 上传实时监测数据
......@@ -148,4 +150,15 @@ public class RealTimeUploadData implements Serializable {
*/
private short hardwareMalfunction;
/**
* 消息接收时间
*/
private Date timestamp;
public void setTimestamp(Date timestamp) {
if (Objects.isNull(timestamp)){
timestamp = new Date();
}
this.timestamp = timestamp;
}
}
......@@ -6,9 +6,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.jmx.support.RegistrationPolicy;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.scheduling.annotation.EnableAsync;
import javax.annotation.PostConstruct;
import java.util.TimeZone;
......@@ -24,6 +22,7 @@ import static java.util.TimeZone.getTimeZone;
@EnableDiscoveryClient
@EnableFeignClients(basePackages = {"cn.com.glsx.*.api"})
@SpringBootApplication
@EnableAsync
public class Application {
public static void main(String[] args) {
......
package cn.com.glsx.evbusiness.modules.mq.consumer;
import cn.com.glsx.evbusiness.config.RabbitMQConfig;
import cn.com.glsx.evbusiness.modules.entity.ChargingOrderMonitorinfo;
import cn.com.glsx.evbusiness.modules.model.ocpp16.RealTimeUploadData;
import cn.com.glsx.evbusiness.modules.service.ChargingGunService;
import cn.com.glsx.evbusiness.modules.service.OrderService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
......@@ -11,28 +13,40 @@ import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Objects;
/**
* 实时监控消息
*
*
*/
@Slf4j
@RabbitListener(queues = RabbitMQConfig.DATA_REPORT_QUEUE_NAME)
@Component
public class RealTimeMonQueen {
@Autowired
private OrderService orderService;
@RabbitHandler
public void process(String jsonDataStr, Message message) {
try {
ChargingOrderMonitorinfo chargingOrderMonitorinfo = JSONObject.parseObject(jsonDataStr,
ChargingOrderMonitorinfo.class);
orderService.addOrderMonitotInfo(chargingOrderMonitorinfo);
} catch (Exception e) {
e.printStackTrace();
log.info("实时监控处理异常{}",e.getMessage());
@Autowired
private OrderService orderService;
@Autowired
private ChargingGunService gunService;
@RabbitHandler
public void process(String jsonDataStr, Message message) {
try {
RealTimeUploadData data = JSONObject.parseObject(jsonDataStr, RealTimeUploadData.class);
if (Objects.isNull(data)) {
throw new RuntimeException("data is empty or parse fail, raw data is: " + JSON.toJSONString(jsonDataStr));
}
data.setTimestamp(message.getMessageProperties().getTimestamp());
// 订单记录
orderService.addOrderMonitorInfo(data);
// 枪状态变更
gunService.syncStatus(data);
log.debug("实时监控处理成功, message: {}", data);
} catch (Exception e) {
e.printStackTrace();
log.info("实时监控处理异常{}", e.getMessage());
}
}
}
}
......@@ -6,12 +6,18 @@ import cn.com.glsx.evbusiness.modules.mapper.ChargingGunMapper;
import cn.com.glsx.evbusiness.modules.mapper.ChargingPileMapper;
import cn.com.glsx.evbusiness.modules.model.ChargingGunResp;
import cn.com.glsx.evbusiness.modules.model.ChargingGunSearch;
import cn.com.glsx.evbusiness.modules.model.ocpp16.RealTimeUploadData;
import cn.com.glsx.evbusiness.utils.CopyUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Objects;
/**
* @Title:
......@@ -20,6 +26,7 @@ import java.util.List;
* @Date 2023/11/14
**/
@Service
@Slf4j
public class ChargingGunService {
@Autowired
......@@ -45,4 +52,25 @@ public class ChargingGunService {
res.setPileInfo(pile);
return res;
}
/**
* 同步上传数据到枪实体
*/
@Async
public void syncStatus(RealTimeUploadData data) {
ChargingGun item = gunMapper.findByPileIdAndNumber(data.getPileId(), data.getGunNumber());
if (Objects.isNull(item)) {
item = new ChargingGun();
CopyUtils.copyPropertiesWithOutEmptyFields(data, item);
item.setCreatedTime(new Date());
gunMapper.insert(item);
} else {
Date lastTime = Objects.isNull(item.getUpdatedTime()) ? item.getUpdatedTime() : item.getCreatedTime();
if (Objects.nonNull(lastTime) && lastTime.after(data.getTimestamp())) {
log.warn("收到迟到数据. 现有数据time: {} > 接收数据time: {}", lastTime, data.getTimestamp());
}
CopyUtils.copyPropertiesWithOutEmptyFields(data, item);
gunMapper.updateByPrimaryKey(item);
}
}
}
......@@ -9,6 +9,7 @@ import cn.com.glsx.evbusiness.modules.entity.ChargingOrderMonitorinfo;
import cn.com.glsx.evbusiness.modules.mapper.ChargingOrderDetailMapper;
import cn.com.glsx.evbusiness.modules.mapper.ChargingOrderMapper;
import cn.com.glsx.evbusiness.modules.mapper.ChargingOrderMonitorinfoMapper;
import cn.com.glsx.evbusiness.modules.model.ocpp16.RealTimeUploadData;
import cn.com.glsx.evbusiness.modules.model.ocpp16.order.ClosePileBO;
import cn.com.glsx.evbusiness.modules.model.ocpp16.order.OrderCreateBO;
import cn.com.glsx.evbusiness.modules.model.ocpp16.order.OrderSearch;
......@@ -22,6 +23,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
......@@ -173,9 +175,16 @@ public class OrderService {
/**
* 新增监控状态 (实时状态更新处理)
*/
public void addOrderMonitotInfo(ChargingOrderMonitorinfo chargingOrderMonitorinfo) {
@Async
public void addOrderMonitorInfo(RealTimeUploadData data) {
ChargingOrderMonitorinfo chargingOrderMonitorinfo = new ChargingOrderMonitorinfo(data.getTransactionSerialNumber());
chargingOrderMonitorinfo.setAccTime(Short.toUnsignedInt(data.getAccumulatedChargingTime()));
chargingOrderMonitorinfo.setChargedAmount(data.getChargingMoney());
chargingOrderMonitorinfo.setCurrent(data.getOutputCurrent());
chargingOrderMonitorinfo.setVoltage(data.getOutputVoltage());
chargingOrderMonitorinfo.setDegrees(data.getChargingDegree());
chargingOrderMonitorinfo.setMonitorTime(data.getTimestamp());
chargingOrderMonitorinfoMapper.insert(chargingOrderMonitorinfo);
// 更新枪的状态 TODO
}
......
package cn.com.glsx.evbusiness.utils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import java.util.HashSet;
import java.util.Set;
public class CopyUtils {
public static String[] getNullPropertyNames (Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set emptyNames = new HashSet();
for(java.beans.PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) emptyNames.add(pd.getName());
}
String[] result = new String[emptyNames.size()];
return (String[]) emptyNames.toArray(result);
}
public static void copyPropertiesWithOutEmptyFields(Object src, Object target) {
BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!