iot.lua 41 KB

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