123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- 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')
- const nobleWrapper = require('./ble/nobleWrapper')
- let socketEvent = null;
- var connectCount = 0;
- var lastBuffer = null;
- var nowConnectedBlueDeviceId = ''
- 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')
- })
- 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)
- })
-
- socket.on('setWifiAccount',function (sendData) {
- let data = sendProtocal.blueToothSendCommand(
- sendData,'wifiAccount','set')
- blueTouth.sendData(data)
- })
-
- socket.on('getWifiAccount',function (sendData) {
- let data = sendProtocal.blueToothSendCommand(
- sendData,'wifiAccount','get')
- blueTouth.sendData(data)
- })
-
- socket.on('setWifiIp',function (sendData) {
- let data = sendProtocal.blueToothSendCommand(
- sendData,'wifiIp','set')
- blueTouth.sendData(data)
- })
-
- socket.on('getWifiIp',function (sendData) {
- let data = sendProtocal.blueToothSendCommand(
- sendData,'wifiIp','get')
- blueTouth.sendData(data)
- })
-
- socket.on('setServerIp',function (sendData) {
- let data = sendProtocal.blueToothSendCommand(
- sendData,'serverIp','set')
- blueTouth.sendData(data)
- })
-
- 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 (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;
- }
-
- }
- 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')
- }
- }
-
- })
-
- } else if (request.cmd ==='disConnectDevice') {
-
- if(nowConnectedBlueDeviceId !='') {
- nobleWrapper.disConnectDevice(nowConnectedBlueDeviceId,function (resp) {
- io.sockets.emit('disConnectDeviceBack', resp)
- })
- }
- } else if(request.cmd ==='getCharServiceList') {
-
- if(nowConnectedBlueDeviceId !='') {
- nobleWrapper.getCharList(nowConnectedBlueDeviceId,function (info) {
- io.sockets.emit('pushCharacteristicServerList', info)
- })
- }
- } else if(request.cmd == 'setup') {
- if(nowConnectedBlueDeviceId !='') {
- nobleWrapper.setUp(request.data)
- }
- } else if(request.cmd == 'closeSetUp') {
-
- console.log('close')
- 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;
- }
- 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);
- let commond = allCommond.substring(0,2)
- if(commond == 'e5') {
- footPressure(validNumByteArray)
- }
- }
- var col = 0
- var tempPressureData = []
- function footPressure(validNumByteArray) {
- let offset = 10;
- let nowCol = validNumByteArray[8];
-
- 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;
-
- }
- }
- module.exports = server
|