iot.lua 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. --- 孢子仪mqtt联网模块
  2. -- @module
  3. -- @author lcg
  4. -- @release 2019.08.01
  5. require 'sys'
  6. require 'hmi'
  7. require 'audio'
  8. require "mqtt"
  9. require "led"
  10. require "utils"
  11. require "lbsLoc"
  12. require "update"
  13. require "control"
  14. require "sim"
  15. require "config"
  16. require "nvm"
  17. require "gpsv2"
  18. module(..., package.seeall)
  19. -- 指示设备状态标志位:开机重启、掉线重连
  20. dev_sta = 0
  21. --
  22. local mqtt_status = "OK"
  23. local mqtt_num = 0
  24. -- 本地存储文件
  25. local DEVICE_CONF = "/device.conf"
  26. local HOST_CONF = "/host.conf"
  27. local SERVER_CONF = "/server.conf"
  28. local PARAMCONF = "/paramconf.conf"
  29. local NETCONF = "/netconf.conf"
  30. local TAKEPHOTO = "/photo.conf"
  31. local GPS_CONF = "/gps.conf"
  32. --定时拍照开关,0-开,1-关
  33. local photo_sw = 0
  34. -- 上报json数据的基础表
  35. local message = {cmd = "", ext = {}}
  36. -- 上报遗言的json表
  37. local willmsg = {cmd = 'offline', ext = {imei = ''}}
  38. -- 服务器上报数据间隔时长,间隔期间用于阻塞读取服务器下发的指令
  39. -- hmi.hmiconf.datt 和下面的timeout 功能一样
  40. -- local timeout = 10 * 60 * 1000
  41. -- MQTT服务器配置表
  42. serverconf = {
  43. ip = "120.27.222.26",-- 云飞
  44. -- ip = "39.104.94.153",-- 云飞
  45. port = 1883,
  46. sub = "/yfkj/bzy/s2c/",
  47. pub = "/yfkj/bzy/c2s/",
  48. keepalive = 60,
  49. lastwill = "/yfkj/bzy/offline/",
  50. uid = "",
  51. pwd = "",
  52. qos = 0,
  53. }
  54. -- HTTP服务器配置表
  55. hostconf = {
  56. url = "http://120.27.222.26/update",-- 云飞
  57. length = 0,
  58. username = "",
  59. password = "",
  60. }
  61. -- 经纬度数据存放变量
  62. local loc_lat = 0
  63. local loc_lng = 0
  64. --定位状态
  65. local gps_status = 0
  66. --GPS定位数据变量
  67. local gps_loc_lat = 0
  68. local gps_loc_lng = 0
  69. -- 上传的数据格式:
  70. -- {
  71. -- "cmd": "status",
  72. -- "ext": {
  73. -- "imei": "866262042555598",
  74. -- "iccid": "89860411101871387773",
  75. -- "csq": 23,
  76. -- "alti": 0,
  77. -- "lat": "",
  78. -- "lng": "",
  79. -- "dtype": 7,
  80. -- "on_off": 0,
  81. -- "dver": "1.0.0",
  82. -- "v_bat": "25.3635",
  83. -- "bat_sta": 0,
  84. -- "usb_sta": 0,
  85. -- "imgres": 0,
  86. -- "wind_sw": 0,
  87. -- "cold_sw": 0,
  88. -- "coll_time": ["9-10", "10-10", "10-10"],
  89. -- "drop_time": 4,
  90. -- "set_temp": 32,
  91. -- "pre_temp": 25,
  92. -- "at": 0,
  93. -- "ah": 0,
  94. -- "rps": 0,
  95. -- "stamp": "20190802233308",
  96. -- "datt": 10,
  97. -- "staytime": 0,
  98. -- "cul_time": 0
  99. -- }
  100. -- }
  101. function ToStringEx(value)
  102. if type(value)=='table' then
  103. return TableToStr(value)
  104. elseif type(value)=='string' then
  105. return "\'"..value.."\'"
  106. else
  107. return tostring(value)
  108. end
  109. end
  110. function TableToStr(t)
  111. if t == nil then return "" end
  112. local retstr= "{"
  113. local i = 1
  114. for key,value in pairs(t) do
  115. local signal = ","
  116. if i==1 then
  117. signal = ""
  118. end
  119. if key == i then
  120. retstr = retstr..signal..ToStringEx(value)
  121. else
  122. if type(key)=='number' or type(key) == 'string' then
  123. retstr = retstr..signal..'['..ToStringEx(key).."]="..ToStringEx(value)
  124. else
  125. if type(key)=='userdata' then
  126. retstr = retstr..signal.."*s"..TableToStr(getmetatable(key)).."*e".."="..ToStringEx(value)
  127. else
  128. retstr = retstr..signal..key.."="..ToStringEx(value)
  129. end
  130. end
  131. end
  132. i = i+1
  133. end
  134. retstr = retstr.."}"
  135. return retstr
  136. end
  137. --手动定位,0-手动定位,1-GPS定位
  138. manual_location = { lat = 0, lng = 0, type = 0}
  139. --定位回调
  140. function getLocCb(result, lat, lng)
  141. if result == 0 then
  142. log.info("get location success!!!!!!!!!!!!!!!!!!!!!!!!!!!")
  143. log.warn("testLbsLoc.getLocCb*****************", result, lat, lng)
  144. loc_lat = lat
  145. loc_lng = lng
  146. gps_status = 2
  147. log.warn("GPS_json>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", loc_lat)
  148. else
  149. loc_lat = 0
  150. loc_lng = 0
  151. gps_status = -1
  152. log.info("get location failed!!!!!!!!!!!!!!!!!!!!!!!!!!!")
  153. end
  154. end
  155. --- 上传状态信息
  156. function upStatus()
  157. local c, dat
  158. -- 设备上报状态表
  159. local status = {
  160. work_sta = 0,-- 工作状态:0待机;1收集;2培养;3拍照;
  161. imei = "", -- 唯一ID
  162. iccid = "",-- SIM卡的iccid号
  163. csq = 0, -- 信号强度
  164. alti = 0,-- 海拔高度
  165. lat = "34.000000",-- 纬度
  166. lng = "113.00000",-- 经度
  167. dtype = 7, -- 设备类型
  168. on_off = 1,-- 设备开关
  169. dver = "2.0.0", -- 设备固件版本
  170. proj = "", -- 设备固件版本
  171. v_bat = 0,-- 电压
  172. bat_sta = 0,-- 电压状态
  173. usb_sta = 0,-- 相机状态
  174. imgres = 0,-- 分辨率
  175. wind_sw = 1,-- 风机开关 0 关闭, 1 开启
  176. cold_sw = 1, -- 制冷机开关 0 关闭, 1 开启
  177. coll_time = '["9-10", "14-15"]',--采集开启和关闭时间 最多有10个
  178. drop_time = 2,-- 滴液次数
  179. set_temp = 25, -- 保温仓设定温度
  180. pre_temp = 30,-- 保温仓当前温度
  181. at = 25,-- 环境温度
  182. ah = 40,-- 环境湿度
  183. box_tem,-- 箱内温度
  184. rps = 0, -- 雨控状态 1雨控,0正常,
  185. stamp = "20190801211805",
  186. datt = 10,-- 数据上传时间间隔
  187. staytime = 0,-- 已培养时间
  188. cul_time = 0,-- 设定培养时间
  189. }
  190. audio.play(0,"FILE","/ldata/dataupload.mp3", hmi.hmiconf.vol)
  191. status.work_sta = hmi.hmista.work_sta
  192. status.imei = misc.getImei()-- 唯一ID
  193. status.iccid = string.upper(sim.getIccid())-- SIM卡的iccid
  194. status.csq = net.getRssi()-- 信号强度
  195. status.alti = 0-- 海拔
  196. status.gps = gps_status
  197. if gps_status == 0 and manual_location.type == 0 then
  198. status.lat = manual_location.lat
  199. status.lng = manual_location.lng
  200. elseif gps_status == 1 and manual_location.type == 1 then
  201. status.lat = manual_location.lat
  202. status.lng = manual_location.lng
  203. elseif gps_status == 2 then
  204. status.lat = loc_lat
  205. status.lng = loc_lng
  206. end
  207. status.dtype = 7 -- 设备类型
  208. status.on_off = hmi.hmiconf.on_off
  209. status.dver = VERSION -- 设备固件版本
  210. status.proj = PROJECT -- 设备固件版本
  211. -- status.v_bat = hmi.hmista.vbat -- 电池电压
  212. status.v_bat = string.gsub(hmi.hmista.vbat, '"', '') -- 电池电压
  213. status.bat_sta = hmi.hmista.bat_sta -- 电池状态
  214. status.usb_sta = hmi.hmista.usb_sta -- 相机状态
  215. status.imgres = hmi.hmista.imgres -- 分辨率
  216. status.wind_sw = hmi.hmista.wind_sw -- 抽风风机开关
  217. status.cold_sw = hmi.hmiconf.cold_sw -- 制冷开关
  218. status.coll_time = hmi.hmiconf.coll_time -- 收集时间段
  219. status.drop_time = hmi.hmiconf.drop_time -- 滴液次数
  220. status.set_temp = hmi.hmiconf.set_temp -- 设定的保温温度
  221. status.pre_temp = hmi.hmista.pre_temp -- 当前保温仓温度
  222. status.at = (hmi.hmista.envTem + 5) / 10 -- 环境温度
  223. status.ah = (hmi.hmista.envHum + 5) / 10 -- 环境湿度
  224. status.box_tem = string.gsub(hmi.hmista.box_tem, '"', '') -- 箱内温度
  225. status.rps = hmi.hmista.rainStatus -- 雨控状态
  226. c = misc.getClock()
  227. status.stamp = string.format('%04d%02d%02d%02d%02d%02d', c.year, c.month, c.day, c.hour, c.min, c.sec)
  228. status.datt = hmi.hmiconf.datt -- 上仓门状态 1打开 0关闭
  229. status.staytime = hmi.hmista.staytime -- 已经培养的时间
  230. status.cul_time = hmi.hmiconf.cul_time -- 加热状态 1加热 0停止
  231. message.cmd = "status"
  232. message.ext = status
  233. dat = json.encode(message)
  234. log.info("upload status:", dat)
  235. return dat
  236. end
  237. --- 上传配置信息
  238. function upNetconf(...)
  239. local c, dat
  240. -- 设备运行上报下发参数表
  241. local netset = {
  242. mqtt = {},
  243. ftp = {
  244. ip = "120.27.222.26",
  245. port = "8088",
  246. uid = "",
  247. pwd = "",
  248. },
  249. }
  250. netset.mqtt = serverconf
  251. audio.play(0,"FILE","/ldata/dataupload.mp3", hmi.hmiconf.vol)
  252. netset.cmd = "netconf"
  253. dat = json.encode(netset)
  254. log.info("upload upNetconf:", dat)
  255. return dat
  256. end
  257. --- 上传配置信息
  258. function upParamConf(...)
  259. local c, dat
  260. -- 设备运行上报下发参数表
  261. local paramconf = {
  262. -- "wind_sw": 0,
  263. -- "coll_time": ["7-9", "10-11"],
  264. -- "set_temp": 25,
  265. -- "imgres": 0,
  266. -- "datt": 0,
  267. -- "drop_time": 0,
  268. -- "cul_time": 24
  269. wind_sw = 0,--风机开关;
  270. cold_sw = 0,--制冷开关;
  271. coll_time = "",
  272. set_temp = 0,
  273. imgres = 0,
  274. datt = 0,
  275. drop_time = 0,
  276. cul_time = 0,
  277. }
  278. audio.play(0,"FILE","/ldata/dataupload.mp3", hmi.hmiconf.vol)
  279. paramconf.wind_sw = hmi.hmiconf.wind_sw--风机开关;
  280. paramconf.cold_sw = hmi.hmiconf.cold_sw -- 制冷开关
  281. paramconf.coll_time = hmi.hmiconf.coll_time
  282. paramconf.set_temp = hmi.hmiconf.set_temp
  283. paramconf.imgres = hmi.hmiconf.imgres
  284. paramconf.datt = hmi.hmiconf.datt
  285. paramconf.drop_time = hmi.hmiconf.drop_time
  286. paramconf.cul_time = hmi.hmiconf.cul_time
  287. message.cmd = "paramconf"
  288. message.ext = paramconf
  289. dat = json.encode(message)
  290. log.info("upload paramconf:", dat)
  291. return dat
  292. end
  293. function setNetconf(dat)
  294. log.warn("--------setNetconf is running!!!")
  295. local cmds, result, err = json.decode(dat)
  296. if not result then log.error("setNetconf error!") end
  297. local str = json.encode(cmds)
  298. log.warn("set netconf is:-----------------:",str)
  299. io.writeFile(NETCONF, str)
  300. audio.play(0,"FILE","/ldata/save.mp3", hmi.hmiconf.vol)
  301. end
  302. function ToStringEx(value)
  303. if type(value)=='table' then
  304. return TableToStr(value)
  305. elseif type(value)=='string' then
  306. return "\'"..value.."\'"
  307. else
  308. return tostring(value)
  309. end
  310. end
  311. function TableToStr(t)
  312. if t == nil then return "" end
  313. local retstr= "{"
  314. local i = 1
  315. for key,value in pairs(t) do
  316. local signal = ","
  317. if i==1 then
  318. signal = ""
  319. end
  320. if key == i then
  321. retstr = retstr..signal..ToStringEx(value)
  322. else
  323. if type(key)=='number' or type(key) == 'string' then
  324. retstr = retstr..signal..'['..ToStringEx(key).."]="..ToStringEx(value)
  325. else
  326. if type(key)=='userdata' then
  327. retstr = retstr..signal.."*s"..TableToStr(getmetatable(key)).."*e".."="..ToStringEx(value)
  328. else
  329. retstr = retstr..signal..key.."="..ToStringEx(value)
  330. end
  331. end
  332. end
  333. i = i+1
  334. end
  335. retstr = retstr.."}"
  336. return retstr
  337. end
  338. function setParamConf(dat)
  339. log.warn("--------setParamConf is running!!!")
  340. log.warn("dat:",dat)
  341. log.warn("type dat:",type(dat))
  342. local cmds, result, err
  343. local status, error = pcall(function ()
  344. cmds, result, err = json.decode(dat)
  345. if not result then log.error("setParamConf error!") end
  346. log.info("----------cmds.ext.wind_sw",cmds.ext.wind_sw)
  347. log.info("==========cmds.ext.coll_time",cmds.ext.coll_time)
  348. log.info("==========cmds.ext.set_temp",cmds.ext.set_temp)
  349. log.info("==========cmds.ext.imgres",cmds.ext.imgres)
  350. log.info("==========cmds.ext.datt",cmds.ext.datt)
  351. log.info("==========cmds.ext.drop_time",cmds.ext.drop_time)
  352. log.info("==========cmds.ext.cul_time",cmds.ext.cul_time)
  353. -- 参数检查:
  354. log.warn("cmds:",cmds)
  355. log.warn("type cmds:",type(cmds))
  356. while true do
  357. -- for k, v in pairs(cmds) do
  358. for k, v in pairs(cmds.ext) do
  359. -- if v == nil then
  360. -- log.error(k .. "is:" .. v)
  361. -- break
  362. -- else
  363. -- log.warn(cmds.ext.k .. "is:" .. v)
  364. -- log.warn(cmds.ext.k .. "is:" .. v)
  365. -- end
  366. -- log.warn("cmds:",k,v)
  367. -- log.warn("cmds.ext:",k,v)
  368. log.warn("k is: " .. k .. " ;" .. " type cmds.ext.v: " ,type(v))
  369. -- type(coll_time) is table!!
  370. -- if k ~= 'coll_time' and type(v) ~= number then
  371. -- error({code=121})
  372. -- end
  373. if type(v) ~= "table" and type(v) ~= "string" and type(v) ~= "number" then
  374. log.error("k is: " .. k .. " ;" .. " type cmds.ext.v: " ,type(v))
  375. error({code=1})
  376. end
  377. -- if type(v) == "table" then
  378. -- log.error("k is: " .. k .. " ;" .. " type cmds.ext.v: " ,type(v))
  379. -- -- local table_son = control.lua_string_split(TableToStr(v), "-")
  380. -- log.info("TableToStr(v):", TableToStr(v))
  381. -- log.info("type TableToStr(v):", type(TableToStr(v)))
  382. -- log.info("string.gsub(TableToStr(v),' []{}'):", string.gsub(TableToStr(v),' {}',''))
  383. -- local table_son = control.lua_string_split(string.gsub(TableToStr(v),' {}',''), "-")
  384. -- -- string.gsub(coll_time_table[i], ' ', '')
  385. -- for i,j in pairs(table_son) do
  386. -- log.error("table_son i is: " .. i .. " ;" .. " table_son j: " ,j .. " ;" .. " type table_son j: " ,type(j))
  387. -- -- table_son[k] = tonumber(table_son[k])
  388. -- end
  389. -- -- error({code=121})
  390. -- end
  391. end
  392. break
  393. end
  394. end)
  395. print("setParamConf status:",status)
  396. print("setParamConf error:",error)
  397. if status == true then
  398. log.warn("-----------status is true")
  399. hmi.hmiconf.wind_sw = cmds.ext.wind_sw
  400. hmi.hmiconf.coll_time = cmds.ext.coll_time
  401. hmi.hmiconf.set_temp = cmds.ext.set_temp
  402. hmi.hmiconf.imgres = cmds.ext.imgres
  403. hmi.hmiconf.datt = cmds.ext.datt
  404. hmi.hmiconf.drop_time = cmds.ext.drop_time
  405. hmi.hmiconf.cul_time = cmds.ext.cul_time
  406. log.info("==========hmi.hmiconf.wind_sw",hmi.hmiconf.wind_sw)
  407. log.info("==========hmi.hmiconf.coll_time",hmi.hmiconf.coll_time)
  408. log.info("==========hmi.hmiconf.set_temp",hmi.hmiconf.set_temp)
  409. log.info("==========hmi.hmiconf.imgres",hmi.hmiconf.imgres)
  410. log.info("==========hmi.hmiconf.datt",hmi.hmiconf.datt)
  411. log.info("==========hmi.hmiconf.drop_time",hmi.hmiconf.drop_time)
  412. log.info("==========hmi.hmiconf.cul_time",hmi.hmiconf.cul_time)
  413. -- local str = '{"cul_time":"12","vol":7,"datt":20,"coll_time":["1-2","11-12","12-12"],"drop_time":"1","cold_sw":1,"passWord":8888,"imgres":0,"thsp":300,"wind_sw":0,"set_temp":25}'
  414. local str = json.encode(hmi.hmiconf)
  415. log.info('will write configfile is:',str)
  416. io.writeFile(hmi.HMI_FILE, str)
  417. -- log.info('set paramconf is:',json.encode(dat))
  418. -- log.info('new paramconf+++:',json.encode(hmi.hmiconf))
  419. audio.play(0,"FILE","/ldata/save.mp3", hmi.hmiconf.vol)
  420. elseif status == false then
  421. log.warn("status is false")
  422. end
  423. end
  424. --- 下发HTTP服务器配置配置信息
  425. function setHostconf(dat)
  426. local cmds, result, err = json.decode(dat)
  427. if not result then log.info("setHostconf set the parameter error!") end
  428. hostconf = cmds.ext
  429. io.writeFile(HOST_CONF, dat)
  430. audio.play(0,"FILE","/ldata/save.mp3", hmi.hmiconf.vol)
  431. end
  432. --- 下发服务器配置信息
  433. function setServerConf(dat)
  434. local cmds, result, err = json.decode(dat)
  435. if not result then log.info("setServerConf set the parameter error!") end
  436. serverconf = cmds.ext
  437. io.writeFile(NETCONF, dat)
  438. audio.play(0,"FILE","/ldata/save.mp3", hmi.hmiconf.vol)
  439. end
  440. --- 开机获取用户配置的参数
  441. function getConfig()
  442. local netconf,loc,cmds, result, err
  443. photo_sw = io.readFile(TAKEPHOTO)
  444. if photo_sw == nil then photo_sw = 0 end
  445. netconf = io.readFile(NETCONF)
  446. log.warn("from NETCONF read netconf:",netconf)
  447. cmds, result, err = json.decode(netconf)
  448. if result then serverconf = cmds.mqtt end
  449. log.warn("from NETCONF read cmds.mqtt:",cmds.mqtt)
  450. loc = io.readFile(GPS_CONF)
  451. cmds,result,err = json.decode(loc)
  452. if result then
  453. manual_location = cmds.ext
  454. end
  455. log.warn("get config:",loc)
  456. end
  457. -- LED指示灯任务
  458. sys.taskInit(function()
  459. pmd.ldoset(7, pmd.LDO_VMMC)
  460. local ledpin = pins.setup(pio.P0_10, 1)
  461. while true do
  462. -- GSM注册中
  463. while not link.isReady() do
  464. led.blinkPwm(ledpin, 500, 500)
  465. sys.wait(100)
  466. end
  467. -- 网络附着中
  468. while datalink == 0 do
  469. led.blinkPwm(ledpin, 1000, 200)
  470. sys.wait(100)
  471. end
  472. -- 服务器已链接
  473. while datalink ~= 0 do
  474. -- 心跳包维持数据链接
  475. if datalink == 1 then
  476. led.blinkPwm(ledpin, 200, 1000)
  477. -- 发送数据中
  478. elseif datalink == 2 then
  479. led.blinkPwm(ledpin, 100, 100)
  480. end
  481. sys.wait(100)
  482. end
  483. sys.wait(1000)
  484. end
  485. end)
  486. sys.taskInit(function()
  487. sys.wait(3000)
  488. nvm.init("config.lua")
  489. -- 判断是否在mqtt1中启动喂狗;如果mqtt1连接云飞,则喂狗;
  490. if serverconf.ip == "120.27.222.26" or string.find(serverconf.ip, "yfzhwlw") ~= nil then
  491. log.error("***************one mqtt task!!!")
  492. else
  493. log.eror("****************two mqtt task!!!")
  494. end
  495. -- 开机即置1
  496. dev_sta = 1
  497. local cnt = hmi.hmiconf.datt * 60
  498. getConfig()
  499. log.info('MQTT111 Task is start!')
  500. while not socket.isReady() do sys.wait(1000) mqtt_num = 0 end
  501. -- 创建MQTT客户端,客户端ID为IMEI号
  502. willmsg.ext.imei = misc.getImei()
  503. local will = {qos = 1, retain = 0, topic = string.format(serverconf.lastwill .. misc.getImei()), payload = json.encode(willmsg)}
  504. local mqttc = mqtt.client(misc.getImei(), serverconf.keepalive, serverconf.uid, serverconf.pwd, 1,will)
  505. local pub = string.format(serverconf.pub .. misc.getImei())
  506. local sub = string.format(serverconf.sub .. misc.getImei())
  507. while true do
  508. while not mqttc:connect(serverconf.ip, serverconf.port) do sys.wait(1000) mqtt_num = 0 end
  509. -- 初始化订阅主题
  510. datalink = 1 -- 数据指示常亮等待数据
  511. if mqttc:subscribe(sub, serverconf.qos) then
  512. if dev_sta == 1 then
  513. -- 开机或重启即上传一次 dev_sta 为 'turnon':
  514. -- {"cmd":"warn","ext":{"status":"turnon","type":"dev_sta"}}
  515. mqttc:publish(pub, upwarn("dev_sta","turnon:" .. rtos.poweron_reason() .. "; mqtt1_disconntct:" .. nvm.get("mqtt1_disconntct")), serverconf.qos)
  516. dev_sta = 0
  517. elseif dev_sta == 2 then
  518. -- 掉线重连 上传一次 dev_sta 为 'relink':
  519. -- {"cmd":"warn","ext":{"status":"relink","type":"dev_sta"}}
  520. mqttc:publish(pub, upwarn("dev_sta","relink"), serverconf.qos)
  521. dev_sta = 0
  522. end
  523. while true do
  524. cnt = cnt + 10
  525. if cnt > tonumber(hmi.hmiconf.datt) * 60 then
  526. -- 推状态送消息
  527. datalink = 2 -- 数据发送指示快速闪烁
  528. if not mqttc:publish(pub, upStatus(), serverconf.qos) then break end
  529. if not mqttc:publish(pub, upParamConf(), serverconf.qos) then break end
  530. log.info('upload is done!')
  531. cnt = 0
  532. end
  533. datalink = 1 -- 数据指示灯常亮等待发送
  534. if update.uptate_ok ~= 0 then
  535. audio.play(0,"FILE","/ldata/alarm.mp3", hmi.hmiconf.vol)
  536. log.warn('======================= update is ok!!!')
  537. if not mqttc:publish(pub, upwarn("bzy_update",update.uptate_ok), serverconf.qos) then break end
  538. update.uptate_ok = 0
  539. end
  540. --haikang.photo_flag = 1
  541. if haikang.photo_flag ~= 0 then
  542. log.warn('======================= take photo is ok!!!')
  543. -- if not mqttc:publish(pub, upwarn("bzy take photo ok!" .. "; photo num:" .. haikang.photo_num .. "; step num:" .. haikang.step,"ok"), serverconf.qos) then break end
  544. -- if not mqttc:publish(pub, upwarn("bzy take photo ok!" .. "; photo num:" .. haikang.photo_num .. "; step num:" .. haikang.step,"ok"), serverconf.qos) then break end
  545. local message = {cmd = "", ext = {}}
  546. local data = {
  547. photo_num = haikang.photo_num,
  548. step_num = haikang.step,
  549. }
  550. message.cmd = "photo"
  551. message.ext = data
  552. log.warn('=======================',json.encode(message))
  553. if not mqttc:publish(pub, json.encode(message), serverconf.qos) then break end
  554. haikang.photo_flag = 0
  555. haikang.step = 0
  556. haikang.photo_num = 0
  557. end
  558. if control.turn_reset_timeout ~= 0 then
  559. if not mqttc:publish(pub, upwarn("turn_reset_timeout:",control.turn_reset_timeout), serverconf.qos) then break end
  560. control.turn_reset_timeout = 0
  561. end
  562. if control.turn_timeout ~= 0 then
  563. if not mqttc:publish(pub, upwarn("turn_timeout:",control.turn_timeout), serverconf.qos) then break end
  564. control.turn_timeout = 0
  565. end
  566. mqtt_status = "OK"
  567. -- 处理服务器的下发数据请求
  568. local r, packet = mqttc:receive(10000)-- 处理服务器下发指令
  569. if r then -- 这里是有数据下发的处理
  570. datalink = 2 -- 数据接收指示灯快闪
  571. local cnf, result, err = json.decode(packet.payload)
  572. if result and cnf.cmd == 'read' then
  573. if cnf.type == 'status' then
  574. if not mqttc:publish(pub, upStatus(), serverconf.qos) then break end
  575. elseif cnf.type == 'netconf' then
  576. if not mqttc:publish(pub, upNetconf(), serverconf.qos) then break end
  577. -- 此处为兼容老设备,下发时cmd为paramconf 则上传时,需要区别一下:故cmd取param
  578. elseif cnf.type == 'param' then
  579. if not mqttc:publish(pub, upParamConf(), serverconf.qos) then break end
  580. end
  581. elseif result and cnf.cmd == 'netset' then
  582. setNetconf(packet.payload)
  583. -- 此处为兼容老设备,下发时cmd为paramconf 上传时,cmd取param
  584. -- elseif result and cnf.cmd == 'paramset' then
  585. elseif result and cnf.cmd == 'paramconf' then
  586. setParamConf(packet.payload)
  587. elseif result and cnf.cmd == 'serverconf' then
  588. setServerConf(packet.payload)
  589. elseif result and cnf.cmd == "reboot" then
  590. if not mqttc:publish(pub, upres("system will restart","ok"), serverconf.qos) then break end
  591. sys.restart("Server remote restart.")
  592. elseif result and cnf.cmd == "update" then
  593. update.run()
  594. if not mqttc:publish(pub, upwarn("bzy update is start","ok"), serverconf.qos) then break end
  595. log.info("Server remote update done!")
  596. elseif result and cnf.cmd == "set_photo_step" then
  597. nvm.set("photo_step", cnf.ext)
  598. if not mqttc:publish(pub, upwarn("set_photo_step",nvm.get("photo_step")), serverconf.qos) then break end
  599. elseif result and cnf.cmd == "get_photo_step" then
  600. if not mqttc:publish(pub, upwarn("get_photo_step",nvm.get("photo_step")), serverconf.qos) then break end
  601. elseif result and cnf.cmd == "poweroff" then
  602. hmi.hmiconf.on_off = 0
  603. local str = json.encode(hmi.hmiconf)
  604. log.info('will write configfile is:',str)
  605. io.writeFile(hmi.HMI_FILE, str)
  606. if not mqttc:publish(pub, upres("poweroff","ok"), serverconf.qos) then break end
  607. if not mqttc:publish(pub, upStatus(), serverconf.qos) then break end
  608. elseif result and cnf.cmd == "poweron" then
  609. hmi.hmiconf.on_off = 1
  610. local str = json.encode(hmi.hmiconf)
  611. log.info('will write configfile is:',str)
  612. io.writeFile(hmi.HMI_FILE, str)
  613. if not mqttc:publish(pub, upres("poweron","ok"), serverconf.qos) then break end
  614. if not mqttc:publish(pub, upStatus(), serverconf.qos) then break end
  615. elseif result and cnf.cmd == "coldoff" then
  616. hmi.hmiconf.cold_sw = 0
  617. local str = json.encode(hmi.hmiconf)
  618. log.info('will write configfile is:',str)
  619. io.writeFile(hmi.HMI_FILE, str)
  620. if not mqttc:publish(pub, upwarn("coldoff","ok"), serverconf.qos) then break end
  621. elseif result and cnf.cmd == "coldon" then
  622. hmi.hmiconf.cold_sw = 1
  623. local str = json.encode(hmi.hmiconf)
  624. log.info('will write configfile is:',str)
  625. io.writeFile(hmi.HMI_FILE, str)
  626. if not mqttc:publish(pub, upwarn("coldon","ok"), serverconf.qos) then break end
  627. elseif result and cnf.cmd == "photo" then
  628. photosw(cnf.ext.ws)
  629. if not mqttc:publish(pub, upres("timer photo status",photo_sw), serverconf.qos) then break end
  630. -- 远程控制:
  631. elseif result and cnf.cmd == "ctrl" then
  632. -- 远程拍照:
  633. if cnf.ext.type == 'takephoto' then
  634. haikang.test_takephoto()
  635. if not mqttc:publish(pub, upwarn("bzy test takephoto cmd send","ok"), yfmqtt.qos) then break end
  636. -- 远程控制对焦拍照:
  637. elseif cnf.ext.type == 'auto_takephoto' then
  638. hmi.remote_auto_pic()
  639. if not mqttc:publish(pub, upwarn("bzy auto takephoto cmd send","ok"), yfmqtt.qos) then break end
  640. -- 转盘操作:
  641. elseif cnf.ext.type == 'turn' then
  642. control.turn()
  643. if not mqttc:publish(pub, upres("turn","ok"), serverconf.qos) then break end
  644. end
  645. end
  646. elseif packet == 'timeout' then -- 服务器下发指令超时处理
  647. datalink = 1 -- 等待数据指示灯常亮
  648. -- 只往云飞平台传数据时,启动喂狗;如果此task为对接到用户的平台,则不喂狗,即保证云飞mqtt任务的连接:即,如果云飞mqtt任务不喂狗,系统会重启;
  649. control.wdg()
  650. log.info('MqttServer111 recv is timeout')
  651. else
  652. log.info('The MqttServer111 connection is broken.')
  653. break
  654. end
  655. datalink = 1 -- 数据指示灯常亮等待发送
  656. end
  657. end
  658. mqttc:disconnect()
  659. -- 发布平台mqtt连接断开!如果对接外部平台,则在云飞mqtt任务中,预警mqtt1的断开!!
  660. sys.publish("MQTT1_DISCONNECT")
  661. -- 断开连接即 设备即将重连网络,即置2,等网络重连成功,则会上报 曾掉线重连网络过。
  662. dev_sta = 2
  663. datalink = 0 -- 服务器断开链接数据指示灯慢闪
  664. sys.wait(1000)
  665. end
  666. end)
  667. --- 上传响应信息
  668. function upres(c_type,c_status)
  669. local dat
  670. -- 设备上报数据表
  671. local data = {
  672. type = c_type,
  673. status = c_status,
  674. }
  675. audio.play(0,"FILE","/ldata/dataupload.mp3", hmi.hmiconf.vol)
  676. message.cmd = "control"
  677. message.ext = data
  678. dat = json.encode(message)
  679. log.info("upload upres:", dat)
  680. return dat
  681. end
  682. --- 上传预警信息
  683. -- t:type
  684. -- s:status
  685. function upwarn(t,s)
  686. -- log.info("------------------------t,:",t)
  687. -- log.info("------------------------s,:",s)
  688. local dat
  689. -- 设备上报数据表
  690. local data = {
  691. type = t,
  692. status = s,
  693. }
  694. message.cmd = "warn"
  695. message.ext = data
  696. dat = json.encode(message)
  697. log.info("upload warn:", dat)
  698. return dat
  699. end
  700. function photo()
  701. local dat
  702. -- 设备上报数据表
  703. local data = {
  704. type = t,
  705. status = s,
  706. }
  707. message.cmd = "warn"
  708. message.ext = data
  709. dat = json.encode(message)
  710. log.info("upload warn:", dat)
  711. return dat
  712. end
  713. ----------------------------云飞mqtt上传任务:----------------------------
  714. -- 云飞MQTT服务器配置表
  715. yfmqtt = {
  716. ip = "120.27.222.26",-- 云飞
  717. port = 1883,
  718. sub = "/yfkj/bzy/s2c/",
  719. pub = "/yfkj/bzy/c2s/",
  720. keepalive = 60,
  721. lastwill = "/yfkj/bzy/offline/",
  722. uid = "",
  723. pwd = "",
  724. qos = 0,
  725. }
  726. -- mqtt1的连接状态:0正常;1异常;
  727. local mqtt1_sta = 0
  728. function mqtt1_disconntct()
  729. nvm.set("mqtt1_disconntct",nvm.get("mqtt1_disconntct")+1)
  730. if nvm.get("mqtt1_disconntct") >= 100000000 then nvm.set("mqtt1_disconntct",0) end
  731. -- 状态置1表示异常;
  732. mqtt1_sta = 1
  733. end
  734. sys.taskInit(function()
  735. sys.wait(3000)
  736. sys.subscribe("MQTT1_DISCONNECT",mqtt1_disconntct)
  737. -- 开机即置1
  738. dev_sta = 1
  739. -- 判断 serverconf
  740. log.info("yunfei mqtt ===================!!!")
  741. if serverconf.ip == "120.27.222.26" then
  742. log.info("serverconf.ip = 120.27.222.26")
  743. else
  744. log.info("serverconf.ip ~= 120.27.222.26")
  745. end
  746. if string.find(serverconf.ip, "yfzhwlw") ~= nil then
  747. log.info("serverconf.ip include yfzhwlw")
  748. else
  749. log.info("serverconf.ip not include yfzhwlw")
  750. end
  751. if serverconf.ip == "120.27.222.26" or string.find(serverconf.ip, "yfzhwlw") ~= nil then
  752. log.info("yunfei mqtt end!!!")
  753. return
  754. else
  755. log.info("yunfei mqtt start running!!!")
  756. end
  757. local cnt = hmi.hmiconf.datt * 60
  758. getConfig()
  759. log.info('MQTT222 Task is start!')
  760. while not socket.isReady() do sys.wait(1000) mqtt_num = 0 end
  761. -- 创建MQTT客户端,客户端ID为IMEI号
  762. willmsg.ext.imei = misc.getImei()
  763. local will = {qos = 1, retain = 0, topic = string.format(yfmqtt.lastwill .. misc.getImei()), payload = json.encode(willmsg)}
  764. local mqttc = mqtt.client(misc.getImei(), yfmqtt.keepalive, yfmqtt.uid, yfmqtt.pwd, 1,will)
  765. local pub = string.format(yfmqtt.pub .. misc.getImei())
  766. local sub = string.format(yfmqtt.sub .. misc.getImei())
  767. while true do
  768. while not mqttc:connect(yfmqtt.ip, yfmqtt.port) do sys.wait(1000) mqtt_num = 0 end
  769. -- 初始化订阅主题
  770. datalink = 1 -- 数据指示常亮等待数据
  771. if mqttc:subscribe(sub, yfmqtt.qos) then
  772. if dev_sta == 1 then
  773. -- 开机或重启即上传一次 dev_sta 为 'turnon':
  774. -- {"cmd":"warn","ext":{"status":"turnon","type":"dev_sta"}}
  775. mqttc:publish(pub, upwarn("dev_sta","turnon:" .. rtos.poweron_reason() .. "mqtt1_disconntct:" .. nvm.get("mqtt1_disconntct")), yfmqtt.qos)
  776. dev_sta = 0
  777. elseif dev_sta == 2 then
  778. -- 掉线重连 上传一次 dev_sta 为 'relink':
  779. -- {"cmd":"warn","ext":{"status":"relink","type":"dev_sta"}}
  780. mqttc:publish(pub, upwarn("dev_sta","relink"), yfmqtt.qos)
  781. dev_sta = 0
  782. end
  783. while true do
  784. cnt = cnt + 10
  785. if cnt > tonumber(hmi.hmiconf.datt) * 60 then
  786. -- 推状态送消息
  787. datalink = 2 -- 数据发送指示快速闪烁
  788. -- audio.play('/ldata/upload.mp3')
  789. if not mqttc:publish(pub, upStatus(), yfmqtt.qos) then break end
  790. -- if not mqttc:publish(pub, upData(), yfmqtt.qos) then break end
  791. log.info('upload is done!')
  792. cnt = 0
  793. end
  794. datalink = 1 -- 数据指示灯常亮等待发送
  795. --------- mqtt1连接异常 ---------1表断开连接;
  796. if mqtt1_sta ~= 0 then
  797. log.warn('======================= mqtt1_sta:',mqtt1_sta)
  798. if not mqttc:publish(pub, upwarn("mqtt1_sta",mqtt1_sta), yfmqtt.qos) then break end
  799. mqtt1_sta = 0
  800. end
  801. if update.uptate_ok ~= 0 then
  802. log.warn('======================= update is ok!!!')
  803. if not mqttc:publish(pub, upwarn("bzy_update",update.uptate_ok), yfmqtt.qos) then break end
  804. update.uptate_ok = 0
  805. end
  806. mqtt_status = 'OK'
  807. -- 处理服务器的下发数据请求
  808. local r, packet = mqttc:receive(10000)-- 处理服务器下发指令
  809. if r then -- 这里是有数据下发的处理
  810. datalink = 2 -- 数据接收指示灯快闪
  811. local cnf, result, err = json.decode(packet.payload)
  812. if result and cnf.cmd == 'read' then
  813. if cnf.type == 'status' then
  814. if not mqttc:publish(pub, upStatus(), yfmqtt.qos) then break end
  815. elseif cnf.type == 'paramconf' then
  816. if not mqttc:publish(pub, upParamConf(), yfmqtt.qos) then break end
  817. elseif cnf.type == 'serverconf' then
  818. if not mqttc:publish(pub, upServerConf(), yfmqtt.qos) then break end
  819. elseif cnf.ext.type == 'hostconf' then
  820. if not mqttc:publish(pub, upHostConf(), yfmqtt.qos) then break end
  821. end
  822. elseif result and cnf.cmd == 'paramconf' then
  823. setParamConf(packet.payload)
  824. elseif result and cnf.cmd == 'hostconf' then
  825. setHostconf(packet.payload)
  826. elseif result and cnf.cmd == 'serverconf' then
  827. setServerConf(packet.payload)
  828. elseif result and cnf.cmd == "set_photo_step" then
  829. nvm.set("photo_step", cnf.ext)
  830. if not mqttc:publish(pub, upwarn("set_photo_step",nvm.get("photo_step")), yfmqtt.qos) then break end
  831. elseif result and cnf.cmd == "get_photo_step" then
  832. if not mqttc:publish(pub, upwarn("get_photo_step",nvm.get("photo_step")), yfmqtt.qos) then break end
  833. elseif result and cnf.cmd == "poweroff" then
  834. hmi.hmiconf.on_off = 0
  835. local str = json.encode(hmi.hmiconf)
  836. log.info('will write configfile is:',str)
  837. io.writeFile(hmi.HMI_FILE, str)
  838. if not mqttc:publish(pub, upres("poweroff","ok"), yfmqtt.qos) then break end
  839. if not mqttc:publish(pub, upStatus(), yfmqtt.qos) then break end
  840. elseif result and cnf.cmd == "poweron" then
  841. hmi.hmiconf.on_off = 1
  842. local str = json.encode(hmi.hmiconf)
  843. log.info('will write configfile is:',str)
  844. io.writeFile(hmi.HMI_FILE, str)
  845. if not mqttc:publish(pub, upres("poweron","ok"), yfmqtt.qos) then break end
  846. if not mqttc:publish(pub, upStatus(), yfmqtt.qos) then break end
  847. elseif result and cnf.cmd == "coldoff" then
  848. hmi.hmiconf.cold_sw = 0
  849. local str = json.encode(hmi.hmiconf)
  850. log.info('will write configfile is:',str)
  851. io.writeFile(hmi.HMI_FILE, str)
  852. if not mqttc:publish(pub, upwarn("coldoff","ok"), yfmqtt.qos) then break end
  853. if not mqttc:publish(pub, upStatus(), yfmqtt.qos) then break end
  854. elseif result and cnf.cmd == "coldon" then
  855. hmi.hmiconf.cold_sw = 1
  856. local str = json.encode(hmi.hmiconf)
  857. log.info('will write configfile is:',str)
  858. io.writeFile(hmi.HMI_FILE, str)
  859. if not mqttc:publish(pub, upwarn("coldon","ok"), yfmqtt.qos) then break end
  860. if not mqttc:publish(pub, upStatus(), yfmqtt.qos) then break end
  861. elseif result and cnf.cmd == "reboot" then
  862. if not mqttc:publish(pub, upres("system will restart","ok"), yfmqtt.qos) then break end
  863. sys.restart("Server remote restart.")
  864. elseif result and cnf.cmd == "update" then
  865. update.run()
  866. if not mqttc:publish(pub, upres("update is start","ok"), yfmqtt.qos) then break end
  867. log.info("Server remote update done!")
  868. elseif result and cnf.cmd == "photo" then
  869. photosw(cnf.ext.ws)
  870. if not mqttc:publish(pub, upres("timer photo status",photo_sw), yfmqtt.qos) then break end
  871. -- 远程控制:
  872. elseif result and cnf.cmd == "ctrl" then
  873. -- 远程拍照:
  874. if cnf.ext.type == 'takephoto' then
  875. haikang.test_takephoto()
  876. if not mqttc:publish(pub, upwarn("bzy test takephoto cmd send","ok"), yfmqtt.qos) then break end
  877. -- 远程控制对焦拍照:
  878. elseif cnf.ext.type == 'auto_takephoto' then
  879. hmi.remote_auto_pic()
  880. if not mqttc:publish(pub, upwarn("bzy auto takephoto cmd send","ok"), yfmqtt.qos) then break end
  881. -- 转盘操作:
  882. elseif cnf.ext.type == 'turn' then
  883. control.turn()
  884. if not mqttc:publish(pub, upres("turn","ok"), yfmqtt.qos) then break end
  885. end
  886. end
  887. elseif packet == 'timeout' then -- 服务器下发指令超时处理
  888. datalink = 1 -- 等待数据指示灯常亮
  889. control.wdg()
  890. log.info('MqttServer222 recv is timeout')
  891. else
  892. log.info('The MqttServer222 connection is broken.')
  893. break
  894. end
  895. datalink = 1 -- 数据指示灯常亮等待发送
  896. end
  897. end
  898. mqttc:disconnect()
  899. -- 断开连接即 设备即将重连网络,即置2,等网络重连成功,则会上报 曾掉线重连网络过。
  900. dev_sta = 2
  901. datalink = 0 -- 服务器断开链接数据指示灯慢闪
  902. sys.wait(1000)
  903. end
  904. end)
  905. -- mqtt任务检测
  906. sys.taskInit(function()
  907. sys.wait(1000*10)
  908. while true do
  909. if mqtt_status == "" then
  910. mqtt_num = mqtt_num + 1
  911. elseif mqtt_status == "OK" then
  912. mqtt_status = ""
  913. mqtt_num = 0
  914. sys.wait(1000)
  915. end
  916. if mqtt_num == 100 then
  917. sys.restart("MQTT1 TASK FAILED!!!")
  918. end
  919. log.warn("====================mqtt_num",mqtt_num,mqtt_status)
  920. sys.wait(1000)
  921. end
  922. end)
  923. -- 网络状况监控任务:
  924. sys.taskInit(function()
  925. local err = 0
  926. while true do
  927. while not socket.isReady() do
  928. err = err + 1
  929. log.warn("socket is not Ready,", "err:", err)
  930. sys.wait(1000)
  931. end
  932. if socket.isReady() then
  933. --没有手动定位并且基站定位为0,调用基站定位
  934. if loc_lat == 0 and manual_location.lat == 0 and gps_loc_lat==0 then
  935. lbsLoc.request(getLocCb)
  936. end
  937. log.info("***socket is Ready!!!")
  938. end
  939. sys.wait(5000)
  940. end
  941. end)
  942. --GPS定位任务
  943. sys.taskInit(function(...)
  944. gpsv2.open(2, 9600, 2, 5)
  945. sys.wait(1000*10)
  946. while true do
  947. --如果未定位成功,并且没有手动设置过定位,打印定位信息
  948. if gpsv2.isFix() == false and gps_status ~= 1 then
  949. log.info("testGps isFix:", gpsv2.isFix())
  950. log.info("testGps lng,lat:", gpsv2.getIntLocation())
  951. log.info("testGps message:", gpsv2.getAltitude(), gpsv2.getSpeed(),
  952. gpsv2.getAzimuth(), gpsv2.getUsedSateCnt(), gpsv2.getViewedSateCnt())
  953. -- 如果已经定位成功,未赋值
  954. elseif gps_loc_lat ==0 and gpsv2.isFix() == true then
  955. gps_status = 1
  956. gps_loc_lng,gps_loc_lat = gpsv2.getDegLocation()
  957. manual_location.lat = gps_loc_lat
  958. manual_location.lng = gps_loc_lng
  959. manual_location.type = 1
  960. gps_conf.ext.lat = gps_loc_lat
  961. gps_conf.ext.lng = gps_loc_lng
  962. gps_conf.ext.type = 1
  963. local gps_table_to_json = json.encode(gps_conf)
  964. io.writeFile(GPS_CONF,gps_table_to_json)
  965. audio.play(0,"FILE","/ldata/save.mp3", hmi.hmiconf.vol)
  966. log.warn("================OK-GPS-OK===============",gps_loc_lat,gps_loc_lng,loc_lat,loc_lng,manual_location.lat,manual_location.lng,manual_location.type)
  967. -- 已经获取到GPS经纬度,关闭GPS
  968. elseif gps_loc_lat ~= 0 and manual_location.type == 1 then
  969. gps_status = 1
  970. gpsv2.close()
  971. log.warn("----------------------GPS CLOSE!!!",gps_loc_lat,gps_loc_lng,loc_lat,loc_lng,gps_status)
  972. end
  973. if manual_location.type == 0 and gpsv2.isFix() == false and manual_location.lat ~= 0 then
  974. gps_status = 0
  975. log.warn("SHOU DONG DING WEI !!!!!",manual_location.lat,manual_location.lng)
  976. elseif manual_location.type == 1 and manual_location.lat ~= 0 then
  977. gps_status = 1 --GPS定位
  978. log.warn("GPS location file read:",manual_location.lat,manual_location.lng)
  979. end
  980. sys.wait(1000)
  981. end
  982. end)
  983. -- sys.taskInit(function()
  984. -- nvm.init("config.lua")
  985. -- local err = 0
  986. -- -- 等待设备联网:
  987. -- -- 两分钟后启动;
  988. -- sys.wait(120000)
  989. -- -- sys.wait(40000)
  990. -- -- nvm.set("socket_err",0)
  991. -- log.error("===config.socket_err",nvm.get("socket_err"))
  992. -- -- log.error("===config.wdg_err",nvm.get("wdg_err"))
  993. -- log.warn("***************socket monitor task is running!***************")
  994. -- while true do
  995. -- while not socket.isReady() do
  996. -- log.warn('socket is not Ready,','err:',err)
  997. -- err = err + 1
  998. -- if err >= 90 then
  999. -- -- if err >= 5 then
  1000. -- --参数+1并赋值,立即写入文件系统,
  1001. -- nvm.set("socket_err",nvm.get("socket_err")+1)
  1002. -- -- nvm.set("wdg_err",nvm.get("wdg_err")+1)
  1003. -- log.error("config.socket_err",nvm.get("socket_err"))
  1004. -- -- control.stop_all_move()
  1005. -- -- control.stop_all_move()
  1006. -- sys.restart("socket connect err overtime:90!!!")
  1007. -- end
  1008. -- sys.wait(1000)
  1009. -- end
  1010. -- if socket.isReady() then
  1011. -- log.info('***socket is Ready!!!')
  1012. -- err = 0
  1013. -- end
  1014. -- sys.wait(5000)
  1015. -- end
  1016. -- end)
  1017. sys.taskInit(function()
  1018. local cnow
  1019. while true do
  1020. cnow = misc.getClock()
  1021. -- log.warn("time tast print >> now time:" .. os.date("%Y%m%d%H%M%S",os.time(cnow)))
  1022. if cnow.hour == 9 and cnow.min == 0 and cnow.sec == 0 and photo_sw == 0 then
  1023. -- if cnow.hour == 14 and cnow.min == 40 and cnow.sec == 01 then
  1024. log.error("timephoto!!! time is" .. os.date("%Y%m%d%H%M%S",os.time(cnow)))
  1025. haikang.test_takephoto()
  1026. sys.wait(15000)
  1027. haikang.http_chose_photo_task()
  1028. end
  1029. -- log.warn("free space is",rtos.get_fs_free_size())
  1030. sys.wait(1000)
  1031. end
  1032. end)
  1033. function photosw(sw)
  1034. if not sw then return end
  1035. sw = tonumber(sw)
  1036. if sw < 2 then
  1037. photo_sw = sw
  1038. io.writeFile(TAKEPHOTO,photo_sw)
  1039. end
  1040. end
  1041. -- sys.taskInit(function()
  1042. -- while true do
  1043. -- -- local url = "http://yfzhwlw.com/test"
  1044. -- local url = "http://yfznscd.com/test"
  1045. -- local param = {
  1046. -- imei="",
  1047. -- }
  1048. -- while not socket.isReady() do sys.waitUntil('IP_READY_IND') end
  1049. -- param.imei = misc.getImei()-- 唯一ID
  1050. -- log.error("#################### type param is:",type(param))
  1051. -- local code, head, body = http.request("POST", url, 5000,nil,param)
  1052. -- log.error("#################### http code is:",code)
  1053. -- log.error("#################### http body is:",body)
  1054. -- -- local cnt = 1
  1055. -- -- for cnt = 1, 3 do
  1056. -- -- while not socket.isReady() do sys.waitUntil('IP_READY_IND') end
  1057. -- -- -- while not socket.isReady() do sys.wait(2000) end
  1058. -- -- local code, head, body = http.request("GET", url, 5000)
  1059. -- -- log.error("#################### http code is:",code)
  1060. -- -- end
  1061. -- sys.wait(10000)
  1062. -- end
  1063. -- end)
  1064. ----设置定位
  1065. function setLocation(dat)
  1066. local cmds, result, err = json.decode(dat)
  1067. if not result then
  1068. log.warn("Set work location failed!")
  1069. end
  1070. manual_location = cmds.ext
  1071. log.warn("set manual_location is:",manual_location.lat,manual_location.lng)
  1072. io.writeFile(GPS_CONF,dat)
  1073. end