util_mobile.lua 5.4 KB

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