123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- local util_mobile = {}
- -- 运营商数据
- local oper_data = {
- -- 中国移动
- ["46000"] = { "CM", "中国移动", { "10086", "CXLL" } },
- ["46002"] = { "CM", "中国移动", { "10086", "CXLL" } },
- ["46007"] = { "CM", "中国移动", { "10086", "CXLL" } },
- ["46008"] = { "CM", "中国移动", { "10086", "CXLL" } },
- -- 中国联通
- ["46001"] = { "CU", "中国联通", { "10010", "2082" } },
- ["46006"] = { "CU", "中国联通", { "10010", "2082" } },
- ["46009"] = { "CU", "中国联通", { "10010", "2082" } },
- ["46010"] = { "CU", "中国联通", { "10010", "2082" } },
- -- 中国电信
- ["46003"] = { "CT", "中国电信", { "10001", "108" } },
- ["46005"] = { "CT", "中国电信", { "10001", "108" } },
- ["46011"] = { "CT", "中国电信", { "10001", "108" } },
- ["46012"] = { "CT", "中国电信", { "10001", "108" } },
- -- 中国广电
- ["46015"] = { "CB", "中国广电" },
- }
- --- 获取 MCC 和 MNC
- -- @return MCC or -1
- -- @return MNC or -1
- function util_mobile.getMccMnc()
- local imsi = mobile.imsi(mobile.simid()) or ""
- return string.sub(imsi, 1, 3) or -1, string.sub(imsi, 4, 5) or -1
- end
- --- 获取 Band
- -- @return Band or -1
- function util_mobile.getBand()
- local info = mobile.getCellInfo()[1] or {}
- return info.band or -1
- end
- --- 获取运营商
- -- @param is_zh 是否返回中文
- -- @return 运营商 or ""
- function util_mobile.getOper(is_zh)
- local imsi = mobile.imsi(mobile.simid()) or ""
- local mcc, mnc = string.sub(imsi, 1, 3), string.sub(imsi, 4, 5)
- local mcc_mnc = mcc .. mnc
- local oper = oper_data[mcc_mnc]
- if oper then
- return is_zh and oper[2] or oper[1]
- else
- return mcc_mnc
- end
- end
- --- 发送查询流量短信
- function util_mobile.queryTraffic()
- local imsi = mobile.imsi(mobile.simid()) or ""
- local mcc_mnc = string.sub(imsi, 1, 5)
- local oper = oper_data[mcc_mnc]
- if oper and oper[3] then
- sms.send(oper[3][1], oper[3][2])
- else
- log.warn("util_mobile.queryTraffic", "查询流量代码未配置")
- end
- end
- --- 获取网络状态
- -- @return 网络状态
- function util_mobile.status()
- local codes = {
- [0] = "网络未注册",
- [1] = "网络已注册",
- [2] = "网络搜索中",
- [3] = "网络注册被拒绝",
- [4] = "网络状态未知",
- [5] = "网络已注册,漫游",
- [6] = "网络已注册,仅SMS",
- [7] = "网络已注册,漫游,仅SMS",
- [8] = "网络已注册,紧急服务",
- [9] = "网络已注册,非主要服务",
- [10] = "网络已注册,非主要服务,漫游",
- }
- local mobile_status = mobile.status()
- if mobile_status and mobile_status >= 0 and mobile_status <= 10 then
- return codes[mobile_status] or "未知网络状态"
- end
- return "未知网络状态"
- end
- --- 追加设备信息
- --- @return string
- function util_mobile.appendDeviceInfo()
- local msg = "\n"
- -- 本机号码
- local number = mobile.number(mobile.simid()) or config.FALLBACK_LOCAL_NUMBER
- if number then
- msg = msg .. "\n本机号码: " .. number
- end
- -- 开机时长
- local ms = mcu.ticks()
- local seconds = math.floor(ms / 1000)
- local minutes = math.floor(seconds / 60)
- local hours = math.floor(minutes / 60)
- seconds = seconds % 60
- minutes = minutes % 60
- local boot_time = string.format("%02d:%02d:%02d", hours, minutes, seconds)
- if ms >= 0 then
- msg = msg .. "\n开机时长: " .. boot_time
- end
- -- 运营商
- local oper = util_mobile.getOper(true)
- if oper ~= "" then
- msg = msg .. "\n运营商: " .. oper
- end
- -- 信号
- msg = msg .. "\n信号: " .. mobile.rsrp() .. "dBm"
- -- 频段
- -- local band = util_mobile.getBand()
- -- if band >= 0 then
- -- msg = msg .. "\n频段: B" .. band
- -- end
- -- 电压, 读取 VBAT 供电电压, 单位为 mV
- -- adc.open(adc.CH_VBAT)
- -- local vbat = adc.get(adc.CH_VBAT)
- -- adc.close(adc.CH_VBAT)
- -- if vbat >= 0 then
- -- msg = msg .. "\n电压: " .. string.format("%.1f", vbat / 1000) .. "V"
- -- end
- -- 温度
- -- adc.open(adc.CH_CPU)
- -- local temp = adc.get(adc.CH_CPU)
- -- adc.close(adc.CH_CPU)
- -- if temp >= 0 then
- -- msg = msg .. "\n温度: " .. string.format("%.1f", temp / 1000) .. "°C"
- -- end
- -- 基站信息
- -- msg = msg .. "\nECI: " .. mobile.eci()
- -- msg = msg .. "\nTAC: " .. mobile.tac()
- -- msg = msg .. "\nENBID: " .. mobile.enbid()
- -- 流量统计
- -- local uplinkGB, uplinkB, downlinkGB, downlinkB = mobile.dataTraffic()
- -- uplinkB = uplinkGB * 1024 * 1024 * 1024 + uplinkB
- -- downlinkB = downlinkGB * 1024 * 1024 * 1024 + downlinkB
- -- local function formatBytes(bytes)
- -- if bytes < 1024 then
- -- return bytes .. "B"
- -- elseif bytes < 1024 * 1024 then
- -- return string.format("%.2fKB", bytes / 1024)
- -- elseif bytes < 1024 * 1024 * 1024 then
- -- return string.format("%.2fMB", bytes / 1024 / 1024)
- -- else
- -- return string.format("%.2fGB", bytes / 1024 / 1024 / 1024)
- -- end
- -- end
- -- msg = msg .. "\n流量: ↑" .. formatBytes(uplinkB) .. " ↓" .. formatBytes(downlinkB)
- -- 位置
- local _, _, map_link = util_location.get()
- if map_link ~= "" then
- msg = msg .. "\n位置: " .. map_link -- 这里使用 U+00a0 防止换行
- end
- return msg
- end
- return util_mobile
|