protocal.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. var log4js = require('log4js');
  2. var machineInfo = require('./global');
  3. function validate(data) {
  4. // 公用协议校验
  5. // HEADER部分
  6. //TODO 待完善
  7. }
  8. function asiciiToString (arr) {
  9. let str = ''
  10. if(arr.length >0) {
  11. for(var i of arr) {
  12. str += String.fromCharCode(i)
  13. }
  14. }
  15. return str
  16. }
  17. Buffer.prototype.toByteArray = function () {
  18. return Array.prototype.slice.call(this, 0)
  19. }
  20. const logger = log4js.getLogger('protocal');
  21. module.exports = {
  22. reconTest: function (data) {
  23. let json = {};
  24. let snLength = data[5] - 1
  25. let sn = data.slice(6, 6 + snLength).toByteArray();
  26. //console.log(asiciiToString(sn))
  27. json.device = asiciiToString(sn);
  28. machineInfo.sn = sn;
  29. json.status = data[6 + snLength];
  30. return json;
  31. },
  32. rePressReset:function(data){
  33. let json = {};
  34. let snLength = data[5] - 1
  35. let sn = data.slice(6, 6 + snLength).toByteArray();
  36. json.device = sn;
  37. machineInfo.sn = sn;
  38. json.status = data[6 + snLength];
  39. return json;
  40. },
  41. reStopScan:function(data) {
  42. let json = {};
  43. let snLength = data[5] - 1
  44. let sn = data.slice(6, 6 + snLength).toByteArray();
  45. json.device = sn;
  46. machineInfo.sn = sn;
  47. json.status = data[6 + snLength];
  48. return json;
  49. },
  50. reSocketPressReStart:function (data) {
  51. let json = {};
  52. let snLength = data[5] - 1
  53. let sn = data.slice(6, 6 + snLength).toByteArray();
  54. json.device = asiciiToString(sn);
  55. machineInfo.sn = sn;
  56. json.status = data[6 + snLength];
  57. return json;
  58. },
  59. pressScanAbnormal:function(data) {
  60. let json = {};
  61. let snLength = data[5] - 1
  62. let sn = data.slice(6, 6 + snLength).toByteArray();
  63. json.device = sn;
  64. machineInfo.sn = sn;
  65. json.status = data[6 + snLength];
  66. return json;
  67. },
  68. reCheckCamera:function (data) {
  69. let json = {};
  70. let snLength = data[5] - 1
  71. let sn = data.slice(6, 6 + snLength).toByteArray();
  72. json.device = sn;
  73. machineInfo.sn = sn;
  74. json.status = data[6 + snLength];
  75. return json;
  76. },
  77. equipmentFault:function (data) {
  78. let json = {};
  79. let snLength = data[5] - 1
  80. let sn = data.slice(6, 6 + snLength).toByteArray();
  81. json.device = sn;
  82. machineInfo.sn = sn;
  83. json.status = data[6 + snLength];
  84. return json;
  85. },
  86. faultRepaired:function (data) {
  87. let json = {};
  88. let snLength = data[5] - 1
  89. let sn = data.slice(6, 6 + snLength).toByteArray();
  90. json.device = sn;
  91. machineInfo.sn = sn;
  92. json.status = data[6 + snLength];
  93. return json;
  94. },
  95. standArea: function (data) {
  96. let json = {};
  97. json.standArea = data[5 + data[5]];
  98. return json;
  99. },
  100. pressCop: function (data) {
  101. let json = {};
  102. json.pressCop = {};
  103. json.pressCop.leftFoot = {};
  104. let temp = []
  105. for (let index = 0; index < 2; index++) {
  106. let dataArray = [];
  107. for (let i = 0; i < 8; i++) {
  108. dataArray.push(data[10 + index * 8 + i]);
  109. }
  110. let ratio = new Float64Array(new Uint8Array(dataArray).buffer)[0];
  111. temp.push(ratio);
  112. }
  113. json.pressCop.leftFoot.x = temp[0];
  114. json.pressCop.leftFoot.y = temp[1];
  115. json.pressCop.rightFoot = {};
  116. let temp2 = []
  117. for (let index = 0; index < 2; index++) {
  118. let dataArray = [];
  119. for (let i = 0; i < 8; i++) {
  120. dataArray.push(data[26 + index * 8 + i]);
  121. }
  122. let ratio2 = new Float64Array(new Uint8Array(dataArray).buffer)[0];
  123. temp2.push(ratio2);
  124. }
  125. json.pressCop.rightFoot.x = temp2[0];
  126. json.pressCop.rightFoot.y = temp2[1];
  127. json.pressCop.body = {};
  128. let temp3 = []
  129. for (let index = 0; index < 2; index++) {
  130. let dataArray = [];
  131. for (let i = 0; i < 8; i++) {
  132. dataArray.push(data[42 + index * 8 + i]);
  133. }
  134. let ratio3 = new Float64Array(new Uint8Array(dataArray).buffer)[0];
  135. temp3.push(ratio3);
  136. }
  137. json.pressCop.body.x = temp3[0];
  138. json.pressCop.body.y = temp3[1];
  139. json.pressCop.bbody = {}
  140. let temp4 = []
  141. for (let index = 0; index < 2; index++) {
  142. let dataArray = [];
  143. for (let i = 0; i < 8; i++) {
  144. dataArray.push(data[58 + index * 8 + i]);
  145. }
  146. let ratio4 = new Float64Array(new Uint8Array(dataArray).buffer)[0];
  147. temp4.push(ratio4);
  148. }
  149. json.pressCop.bbody.x = temp4[0];
  150. json.pressCop.bbody.y = temp4[1];
  151. return json;
  152. },
  153. pressRatio: function (data) {
  154. let json = {};
  155. json.line = {};
  156. json.line.leftLine = [];
  157. for (let index = 11; index < 19; index = index + 4) {
  158. let line = [];
  159. line.push({ "x": data[index], "y": data[index + 1] });
  160. line.push({ "x": data[index + 2], "y": data[index + 3] });
  161. json.line.leftLine.push(line);
  162. }
  163. json.line.rightLine = [];
  164. for (let index = 19; index < 27; index = index + 4) {
  165. json.line.rightLine.push([{ "x": data[index], "y": data[index + 1] }, {
  166. "x": data[index + 2],
  167. "y": data[index + 3]
  168. }]);
  169. }
  170. json.pressRatio = {};
  171. json.pressRatio.area = {};
  172. json.pressRatio.area.left = [];
  173. for (let index = 0; index < 3; index++) {
  174. let dataArray = [];
  175. for (let i = 0; i < 8; i++) {
  176. dataArray.push(data[27 + index * 8 + i]);
  177. }
  178. let ratio = new Float64Array(new Uint8Array(dataArray).buffer)[0];
  179. json.pressRatio.area.left.push(ratio);
  180. }
  181. json.pressRatio.area.right = [];
  182. for (let index = 0; index < 3; index++) {
  183. let dataArray = [];
  184. for (let i = 0; i < 8; i++) {
  185. dataArray.push(data[51 + index * 8 + i]);
  186. }
  187. let ratio = new Float64Array(new Uint8Array(dataArray).buffer)[0];
  188. json.pressRatio.area.right.push(ratio);
  189. }
  190. {
  191. let dataArray = [];
  192. for (let i = 0; i < 8; i++) {
  193. dataArray.push(data[75 + i]);
  194. }
  195. json.pressRatio.general = new Float64Array(new Uint8Array(dataArray).buffer)[0];
  196. }
  197. return json;
  198. },
  199. pressure: function (data) {
  200. let validNumByteArray = new Uint8Array(data.slice(10, 14));
  201. let validNum = new DataView(validNumByteArray.buffer).getInt32();
  202. let json = {};
  203. var offset = 14;
  204. json.pressureData = [];
  205. //json.temp = [];
  206. for (let j =0;j<60;j++) {
  207. json.pressureData[j] = []
  208. for (let k =0;k<60;k++) {
  209. json.pressureData[j][k] = 0
  210. }
  211. }
  212. for (let i = 0; i < validNum; i++) {
  213. json.pressureData[data[offset + i * 3 + 1]][data[offset + i * 3]] = data[offset + i * 3 + 2]
  214. //let press = { x: data[offset + i * 3 + 1]*11, y: data[offset + i * 3]*10, value: data[offset + i * 3 + 2]};
  215. //json.temp.push(press);
  216. //json.pressureData.push(press)
  217. }
  218. // 点数放大1.2倍 横坐标值放大9倍 纵坐标值放大8倍
  219. // json.pressureData = pressureDataProcess(json.pressureData,1.2,9)
  220. return json;
  221. },
  222. // 收集数据结束
  223. collectionDataOver:function (data) {
  224. let json = {};
  225. let snLength = data[5] - 1
  226. let sn = data.slice(6, 6 + snLength).toByteArray();
  227. json.device = sn;
  228. machineInfo.sn = sn;
  229. json.status = data[6 + snLength];
  230. console.log('collectionStatus'+json.status)
  231. return json;
  232. },
  233. errorType:function(data){
  234. let json = {};
  235. let snLength = data[5] - 1
  236. let sn = data.slice(6, 6 + snLength).toByteArray();
  237. json.device = asiciiToString(sn);
  238. machineInfo.sn = sn;
  239. json.status = data[6 + snLength];
  240. return json;
  241. }
  242. }
  243. // 压力值扩散方法
  244. function pressureDataProcess(a,scale,size) {
  245. var height = 60;
  246. var width = 60;
  247. var newHeight = 60*scale;
  248. var newWidth = 60*scale;
  249. var u =0.0;
  250. var v = 0.0;
  251. var x = 0.0;
  252. var y = 0.0;
  253. var m = 0;
  254. var n = 0;
  255. let temp = new Array()
  256. let rearr = []
  257. for(let i = 0;i<newHeight;++i) {
  258. temp[i] = new Array()
  259. for(let j=0;j<newWidth;++j) {
  260. y = i/scale;
  261. x = j/scale;
  262. m = parseInt(y);
  263. n = parseInt(x);
  264. v = y-m;
  265. u = x-n;
  266. let tt = {}
  267. if(m<height-1 &&n<width-1){
  268. temp[i][j] = parseInt((1.0-v)*((1.0-u)*a[m][n]+u*a[m][n+1])
  269. + v*((1.0-u)*a[m+1][n] + u*a[m+1][n+1])
  270. )
  271. } else {
  272. temp[i][j] = a[m][n]
  273. }
  274. tt.x = i*size
  275. tt.y = j*size
  276. if(temp[i][j]>0) {
  277. tt.value = temp[i][j]
  278. rearr.push(tt)
  279. }
  280. }
  281. }
  282. return rearr
  283. }
  284. function randomFrom(lowerValue = 180,upperValue = 250)
  285. {
  286. return Math.floor(Math.random() * (upperValue - lowerValue + 1) + lowerValue);
  287. }