deviceConnect3.js 6.5 KB

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