tracker.lua 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. --- 模块功能:GPS TRACKERE 主逻辑
  2. -- @author openLuat
  3. -- @module default
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2018.03.27
  7. require "net"
  8. require "misc"
  9. require "mqtt"
  10. require "gpsv2"
  11. require "utils"
  12. require "update"
  13. require "funlib"
  14. require "pins"
  15. require"nvm"
  16. module(..., package.seeall)
  17. -- 无网络重启时间,飞行模式启动时间
  18. local rstTim, flyTim = 600000, 300000
  19. -- 解除报警的等待时间秒,GPS打开的起始时间utc秒
  20. local clearTime, startTime = 300, 0
  21. -- 轨迹消息缓冲区
  22. local trackFile = {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}}
  23. -- 传感器数据
  24. local sens = {
  25. vib = false, -- 震动检测
  26. acc = false, -- 开锁检测
  27. act = false, -- 启动检测
  28. chg = false, -- 充电检测
  29. und = false, -- 剪线检测
  30. wup = false, -- 唤醒检测
  31. vcc = 0, -- 电池电压
  32. }
  33. local frameSn = 0
  34. --是否发送报警位置
  35. local isSendAlarm = false
  36. -- 配置文件
  37. local dtu = {
  38. host = "", -- 自定义参数服务器
  39. passon = 0, --透传标志位
  40. plate = 0, --识别码标志位
  41. convert = 0, --hex转换标志位
  42. reg = 0, -- 登陆注册包
  43. param_ver = 0, -- 参数版本
  44. flow = 0, -- 流量监控
  45. fota = 0, -- 远程升级
  46. uartReadTime = 500, -- 串口读超时
  47. netReadTime = 50, -- 网络读超时
  48. pwrmod = "normal",
  49. password = "",
  50. upprot = {}, -- 上行自定义协议
  51. dwprot = {}, -- 下行自定义协议
  52. apn = {nil, nil, nil}, -- 用户自定义APN
  53. cmds = {{}, {}}, -- 自动采集任务参数
  54. pins = {"", "", ""}, -- 用户自定义IO: netled,netready,rstcnf,
  55. conf = {{
  56. -- 数组下标代表通道ID号
  57. "tcp", -- 协议 TCP | UDP | MQTT |
  58. "ping", -- 心跳包内容
  59. "180", -- 心跳包间隔
  60. "116.62.220.88", -- HOST 地址
  61. "21890", -- HOST 端口
  62. 1, -- 通道透传捆绑的串口ID
  63. "", -- 保持为""
  64. "", -- 保持为""
  65. "", -- 自动采集任务间隔
  66. "" -- SSL 标志位 可选"ssl"
  67. }, {}, {}, {}, {}, {}, {}}, -- 用户通道参数
  68. preset = {number = "", delay = 1, smsword = "SMS_UPDATE"}, -- 用户预定义的来电电话,延时时间,短信关键字
  69. uconf = {
  70. {1, 115200, 8, uart.PAR_NONE, uart.STOP_1},
  71. {2, 115200, 8, uart.PAR_NONE, uart.STOP_1},
  72. {3, 115200, 8, uart.PAR_NONE, uart.STOP_1},
  73. }, -- 串口配置表
  74. gps = {
  75. fun = {"1", "9600", "0", "1", "1", "json", "0", ";", "60"}, -- 用户捆绑GPS的串口,波特率,功耗模式,采集间隔,采集方式支持触发和持续, 报文数据格式支持 json 和 hex,缓冲条数,分隔符,状态报文间隔
  76. pio = {"", "", "", "", "0", "16"}, -- 配置GPS用到的IO: led脚,vib震动输入脚,ACC输入脚,内置电池充电状态监视脚,adc通道,分压比
  77. },
  78. warn = {
  79. gpio = {},
  80. adc0 = {},
  81. adc1 = {},
  82. vbatt = {}
  83. },
  84. task = {}, -- 用户自定义任务列表
  85. }
  86. --停止时间阀值
  87. local stopTimeThreshold = 10
  88. --定位上报间隔
  89. local locateReportInterval = 30
  90. ----------------------------------------------------------传感器部分----------------------------------------------------------
  91. -- 配置GPS用到的IO: led脚,vib震动输入脚,ACC输入脚,内置电池充电状态监视脚,adc通道,分压比
  92. function sensMonitor(ledio, vibio, accio, chgio, adcid, ratio)
  93. -- 点火监测采样队列
  94. local powerVolt, adcQue, acc, chg = 0, {0, 0, 0, 0, 0}
  95. -- GPS 定位成功指示灯
  96. if ledio and default.pios[ledio] then
  97. default.pios[ledio] = nil
  98. local led = pins.setup(tonumber(ledio:sub(4, -1)), 0)
  99. sys.subscribe("GPS_MSG_REPORT", led)
  100. end
  101. -- 震动传感器检测
  102. if vibio and default.pios[vibio] then
  103. pins.setup(tonumber(vibio:sub(4, -1)), function(msg) if msg == cpu.INT_GPIO_NEGEDGE then sens.vib = true end end, pio.PULLUP)
  104. default.pios[vibio] = nil
  105. end
  106. -- ACC开锁检测
  107. if accio and default.pios[accio] then
  108. acc = pins.setup(tonumber(accio:sub(4, -1)), nil, pio.PULLUP)
  109. default.pios[accio] = nil
  110. end
  111. -- 内置锂电池充电状态监控脚
  112. if chgio and default.pios[chgio] then
  113. chg = pins.setup(tonumber(chgio:sub(4, -1)), nil, pio.PULLUP)
  114. default.pios[chgio] = nil
  115. end
  116. adc.open(tonumber(adcid) or 0)
  117. while true do
  118. local adcValue, voltValue = adc.read(tonumber(adcid) or 0)
  119. if adcValue ~= 0xFFFF or voltValue ~= 0xFFFF then
  120. voltValue = voltValue * (tonumber(ratio)) / 3
  121. -- 点火检测部分
  122. powerVolt = (adcQue[1] + adcQue[2] + adcQue[3] + adcQue[4] + adcQue[5]) / 5
  123. table.remove(adcQue, 1)
  124. table.insert(adcQue, voltValue)
  125. if voltValue + 1500 < powerVolt or voltValue - 1500 > powerVolt then
  126. sens.act = true
  127. else
  128. sens.act = false
  129. end
  130. end
  131. sens.acc, sens.chg = acc and acc() == 0, chg and chg() == 0
  132. sens.vcc, sens.und = voltValue, voltValue < 4000
  133. sys.wait(1000)
  134. sens.vib = false
  135. end
  136. adc.close(tonumber(adcid) or 0)
  137. end
  138. ----------------------------------------------------------设备逻辑任务----------------------------------------------------------
  139. -- 上报设备状态,这里是用户自定义上报报文的顺序的
  140. -- sta = {"isopen", "vib", "acc", "act", "chg", "und", "volt", "vbat", "csq"}
  141. function deviceMessage(format)
  142. if format:lower() ~= "hex" then
  143. return json.encode({
  144. sta = {gpsv2.isOpen(), sens.vib, sens.acc, sens.act, sens.chg, sens.und, sens.vcc, misc.getVbatt(), net.getRssi()}
  145. })
  146. else
  147. return pack.pack(">b7IHb", 0x55, gpsv2.isOpen() and 1 or 0, sens.vib and 1 or 0,
  148. sens.acc and 1 or 0, sens.act and 1 or 0, sens.chg and 1 or 0, sens.und and 1 or 0, sens.vcc, misc.getVbatt(), net.getRssi())
  149. end
  150. end
  151. -- 上传定位信息
  152. -- [是否有效,经度,纬度,海拔,方位角,速度,载噪比,定位卫星,时间戳]
  153. -- 用户自定义上报GPS数据的报文顺序
  154. -- msg = {"isfix", "stamp", "lng", "lat", "altitude", "azimuth", "speed", "sateCno", "sateCnt"},
  155. function locateMessage(format)
  156. local isFix = gpsv2.isFix() and 1 or 0
  157. local lng, lat = gpsv2.getDegLocation()
  158. local altitude = gpsv2.getAltitude()
  159. local azimuth = gpsv2.getAzimuth()
  160. local speed = gpsv2.getSpeed()
  161. local sateCnt = gpsv2.getUsedSateCnt()
  162. local sateCno = gpsv2.getCno()
  163. table.sort(sateCno)
  164. sateCno = table.remove(sateCno) or 0
  165. if format:lower() ~= "hex" then
  166. local msg = {["isFix"] = isFix, ["time"] = os.time(), ["lng"] = lng*60*30000, ["lat"] = lat*60*30000,
  167. ["altitude"] = altitude, ["azimuth"] = azimuth, ["speed"] = speed, ["sateCno"] = sateCno, ["sateCnt"] = sateCnt}
  168. log.info("tracker.locateMessage", "------->locate data:", json.encode(msg))
  169. return msg
  170. else
  171. return pack.pack(">b2i3H2b3", 0xAA, isFix and 1 or 0, os.time(), lng, lat, altitude, azimuth, speed, sateCno, sateCnt)
  172. end
  173. end
  174. -- 用户捆绑GPS的串口,波特率,功耗模式,采集间隔,采集方式支持触发和持续, 报文数据格式支持 json 和 hex,缓冲条数,数据分隔符(不包含,),状态报文间隔分钟
  175. function alert(uid, baud, pwmode, sleep, guard, format, num, sep, interval, cid)
  176. while true do
  177. if sys.waitUntil("NTP_SUCCEED", 3600*1000) then
  178. log.info("tracker.alert", "------->ntp succeed, unpack(dtu.gps.fun):", unpack(dtu.gps.fun))
  179. break
  180. end
  181. end
  182. uid, baud, pwmode, sleep, num = tonumber(uid), tonumber(baud), tonumber(pwmode), tonumber(sleep), tonumber(num) or 0
  183. guard, interval = tonumber(guard) == 0, (tonumber(interval) or 0) * 60000
  184. local cnt, report = 0, function(format)
  185. log.info("tracker.alert","---------> 0")
  186. sys.publish("NET_SENT_RDY_" .. (tonumber(cid) or uid), deviceMessage(format))
  187. end
  188. --log.info("tracker.alert","------------>", uid, baud, pwmode, sleep, num)
  189. while true do
  190. -- 布防判断
  191. --if not gpsv2.isOpen() and (not guard or sens.vib or sens.acc or sens.act or sens.und or sens.wup) then
  192. if not gpsv2.isOpen() then
  193. --sens.wup = false
  194. startTime = os.time()
  195. -- GPS TRACKER 模式
  196. gpsv2.open(uid, baud, pwmode, sleep)
  197. -- 布防上报
  198. --report(format)
  199. --if interval ~= 0 then sys.timerLoopStart(report, interval, format) end
  200. end
  201. while gpsv2.isOpen() do
  202. --[[
  203. -- 撤防判断
  204. if os.difftime(os.time(), startTime) > clearTime then
  205. if guard and sens.vib and sens.acc and sens.act and sens.und and gpsv2.getSpeed() == 0 then
  206. sys.timerStopAll(report)
  207. gpsv2.close(uid)
  208. else
  209. startTime = os.time()
  210. end
  211. end
  212. ]]
  213. -- 上报消息
  214. if sys.waitUntil("GPS_MSG_REPORT") then
  215. if num == 0 then
  216. sys.publish("NET_SENT_RDY_" .. (tonumber(cid) or uid), locateMessage(format))
  217. else
  218. cnt = cnt < num and cnt + 1 or 0
  219. table.insert(trackFile, locateMessage(format))
  220. if cnt == 0 then sys.publish("NET_SENT_RDY_" .. (tonumber(cid) or uid), table.concat(trackFile, sep)) end
  221. end
  222. end
  223. sys.wait(100)
  224. end
  225. sys.wait(100)
  226. end
  227. end
  228. --传感器监测任务
  229. --sys.taskInit(sensMonitor, unpack(dtu.gps.pio))
  230. --GPS串口任务
  231. sys.taskInit(alert, unpack(dtu.gps.fun))
  232. ---------------------------------------------------------- DTU的网络任务部分 ----------------------------------------------------------
  233. --生成请求报文
  234. local function createReqData(protoType)
  235. if tonumber(protoType) == 0x01 then
  236. --登录包
  237. frameSn = 1
  238. imei = misc.getImei()
  239. if not imei then
  240. log.error("tracker.loginMsg","getImei failed!")
  241. return false
  242. end
  243. local binData = funlib.hex2bin("676701000b"..string.format("%04x",frameSn)..string.format("%016s",imei).."00")
  244. return binData
  245. elseif tonumber(protoType) == 0x03 then
  246. --心跳包
  247. frameSn = (frameSn + 1 > 65535) and 1 or (frameSn + 1)
  248. local status = gpsv2.isFix() and 1 or 0
  249. local binData = funlib.hex2bin("6767030004"..string.format("%04x",frameSn)..string.format("%04x",status))
  250. return binData
  251. elseif tonumber(protoType) == 0x84 then
  252. --告警包
  253. local data = locateMessage("json")
  254. frameSn = (frameSn + 1 > 65535) and 1 or (frameSn + 1)
  255. binData = funlib.hex2bin("676784001F"..string.format("%04x",frameSn)..string.format("%08x",data.time)..string.format("%08x",data.lat)
  256. ..string.format("%08x",data.lng)..string.format("%02x",data.speed)..string.format("%04x",data.azimuth).."00000000000000000000"
  257. ..string.format("%02x",data.isFix).."010000")
  258. return binData
  259. else
  260. return false
  261. end
  262. end
  263. --解析响应报文
  264. local function parseRespData(packet)
  265. if not packet then
  266. log.error("tracker.parseRespData", "packet not existed!")
  267. return false
  268. end
  269. if #packet < 7 then
  270. log.error("tracker.parseRespData", "packet length < 7!")
  271. return false
  272. end
  273. local parse = {}
  274. local header = packet:sub(1,2):toHex()
  275. if header ~= "6767" then
  276. log.error("tracker.parseRespData", "packet header invalid! packet:", packet:toHex())
  277. return false
  278. end
  279. parse.heaser = header
  280. local protoType = packet:sub(3,3):toHex()
  281. parse.protoType = protoType
  282. local packetLen = tonumber(packet:sub(4,5):toHex(),16)
  283. if packetLen + 5 ~= #packet then
  284. log.error("tracker.parseRespData", "packet length error! packet:", packet:toHex())
  285. return false
  286. end
  287. parse.packetLen = packetLen
  288. --登录响应包
  289. if protoType == "01" then
  290. local respSn = tonumber(packet:sub(6,7):toHex(),16)
  291. parse.respSn = respSn
  292. return true,parse
  293. end
  294. return false
  295. end
  296. ---------------------------------------------------------- SOKCET 服务 ----------------------------------------------------------
  297. local function tcpTask(cid, pios, reg, convert, passon, upprot, dwprot, prot, ping, timeout, addr, port, uid, gap, report, intervalTime, ssl)
  298. cid, prot, timeout, uid = tonumber(cid) or 1, prot:upper(), tonumber(timeout) or 120, tonumber(uid) or 1
  299. if not ping or ping == "" then ping = "0x00" end
  300. local login = false
  301. local heartbeatTimerId = false
  302. while true do
  303. local idx = 0
  304. if not socket.isReady() and not sys.waitUntil("IP_READY_IND", rstTim) then sys.restart("网络初始化失败!") end
  305. local c = prot == "TCP" and socket.tcp(ssl and ssl:lower() == "ssl") or socket.udp()
  306. if c:connect(addr, port) then
  307. -- 登陆报文
  308. datalink = true
  309. local retryCount = 0
  310. while not login do
  311. local loginData = createReqData(0x01)
  312. if loginData then
  313. if not c:send(loginData) then
  314. retryCount = retryCount + 1
  315. log.error("tracker.tcpTask","send loginData failed! loginData:", loginData:toHex(), "retryCount:", retryCount)
  316. else
  317. local result, data = c:recv(2 * 1000)
  318. if not result then
  319. retryCount = retryCount + 1
  320. log.error("tracker.tcpTask","wait login response timeout! loginData:", loginData:toHex(), "retryCount:", retryCount)
  321. else
  322. local reqSn = tonumber(loginData:sub(6,7):toHex(),16)
  323. local res,parseData = parseRespData(data)
  324. if res and parseData.respSn == reqSn then
  325. login = true
  326. retryCount = 0
  327. break
  328. else
  329. retryCount = retryCount + 1
  330. log.error("tracker.tcpTask","parseRespData failed! respData:", data:toHex(), "retryCount:", retryCount)
  331. end
  332. end
  333. end
  334. else
  335. log.error("tracker.tcpTask","loginMsg failed!")
  336. break
  337. end
  338. if retryCount > 3 then
  339. retryCount = 0
  340. break
  341. end
  342. sys.wait(10*1000)
  343. end
  344. --上报定位/心跳数据报文
  345. while login do
  346. local result, data, param = c:recv(timeout * 1000, "GPS_SENT_RDY_" .. (cid or uid))
  347. --log.info("tracker.tcpTask", "result, data, param", result, data, param and param:toHex())
  348. if result then
  349. if #data>=7 and data:sub(1, 3):toHex() == "676703" then
  350. log.info("tracker.tcpTask","recv heartbeat resp data, data:", data:toHex())
  351. elseif #data>=7 and data:sub(1, 3):toHex() == "676782" then
  352. log.info("tracker.tcpTask","recv locate resp data, data:", data:toHex())
  353. elseif #data>=7 and data:sub(1, 3):toHex() == "676784" then
  354. log.info("tracker.tcpTask","recv alarm resp data, data:", data:toHex())
  355. end
  356. elseif data == "timeout" then
  357. local heartbeatData = createReqData(0x03)
  358. if heartbeatData then
  359. if not c:send(heartbeatData) then
  360. log.error("tracker.tcpTask","send heartbeatData failed! heartbeatData:", heartbeatData:toHex())
  361. break
  362. else
  363. log.info("tracker.tcpTask","send heartbeatData success, heartbeatData:", heartbeatData:toHex())
  364. end
  365. else
  366. log.error("tracker.tcpTask","create heartbeatData failed!")
  367. break
  368. end
  369. elseif data == ("GPS_SENT_RDY_" .. (cid or uid)) then
  370. if not c:send(param) then
  371. log.error("tracker.tcpTask","send locate data failed! locateData:", param:toHex())
  372. break
  373. else
  374. log.info("tracker.tcpTask","send locate data success, locateData:", param:toHex())
  375. end
  376. else
  377. log.error("tracker.tcpTask","recv data failed!")
  378. break
  379. end
  380. end
  381. end
  382. c:close()
  383. log.error("tracker.tcpTask","datalink failed!")
  384. datalink = false
  385. login = false
  386. sys.wait((2 ^ idx) * 1000)
  387. idx = idx > 9 and 0 or idx + 1
  388. end
  389. end
  390. --定位数据预处理
  391. local function locateDataFilterTask( cid )
  392. cid = tonumber(cid) or 1
  393. local startTime = os.time()
  394. local locateData = ""
  395. local points = {}
  396. local stopStartTime = 0
  397. local isStopStart = false
  398. local isTurning = false
  399. while true do
  400. local res,data = sys.waitUntil("NET_SENT_RDY_" .. cid, 60*1000)
  401. if res then
  402. --静止超过时间阀值,停止上报位置
  403. if data.speed == 0 and not isStopStart then
  404. isStopStart = true
  405. stopStartTime = os.time()
  406. elseif data.speed == 0 and isStopStart and stopStartTime>0 and os.time()-stopStartTime > stopTimeThreshold then
  407. log.info("tracker.locateDataFilterTask","stop > 10s,stop gps send!")
  408. elseif data.speed > 0 then
  409. --缓存点位
  410. if #points == 0 then
  411. table.insert(points,data)
  412. else
  413. if #points >= 3 then
  414. table.remove(points,1)
  415. end
  416. table.insert(points,data)
  417. --过滤过期的点
  418. local tmp_points = {}
  419. for i=1,#points do
  420. if data.time - points[i].time <= 4 then
  421. table.insert(tmp_points,points[i])
  422. end
  423. end
  424. points = tmp_points
  425. --达到三个点时,进行拐点检测
  426. if #points>=3 then
  427. if math.abs(points[2].azimuth-points[1].azimuth) >= 10 and math.abs(points[3].azimuth-points[2].azimuth) >= 10 then
  428. isTurning = true
  429. else
  430. isTurning = false
  431. end
  432. else
  433. isTurning = false
  434. end
  435. end
  436. isStopStart = false
  437. stopStartTime = 0
  438. --拐弯时,收到定位数据就上报
  439. if isTurning then
  440. log.info("tracker.locateDataFilterTask","device is turning, start fast report locate!")
  441. frameSn = (frameSn + 1 > 65535) and 1 or (frameSn + 1)
  442. locateData = funlib.hex2bin("676782001C"..string.format("%04x",frameSn)..string.format("%08x",data.time)..string.format("%08x",data.lat)
  443. ..string.format("%08x",data.lng)..string.format("%02x",data.speed)..string.format("%04x",data.azimuth).."00000000000000000000"..string.format("%02x",data.isFix))
  444. sys.publish("GPS_SENT_RDY_"..cid, locateData)
  445. locateData = ""
  446. startTime = os.time()
  447. else
  448. --如果达到间隔时间上报定位包
  449. if os.time()-startTime >= locateReportInterval then
  450. log.info("tracker.locateDataFilterTask","report interval locate data")
  451. frameSn = (frameSn + 1 > 65535) and 1 or (frameSn + 1)
  452. locateData = funlib.hex2bin("676782001C"..string.format("%04x",frameSn)..string.format("%08x",data.time)..string.format("%08x",data.lat)
  453. ..string.format("%08x",data.lng)..string.format("%02x",data.speed)..string.format("%04x",data.azimuth).."00000000000000000000"..string.format("%02x",data.isFix))
  454. sys.publish("GPS_SENT_RDY_"..cid, locateData)
  455. locateData = ""
  456. startTime = os.time()
  457. else
  458. locateData = funlib.hex2bin("676702001C"..string.format("%04x",frameSn)..string.format("%08x",data.time)..string.format("%08x",data.lat)
  459. ..string.format("%08x",data.lng)..string.format("%02x",data.speed)..string.format("%04x",data.azimuth).."00000000000000000000"..string.format("%02x",data.isFix))
  460. end
  461. end
  462. else
  463. log.warn("tracker.locateDataFilterTask","stop start ", os.time()-stopStartTime)
  464. end
  465. else
  466. if locateData ~= "" then
  467. log.info("tracker.locateDataFilterTask", "wait ".."NET_SENT_RDY_" .. cid .." timeout, upload locateData")
  468. sys.publish("GPS_SENT_RDY_"..cid, locateData)
  469. locateData = ""
  470. startTime = os.time()
  471. else
  472. log.warn("tracker.locateDataFilterTask", "wait NET_SENT_RDY_" .. cid .." timeout!" )
  473. end
  474. end
  475. end
  476. end
  477. ---------------------------------------------------------- 参数配置,任务转发,线程守护主进程----------------------------------------------------------
  478. function connect(pios, conf, reg, convert, passon, upprot, dwprot)
  479. local flyTag = false
  480. if not socket.isReady() and not sys.waitUntil("IP_READY_IND", rstTim) then sys.restart("网络初始化失败!") end
  481. --sys.waitUntil("DTU_PARAM_READY", 120000)
  482. -- 自动创建透传任务并填入参数
  483. for k, v in pairs(conf or {}) do
  484. -- log.info("Task parameter information:", k, pios, reg, convert, passon, upprot, dwprot, unpack(v))
  485. if v[1] and (v[1]:upper() == "TCP" or v[1]:upper() == "UDP") then
  486. log.warn("----------------------- TCP/UDP is start! --------------------------------------")
  487. sys.taskInit(tcpTask, k, pios, reg, convert, passon, upprot, dwprot, unpack(v))
  488. sys.taskInit(locateDataFilterTask, k)
  489. end
  490. end
  491. -- 守护进程
  492. datalink = true
  493. while true do
  494. -- 这里是网络正常,但是链接服务器失败重启
  495. if datalink then sys.timerStart(sys.restart, rstTim, "Server connection failed") end
  496. sys.wait(1000)
  497. end
  498. end
  499. sys.taskInit(connect, pios, dtu.conf, dtu.reg, tonumber(dtu.convert) or 0, (tonumber(dtu.passon) == 0), dtu.upprot, dtu.dwprot)
  500. --gpio中断
  501. function gpio13IntFnc(msg)
  502. log.info("tracker.gpio13IntFnc",msg,getGpio13Fnc())
  503. --上升沿中断
  504. if msg==cpu.INT_GPIO_POSEDGE then
  505. log.info("tracker.gpio13IntFnc","up edge")
  506. locateData = createReqData(0x84)
  507. sys.publish("GPS_SENT_RDY_1", locateData)
  508. --下降沿中断
  509. else
  510. log.info("tracker.gpio13IntFnc","down edge")
  511. end
  512. end
  513. --GPIO16配置为中断,可通过getGpio16Fnc()获取输入电平,产生中断时,自动执行gpio16IntFnc函数
  514. getGpio13Fnc = pins.setup(pio.P0_13,gpio13IntFnc)
  515. -- NTP同步后清零一次startTime,避免第一次开机的时候utc时间跳变
  516. sys.subscribe("NTP_SUCCEED", function()
  517. log.info("tracker.subscribe.NTP_SUCCEED","recv NTP_SUCCEED")
  518. startTime = os.time()
  519. end)
  520. -- 订阅服务器远程唤醒指令
  521. --sys.subscribe("REMOTE_WAKEUP", function()sens.wup = true end)