//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') const nobleWrapper = require('./ble/nobleWrapper') // socket 服务开启 let socketEvent = null; var connectCount = 0; var lastBuffer = null; var nowConnectedBlueDeviceId = '' var log4js = require('log4js'); var allLogPath = process.env['APPDATA'] + '/zxy/logs/'+ commonFunction.getLogDate() + '.log' log4js.configure({ appenders: { cheese: { type: 'file', filename: allLogPath } }, categories: { default: { appenders: ['cheese'], level: 'info' } } }); const logger = log4js.getLogger('cheese'); var receiveType = 'pressure' 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); // io.sockets.emit('setDeviceStatus', blueTouth.getBlueServerStatus()); // 将io对象传递给blueTooth js (发送连接客户端已经断开客户端消息发送) // blueTouth.setIo(io) }) // 发送获取采样频率指令 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') }) socket.on('searchModel',function (sendData) { let data = sendProtocal.searchModel(); blueTouth.sendData(data) }) socket.on('setModel',function (sendData) { let data = sendProtocal.inSetModel(); blueTouth.sendData(data) }) socket.on('InModel',function (sendData) { let data = sendProtocal.inTransModel(); blueTouth.sendData(data) }) // 蓝牙设置wifi账号密码 socket.on('setWifiAccount',function (sendData) { let data = sendProtocal.blueToothSendCommand( sendData,'wifiAccount','set') blueTouth.sendData(data) }) // 蓝牙获取wifi账号密码 socket.on('getWifiAccount',function (sendData) { let data = sendProtocal.blueToothSendCommand( sendData,'wifiAccount','get') blueTouth.sendData(data) }) // 蓝牙设置wifiIP socket.on('setWifiIp',function (sendData) { let data = sendProtocal.blueToothSendCommand( sendData,'wifiIp','set') blueTouth.sendData(data) }) // 蓝牙获取wifi账号密码 socket.on('getWifiIp',function (sendData) { let data = sendProtocal.blueToothSendCommand( sendData,'wifiIp','get') blueTouth.sendData(data) }) // 蓝牙设置 服务端IP与端口 socket.on('setServerIp',function (sendData) { let data = sendProtocal.blueToothSendCommand( sendData,'serverIp','set') blueTouth.sendData(data) }) // 蓝牙获取wifi账号密码 socket.on('getServerIp',function (sendData) { let data = sendProtocal.blueToothSendCommand(sendData,'serverIp','get') blueTouth.sendData(data) }) socket.on('ble', function(request) { if (request.cmd === 'scan') { nobleWrapper.scan(function(resp) { io.sockets.emit('blueDeviceList', resp) }) } else if(request.cmd === 'connectDevice') { // 连接蓝牙设备 request.args.on_data_cb = function (data) { // 接收数据! if(receiveType == 'pressure') { 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; } } else { io.sockets.emit('pushBlueData', commonFunction.toBuffer(data).toString('hex')) } // io.sockets.emit('blueToothData', buffer) } nobleWrapper.connectDevice(request.args,function(resp) { io.sockets.emit('connectDeviceBack', resp) if(resp.ok) { nowConnectedBlueDeviceId = request.args.id } else { if(resp.msg == 'connectError') { // 连接错误 console.log('server connectError') } } //io.sockets.emit('blueDeviceList', resp) }) // nobleWrapper.connectDevice(request.args, request.args.successCallBack, request.args.errorCallBack); } else if (request.cmd ==='disConnectDevice') { // 断开蓝牙设备 if(nowConnectedBlueDeviceId !='') { nobleWrapper.disConnectDevice(nowConnectedBlueDeviceId,function (resp) { io.sockets.emit('disConnectDeviceBack', resp) }) } } else if(request.cmd ==='getCharServiceList') { // 获取CharServiceList if(nowConnectedBlueDeviceId !='') { nobleWrapper.getCharList(nowConnectedBlueDeviceId,function (info) { io.sockets.emit('pushCharacteristicServerList', info) }) } } else if(request.cmd == 'setup') { if(nowConnectedBlueDeviceId !='') { receiveType = request.type nobleWrapper.setUp(request.data) } } else if(request.cmd == 'closeSetUp') { // 用户关闭dialog 并把CharacteristicServer 取消订阅 unsubscribe nobleWrapper.disSetUp() } }); socket.on('sendBleData',function (data) { let sendData = '' switch (data.type) { case 'setZero' : sendData = sendProtocal.beginSetZero() break; case 'stopSetZero': sendData = sendProtocal.endSetZero() break; case 'startCollection': sendData = sendProtocal.startCollection() break; case 'stopCollection': sendData = sendProtocal.endCollection() break; case 'resetDevice': sendData = sendProtocal.resetDevice() break; case 'sendOwnData': sendData = data.data break; } nobleWrapper.write(sendData,function (res) { console.log(res) }) }) }) const frame_header_and_size_bytes = 1 + 2; socketServer.on('connection',(socket)=>{ 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); logger.info(allCommond) 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