app.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. const nobleWrapper = require('./ble/nobleWrapper')
  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. // io.sockets.emit('setDeviceStatus', blueTouth.getBlueServerStatus());
  59. // 将io对象传递给blueTooth js (发送连接客户端已经断开客户端消息发送)
  60. // blueTouth.setIo(io)
  61. })
  62. // 发送获取采样频率指令
  63. socket.on('checkCaiyang',function (sendData) {
  64. let data = sendProtocal.sendMsg(sendData,'caiyang','get')
  65. socketEvent.write(data)
  66. })
  67. // 发送设置采样频率指令
  68. socket.on('setCaiyang',function (sendData) {
  69. let data = sendProtocal.sendMsg(sendData,'caiyang','set')
  70. socketEvent.write(data)
  71. })
  72. // 发送检查传感器 阈值
  73. socket.on('checkChuangan',function (sendData) {
  74. let data = sendProtocal.sendMsg(sendData,'chuangan','get')
  75. socketEvent.write(data)
  76. })
  77. // 发送 设置传感器 阈值
  78. socket.on('setChuangan',function (sendData) {
  79. let data = sendProtocal.sendMsg(sendData,'chuangan','set')
  80. socketEvent.write(data)
  81. })
  82. // 发送检查驱动
  83. socket.on('checkQudong',function (sendData) {
  84. let data = sendProtocal.sendMsg(sendData,'qudong','get')
  85. socketEvent.write(data)
  86. })
  87. // 发送 设置驱动
  88. socket.on('setQudong',function (sendData) {
  89. let data = sendProtocal.sendMsg(sendData,'qudong','set')
  90. socketEvent.write(data)
  91. })
  92. // 发送检查功能
  93. socket.on('checkGongneng',function (sendData) {
  94. let data = sendProtocal.sendMsg(sendData,'gongneng','get')
  95. socketEvent.write(data)
  96. })
  97. // 发送 设置功能
  98. socket.on('setGongneng',function (sendData) {
  99. let data = sendProtocal.sendMsg(sendData,'gongneng','set')
  100. socketEvent.write(data)
  101. })
  102. // 发送检查设备
  103. socket.on('checkShebei',function (sendData) {
  104. let data = sendProtocal.sendMsg(sendData,'shebei','get')
  105. socketEvent.write(data)
  106. })
  107. // 发送设置设备
  108. socket.on('setShebei',function (sendData) {
  109. let data = sendProtocal.sendMsg(sendData,'shebei','set')
  110. socketEvent.write(data)
  111. })
  112. socket.on('sendBlueTouth',function (sendData) {
  113. blueTouth.sendData('123')
  114. })
  115. socket.on('searchModel',function (sendData) {
  116. let data = sendProtocal.searchModel();
  117. blueTouth.sendData(data)
  118. })
  119. socket.on('setModel',function (sendData) {
  120. let data = sendProtocal.inSetModel();
  121. blueTouth.sendData(data)
  122. })
  123. socket.on('InModel',function (sendData) {
  124. let data = sendProtocal.inTransModel();
  125. blueTouth.sendData(data)
  126. })
  127. // 蓝牙设置wifi账号密码
  128. socket.on('setWifiAccount',function (sendData) {
  129. let data = sendProtocal.blueToothSendCommand(
  130. sendData,'wifiAccount','set')
  131. blueTouth.sendData(data)
  132. })
  133. // 蓝牙获取wifi账号密码
  134. socket.on('getWifiAccount',function (sendData) {
  135. let data = sendProtocal.blueToothSendCommand(
  136. sendData,'wifiAccount','get')
  137. blueTouth.sendData(data)
  138. })
  139. // 蓝牙设置wifiIP
  140. socket.on('setWifiIp',function (sendData) {
  141. let data = sendProtocal.blueToothSendCommand(
  142. sendData,'wifiIp','set')
  143. blueTouth.sendData(data)
  144. })
  145. // 蓝牙获取wifi账号密码
  146. socket.on('getWifiIp',function (sendData) {
  147. let data = sendProtocal.blueToothSendCommand(
  148. sendData,'wifiIp','get')
  149. blueTouth.sendData(data)
  150. })
  151. // 蓝牙设置 服务端IP与端口
  152. socket.on('setServerIp',function (sendData) {
  153. let data = sendProtocal.blueToothSendCommand(
  154. sendData,'serverIp','set')
  155. blueTouth.sendData(data)
  156. })
  157. // 蓝牙获取wifi账号密码
  158. socket.on('getServerIp',function (sendData) {
  159. let data = sendProtocal.blueToothSendCommand(
  160. sendData,'serverIp','get')
  161. blueTouth.sendData(data)
  162. })
  163. socket.on('ble', function(request) {
  164. if (request.cmd === 'scan') {
  165. nobleWrapper.scan(function(resp) {
  166. io.sockets.emit('blueDeviceList', resp)
  167. })
  168. } else if (request.cmd === 'connect') {
  169. let on_data_cb = function (buffer) {
  170. console.log('Received: "' + buffer.length + '"');
  171. }
  172. let resp_cb = function(resp) {
  173. io.sockets.emit('blueConnect', resp)
  174. }
  175. nobleWrapper.connect(request.args, on_data_cb, resp_cb);
  176. } else if (request.cmd === 'send') {
  177. nobleWrapper.write(request.args.msg, function(resp) {
  178. io.sockets.emit('blueSend', resp)
  179. });
  180. }
  181. });
  182. })
  183. const frame_header_and_size_bytes = 1 + 2;
  184. socketServer.on('connection',(socket)=>{
  185. socketEvent = socket
  186. socketServer.getConnections((err, count)=>{
  187. if(err){
  188. console.warn(err);
  189. } else {
  190. console.log(`当前有${count}个连接`);
  191. connectCount = count
  192. io.sockets.emit('connectCount', count);
  193. }
  194. });
  195. socket.on('data', (data)=>{
  196. if (lastBuffer !== null) {
  197. data = Buffer.concat([lastBuffer, data]);
  198. }
  199. let flag = 1;
  200. while(flag){
  201. if (data.length < frame_header_and_size_bytes) {
  202. lastBuffer = data;
  203. break;
  204. }
  205. let dataLength = data.readUIntLE(1, 2)
  206. let frameLength = frame_header_and_size_bytes + dataLength
  207. if(data.length > frameLength) {
  208. let frame = data.slice(0, frameLength);
  209. setAllCommand(frame)
  210. lastBuffer = data.slice(frameLength)
  211. if(lastBuffer.length >= frame_header_and_size_bytes) {
  212. data = lastBuffer
  213. continue;
  214. }
  215. } else if(data.length === frameLength) {
  216. setAllCommand(data)
  217. lastBuffer = null;
  218. } else {
  219. lastBuffer = data;
  220. }
  221. flag = 0;
  222. }
  223. });
  224. socket.on('error', (err)=>{
  225. console.warn(err);
  226. socket.destroy();
  227. });
  228. socket.on('end', ()=>{
  229. io.sockets.emit('disConnectOneClient');
  230. });
  231. })
  232. // 处理所有指令(根据帧头判断业务逻辑)
  233. function setAllCommand(validateData) {
  234. let validNumByteArray = new Uint8Array(validateData);
  235. let allCommond = commonFunction.buf2hex(validNumByteArray);
  236. let commond = allCommond.substring(0,2)
  237. // 判断 帧头 e5 足压扫描
  238. if(commond == 'e5') {
  239. footPressure(validNumByteArray)
  240. }
  241. }
  242. var col = 0
  243. var tempPressureData = []
  244. // 处理足压数据
  245. function footPressure(validNumByteArray) {
  246. let offset = 10;
  247. let nowCol = validNumByteArray[8];
  248. //console.log(nowCol)
  249. if(nowCol== 51) {
  250. tempPressureData[col] = validNumByteArray.slice(offset)
  251. io.sockets.emit('pressureData',commonFunction.setPressureData(tempPressureData));
  252. tempPressureData = [];
  253. col = 0
  254. }
  255. if(nowCol >= col) {
  256. tempPressureData[nowCol] = validNumByteArray.slice(offset)
  257. col = nowCol;
  258. } else {
  259. io.sockets.emit('pressureData',commonFunction.setPressureData(tempPressureData));
  260. tempPressureData = [];
  261. tempPressureData[nowCol] = validNumByteArray.slice(offset)
  262. col = nowCol;
  263. // col = 0
  264. }
  265. }
  266. module.exports = server