Forráskód Böngészése

add: 低电压关机保护,电压低于3.3就关机;长连接功耗测试;增加打开关闭GPS按钮

chenxuuu 1 éve
szülő
commit
21a96b2917

+ 18 - 0
script/turnkey/hz201p/attributes.lua

@@ -19,6 +19,8 @@ local attributes = {
     vbat = 0,
     audioStatus = "空闲",
     callStatus = "不支持",
+    isGPSOn = true,
+    sleepMode = false,
 }
 
 --已修改的数据,缓存在这里,等待上报
@@ -64,6 +66,17 @@ function t.set(k,v,fromCloud)
     if attributes[k] == v then
         return
     end
+    --休眠模式下,只有sleepMode属性可以修改
+    if attributes.sleepMode then
+        log.info("attributes.set", "sleepMode",k,v)
+        if fromCloud then--云端下发的数据只能修改sleepMode属性
+            if k ~= "sleepMode" then
+                return
+            end
+        else
+            return
+        end
+    end
     if type(v) == "table" then
         local hasChange = false
         for k1,v1 in pairs(v) do
@@ -95,4 +108,9 @@ function t.all()
     return attributes
 end
 
+--刷新所有数据
+function t.setAll()
+    reportTemp = attributes
+end
+
 return t

+ 21 - 2
script/turnkey/hz201p/battery.lua

@@ -12,16 +12,21 @@ function voltage_to_percentage(voltage)
     end
 end
 
+
 adc.open(adc.CH_VBAT)
 sys.taskInit(function ()
-    while true do
+    repeat
         local voltage = adc.get(adc.CH_VBAT)
         local percentage = voltage_to_percentage(voltage)
         log.info("battery", "voltage:", voltage, "percentage:", percentage)
+        --低于3.3V时,关机
+        if voltage < 3300 then
+            pm.shutdown()
+        end
         attributes.set("battery", percentage)
         attributes.set("vbat", voltage)
         sys.wait(60000)
-    end
+    until nil
 end)
 
 --充电状态检测
@@ -31,3 +36,17 @@ local function chargeCheck()
 end
 gpio.setup(42, chargeCheck, 0, gpio.BOTH)
 attributes.set("isCharging", gpio.get(42) == 0)
+
+sys.subscribe("SLEEP_CMD_RECEIVED", function(on)
+    if on then
+        log.info("battery","enter sleepMode wait")
+        pm.power(pm.WORK_MODE, 1)--进入休眠模式
+    else
+        log.info("battery","exit sleepMode wait")
+        pm.power(pm.WORK_MODE, 0)--退出休眠模式
+        --上报所有参数
+        sys.timerStart(attributes.setAll, 6000)
+        --重启一下
+        --pm.reboot()
+    end
+end)

+ 4 - 1
script/turnkey/hz201p/ccVolte.lua

@@ -125,7 +125,10 @@ sys.taskInit(function()
     while true do
         local _,cmd,param = sys.waitUntil("AUDIO_CMD_RECEIVED")
         log.info("audio", cmd, param)
-        if cmd == "call" then
+        --低功耗模式就别放了
+        if attributes.get("sleepMode") then
+            log.info("audio", "sleepMode cancel play")
+        elseif cmd == "call" then
             if ccReady then
                 cc.dial(0,param) --拨打电话
                 attributes.set("callStatus", "通话中")

+ 9 - 0
script/turnkey/hz201p/cloud.lua

@@ -32,6 +32,15 @@ local function onReceive(response)
         if k == "phone" then
             -- 设备接收到云端下发的电话号码,开始拨打电话。
             sys.publish("AUDIO_CMD_RECEIVED","call",v)
+        elseif k == "sleepMode" then
+            -- 设备收到低功耗要求,更改低功耗模式
+            attributes.set("isGPSOn", false)
+            attributes.set("isFixed", "定位功能已关闭")
+            attributes.set("lat", "无数据")
+            attributes.set("lng", "无数据")
+            --最后再更改变量
+            attributes.set(k, v, true)
+            sys.timerStart(sys.publish, 6000, "SLEEP_CMD_RECEIVED", v)
         else
             attributes.set(k, v, true)
         end

+ 78 - 44
script/turnkey/hz201p/gnss.lua

@@ -1,7 +1,29 @@
+-- gnss的供电
+local gnssEnvPower = gpio.setup(26, 1)
+local gpsPower = gpio.setup(2, 1)
+-- gnss的复位
+local gpsRst = gpio.setup(27, 1)
+local isOn = true
+
+local function power(on)
+    if on ~= isOn then
+        if on then--开机后要清空一下
+            libgnss.clear()
+        end
+        gnssEnvPower(on and 1 or 0)
+        gpsPower(on and 1 or 0)
+        if on then--开机后要清空一下
+            gpsRst(0)
+            sys.timerStart(gpsRst, 500, 1)
+            libgnss.clear()
+        end
+        isOn = on
+    end
+end
+
+
 sys.taskInit(function()
     log.info("GPS", "start")
-    -- gnss的复位
-    local gpsRst = gpio.setup(27, 1)
 
     local uartId = 2
     libgnss.clear() -- 清空数据,兼初始化
@@ -32,28 +54,37 @@ end)
 local latLbs, lngLbs, typeLbs
 
 sys.timerLoopStart(function ()
-    local isFixed = libgnss.isFix()
-    if isFixed then--优先使用gps数据
-        local loc = libgnss.getRmc(2)
-        attributes.set("isFixed", "已定位")
-        attributes.set("lat", tostring(loc.lat))
-        attributes.set("lng", tostring(loc.lng))
-        attributes.set("location", {
-            lat = loc.lat,
-            lng = loc.lng,
-        })
-    elseif latLbs and lngLbs then
-        attributes.set("isFixed", typeLbs)
-        attributes.set("lat", tostring(latLbs))
-        attributes.set("lng", tostring(lngLbs))
-        attributes.set("location", {
-            lat = tonumber(latLbs),
-            lng = tonumber(lngLbs),
-        })
+    if attributes.get("isGPSOn") then
+        if attributes.get("sleepMode") then--休眠模式,关闭GPS
+            attributes.set("isGPSOn", false)
+        else
+            local isFixed = libgnss.isFix()
+            if isFixed then--优先使用gps数据
+                local loc = libgnss.getRmc(2)
+                attributes.set("isFixed", "已定位")
+                attributes.set("lat", tostring(loc.lat))
+                attributes.set("lng", tostring(loc.lng))
+                attributes.set("location", {
+                    lat = loc.lat,
+                    lng = loc.lng,
+                })
+            elseif latLbs and lngLbs then
+                attributes.set("isFixed", typeLbs)
+                attributes.set("lat", tostring(latLbs))
+                attributes.set("lng", tostring(lngLbs))
+                attributes.set("location", {
+                    lat = tonumber(latLbs),
+                    lng = tonumber(lngLbs),
+                })
+            else
+                attributes.set("isFixed", "获取中")
+                attributes.set("lat", "无数据")
+                attributes.set("lng", "无数据")
+            end
+            power(true)
+        end
     else
-        attributes.set("isFixed", "获取中")
-        attributes.set("lat", "无数据")
-        attributes.set("lng", "无数据")
+        power(false)
     end
 end,3000)
 
@@ -63,7 +94,7 @@ local lbsLoc = require("lbsLoc")
 local function getLocCb(result, lat, lng, addr, time, locType)
     log.info("testLbsLoc.getLocCb", result, lat, lng)
     -- 基站定位获取经纬度成功
-    if result == 0 then
+    if result == 0 and attributes.get("isGPSOn") and not attributes.get("sleepMode") then
         latLbs, lngLbs = lat, lng
         typeLbs = locType == 0 and "基站定位" or "WIFI定位"
     end
@@ -72,29 +103,32 @@ end
 sys.taskInit(function()
     sys.waitUntil("IP_READY", 30000)
     while mobile do -- 没有mobile库就没有基站定位
-        --基站定位信息
-        mobile.reqCellInfo(15)
-        sys.waitUntil("CELL_INFO_UPDATE", 3000)
-        --wifi定位信息
-        wlan.scan()
-        local reqWifi
-        local r = sys.waitUntil("WLAN_SCAN_DONE", 60000)
-        if r then
-            local results = wlan.scanResult()
-            log.info("wifi scan", "count", #results)
-            if #results > 0 then
-                local reqWifi = {}
-                for k,v in pairs(results) do
-                    log.info("scan", v["ssid"], v["rssi"], v["bssid"]:toHex())
-                    local bssid = v["bssid"]:toHex()
-                    bssid = string.format ("%s:%s:%s:%s:%s:%s", bssid:sub(1,2), bssid:sub(3,4), bssid:sub(5,6), bssid:sub(7,8), bssid:sub(9,10), bssid:sub(11,12))
-                    reqWifi[bssid]=v["rssi"]
+        if attributes.get("isGPSOn") and not attributes.get("sleepMode") then--开启定位功能后再定位
+            --基站定位信息
+            mobile.reqCellInfo(15)
+            sys.waitUntil("CELL_INFO_UPDATE", 3000)
+            --wifi定位信息
+            wlan.scan()
+            local reqWifi
+            local r = sys.waitUntil("WLAN_SCAN_DONE", 60000)
+            if r then
+                local results = wlan.scanResult()
+                log.info("wifi scan", "count", #results)
+                if #results > 0 then
+                    local reqWifi = {}
+                    for k,v in pairs(results) do
+                        log.info("scan", v["ssid"], v["rssi"], v["bssid"]:toHex())
+                        local bssid = v["bssid"]:toHex()
+                        bssid = string.format ("%s:%s:%s:%s:%s:%s", bssid:sub(1,2), bssid:sub(3,4), bssid:sub(5,6), bssid:sub(7,8), bssid:sub(9,10), bssid:sub(11,12))
+                        reqWifi[bssid]=v["rssi"]
+                    end
                 end
             end
-        end
-        if not libgnss.isFix() then--没定位成功再去获取
-            lbsLoc.request(getLocCb,nil,nil,nil,nil,nil,nil,reqWifi)
+            if not libgnss.isFix() then--没定位成功再去获取
+                lbsLoc.request(getLocCb,nil,nil,nil,nil,nil,nil,reqWifi)
+            end
         end
         sys.wait(60000)
     end
 end)
+

+ 6 - 4
script/turnkey/hz201p/main.lua

@@ -32,9 +32,6 @@ if allDone then
 
     -- gnss的备电和gsensor的供电
     local vbackup = gpio.setup(24, 1)
-    -- gnss的供电
-    local gnssEnvPower = gpio.setup(26, 1)
-    local gpsPower = gpio.setup(2, 1)
     -- 使用合宙iot平台时需要这个参数
     PRODUCT_KEY = "YXdzIDo5QawWCIRywShMAKjmJsInXtsb" -- 到 iot.openluat.com 创建项目,获取正确的项目id
     libfota = require "libfota"
@@ -76,7 +73,12 @@ if allDone then
     local redLed = gpio.setup(16, 0, nil, nil, 4)
     sys.taskInit(function()
         while true do
-            if attributes.get("ledControl") then
+            if attributes.get("sleepMode") then
+                blueLed(0)
+                redLed(0)
+                --直接死等到休眠状态变化后再跑
+                sys.waitUntil("SLEEP_CMD_RECEIVED")
+            elseif attributes.get("ledControl") then
                 blueLed(attributes.get("blueLed") and 1 or 0)
                 redLed(attributes.get("redLed") and 1 or 0)
                 sys.wait(500)