deviceConnect3.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // pages/deviceConnect3/deviceConnect3.ts
  2. const app = getApp();
  3. let xBlufi = require("../../utils/blufi/xBlufi.js");
  4. import route_constant from '../../utils/route_constant.js'
  5. import route_util from '../../utils/route_util.js';
  6. let percentIn = null;
  7. let _this = null;
  8. let errTi = null;
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. nvabarData: {
  15. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  16. title: '连接配网', //导航栏 中间的标题
  17. },
  18. scopeBluetooth: app.globalData.scopeBluetooth,
  19. ssid: app.globalData.ssid,
  20. password: app.globalData.pwdData,
  21. version: '2.0',
  22. name: '',
  23. connectedDeviceId: '',
  24. connected: true,
  25. deviceInfo: null,
  26. isInitOK: false,
  27. customData: '',
  28. percent: 0,
  29. ruterStatus: 0, // 0 连接中 // 1 成功 // 2失败
  30. },
  31. onShow: function (options) {},
  32. onLoad: function (options) {
  33. _this = this;
  34. _this.setData({
  35. name: options.name,
  36. connectedDeviceId: options.deviceId,
  37. })
  38. xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
  39. xBlufi.notifyInitBleEsp32({
  40. deviceId: options.deviceId,
  41. });
  42. percentIn = setInterval(() => {
  43. if (_this.data.percent === 100) {
  44. clearInterval(percentIn);
  45. _this.setData({
  46. ruterStatus: 2
  47. });
  48. return;
  49. };
  50. _this.data.percent++;
  51. _this.setData({
  52. percent: _this.data.percent
  53. });
  54. }, 600);
  55. },
  56. onUnload: function () {
  57. // 关闭蓝牙连接
  58. let _this = this
  59. xBlufi.notifyConnectBle({
  60. isStart: false,
  61. deviceId: _this.data.connectedDeviceId,
  62. name: _this.data.name,
  63. });
  64. xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
  65. clearTimeout(errTi);
  66. clearInterval(percentIn);
  67. },
  68. funListenDeviceMsgEvent: function (options) {
  69. let _this = this;
  70. switch (options.type) {
  71. case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
  72. _this.setData({
  73. connected: options.result
  74. });
  75. if (!options.result) {
  76. wx.showModal({
  77. title: '很抱歉提醒你!',
  78. content: '小程序与设备异常断开',
  79. showCancel: false, //是否显示取消按钮
  80. success: function (res) {
  81. wx.navigateBack({
  82. delta: 1
  83. });
  84. },
  85. })
  86. }
  87. break;
  88. case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_ROUTER_RESULT:
  89. wx.hideLoading();
  90. if (!options.result)
  91. errTi = setTimeout(() => {
  92. _this.setData({
  93. ruterStatus: 2
  94. });
  95. clearTimeout(errTi);
  96. clearInterval(percentIn);
  97. }, 5000);
  98. else {
  99. // 配网成功
  100. if (options.data.progress == 100) {
  101. clearTimeout(errTi);
  102. clearInterval(percentIn);
  103. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
  104. getCurrentPages()[0].addConnectWifiDevice({
  105. deviceId: _this.data.name,
  106. });
  107. _this.setData({
  108. percent: 100,
  109. ruterStatus: 1
  110. });
  111. // 记住密码
  112. wx.setStorage({
  113. key: "wifiInfo",
  114. data: JSON.stringify({
  115. "password": app.globalData.pwdData,
  116. "ssid": app.globalData.ssid
  117. })
  118. });
  119. }
  120. }
  121. break;
  122. case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT:
  123. wx.hideLoading();
  124. // console.log("初始化结果:", JSON.stringify(options))
  125. if (options.result) {
  126. console.log('初始化成功');
  127. _this.OnClickStart();
  128. } else {
  129. console.log('初始化失败')
  130. _this.setData({
  131. connected: false
  132. })
  133. _this.setData({
  134. ruterStatus: 2
  135. });
  136. }
  137. break;
  138. }
  139. },
  140. OnClickStart: function () {
  141. if (!app.globalData.ssid) {
  142. wx.showToast({
  143. title: 'SSID不能为空',
  144. icon: 'none'
  145. })
  146. return
  147. }
  148. if (!app.globalData.pwdData) {
  149. wx.showToast({
  150. title: '密码不能为空',
  151. icon: 'none'
  152. })
  153. return;
  154. }
  155. xBlufi.notifySendRouterSsidAndPassword({
  156. ssid: app.globalData.ssid,
  157. password: app.globalData.pwdData
  158. });
  159. },
  160. bindPasswordInput: function (e) {
  161. this.setData({
  162. password: e.detail.value
  163. })
  164. },
  165. bindCustomDataInput: function (e) {
  166. this.setData({
  167. customData: e.detail.value
  168. })
  169. },
  170. egen() {
  171. wx.navigateBack({
  172. delta: 1
  173. })
  174. },
  175. goIndex() {
  176. // BLUFI_7cdfa1fd3cfc
  177. app.globalData.newDeviceId = this.data.name;
  178. route_util.goBackRoute(route_constant.indexRoot);
  179. }
  180. })