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