|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
-
- const plugin = requirePlugin('ppScale-plugin')
- var app = getApp();
- Page({
- data: {
-
- selmac: null,
-
- scaleing: false,
-
- state: 0,
-
- weight: '00.0',
-
- unit: 'kg',
-
- devLink: false,
-
- currentWeight: 0,
-
- currentImpedance: 0,
-
- selectDev: null,
-
- deviceModel: null,
-
- lastData: null,
-
- domainValue: '',
-
- wifiValue: '',
-
- pwValue: '',
-
- deviceUnitArray: Object.keys(plugin.PPBluetoothDefine.PPDeviceUnit),
-
- deviceUnitIndex: 0,
-
- netWorkFlag: false,
-
- domainFlag: false,
-
- unitFlag: false,
-
- consoleList: [],
-
- param: {
- appKey:"lefu7afd0a504245139b"
- }
- },
-
-
-
- onUnload() {
-
- plugin.Blue.stop()
- },
-
-
-
- onHide() {
-
- this.seeConsole('销毁蓝牙插件')
- plugin.Blue.stop()
- },
- onShow() {
-
- this.getDeviceSettingList(this.data.param)
-
-
-
- let throttledScroll = this.throttle(this.handleProgress, 1500);
-
- this.setData({
- selmac: app.globalData.selmac
- })
-
- wx.onBluetoothAdapterStateChange((res) => {
- if (!res.available) {
- this.seeConsole('蓝牙设备断开')
- }
- })
-
-
- this.seeConsole('开启蓝牙开始扫描设备')
-
- plugin.bus.subscribe("devicesList", (devList) => {
-
- let selectDev = devList.find(item => {
- return item.deviceId == this.data.selmac.deviceId
- })
-
- this.setData({
- selectDev: selectDev
- })
- if (!this.data.deviceModel && this.data.selectDev) {
-
- this.seeConsole('获取秤端设备模型')
-
- this.setData({
- deviceModel: plugin.Blue.getDeviceModel(selectDev)
- })
- }
-
- if (this.data.deviceModel && this.data.deviceModel.deviceConnectType == plugin.PPBluetoothDefine.PPDeviceConnectType
- .PPDeviceConnectTypeBleAdv) {
- this.seeConsole('设备连接广播秤')
-
- plugin.Blue.setbroadcastDev(selectDev)
- }
-
- if (this.data.deviceModel && this.data.deviceModel.deviceConnectType == plugin.PPBluetoothDefine.PPDeviceConnectType
- .PPDeviceConnectTypeBleConnect) {
-
- this.seeConsole('停止扫描')
- plugin.Blue.stopBluetoothDevicesDiscovery()
-
- this.seeConsole('开始连接设备')
- plugin.Blue.createBLEConnection(selectDev)
- }
- });
-
- plugin.bus.subscribe("deviceConnect", (res) => {
- this.seeConsole('成功连接设备')
-
- plugin.ScaleAction.startDataProgress()
- setTimeout(()=>{
- plugin.ScaleAction.syncDeviceSetting({
- unit: +this.data.deviceUnitIndex,
- })
- },800)
- });
-
-
- plugin.bus.subscribe("devicesModel", (res) => {
-
- });
-
-
- plugin.bus.subscribe("progressData", (res) => {
- throttledScroll()
-
-
- if (this.data.selectDev) {
-
- if (this.data.deviceModel && this.data.deviceModel.deviceConnectType == plugin.PPBluetoothDefine.PPDeviceConnectType
- .PPDeviceConnectTypeBleAdv) {
- this.setData({
- devLink: true
- })
- }
- if (res.weight > 0) {
-
- this.setData({
- scaleing: true
- })
-
- this.setData({
- weight: (res.weight / 100).toFixed(2)
- })
-
- this.setData({
- unit: this.getKeyByUnit(res.unit)
- })
- } else {
-
- this.setData({
- state: 0
- })
-
- this.setData({
- scaleing: false
- })
- }
- if (res.isHeartRating) {
-
- this.setData({
- state: 1
- })
- }
- }
- });
-
-
- plugin.bus.subscribe("deviceWillDisconnect", (res) => {
- this.seeConsole('设备自动断开连接')
-
- this.setData({
- devLink: false
- })
-
- this.reconnectDevice()
- })
-
-
- plugin.bus.subscribe("lockData", (res) => {
- this.seeConsole('锁定测量数据:' + JSON.stringify(res))
-
- this.setData({
- currentWeight: (res.weight / 100).toFixed(2)
- })
-
- this.setData({
- currentImpedance: res.resistance
- })
-
- this.setData({
- lastData: JSON.stringify(res)
- })
-
- this.setData({
- state: 0
- })
- this.setData({
- scaleing: false
- })
-
- });
-
- plugin.bus.subscribe("connectState", (res) => {
-
- if (this.data.selectDev) {
- if (res == plugin.BLUE_STATE.TRANSMITDATA || res == plugin.BLUE_STATE.LOCKDATA || res == plugin.BLUE_STATE
- .HEARTRATING || res ==
- plugin.BLUE_STATE.CONNECTSUCCESS || res == plugin.BLUE_STATE.WIFISUCCESS) {
- this.setData({
- devLink: true
- })
- } else {
- this.setData({
- devLink: false
- })
- }
-
- if (res == plugin.BLUE_STATE.CONNECTFAILED) {
- this.reconnectDevice()
- }
- }
- });
-
- plugin.bus.subscribe("syncDeviceSetting", (res) => {
- this.seeConsole('成功配置设备')
- })
- plugin.bus.subscribe("snData", () => {
- this.seeConsole('成功配置wifi网络')
- })
- plugin.bus.subscribe("domainChange", () => {
- this.seeConsole('成功更新秤端请求服务器域名')
- })
- plugin.bus.subscribe("syncDeviceTimeSuccess", () => {
- this.seeConsole('成功同步时间')
- })
- plugin.bus.subscribe("syncDeviceHistorySuccess", (e) => {
- console.log("历史列表",e)
- this.seeConsole('成功同步历史')
- })
- plugin.bus.subscribe("deviceInfo", (res) => {
- console.log("deviceInfo", res)
- this.seeConsole('设备信息')
- })
-
- plugin.bus.subscribe("deleteDeviceHistoryDataSuccess", () => {
- this.seeConsole('成功删除同步历史')
- })
-
- plugin.Blue.start([], false)
-
- },
- connect() {
- this.seeConsole('开始连接设备')
- if (this.data.deviceModel && this.data.deviceModel.deviceConnectType == plugin.PPBluetoothDefine.PPDeviceConnectType
- .PPDeviceConnectTypeBleConnect) {
-
- plugin.Blue.createBLEConnection(this.data.selectDev)
- }
- },
- disconnect() {
- this.seeConsole('断开连接')
- plugin.Blue.disconnect()
- },
- switchUnit() {
- this.setData({
- unitFlag: true
- })
- },
- setUserMes() {
- this.seeConsole('下发用户信息')
- if (this.data.devLink) {
-
- plugin.ScaleAction.syncDeviceSetting({
- age: 28,
- height: 167,
- gender: 1,
- })
- } else {
- this.seeConsole('停止下发用户信息')
- wx.showToast({
- title: '需要连接蓝牙秤',
- icon: 'error',
- duration: 1000,
- mask: true
- })
- }
- },
- syncHistory() {
- this.seeConsole('同步历史')
- if (this.data.devLink) {
-
- plugin.ScaleAction.fetchDeviceHistoryData()
- } else {
- this.seeConsole('停止同步历史')
- wx.showToast({
- title: '需要连接蓝牙秤',
- icon: 'error',
- duration: 1000,
- mask: true
- })
- }
- },
- deleteSyncHistory() {
- this.seeConsole('删除同步历史')
- if (this.data.devLink) {
-
- plugin.ScaleAction.deleteDeviceHistoryData()
- } else {
- this.seeConsole('停止删除同步历史')
- wx.showToast({
- title: '需要连接蓝牙秤',
- icon: 'error',
- duration: 1000,
- mask: true
- })
- }
- },
- syncTime() {
- this.seeConsole('同步时间')
- if (this.data.devLink) {
-
- plugin.ScaleAction.startDataProgress()
- } else {
- this.seeConsole('停止同步时间')
- wx.showToast({
- title: '需要连接蓝牙秤',
- icon: 'error',
- duration: 1000,
- mask: true
- })
- }
- },
- configureNetwork() {
- this.setData({
- netWorkFlag: true
- })
- },
- modifyDNS() {
- this.setData({
- domainFlag: true
- })
- },
-
-
- reconnectDevice() {
- this.seeConsole('尝试重新连接')
-
- plugin.Blue.disconnect((disres) => {
- if (disres.errCode == 0) {
- this.seeConsole('开始扫描设备')
- plugin.Blue.startBluetoothDevicesDiscovery()
- } else {
- this.reconnectDevice()
- }
- })
- },
-
-
-
- getKeyByUnit(unit) {
- for (const key in plugin.PPBluetoothDefine.PPDeviceUnit) {
- if (plugin.PPBluetoothDefine.PPDeviceUnit[key] === unit) {
- return key;
- }
- }
- return null;
- },
- bindKeyInput: function (e) {
- this.setData({
- inputValue: e.detail.value
- })
- },
- bindDomainInput: function (e) {
- this.setData({
- domainValue: e.detail.value
- })
- },
- bindWifiInput: function (e) {
- this.setData({
- wifiValue: e.detail.value
- })
- },
- bindPasswdInput: function (e) {
- this.setData({
- pwValue: e.detail.value
- })
- },
- bindPickerChange: function (e) {
- this.setData({
- deviceUnitIndex: e.detail.value
- });
- },
- changeUnit() {
- this.seeConsole('切换单位')
- if (this.data.devLink) {
-
- plugin.ScaleAction.syncDeviceSetting({
- unit: +this.data.deviceUnitIndex,
- })
-
- } else {
- this.seeConsole('停止切换单位')
- wx.showToast({
- title: '需要连接蓝牙秤',
- icon: 'error',
- duration: 1000,
- mask: true
- })
- }
- this.setData({
- unitFlag: false
- })
- },
- configWifi() {
- this.seeConsole('开始配置wifi')
-
- if (this.data.devLink && this.data.wifiValue && this.data.pwValue) {
- console.log("this.data.wifiValue && this.data.pwValue",this.data.wifiValue , this.data.pwValue)
- plugin.ScaleAction.configWifi(this.data.wifiValue, this.data.pwValue)
- } else {
- this.seeConsole('停止配置wifi')
- wx.showToast({
- title: '需要连接蓝牙秤或wifi名称或密码',
- duration: 1000,
- mask: true
- })
- }
- this.setData({
- netWorkFlag: false
- })
- },
- changeDomain() {
- this.seeConsole('开始配置域名')
-
- if (this.data.domainValue != '' && this.data.devLink) {
- plugin.ScaleAction.configDomain(this.data.domainValue.replace(/^(https?:\/\/)/, ""))
- } else {
- this.seeConsole('停止配置域名')
- wx.showToast({
- title: '需要连接蓝牙秤或输入域名',
- duration: 1000,
- mask: true
- })
- }
- this.setData({
- domainFlag: false
- })
- },
-
- throttle: function (func, delay) {
- let timer = null;
- let lastTime = 0;
- return function () {
- const now = Date.now();
- if (now - lastTime >= delay) {
- func();
- lastTime = now;
- } else {
- clearTimeout(timer);
- timer = setTimeout(func, delay);
- }
- };
- },
-
- handleProgress: function () {
- this.seeConsole('正在测量体重:' + this.data.weight)
- },
- seeConsole(string) {
- this.setData({
- consoleList: [string].concat(this.data.consoleList)
- })
- },
-
-
- clear() {
- this.setData({
- consoleList: []
- })
- },
-
-
- async refreshToken(fn, flag) {
- let bodyToken = wx.getStorageSync('bodyToken');
- let bodyTokenTime = wx.getStorageSync('bodyTokenTime');
- if (!bodyToken || bodyTokenTime * 1000 < +new Date() || flag) {
- let res = await plugin.refreshToken({
- url: 'https://uniquehealth.lefuenergy.com',
- data: {
- "appSecret":"AlgH7XDNWG1nABz3LSKdhaYTFUdETSBmKL4pRmD5mS4=",
- "appKey": "lefu7afd0a504245139b"
- }
- });
- console.log("res", res);
- if (res.data.code == 200) {
- wx.setStorageSync('bodyToken', res.data.data.token);
- wx.setStorageSync('bodyTokenTime', res.data.data.expireTime);
- fn();
- }
- } else {
- fn();
- }
- },
- getDeviceSettingList(param) {
- let bodyToken = wx.getStorageSync('bodyToken');
- plugin.getDeviceSettingList({
- url: 'https://uniquehealth.lefuenergy.com',
- data: param,
- header: {
- "token": bodyToken,
- "Accept-Language": wx.getStorageSync("lang") || 'zh'
- }
- }).then(this.handleResponse);
- },
- handleResponse(res) {
- if (res.data.code == 200) {
-
- plugin.Blue.setDeviceSetting(res.data.data)
- } else if (res.data.code == 401) {
-
- this.refreshToken(() => {
- this.getDeviceSettingList(this.data.param)
- }, true);
- }
- },
- })
|