123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- module.exports = {
- // buffer 转16进制
- buf2hex:function (buffer) {
- // create a byte array (Uint8Array) that we can use to read the array buffer
- const byteArray = new Uint8Array(buffer);
- // for each element, we want to get its two-digit hexadecimal representation
- const hexParts = [];
- for(let i = 0; i < byteArray.length; i++) {
- // convert value to hexadecimal
- const hex = byteArray[i].toString(16);
- // pad with zeros to length 2
- const paddedHex = ('00' + hex).slice(-2);
- // push to array
- hexParts.push(paddedHex);
- }
- // join all the hex values of the elements into a single string
- return hexParts.join('');
- },
- // 足压数据补齐点位为60*60 并进行点扩散
- setPressureData:function (data) {
- let dataAll = []
- // 补到之前 60*60 个点位
- for (let j =0;j<60;j++) {
- dataAll[j] = []
- for (let k =0;k<60;k++) {
- if(typeof (data[j]) == 'undefined') {
- dataAll[j][k] = 0
- } else {
- if(typeof (data[j][k]) == 'undefined') {
- dataAll[j][k] = 0
- } else {
- dataAll[j][k] = data[j][k]
- }
- }
- }
- }
- return pressureDataProcess(dataAll,1,4)
- },
- // arrayBuffer 转string
- ab2Ascii(buffer) {
- var str = Array.prototype.map.call(
- new Uint8Array(buffer),
- function (bit) {
- return String.fromCharCode(bit);
- }
- )
- return str.join('');
- },
- // arrayBuffer to buffer
- toBuffer(ab) {
- var buf = new Buffer(ab.byteLength);
- var view = new Uint8Array(ab);
- for (var i = 0; i < buf.length; ++i) {
- buf[i] = view[i];
- }
- return buf;
- },
- getLogDate() {
- var myDate = new Date();
- return (myDate.getYear()-100).toString()+ '-' + addStrZero(myDate.getMonth()+1) + '-' + addStrZero(myDate.getDate()) +'_'+addStrZero(myDate.getHours())
- }
- }
- function addStrZero(num) {
- return num < 10 ? '0'+num.toString():num.toString()
- }
- 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
- }
|