deviceConnect3.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. // 同一个设备
  109. const devicelist = arr.filter((v) => v.deviceId === _this.data.name);
  110. if (devicelist.length > 0) {
  111. arr = arr.filter((v) => v.deviceId !== _this.data.name);
  112. };
  113. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
  114. arr.unshift({
  115. /// Wifi mqtt-连接
  116. connectType: 3,
  117. deviceId: _this.data.name,
  118. name: _this.data.name,
  119. state: "online"
  120. });
  121. getCurrentPages()[0].addConnectWifiDevice(arr);
  122. _this.setData({
  123. percent: 100,
  124. ruterStatus: 1
  125. });
  126. // wx.setStorage({
  127. // key: "devicelist",
  128. // data: JSON.stringify(arr),
  129. // success() {
  130. // _this.setData({
  131. // percent: 100,
  132. // ruterStatus: 1
  133. // });
  134. // }
  135. // })
  136. }
  137. },
  138. fail() {
  139. wx.setStorage({
  140. key: "devicelist",
  141. data: JSON.stringify([{
  142. name: _this.data.name,
  143. deviceId: _this.data.name,
  144. state: "online"
  145. }]),
  146. success() {
  147. _this.setData({
  148. percent: 100,
  149. ruterStatus: 1
  150. });
  151. }
  152. })
  153. }
  154. })
  155. // 记住密码
  156. wx.setStorage({
  157. key: "wifiInfo",
  158. data: JSON.stringify({
  159. "password": app.globalData.pwdData,
  160. "ssid": app.globalData.ssid
  161. })
  162. });
  163. }
  164. }
  165. break;
  166. case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT:
  167. wx.hideLoading();
  168. // console.log("初始化结果:", JSON.stringify(options))
  169. if (options.result) {
  170. console.log('初始化成功');
  171. _this.OnClickStart();
  172. } else {
  173. console.log('初始化失败')
  174. _this.setData({
  175. connected: false
  176. })
  177. _this.setData({
  178. ruterStatus: 2
  179. });
  180. }
  181. break;
  182. }
  183. },
  184. OnClickStart: function () {
  185. if (!app.globalData.ssid) {
  186. wx.showToast({
  187. title: 'SSID不能为空',
  188. icon: 'none'
  189. })
  190. return
  191. }
  192. if (!app.globalData.pwdData) {
  193. wx.showToast({
  194. title: '密码不能为空',
  195. icon: 'none'
  196. })
  197. return;
  198. }
  199. xBlufi.notifySendRouterSsidAndPassword({
  200. ssid: app.globalData.ssid,
  201. password: app.globalData.pwdData
  202. });
  203. },
  204. bindPasswordInput: function (e) {
  205. this.setData({
  206. password: e.detail.value
  207. })
  208. },
  209. bindCustomDataInput: function (e) {
  210. this.setData({
  211. customData: e.detail.value
  212. })
  213. },
  214. egen() {
  215. wx.navigateBack({
  216. delta: 1
  217. })
  218. },
  219. goIndex() {
  220. // BLUFI_7cdfa1fd3cfc
  221. app.globalData.newDeviceId = this.data.name;
  222. wx.redirectTo({
  223. url: '/pages/index/index'
  224. });
  225. }
  226. })