app.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. // socket 服务开启
  13. let socketEvent = null;
  14. var connectCount = 0;
  15. var lastBuffer = null;
  16. io.sockets.on('connection', function (socket) {
  17. // 取零
  18. socket.on('startSetZero', function () {
  19. if(socketEvent!==null) {
  20. let sendData = sendProtocal.beginSetZero();
  21. socketEvent.write(sendData)
  22. }
  23. })
  24. // 停止取零
  25. socket.on('stopSetZero', function () {
  26. if(socketEvent!==null) {
  27. let sendData = sendProtocal.endSetZero();
  28. socketEvent.write(sendData)
  29. }
  30. })
  31. // 开始采集
  32. socket.on('startCollection', function () {
  33. if(socketEvent!==null) {
  34. let sendData = sendProtocal.startCollection();
  35. socketEvent.write(sendData)
  36. }
  37. })
  38. // 停止采集
  39. socket.on('stopCollection', function () {
  40. if(socketEvent!==null) {
  41. let sendData = sendProtocal.endCollection();
  42. socketEvent.write(sendData)
  43. }
  44. })
  45. // 复位设备
  46. socket.on('resetDevice', function () {
  47. if(socketEvent!==null) {
  48. let sendData = sendProtocal.resetDevice();
  49. socketEvent.write(sendData)
  50. }
  51. })
  52. socket.on('syncConnectCount', function () {
  53. if(connectCount !='0') {
  54. console.log('已有初始连接' +connectCount +'个')
  55. }
  56. io.sockets.emit('connectCount', connectCount);
  57. io.sockets.emit('setDeviceStatus', blueTouth.getBlueServerStatus());
  58. // 将io对象传递给blueTooth js (发送连接客户端已经断开客户端消息发送)
  59. blueTouth.setIo(io)
  60. })
  61. // 发送获取采样频率指令
  62. socket.on('checkCaiyang',function (sendData) {
  63. let data = sendProtocal.sendMsg(sendData,'caiyang','get')
  64. socketEvent.write(data)
  65. })
  66. // 发送设置采样频率指令
  67. socket.on('setCaiyang',function (sendData) {
  68. let data = sendProtocal.sendMsg(sendData,'caiyang','set')
  69. socketEvent.write(data)
  70. })
  71. // 发送检查传感器 阈值
  72. socket.on('checkChuangan',function (sendData) {
  73. let data = sendProtocal.sendMsg(sendData,'chuangan','get')
  74. socketEvent.write(data)
  75. })
  76. // 发送 设置传感器 阈值
  77. socket.on('setChuangan',function (sendData) {
  78. let data = sendProtocal.sendMsg(sendData,'chuangan','set')
  79. socketEvent.write(data)
  80. })
  81. // 发送检查驱动
  82. socket.on('checkQudong',function (sendData) {
  83. let data = sendProtocal.sendMsg(sendData,'qudong','get')
  84. socketEvent.write(data)
  85. })
  86. // 发送 设置驱动
  87. socket.on('setQudong',function (sendData) {
  88. let data = sendProtocal.sendMsg(sendData,'qudong','set')
  89. socketEvent.write(data)
  90. })
  91. // 发送检查功能
  92. socket.on('checkGongneng',function (sendData) {
  93. let data = sendProtocal.sendMsg(sendData,'gongneng','get')
  94. socketEvent.write(data)
  95. })
  96. // 发送 设置功能
  97. socket.on('setGongneng',function (sendData) {
  98. let data = sendProtocal.sendMsg(sendData,'gongneng','set')
  99. socketEvent.write(data)
  100. })
  101. // 发送检查设备
  102. socket.on('checkShebei',function (sendData) {
  103. let data = sendProtocal.sendMsg(sendData,'shebei','get')
  104. socketEvent.write(data)
  105. })
  106. // 发送设置设备
  107. socket.on('setShebei',function (sendData) {
  108. let data = sendProtocal.sendMsg(sendData,'shebei','set')
  109. socketEvent.write(data)
  110. })
  111. socket.on('sendBlueTouth',function (sendData) {
  112. blueTouth.sendData('123')
  113. })
  114. socket.on('searchModel',function (sendData) {
  115. let data = sendProtocal.searchModel();
  116. blueTouth.sendData(data)
  117. })
  118. socket.on('setModel',function (sendData) {
  119. let data = sendProtocal.inSetModel();
  120. blueTouth.sendData(data)
  121. })
  122. socket.on('InModel',function (sendData) {
  123. let data = sendProtocal.inTransModel();
  124. blueTouth.sendData(data)
  125. })
  126. // 蓝牙设置wifi账号密码
  127. socket.on('setWifiAccount',function (sendData) {
  128. let data = sendProtocal.blueToothSendCommand(
  129. sendData,'wifiAccount','set')
  130. blueTouth.sendData(data)
  131. })
  132. // 蓝牙获取wifi账号密码
  133. socket.on('getWifiAccount',function (sendData) {
  134. let data = sendProtocal.blueToothSendCommand(
  135. sendData,'wifiAccount','get')
  136. blueTouth.sendData(data)
  137. })
  138. // 蓝牙设置wifiIP
  139. socket.on('setWifiIp',function (sendData) {
  140. let data = sendProtocal.blueToothSendCommand(
  141. sendData,'wifiIp','set')
  142. blueTouth.sendData(data)
  143. })
  144. // 蓝牙获取wifi账号密码
  145. socket.on('getWifiIp',function (sendData) {
  146. let data = sendProtocal.blueToothSendCommand(
  147. sendData,'wifiIp','get')
  148. blueTouth.sendData(data)
  149. })
  150. // 蓝牙设置 服务端IP与端口
  151. socket.on('setServerIp',function (sendData) {
  152. let data = sendProtocal.blueToothSendCommand(
  153. sendData,'serverIp','set')
  154. blueTouth.sendData(data)
  155. })
  156. // 蓝牙获取wifi账号密码
  157. socket.on('getServerIp',function (sendData) {
  158. let data = sendProtocal.blueToothSendCommand(
  159. sendData,'serverIp','get')
  160. blueTouth.sendData(data)
  161. })
  162. })
  163. const frame_header_and_size_bytes = 1 + 2;
  164. socketServer.on('connection',(socket)=>{
  165. socketEvent = socket
  166. socketServer.getConnections((err, count)=>{
  167. if(err){
  168. console.warn(err);
  169. } else {
  170. console.log(`当前有${count}个连接`);
  171. connectCount = count
  172. io.sockets.emit('connectCount', count);
  173. }
  174. });
  175. socket.on('data', (data)=>{
  176. if (lastBuffer !== null) {
  177. data = Buffer.concat([lastBuffer, data]);
  178. }
  179. let flag = 1;
  180. while(flag){
  181. if (data.length < frame_header_and_size_bytes) {
  182. lastBuffer = data;
  183. break;
  184. }
  185. let dataLength = data.readUIntLE(1, 2)
  186. let frameLength = frame_header_and_size_bytes + dataLength
  187. if(data.length > frameLength) {
  188. let frame = data.slice(0, frameLength);
  189. setAllCommand(frame)
  190. lastBuffer = data.slice(frameLength)
  191. if(lastBuffer.length >= frame_header_and_size_bytes) {
  192. data = lastBuffer
  193. continue;
  194. }
  195. } else if(data.length === frameLength) {
  196. setAllCommand(data)
  197. lastBuffer = null;
  198. } else {
  199. lastBuffer = data;
  200. }
  201. flag = 0;
  202. }
  203. });
  204. socket.on('error', (err)=>{
  205. console.warn(err);
  206. socket.destroy();
  207. });
  208. socket.on('end', ()=>{
  209. io.sockets.emit('disConnectOneClient');
  210. });
  211. })
  212. // 处理所有指令(根据帧头判断业务逻辑)
  213. function setAllCommand(validateData) {
  214. let validNumByteArray = new Uint8Array(validateData);
  215. let allCommond = commonFunction.buf2hex(validNumByteArray);
  216. let commond = allCommond.substring(0,2)
  217. // 判断 帧头 e5 足压扫描
  218. if(commond == 'e5') {
  219. footPressure(validNumByteArray)
  220. }
  221. }
  222. var col = 0
  223. var tempPressureData = []
  224. // 处理足压数据
  225. function footPressure(validNumByteArray) {
  226. let offset = 10;
  227. let nowCol = validNumByteArray[8];
  228. //console.log(nowCol)
  229. if(nowCol== 51) {
  230. tempPressureData[col] = validNumByteArray.slice(offset)
  231. io.sockets.emit('pressureData',commonFunction.setPressureData(tempPressureData));
  232. tempPressureData = [];
  233. col = 0
  234. }
  235. if(nowCol >= col) {
  236. tempPressureData[nowCol] = validNumByteArray.slice(offset)
  237. col = nowCol;
  238. } else {
  239. io.sockets.emit('pressureData',commonFunction.setPressureData(tempPressureData));
  240. tempPressureData = [];
  241. tempPressureData[nowCol] = validNumByteArray.slice(offset)
  242. col = nowCol;
  243. // col = 0
  244. }
  245. }
  246. module.exports = server