You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wjDataDecode.js 4.4KB

5 月之前
1
  1. "use strict";var iPackageLen,_wjUtils=require("./wjUtils.js"),_wjUtils2=_interopRequireDefault(_wjUtils);function _interopRequireDefault(t){return t&&t.__esModule?t:{default:t}}var bleCount,rxCount=0,strTemData="",strRxBuffer="",isPagFinish=!0;function decode_GB(t){var e=_wjUtils2.default.getCMD_TYPE(),a=0,r={},t=_wjUtils2.default.byteArray2hexStr(t),s=(_wjUtils2.default.showLog("WJ当前接受包的数据:",t),_wjUtils2.default.hexStr2byteArray(t));if(1<s.byteLength?2==e?51==s[0]&&1==isPagFinish?(isPagFinish=!1,iPackageLen=s[3],strTemData=t):strTemData+=t:80==s[0]&&1==isPagFinish?(isPagFinish=!1,iPackageLen=s[3],strTemData=t):strTemData+=t:strTemData+=t,strTemData.length/2>=iPackageLen+5){rxCount+=1,isPagFinish=!0;var o=_wjUtils2.default.hexStr2byteArray(strTemData);if(_wjUtils2.default.showLog("接受完第",rxCount,"包数据,内容 ",strTemData),2==e)for(var i=1;i<iPackageLen+3;i++)a^=o[i];else for(i=0;i<iPackageLen+4;i++)a^=o[i];if(o[strTemData.length/2-1]!=a)return _wjUtils2.default.showError("WJ第",rxCount," 包校验bcc:Error!"),r.serviceCode=-1,r.serviceInfo="校验错误。。。",r;128==(128&o[1])&&(bleCount=((127&o[1])<<8)+o[2],_wjUtils2.default.showLog("总共:",bleCount,"大包")),_wjUtils2.default.showLog("WJ第",rxCount," 包校验bcc:ok!"),strRxBuffer+=strTemData.substring(8,strTemData.length-2)}return bleCount<=rxCount?(r.serviceCode=0,r.serviceInfo="数据接收完成!",r.serviceData={},r.serviceData.strData=strRxBuffer):(r.serviceCode=1,r.serviceInfo="数据接收中。。。"),r}function decode_WX(t){console.log("接受到的BUFFER数据",t);var e=0,a={},t=_wjUtils2.default.byteArray2hexStr(t),r=(_wjUtils2.default.showLog("WJ当前接受包的数据:",t),_wjUtils2.default.hexStr2byteArray(t));if(1<r.byteLength&&254==r[0]&&1==r[1]&&1==isPagFinish?(isPagFinish=!1,iPackageLen=(255&r[2])<<8,iPackageLen+=255&r[3],strTemData=t):strTemData+=t,strTemData.length/2>=iPackageLen){_wjUtils2.default.showLog("大包长度:",iPackageLen,strTemData.length),isPagFinish=!0,rxCount+=1;var s=_wjUtils2.default.hexStr2byteArray(strTemData),r={},o=0,i=0,u=0;for(r.FixHead={},r.FixHead.MagicNumber=strTemData.substr(2*o,2),r.FixHead.Ver=strTemData.substring(2*++o,2),r.FixHead.Len=(s[++o]<<8)+s[o+1],r.FixHead.CmdId=(s[o+=2]<<8)+s[o+1],r.FixHead.Seq=(s[o+=2]<<8)+s[o+1],o+=2,r.ProtoBuf={},r.ProtoBuf.BaseRequest={},r.ProtoBuf.BaseRequest.Mark=strTemData.substr(2*o,2),o++,u=i=0;128==(128&s[o]);)i+=(127&s[o])<<7*u,o++,u++;for(i+=(127&s[o])<<7*u,r.ProtoBuf.BaseRequest.Data=strTemData.substr(2*++o,2*i),o+=i,r.ProtoBuf.Data={},r.ProtoBuf.Data.Mark=strTemData.substr(2*o,2),o++,u=i=0;128==(128&s[o]);)i+=(127&s[o])<<7*u,o++,u++;for(i+=(127&s[o])<<7*u,r.ProtoBuf.Data.Data=strTemData.substr(2*++o,2*i),o+=i,r.ProtoBuf.DataType={},r.ProtoBuf.DataType.Mark=strTemData.substr(2*o,2),o++,u=i=0;128==(128&s[o]);)i+=(127&s[o])<<7*u,o++,u++;i+=(127&s[o])<<7*u,o++,r.ProtoBuf.DataType.Varint=i;for(var n=_wjUtils2.default.hexStr2byteArray(r.ProtoBuf.Data.Data),l=1;l<n.byteLength-1;l++)e^=n[l];if("fe01001a271100010a0018808004200128023a06"!=t&&!t.startsWith("5701")&&!t.startsWith("5348")&&"fe010013271300020a001201411a0400000000"!=t){if(n[n.byteLength-1]!=e)return _wjUtils2.default.showError("WJ第",rxCount," 包校验bcc:Error!"),a.serviceCode=-1,a.serviceInfo="校验错误。。。",a;2==_wjUtils2.default.getCMD_TYPE()?(128==(128&n[1])&&(bleCount=1+(127&n[1]),_wjUtils2.default.showLog("总共:",bleCount,"大包")),strRxBuffer+=r.ProtoBuf.Data.Data.substr(6,2*n[2])):(128==(128&n[2])&&(bleCount=1+(127&n[2]),_wjUtils2.default.showLog("总共:",bleCount,"大包")),strRxBuffer+=r.ProtoBuf.Data.Data.substr(8,2*n[3])),_wjUtils2.default.showLog("WJ第",rxCount," 包校验bcc:ok!")}_wjUtils2.default.showLog("接受完第",rxCount,"大包数据,内容 ",strTemData)}return console.log("rxCount",rxCount,"bleCount",bleCount),strTemData.startsWith("fe01001a271100010a0018808004200128023a06")&&52===strTemData.length||strTemData.startsWith("fe010013271300020a001201411a0400000000")?(a.serviceCode=0,a.serviceInfo="数据接收完成!",a.serviceData={},a.serviceData.strData=strTemData,console.log("握手指令")):bleCount<=rxCount?(console.log("接收数据完成"),a.serviceCode=0,a.serviceInfo="数据接收完成!",a.serviceData={},a.serviceData.strData=strRxBuffer,rxCount=0):(console.log("数据接收中"),a.serviceCode=1,a.serviceInfo="数据接收中。。。"),a}function init(){rxCount=iPackageLen=0,isPagFinish=!(strRxBuffer=strTemData="")}module.exports={decode_GB:decode_GB,decode_WX:decode_WX,init:init};