|
|
@@ -1,22 +1,29 @@
|
|
|
package com.kym.service.enplus.impl;
|
|
|
|
|
|
+import cn.hutool.extra.mail.MailUtil;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
+import com.kym.entity.admin.MonitorLog;
|
|
|
+import com.kym.entity.common.RedisKeys;
|
|
|
+import com.kym.entity.enplus.EnConnectorStatusInfo;
|
|
|
import com.kym.entity.miniapp.ChargeOrder;
|
|
|
import com.kym.entity.miniapp.WalletDetail;
|
|
|
+import com.kym.service.admin.MonitorLogService;
|
|
|
import com.kym.service.enplus.EnNotifyService;
|
|
|
import com.kym.service.enplus.EnPlusService;
|
|
|
import com.kym.service.miniapp.AccountService;
|
|
|
import com.kym.service.miniapp.ChargeOrderService;
|
|
|
import com.kym.service.miniapp.WalletDetailService;
|
|
|
+import com.kym.service.utils.KymCache;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* @author skyline
|
|
|
@@ -28,17 +35,29 @@ import java.time.format.DateTimeFormatter;
|
|
|
public class EnNotifyServiceImpl implements EnNotifyService {
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(EnNotifyServiceImpl.class);
|
|
|
|
|
|
- @Autowired
|
|
|
- private EnPlusService enPlusService;
|
|
|
+ private final EnPlusService enPlusService;
|
|
|
|
|
|
- @Autowired
|
|
|
- private ChargeOrderService chargeOrderService;
|
|
|
+ private final ChargeOrderService chargeOrderService;
|
|
|
|
|
|
- @Autowired
|
|
|
- private AccountService accountService;
|
|
|
+ private final AccountService accountService;
|
|
|
|
|
|
- @Autowired
|
|
|
- private WalletDetailService walletDetailService;
|
|
|
+ private final WalletDetailService walletDetailService;
|
|
|
+
|
|
|
+ private final MonitorLogService monitorLogService;
|
|
|
+
|
|
|
+ private final KymCache kymCache;
|
|
|
+
|
|
|
+ private final RedisTemplate<String, String> redisTemplate;
|
|
|
+
|
|
|
+ public EnNotifyServiceImpl(EnPlusService enPlusService, ChargeOrderService chargeOrderService, AccountService accountService, WalletDetailService walletDetailService, MonitorLogService monitorLogService, KymCache kymCache, RedisTemplate<String, String> redisTemplate) {
|
|
|
+ this.enPlusService = enPlusService;
|
|
|
+ this.chargeOrderService = chargeOrderService;
|
|
|
+ this.accountService = accountService;
|
|
|
+ this.walletDetailService = walletDetailService;
|
|
|
+ this.monitorLogService = monitorLogService;
|
|
|
+ this.kymCache = kymCache;
|
|
|
+ this.redisTemplate = redisTemplate;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* EN+ 充电站设备状态变化推送
|
|
|
@@ -50,7 +69,32 @@ public class EnNotifyServiceImpl implements EnNotifyService {
|
|
|
public String handleNotificationStationStatus(JSONObject json) {
|
|
|
var data = enPlusService.signValidation(json);
|
|
|
LOGGER.info("【EN+推送】收到充电桩设备状态变化推送:\n{},解密数据:\\n{}\"", json, data);
|
|
|
- // TODO: 2023-08-21 更新数据库中设备的状态,如有设备离线,发送通知
|
|
|
+ // TODO: 2023-08-21 更新数据库中设备的状态
|
|
|
+ var connectorStatusInfo = JSONObject.parseObject(data).getJSONObject("ConnectorStatusInfo").toJavaObject(EnConnectorStatusInfo.class);
|
|
|
+ if (connectorStatusInfo.getStatus() == 0) {
|
|
|
+ // 如果设备离线,则存入redis,有效期24h,5分钟之内如果收到该设备上线的推送,则不发送通知并删除redis记录,否则发送通知
|
|
|
+ var monitorLog = new MonitorLog()
|
|
|
+ .setStationId(kymCache.getStationId(connectorStatusInfo.getConnectorId()))
|
|
|
+ .setSn(connectorStatusInfo.getConnectorId())
|
|
|
+ .setOfflineTime(LocalDateTime.now()).setType(2)
|
|
|
+ .setOfflineStatus(connectorStatusInfo.getStatus());
|
|
|
+ monitorLogService.save(monitorLog);
|
|
|
+ redisTemplate.opsForValue().set(RedisKeys.OFFLINE.concat(connectorStatusInfo.getConnectorId()), "", 1, TimeUnit.DAYS);
|
|
|
+ MailUtil.send("skyline@kuaiyuman.cn", "通知", "站点:%s,设备%s离线".formatted(monitorLog.getStationId(), monitorLog.getSn()), false);
|
|
|
+ } else {
|
|
|
+ // 查询redis是否有记录,有就删除并更新数据库恢复时间
|
|
|
+ var exist = redisTemplate.hasKey(RedisKeys.OFFLINE.concat(connectorStatusInfo.getConnectorId()));
|
|
|
+ if (Boolean.TRUE.equals(exist)) {
|
|
|
+ monitorLogService.lambdaUpdate()
|
|
|
+ .eq(MonitorLog::getSn, connectorStatusInfo.getConnectorId())
|
|
|
+ .eq(MonitorLog::getIsRecover, 0) // 未恢复的记录
|
|
|
+ .set(MonitorLog::getRecoverTime, LocalDateTime.now())
|
|
|
+ .set(MonitorLog::getIsRecover, 1) // 设置为已恢复
|
|
|
+ .update();
|
|
|
+ redisTemplate.delete(RedisKeys.OFFLINE.concat(connectorStatusInfo.getConnectorId()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return """
|
|
|
{
|
|
|
"Status":%d
|