statusbar.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. statusbar_8000 = {}
  2. lbsLoc2 = require "lbsLoc2"
  3. --状态栏数据
  4. statusbar_8000.data = {
  5. -- 电池状态
  6. bat_level = 0, -- 电量等级 (0-4)
  7. sig_stren = 0, -- 4G信号强度 (0-4)
  8. net_ready = false,
  9. -- 天气信息
  10. weather = {
  11. text = "", -- 天气文本 ("晴", "雨"等)
  12. temp = "", -- 温度
  13. humidity = "", -- 湿度
  14. result = false, -- 是否获取成功
  15. },
  16. weather_icon = "/luadb/default.jpg"
  17. }
  18. local weather_icons = {
  19. ["云"] = "/luadb/cloudy.jpg",
  20. ["雨"] = "/luadb/rain.jpg",
  21. ["晴"] = "/luadb/sunny.jpg",
  22. ["雪"] = "/luadb/snow.jpg",
  23. ["阴"] = "/luadb/cloudyday.jpg",
  24. ["风"] = "/luadb/wind.jpg"
  25. }
  26. function statusbar_8000.get_bat_level()
  27. local data = statusbar_8000.data
  28. adc.open(adc.CH_VBAT)
  29. local vbat = adc.get(adc.CH_VBAT)
  30. local dump_power = vbat/4200
  31. log.info("当前电量为:",dump_power,vbat)
  32. dump_rate = dump_power*100
  33. if dump_rate == 0 then
  34. data.bat_level = 0
  35. elseif dump_rate <= 25 then
  36. data.bat_level = 1
  37. elseif dump_rate <=50 then
  38. data.bat_level = 2
  39. elseif dump_rate <= 75 then
  40. data.bat_level = 3
  41. else
  42. data.bat_level = 4
  43. end
  44. log.info("bat_level:",data.bat_level)
  45. end
  46. function statusbar_8000.get_sig_strength()
  47. local data = statusbar_8000.data
  48. local cp = mobile.status()
  49. log.info("mobile status:", cp)
  50. if cp == 1 or cp == 5 then
  51. data.net_ready = true
  52. else
  53. data.net_ready = false
  54. return
  55. end
  56. local c1 = mobile.csq()
  57. if c1 < 8 or c1 > 31 then -- "网络注册失败
  58. data.sig_stren = 1
  59. elseif c1 <= 15 then -- 网络信号弱
  60. data.sig_stren = 2
  61. elseif c1 <= 26 then -- 网络信号正常
  62. data.sig_stren = 3
  63. else
  64. data.sig_stren = 4
  65. end
  66. end
  67. function statusbar_8000.get_weather()
  68. local lbs_location
  69. local winfo
  70. local data = statusbar_8000.data
  71. if not data.net_ready then
  72. return
  73. end
  74. mobile.reqCellInfo(15)
  75. sys.waitUntil("CELL_INFO_UPDATE", 3000)
  76. local lbs_lat, lbs_lng ,t = lbsLoc2.request(5000, nil, nil, true)
  77. log.info("lat,lng",lbs_lat,lbs_lng)
  78. if lbs_lat and lbs_lng then
  79. lbs_location = lbs_lng..","..lbs_lat
  80. else
  81. lbs_location = "101010100"
  82. end
  83. log.info("lbs_location",lbs_location)
  84. local url = "https://devapi.qweather.com/v7/weather/now?location="..lbs_location.."&key=2c69d20742df4b3bbfa3c7006ce35210"
  85. local code, headers, body = http.request("GET", url).wait(300)
  86. log.info("url=",url)
  87. log.info("http.gzip", code)
  88. if code ~= 200 then
  89. return ""
  90. end
  91. log.info("21")
  92. local re = miniz.uncompress(body:sub(11), 0)
  93. log.info("和风天气", re)
  94. if re == nil or re == "" then
  95. return
  96. end
  97. local jdata = json.decode(re)
  98. log.info("jdata", jdata)
  99. if not jdata then
  100. return
  101. end
  102. log.info("和风天气", jdata.code,jdata.now)
  103. if not jdata.now then
  104. return
  105. end
  106. log.info("和风天气", "天气", jdata.now.text)
  107. data.weather.text = jdata.now.text
  108. data.weather.temp = jdata.now.temp
  109. data.weather.humidity = jdata.now.humidity
  110. data.result = true
  111. for keyword, icon in pairs(weather_icons) do
  112. if string.find(data.weather.text, keyword) then
  113. data.weather_icon = icon
  114. break
  115. end
  116. end
  117. end
  118. return statusbar_8000