Compare commits
3
Commits
21a8b97bb2
...
e87e5fc9de
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e87e5fc9de | ||
|
|
c5b29924b8 | ||
|
|
a90f35fbd8 |
@@ -0,0 +1,304 @@
|
|||||||
|
# Node.js 4G SMS Gateway
|
||||||
|
|
||||||
|
基于Node.js的4G短信网关,通过串口与4G模块通信,接收短信并转发到多种推送渠道。
|
||||||
|
|
||||||
|
## 功能特性
|
||||||
|
|
||||||
|
- ✅ 4G模块AT指令交互
|
||||||
|
- ✅ 自动接收短信(PDU模式)
|
||||||
|
- ✅ 长短信自动合并
|
||||||
|
- ✅ 多通道推送(邮件/钉钉/飞书/Telegram等)
|
||||||
|
- ✅ 短信发送功能
|
||||||
|
- ✅ RESTful API接口
|
||||||
|
- ✅ Web管理界面
|
||||||
|
|
||||||
|
## 环境要求
|
||||||
|
|
||||||
|
- Node.js >= 18.x
|
||||||
|
- 4G模块通过USB转串口连接到服务器
|
||||||
|
- 支持的4G模块:ML307R-DC、SIM7600等AT指令兼容模块
|
||||||
|
|
||||||
|
## 安装
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install
|
||||||
|
```
|
||||||
|
|
||||||
|
## 配置
|
||||||
|
|
||||||
|
复制配置模板:
|
||||||
|
```bash
|
||||||
|
cp config.example.json config.json
|
||||||
|
```
|
||||||
|
|
||||||
|
`config.json` 必须存在,否则程序会直接退出。最小可运行配置如下:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"serial": {
|
||||||
|
"path": "/dev/ttyUSB0",
|
||||||
|
"baudRate": 115200,
|
||||||
|
"autoDetect": true,
|
||||||
|
"probeTimeout": 1200
|
||||||
|
},
|
||||||
|
"api": {
|
||||||
|
"port": 3000,
|
||||||
|
"webToken": "change-this-token"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
完整配置可参考 `config.example.json`。主要配置项如下:
|
||||||
|
|
||||||
|
| 配置项 | 是否必填 | 说明 |
|
||||||
|
|--------|----------|------|
|
||||||
|
| `serial.path` | 是 | 串口路径;Linux 常用 `/dev/ttyUSB0`,Windows 使用 `COM3` 等。`autoDetect=true` 时作为优先探测项,`autoDetect=false` 时作为实际串口。 |
|
||||||
|
| `serial.baudRate` | 是 | 串口波特率,通常为 `115200`。 |
|
||||||
|
| `serial.autoDetect` | 否 | 是否自动探测可响应 `AT` 的串口,默认按非 `false` 处理。 |
|
||||||
|
| `serial.probeTimeout` | 否 | 自动探测单个串口的超时时间,单位毫秒,默认 `1200`。 |
|
||||||
|
| `api.port` | 是 | API 和 Web 管理台监听端口。 |
|
||||||
|
| `api.webToken` | 是 | 唯一鉴权凭据;已取消 `admin/admin123` Basic Auth。 |
|
||||||
|
| `mobileData.cid` | 否 | 移动数据 PDP CID,默认 `1`;有效范围 `1-15`,不要使用 `8`。 |
|
||||||
|
| `simCards` | 否 | SIM 卡显示手机号,`slot=0` 是 SIM1,`slot=1` 是 SIM2。 |
|
||||||
|
| `smtp` | 否 | 邮件通知配置;填写 `smtp.server` 后启用。 |
|
||||||
|
| `inbox` | 否 | Web 收件箱持久化和双卡显示策略。 |
|
||||||
|
| `pushChannels` | 否 | 推送通道列表;通道开启后才要求目标字段。 |
|
||||||
|
|
||||||
|
`api.webToken` 可以通过以下方式传入:
|
||||||
|
|
||||||
|
- `Authorization: Bearer <token>`
|
||||||
|
- `X-Web-Token: <token>`
|
||||||
|
- `?token=<token>`
|
||||||
|
- Web 管理台 token 登录页
|
||||||
|
|
||||||
|
### 完整配置示例
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"serial": {
|
||||||
|
"path": "/dev/ttyUSB0",
|
||||||
|
"baudRate": 115200,
|
||||||
|
"autoDetect": true,
|
||||||
|
"probeTimeout": 1200
|
||||||
|
},
|
||||||
|
"mobileData": {
|
||||||
|
"cid": 1
|
||||||
|
},
|
||||||
|
"simCards": [
|
||||||
|
{ "slot": 0, "phoneNumber": "+8613800138000" },
|
||||||
|
{ "slot": 1, "phoneNumber": "+447700900000" }
|
||||||
|
],
|
||||||
|
"smtp": {
|
||||||
|
"server": "smtp.qq.com",
|
||||||
|
"port": 465,
|
||||||
|
"user": "your@qq.com",
|
||||||
|
"pass": "your_auth_code",
|
||||||
|
"sendTo": "recipient@example.com"
|
||||||
|
},
|
||||||
|
"inbox": {
|
||||||
|
"maxReceivedMessages": 200,
|
||||||
|
"receivedMessagesFile": "data/received-messages.json",
|
||||||
|
"partitionBySim": true,
|
||||||
|
"allowAllSimMessages": false,
|
||||||
|
"dualSimDefaultScope": "all"
|
||||||
|
},
|
||||||
|
"pushChannels": [
|
||||||
|
{
|
||||||
|
"enabled": true,
|
||||||
|
"type": "dingtalk",
|
||||||
|
"name": "钉钉通知",
|
||||||
|
"url": "https://oapi.dingtalk.com/robot/send?access_token=xxx",
|
||||||
|
"secret": "SECxxx"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"api": {
|
||||||
|
"port": 3000,
|
||||||
|
"webToken": "change-this-token"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 可选项细节
|
||||||
|
|
||||||
|
- `smtp`:不需要邮件时可以删除整个对象,或清空 `server`。启用邮件时需要正确填写 `server`、`port`、`user`、`pass`、`sendTo`。
|
||||||
|
- `inbox.maxReceivedMessages`:默认 `200`,最大按 `5000` 处理。
|
||||||
|
- `inbox.receivedMessagesFile`:默认 `data/received-messages.json`,相对路径会基于项目根目录解析。
|
||||||
|
- `inbox.partitionBySim`:默认 `true`,按当前 SIM 隔离历史短信。
|
||||||
|
- `inbox.allowAllSimMessages`:默认 `false`,控制是否允许显式查询全部 SIM 历史。
|
||||||
|
- `inbox.dualSimDefaultScope`:可选 `all` 或 `current`;`current` 表示双卡时默认只看当前 SIM。
|
||||||
|
- `simCards`:只影响管理台显示,不影响短信收发。代码也兼容旧字段名 `simSlots`、`sims`、`sim`。
|
||||||
|
|
||||||
|
## 运行
|
||||||
|
|
||||||
|
### 开发模式
|
||||||
|
```bash
|
||||||
|
npm run dev
|
||||||
|
```
|
||||||
|
|
||||||
|
### 生产模式
|
||||||
|
```bash
|
||||||
|
npm start
|
||||||
|
```
|
||||||
|
|
||||||
|
### 使用PM2(推荐)
|
||||||
|
```bash
|
||||||
|
pm2 start ecosystem.config.js
|
||||||
|
pm2 save
|
||||||
|
pm2 startup
|
||||||
|
```
|
||||||
|
|
||||||
|
## API接口
|
||||||
|
|
||||||
|
### 查询状态
|
||||||
|
```bash
|
||||||
|
GET /api/status
|
||||||
|
```
|
||||||
|
|
||||||
|
### 发送短信
|
||||||
|
```bash
|
||||||
|
POST /api/sms/send
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"phone": "13800138000",
|
||||||
|
"message": "测试短信"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 查询收到的短信
|
||||||
|
```bash
|
||||||
|
GET /api/sms/received?limit=50
|
||||||
|
```
|
||||||
|
|
||||||
|
双卡双待模式下,默认返回全部 SIM 历史;显式传 `scope=current` 时才按当前卡槽过滤。短信记录会保留 `simSlot`、`simLabel` 与 `simSource`,用于判断收件卡槽。
|
||||||
|
也可以显式按卡槽查询:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
GET /api/sms/received?limit=50&simSlot=0 # SIM1
|
||||||
|
GET /api/sms/received?limit=50&simSlot=1 # SIM2
|
||||||
|
```
|
||||||
|
|
||||||
|
直出模式下,新短信主要通过模组 `+CMT` URC 进入收件箱;如果 URC 未携带卡槽信息,会显示为未知 SIM。
|
||||||
|
|
||||||
|
### 查询信号强度
|
||||||
|
```bash
|
||||||
|
GET /api/modem/signal
|
||||||
|
```
|
||||||
|
|
||||||
|
### 查询模组信息
|
||||||
|
```bash
|
||||||
|
GET /api/modem/info
|
||||||
|
```
|
||||||
|
|
||||||
|
### 查询与切换SIM
|
||||||
|
```bash
|
||||||
|
GET /api/modem/sim
|
||||||
|
POST /api/modem/sim/switch
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"slot": 1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
当模组返回 `AT+DUALSIM?`、`AT+SWITCHSIM?` 与 `AT+BINDSIM?` 能力时,管理台会显示 SIM1/SIM2。短信接收使用 `CNMI` 直出模式,不为了接收短信切换 `SWITCHSIM`。如果模组上报的 URC 未携带卡槽信息,新短信会记录为未知 SIM。
|
||||||
|
|
||||||
|
管理台切卡会同时设置 `AT+SWITCHSIM` 与 `AT+BINDSIM`,随后恢复短信直出配置。发送短信前会提示当前使用的 SIM。
|
||||||
|
|
||||||
|
如果希望管理台显示手机号,可在 `config.json` 中配置:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"simCards": [
|
||||||
|
{ "slot": 0, "phoneNumber": "+8613800138000" },
|
||||||
|
{ "slot": 1, "phoneNumber": "+447700900000" }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 发送AT命令
|
||||||
|
```bash
|
||||||
|
POST /api/modem/at
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"command": "AT+CSQ"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 获取日志
|
||||||
|
```bash
|
||||||
|
GET /api/logs
|
||||||
|
```
|
||||||
|
|
||||||
|
所有API均需要有效 token。可以通过 `Authorization: Bearer <token>`、`X-Web-Token` 请求头或 `token` 查询参数传入。
|
||||||
|
|
||||||
|
## 项目结构
|
||||||
|
|
||||||
|
```
|
||||||
|
.
|
||||||
|
├── src/
|
||||||
|
│ ├── index.js # 入口文件
|
||||||
|
│ ├── modem.js # 4G模块通信
|
||||||
|
│ ├── sms.js # 短信处理
|
||||||
|
│ ├── push.js # 推送通道
|
||||||
|
│ ├── concat.js # 长短信合并
|
||||||
|
│ ├── api.js # REST API
|
||||||
|
│ └── logger.js # 日志系统
|
||||||
|
├── config.json # 配置文件
|
||||||
|
├── package.json
|
||||||
|
└── README.md
|
||||||
|
```
|
||||||
|
|
||||||
|
## 推送通道类型
|
||||||
|
|
||||||
|
| 类型 | type值 | 配置项 |
|
||||||
|
|------|--------|--------|
|
||||||
|
| 钉钉机器人 | `dingtalk` | url, secret(可选) |
|
||||||
|
| 飞书机器人 | `feishu` | url, secret(可选) |
|
||||||
|
| Telegram Bot | `telegram` | url(bot token), key1(chat_id) |
|
||||||
|
| PushPlus | `pushplus` | key1(token) |
|
||||||
|
| Server酱 | `serverchan` | key1(sendkey) |
|
||||||
|
| Bark | `bark` | url(bark服务器) |
|
||||||
|
| POST JSON | `post_json` | url |
|
||||||
|
| GET请求 | `get` | url |
|
||||||
|
| 自定义模板 | `custom` | url, customBody |
|
||||||
|
|
||||||
|
`pushChannels` 中 `enabled: false` 的通道可以保留空字段;`enabled: true` 时必须配置对应目标:
|
||||||
|
|
||||||
|
- `post_json`、`bark`、`get`、`dingtalk`、`custom`、`feishu`:必须有 `url`。
|
||||||
|
- `telegram`:`url` 填 Bot Token,`key1` 填 Chat ID。
|
||||||
|
- `pushplus`:`key1` 填 PushPlus Token。
|
||||||
|
- `serverchan`:`key1` 填 Server酱 SendKey。
|
||||||
|
- `custom`:`customBody` 如果填写,必须是合法 JSON 字符串,支持 `{sender}`、`{message}`、`{timestamp}` 占位符。
|
||||||
|
|
||||||
|
## 长短信处理
|
||||||
|
|
||||||
|
- 自动检测长短信(分段数 > 1)
|
||||||
|
- 使用参考号和发送者号码标识同一条长短信
|
||||||
|
- 收齐所有分段后自动合并
|
||||||
|
- 30秒超时保护(未收齐也转发)
|
||||||
|
|
||||||
|
## 故障排查
|
||||||
|
|
||||||
|
### 串口打不开
|
||||||
|
```bash
|
||||||
|
# Linux检查串口设备
|
||||||
|
ls -l /dev/ttyUSB*
|
||||||
|
# 添加用户到dialout组
|
||||||
|
sudo usermod -a -G dialout $USER
|
||||||
|
```
|
||||||
|
|
||||||
|
### 模块不响应
|
||||||
|
- 检查串口路径和波特率
|
||||||
|
- 检查USB连接
|
||||||
|
- 检查SIM卡是否插好
|
||||||
|
|
||||||
|
### 收不到短信
|
||||||
|
- 检查AT+CEREG?网络注册状态
|
||||||
|
- 检查AT+CNMI配置是否正确
|
||||||
|
- 检查SIM卡余额和服务状态
|
||||||
|
|
||||||
|
## 许可证
|
||||||
|
|
||||||
|
MIT
|
||||||
+24
-6
@@ -4,19 +4,40 @@
|
|||||||
"baudRate": 115200,
|
"baudRate": 115200,
|
||||||
"autoDetect": true,
|
"autoDetect": true,
|
||||||
"probeTimeout": 1200,
|
"probeTimeout": 1200,
|
||||||
"_comment": "Windows使用COM1/COM2/COM3等,也可以直接写数字如3; Linux使用/dev/ttyUSB0"
|
"_comment": "必填。path是手动串口或自动探测优先项;autoDetect=false时必须写真实串口。Windows使用COM1/COM2/COM3等,也可以直接写数字如3;Linux使用/dev/ttyUSB0。baudRate通常为115200,probeTimeout为AT探测超时毫秒。"
|
||||||
},
|
},
|
||||||
"mobileData": {
|
"mobileData": {
|
||||||
"cid": 1,
|
"cid": 1,
|
||||||
"_comment": "每次启动都会强制关闭移动数据,管理台开关只影响当前运行期; ML307 cid8为IMS专用,请勿使用"
|
"_comment": "可选,默认cid=1。有效范围1-15,cid8为IMS专用,请勿使用。ML307每次启动都会强制断开MIPCALL应用层拨号,并保护性尝试CGACT;管理台开关只影响当前运行期。"
|
||||||
},
|
},
|
||||||
|
"_simCards_comment": "可选。用于给SIM1/SIM2显示手机号;slot=0是SIM1,slot=1是SIM2。也兼容旧字段名simSlots、sims、sim。",
|
||||||
|
"simCards": [
|
||||||
|
{
|
||||||
|
"slot": 0,
|
||||||
|
"phoneNumber": "+8613800138000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"slot": 1,
|
||||||
|
"phoneNumber": "+447700900000"
|
||||||
|
}
|
||||||
|
],
|
||||||
"smtp": {
|
"smtp": {
|
||||||
|
"_comment": "可选。填写server后会启用邮件通知;启用时server、port、user、pass、sendTo都需要正确填写。不需要邮件可删除smtp或清空server。",
|
||||||
"server": "smtp.qq.com",
|
"server": "smtp.qq.com",
|
||||||
"port": 465,
|
"port": 465,
|
||||||
"user": "your@qq.com",
|
"user": "your@qq.com",
|
||||||
"pass": "your_auth_code",
|
"pass": "your_auth_code",
|
||||||
"sendTo": "recipient@example.com"
|
"sendTo": "recipient@example.com"
|
||||||
},
|
},
|
||||||
|
"inbox": {
|
||||||
|
"maxReceivedMessages": 200,
|
||||||
|
"receivedMessagesFile": "data/received-messages.json",
|
||||||
|
"partitionBySim": true,
|
||||||
|
"allowAllSimMessages": false,
|
||||||
|
"dualSimDefaultScope": "all",
|
||||||
|
"_comment": "全部可选。maxReceivedMessages默认200且最大5000;receivedMessagesFile默认data/received-messages.json;partitionBySim默认true;allowAllSimMessages默认false;dualSimDefaultScope可为all或current。默认显示全部短信;直出URC未携带卡槽时,新短信不区分SIM1/SIM2收件归属。"
|
||||||
|
},
|
||||||
|
"_pushChannels_comment": "可选。enabled=false时目标字段可以为空;enabled=true时按type要求填写目标:post_json/bark/get/dingtalk/custom/feishu需要url,telegram需要url(bot token)+key1(chat id),pushplus需要key1(token),serverchan需要key1(sendkey)。customBody如果填写必须是合法JSON字符串。",
|
||||||
"pushChannels": [
|
"pushChannels": [
|
||||||
{
|
{
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
@@ -72,9 +93,6 @@
|
|||||||
"api": {
|
"api": {
|
||||||
"port": 3000,
|
"port": 3000,
|
||||||
"webToken": "change-this-token",
|
"webToken": "change-this-token",
|
||||||
"auth": {
|
"_comment": "必填。port是API和管理台监听端口;webToken是唯一鉴权凭据,可通过Authorization: Bearer、X-Web-Token、token查询参数或管理台登录页提交。"
|
||||||
"username": "admin",
|
|
||||||
"password": "admin123"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-3
@@ -144,7 +144,6 @@
|
|||||||
<span class="sim-slot__state" id="simSlot0State">--</span>
|
<span class="sim-slot__state" id="simSlot0State">--</span>
|
||||||
</div>
|
</div>
|
||||||
<span class="sim-slot__phone" id="simSlot0Phone">--</span>
|
<span class="sim-slot__phone" id="simSlot0Phone">--</span>
|
||||||
<button class="button button--secondary button--compact" type="button" data-sim-switch="0">切到SIM1</button>
|
|
||||||
</article>
|
</article>
|
||||||
|
|
||||||
<article class="sim-slot" id="simSlot1">
|
<article class="sim-slot" id="simSlot1">
|
||||||
@@ -153,7 +152,6 @@
|
|||||||
<span class="sim-slot__state" id="simSlot1State">--</span>
|
<span class="sim-slot__state" id="simSlot1State">--</span>
|
||||||
</div>
|
</div>
|
||||||
<span class="sim-slot__phone" id="simSlot1Phone">--</span>
|
<span class="sim-slot__phone" id="simSlot1Phone">--</span>
|
||||||
<button class="button button--secondary button--compact" type="button" data-sim-switch="1">切到SIM2</button>
|
|
||||||
</article>
|
</article>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -227,6 +225,10 @@
|
|||||||
<span>当前发送SIM</span>
|
<span>当前发送SIM</span>
|
||||||
<strong id="sendSimLabel">--</strong>
|
<strong id="sendSimLabel">--</strong>
|
||||||
</p>
|
</p>
|
||||||
|
<div class="send-sim-picker" aria-label="选择发送SIM">
|
||||||
|
<button class="button button--secondary button--compact" type="button" data-sim-switch="0">SIM1</button>
|
||||||
|
<button class="button button--secondary button--compact" type="button" data-sim-switch="1">SIM2</button>
|
||||||
|
</div>
|
||||||
<form id="smsForm" class="stack-form">
|
<form id="smsForm" class="stack-form">
|
||||||
<label class="field">
|
<label class="field">
|
||||||
<span class="field__label">目标号码</span>
|
<span class="field__label">目标号码</span>
|
||||||
@@ -419,4 +421,4 @@
|
|||||||
<script src="/assets/admin.js" type="module"></script>
|
<script src="/assets/admin.js" type="module"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
+31
-7
@@ -70,6 +70,10 @@
|
|||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[hidden] {
|
||||||
|
display: none !important;
|
||||||
|
}
|
||||||
|
|
||||||
html {
|
html {
|
||||||
scroll-behavior: smooth;
|
scroll-behavior: smooth;
|
||||||
scroll-padding-top: 80px;
|
scroll-padding-top: 80px;
|
||||||
@@ -726,9 +730,13 @@ h3 {
|
|||||||
gap: var(--space-md);
|
gap: var(--space-md);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.sim-slot-grid.is-single {
|
||||||
|
grid-template-columns: minmax(0, 1fr);
|
||||||
|
}
|
||||||
|
|
||||||
.sim-slot {
|
.sim-slot {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: minmax(0, 1fr) auto;
|
grid-template-columns: minmax(0, 1fr);
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 8px 12px;
|
gap: 8px 12px;
|
||||||
border: 1px solid var(--hairline);
|
border: 1px solid var(--hairline);
|
||||||
@@ -737,9 +745,14 @@ h3 {
|
|||||||
padding: 12px 14px;
|
padding: 12px 14px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.sim-slot.is-single {
|
||||||
|
grid-template-columns: minmax(0, 1fr);
|
||||||
|
min-height: 76px;
|
||||||
|
}
|
||||||
|
|
||||||
.sim-slot.is-active {
|
.sim-slot.is-active {
|
||||||
border-color: rgba(93, 184, 114, 0.55);
|
border-color: var(--surface-cream-strong);
|
||||||
box-shadow: inset 3px 0 0 var(--success);
|
background: var(--surface-soft);
|
||||||
}
|
}
|
||||||
|
|
||||||
.sim-slot__topline {
|
.sim-slot__topline {
|
||||||
@@ -769,8 +782,8 @@ h3 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.sim-slot.is-active .sim-slot__state {
|
.sim-slot.is-active .sim-slot__state {
|
||||||
background: rgba(93, 184, 114, 0.16);
|
background: var(--surface-cream-strong);
|
||||||
color: #2f7c44;
|
color: var(--body-strong);
|
||||||
}
|
}
|
||||||
|
|
||||||
.sim-slot__phone {
|
.sim-slot__phone {
|
||||||
@@ -966,7 +979,7 @@ h3 {
|
|||||||
display: inline-flex;
|
display: inline-flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 8px;
|
gap: 8px;
|
||||||
margin: -4px 0 var(--space-md);
|
margin: -4px 0 8px;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -984,6 +997,17 @@ h3 {
|
|||||||
overflow-wrap: anywhere;
|
overflow-wrap: anywhere;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.send-sim-picker {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 8px;
|
||||||
|
margin: 0 0 var(--space-md);
|
||||||
|
}
|
||||||
|
|
||||||
|
.send-sim-picker .button {
|
||||||
|
min-width: 96px;
|
||||||
|
}
|
||||||
|
|
||||||
.field {
|
.field {
|
||||||
display: grid;
|
display: grid;
|
||||||
gap: var(--space-xs);
|
gap: var(--space-xs);
|
||||||
@@ -1592,4 +1616,4 @@ body.modal-open {
|
|||||||
.field-row--wide {
|
.field-row--wide {
|
||||||
grid-column: auto;
|
grid-column: auto;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+80
-11
@@ -50,6 +50,7 @@ const elements = {
|
|||||||
simStatus: document.querySelector('#simStatus'),
|
simStatus: document.querySelector('#simStatus'),
|
||||||
simSlot0: document.querySelector('#simSlot0'),
|
simSlot0: document.querySelector('#simSlot0'),
|
||||||
simSlot1: document.querySelector('#simSlot1'),
|
simSlot1: document.querySelector('#simSlot1'),
|
||||||
|
simSlotGrid: document.querySelector('.sim-slot-grid'),
|
||||||
simSlot0State: document.querySelector('#simSlot0State'),
|
simSlot0State: document.querySelector('#simSlot0State'),
|
||||||
simSlot1State: document.querySelector('#simSlot1State'),
|
simSlot1State: document.querySelector('#simSlot1State'),
|
||||||
simSlot0Phone: document.querySelector('#simSlot0Phone'),
|
simSlot0Phone: document.querySelector('#simSlot0Phone'),
|
||||||
@@ -253,6 +254,11 @@ function renderSendSimLabel() {
|
|||||||
setText(elements.sendSimLabel, getActiveSimLabel());
|
setText(elements.sendSimLabel, getActiveSimLabel());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setSimSwitchStatus(message, type = '') {
|
||||||
|
setStatusMessage(elements.simStatus, message);
|
||||||
|
setStatusMessage(elements.smsStatus, message, type);
|
||||||
|
}
|
||||||
|
|
||||||
function getSimSlot(sim = {}, slotNumber) {
|
function getSimSlot(sim = {}, slotNumber) {
|
||||||
return (sim.slots || []).find(slot => slot.slot === slotNumber) || {
|
return (sim.slots || []).find(slot => slot.slot === slotNumber) || {
|
||||||
slot: slotNumber,
|
slot: slotNumber,
|
||||||
@@ -281,7 +287,18 @@ function renderSimSlot(slot, card, stateNode, phoneNode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
card.classList.toggle('is-active', Boolean(slot.active));
|
card.classList.toggle('is-active', Boolean(slot.active));
|
||||||
setText(stateNode, slot.active ? '当前使用' : '未选中');
|
if (slot.active && slot.present === false) {
|
||||||
|
setText(stateNode, '当前未就绪');
|
||||||
|
} else if (slot.active) {
|
||||||
|
setText(stateNode, '当前使用');
|
||||||
|
} else if (slot.present === true) {
|
||||||
|
setText(stateNode, '可用');
|
||||||
|
} else if (slot.present === false) {
|
||||||
|
setText(stateNode, '未检测到SIM');
|
||||||
|
} else {
|
||||||
|
setText(stateNode, '未确认');
|
||||||
|
}
|
||||||
|
|
||||||
const phoneText = formatSimSlotPhone(slot);
|
const phoneText = formatSimSlotPhone(slot);
|
||||||
if (phoneNode) {
|
if (phoneNode) {
|
||||||
phoneNode.textContent = phoneText;
|
phoneNode.textContent = phoneText;
|
||||||
@@ -289,29 +306,81 @@ function renderSimSlot(slot, card, stateNode, phoneNode) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getVisibleSimSlots(sim = {}, activeSlot = null) {
|
||||||
|
const slots = sim.slots || [];
|
||||||
|
const presentSlots = slots.filter(slot => slot.present === true);
|
||||||
|
|
||||||
|
if (presentSlots.length >= 2) {
|
||||||
|
return presentSlots;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (activeSlot?.present === true) {
|
||||||
|
return [activeSlot];
|
||||||
|
}
|
||||||
|
|
||||||
|
return slots.filter(slot => slot.active || slot.present !== false);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSimModeLabel(sim = {}, activeSlot = null) {
|
||||||
|
const hardwareSwitchable = Boolean(sim.supported && sim.canSwitch);
|
||||||
|
if (!hardwareSwitchable) {
|
||||||
|
return '单卡/未检测到双卡';
|
||||||
|
}
|
||||||
|
|
||||||
|
const presentSlots = (sim.slots || []).filter(slot => slot.present === true);
|
||||||
|
if (presentSlots.length >= 2) {
|
||||||
|
return sim.modeLabel || '双卡';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (activeSlot?.present === true) {
|
||||||
|
return `仅检测到 ${getSimDisplayLabel(activeSlot)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return '未检测到可用SIM';
|
||||||
|
}
|
||||||
|
|
||||||
function renderSimStatus(sim = {}) {
|
function renderSimStatus(sim = {}) {
|
||||||
state.currentSim = sim;
|
state.currentSim = sim;
|
||||||
|
|
||||||
const supported = Boolean(sim.supported && sim.canSwitch);
|
const supported = Boolean(sim.supported && sim.canSwitch);
|
||||||
const activeSlot = getActiveSimSlot(sim);
|
const activeSlot = getActiveSimSlot(sim);
|
||||||
const modeLabel = supported ? (sim.modeLabel || '双卡') : '单卡/未检测到双卡';
|
const modeLabel = getSimModeLabel(sim, activeSlot);
|
||||||
|
|
||||||
setText(elements.simMode, modeLabel);
|
setText(elements.simMode, modeLabel);
|
||||||
|
|
||||||
renderSimSlot(getSimSlot(sim, 0), elements.simSlot0, elements.simSlot0State, elements.simSlot0Phone);
|
const visibleSlots = getVisibleSimSlots(sim, activeSlot);
|
||||||
renderSimSlot(getSimSlot(sim, 1), elements.simSlot1, elements.simSlot1State, elements.simSlot1Phone);
|
const singleSlot = visibleSlots.length === 1;
|
||||||
|
const visibleSlotNumbers = new Set(visibleSlots.map(slot => slot.slot));
|
||||||
|
|
||||||
|
elements.simSlotGrid?.classList.toggle('is-single', singleSlot);
|
||||||
|
|
||||||
|
[
|
||||||
|
[getSimSlot(sim, 0), elements.simSlot0, elements.simSlot0State, elements.simSlot0Phone],
|
||||||
|
[getSimSlot(sim, 1), elements.simSlot1, elements.simSlot1State, elements.simSlot1Phone]
|
||||||
|
].forEach(([slot, card, stateNode, phoneNode]) => {
|
||||||
|
if (card) {
|
||||||
|
card.hidden = !visibleSlotNumbers.has(slot.slot);
|
||||||
|
card.classList.toggle('is-single', singleSlot && visibleSlotNumbers.has(slot.slot));
|
||||||
|
}
|
||||||
|
renderSimSlot(slot, card, stateNode, phoneNode);
|
||||||
|
});
|
||||||
|
|
||||||
elements.simSwitchButtons.forEach((button) => {
|
elements.simSwitchButtons.forEach((button) => {
|
||||||
const slot = Number.parseInt(button.dataset.simSwitch, 10);
|
const slot = Number.parseInt(button.dataset.simSwitch, 10);
|
||||||
|
const slotInfo = getSimSlot(sim, slot);
|
||||||
const isActive = activeSlot?.slot === slot;
|
const isActive = activeSlot?.slot === slot;
|
||||||
button.disabled = state.simBusy || Boolean(sim.switching) || !supported || isActive;
|
const unavailable = slotInfo.present === false;
|
||||||
button.textContent = isActive ? '正在使用' : `切到SIM${slot + 1}`;
|
button.hidden = singleSlot;
|
||||||
|
button.disabled = state.simBusy || Boolean(sim.switching) || !supported || isActive || unavailable;
|
||||||
|
button.textContent = isActive ? `SIM${slot + 1} 当前` : `切到SIM${slot + 1}`;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (state.simBusy || sim.switching) {
|
if (state.simBusy || sim.switching) {
|
||||||
setStatusMessage(elements.simStatus, '正在切换SIM...');
|
setSimSwitchStatus('正在切换SIM...');
|
||||||
} else if (supported && activeSlot) {
|
} else if (supported && activeSlot?.present === true) {
|
||||||
setStatusMessage(elements.simStatus, `当前 ${getSimDisplayLabel(activeSlot)}`, 'success');
|
setStatusMessage(elements.simStatus, `当前 ${getSimDisplayLabel(activeSlot)}`);
|
||||||
|
} else if (supported && activeSlot?.present === false) {
|
||||||
|
setStatusMessage(elements.simStatus, `${getSimDisplayLabel(activeSlot)} 未就绪,请检查是否插卡`);
|
||||||
} else if (!supported) {
|
} else if (!supported) {
|
||||||
setStatusMessage(elements.simStatus, '双卡不可用');
|
setStatusMessage(elements.simStatus, '双卡不可用');
|
||||||
} else {
|
} else {
|
||||||
@@ -803,10 +872,10 @@ async function switchSim(slot) {
|
|||||||
body: JSON.stringify({ slot: slotNumber })
|
body: JSON.stringify({ slot: slotNumber })
|
||||||
});
|
});
|
||||||
renderSimStatus(result.data || {});
|
renderSimStatus(result.data || {});
|
||||||
setStatusMessage(elements.simStatus, result.message || `已切换到SIM${slotNumber + 1}`, 'success');
|
setSimSwitchStatus(result.message || `已切换到SIM${slotNumber + 1}`, 'success');
|
||||||
await refreshAll();
|
await refreshAll();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setStatusMessage(elements.simStatus, err.message, 'error');
|
setSimSwitchStatus(err.message, 'error');
|
||||||
await refreshSim().catch(() => {});
|
await refreshSim().catch(() => {});
|
||||||
} finally {
|
} finally {
|
||||||
state.simBusy = false;
|
state.simBusy = false;
|
||||||
|
|||||||
+229
-17
@@ -33,6 +33,7 @@ class ModemManager extends EventEmitter {
|
|||||||
switchSlot: null,
|
switchSlot: null,
|
||||||
bindSlot: null,
|
bindSlot: null,
|
||||||
switching: false,
|
switching: false,
|
||||||
|
probing: false,
|
||||||
slots: this.createSimSlots(),
|
slots: this.createSimSlots(),
|
||||||
lastCheckedAt: null,
|
lastCheckedAt: null,
|
||||||
error: '',
|
error: '',
|
||||||
@@ -63,6 +64,7 @@ class ModemManager extends EventEmitter {
|
|||||||
slot,
|
slot,
|
||||||
name: `SIM${slot + 1}`,
|
name: `SIM${slot + 1}`,
|
||||||
active: false,
|
active: false,
|
||||||
|
present: null,
|
||||||
phoneNumber,
|
phoneNumber,
|
||||||
phoneLabel: phoneNumber || `SIM${slot + 1}`,
|
phoneLabel: phoneNumber || `SIM${slot + 1}`,
|
||||||
lastCheckedAt: null
|
lastCheckedAt: null
|
||||||
@@ -538,6 +540,25 @@ class ModemManager extends EventEmitter {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateSimSlotPresence(status, slotNumber, present) {
|
||||||
|
const slot = this.normalizeSimSlot(slotNumber);
|
||||||
|
if (slot === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
status.slots = status.slots.map(item => {
|
||||||
|
if (item.slot !== slot) {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...item,
|
||||||
|
present: Boolean(present),
|
||||||
|
lastCheckedAt: new Date().toISOString()
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
getBusinessSimSlot(status = this.sim) {
|
getBusinessSimSlot(status = this.sim) {
|
||||||
return this.normalizeSimSlot(status.bindSlot) ?? this.normalizeSimSlot(status.switchSlot);
|
return this.normalizeSimSlot(status.bindSlot) ?? this.normalizeSimSlot(status.switchSlot);
|
||||||
}
|
}
|
||||||
@@ -637,14 +658,27 @@ class ModemManager extends EventEmitter {
|
|||||||
retryDelay = 2000,
|
retryDelay = 2000,
|
||||||
settleDelay = 3000,
|
settleDelay = 3000,
|
||||||
confirmTimeout = 15000,
|
confirmTimeout = 15000,
|
||||||
confirmInterval = 1000
|
confirmInterval = 1000,
|
||||||
|
simReadyAttempts = 10,
|
||||||
|
simReadyDelay = 1000
|
||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
const currentSlot = await this.querySwitchSimSlot().catch(() => this.sim.switchSlot);
|
const currentSlot = await this.querySwitchSimSlot().catch(() => this.sim.switchSlot);
|
||||||
this.sim.switchSlot = currentSlot;
|
this.sim.switchSlot = currentSlot;
|
||||||
|
|
||||||
if (currentSlot === targetSlot) {
|
if (currentSlot === targetSlot) {
|
||||||
await this.waitSIMReady();
|
try {
|
||||||
|
await this.waitSIMReady({
|
||||||
|
attempts: simReadyAttempts,
|
||||||
|
delay: simReadyDelay,
|
||||||
|
label: `SIM${targetSlot + 1}`
|
||||||
|
});
|
||||||
|
this.updateSimSlotPresence(this.sim, targetSlot, true);
|
||||||
|
} catch (err) {
|
||||||
|
this.updateSimSlotPresence(this.sim, targetSlot, false);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
return targetSlot;
|
return targetSlot;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -674,11 +708,155 @@ class ModemManager extends EventEmitter {
|
|||||||
this.sim.switchSlot = targetSlot;
|
this.sim.switchSlot = targetSlot;
|
||||||
this.markActiveSimSlot(this.sim, this.getBusinessSimSlot(this.sim) ?? targetSlot);
|
this.markActiveSimSlot(this.sim, this.getBusinessSimSlot(this.sim) ?? targetSlot);
|
||||||
await this.sleep(settleDelay);
|
await this.sleep(settleDelay);
|
||||||
await this.waitSIMReady();
|
|
||||||
|
try {
|
||||||
|
await this.waitSIMReady({
|
||||||
|
attempts: simReadyAttempts,
|
||||||
|
delay: simReadyDelay,
|
||||||
|
label: `SIM${targetSlot + 1}`
|
||||||
|
});
|
||||||
|
this.updateSimSlotPresence(this.sim, targetSlot, true);
|
||||||
|
} catch (err) {
|
||||||
|
this.updateSimSlotPresence(this.sim, targetSlot, false);
|
||||||
|
await this.restoreSwitchSimSlot(currentSlot, `${context}失败后回退`).catch((restoreErr) => {
|
||||||
|
logger.warn(`${context}失败后回退SIM失败: ${restoreErr.message}`);
|
||||||
|
});
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
return targetSlot;
|
return targetSlot;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async restoreSwitchSimSlot(slotNumber, context = 'SIM回退', timeout = 20000) {
|
||||||
|
const slot = this.normalizeSimSlot(slotNumber);
|
||||||
|
if (slot === null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.warn(`${context}: 恢复到SIM${slot + 1}`);
|
||||||
|
await this.sendATCommand(`AT+SWITCHSIM=${slot}`, 10000).catch((err) => {
|
||||||
|
logger.warn(`${context}命令失败: ${err.message}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
const restored = await this.waitForSwitchSimSlot(slot, timeout, 1000);
|
||||||
|
if (restored) {
|
||||||
|
this.sim.switchSlot = slot;
|
||||||
|
this.markActiveSimSlot(this.sim, this.getBusinessSimSlot(this.sim) ?? slot);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.warn(`${context}: 未能确认恢复到SIM${slot + 1}`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
async restoreBindSimSlot(slotNumber, context = '业务SIM回退') {
|
||||||
|
const slot = this.normalizeSimSlot(slotNumber);
|
||||||
|
if (slot === null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.sendATWithRetry(`AT+BINDSIM=${slot}`, {
|
||||||
|
timeout: 5000,
|
||||||
|
retries: 1,
|
||||||
|
retryDelay: 500,
|
||||||
|
label: `${context}到SIM${slot + 1}`
|
||||||
|
}).catch((err) => {
|
||||||
|
logger.warn(`${context}到SIM${slot + 1}失败: ${err.message}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
async restoreSimAfterProbe(switchSlot, bindSlot, context = 'SIM探测后恢复') {
|
||||||
|
const targetSwitchSlot = this.normalizeSimSlot(switchSlot);
|
||||||
|
const targetBindSlot = this.normalizeSimSlot(bindSlot) ?? targetSwitchSlot;
|
||||||
|
|
||||||
|
if (targetSwitchSlot !== null) {
|
||||||
|
await this.restoreSwitchSimSlot(targetSwitchSlot, context).catch((err) => {
|
||||||
|
logger.warn(`${context}切回SIM${targetSwitchSlot + 1}失败: ${err.message}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetBindSlot !== null) {
|
||||||
|
await this.restoreBindSimSlot(targetBindSlot, context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async probeInactiveSimSlot(status, slotNumber, originalSlot, originalBindSlot = originalSlot) {
|
||||||
|
const targetSlot = this.normalizeSimSlot(slotNumber);
|
||||||
|
const restoreSlot = this.normalizeSimSlot(originalSlot);
|
||||||
|
const restoreBindSlot = this.normalizeSimSlot(originalBindSlot) ?? restoreSlot;
|
||||||
|
if (targetSlot === null || restoreSlot === null || targetSlot === restoreSlot) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(`探测SIM${targetSlot + 1}是否可用`);
|
||||||
|
let switched = false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const resp = await this.sendATCommand(`AT+SWITCHSIM=${targetSlot}`, 10000);
|
||||||
|
if (!resp.includes('OK')) {
|
||||||
|
logger.warn(`探测SIM${targetSlot + 1}切换返回非OK: ${this.formatATResponse(resp)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
switched = await this.waitForSwitchSimSlot(targetSlot, 15000, 1000);
|
||||||
|
if (!switched) {
|
||||||
|
this.updateSimSlotPresence(status, targetSlot, false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.sleep(1000);
|
||||||
|
await this.waitSIMReady({
|
||||||
|
attempts: 5,
|
||||||
|
delay: 1000,
|
||||||
|
label: `SIM${targetSlot + 1}`
|
||||||
|
});
|
||||||
|
this.updateSimSlotPresence(status, targetSlot, true);
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
logger.warn(`探测SIM${targetSlot + 1}失败: ${err.message}`);
|
||||||
|
this.updateSimSlotPresence(status, targetSlot, false);
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
if (switched) {
|
||||||
|
await this.restoreSimAfterProbe(restoreSlot, restoreBindSlot, `探测SIM${targetSlot + 1}后恢复`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async probeSimSlotPresence(status, currentSlot) {
|
||||||
|
const originalSlot = this.normalizeSimSlot(currentSlot);
|
||||||
|
if (!status.supported || !status.canSwitch || originalSlot === null) {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.sim.switching || this.sim.probing) {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
const originalBindSlot = this.normalizeSimSlot(status.bindSlot) ?? originalSlot;
|
||||||
|
|
||||||
|
this.sim.probing = true;
|
||||||
|
try {
|
||||||
|
for (const slot of status.slots) {
|
||||||
|
if (slot.slot !== originalSlot) {
|
||||||
|
await this.probeInactiveSimSlot(status, slot.slot, originalSlot, originalBindSlot);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.restoreSimAfterProbe(originalSlot, originalBindSlot, 'SIM探测结束恢复');
|
||||||
|
|
||||||
|
const restoredSlot = await this.querySwitchSimSlot().catch(() => originalSlot);
|
||||||
|
status.switchSlot = this.normalizeSimSlot(restoredSlot) ?? originalSlot;
|
||||||
|
status.bindSlot = await this.queryBindSimSlot();
|
||||||
|
this.markActiveSimSlot(status, this.getBusinessSimSlot(status));
|
||||||
|
return status;
|
||||||
|
} finally {
|
||||||
|
this.sim.probing = false;
|
||||||
|
status.probing = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async waitForSwitchSimSlot(targetSlot, timeout = 15000, interval = 1000) {
|
async waitForSwitchSimSlot(targetSlot, timeout = 15000, interval = 1000) {
|
||||||
const startedAt = Date.now();
|
const startedAt = Date.now();
|
||||||
|
|
||||||
@@ -806,12 +984,27 @@ class ModemManager extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isSimReadyResponse(resp) {
|
||||||
|
return String(resp || '').includes('+CPIN: READY');
|
||||||
|
}
|
||||||
|
|
||||||
|
async isCurrentSimReady(timeout = 2000) {
|
||||||
|
try {
|
||||||
|
const resp = await this.sendATCommand('AT+CPIN?', timeout);
|
||||||
|
return this.isSimReadyResponse(resp);
|
||||||
|
} catch (err) {
|
||||||
|
logger.debug(`查询当前SIM就绪状态失败: ${err.message}`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async getSimStatus(options = {}) {
|
async getSimStatus(options = {}) {
|
||||||
return await this.refreshSimStatus(options);
|
return await this.refreshSimStatus(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
async refreshSimStatus(options = {}) {
|
async refreshSimStatus(options = {}) {
|
||||||
const refreshIdentity = options.refreshIdentity === true;
|
const refreshIdentity = options.refreshIdentity === true;
|
||||||
|
const probeSlots = options.probeSlots === true;
|
||||||
const status = {
|
const status = {
|
||||||
...this.sim,
|
...this.sim,
|
||||||
slots: this.sim.slots.map(slot => ({ ...slot })),
|
slots: this.sim.slots.map(slot => ({ ...slot })),
|
||||||
@@ -839,6 +1032,8 @@ class ModemManager extends EventEmitter {
|
|||||||
if (switchSlot !== null) {
|
if (switchSlot !== null) {
|
||||||
status.canSwitch = true;
|
status.canSwitch = true;
|
||||||
status.switchSlot = switchSlot;
|
status.switchSlot = switchSlot;
|
||||||
|
const currentSimReady = await this.isCurrentSimReady();
|
||||||
|
this.updateSimSlotPresence(status, switchSlot, currentSimReady);
|
||||||
} else {
|
} else {
|
||||||
status.switchSlot = null;
|
status.switchSlot = null;
|
||||||
status.error = '未能读取当前AT SIM卡槽';
|
status.error = '未能读取当前AT SIM卡槽';
|
||||||
@@ -852,6 +1047,10 @@ class ModemManager extends EventEmitter {
|
|||||||
this.updateSimSlotPhoneNumber(status, switchSlot, phoneNumber);
|
this.updateSimSlotPhoneNumber(status, switchSlot, phoneNumber);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (probeSlots && status.canSwitch && switchSlot !== null) {
|
||||||
|
await this.probeSimSlotPresence(status, switchSlot);
|
||||||
|
}
|
||||||
|
|
||||||
status.lastCheckedAt = new Date().toISOString();
|
status.lastCheckedAt = new Date().toISOString();
|
||||||
this.sim = status;
|
this.sim = status;
|
||||||
return this.getCachedSimStatus();
|
return this.getCachedSimStatus();
|
||||||
@@ -896,12 +1095,20 @@ class ModemManager extends EventEmitter {
|
|||||||
await this.switchSimForIdentityRead(targetSlot, {
|
await this.switchSimForIdentityRead(targetSlot, {
|
||||||
context: `切换SIM${targetSlot + 1}`,
|
context: `切换SIM${targetSlot + 1}`,
|
||||||
purpose: '用于业务绑定',
|
purpose: '用于业务绑定',
|
||||||
retries: 4,
|
retries: 2,
|
||||||
retryDelay: 3000,
|
retryDelay: 1000,
|
||||||
settleDelay: 3000
|
settleDelay: 1000,
|
||||||
|
confirmTimeout: 5000,
|
||||||
|
confirmInterval: 1000,
|
||||||
|
simReadyAttempts: 3,
|
||||||
|
simReadyDelay: 1000
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
await this.waitSIMReady();
|
await this.waitSIMReady({
|
||||||
|
attempts: 3,
|
||||||
|
delay: 1000,
|
||||||
|
label: `SIM${targetSlot + 1}`
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.normalizeSimSlot(before.bindSlot) !== targetSlot) {
|
if (this.normalizeSimSlot(before.bindSlot) !== targetSlot) {
|
||||||
@@ -1753,7 +1960,7 @@ class ModemManager extends EventEmitter {
|
|||||||
|
|
||||||
// 7. 按文档配置短信功能,并启用本项目需要的PDU模式
|
// 7. 按文档配置短信功能,并启用本项目需要的PDU模式
|
||||||
await this.configureSMS();
|
await this.configureSMS();
|
||||||
await this.refreshSimStatus({ refreshIdentity: true });
|
await this.refreshSimStatus({ refreshIdentity: true, probeSlots: true });
|
||||||
|
|
||||||
logger.info('模组初始化完成');
|
logger.info('模组初始化完成');
|
||||||
this.emit('ready');
|
this.emit('ready');
|
||||||
@@ -1762,24 +1969,29 @@ class ModemManager extends EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* 等待SIM卡完成初始化
|
* 等待SIM卡完成初始化
|
||||||
*/
|
*/
|
||||||
async waitSIMReady() {
|
async waitSIMReady(options = {}) {
|
||||||
for (let attempt = 1; attempt <= 10; attempt++) {
|
const attempts = options.attempts ?? 10;
|
||||||
|
const delay = options.delay ?? 1000;
|
||||||
|
const timeout = options.timeout ?? 2000;
|
||||||
|
const label = options.label || 'SIM卡';
|
||||||
|
|
||||||
|
for (let attempt = 1; attempt <= attempts; attempt++) {
|
||||||
try {
|
try {
|
||||||
const resp = await this.sendATCommand('AT+CPIN?', 2000);
|
const resp = await this.sendATCommand('AT+CPIN?', timeout);
|
||||||
if (resp.includes('+CPIN: READY')) {
|
if (this.isSimReadyResponse(resp)) {
|
||||||
logger.info('✓ SIM卡已就绪');
|
logger.info(`✓ ${label}已就绪`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.warn(`SIM卡未就绪(${attempt}/10): ${this.formatATResponse(resp)}`);
|
logger.warn(`${label}未就绪(${attempt}/${attempts}): ${this.formatATResponse(resp)}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.warn(`查询SIM卡状态失败(${attempt}/10): ${err.message}`);
|
logger.warn(`查询${label}状态失败(${attempt}/${attempts}): ${err.message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.sleep(1000);
|
await this.sleep(delay);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error('SIM卡未就绪');
|
throw new Error(`${label}未就绪,可能未插卡或SIM初始化失败`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user