util_mobile.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. local util_mobile = {}
  2. --- 验证 pin 码
  3. -- @param pin_code string, pin 码
  4. function util_mobile.pinVerify(pin_code)
  5. local sim_id = mobile.simid()
  6. pin_code = tostring(pin_code or "")
  7. if #pin_code < 4 or #pin_code > 8 then
  8. log.warn("util_mobile.pinVerify", "pin 码长度不正确")
  9. return
  10. end
  11. local cpin_is_ready = mobile.simPin(sim_id)
  12. if cpin_is_ready then
  13. log.info("util_mobile.pinVerify", "无需验证 pin 码")
  14. return
  15. end
  16. cpin_is_ready = mobile.simPin(sim_id, mobile.PIN_VERIFY, pin_code)
  17. log.info("util_mobile.pinVerify", "验证 pin 码" .. (cpin_is_ready and "成功" or "失败"))
  18. end
  19. -- 运营商数据
  20. local oper_data = {
  21. -- 中国移动
  22. ["46000"] = { "CM", "中国移动", { "10086", "CXLL" } },
  23. ["46002"] = { "CM", "中国移动", { "10086", "CXLL" } },
  24. ["46007"] = { "CM", "中国移动", { "10086", "CXLL" } },
  25. ["46008"] = { "CM", "中国移动", { "10086", "CXLL" } },
  26. -- 中国联通
  27. ["46001"] = { "CU", "中国联通", { "10010", "2082" } },
  28. ["46006"] = { "CU", "中国联通", { "10010", "2082" } },
  29. ["46009"] = { "CU", "中国联通", { "10010", "2082" } },
  30. ["46010"] = { "CU", "中国联通", { "10010", "2082" } },
  31. -- 中国电信
  32. ["46003"] = { "CT", "中国电信", { "10001", "108" } },
  33. ["46005"] = { "CT", "中国电信", { "10001", "108" } },
  34. ["46011"] = { "CT", "中国电信", { "10001", "108" } },
  35. ["46012"] = { "CT", "中国电信", { "10001", "108" } },
  36. -- 中国广电
  37. ["46015"] = { "CB", "中国广电" },
  38. }
  39. --- 获取 MCC 和 MNC
  40. -- @return MCC or -1
  41. -- @return MNC or -1
  42. function util_mobile.getMccMnc()
  43. local imsi = mobile.imsi(mobile.simid()) or ""
  44. return string.sub(imsi, 1, 3) or -1, string.sub(imsi, 4, 5) or -1
  45. end
  46. --- 获取 Band
  47. -- @return Band or -1
  48. function util_mobile.getBand()
  49. local info = mobile.getCellInfo()[1] or {}
  50. return info.band or -1
  51. end
  52. --- 获取运营商
  53. -- @param is_zh 是否返回中文
  54. -- @return 运营商 or ""
  55. function util_mobile.getOper(is_zh)
  56. local imsi = mobile.imsi(mobile.simid()) or ""
  57. local mcc, mnc = string.sub(imsi, 1, 3), string.sub(imsi, 4, 5)
  58. local mcc_mnc = mcc .. mnc
  59. local oper = oper_data[mcc_mnc]
  60. if oper then
  61. return is_zh and oper[2] or oper[1]
  62. else
  63. return mcc_mnc
  64. end
  65. end
  66. --- 发送查询流量短信
  67. function util_mobile.queryTraffic()
  68. local imsi = mobile.imsi(mobile.simid()) or ""
  69. local mcc_mnc = string.sub(imsi, 1, 5)
  70. local oper = oper_data[mcc_mnc]
  71. if oper and oper[3] then
  72. sms.send(oper[3][1], oper[3][2])
  73. else
  74. log.warn("util_mobile.queryTraffic", "查询流量代码未配置")
  75. end
  76. end
  77. --- 获取网络状态
  78. -- @return 网络状态
  79. function util_mobile.status()
  80. local codes = {
  81. [0] = "网络未注册",
  82. [1] = "网络已注册",
  83. [2] = "网络搜索中",
  84. [3] = "网络注册被拒绝",
  85. [4] = "网络状态未知",
  86. [5] = "网络已注册,漫游",
  87. [6] = "网络已注册,仅SMS",
  88. [7] = "网络已注册,漫游,仅SMS",
  89. [8] = "网络已注册,紧急服务",
  90. [9] = "网络已注册,非主要服务",
  91. [10] = "网络已注册,非主要服务,漫游",
  92. }
  93. local mobile_status = mobile.status()
  94. if mobile_status and mobile_status >= 0 and mobile_status <= 10 then
  95. return codes[mobile_status] or "未知网络状态"
  96. end
  97. return "未知网络状态"
  98. end
  99. --- 追加设备信息
  100. --- @return string
  101. function util_mobile.appendDeviceInfo()
  102. local msg = "\n"
  103. -- 本机号码
  104. local number = mobile.number(mobile.simid()) or config.FALLBACK_LOCAL_NUMBER
  105. if number then
  106. msg = msg .. "\n本机号码: " .. number
  107. end
  108. -- 开机时长
  109. local ms = mcu.ticks()
  110. local seconds = math.floor(ms / 1000)
  111. local minutes = math.floor(seconds / 60)
  112. local hours = math.floor(minutes / 60)
  113. seconds = seconds % 60
  114. minutes = minutes % 60
  115. local boot_time = string.format("%02d:%02d:%02d", hours, minutes, seconds)
  116. if ms >= 0 then
  117. msg = msg .. "\n开机时长: " .. boot_time
  118. end
  119. -- 运营商
  120. local oper = util_mobile.getOper(true)
  121. if oper ~= "" then
  122. msg = msg .. "\n运营商: " .. oper
  123. end
  124. -- 信号
  125. msg = msg .. "\n信号: " .. mobile.rsrp() .. "dBm"
  126. -- 频段
  127. -- local band = util_mobile.getBand()
  128. -- if band >= 0 then
  129. -- msg = msg .. "\n频段: B" .. band
  130. -- end
  131. -- 电压, 读取 VBAT 供电电压, 单位为 mV
  132. -- adc.open(adc.CH_VBAT)
  133. -- local vbat = adc.get(adc.CH_VBAT)
  134. -- adc.close(adc.CH_VBAT)
  135. -- if vbat >= 0 then
  136. -- msg = msg .. "\n电压: " .. string.format("%.1f", vbat / 1000) .. "V"
  137. -- end
  138. -- 温度
  139. -- adc.open(adc.CH_CPU)
  140. -- local temp = adc.get(adc.CH_CPU)
  141. -- adc.close(adc.CH_CPU)
  142. -- if temp >= 0 then
  143. -- msg = msg .. "\n温度: " .. string.format("%.1f", temp / 1000) .. "°C"
  144. -- end
  145. -- 基站信息
  146. -- msg = msg .. "\nECI: " .. mobile.eci()
  147. -- msg = msg .. "\nTAC: " .. mobile.tac()
  148. -- msg = msg .. "\nENBID: " .. mobile.enbid()
  149. -- 流量统计
  150. -- local uplinkGB, uplinkB, downlinkGB, downlinkB = mobile.dataTraffic()
  151. -- uplinkB = uplinkGB * 1024 * 1024 * 1024 + uplinkB
  152. -- downlinkB = downlinkGB * 1024 * 1024 * 1024 + downlinkB
  153. -- local function formatBytes(bytes)
  154. -- if bytes < 1024 then
  155. -- return bytes .. "B"
  156. -- elseif bytes < 1024 * 1024 then
  157. -- return string.format("%.2fKB", bytes / 1024)
  158. -- elseif bytes < 1024 * 1024 * 1024 then
  159. -- return string.format("%.2fMB", bytes / 1024 / 1024)
  160. -- else
  161. -- return string.format("%.2fGB", bytes / 1024 / 1024 / 1024)
  162. -- end
  163. -- end
  164. -- msg = msg .. "\n流量: ↑" .. formatBytes(uplinkB) .. " ↓" .. formatBytes(downlinkB)
  165. -- 位置
  166. local _, _, map_link = util_location.get()
  167. if map_link ~= "" then
  168. msg = msg .. "\n位置: " .. map_link -- 这里使用 U+00a0 防止换行
  169. end
  170. return msg
  171. end
  172. return util_mobile