app.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. const frame_header_and_size_bytes = 1 + 2;
  109. socketServer.on('connection',(socket)=>{
  110. console.log('连接已建立')
  111. socketEvent = socket
  112. socketServer.getConnections((err, count)=>{
  113. if(err){
  114. console.warn(err);
  115. } else {
  116. console.log(`当前有${count}个连接`);
  117. connectCount = count
  118. io.sockets.emit('connectCount', count);
  119. }
  120. });
  121. socket.on('data', (data)=>{
  122. if (lastBuffer !== null) {
  123. data = Buffer.concat([lastBuffer, data]);
  124. }
  125. let flag = 1;
  126. while(flag){
  127. if (data.length < frame_header_and_size_bytes) {
  128. lastBuffer = data;
  129. break;
  130. }
  131. let dataLength = data.readUIntLE(1, 2)
  132. let frameLength = frame_header_and_size_bytes + dataLength
  133. if(data.length > frameLength) {
  134. let frame = data.slice(0, frameLength);
  135. setAllCommand(frame)
  136. lastBuffer = data.slice(frameLength)
  137. if(lastBuffer.length >= frame_header_and_size_bytes) {
  138. data = lastBuffer
  139. continue;
  140. }
  141. } else if(data.length === frameLength) {
  142. setAllCommand(data)
  143. lastBuffer = null;
  144. } else {
  145. lastBuffer = data;
  146. }
  147. flag = 0;
  148. }
  149. });
  150. socket.on('error', (err)=>{
  151. console.warn(err);
  152. socket.destroy();
  153. });
  154. socket.on('end', ()=>{
  155. io.sockets.emit('disConnectOneClient');
  156. });
  157. })
  158. // 处理所有指令(根据帧头判断业务逻辑)
  159. function setAllCommand(validateData) {
  160. let validNumByteArray = new Uint8Array(validateData);
  161. let allCommond = commonFunction.buf2hex(validNumByteArray);
  162. let commond = allCommond.substring(0,2)
  163. // 判断 帧头 e5 足压扫描
  164. if(commond == 'e5') {
  165. footPressure(validNumByteArray)
  166. }
  167. }
  168. var col = 0
  169. var tempPressureData = []
  170. // 处理足压数据
  171. function footPressure(validNumByteArray) {
  172. let offset = 10;
  173. let nowCol = validNumByteArray[8];
  174. //console.log(nowCol)
  175. if(nowCol== 51) {
  176. tempPressureData[col] = validNumByteArray.slice(offset)
  177. io.sockets.emit('pressureData',commonFunction.setPressureData(tempPressureData));
  178. tempPressureData = [];
  179. col = 0
  180. }
  181. if(nowCol >= col) {
  182. tempPressureData[nowCol] = validNumByteArray.slice(offset)
  183. col = nowCol;
  184. } else {
  185. io.sockets.emit('pressureData',commonFunction.setPressureData(tempPressureData));
  186. tempPressureData = [];
  187. tempPressureData[nowCol] = validNumByteArray.slice(offset)
  188. col = nowCol;
  189. // col = 0
  190. }
  191. }
  192. module.exports = server