随手精灵小程序,单独为了获取WIFI信息加强围栏告警功能能力
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

552 lines
18KB

  1. // 示例demo
  2. const plugin = requirePlugin('ppScale-plugin')
  3. var app = getApp();
  4. Page({
  5. data: {
  6. // 选择的设备对象
  7. selmac: null,
  8. // 是否在测量中
  9. scaleing: false,
  10. // 测量心率测量状态
  11. state: 0,
  12. // 体重数据
  13. weight: '00.0',
  14. // 测量单位
  15. unit: 'kg',
  16. // 蓝牙连接状态
  17. devLink: false,
  18. // 体重数据
  19. currentWeight: 0,
  20. // 阻抗数据
  21. currentImpedance: 0,
  22. // 扫描筛选出的设备
  23. selectDev: null,
  24. // 设备模型
  25. deviceModel: null,
  26. // 锁定数据
  27. lastData: null,
  28. // 域名值
  29. domainValue: '',
  30. // wifi名
  31. wifiValue: '',
  32. // 密码值
  33. pwValue: '',
  34. // 单位数组
  35. deviceUnitArray: Object.keys(plugin.PPBluetoothDefine.PPDeviceUnit),
  36. // 单位数组下标
  37. deviceUnitIndex: 0,
  38. // 网络dialog
  39. netWorkFlag: false,
  40. // 域名dialog
  41. domainFlag: false,
  42. // 单位dialog
  43. unitFlag: false,
  44. // 调试log列表
  45. consoleList: [],
  46. // 获取设备数据传参
  47. param: {
  48. appKey:"lefu7afd0a504245139b"
  49. }
  50. },
  51. /**
  52. * 页面卸载时调用
  53. */
  54. onUnload() {
  55. // 业务结束后需要调用此方法
  56. plugin.Blue.stop()
  57. },
  58. /**
  59. * 页面隐藏时调用
  60. */
  61. onHide() {
  62. // 业务结束后需要调用此方法
  63. this.seeConsole('销毁蓝牙插件')
  64. plugin.Blue.stop()
  65. },
  66. onShow() {
  67. // 高于1.0.5版本 如无法扫描设备 需请求获取并设置最新设备配置列表
  68. this.getDeviceSettingList(this.data.param)
  69. // 高于1.0.5版本 如无法扫描设备 直接设置最新设备列表
  70. // plugin.Blue.setDeviceSetting([{"createBy":null,"createTime":"2023-09-14 11:38:56","updateBy":null,"updateTime":"2023-10-18 16:08:39","remark":null,"id":12,"deviceName":"LFSmart Scale","sign":"CF4303","advLength":20,"macAddressStart":7,"deviceConnectType":2,"deviceType":1,"deviceProtocolType":2,"deviceCalcuteType":3,"devicePowerType":2,"deviceFuncType":75,"deviceAccuracyType":2,"deviceUnitType":"0,3","calorieStatus":1,"uhStatus":1,"status":0,"imgUrl":"https://unique.lefuenergy.com/dev/2023/09/28/4dianji_border_20230928102849A007.png"}])
  71. // 使用节流函数包装测重事件处理函数
  72. let throttledScroll = this.throttle(this.handleProgress, 1500);
  73. // 记录设备对象
  74. this.setData({
  75. selmac: app.globalData.selmac
  76. })
  77. // 蓝牙状态监听
  78. wx.onBluetoothAdapterStateChange((res) => {
  79. if (!res.available) {
  80. this.seeConsole('蓝牙设备断开')
  81. }
  82. })
  83. // 是否开启插件日志
  84. // plugin.Blue.visibleLog()
  85. this.seeConsole('开启蓝牙开始扫描设备')
  86. // 获取扫描设备列表
  87. plugin.bus.subscribe("devicesList", (devList) => {
  88. // 根据deviceId查找匹配设备
  89. let selectDev = devList.find(item => {
  90. return item.deviceId == this.data.selmac.deviceId
  91. })
  92. // 记录选择出设备
  93. this.setData({
  94. selectDev: selectDev
  95. })
  96. if (!this.data.deviceModel && this.data.selectDev) {
  97. //获取与保存秤类型
  98. this.seeConsole('获取秤端设备模型')
  99. // 获取与记录设备模型
  100. this.setData({
  101. deviceModel: plugin.Blue.getDeviceModel(selectDev)
  102. })
  103. }
  104. // 匹配广播秤
  105. if (this.data.deviceModel && this.data.deviceModel.deviceConnectType == plugin.PPBluetoothDefine.PPDeviceConnectType
  106. .PPDeviceConnectTypeBleAdv) {
  107. this.seeConsole('设备连接广播秤')
  108. // 广播秤 匹配与数据监听
  109. plugin.Blue.setbroadcastDev(selectDev)
  110. }
  111. // 匹配连接秤
  112. if (this.data.deviceModel && this.data.deviceModel.deviceConnectType == plugin.PPBluetoothDefine.PPDeviceConnectType
  113. .PPDeviceConnectTypeBleConnect) {
  114. // 停止扫描
  115. this.seeConsole('停止扫描')
  116. plugin.Blue.stopBluetoothDevicesDiscovery()
  117. // 连接设备
  118. this.seeConsole('开始连接设备')
  119. plugin.Blue.createBLEConnection(selectDev)
  120. }
  121. });
  122. // 设备链接回调
  123. plugin.bus.subscribe("deviceConnect", (res) => {
  124. this.seeConsole('成功连接设备')
  125. // 开启监听蓝牙秤数据 必填
  126. plugin.ScaleAction.startDataProgress()
  127. setTimeout(()=>{
  128. plugin.ScaleAction.syncDeviceSetting({
  129. unit: +this.data.deviceUnitIndex,
  130. })
  131. },800)
  132. });
  133. // 设备链接回调
  134. plugin.bus.subscribe("devicesModel", (res) => {
  135. // console.log("res",res)
  136. });
  137. // 监听秤端数据回调
  138. plugin.bus.subscribe("progressData", (res) => {
  139. throttledScroll()
  140. // console.log("selectDev",this.data.selectDev)
  141. // console.log("selectDev",plugin.Blue.getDeviceModel(this.data.selectDev))
  142. if (this.data.selectDev) {
  143. // 广播秤类型连接
  144. if (this.data.deviceModel && this.data.deviceModel.deviceConnectType == plugin.PPBluetoothDefine.PPDeviceConnectType
  145. .PPDeviceConnectTypeBleAdv) {
  146. this.setData({
  147. devLink: true
  148. })
  149. }
  150. if (res.weight > 0) {
  151. // 有数据显示为测量中
  152. this.setData({
  153. scaleing: true
  154. })
  155. // 记录体重数据
  156. this.setData({
  157. weight: (res.weight / 100).toFixed(2)
  158. })
  159. // 获取测量过程秤显示单位
  160. this.setData({
  161. unit: this.getKeyByUnit(res.unit)
  162. })
  163. } else {
  164. // 无数据显示为未测量
  165. this.setData({
  166. state: 0
  167. })
  168. // 无数据显示为未测量
  169. this.setData({
  170. scaleing: false
  171. })
  172. }
  173. if (res.isHeartRating) {
  174. // 心率测量中状态设置
  175. this.setData({
  176. state: 1
  177. })
  178. }
  179. }
  180. });
  181. // 设备自动断开监听
  182. plugin.bus.subscribe("deviceWillDisconnect", (res) => {
  183. this.seeConsole('设备自动断开连接')
  184. // 设备自动断连后重连
  185. this.setData({
  186. devLink: false
  187. })
  188. // 重新连接方法
  189. this.reconnectDevice()
  190. })
  191. // 锁定数据
  192. plugin.bus.subscribe("lockData", (res) => {
  193. this.seeConsole('锁定测量数据:' + JSON.stringify(res))
  194. // 测量结束
  195. this.setData({
  196. currentWeight: (res.weight / 100).toFixed(2)
  197. })
  198. // 记录阻抗值
  199. this.setData({
  200. currentImpedance: res.resistance
  201. })
  202. // 记录测量结果数据
  203. this.setData({
  204. lastData: JSON.stringify(res)
  205. })
  206. // 状态还原
  207. this.setData({
  208. state: 0
  209. })
  210. this.setData({
  211. scaleing: false
  212. })
  213. });
  214. // 蓝牙连接状态监听
  215. plugin.bus.subscribe("connectState", (res) => {
  216. // 连接状态
  217. if (this.data.selectDev) {
  218. if (res == plugin.BLUE_STATE.TRANSMITDATA || res == plugin.BLUE_STATE.LOCKDATA || res == plugin.BLUE_STATE
  219. .HEARTRATING || res ==
  220. plugin.BLUE_STATE.CONNECTSUCCESS || res == plugin.BLUE_STATE.WIFISUCCESS) {
  221. this.setData({
  222. devLink: true
  223. })
  224. } else {
  225. this.setData({
  226. devLink: false
  227. })
  228. }
  229. // 连接失败则重新连接
  230. if (res == plugin.BLUE_STATE.CONNECTFAILED) {
  231. this.reconnectDevice()
  232. }
  233. }
  234. });
  235. // 秤端操作回调
  236. plugin.bus.subscribe("syncDeviceSetting", (res) => {
  237. this.seeConsole('成功配置设备')
  238. })
  239. plugin.bus.subscribe("snData", () => {
  240. this.seeConsole('成功配置wifi网络')
  241. })
  242. plugin.bus.subscribe("domainChange", () => {
  243. this.seeConsole('成功更新秤端请求服务器域名')
  244. })
  245. plugin.bus.subscribe("syncDeviceTimeSuccess", () => {
  246. this.seeConsole('成功同步时间')
  247. })
  248. plugin.bus.subscribe("syncDeviceHistorySuccess", (e) => {
  249. console.log("历史列表",e)
  250. this.seeConsole('成功同步历史')
  251. })
  252. plugin.bus.subscribe("deviceInfo", (res) => {
  253. console.log("deviceInfo", res)
  254. this.seeConsole('设备信息')
  255. })
  256. plugin.bus.subscribe("deleteDeviceHistoryDataSuccess", () => {
  257. this.seeConsole('成功删除同步历史')
  258. })
  259. // 开始扫描
  260. plugin.Blue.start([], false)
  261. },
  262. connect() {
  263. this.seeConsole('开始连接设备')
  264. if (this.data.deviceModel && this.data.deviceModel.deviceConnectType == plugin.PPBluetoothDefine.PPDeviceConnectType
  265. .PPDeviceConnectTypeBleConnect) {
  266. // 连接设备
  267. plugin.Blue.createBLEConnection(this.data.selectDev)
  268. }
  269. },
  270. disconnect() {
  271. this.seeConsole('断开连接')
  272. plugin.Blue.disconnect()
  273. },
  274. switchUnit() {
  275. this.setData({
  276. unitFlag: true
  277. })
  278. },
  279. setUserMes() {
  280. this.seeConsole('下发用户信息')
  281. if (this.data.devLink) {
  282. // 根据设备已有单位切换
  283. plugin.ScaleAction.syncDeviceSetting({
  284. age: 28,
  285. height: 167,
  286. gender: 1,
  287. })
  288. } else {
  289. this.seeConsole('停止下发用户信息')
  290. wx.showToast({
  291. title: '需要连接蓝牙秤',
  292. icon: 'error',
  293. duration: 1000,
  294. mask: true
  295. })
  296. }
  297. },
  298. syncHistory() {
  299. this.seeConsole('同步历史')
  300. if (this.data.devLink) {
  301. // 根据设备已有单位切换
  302. plugin.ScaleAction.fetchDeviceHistoryData()
  303. } else {
  304. this.seeConsole('停止同步历史')
  305. wx.showToast({
  306. title: '需要连接蓝牙秤',
  307. icon: 'error',
  308. duration: 1000,
  309. mask: true
  310. })
  311. }
  312. },
  313. deleteSyncHistory() {
  314. this.seeConsole('删除同步历史')
  315. if (this.data.devLink) {
  316. // 根据设备已有单位切换
  317. plugin.ScaleAction.deleteDeviceHistoryData()
  318. } else {
  319. this.seeConsole('停止删除同步历史')
  320. wx.showToast({
  321. title: '需要连接蓝牙秤',
  322. icon: 'error',
  323. duration: 1000,
  324. mask: true
  325. })
  326. }
  327. },
  328. syncTime() {
  329. this.seeConsole('同步时间')
  330. if (this.data.devLink) {
  331. // 根据设备已有单位切换
  332. plugin.ScaleAction.startDataProgress()
  333. } else {
  334. this.seeConsole('停止同步时间')
  335. wx.showToast({
  336. title: '需要连接蓝牙秤',
  337. icon: 'error',
  338. duration: 1000,
  339. mask: true
  340. })
  341. }
  342. },
  343. configureNetwork() {
  344. this.setData({
  345. netWorkFlag: true
  346. })
  347. },
  348. modifyDNS() {
  349. this.setData({
  350. domainFlag: true
  351. })
  352. },
  353. /**
  354. * 重新连接设备
  355. */
  356. reconnectDevice() {
  357. this.seeConsole('尝试重新连接')
  358. // 设备断开连接后重新连接
  359. plugin.Blue.disconnect((disres) => {
  360. if (disres.errCode == 0) {
  361. this.seeConsole('开始扫描设备')
  362. plugin.Blue.startBluetoothDevicesDiscovery()
  363. } else {
  364. this.reconnectDevice()
  365. }
  366. })
  367. },
  368. /**
  369. * 根据单位值获取设备单位描述
  370. * @param {number} unit 单位值
  371. * @return {string} 单位描述
  372. */
  373. getKeyByUnit(unit) {
  374. for (const key in plugin.PPBluetoothDefine.PPDeviceUnit) {
  375. if (plugin.PPBluetoothDefine.PPDeviceUnit[key] === unit) {
  376. return key;
  377. }
  378. }
  379. return null;
  380. },
  381. bindKeyInput: function (e) {
  382. this.setData({
  383. inputValue: e.detail.value
  384. })
  385. },
  386. bindDomainInput: function (e) {
  387. this.setData({
  388. domainValue: e.detail.value
  389. })
  390. },
  391. bindWifiInput: function (e) {
  392. this.setData({
  393. wifiValue: e.detail.value
  394. })
  395. },
  396. bindPasswdInput: function (e) {
  397. this.setData({
  398. pwValue: e.detail.value
  399. })
  400. },
  401. bindPickerChange: function (e) {
  402. this.setData({
  403. deviceUnitIndex: e.detail.value
  404. });
  405. },
  406. changeUnit() {
  407. this.seeConsole('切换单位')
  408. if (this.data.devLink) {
  409. // 根据设备已有单位切换
  410. plugin.ScaleAction.syncDeviceSetting({
  411. unit: +this.data.deviceUnitIndex,
  412. })
  413. } else {
  414. this.seeConsole('停止切换单位')
  415. wx.showToast({
  416. title: '需要连接蓝牙秤',
  417. icon: 'error',
  418. duration: 1000,
  419. mask: true
  420. })
  421. }
  422. this.setData({
  423. unitFlag: false
  424. })
  425. },
  426. configWifi() {
  427. this.seeConsole('开始配置wifi')
  428. // 配置wifi
  429. if (this.data.devLink && this.data.wifiValue && this.data.pwValue) {
  430. console.log("this.data.wifiValue && this.data.pwValue",this.data.wifiValue , this.data.pwValue)
  431. plugin.ScaleAction.configWifi(this.data.wifiValue, this.data.pwValue)
  432. } else {
  433. this.seeConsole('停止配置wifi')
  434. wx.showToast({
  435. title: '需要连接蓝牙秤或wifi名称或密码',
  436. duration: 1000,
  437. mask: true
  438. })
  439. }
  440. this.setData({
  441. netWorkFlag: false
  442. })
  443. },
  444. changeDomain() {
  445. this.seeConsole('开始配置域名')
  446. // 配置域名
  447. if (this.data.domainValue != '' && this.data.devLink) {
  448. plugin.ScaleAction.configDomain(this.data.domainValue.replace(/^(https?:\/\/)/, ""))
  449. } else {
  450. this.seeConsole('停止配置域名')
  451. wx.showToast({
  452. title: '需要连接蓝牙秤或输入域名',
  453. duration: 1000,
  454. mask: true
  455. })
  456. }
  457. this.setData({
  458. domainFlag: false
  459. })
  460. },
  461. // 节流函数
  462. throttle: function (func, delay) {
  463. let timer = null;
  464. let lastTime = 0;
  465. return function () {
  466. const now = Date.now();
  467. if (now - lastTime >= delay) {
  468. func();
  469. lastTime = now;
  470. } else {
  471. clearTimeout(timer);
  472. timer = setTimeout(func, delay);
  473. }
  474. };
  475. },
  476. // 测量中体重记录
  477. handleProgress: function () {
  478. this.seeConsole('正在测量体重:' + this.data.weight)
  479. },
  480. seeConsole(string) {
  481. this.setData({
  482. consoleList: [string].concat(this.data.consoleList)
  483. })
  484. },
  485. /**
  486. * 清除控制台
  487. */
  488. clear() {
  489. this.setData({
  490. consoleList: []
  491. })
  492. },
  493. /**
  494. * 刷新token
  495. * @param {function} fn 获取token后执行函数
  496. * @param {boolean} flag 请求刷新token
  497. */
  498. async refreshToken(fn, flag) {
  499. let bodyToken = wx.getStorageSync('bodyToken');
  500. let bodyTokenTime = wx.getStorageSync('bodyTokenTime');
  501. if (!bodyToken || bodyTokenTime * 1000 < +new Date() || flag) {
  502. let res = await plugin.refreshToken({
  503. url: 'https://uniquehealth.lefuenergy.com',
  504. data: {
  505. "appSecret":"AlgH7XDNWG1nABz3LSKdhaYTFUdETSBmKL4pRmD5mS4=",
  506. "appKey": "lefu7afd0a504245139b"
  507. }
  508. });
  509. console.log("res", res);
  510. if (res.data.code == 200) {
  511. wx.setStorageSync('bodyToken', res.data.data.token);
  512. wx.setStorageSync('bodyTokenTime', res.data.data.expireTime);
  513. fn();
  514. }
  515. } else {
  516. fn();
  517. }
  518. },
  519. getDeviceSettingList(param) {
  520. let bodyToken = wx.getStorageSync('bodyToken');
  521. plugin.getDeviceSettingList({
  522. url: 'https://uniquehealth.lefuenergy.com',
  523. data: param,
  524. header: {
  525. "token": bodyToken,
  526. "Accept-Language": wx.getStorageSync("lang") || 'zh'
  527. }
  528. }).then(this.handleResponse);
  529. },
  530. handleResponse(res) {
  531. if (res.data.code == 200) {
  532. // 配置蓝牙设备列表
  533. plugin.Blue.setDeviceSetting(res.data.data)
  534. } else if (res.data.code == 401) {
  535. // token过期 重新请求token解析数据
  536. this.refreshToken(() => {
  537. this.getDeviceSettingList(this.data.param)
  538. }, true);
  539. }
  540. },
  541. })