deviceConnect3.js 6.4 KB

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