fix: 修改 ICCID 查询方式

This commit is contained in:
2026-06-27 09:31:09 +08:00
parent d688dffef2
commit a48cad170c
+47 -19
View File
@@ -1316,30 +1316,58 @@ class ModemManager extends EventEmitter {
return this.iccid; return this.iccid;
} }
try { const commands = ['AT+ICCID', 'AT+CCID', 'AT+QCCID'];
const resp = await this.sendATCommand('AT+CCID', 2000);
const match = resp.match(/\+CCID:\s*(\d+)/); for (const command of commands) {
if (match) { try {
const previousICCID = this.iccid; const resp = await this.sendATCommand(command, 2000);
this.iccid = match[1]; const iccid = this.parseICCIDResponse(resp);
this.iccidCheckedAt = new Date().toISOString();
if (previousICCID !== this.iccid) { if (iccid) {
logger.info(`✓ 当前SIM ICCID: ${this.formatICCID(this.iccid)}`); const previousICCID = this.iccid;
this.iccid = iccid;
this.iccidCheckedAt = new Date().toISOString();
if (previousICCID !== this.iccid) {
logger.info(`✓ 当前SIM ICCID: ${this.formatICCID(this.iccid)}`);
}
return this.iccid;
} }
return this.iccid;
logger.debug(`${command}未返回可识别ICCID: ${this.formatATResponse(resp)}`);
} catch (err) {
logger.debug(`${command}查询ICCID失败: ${err.message}`);
}
}
this.iccid = null;
this.iccidCheckedAt = new Date().toISOString();
logger.warn('查询ICCID失败: 模组未返回可识别ICCID');
return null;
}
parseICCIDResponse(resp) {
const lines = String(resp || '')
.split('\n')
.map(line => line.trim())
.filter(Boolean);
for (const line of lines) {
if (/^AT/i.test(line) || line === 'OK' || line === 'ERROR') {
continue;
} }
this.iccid = null; const prefixed = line.match(/^\+(?:ICCID|CCID|QCCID):\s*"?([0-9]{18,22})"?/i);
this.iccidCheckedAt = new Date().toISOString(); if (prefixed) {
return null; return prefixed[1];
} catch (err) { }
if (options.refresh) {
this.iccid = null; const plain = line.match(/^"?([0-9]{18,22})"?$/);
this.iccidCheckedAt = new Date().toISOString(); if (plain) {
return plain[1];
} }
logger.error('查询ICCID失败:', err);
return null;
} }
return null;
} }
formatICCID(iccid) { formatICCID(iccid) {