commonFunction.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. module.exports = {
  2. // buffer 转16进制
  3. buf2hex:function (buffer) {
  4. // create a byte array (Uint8Array) that we can use to read the array buffer
  5. const byteArray = new Uint8Array(buffer);
  6. // for each element, we want to get its two-digit hexadecimal representation
  7. const hexParts = [];
  8. for(let i = 0; i < byteArray.length; i++) {
  9. // convert value to hexadecimal
  10. const hex = byteArray[i].toString(16);
  11. // pad with zeros to length 2
  12. const paddedHex = ('00' + hex).slice(-2);
  13. // push to array
  14. hexParts.push(paddedHex);
  15. }
  16. // join all the hex values of the elements into a single string
  17. return hexParts.join('');
  18. },
  19. // 足压数据补齐点位为60*60 并进行点扩散
  20. setPressureData:function (data) {
  21. let dataAll = []
  22. // 补到之前 60*60 个点位
  23. for (let j =0;j<60;j++) {
  24. dataAll[j] = []
  25. for (let k =0;k<60;k++) {
  26. if(typeof (data[j]) == 'undefined') {
  27. dataAll[j][k] = 0
  28. } else {
  29. if(typeof (data[j][k]) == 'undefined') {
  30. dataAll[j][k] = 0
  31. } else {
  32. dataAll[j][k] = data[j][k]
  33. }
  34. }
  35. }
  36. }
  37. return pressureDataProcess(dataAll,1,4)
  38. },
  39. // arrayBuffer 转string
  40. ab2Ascii(buffer) {
  41. var str = Array.prototype.map.call(
  42. new Uint8Array(buffer),
  43. function (bit) {
  44. return String.fromCharCode(bit);
  45. }
  46. )
  47. return str.join('');
  48. },
  49. // arrayBuffer to buffer
  50. toBuffer(ab) {
  51. var buf = new Buffer(ab.byteLength);
  52. var view = new Uint8Array(ab);
  53. for (var i = 0; i < buf.length; ++i) {
  54. buf[i] = view[i];
  55. }
  56. return buf;
  57. },
  58. getLogDate() {
  59. var myDate = new Date();
  60. return (myDate.getYear()-100).toString()+ '-' + addStrZero(myDate.getMonth()+1) + '-' + addStrZero(myDate.getDate()) +'_'+addStrZero(myDate.getHours())
  61. }
  62. }
  63. function addStrZero(num) {
  64. return num < 10 ? '0'+num.toString():num.toString()
  65. }
  66. function pressureDataProcess(a,scale,size) {
  67. var height = 60;
  68. var width = 60;
  69. var newHeight = 60*scale;
  70. var newWidth = 60*scale;
  71. var u =0.0;
  72. var v = 0.0;
  73. var x = 0.0;
  74. var y = 0.0;
  75. var m = 0;
  76. var n = 0;
  77. let temp = new Array()
  78. let rearr = []
  79. for(let i = 0;i<newHeight;++i) {
  80. temp[i] = new Array()
  81. for(let j=0;j<newWidth;++j) {
  82. y = i/scale;
  83. x = j/scale;
  84. m = parseInt(y);
  85. n = parseInt(x);
  86. v = y-m;
  87. u = x-n;
  88. let tt = {}
  89. if(m<height-1 &&n<width-1){
  90. temp[i][j] = parseInt((1.0-v)*((1.0-u)*a[m][n]+u*a[m][n+1])
  91. + v*((1.0-u)*a[m+1][n] + u*a[m+1][n+1])
  92. )
  93. } else {
  94. temp[i][j] = a[m][n]
  95. }
  96. tt.x = i*size
  97. tt.y = j*size
  98. if(temp[i][j]>0) {
  99. tt.value = temp[i][j]
  100. rearr.push(tt)
  101. }
  102. }
  103. }
  104. return rearr
  105. }