123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- var log4js = require('log4js');
- var machineInfo = require('./global');
- function validate(data) {
- // 公用协议校验
- // HEADER部分
- //TODO 待完善
- }
- function asiciiToString (arr) {
- let str = ''
- if(arr.length >0) {
- for(var i of arr) {
- str += String.fromCharCode(i)
- }
- }
- return str
- }
- Buffer.prototype.toByteArray = function () {
- return Array.prototype.slice.call(this, 0)
- }
- const logger = log4js.getLogger('protocal');
- module.exports = {
- reconTest: function (data) {
- let json = {};
- let snLength = data[5] - 1
- let sn = data.slice(6, 6 + snLength).toByteArray();
- //console.log(asiciiToString(sn))
- json.device = asiciiToString(sn);
- machineInfo.sn = sn;
- json.status = data[6 + snLength];
- return json;
- },
- rePressReset:function(data){
- let json = {};
- let snLength = data[5] - 1
- let sn = data.slice(6, 6 + snLength).toByteArray();
- json.device = sn;
- machineInfo.sn = sn;
- json.status = data[6 + snLength];
- return json;
- },
- reStopScan:function(data) {
- let json = {};
- let snLength = data[5] - 1
- let sn = data.slice(6, 6 + snLength).toByteArray();
- json.device = sn;
- machineInfo.sn = sn;
- json.status = data[6 + snLength];
- return json;
- },
- reSocketPressReStart:function (data) {
- let json = {};
- let snLength = data[5] - 1
- let sn = data.slice(6, 6 + snLength).toByteArray();
- json.device = asiciiToString(sn);
- machineInfo.sn = sn;
- json.status = data[6 + snLength];
- return json;
- },
- pressScanAbnormal:function(data) {
- let json = {};
- let snLength = data[5] - 1
- let sn = data.slice(6, 6 + snLength).toByteArray();
- json.device = sn;
- machineInfo.sn = sn;
- json.status = data[6 + snLength];
- return json;
- },
- reCheckCamera:function (data) {
- let json = {};
- let snLength = data[5] - 1
- let sn = data.slice(6, 6 + snLength).toByteArray();
- json.device = sn;
- machineInfo.sn = sn;
- json.status = data[6 + snLength];
- return json;
- },
- equipmentFault:function (data) {
- let json = {};
- let snLength = data[5] - 1
- let sn = data.slice(6, 6 + snLength).toByteArray();
- json.device = sn;
- machineInfo.sn = sn;
- json.status = data[6 + snLength];
- return json;
- },
- faultRepaired:function (data) {
- let json = {};
- let snLength = data[5] - 1
- let sn = data.slice(6, 6 + snLength).toByteArray();
- json.device = sn;
- machineInfo.sn = sn;
- json.status = data[6 + snLength];
- return json;
- },
- standArea: function (data) {
- let json = {};
- json.standArea = data[5 + data[5]];
- return json;
- },
- pressCop: function (data) {
- let json = {};
- json.pressCop = {};
- json.pressCop.leftFoot = {};
- let temp = []
- for (let index = 0; index < 2; index++) {
- let dataArray = [];
- for (let i = 0; i < 8; i++) {
- dataArray.push(data[10 + index * 8 + i]);
- }
- let ratio = new Float64Array(new Uint8Array(dataArray).buffer)[0];
- temp.push(ratio);
- }
- json.pressCop.leftFoot.x = temp[0];
- json.pressCop.leftFoot.y = temp[1];
- json.pressCop.rightFoot = {};
- let temp2 = []
- for (let index = 0; index < 2; index++) {
- let dataArray = [];
- for (let i = 0; i < 8; i++) {
- dataArray.push(data[26 + index * 8 + i]);
- }
- let ratio2 = new Float64Array(new Uint8Array(dataArray).buffer)[0];
- temp2.push(ratio2);
- }
- json.pressCop.rightFoot.x = temp2[0];
- json.pressCop.rightFoot.y = temp2[1];
- json.pressCop.body = {};
- let temp3 = []
- for (let index = 0; index < 2; index++) {
- let dataArray = [];
- for (let i = 0; i < 8; i++) {
- dataArray.push(data[42 + index * 8 + i]);
- }
- let ratio3 = new Float64Array(new Uint8Array(dataArray).buffer)[0];
- temp3.push(ratio3);
- }
- json.pressCop.body.x = temp3[0];
- json.pressCop.body.y = temp3[1];
- json.pressCop.bbody = {}
- let temp4 = []
- for (let index = 0; index < 2; index++) {
- let dataArray = [];
- for (let i = 0; i < 8; i++) {
- dataArray.push(data[58 + index * 8 + i]);
- }
- let ratio4 = new Float64Array(new Uint8Array(dataArray).buffer)[0];
- temp4.push(ratio4);
- }
- json.pressCop.bbody.x = temp4[0];
- json.pressCop.bbody.y = temp4[1];
- return json;
- },
- pressRatio: function (data) {
- let json = {};
- json.line = {};
- json.line.leftLine = [];
- for (let index = 11; index < 19; index = index + 4) {
- let line = [];
- line.push({ "x": data[index], "y": data[index + 1] });
- line.push({ "x": data[index + 2], "y": data[index + 3] });
- json.line.leftLine.push(line);
- }
- json.line.rightLine = [];
- for (let index = 19; index < 27; index = index + 4) {
- json.line.rightLine.push([{ "x": data[index], "y": data[index + 1] }, {
- "x": data[index + 2],
- "y": data[index + 3]
- }]);
- }
- json.pressRatio = {};
- json.pressRatio.area = {};
- json.pressRatio.area.left = [];
- for (let index = 0; index < 3; index++) {
- let dataArray = [];
- for (let i = 0; i < 8; i++) {
- dataArray.push(data[27 + index * 8 + i]);
- }
- let ratio = new Float64Array(new Uint8Array(dataArray).buffer)[0];
- json.pressRatio.area.left.push(ratio);
- }
- json.pressRatio.area.right = [];
- for (let index = 0; index < 3; index++) {
- let dataArray = [];
- for (let i = 0; i < 8; i++) {
- dataArray.push(data[51 + index * 8 + i]);
- }
- let ratio = new Float64Array(new Uint8Array(dataArray).buffer)[0];
- json.pressRatio.area.right.push(ratio);
- }
- {
- let dataArray = [];
- for (let i = 0; i < 8; i++) {
- dataArray.push(data[75 + i]);
- }
- json.pressRatio.general = new Float64Array(new Uint8Array(dataArray).buffer)[0];
- }
- return json;
- },
- pressure: function (data) {
- let validNumByteArray = new Uint8Array(data.slice(10, 14));
- let validNum = new DataView(validNumByteArray.buffer).getInt32();
- let json = {};
- var offset = 14;
- json.pressureData = [];
- //json.temp = [];
- for (let j =0;j<60;j++) {
- json.pressureData[j] = []
- for (let k =0;k<60;k++) {
- json.pressureData[j][k] = 0
- }
- }
- for (let i = 0; i < validNum; i++) {
- json.pressureData[data[offset + i * 3 + 1]][data[offset + i * 3]] = data[offset + i * 3 + 2]
- //let press = { x: data[offset + i * 3 + 1]*11, y: data[offset + i * 3]*10, value: data[offset + i * 3 + 2]};
- //json.temp.push(press);
- //json.pressureData.push(press)
- }
- // 点数放大1.2倍 横坐标值放大9倍 纵坐标值放大8倍
- // json.pressureData = pressureDataProcess(json.pressureData,1.2,9)
- return json;
- },
- // 收集数据结束
- collectionDataOver:function (data) {
- let json = {};
- let snLength = data[5] - 1
- let sn = data.slice(6, 6 + snLength).toByteArray();
- json.device = sn;
- machineInfo.sn = sn;
- json.status = data[6 + snLength];
- console.log('collectionStatus'+json.status)
- return json;
- },
- errorType:function(data){
- let json = {};
- let snLength = data[5] - 1
- let sn = data.slice(6, 6 + snLength).toByteArray();
- json.device = asiciiToString(sn);
- machineInfo.sn = sn;
- json.status = data[6 + snLength];
- return json;
- }
- }
- // 压力值扩散方法
- function pressureDataProcess(a,scale,size) {
- var height = 60;
- var width = 60;
- var newHeight = 60*scale;
- var newWidth = 60*scale;
- var u =0.0;
- var v = 0.0;
- var x = 0.0;
- var y = 0.0;
- var m = 0;
- var n = 0;
- let temp = new Array()
- let rearr = []
- for(let i = 0;i<newHeight;++i) {
- temp[i] = new Array()
- for(let j=0;j<newWidth;++j) {
- y = i/scale;
- x = j/scale;
- m = parseInt(y);
- n = parseInt(x);
- v = y-m;
- u = x-n;
- let tt = {}
- if(m<height-1 &&n<width-1){
- temp[i][j] = parseInt((1.0-v)*((1.0-u)*a[m][n]+u*a[m][n+1])
- + v*((1.0-u)*a[m+1][n] + u*a[m+1][n+1])
- )
- } else {
- temp[i][j] = a[m][n]
- }
- tt.x = i*size
- tt.y = j*size
- if(temp[i][j]>0) {
- tt.value = temp[i][j]
- rearr.push(tt)
- }
- }
- }
- return rearr
- }
- function randomFrom(lowerValue = 180,upperValue = 250)
- {
- return Math.floor(Math.random() * (upperValue - lowerValue + 1) + lowerValue);
- }
|