service.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. module.exports = {
  2. addWifiDevice: addWifiDevice,
  3. updateDeviceList: updateDeviceList,
  4. }
  5. // 新添加Wifi设备 猫王小王子OTR-X
  6. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
  7. function addWifiDevice(device, deviceList) {
  8. // 同一个设备处理
  9. var tempList = deviceList.filter((v) => v.deviceId === device.deviceId);
  10. const strings = require('../strings');
  11. if (!strings.isEmpty(tempList)) {
  12. deviceList = deviceList.filter((v) => v.deviceId !== device.deviceId);
  13. }
  14. /// 添加到第一个
  15. deviceList.unshift({
  16. connectType: 3,
  17. devName: "",
  18. state: "online",
  19. name: device.deviceId,
  20. deviceId: device.deviceId,
  21. mac: device.deviceId,
  22. image: "./../../img/min.png",
  23. });
  24. return deviceList;
  25. };
  26. ///更新列表排序
  27. function updateDeviceList(deviceList, isInit, closeAllBlue) {
  28. const strings = require('../strings');
  29. if (strings.isEmpty(deviceList)) {
  30. return [];
  31. }
  32. const app = getApp();
  33. var finalList = [];
  34. if (isInit) {
  35. deviceList[0].state = "offline";
  36. }
  37. /// 让所有蓝牙设备离线
  38. if (closeAllBlue) {
  39. deviceList.forEach(element => {
  40. if (element.connectType != 3) {
  41. element.state = "offline";
  42. }
  43. });
  44. }
  45. ///挑选出第一个在线设备
  46. var isFirstOnline = false;
  47. if (deviceList[0].state == "online") {
  48. isFirstOnline = true;
  49. finalList.push(deviceList[0]);
  50. }
  51. ///区分在线和离线
  52. for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
  53. if (isInit) {
  54. deviceList[i].state = "offline";
  55. }
  56. }
  57. var onLineList = [];
  58. var onNoLineList = [];
  59. ///添加在线的
  60. for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
  61. var device = deviceList[i];
  62. if (device.state == "online") {
  63. onLineList.push(device)
  64. }
  65. }
  66. ///添加离线的
  67. for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
  68. var device = deviceList[i];
  69. if (device.state != "online") {
  70. onNoLineList.push(device)
  71. }
  72. }
  73. // 区分在线wifi和蓝牙 wifi在前 离线在后
  74. var onLineWifiList = [];
  75. var onLineBlueList = [];
  76. ///添加在线wifi
  77. onLineList.forEach(element => {
  78. if (element.connectType == 3) {
  79. onLineWifiList.push(element);
  80. }
  81. });
  82. ///添加在线蓝牙
  83. onLineList.forEach(element => {
  84. if (element.connectType != 3) {
  85. onLineBlueList.push(element)
  86. }
  87. });
  88. finalList = finalList.concat(onLineWifiList);
  89. finalList = finalList.concat(onLineBlueList);
  90. ///只需要蓝牙和wifi在线的
  91. var mDeviceList = []
  92. mDeviceList = mDeviceList.concat(onLineWifiList);
  93. mDeviceList = mDeviceList.concat(onLineBlueList);
  94. app.globalData.mDeviceList = mDeviceList;
  95. ///区分离线wifi和蓝牙 wifi在前 离线在后
  96. var onNoLineWifiList = [];
  97. var onNoLineBlueList = [];
  98. ///添加离线wifi
  99. onNoLineList.forEach(element => {
  100. if (element.connectType == 3) {
  101. onNoLineWifiList.push(element)
  102. }
  103. });
  104. ///添加离线蓝牙
  105. onNoLineList.forEach(element => {
  106. if (element.connectType != 3) {
  107. onNoLineBlueList.push(element)
  108. }
  109. });
  110. finalList = finalList.concat(onNoLineWifiList);
  111. finalList = finalList.concat(onNoLineBlueList);
  112. return finalList;
  113. };