//const socketClient = require('./socketClient') const socketServer = require('./socketServer') const Koa = require('koa') const app = new Koa() var http = require('http'); var server = http.createServer(app.callback()); io = require('socket.io').listen(server); var sendProtocal = require('./protocal/sendProtocal') var command = require('./protocal/command') var commonFunction = require('./protocal/commonFunction') var blueTouth = require('./blueTouth') blueTouth.setIo(io) // socket 服务开启 let socketEvent = null; var connectCount = 0; var lastBuffer = null; io.sockets.on('connection', function (socket) { // 取零 socket.on('startSetZero', function () { if(socketEvent!==null) { let sendData = sendProtocal.beginSetZero(); socketEvent.write(sendData) } }) // 停止取零 socket.on('stopSetZero', function () { if(socketEvent!==null) { let sendData = sendProtocal.endSetZero(); socketEvent.write(sendData) } }) // 开始采集 socket.on('startCollection', function () { if(socketEvent!==null) { let sendData = sendProtocal.startCollection(); socketEvent.write(sendData) } }) // 停止采集 socket.on('stopCollection', function () { if(socketEvent!==null) { let sendData = sendProtocal.endCollection(); socketEvent.write(sendData) } }) // 复位设备 socket.on('resetDevice', function () { if(socketEvent!==null) { let sendData = sendProtocal.resetDevice(); socketEvent.write(sendData) } }) socket.on('syncConnectCount', function () { if(connectCount !='0') { console.log('已有初始连接' +connectCount +'个') } io.sockets.emit('connectCount', connectCount); }) // 发送获取采样频率指令 socket.on('checkCaiyang',function (sendData) { let data = sendProtocal.sendMsg(sendData,'caiyang','get') socketEvent.write(data) }) // 发送设置采样频率指令 socket.on('setCaiyang',function (sendData) { let data = sendProtocal.sendMsg(sendData,'caiyang','set') socketEvent.write(data) }) // 发送检查传感器 阈值 socket.on('checkChuangan',function (sendData) { let data = sendProtocal.sendMsg(sendData,'chuangan','get') socketEvent.write(data) }) // 发送 设置传感器 阈值 socket.on('setChuangan',function (sendData) { let data = sendProtocal.sendMsg(sendData,'chuangan','set') socketEvent.write(data) }) // 发送检查驱动 socket.on('checkQudong',function (sendData) { let data = sendProtocal.sendMsg(sendData,'qudong','get') socketEvent.write(data) }) // 发送 设置驱动 socket.on('setQudong',function (sendData) { let data = sendProtocal.sendMsg(sendData,'qudong','set') socketEvent.write(data) }) // 发送检查功能 socket.on('checkGongneng',function (sendData) { let data = sendProtocal.sendMsg(sendData,'gongneng','get') socketEvent.write(data) }) // 发送 设置功能 socket.on('setGongneng',function (sendData) { let data = sendProtocal.sendMsg(sendData,'gongneng','set') socketEvent.write(data) }) // 发送检查设备 socket.on('checkShebei',function (sendData) { let data = sendProtocal.sendMsg(sendData,'shebei','get') socketEvent.write(data) }) // 发送设置设备 socket.on('setShebei',function (sendData) { let data = sendProtocal.sendMsg(sendData,'shebei','set') socketEvent.write(data) }) socket.on('sendBlueTouth',function (sendData) { blueTouth.sendData('123') }) }) const frame_header_and_size_bytes = 1 + 2; socketServer.on('connection',(socket)=>{ console.log('连接已建立') socketEvent = socket socketServer.getConnections((err, count)=>{ if(err){ console.warn(err); } else { console.log(`当前有${count}个连接`); connectCount = count io.sockets.emit('connectCount', count); } }); socket.on('data', (data)=>{ if (lastBuffer !== null) { data = Buffer.concat([lastBuffer, data]); } let flag = 1; while(flag){ if (data.length < frame_header_and_size_bytes) { lastBuffer = data; break; } let dataLength = data.readUIntLE(1, 2) let frameLength = frame_header_and_size_bytes + dataLength if(data.length > frameLength) { let frame = data.slice(0, frameLength); setAllCommand(frame) lastBuffer = data.slice(frameLength) if(lastBuffer.length >= frame_header_and_size_bytes) { data = lastBuffer continue; } } else if(data.length === frameLength) { setAllCommand(data) lastBuffer = null; } else { lastBuffer = data; } flag = 0; } }); socket.on('error', (err)=>{ console.warn(err); socket.destroy(); }); socket.on('end', ()=>{ io.sockets.emit('disConnectOneClient'); }); }) // 处理所有指令(根据帧头判断业务逻辑) function setAllCommand(validateData) { let validNumByteArray = new Uint8Array(validateData); let allCommond = commonFunction.buf2hex(validNumByteArray); let commond = allCommond.substring(0,2) // 判断 帧头 e5 足压扫描 if(commond == 'e5') { footPressure(validNumByteArray) } } var col = 0 var tempPressureData = [] // 处理足压数据 function footPressure(validNumByteArray) { let offset = 10; let nowCol = validNumByteArray[8]; //console.log(nowCol) if(nowCol== 51) { tempPressureData[col] = validNumByteArray.slice(offset) io.sockets.emit('pressureData',commonFunction.setPressureData(tempPressureData)); tempPressureData = []; col = 0 } if(nowCol >= col) { tempPressureData[nowCol] = validNumByteArray.slice(offset) col = nowCol; } else { io.sockets.emit('pressureData',commonFunction.setPressureData(tempPressureData)); tempPressureData = []; tempPressureData[nowCol] = validNumByteArray.slice(offset) col = nowCol; // col = 0 } } module.exports = server