app.js 6.5 KB

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