Prechádzať zdrojové kódy

Merge branch 'master' of https://gitee.com/openLuat/LuatOS

alienwalker 1 rok pred
rodič
commit
f067df6b53
2 zmenil súbory, kde vykonal 150 pridanie a 7 odobranie
  1. 98 0
      demo/airlbs/main.lua
  2. 52 7
      script/libs/airlbs.lua

+ 98 - 0
demo/airlbs/main.lua

@@ -0,0 +1,98 @@
+-- LuaTools需要PROJECT和VERSION这两个信息
+PROJECT = "helloworld"
+VERSION = "1.0.0"
+
+-- 使用合宙iot平台时需要这个参数
+PRODUCT_KEY = "xxx" -- 到 iot.openluat.com 创建项目,获取正确的项目id
+
+-- 引入必要的库文件(lua编写), 内部库不需要require
+sys = require("sys")
+sysplus = require("sysplus")
+libnet = require "libnet"
+
+log.info("main", "hello world")
+
+print(_VERSION)
+
+if wdt then
+    --添加硬狗防止程序卡死,在支持的设备上启用这个功能
+    wdt.init(20000)--初始化watchdog设置为9s
+    sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
+end
+
+local airlbs = require "airlbs"
+
+-- 此为收费服务,需自行联系销售申请
+local airlbs_project_id = "xxx"
+local airlbs_project_key = "xxx"
+
+sys.taskInit(function()
+    sys.waitUntil("IP_READY")
+
+    socket.sntp()
+    sys.waitUntil("NTP_UPDATE", 1000)
+
+    while 1 do
+        local result , data = airlbs.request({project_id = airlbs_project_id,project_key = airlbs_project_key})
+        if result then
+            print("airlbs", json.encode(data))
+        end
+        sys.wait(20000)
+    end
+
+end)
+
+-- 用户代码已结束---------------------------------------------
+-- 结尾总是这一句
+sys.run()
+-- sys.run()之后后面不要加任何语句!!!!!
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 52 - 7
script/libs/airlbs.lua

@@ -1,4 +1,31 @@
+--[[
+@module airlbs
+@summary airlbs 定位服务(收费服务,需自行联系销售申请)
+@version 1.0
+@date    2024.11.01
+@author  Dozingfiretruck
+@usage
+--注意:因使用了sys.wait()所有api需要在协程中使用
+--注意:使用前需同步时间
+-- 用法实例
+local airlbs = require "airlbs"
+
+sys.taskInit(function()
+    sys.waitUntil("IP_READY")
+
+    socket.sntp()
+    sys.waitUntil("NTP_UPDATE", 1000)
 
+    while 1 do
+        local result , data = airlbs.request({project_id = "xxx",project_key = 'xxx',timeout = 1000})
+        if result then
+            print("airlbs", json.encode(data))
+        end
+        sys.wait(20000)
+    end
+
+end)
+]]
 
 sys = require("sys")
 sysplus = require("sysplus")
@@ -13,6 +40,7 @@ local lib_topic = lib_name.."topic"
 
 local location_data = 0
 local disconnect = -1
+local airlbs_timeout = 15000
 
 local airlbs ={}
 
@@ -41,13 +69,14 @@ local function airlbs_task(task_name, buff, timeout)
             break
         end
         if buff:used() > 0 then
+            local location = nil
             local data = buff:query(0,1) -- 获取数据
             if data:toHex() == '00' then
-                local location = json.decode(buff:query(1))
-                sys.publish(lib_topic, location_data, location)
+                location = json.decode(buff:query(1))
             else
                 log.error(lib_name,"not json data")
             end
+            sys.publish(lib_topic, location_data, location)
             buff:del()
             break
         end
@@ -67,6 +96,18 @@ local function netCB(msg)
 	log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
 end
 
+--[[
+获取定位数据
+@api airlbs.request(param)
+@param table 参数(联系销售获取id与key) project_id:项目ID project_key:项目密钥 timeout:超时时间,单位毫秒 默认15000
+@return bool 成功返回true,失败会返回false
+@return table 定位成功生效,成功返回定位数据
+@usage
+local result , data = airlbs.request({project_id = airlbs_project_id,project_key = airlbs_project_key})
+if result then
+    print("airlbs", json.encode(data))
+end
+]]
 function airlbs.request(param)
     if not mobile then 
         log.error(lib_name,"no mobile")
@@ -97,7 +138,7 @@ function airlbs.request(param)
     -- log.debug(lib_name,"hmac_sha1", hmac_data)
 
     mobile.reqCellInfo(60)
-    sys.waitUntil("CELL_INFO_UPDATE", param.timeout or 15000)
+    sys.waitUntil("CELL_INFO_UPDATE", param.timeout or airlbs_timeout)
     -- log.info("cell", json.encode(mobile.getCellInfo()))
 
     local lbs_data = {cells={}}
@@ -118,20 +159,23 @@ function airlbs.request(param)
 
     udp_buff:write(string.char(auth_type) .. project_id .. imei .. muid .. timestamp .. nonce .. hmac_data:fromHex() .. string.char(lbs_data_type) .. lbs_jdata)
 
-    sysplus.taskInitEx(airlbs_task, lib_name, netCB, lib_name, udp_buff, param.timeout or 15000)
+    sysplus.taskInitEx(airlbs_task, lib_name, netCB, lib_name, udp_buff, param.timeout or airlbs_timeout)
 
     while 1 do
-        local result, tp, data = sys.waitUntil(lib_topic, param.timeout or 15000)
+        local result, tp, data = sys.waitUntil(lib_topic, param.timeout or airlbs_timeout)
         log.info("event", result, tp, data)
         if not result then
             return false,"timeout"
         elseif tp == location_data then
+            if not data then
+                log.error(lib_name,"no data, please check project_id and key")
+                return false
             -- data.result 0-找不到 1-成功 2-qps超限 3-欠费? 4-其他错误 
-            if data.result == 0 then
+            elseif data.result == 0 then
                 log.error(lib_name,"no location")
                 return false
             elseif data.result == 1 then
-                return true,{lon = data.lon,lat = data.lat}
+                return true,{lng = data.lng,lat = data.lat}
             elseif data.result == 2 then
                 log.error(lib_name,"qps limit")
                 return false
@@ -143,6 +187,7 @@ function airlbs.request(param)
                 return false
             end
         else
+            log.error(lib_name,"net error")
             return false
         end
     end