dyutils = require("dyutils") bit = require("bit") lpack = require("lua_pack") cjson = require("cjson") base64 = require("base64") syslog = require("syslog") bcd = require("bcd") sqlite3 = require("lsqlite3") bnot = bit.bnot band, bor, bxor = bit.band, bit.bor, bit.bxor lshift, rshift, rol = bit.lshift, bit.rshift, bit.rol last_report = 0 INTERVAL=10 function protocol_encode(json_str, len) local err_code=0 --取出入参-- local obj=cjson.decode(json_str) local id=obj.identifier --编码-- local result = {} local sn=obj.sn if (id == "conn") then result['UUID'] = "FFF2" result['data'] = "CC80020301010001" elseif (id == "start") then result['UUID'] = "FFF2" result['data'] = "CC80020301020002" end result['sn'] = sn json_str=cjson.encode(result) return 8192,json_str,#json_str end function protocol_decode(input_str, len) local err_code=0 local now = os.time() --取出入参-- local obj={} local input_obj=cjson.decode(input_str) local bin_str=input_obj.raw_data --入参中的raw_data的二进制转换-- local t_str='' for i=1,#bin_str,2 do t_str=t_str .. string.char(tonumber(string.sub(bin_str, i, i+1),16)) end bin_str=t_str local data_obj = cjson.decode(bin_str) obj['sn'] = data_obj['sn'] local uuid = data_obj['uuid'] local data_str = data_obj['data_str'] --print (data_str) local data_bin='' for i=1,#data_str,2 do data_bin=data_bin .. string.char(tonumber(string.sub(data_str, i, i+1),16)) end _,pre,ver,len,type,sub_type = bunpack(data_bin, ">S1C4") if pre ~= 0xAA80 then print("precode not right "..pre) return 0, nil, 0 end if type == 1 then if sub_type == 5 then if now < last_report + INTERVAL then return 0,nil,0 end last_report = now _,_,PH,_,_,PL,_ = bunpack(string.sub(data_bin, 7), "C6") p = PH * 256 + bxor(PH, PL) obj['blood_pressure'] = p obj['identifier'] = "blood_pressure" elseif sub_type == 6 then obj['identifier'] = "final_result" _,c1,c2,c3,c4,c5,c6 = bunpack(string.sub(data_bin, 14), "C6") obj['systolic'] = c1 * 256 + c2 obj['diastolic'] = c3 * 256 + c4 obj['PUL'] = c5 * 256 + c6 end elseif type == 4 then end --出参格式化-- local json_str=cjson.encode(obj) --print(err_code, json_str, #json_str) return err_code,json_str,#json_str end --protocol_decode(' {"raw_data":"7B22646174615F737472223A20226161383030323038303130353030303030303030323930303237222C2022736E223A20224F79397468556479222C202275756964223A202230303030666666312D303030302D313030302D383030302D303038303566396233346662227D","identifier":""}', 0)