service.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. module.exports = {
  2. updateDeviceList: updateDeviceList,
  3. }
  4. ///更新列表排序
  5. function updateDeviceList(deviceList, isInit, closeAllBlue) {
  6. const strings = require('../strings');
  7. if (strings.isEmpty(deviceList)) {
  8. return [];
  9. }
  10. const app = getApp();
  11. const store = require('../store');
  12. var finalList = [];
  13. if (isInit) {
  14. deviceList[0].state = "offline";
  15. }
  16. /// 让所有蓝牙设备离线
  17. if (closeAllBlue) {
  18. deviceList.forEach(element => {
  19. if (element.connectType != 3) {
  20. element.state = "offline";
  21. }
  22. });
  23. }
  24. ///挑选出第一个在线设备
  25. var isFirstOnline = false;
  26. if (deviceList[0].state == "online") {
  27. isFirstOnline = true;
  28. finalList.push(deviceList[0]);
  29. }
  30. ///区分在线和离线
  31. for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
  32. if (isInit) {
  33. deviceList[i].state = "offline";
  34. }
  35. }
  36. var onLineList = [];
  37. var onNoLineList = [];
  38. ///添加在线的
  39. for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
  40. var device = deviceList[i];
  41. if (device.state == "online") {
  42. onLineList.push(device)
  43. }
  44. }
  45. ///添加离线的
  46. for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
  47. var device = deviceList[i];
  48. if (device.state != "online") {
  49. onNoLineList.push(device)
  50. }
  51. }
  52. // 区分在线wifi和蓝牙 wifi在前 离线在后
  53. var onLineWifiList = [];
  54. var onLineBlueList = [];
  55. ///添加在线wifi
  56. onLineList.forEach(element => {
  57. if (element.connectType == 3) {
  58. onLineWifiList.push(element);
  59. }
  60. });
  61. ///添加在线蓝牙
  62. onLineList.forEach(element => {
  63. if (element.connectType != 3) {
  64. onLineBlueList.push(element)
  65. }
  66. });
  67. finalList = finalList.concat(onLineWifiList);
  68. finalList = finalList.concat(onLineBlueList);
  69. ///只需要蓝牙和wifi在线的
  70. var mDeviceList = []
  71. mDeviceList = mDeviceList.concat(onLineWifiList);
  72. mDeviceList = mDeviceList.concat(onLineBlueList);
  73. app.globalData.mDeviceList = mDeviceList;
  74. ///区分离线wifi和蓝牙 wifi在前 离线在后
  75. var onNoLineWifiList = [];
  76. var onNoLineBlueList = [];
  77. ///添加离线wifi
  78. onNoLineList.forEach(element => {
  79. if (element.connectType == 3) {
  80. onNoLineWifiList.push(element)
  81. }
  82. });
  83. ///添加离线蓝牙
  84. onNoLineList.forEach(element => {
  85. if (element.connectType != 3) {
  86. onNoLineBlueList.push(element)
  87. }
  88. });
  89. finalList = finalList.concat(onNoLineWifiList);
  90. finalList = finalList.concat(onNoLineBlueList);
  91. store.setStore("deviceList", finalList);
  92. return finalList;
  93. };