message.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. module.exports = {
  2. subscribeSingleDevice: subscribeSingleDevice,
  3. subscribeAllDevice: subscribeAllDevice,
  4. onlineDevice: onlineDevice,
  5. }
  6. // 订阅在线单个设备
  7. function subscribeSingleDevice(deviceId) {
  8. var topic = `/AIrSMArT_${deviceId.split("BLUFI_")[1]}/status/onoffline`;
  9. const app = getApp();
  10. app.unsubscribe(topic);
  11. app.subscribe(topic);
  12. };
  13. // 订阅在线设备
  14. function subscribeAllDevice() {
  15. const strings = require('../strings');
  16. var deviceList = getCurrentPages()[0].getDeviceList();
  17. if (strings.isEmpty(deviceList)) {
  18. return;
  19. }
  20. const app = getApp();
  21. ///扫描所有在线Wifi设备
  22. for (var i = 0; i < deviceList.length; i++) {
  23. var device = deviceList[i];
  24. if (device.connectType == 3) {
  25. var deviceId = device.deviceId;
  26. var topic = `/AIrSMArT_${deviceId.split("BLUFI_")[1]}/status/onoffline`;
  27. app.subscribe(topic);
  28. // break;
  29. }
  30. }
  31. };
  32. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  33. ///连上就调用2次 处理离线在线问题 wifi设备 BLUFI_
  34. /// payloads:{"uuid":"AIrSMArT_7cdfa1fcbb24","state":"online","userid":"1"}
  35. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  36. ///更新在线状态,连接第一个在线设备
  37. // deviceId: BLUFI_7cdfa1fd3af0
  38. // uuid: AIrSMArT_7cdfa1fd3af0
  39. function onlineDevice(payloads, changeCallback) {
  40. var isChanged = false;
  41. const strings = require('../strings');
  42. var deviceList = getCurrentPages()[0].getDeviceList();
  43. /// 处理在线设备
  44. if (!strings.isEmpty(deviceList)) {
  45. for (var i = 0; i < deviceList.length; i++) {
  46. if (payloads && payloads.uuid) {
  47. var device = deviceList[i];
  48. var connectType = device.connectType;
  49. if (connectType == 3) {
  50. var deviceId = device.deviceId;
  51. var splitDeviceId = deviceId.split("BLUFI_");
  52. if (splitDeviceId.length == 2) {
  53. var index = payloads.uuid.indexOf(splitDeviceId[1]);
  54. if (index !== -1) {
  55. if (device.state != payloads.state) {
  56. isChanged = true;
  57. device.state = payloads.state;
  58. }
  59. break;
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. ///更新数据
  67. if (isChanged) {
  68. changeCallback(deviceList);
  69. }
  70. ///是否已登录
  71. var isLogin = getCurrentPages()[0].getIsLogin();
  72. if (!isLogin) {
  73. return;
  74. }
  75. ///当前没有连接设备,则去连接第一个wifi设备
  76. var list = getCurrentPages()[0].getDeviceList();
  77. var deviceListSelect = getCurrentPages()[0].getDeviceListSelect();
  78. ///去连接第一个
  79. if (deviceListSelect === null) {
  80. if (!strings.isEmpty(list)) {
  81. var deviceList = getCurrentPages()[0].getDeviceList();
  82. var autoConnected = getCurrentPages()[0].getAutoConnected();
  83. var connectDeviceIding = getCurrentPages()[0].getConnectDeviceIding();
  84. /// 还没有自动连接采用第一个
  85. if (!autoConnected) {
  86. for (var i = 0; i < list.length; i++) {
  87. var device = list[i];
  88. var deviceId = device.deviceId;
  89. if (device.connectType == 3 && device.state === "online") {
  90. connectDeviceIding = deviceId;
  91. break;
  92. }
  93. }
  94. }
  95. for (var i = 0; i < list.length; i++) {
  96. var device = list[i];
  97. var deviceId = device.deviceId;
  98. if (deviceId == connectDeviceIding) {
  99. if (device.connectType == 3 && device.state === "online") {
  100. getCurrentPages()[0].actionDevice(device);
  101. }
  102. break;
  103. }
  104. }
  105. }
  106. } else {
  107. // 当前播放设备离线
  108. if (list.length > deviceListSelect && list[deviceListSelect].state !== "online") {
  109. that.setData({
  110. actionIndex: null,
  111. deviceListSelect: null,
  112. });
  113. };
  114. }
  115. };