feat: 增强 SIM 流量卡管理功能
This commit is contained in:
parent
b011289fdf
commit
3ea86962a0
@ -12,4 +12,8 @@ public interface GnssDeviceMapper extends MPJBaseMapper<GnssDevice> {
|
||||
|
||||
@Update({"update gnssdevices set syn=false where group_id=#{group_id}"})
|
||||
int setSynFlagByGroupId(int group_id);
|
||||
|
||||
@Update({"update gnssdevices set iccid=#{iccid} where deviceid=#{deviceId}"})
|
||||
int updateIccidByDeviceId(String deviceId, String iccid);
|
||||
|
||||
}
|
||||
|
||||
@ -4,9 +4,7 @@ import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.imdroid.secapi.dto.GnssStatusJoin;
|
||||
import com.imdroid.secapi.dto.SimCard;
|
||||
import com.imdroid.secapi.dto.SimCardsMapper;
|
||||
import com.imdroid.secapi.dto.*;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
@ -20,7 +18,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.*;
|
||||
|
||||
@ -35,7 +32,10 @@ public class SimCardQueryServiceImpl implements SimCardQueryService{
|
||||
private String KEY;
|
||||
|
||||
@Autowired
|
||||
SimCardsMapper simCardsMapper;
|
||||
TrafficCardMapper trafficCardMapper;
|
||||
|
||||
@Autowired
|
||||
GnssDeviceMapper gnssDeviceMapper;
|
||||
|
||||
@Override
|
||||
public BaseResponse<CardInfoData> queryCardInfo(GnssStatusJoin device) {
|
||||
@ -59,47 +59,82 @@ public class SimCardQueryServiceImpl implements SimCardQueryService{
|
||||
|
||||
private <T> BaseResponse<T> executeQuery(GnssStatusJoin device, String path, Class<T> responseType) {
|
||||
try {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("username", USERNAME);
|
||||
params.put("key", KEY);
|
||||
params.put("card", device.getIccid());
|
||||
params.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
|
||||
BaseResponse<T> response = queryByParams(device.getIccid(), path, responseType);
|
||||
|
||||
String signature = calculateSignature(params);
|
||||
params.put("signature", signature);
|
||||
/*
|
||||
系统中存在一些奇怪的卡 :
|
||||
|
||||
logger.info("Request params: {}", params);
|
||||
String response = sendHttpPost(path, params);
|
||||
logger.info("查询响应: 设备={}, ICCID={}, 响应={}",
|
||||
device.getDeviceid(), device.getIccid(), response);
|
||||
向 DTU发送AT指令查询 ICCID 返回的值比如是:89861124224084565106,但实际是 8986112422408456510B,
|
||||
从而导致卡商无法查询到该卡,正确的方法是舍弃最后一位,比如 8986112422408456510
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
|
||||
所以,当出现 " 无效的卡号 " 错误的时候,应该尝试去舍弃最后一位, 这样才是正确的 ICCID 号码
|
||||
|
||||
// 特殊处理CardStatus的数组响应
|
||||
if (responseType == CardStatusData.class) {
|
||||
JsonNode root = mapper.readTree(response);
|
||||
if (root.get("Status").asInt() == 1 && root.get("Data").isArray()) {
|
||||
CardStatusData statusData = new CardStatusData();
|
||||
statusData.setStatusCode(root.get("Data").get(0).asInt());
|
||||
statusData.setStatusDesc(root.get("Data").get(1).asText());
|
||||
BaseResponse<CardStatusData> baseResponse = new BaseResponse<>();
|
||||
baseResponse.setStatus(1);
|
||||
baseResponse.setMessage("Success");
|
||||
baseResponse.setData(statusData);
|
||||
return (BaseResponse<T>) baseResponse;
|
||||
*/
|
||||
if (shouldTryTruncatedIccid(response) && device.getIccid() != null && device.getIccid().length() > 1) {
|
||||
String truncatedIccid = device.getIccid().substring(0, device.getIccid().length() - 1);
|
||||
BaseResponse<T> retryResponse = queryByParams(truncatedIccid, path, responseType);
|
||||
if (retryResponse != null && retryResponse.getStatus() == 1) {
|
||||
updateGnssDeviceIccid(device, truncatedIccid);
|
||||
device.setIccid(truncatedIccid);
|
||||
return retryResponse;
|
||||
}
|
||||
}
|
||||
|
||||
JavaType type = mapper.getTypeFactory().constructParametricType(BaseResponse.class, responseType);
|
||||
return mapper.readValue(response, type);
|
||||
|
||||
return response;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询失败: 设备={}, 错误={}", device.getDeviceid(), e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private <T> BaseResponse<T> queryByParams(String iccid, String path, Class<T> responseType) throws Exception {
|
||||
Map<String, String> params = buildQueryParams(iccid);
|
||||
String response = sendHttpPost(path, params);
|
||||
logger.debug("查询响应: ICCID={}, 响应={}", iccid, response);
|
||||
|
||||
return parseResponse(response, responseType);
|
||||
}
|
||||
|
||||
private Map<String, String> buildQueryParams(String iccid) {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("username", USERNAME);
|
||||
params.put("key", KEY);
|
||||
params.put("card", iccid);
|
||||
params.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
|
||||
params.put("signature", calculateSignature(params));
|
||||
return params;
|
||||
}
|
||||
|
||||
private <T> BaseResponse<T> parseResponse(String response, Class<T> responseType) throws Exception {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
|
||||
|
||||
// 特殊处理CardStatus的数组响应
|
||||
if (responseType == CardStatusData.class) {
|
||||
JsonNode root = mapper.readTree(response);
|
||||
if (root.get("Status").asInt() == 1 && root.get("Data").isArray()) {
|
||||
CardStatusData statusData = new CardStatusData();
|
||||
statusData.setStatusCode(root.get("Data").get(0).asInt());
|
||||
statusData.setStatusDesc(root.get("Data").get(1).asText());
|
||||
BaseResponse<CardStatusData> baseResponse = new BaseResponse<>();
|
||||
baseResponse.setStatus(1);
|
||||
baseResponse.setMessage("Success");
|
||||
baseResponse.setData(statusData);
|
||||
return (BaseResponse<T>) baseResponse;
|
||||
}
|
||||
}
|
||||
|
||||
JavaType type = mapper.getTypeFactory().constructParametricType(BaseResponse.class, responseType);
|
||||
return mapper.readValue(response, type);
|
||||
}
|
||||
|
||||
private boolean shouldTryTruncatedIccid(BaseResponse<?> response) {
|
||||
return response != null &&
|
||||
(response.getStatus() == 0 || response.getStatus() == -6) &&
|
||||
response.getMessage() != null &&
|
||||
response.getMessage().contains("无效的卡号");
|
||||
}
|
||||
|
||||
private String calculateSignature(Map<String, String> params) {
|
||||
try {
|
||||
List<String> paramList = new ArrayList<>();
|
||||
@ -147,30 +182,34 @@ public class SimCardQueryServiceImpl implements SimCardQueryService{
|
||||
return device.getIccid() != null && !device.getIccid().trim().isEmpty();
|
||||
}
|
||||
|
||||
public SimCard CreateOrUpdateSimCard(GnssStatusJoin device) {
|
||||
SimCard simCard = simCardsMapper.queryByDeviceId(device.getDeviceid());
|
||||
if (simCard == null) {
|
||||
simCard = createNewSimCard(device);
|
||||
public TrafficCard createOrUpdateTrafficCard(GnssStatusJoin device) {
|
||||
TrafficCard card = trafficCardMapper.selectById(device.getIccid());
|
||||
if (card == null) {
|
||||
card = new TrafficCard();
|
||||
card.setIccid(device.getIccid());
|
||||
card.setStatus(-1);
|
||||
card.setUpdateTime(new Date());
|
||||
trafficCardMapper.insert(card);
|
||||
}
|
||||
return simCard;
|
||||
}
|
||||
|
||||
public SimCard createNewSimCard(GnssStatusJoin device) {
|
||||
SimCard newCard = new SimCard();
|
||||
newCard.setDeviceid(device.getDeviceid());
|
||||
newCard.setUpdatetime(new Date());
|
||||
newCard.setIccid(device.getIccid());
|
||||
newCard.setStatus(-1);
|
||||
newCard.setMsisdn("");
|
||||
newCard.setRemaining(BigDecimal.ZERO);
|
||||
newCard.setUsed(BigDecimal.ZERO);
|
||||
newCard.setTotal(BigDecimal.ZERO);
|
||||
|
||||
simCardsMapper.insert(newCard);
|
||||
return newCard;
|
||||
return card;
|
||||
}
|
||||
|
||||
public boolean isValidResponse(BaseResponse<?> response) {
|
||||
return response != null && response.getStatus() == 1;
|
||||
}
|
||||
|
||||
private void updateGnssDeviceIccid(GnssStatusJoin device, String newIccid) {
|
||||
try {
|
||||
String originalIccid = device.getIccid();
|
||||
|
||||
GnssDevice gnssDevice = gnssDeviceMapper.queryByDeviceId(device.getDeviceid());
|
||||
if (gnssDevice != null) {
|
||||
gnssDeviceMapper.updateIccidByDeviceId(device.getDeviceid(), newIccid);
|
||||
logger.debug("更新设备ICCID: 设备={}, 原ICCID={}, 新ICCID={}",
|
||||
device.getDeviceid(), originalIccid, newIccid);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("更新设备ICCID失败: 设备={}, 错误={}", device.getDeviceid(), e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,104 @@
|
||||
package com.imdroid.beidou_ehm.simcard;
|
||||
|
||||
import com.imdroid.secapi.dto.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Service
|
||||
public class TrafficCardService {
|
||||
|
||||
@Autowired
|
||||
private TrafficCardMapper trafficCardMapper;
|
||||
|
||||
@Autowired
|
||||
private TrafficDeviceMappingMapper mappingMapper;
|
||||
|
||||
@Autowired
|
||||
private GnssDeviceMapper deviceMapper;
|
||||
|
||||
@Transactional
|
||||
public boolean checkAndHandleICCIDChanges(GnssStatusJoin device) {
|
||||
if (device == null || device.getIccid() == null || device.getIccid().trim().isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String currentIccid = device.getIccid().trim();
|
||||
|
||||
// 查询当前设备的活跃映射记录
|
||||
TrafficDeviceMapping activeMapping = mappingMapper.findActiveByDeviceId(device.getDeviceid());
|
||||
if (activeMapping == null) {
|
||||
// 第一次为设备创建映射
|
||||
createNewMapping(device.getDeviceid(), currentIccid);
|
||||
return true;
|
||||
} else if (!currentIccid.equals(activeMapping.getIccid())) {
|
||||
// ICCID变更,关闭旧映射,创建新映射
|
||||
closeExistingMapping(activeMapping);
|
||||
createNewMapping(device.getDeviceid(), currentIccid);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public TrafficCard getOrCreateTrafficCard(String iccid) {
|
||||
TrafficCard card = trafficCardMapper.findByIccid(iccid);
|
||||
|
||||
if (card == null) {
|
||||
card = new TrafficCard();
|
||||
card.setIccid(iccid);
|
||||
card.setMsisdn("");
|
||||
card.setStatus(TrafficCard.STATUS_UNKNOWN);
|
||||
card.setRemaining(0);
|
||||
card.setTotal(0);
|
||||
card.setUsed(0);
|
||||
card.setUpdateTime(new Date());
|
||||
card.setQueryStatus(TrafficCard.QUERY_STATUS_NORMAL);
|
||||
trafficCardMapper.insert(card);
|
||||
}
|
||||
|
||||
return card;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void updateDeviceSimMapping(String deviceId, String iccid) {
|
||||
getOrCreateTrafficCard(iccid);
|
||||
|
||||
// 查询现有映射
|
||||
TrafficDeviceMapping activeMapping = mappingMapper.findActiveByDeviceId(deviceId);
|
||||
|
||||
if (activeMapping != null) {
|
||||
// 如果ICCID变化,关闭现有映射并创建新映射
|
||||
if (!iccid.equals(activeMapping.getIccid())) {
|
||||
closeExistingMapping(activeMapping);
|
||||
createNewMapping(deviceId, iccid);
|
||||
}
|
||||
} else {
|
||||
// 没有现有映射,创建新映射
|
||||
createNewMapping(deviceId, iccid);
|
||||
}
|
||||
}
|
||||
|
||||
public void markCardAsNotCurrentVendor(String iccid) {
|
||||
trafficCardMapper.updateQueryStatus(iccid, TrafficCard.QUERY_STATUS_NOT_CURRENT_VENDOR);
|
||||
}
|
||||
|
||||
public void markCardAsQueryFailed(String iccid) {
|
||||
trafficCardMapper.updateQueryStatus(iccid, TrafficCard.QUERY_STATUS_OTHER_ERROR);
|
||||
}
|
||||
|
||||
private void createNewMapping(String deviceId, String iccid) {
|
||||
TrafficDeviceMapping newMapping = new TrafficDeviceMapping();
|
||||
newMapping.setDeviceid(deviceId);
|
||||
newMapping.setIccid(iccid);
|
||||
newMapping.setStartTime(new Date());
|
||||
newMapping.setEndTime(null);
|
||||
mappingMapper.insert(newMapping);
|
||||
}
|
||||
|
||||
private void closeExistingMapping(TrafficDeviceMapping mapping) {
|
||||
mappingMapper.endMapping(mapping.getId());
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,5 @@
|
||||
package com.imdroid.beidou_ehm.task;
|
||||
|
||||
import com.alibaba.excel.util.StringUtils;
|
||||
import com.imdroid.beidou_ehm.service.WarningService;
|
||||
import com.imdroid.beidou_ehm.simcard.*;
|
||||
import com.imdroid.common.util.ThreadManager;
|
||||
@ -13,8 +12,6 @@ import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@ -24,29 +21,38 @@ import java.util.concurrent.TimeUnit;
|
||||
public class SimStatusChecker {
|
||||
// SIM 卡流量和状态查询定时任务
|
||||
// 1. 每小时任务
|
||||
// a. 通过CardInfo 检查 iccid 和 sim卡号
|
||||
// b. 通过QueryCardStatus 检查SIM卡当前的状态
|
||||
// a. 通过 CardInfo 检查 iccid 和 sim卡号
|
||||
// b. 通过 QueryCardStatus 检查SIM卡当前的状态
|
||||
// 2. 每两小时任务
|
||||
// a. QueryGprs 检查 SIM 流量
|
||||
final Logger logger = LoggerFactory.getLogger(SimStatusChecker.class);
|
||||
|
||||
@Autowired
|
||||
private GnssStatusMapper gnssStatusMapper;
|
||||
|
||||
@Autowired
|
||||
private SimCardsMapper simCardsMapper;
|
||||
private GnssDeviceMapper deviceMapper;
|
||||
|
||||
@Autowired
|
||||
private TrafficCardMapper trafficCardMapper;
|
||||
|
||||
@Autowired
|
||||
private TrafficRecordMapper trafficRecordMapper;
|
||||
|
||||
@Autowired
|
||||
private WarningService warningService;
|
||||
|
||||
@Autowired
|
||||
private TrafficCardService trafficCardService;
|
||||
|
||||
@Autowired
|
||||
private SimCardQueryServiceImpl simCardQueryServiceImpl;
|
||||
@Autowired
|
||||
WarningService warningService;
|
||||
|
||||
// 每小时执行一次状态检查调度
|
||||
@Scheduled(cron = "0 0 * * * ?")
|
||||
//@Scheduled(cron = "0 */10 * * * ?")
|
||||
private void scheduleSimCardStatusCheck() {
|
||||
List<GnssStatusJoin> onlineDevices = gnssStatusMapper.queryOnline();
|
||||
logger.debug("当前在线设备数量: {}", onlineDevices.size());
|
||||
|
||||
// logger.debug("当前在线设备数量: {}", onlineDevices.size());
|
||||
for (GnssStatusJoin onlineDevice : onlineDevices) {
|
||||
int delay = Math.abs(onlineDevice.getDeviceid().hashCode() % 3600 );
|
||||
//logger.debug("- 设备: {}, SIM状态查询,延迟执行: {}秒", onlineDevice.getDeviceid(), delay);
|
||||
@ -79,13 +85,23 @@ public class SimStatusChecker {
|
||||
|
||||
private void checkDeviceSimCardStatus(GnssStatusJoin device) {
|
||||
try {
|
||||
// 不允许尚未从自检得到 ICCID 的设备参加 SIM 卡状态查询
|
||||
if (!simCardQueryServiceImpl.hasValidIccid(device)) {
|
||||
return;
|
||||
}
|
||||
// SimCards 表中没有数据就初始化,这其实说明它在自检中刚获得属于自己的 ICCID 号
|
||||
SimCard simCard = simCardQueryServiceImpl.CreateOrUpdateSimCard(device);
|
||||
updateSimCardInfo(device, simCard);
|
||||
boolean iccidChanged = trafficCardService.checkAndHandleICCIDChanges(device);
|
||||
if (iccidChanged) {
|
||||
logger.info("设备 {} ICCID变更已处理: {}", device.getDeviceid(), device.getIccid());
|
||||
}
|
||||
|
||||
TrafficCard trafficCard = trafficCardService.getOrCreateTrafficCard(device.getIccid());
|
||||
|
||||
// 如果 MSISDN 为空,说明是首次创建的记录,要先去查询基本信息
|
||||
if (trafficCard.getMsisdn() == null || trafficCard.getMsisdn().isEmpty()) {
|
||||
updateTrafficCardBasicInfoFromAPI(device, trafficCard);
|
||||
}
|
||||
|
||||
// 查询SIM卡状态
|
||||
updateTrafficCardStatusFromAPI(device, trafficCard);
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("设备{}状态查询失败: ", device.getDeviceid(), e);
|
||||
@ -94,75 +110,103 @@ public class SimStatusChecker {
|
||||
|
||||
private void checkDeviceSimCardTraffic(GnssStatusJoin device) {
|
||||
try {
|
||||
// 不允许尚未从自检得到 ICCID 的设备参加 SIM 卡状态查询
|
||||
if (!simCardQueryServiceImpl.hasValidIccid(device)) {
|
||||
return;
|
||||
}
|
||||
// SimCards 表中没有数据就初始化,这说明它在自检中获得属于自己的 ICCID 号
|
||||
SimCard simCard = simCardQueryServiceImpl.CreateOrUpdateSimCard(device);
|
||||
// 如果该卡状态不是已激活,而是其他状态,那么不运行它参与 SIM 卡流量检测
|
||||
if(simCard.getStatus() != SimCard.STATUS_ACTIVATED){
|
||||
|
||||
TrafficCard trafficCard = trafficCardService.getOrCreateTrafficCard(device.getIccid());
|
||||
|
||||
// 检查查询状态,如果已标记为非当前卡商或查询失败,则跳过
|
||||
if (trafficCard.getQueryStatus() != null &&
|
||||
trafficCard.getQueryStatus() != TrafficCard.QUERY_STATUS_NORMAL) {
|
||||
logger.debug("设备 {} 的SIM卡 {} 查询状态为 {},跳过流量查询",
|
||||
device.getDeviceid(),
|
||||
device.getIccid(),
|
||||
trafficCard.getQueryStatus());
|
||||
return;
|
||||
}
|
||||
updateSimCardTrafficFromAPI(device, simCard);
|
||||
|
||||
// 如果该卡状态不是已激活,跳过流量查询
|
||||
if(trafficCard.getStatus() != TrafficCard.STATUS_ACTIVATED) {
|
||||
logger.debug("设备 {} 的SIM卡 {} 状态为 {},跳过流量查询",
|
||||
device.getDeviceid(),
|
||||
device.getIccid(),
|
||||
trafficCard.getStatus());
|
||||
return;
|
||||
}
|
||||
|
||||
// 查询流量信息
|
||||
try {
|
||||
updateTrafficCardTrafficFromAPI(device, trafficCard);
|
||||
} catch (Exception e) {
|
||||
// 如果查询失败,标记为查询失败状态
|
||||
if (e.getMessage() != null && e.getMessage().contains("不属于当前卡商")) {
|
||||
trafficCardService.markCardAsNotCurrentVendor(device.getIccid());
|
||||
} else {
|
||||
trafficCardService.markCardAsQueryFailed(device.getIccid());
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("设备{}查询失败: ", device.getDeviceid(), e);
|
||||
logger.error("设备{}流量查询失败: ", device.getDeviceid(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateSimCardInfo(GnssStatusJoin device, SimCard simCard) throws Exception {
|
||||
// 自检中只是获取保存了设备的 ICCID ,所有如果判断如果没有 MSISDN,先更新基本信息
|
||||
if (StringUtils.isBlank(simCard.getMsisdn())) {
|
||||
updateSimCardBasicInfoFromAPI(device, simCard);
|
||||
}
|
||||
// 更新状态
|
||||
updateSimCardStatusFromAPI(device, simCard);
|
||||
}
|
||||
private void updateSimCardBasicInfoFromAPI(GnssStatusJoin device, SimCard simCard) {
|
||||
private void updateTrafficCardBasicInfoFromAPI(GnssStatusJoin device, TrafficCard trafficCard) {
|
||||
try {
|
||||
BaseResponse<CardInfoData> response = simCardQueryServiceImpl.queryCardInfo(device);
|
||||
if (!simCardQueryServiceImpl.isValidResponse(response)) {
|
||||
if (response == null || response.getStatus() != 1 || response.getData() == null) {
|
||||
logger.warn("设备 {} 的SIM卡基本信息查询失败: {}",
|
||||
device.getDeviceid(),
|
||||
response != null ? response.getMessage() : "无响应");
|
||||
return;
|
||||
}
|
||||
|
||||
CardInfoData info = response.getData();
|
||||
simCard.setUpdatetime(new Date());
|
||||
simCard.setMsisdn(info.getMsisdn());
|
||||
simCardsMapper.updateSimCardInfo(simCard);
|
||||
trafficCard.setUpdateTime(new Date());
|
||||
trafficCard.setMsisdn(info.getMsisdn());
|
||||
trafficCardMapper.updateCardInfo(trafficCard);
|
||||
|
||||
logger.debug("更新SIM卡基本信息 - imsi: {}, msisdn: {}, iccid: {}",
|
||||
info.getImsi(), info.getMsisdn(), info.getIccid());
|
||||
info.getImsi(), info.getMsisdn(), info.getIccid());
|
||||
} catch (Exception e) {
|
||||
logger.error("更新设备{}的SIM卡基本信息失败: ", device.getDeviceid(), e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
private void updateSimCardStatusFromAPI(GnssStatusJoin device, SimCard simCard) {
|
||||
|
||||
private void updateTrafficCardStatusFromAPI(GnssStatusJoin device, TrafficCard trafficCard) {
|
||||
try {
|
||||
BaseResponse<CardStatusData> response = simCardQueryServiceImpl.queryCardStatus(device);
|
||||
if (!simCardQueryServiceImpl.isValidResponse(response)) {
|
||||
if (response == null || response.getStatus() != 1 || response.getData() == null) {
|
||||
logger.warn("设备 {} 的SIM卡状态查询失败: {}",
|
||||
device.getDeviceid(),
|
||||
response != null ? response.getMessage() : "无响应");
|
||||
return;
|
||||
}
|
||||
CardStatusData status = response.getData();
|
||||
simCard.setUpdatetime(new Date());
|
||||
simCard.setStatus(status.getStatusCode());
|
||||
simCardsMapper.updateCardStatusInfo(simCard);
|
||||
|
||||
checkSimCardStatus(device, simCard);
|
||||
CardStatusData status = response.getData();
|
||||
trafficCard.setUpdateTime(new Date());
|
||||
trafficCard.setStatus(status.getStatusCode());
|
||||
trafficCardMapper.updateCardInfo(trafficCard);
|
||||
|
||||
checkTrafficCardStatus(device, trafficCard);
|
||||
|
||||
logger.debug("更新SIM卡状态 - Code: {}, 描述: {}",
|
||||
status.getStatusCode(), status.getStatusDesc());
|
||||
} catch (Exception e) {
|
||||
logger.error("更新设备{}的SIM卡状态失败: ", device.getDeviceid(), e);
|
||||
// throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private void updateSimCardTrafficFromAPI(GnssStatusJoin device, SimCard simCard) {
|
||||
private void updateTrafficCardTrafficFromAPI(GnssStatusJoin device, TrafficCard trafficCard) {
|
||||
try {
|
||||
BaseResponse<GprsData> response = simCardQueryServiceImpl.queryGprs(device);
|
||||
if (!simCardQueryServiceImpl.isValidResponse(response)) {
|
||||
if (response == null || response.getStatus() != 1 || response.getData() == null) {
|
||||
logger.warn("设备 {} 的SIM卡流量查询失败: {}",
|
||||
device.getDeviceid(),
|
||||
response != null ? response.getMessage() : "无响应");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -173,43 +217,55 @@ public class SimStatusChecker {
|
||||
return;
|
||||
}
|
||||
|
||||
simCard.setUpdatetime(new Date());
|
||||
simCard.setRemaining(BigDecimal.valueOf(usage.getLeft()));
|
||||
simCard.setUsed(BigDecimal.valueOf(usage.getUsed()));
|
||||
simCard.setTotal(BigDecimal.valueOf(usage.getTotal()));
|
||||
simCardsMapper.updateCardTrafficInfo(simCard);
|
||||
trafficCard.setUpdateTime(new Date());
|
||||
// 将浮点数转换为整数存储(MB值×1000)
|
||||
trafficCard.setRemaining((int)(usage.getLeft() * 1000));
|
||||
trafficCard.setUsed((int)(usage.getUsed() * 1000));
|
||||
trafficCard.setTotal((int)(usage.getTotal() * 1000));
|
||||
|
||||
checkSimCardTraffic(device, simCard);
|
||||
trafficCardMapper.updateCardTrafficInfo(trafficCard);
|
||||
|
||||
TrafficRecord record = new TrafficRecord();
|
||||
record.setIccid(trafficCard.getIccid());
|
||||
record.setRecordTime(new Date());
|
||||
record.setRemaining(trafficCard.getRemaining());
|
||||
record.setTotal(trafficCard.getTotal());
|
||||
record.setUsed(trafficCard.getUsed());
|
||||
trafficRecordMapper.insert(record);
|
||||
|
||||
checkTrafficCardTraffic(device, trafficCard);
|
||||
|
||||
logger.debug("更新流量信息成功 - deviceId: {}, 剩余: {}MB, 总量: {}MB, 已用: {}MB",
|
||||
device.getIccid(),
|
||||
simCard.getRemaining(),
|
||||
simCard.getTotal(),
|
||||
simCard.getUsed());
|
||||
trafficCard.getRemaining() / 1000.0,
|
||||
trafficCard.getTotal() / 1000.0,
|
||||
trafficCard.getUsed() / 1000.0);
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("设备{}更新SIM卡流量失败: ", device.getDeviceid(), e);
|
||||
//throw e;
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public void checkSimCardTraffic(GnssStatusJoin device, SimCard simCard) {
|
||||
public void checkTrafficCardTraffic(GnssStatusJoin device, TrafficCard trafficCard) {
|
||||
GnssStatus status = gnssStatusMapper.getByDeviceId(device.getDeviceid());
|
||||
if (status == null) return;
|
||||
|
||||
boolean isUpdated = false;
|
||||
|
||||
BigDecimal usedPercentage = simCard.getUsed()
|
||||
.divide(simCard.getTotal(), 4, RoundingMode.HALF_UP)
|
||||
.multiply(BigDecimal.valueOf(100));
|
||||
// 计算流量使用百分比
|
||||
int usedPercentage = 0;
|
||||
if (trafficCard.getTotal() > 0) {
|
||||
usedPercentage = (int)((trafficCard.getUsed() * 100.0) / trafficCard.getTotal());
|
||||
}
|
||||
|
||||
// 检查流量使用情况
|
||||
if (warningService.check(status, WarningCfg.TYPE_SIM_LOW_TRAFFIC,
|
||||
WarningCfg.TYPE_NAME_SIM_LOW_TRAFFIC,
|
||||
false, // 大于等于流量门限值,那么就报警
|
||||
usedPercentage.intValue(),
|
||||
usedPercentage,
|
||||
null,
|
||||
String.format("流量已使用 %.2f%%", usedPercentage.doubleValue()))) {
|
||||
String.format("流量已使用 %d%%", usedPercentage))) {
|
||||
isUpdated = true;
|
||||
}
|
||||
|
||||
@ -219,27 +275,26 @@ public class SimStatusChecker {
|
||||
}
|
||||
}
|
||||
|
||||
// 检查SIM卡状态
|
||||
public void checkSimCardStatus(GnssStatusJoin device, SimCard simCard) {
|
||||
public void checkTrafficCardStatus(GnssStatusJoin device, TrafficCard trafficCard) {
|
||||
GnssStatus status = gnssStatusMapper.getByDeviceId(device.getDeviceid());
|
||||
if (status == null) return;
|
||||
|
||||
boolean isUpdated = false;
|
||||
|
||||
// 检查SIM卡状态是否异常(停机、注销、失效)
|
||||
if (simCard.getStatus() == SimCard.STATUS_SUSPENDED ||
|
||||
simCard.getStatus() == SimCard.STATUS_CANCELLED ||
|
||||
simCard.getStatus() == SimCard.STATUS_INVALID) {
|
||||
if (trafficCard.getStatus() == TrafficCard.STATUS_SUSPENDED ||
|
||||
trafficCard.getStatus() == TrafficCard.STATUS_CANCELLED ||
|
||||
trafficCard.getStatus() == TrafficCard.STATUS_INVALID) {
|
||||
|
||||
String statusDesc;
|
||||
switch(simCard.getStatus()) {
|
||||
case SimCard.STATUS_SUSPENDED:
|
||||
switch(trafficCard.getStatus()) {
|
||||
case TrafficCard.STATUS_SUSPENDED:
|
||||
statusDesc = "停机";
|
||||
break;
|
||||
case SimCard.STATUS_CANCELLED:
|
||||
case TrafficCard.STATUS_CANCELLED:
|
||||
statusDesc = "注销";
|
||||
break;
|
||||
case SimCard.STATUS_INVALID:
|
||||
case TrafficCard.STATUS_INVALID:
|
||||
statusDesc = "失效";
|
||||
break;
|
||||
default:
|
||||
@ -248,12 +303,13 @@ public class SimStatusChecker {
|
||||
|
||||
if (warningService.check(status, WarningCfg.TYPE_SIM_STATUS_ABNORMAL,
|
||||
WarningCfg.TYPE_NAME_SIM_STATUS_ABNORMAL, false,
|
||||
simCard.getStatus(), null,
|
||||
"SIM卡状态: " + statusDesc)) {
|
||||
trafficCard.getStatus(),
|
||||
null,
|
||||
"SIM卡状态异常: " + statusDesc)) {
|
||||
isUpdated = true;
|
||||
}
|
||||
} else if (simCard.getStatus() == SimCard.STATUS_ACTIVATED) {
|
||||
// 状态正常(已激活),清除告警
|
||||
} else {
|
||||
// 清除状态异常告警
|
||||
if ((status.getWarningcode() & WarningCfg.TYPE_SIM_STATUS_ABNORMAL) != 0) {
|
||||
warningService.clearWarning(status, WarningCfg.TYPE_SIM_STATUS_ABNORMAL);
|
||||
isUpdated = true;
|
||||
@ -265,5 +321,4 @@ public class SimStatusChecker {
|
||||
gnssStatusMapper.updateById(status);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user