local cjson=require("cjson") local base64=require("base64") local syslog=require("syslog") local DEBUG=true function debug(log) if DEBUG then if type(log) == 'table' then for k,v in pairs(log) do syslog.syslog("LOG_WARNING",k,v) if type(v) == 'table' then debug(v) end end else if log then syslog.syslog("LOG_WARNING", log) end end end end function exec_host_cmd(cmd) local result=io.popen(cmd):read("*a") return result end function protocol_encode(json_str, len) syslog.openlog("camerajpg syslog", syslog.LOG_PERROR + syslog.LOG_ODELAY, "LOG_USER") local err_code=0 --取出入参-- local obj=cjson.decode(json_str) local id=obj.identifier local sn=obj.sn local obj_ext_data = cjson.decode(obj.ext_data) local rtsp_url=obj_ext_data.rtsp_url local ftp_config=obj_ext_data.ftp_config local cmd_str=obj.cmd_str if id ~= "get_jpg" or rtsp_url == nil then return 8192,json_str,#json_str end local jpg_name=string.format("%s.jpg", os.date("%Y-%m-%d_%H%M%S")) --清除历史图片 exec_host_cmd("rm /tmp/*.jpg") --截图 local cmd_str=string.format("ffmpeg -rtsp_transport tcp -nostdin -i \"%s\" -y -f image2 -t 0.001 /tmp/%s 2>&1 |logger -t 'camerajpg'", rtsp_url, jpg_name) debug(cmd_str) local result=exec_host_cmd(cmd_str) local jpg_url = "" --传图片 if ftp_config == nil then cmd_str=string.format("scp /tmp/%s root@118.178.126.254:/opt/wookong/repository/jpg_file/ 2>&1 |logger -t 'camerajpg'", jpg_name) debug(cmd_str) result=exec_host_cmd(cmd_str) --生成图片获取url jpg_url = string.format("http://118.178.126.254/repository/jpg_file/%s", jpg_name) else ftp_dir = string.format("%s/%s", sn, os.date("%Y%m%d")) cmd_str=string.format("rm -rf /tmp/%s;mkdir -p /tmp/%s;mv /tmp/%s /tmp/%s", sn, ftp_dir, jpg_name, ftp_dir) debug(cmd_str) result=exec_host_cmd(cmd_str) cmd_str=string.format("lftp %s -e \"mirror -R /tmp/%s;exit\" 2>&1 |logger -t 'camerajpg'", ftp_config, sn) debug(cmd_str) result=exec_host_cmd(cmd_str) --cmd_str=string.format("lftp %s -e \"put /tmp/%s;exit\" 2>&1 |logger -t 'camerajpg'", ftp_config, jpg_name) --debug(cmd_str) --result=exec_host_cmd(cmd_str) --生成图片获取url host_index=string.find(ftp_config,"@") host_info=string.sub(ftp_config,host_index+1,-1) jpg_url = string.format("/%s/%s", ftp_dir, jpg_name) jpg_name=string.format("%s/%s", ftp_dir, jpg_name) end local file = io.open(string.format("/tmp/%s", jpg_name), "rb") if file then file:close() end if file == nil then jpg_url=string.format("drr: %s No such file or directory", jpg_name) end --result=string.format("{\"jpg_url\": \"%s\"}", jpg_url) obj={ identifier=id, jpg_url=jpg_url, sn=obj.sn } json_str=cjson.encode(obj) debug(json_str) syslog.closelog() return 4096,json_str,#json_str end function protocol_decode(input_str, len) local err_code=0 --取出入参-- local obj={} local input_obj=cjson.decode(input_str) local id=input_obj.identifier --入参中的raw_data的二进制转换-- --出参格式化-- obj.term_addr=string.format("%02X", addr) obj.identifier=id local json_str=cjson.encode(obj) --print(err_code, json_str, #json_str) return err_code,json_str,#json_str end