app.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. var nowConnectedBlueDeviceId = ''
  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(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 === 'connectDevice') {
  169. // 连接蓝牙设备
  170. request.args.on_data_cb = function (data) {
  171. // 接收数据!
  172. if (lastBuffer !== null) {
  173. data = Buffer.concat([lastBuffer, data]);
  174. }
  175. let flag = 1;
  176. while(flag){
  177. if (data.length < frame_header_and_size_bytes) {
  178. lastBuffer = data;
  179. break;
  180. }
  181. let dataLength = data.readUIntLE(1, 2)
  182. let frameLength = frame_header_and_size_bytes + dataLength
  183. if(data.length > frameLength) {
  184. let frame = data.slice(0, frameLength);
  185. setAllCommand(frame)
  186. lastBuffer = data.slice(frameLength)
  187. if(lastBuffer.length >= frame_header_and_size_bytes) {
  188. data = lastBuffer
  189. continue;
  190. }
  191. } else if(data.length === frameLength) {
  192. setAllCommand(data)
  193. lastBuffer = null;
  194. } else {
  195. lastBuffer = data;
  196. }
  197. flag = 0;
  198. }
  199. // io.sockets.emit('blueToothData', buffer)
  200. }
  201. nobleWrapper.connectDevice(request.args,function(resp) {
  202. io.sockets.emit('connectDeviceBack', resp)
  203. if(resp.ok) {
  204. nowConnectedBlueDeviceId = request.args.id
  205. } else {
  206. if(resp.msg == 'connectError') {
  207. // 连接错误
  208. console.log('server connectError')
  209. }
  210. }
  211. //io.sockets.emit('blueDeviceList', resp)
  212. })
  213. // nobleWrapper.connectDevice(request.args, request.args.successCallBack, request.args.errorCallBack);
  214. } else if (request.cmd ==='disConnectDevice') {
  215. // 断开蓝牙设备
  216. if(nowConnectedBlueDeviceId !='') {
  217. nobleWrapper.disConnectDevice(nowConnectedBlueDeviceId,function (resp) {
  218. io.sockets.emit('disConnectDeviceBack', resp)
  219. })
  220. }
  221. } else if(request.cmd ==='getCharServiceList') {
  222. // 获取CharServiceList
  223. if(nowConnectedBlueDeviceId !='') {
  224. nobleWrapper.getCharList(nowConnectedBlueDeviceId,function (info) {
  225. io.sockets.emit('pushCharacteristicServerList', info)
  226. })
  227. }
  228. } else if(request.cmd == 'setup') {
  229. if(nowConnectedBlueDeviceId !='') {
  230. nobleWrapper.setUp(request.data)
  231. }
  232. } else if(request.cmd == 'closeSetUp') {
  233. // 用户关闭dialog 并把CharacteristicServer 取消订阅 unsubscribe
  234. console.log('close')
  235. nobleWrapper.disSetUp()
  236. }
  237. });
  238. socket.on('sendBleData',function (data) {
  239. let sendData = ''
  240. switch (data.type) {
  241. case 'setZero' :
  242. sendData = sendProtocal.beginSetZero()
  243. break;
  244. case 'stopSetZero':
  245. sendData = sendProtocal.endSetZero()
  246. break;
  247. case 'startCollection':
  248. sendData = sendProtocal.startCollection()
  249. break;
  250. case 'stopCollection':
  251. sendData = sendProtocal.endCollection()
  252. break;
  253. case 'resetDevice':
  254. sendData = sendProtocal.resetDevice()
  255. break;
  256. }
  257. nobleWrapper.write(sendData,function (res) {
  258. console.log(res)
  259. })
  260. })
  261. })
  262. const frame_header_and_size_bytes = 1 + 2;
  263. socketServer.on('connection',(socket)=>{
  264. socketEvent = socket
  265. socketServer.getConnections((err, count)=>{
  266. if(err){
  267. console.warn(err);
  268. } else {
  269. console.log(`当前有${count}个连接`);
  270. connectCount = count
  271. io.sockets.emit('connectCount', count);
  272. }
  273. });
  274. socket.on('data', (data)=>{
  275. if (lastBuffer !== null) {
  276. data = Buffer.concat([lastBuffer, data]);
  277. }
  278. let flag = 1;
  279. while(flag){
  280. if (data.length < frame_header_and_size_bytes) {
  281. lastBuffer = data;
  282. break;
  283. }
  284. let dataLength = data.readUIntLE(1, 2)
  285. let frameLength = frame_header_and_size_bytes + dataLength
  286. if(data.length > frameLength) {
  287. let frame = data.slice(0, frameLength);
  288. setAllCommand(frame)
  289. lastBuffer = data.slice(frameLength)
  290. if(lastBuffer.length >= frame_header_and_size_bytes) {
  291. data = lastBuffer
  292. continue;
  293. }
  294. } else if(data.length === frameLength) {
  295. setAllCommand(data)
  296. lastBuffer = null;
  297. } else {
  298. lastBuffer = data;
  299. }
  300. flag = 0;
  301. }
  302. });
  303. socket.on('error', (err)=>{
  304. console.warn(err);
  305. socket.destroy();
  306. });
  307. socket.on('end', ()=>{
  308. io.sockets.emit('disConnectOneClient');
  309. });
  310. })
  311. // 处理所有指令(根据帧头判断业务逻辑)
  312. function setAllCommand(validateData) {
  313. let validNumByteArray = new Uint8Array(validateData);
  314. let allCommond = commonFunction.buf2hex(validNumByteArray);
  315. let commond = allCommond.substring(0,2)
  316. // 判断 帧头 e5 足压扫描
  317. if(commond == 'e5') {
  318. footPressure(validNumByteArray)
  319. }
  320. }
  321. var col = 0
  322. var tempPressureData = []
  323. // 处理足压数据
  324. function footPressure(validNumByteArray) {
  325. let offset = 10;
  326. let nowCol = validNumByteArray[8];
  327. //console.log(nowCol)
  328. if(nowCol== 51) {
  329. tempPressureData[col] = validNumByteArray.slice(offset)
  330. io.sockets.emit('pressureData',commonFunction.setPressureData(tempPressureData));
  331. tempPressureData = [];
  332. col = 0
  333. }
  334. if(nowCol >= col) {
  335. tempPressureData[nowCol] = validNumByteArray.slice(offset)
  336. col = nowCol;
  337. } else {
  338. io.sockets.emit('pressureData',commonFunction.setPressureData(tempPressureData));
  339. tempPressureData = [];
  340. tempPressureData[nowCol] = validNumByteArray.slice(offset)
  341. col = nowCol;
  342. // col = 0
  343. }
  344. }
  345. module.exports = server