app.js 8.4 KB

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