123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- //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')
- // 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)
- })
- })
- 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){
- let validNumByteArray = new Uint8Array(data.slice(1, 3));
- let validNum = validNumByteArray[0]
- if(data.length >validNum) {
- let validateData = data.slice(0, 1 + 2 +validNum);
- setAllCommand(validateData)
- lastBuffer = data.slice(1+2+validNum)
- if(lastBuffer.length >=1+2+validNum) {
- data = lastBuffer
- continue;
- }
- } else if(data.length == 1+2+validNum) {
- 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');
- //socketServer.close();
- });
- })
- // 处理所有指令(根据帧头判断业务逻辑)
- 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
|