app.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //const socketClient = require('./socketClient')
  2. const socketServer = require('./socketServer')
  3. const Koa = require('koa')
  4. const app = new Koa()
  5. var http = require('http');
  6. var server = http.createServer(app.callback());
  7. io = require('socket.io').listen(server);
  8. var sendProtocal = require('./protocal/sendProtocal')
  9. var command = require('./protocal/command')
  10. var commonFunction = require('./protocal/commonFunction')
  11. // socket 服务开启
  12. let socketEvent = null;
  13. var connectCount = 0;
  14. var lastBuffer = null;
  15. io.sockets.on('connection', function (socket) {
  16. // 取零
  17. socket.on('startSetZero', function () {
  18. if(socketEvent!==null) {
  19. let sendData = sendProtocal.beginSetZero();
  20. socketEvent.write(sendData)
  21. }
  22. })
  23. // 停止取零
  24. socket.on('stopSetZero', function () {
  25. if(socketEvent!==null) {
  26. let sendData = sendProtocal.endSetZero();
  27. socketEvent.write(sendData)
  28. }
  29. })
  30. // 开始采集
  31. socket.on('startCollection', function () {
  32. if(socketEvent!==null) {
  33. let sendData = sendProtocal.startCollection();
  34. socketEvent.write(sendData)
  35. }
  36. })
  37. // 停止采集
  38. socket.on('stopCollection', function () {
  39. if(socketEvent!==null) {
  40. let sendData = sendProtocal.endCollection();
  41. socketEvent.write(sendData)
  42. }
  43. })
  44. // 复位设备
  45. socket.on('resetDevice', function () {
  46. if(socketEvent!==null) {
  47. let sendData = sendProtocal.resetDevice();
  48. socketEvent.write(sendData)
  49. }
  50. })
  51. socket.on('syncConnectCount', function () {
  52. if(connectCount !='0') {
  53. console.log('已有初始连接' +connectCount +'个')
  54. }
  55. io.sockets.emit('connectCount', connectCount);
  56. })
  57. // 发送获取采样频率指令
  58. socket.on('checkCaiyang',function (sendData) {
  59. let data = sendProtocal.sendMsg(sendData,'caiyang','get')
  60. socketEvent.write(data)
  61. })
  62. // 发送设置采样频率指令
  63. socket.on('setCaiyang',function (sendData) {
  64. let data = sendProtocal.sendMsg(sendData,'caiyang','set')
  65. socketEvent.write(data)
  66. })
  67. // 发送检查传感器 阈值
  68. socket.on('checkChuangan',function (sendData) {
  69. let data = sendProtocal.sendMsg(sendData,'chuangan','get')
  70. socketEvent.write(data)
  71. })
  72. // 发送 设置传感器 阈值
  73. socket.on('setChuangan',function (sendData) {
  74. let data = sendProtocal.sendMsg(sendData,'chuangan','set')
  75. socketEvent.write(data)
  76. })
  77. // 发送检查驱动
  78. socket.on('checkQudong',function (sendData) {
  79. let data = sendProtocal.sendMsg(sendData,'qudong','get')
  80. socketEvent.write(data)
  81. })
  82. // 发送 设置驱动
  83. socket.on('setQudong',function (sendData) {
  84. let data = sendProtocal.sendMsg(sendData,'qudong','set')
  85. socketEvent.write(data)
  86. })
  87. // 发送检查功能
  88. socket.on('checkGongneng',function (sendData) {
  89. let data = sendProtocal.sendMsg(sendData,'gongneng','get')
  90. socketEvent.write(data)
  91. })
  92. // 发送 设置功能
  93. socket.on('setGongneng',function (sendData) {
  94. let data = sendProtocal.sendMsg(sendData,'gongneng','set')
  95. socketEvent.write(data)
  96. })
  97. // 发送检查设备
  98. socket.on('checkShebei',function (sendData) {
  99. let data = sendProtocal.sendMsg(sendData,'shebei','get')
  100. socketEvent.write(data)
  101. })
  102. // 发送设置设备
  103. socket.on('setShebei',function (sendData) {
  104. let data = sendProtocal.sendMsg(sendData,'shebei','set')
  105. socketEvent.write(data)
  106. })
  107. })
  108. socketServer.on('connection',(socket)=>{
  109. console.log('连接已建立')
  110. socketEvent = socket
  111. socketServer.getConnections((err, count)=>{
  112. if(err){
  113. console.warn(err);
  114. } else {
  115. console.log(`当前有${count}个连接`);
  116. connectCount = count
  117. io.sockets.emit('connectCount', count);
  118. }
  119. });
  120. socket.on('data', (data)=>{
  121. if (lastBuffer !== null) {
  122. data = Buffer.concat([lastBuffer, data]);
  123. }
  124. let flag = 1;
  125. while(flag){
  126. let validNumByteArray = new Uint8Array(data.slice(1, 3));
  127. let validNum = validNumByteArray[0]
  128. if(data.length >validNum) {
  129. let validateData = data.slice(0, 1 + 2 +validNum);
  130. setAllCommand(validateData)
  131. lastBuffer = data.slice(1+2+validNum)
  132. if(lastBuffer.length >=1+2+validNum) {
  133. data = lastBuffer
  134. continue;
  135. }
  136. } else if(data.length == 1+2+validNum) {
  137. setAllCommand(data)
  138. lastBuffer = null;
  139. } else {
  140. lastBuffer = data;
  141. }
  142. flag = 0;
  143. }
  144. // 处理压力板发过来的数据
  145. });
  146. socket.on('error', (err)=>{
  147. console.warn(err);
  148. socket.destroy();
  149. });
  150. socket.on('end', ()=>{
  151. io.sockets.emit('disConnectOneClient');
  152. //socketServer.close();
  153. });
  154. })
  155. // 处理所有指令(根据帧头判断业务逻辑)
  156. function setAllCommand(validateData) {
  157. let validNumByteArray = new Uint8Array(validateData);
  158. let allCommond = commonFunction.buf2hex(validNumByteArray);
  159. let commond = allCommond.substring(0,2)
  160. // 判断 帧头 e5 足压扫描
  161. if(commond == 'e5') {
  162. footPressure(validNumByteArray)
  163. }
  164. }
  165. var col = 0
  166. var tempPressureData = []
  167. // 处理足压数据
  168. function footPressure(validNumByteArray) {
  169. let offset = 10;
  170. let nowCol = validNumByteArray[8];
  171. //console.log(nowCol)
  172. if(nowCol== 51) {
  173. tempPressureData[col] = validNumByteArray.slice(offset)
  174. io.sockets.emit('pressureData',commonFunction.setPressureData(tempPressureData));
  175. tempPressureData = [];
  176. col = 0
  177. }
  178. if(nowCol >= col) {
  179. tempPressureData[nowCol] = validNumByteArray.slice(offset)
  180. col = nowCol;
  181. } else {
  182. io.sockets.emit('pressureData',commonFunction.setPressureData(tempPressureData));
  183. tempPressureData = [];
  184. tempPressureData[nowCol] = validNumByteArray.slice(offset)
  185. col = nowCol;
  186. // col = 0
  187. }
  188. }
  189. module.exports = server