app.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. var log4js = require('log4js');
  19. var allLogPath = process.env['APPDATA'] + '/zxy/logs/'+ commonFunction.getLogDate() + '.log'
  20. log4js.configure({
  21. appenders: { cheese: { type: 'file', filename: allLogPath } },
  22. categories: { default: { appenders: ['cheese'], level: 'info' } }
  23. });
  24. const logger = log4js.getLogger('cheese');
  25. var receiveType = 'pressure'
  26. io.sockets.on('connection', function (socket) {
  27. // 取零
  28. socket.on('startSetZero', function () {
  29. if(socketEvent!==null) {
  30. let sendData = sendProtocal.beginSetZero();
  31. socketEvent.write(sendData)
  32. }
  33. })
  34. // 停止取零
  35. socket.on('stopSetZero', function () {
  36. if(socketEvent!==null) {
  37. let sendData = sendProtocal.endSetZero();
  38. socketEvent.write(sendData)
  39. }
  40. })
  41. // 开始采集
  42. socket.on('startCollection', function () {
  43. if(socketEvent!==null) {
  44. let sendData = sendProtocal.startCollection();
  45. socketEvent.write(sendData)
  46. }
  47. })
  48. // 停止采集
  49. socket.on('stopCollection', function () {
  50. if(socketEvent!==null) {
  51. let sendData = sendProtocal.endCollection();
  52. socketEvent.write(sendData)
  53. }
  54. })
  55. // 复位设备
  56. socket.on('resetDevice', function () {
  57. if(socketEvent!==null) {
  58. let sendData = sendProtocal.resetDevice();
  59. socketEvent.write(sendData)
  60. }
  61. })
  62. socket.on('syncConnectCount', function () {
  63. if(connectCount !='0') {
  64. console.log('已有初始连接' +connectCount +'个')
  65. }
  66. io.sockets.emit('connectCount', connectCount);
  67. // io.sockets.emit('setDeviceStatus', blueTouth.getBlueServerStatus());
  68. // 将io对象传递给blueTooth js (发送连接客户端已经断开客户端消息发送)
  69. // blueTouth.setIo(io)
  70. })
  71. // 发送获取采样频率指令
  72. socket.on('checkCaiyang',function (sendData) {
  73. let data = sendProtocal.sendMsg(sendData,'caiyang','get')
  74. socketEvent.write(data)
  75. })
  76. // 发送设置采样频率指令
  77. socket.on('setCaiyang',function (sendData) {
  78. let data = sendProtocal.sendMsg(sendData,'caiyang','set')
  79. socketEvent.write(data)
  80. })
  81. // 发送检查传感器 阈值
  82. socket.on('checkChuangan',function (sendData) {
  83. let data = sendProtocal.sendMsg(sendData,'chuangan','get')
  84. socketEvent.write(data)
  85. })
  86. // 发送 设置传感器 阈值
  87. socket.on('setChuangan',function (sendData) {
  88. let data = sendProtocal.sendMsg(sendData,'chuangan','set')
  89. socketEvent.write(data)
  90. })
  91. // 发送检查驱动
  92. socket.on('checkQudong',function (sendData) {
  93. let data = sendProtocal.sendMsg(sendData,'qudong','get')
  94. socketEvent.write(data)
  95. })
  96. // 发送 设置驱动
  97. socket.on('setQudong',function (sendData) {
  98. let data = sendProtocal.sendMsg(sendData,'qudong','set')
  99. socketEvent.write(data)
  100. })
  101. // 发送检查功能
  102. socket.on('checkGongneng',function (sendData) {
  103. let data = sendProtocal.sendMsg(sendData,'gongneng','get')
  104. socketEvent.write(data)
  105. })
  106. // 发送 设置功能
  107. socket.on('setGongneng',function (sendData) {
  108. let data = sendProtocal.sendMsg(sendData,'gongneng','set')
  109. socketEvent.write(data)
  110. })
  111. // 发送检查设备
  112. socket.on('checkShebei',function (sendData) {
  113. let data = sendProtocal.sendMsg(sendData,'shebei','get')
  114. socketEvent.write(data)
  115. })
  116. // 发送设置设备
  117. socket.on('setShebei',function (sendData) {
  118. let data = sendProtocal.sendMsg(sendData,'shebei','set')
  119. socketEvent.write(data)
  120. })
  121. socket.on('sendBlueTouth',function (sendData) {
  122. blueTouth.sendData('123')
  123. })
  124. socket.on('searchModel',function (sendData) {
  125. let data = sendProtocal.searchModel();
  126. blueTouth.sendData(data)
  127. })
  128. socket.on('setModel',function (sendData) {
  129. let data = sendProtocal.inSetModel();
  130. blueTouth.sendData(data)
  131. })
  132. socket.on('InModel',function (sendData) {
  133. let data = sendProtocal.inTransModel();
  134. blueTouth.sendData(data)
  135. })
  136. // 蓝牙设置wifi账号密码
  137. socket.on('setWifiAccount',function (sendData) {
  138. let data = sendProtocal.blueToothSendCommand(
  139. sendData,'wifiAccount','set')
  140. blueTouth.sendData(data)
  141. })
  142. // 蓝牙获取wifi账号密码
  143. socket.on('getWifiAccount',function (sendData) {
  144. let data = sendProtocal.blueToothSendCommand(
  145. sendData,'wifiAccount','get')
  146. blueTouth.sendData(data)
  147. })
  148. // 蓝牙设置wifiIP
  149. socket.on('setWifiIp',function (sendData) {
  150. let data = sendProtocal.blueToothSendCommand(
  151. sendData,'wifiIp','set')
  152. blueTouth.sendData(data)
  153. })
  154. // 蓝牙获取wifi账号密码
  155. socket.on('getWifiIp',function (sendData) {
  156. let data = sendProtocal.blueToothSendCommand(
  157. sendData,'wifiIp','get')
  158. blueTouth.sendData(data)
  159. })
  160. // 蓝牙设置 服务端IP与端口
  161. socket.on('setServerIp',function (sendData) {
  162. let data = sendProtocal.blueToothSendCommand(
  163. sendData,'serverIp','set')
  164. blueTouth.sendData(data)
  165. })
  166. // 蓝牙获取wifi账号密码
  167. socket.on('getServerIp',function (sendData) {
  168. let data = sendProtocal.blueToothSendCommand(sendData,'serverIp','get')
  169. blueTouth.sendData(data)
  170. })
  171. socket.on('ble', function(request) {
  172. if (request.cmd === 'scan') {
  173. nobleWrapper.scan(function(resp) {
  174. io.sockets.emit('blueDeviceList', resp)
  175. })
  176. } else if(request.cmd === 'connectDevice') {
  177. // 连接蓝牙设备
  178. request.args.on_data_cb = function (data) {
  179. // 接收数据!
  180. if(receiveType == 'pressure') {
  181. if (lastBuffer !== null) {
  182. data = Buffer.concat([lastBuffer, data]);
  183. }
  184. let flag = 1;
  185. while(flag){
  186. if (data.length < frame_header_and_size_bytes) {
  187. lastBuffer = data;
  188. break;
  189. }
  190. let dataLength = data.readUIntLE(1, 2)
  191. let frameLength = frame_header_and_size_bytes + dataLength
  192. if(data.length > frameLength) {
  193. let frame = data.slice(0, frameLength);
  194. setAllCommand(frame)
  195. lastBuffer = data.slice(frameLength)
  196. if(lastBuffer.length >= frame_header_and_size_bytes) {
  197. data = lastBuffer
  198. continue;
  199. }
  200. } else if(data.length === frameLength) {
  201. setAllCommand(data)
  202. lastBuffer = null;
  203. } else {
  204. lastBuffer = data;
  205. }
  206. flag = 0;
  207. }
  208. } else {
  209. io.sockets.emit('pushBlueData', commonFunction.toBuffer(data).toString('hex'))
  210. }
  211. // io.sockets.emit('blueToothData', buffer)
  212. }
  213. nobleWrapper.connectDevice(request.args,function(resp) {
  214. io.sockets.emit('connectDeviceBack', resp)
  215. if(resp.ok) {
  216. nowConnectedBlueDeviceId = request.args.id
  217. } else {
  218. if(resp.msg == 'connectError') {
  219. // 连接错误
  220. console.log('server connectError')
  221. }
  222. }
  223. //io.sockets.emit('blueDeviceList', resp)
  224. })
  225. // nobleWrapper.connectDevice(request.args, request.args.successCallBack, request.args.errorCallBack);
  226. } else if (request.cmd ==='disConnectDevice') {
  227. // 断开蓝牙设备
  228. if(nowConnectedBlueDeviceId !='') {
  229. nobleWrapper.disConnectDevice(nowConnectedBlueDeviceId,function (resp) {
  230. io.sockets.emit('disConnectDeviceBack', resp)
  231. })
  232. }
  233. } else if(request.cmd ==='getCharServiceList') {
  234. // 获取CharServiceList
  235. if(nowConnectedBlueDeviceId !='') {
  236. nobleWrapper.getCharList(nowConnectedBlueDeviceId,function (info) {
  237. io.sockets.emit('pushCharacteristicServerList', info)
  238. })
  239. }
  240. } else if(request.cmd == 'setup') {
  241. if(nowConnectedBlueDeviceId !='') {
  242. receiveType = request.type
  243. nobleWrapper.setUp(request.data)
  244. }
  245. } else if(request.cmd == 'closeSetUp') {
  246. // 用户关闭dialog 并把CharacteristicServer 取消订阅 unsubscribe
  247. nobleWrapper.disSetUp()
  248. }
  249. });
  250. socket.on('sendBleData',function (data) {
  251. let sendData = ''
  252. switch (data.type) {
  253. case 'setZero' :
  254. sendData = sendProtocal.beginSetZero()
  255. break;
  256. case 'stopSetZero':
  257. sendData = sendProtocal.endSetZero()
  258. break;
  259. case 'startCollection':
  260. sendData = sendProtocal.startCollection()
  261. break;
  262. case 'stopCollection':
  263. sendData = sendProtocal.endCollection()
  264. break;
  265. case 'resetDevice':
  266. sendData = sendProtocal.resetDevice()
  267. break;
  268. case 'sendOwnData':
  269. sendData = data.data
  270. break;
  271. }
  272. nobleWrapper.write(sendData,function (res) {
  273. console.log(res)
  274. })
  275. })
  276. })
  277. const frame_header_and_size_bytes = 1 + 2;
  278. socketServer.on('connection',(socket)=>{
  279. socketEvent = socket
  280. socketServer.getConnections((err, count)=>{
  281. if(err){
  282. console.warn(err);
  283. } else {
  284. console.log(`当前有${count}个连接`);
  285. connectCount = count
  286. io.sockets.emit('connectCount', count);
  287. }
  288. });
  289. socket.on('data', (data)=>{
  290. if (lastBuffer !== null) {
  291. data = Buffer.concat([lastBuffer, data]);
  292. }
  293. let flag = 1;
  294. while(flag){
  295. if (data.length < frame_header_and_size_bytes) {
  296. lastBuffer = data;
  297. break;
  298. }
  299. let dataLength = data.readUIntLE(1, 2)
  300. let frameLength = frame_header_and_size_bytes + dataLength
  301. if(data.length > frameLength) {
  302. let frame = data.slice(0, frameLength);
  303. setAllCommand(frame)
  304. lastBuffer = data.slice(frameLength)
  305. if(lastBuffer.length >= frame_header_and_size_bytes) {
  306. data = lastBuffer
  307. continue;
  308. }
  309. } else if(data.length === frameLength) {
  310. setAllCommand(data)
  311. lastBuffer = null;
  312. } else {
  313. lastBuffer = data;
  314. }
  315. flag = 0;
  316. }
  317. });
  318. socket.on('error', (err)=>{
  319. console.warn(err);
  320. socket.destroy();
  321. });
  322. socket.on('end', ()=>{
  323. io.sockets.emit('disConnectOneClient');
  324. });
  325. })
  326. // 处理所有指令(根据帧头判断业务逻辑)
  327. function setAllCommand(validateData) {
  328. let validNumByteArray = new Uint8Array(validateData);
  329. let allCommond = commonFunction.buf2hex(validNumByteArray);
  330. logger.info(allCommond)
  331. let commond = allCommond.substring(0,2)
  332. // 判断 帧头 e5 足压扫描
  333. if(commond == 'e5') {
  334. footPressure(validNumByteArray)
  335. }
  336. }
  337. var col = 0
  338. var tempPressureData = []
  339. // 处理足压数据
  340. function footPressure(validNumByteArray) {
  341. let offset = 10;
  342. let nowCol = validNumByteArray[8];
  343. //console.log(nowCol)
  344. if(nowCol== 51) {
  345. tempPressureData[col] = validNumByteArray.slice(offset)
  346. io.sockets.emit('pressureData',commonFunction.setPressureData(tempPressureData));
  347. tempPressureData = [];
  348. col = 0
  349. }
  350. if(nowCol >= col) {
  351. tempPressureData[nowCol] = validNumByteArray.slice(offset)
  352. col = nowCol;
  353. } else {
  354. io.sockets.emit('pressureData',commonFunction.setPressureData(tempPressureData));
  355. tempPressureData = [];
  356. tempPressureData[nowCol] = validNumByteArray.slice(offset)
  357. col = nowCol;
  358. // col = 0
  359. }
  360. }
  361. module.exports = server