var e = getApp();

Page({
    data: {
        status: "",
        sousuo: "",
        connectedDeviceId: "",
        services: "",
        characteristics: "",
        writeServicweId: "",
        writeCharacteristicsId: "",
        readServicweId: "",
        readCharacteristicsId: "",
        notifyServicweId: "",
        notifyCharacteristicsId: "",
        inputValue: "",
        characteristics1: "",
        waterImage: "../../images/w4.png",
        isPlatform: "android",
        isOpenSuccess: "0",
        isTitle: "0",
        isClickClose: "0",
        device_id: ""
    },
    onLoad: function() {
        wx.openBluetoothAdapter ? wx.openBluetoothAdapter() : wx.showModal({
            title: "提示",
            content: "当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。"
        });
        var e = this;
        wx.getSystemInfo({
            success: function(t) {
                "ios" != t.platform && "android" != t.platform || e.setData({
                    isPlatform: t.platform
                });
            }
        });
    },
    onPullDownRefresh: function() {},
    onShow: function() {
        var t = this;
        wx.getStorage({
            key: "device_id",
            success: function(e) {
                t.setData({
                    device_id: e.data
                });
            }
        }), "" != e.globalData.connectedDeviceId && (t.setData({
            connectedDeviceId: e.globalData.connectedDeviceId,
            writeServicweId: e.globalData.writeServicweId,
            writeCharacteristicsId: e.globalData.writeCharacteristicsId,
            readServicweId: e.globalData.readServicweId,
            readCharacteristicsId: e.globalData.readCharacteristicsId,
            notifyServicweId: e.globalData.notifyServicweId,
            notifyCharacteristicsId: e.globalData.notifyCharacteristicsId
        }), console.log("connectedDeviceId:" + t.data.connectedDeviceId)), "" != t.data.connectedDeviceId && wx.setNavigationBarTitle({
            title: "已连接蓝牙" + t.data.connectedDeviceId
        }), wx.onBLEConnectionStateChange(function(c) {
            console.log("该方法回调中可以用于处理连接意外断开等异常情况"), console.log("device " + c.deviceId + " state has changed, connected: " + c.connected), 
            c.connected ? wx.setNavigationBarTitle({
                title: "已连接蓝牙" + c.deviceId
            }) : (wx.setNavigationBarTitle({
                title: "已断开水控器连接"
            }), t.setData({
                connectedDeviceId: "",
                isOpenSuccess: "0",
                isClickClose: "0",
                waterImage: "../../images/w4.png"
            }), e.globalData.connectedDeviceId = "");
        });
    },
    query: function(t) {
        var c = this;
        wx.navigateTo({
            url: "../getDeviceId/getDeviceId"
        }), wx.onBLEConnectionStateChange(function(t) {
            console.log("ios该方法回调中可以用于处理连接意外断开等异常情况"), console.log("device " + t.deviceId + " state has changed, connected: " + t.connected), 
            t.connected ? wx.setNavigationBarTitle({
                title: "已连接蓝牙" + t.deviceId
            }) : (wx.setNavigationBarTitle({
                title: "已断开水控器连接"
            }), c.setData({
                connectedDeviceId: "",
                isOpenSuccess: "0",
                isClickClose: "0",
                waterImage: "../../images/w4.png"
            }), e.globalData.connectedDeviceId = "");
        });
    },
    scanOrQuery: function(e) {
        t = this, console.log("触发");
        var t = this;
        wx.showActionSheet({
            itemList: [ "扫码", "搜索" ],
            itemColor: "#3CC51F",
            success: function(e) {
                console.log(e.tapIndex), 0 == e.tapIndex ? t.scan() : wx.navigateTo({
                    url: "../getDeviceId/getDeviceId"
                });
            },
            fail: function(e) {
                console.log(e.errMsg);
            }
        });
    },
    scan: function() {
        wx.showLoading({
            title: "加载中"
        }), wx.closeBluetoothAdapter({
            success: function(e) {
                console.log(e);
            }
        });
        var t = this;
        console.log("扫码");
        var c, i;
        wx.scanCode({
            onlyFromCamera: !0,
            success: function(a) {
                console.log("扫码：" + a.result), i = a.result, wx.getSystemInfo({
                    success: function(a) {
                        console.log(a.platform), console.log(e.url), wx.request({
                            url: e.url + "/applet/getMacOrUuid",
                            data: {
                                platform: a.platform,
                                qrCode: i
                            },
                            success: function(e) {
                                c = e.data.resultObject, console.log("deviceId:" + c), wx.setStorageSync("device_id", c), 
                                t.setData({
                                    device_id: c
                                }), wx.openBluetoothAdapter({
                                    success: function(e) {
                                        console.log("初始化蓝牙适配器:" + JSON.stringify(e)), t.connectTOBlue(c);
                                    },
                                    fail: function(e) {
                                        wx.showModal({
                                            title: "提示",
                                            content: "请打开手机蓝牙,并下拉界面",
                                            success: function(e) {}
                                        });
                                    }
                                });
                            }
                        });
                    }
                });
            },
            complete: function(e) {
                wx.hideLoading();
            }
        });
    },
    open: function() {
        console.log("开");
        var t = this;
        "1" == t.data.isOpenSuccess ? e.showToast("已开启", "none") : (wx.showLoading({
            title: "加载中"
        }), console.log("device_id::", t.data.connectedDeviceId), "" != t.data.connectedDeviceId ? t.lanya8("FEFE09B001010000") : (wx.hideLoading(), 
        e.showToast("请先搜索/扫码！", "none")));
    },
    close: function() {
        var t = this;
        console.log("关isClickClose:" + t.data.isClickClose), wx.showLoading({
            title: "加载中"
        }), setTimeout(function() {
            wx.hideLoading(), t.setData({
                isClickClose: "0"
            });
        }, 15e3), "1" != t.data.isClickClose ? "" != t.data.connectedDeviceId ? (t.setData({
            isClickClose: "1"
        }), t.lanya8("FEFE09B30000")) : (wx.hideLoading(), e.showToast("请先搜索/扫码！", "none")) : console.log("已经点击过停止按钮了");
    },
    lanya1: function() {
        var e = this;
        wx.openBluetoothAdapter({
            success: function(t) {
                console.log("初始化蓝牙适配器:" + JSON.stringify(t)), e.lanya2();
            },
            fail: function(e) {
                wx.showModal({
                    title: "提示",
                    content: "请打开手机蓝牙,并下拉界面",
                    success: function(e) {}
                });
            }
        });
    },
    lanya2: function() {
        this.lanya3();
    },
    lanya3: function() {
        var e = this;
        wx.startBluetoothDevicesDiscovery({
            success: function(t) {
                console.log("搜索33设备:" + JSON.stringify(t)), setTimeout(function() {
                    e.lanya4();
                }, 1500);
            }
        });
    },
    lanya4: function() {
        var e = this;
        wx.getBluetoothDevices({
            success: function(t) {
                console.log("444:" + JSON.stringify(t.devices));
                var c = t.devices, i = new Array();
                if (c.length > 0) for (s = 0; s < c.length; s++) c[s].name.indexOf("MT") >= 0 && i.push(c[s]);
                if (i.length > 0) {
                    for (var a = i[0], s = 0; s < i.length; s++) a.RSSI < i[s].RSSI && (a = i[s]);
                    var o = {
                        title: "名称" + a.name,
                        content: "确定是否连接开阀",
                        success: function(t) {
                            t.confirm ? (console.log("用户点击确定"), e.connectTOBlue(a.deviceId)) : t.cancel && console.log("用户点击取消");
                        },
                        complete: function(e) {
                            e.confirm || e.cancel ? console.log("用户点击确定 / 取消") : wx.showModal(o);
                        }
                    };
                    wx.showModal(o);
                }
                e.setData({
                    devices: t.devices
                }), wx.hideNavigationBarLoading(), wx.stopPullDownRefresh();
            }
        });
    },
    lanya5: function() {
        var e = this;
        wx.stopBluetoothDevicesDiscovery({
            success: function(t) {
                e.setData({
                    msg: "停止搜索周边设备/" + JSON.stringify(t.errMsg),
                    sousuo: t.discovering ? "在搜索。" : "未搜索。",
                    status: t.available ? "可用。" : "不可用。"
                });
            }
        });
    },
    connectTO: function(e) {
        var t = this;
        wx.createBLEConnection({
            deviceId: "10:CE:A9:DC:06:04",
            success: function(e) {
                console.log(e.errMsg), t.setData({
                    connectedDeviceId: "10:CE:A9:DC:06:04",
                    msg1: ""
                });
            },
            fail: function() {
                console.log("调用失败");
            },
            complete: function() {
                console.log("调用结束"), t.lanya6();
            }
        });
    },
    lanya6: function() {
        var e = this;
        wx.getBLEDeviceServices({
            deviceId: e.data.connectedDeviceId,
            success: function(t) {
                console.log("device services:", JSON.stringify(t.services)), e.setData({
                    services: t.services
                }), e.lanya7();
            }
        });
    },
    lanya7: function() {
        var e = this;
        wx.getBLEDeviceCharacteristics({
            deviceId: e.data.connectedDeviceId,
            serviceId: e.data.services[0].uuid,
            success: function(t) {
                for (var c = 0; c < t.characteristics.length; c++) t.characteristics[c].properties.notify && (console.log("11111111", e.data.services[0].uuid), 
                console.log("22222222222222222", t.characteristics[c].uuid), e.setData({
                    notifyServicweId: e.data.services[0].uuid,
                    notifyCharacteristicsId: t.characteristics[c].uuid
                })), t.characteristics[c].properties.write ? e.setData({
                    writeServicweId: e.data.services[0].uuid,
                    writeCharacteristicsId: t.characteristics[c].uuid
                }) : t.characteristics[c].properties.read && e.setData({
                    readServicweId: e.data.services[0].uuid,
                    readCharacteristicsId: t.characteristics[c].uuid
                });
                console.log("device getBLEDeviceCharacteristics:", t.characteristics);
            },
            fail: function() {
                console.log("fail");
            },
            complete: function() {
                console.log("complete"), e.lanya9();
            }
        });
    },
    lanya0: function() {
        var e = this;
        wx.closeBLEConnection({
            deviceId: e.data.connectedDeviceId,
            success: function(t) {
                console.log(t.errMsg), e.setData({
                    connectedDeviceId: ""
                });
            }
        });
    },
    inputTextchange: function(e) {
        this.setData({
            inputValue: e.detail.value
        });
    },
    lanya8: function(t) {
        var c = this, i = new Uint8Array(t.match(/[\da-f]{2}/gi).map(function(e) {
            return parseInt(e, 16);
        }));
        console.log(i);
        var a = i.buffer;
        console.log(a), console.log("deviceId", c.data.connectedDeviceId), console.log("writeServicweId", c.data.writeServicweId), 
        console.log("writeCharacteristicsId", c.data.writeCharacteristicsId), wx.writeBLECharacteristicValue({
            deviceId: c.data.connectedDeviceId,
            serviceId: c.data.writeServicweId,
            characteristicId: c.data.writeCharacteristicsId,
            value: a,
            success: function(e) {
                console.log("writeBLECharacteristicValue success", e.errMsg);
            }
        });
        var s = wx.getStorageSync("openId"), o = wx.getStorageSync("schoolName");
        wx.onBLECharacteristicValueChange(function(i) {
            var a = c.buf2hex(i.value).toUpperCase();
            if (t.toUpperCase() != a) if (console.log(a), console.log("value.length:" + a.length), 
            40 == a.length) {
                var n = a.substring(6, 8);
                console.log("dType:" + n), "B0" == n && -1 == a.indexOf("FFFFFF") && (r = (r = a.substring(34, 40)).substring(4, 6) + r.substring(2, 4) + r.substring(0, 2), 
                a = a.substring(0, 34) + r, console.log("value:" + a), c.lanya8("FEFE09B201010D000041631908312210030-5264")), 
                "B0" == n && a.indexOf("FFFFFF") > 0 ? c.lanya8("FEFE09B201010D000041631908312210030-5264") : "B3" == n && (r = (r = a.substring(34, 40)).substring(4, 6) + r.substring(2, 4) + r.substring(0, 2), 
                a = a.substring(0, 34) + r, console.log("value:" + a, "openid" + s, "devicedID" + c.data.connectedDeviceId, "schoolName" + o), 
                wx.closeBLEConnection({
                    deviceId: c.data.connectedDeviceId,
                    success: function(e) {
                        console.log("断开连接：" + e.errMsg), wx.setNavigationBarTitle({
                            title: "已断开水控器连接"
                        }), c.setData({
                            connectedDeviceId: "",
                            isOpenSuccess: "0",
                            isClickClose: "0",
                            waterImage: "../../images/w4.png"
                        });
                    }
                }), wx.closeBluetoothAdapter({
                    success: function(e) {
                        console.log(e);
                    }
                }), wx.hideLoading(), e.showToast("停止成功", "success"), c.setData({
                    waterImage: "../../images/w4.png",
                    isOpenSuccess: "0",
                    isClickClose: "0"
                }), c.lanya8("FEFE09B40000"));
            } else if (16 == a.length) "B1" == (n = a.substring(6, 8)) ? c.lanya8("FEFE09B201010D000041631908312210030-5264") : "B2" == n ? (wx.hideLoading(), 
            e.showToast("启动成功", "success"), c.setData({
                waterImage: "../../images/w4.gif",
                isOpenSuccess: "1",
                isTitle: "1"
            })) : "B5" == n && (console.log("valueb55555555555555 :" + a), "55" == a.substring(14, 16) && (wx.request({
                url: e.url + "/applet/saveTemperatureSetData",
                data: {
                    openId: s,
                    deviceId: c.data.connectedDeviceId,
                    schoolName: o
                },
                success: function(e) {}
            }), wx.closeBLEConnection({
                deviceId: c.data.connectedDeviceId,
                success: function(e) {
                    console.log("断开连接：" + e.errMsg);
                }
            }), wx.closeBluetoothAdapter({
                success: function(e) {
                    console.log(e);
                }
            }))); else if (20 == a.length) "B2" == (n = a.substring(6, 8)) && (wx.hideLoading(), 
            e.showToast("启动成功", "success"), c.setData({
                waterImage: "../../images/w4.gif",
                isOpenSuccess: "1",
                isTitle: "1"
            })); else if (38 == a.length) {
                var r = (a += "00").substring(34, 40);
                r = r.substring(4, 6) + r.substring(2, 4) + r.substring(0, 2), a = a.substring(0, 34) + r, 
                console.log("value:" + a), wx.request({
                    url: e.url + "/applet/saveConsLog",
                    data: {
                        openId: s,
                        value: a,
                        deviceId: c.data.connectedDeviceId,
                        schoolName: o
                    },
                    success: function(e) {
                        wx.hideLoading(), wx.showModal({
                            title: "提示",
                            content: "停止成功,消费了" + e.data.resultObject + "元",
                            showCancel: !1
                        }), c.setData({
                            waterImage: "../../images/w4.png",
                            isOpenSuccess: "0",
                            isClickClose: "0"
                        }), c.lanya8("FEFE09B40000");
                    }
                });
            }
        });
    },
    lanya9: function() {
        var t = this;
        wx.notifyBLECharacteristicValueChange({
            state: !0,
            deviceId: t.data.connectedDeviceId,
            serviceId: t.data.notifyServicweId,
            characteristicId: t.data.notifyCharacteristicsId,
            success: function(t) {
                console.log("notifyBLECharacteristicValueChange success", t.errMsg), wx.hideLoading(), 
                console.log("1231321312313123"), e.showToast("连接成功", "success");
            },
            fail: function() {
                console.log("失败"), console.log(t.data.notifyServicweId), console.log(t.data.notifyCharacteristicsId);
            }
        });
    },
    connectTOBlue: function(t) {
        var c = this, i = t;
        wx.createBLEConnection({
            deviceId: i,
            success: function(e) {
                console.log(e.errMsg), c.setData({
                    connectedDeviceId: i
                }), c.lanya6();
            },
            fail: function() {
                console.log("调用失败"), e.showToast("蓝牙连接失败或被占用", "none");
            },
            complete: function() {
                console.log("调用结束");
            }
        });
    },
    buf2hex: function(e) {
        return Array.prototype.map.call(new Uint8Array(e), function(e) {
            return ("00" + e.toString(16)).slice(-2);
        }).join("");
    },
    connectTO2: function(e) {
        var t = this;
        wx.showLoading({
            title: "连接中"
        }), t.connectTOBlue2(e.currentTarget.id);
    },
    connectTOBlue2: function(t) {
        var c = this, i = t;
        wx.getStorageSync("schoolName"), wx.createBLEConnection({
            deviceId: i,
            success: function() {
                console.log(e.errMsg), c.setData({
                    connectedDeviceId: i
                }), c.lanya61(), e.showToast("直接连接成功", "none");
            },
            fail: function() {
                wx.openBluetoothAdapter({
                    success: function(e) {
                        console.log("初始化蓝牙适配器:" + JSON.stringify(e)), c.connectTOBlue2(i);
                    },
                    fail: function(e) {
                        wx.showModal({
                            title: "提示",
                            content: "请检查手机蓝牙是否打开",
                            success: function(e) {}
                        });
                    }
                });
            },
            complete: function() {
                console.log("调用结束");
            }
        });
    },
    lanya61: function() {
        var e = this;
        wx.getBLEDeviceServices({
            deviceId: e.data.connectedDeviceId,
            success: function(t) {
                console.log("device services:", JSON.stringify(t.services)), e.setData({
                    services: t.services,
                    msg: JSON.stringify(t.services)
                }), e.lanya71();
            }
        });
    },
    lanya71: function() {
        var e = this;
        wx.getBLEDeviceCharacteristics({
            deviceId: e.data.connectedDeviceId,
            serviceId: e.data.services[0].uuid,
            success: function(t) {
                for (var c = 0; c < t.characteristics.length; c++) t.characteristics[c].properties.notify && (console.log("11111111", e.data.services[0].uuid), 
                console.log("22222222222222222", t.characteristics[c].uuid), e.setData({
                    notifyServicweId: e.data.services[0].uuid,
                    notifyCharacteristicsId: t.characteristics[c].uuid
                })), t.characteristics[c].properties.write ? e.setData({
                    writeServicweId: e.data.services[0].uuid,
                    writeCharacteristicsId: t.characteristics[c].uuid
                }) : t.characteristics[c].properties.read && e.setData({
                    readServicweId: e.data.services[0].uuid,
                    readCharacteristicsId: t.characteristics[c].uuid
                });
                console.log("device getBLEDeviceCharacteristics:", t.characteristics), e.setData({
                    msg: JSON.stringify(t.characteristics)
                });
            },
            fail: function() {
                console.log("fail");
            },
            complete: function() {
                console.log("complete"), e.lanya9();
            }
        });
    },
    lanya91: function() {
        var t = this;
        wx.notifyBLECharacteristicValueChange({
            state: !0,
            deviceId: t.data.connectedDeviceId,
            serviceId: t.data.notifyServicweId,
            characteristicId: t.data.notifyCharacteristicsId,
            success: function(c) {
                console.log("notifyBLECharacteristicValueChange success", c.errMsg), e.globalData.connectedDeviceId = t.data.connectedDeviceId, 
                e.globalData.writeServicweId = t.data.writeServicweId, e.globalData.writeCharacteristicsId = t.data.writeCharacteristicsId, 
                e.globalData.readServicweId = t.data.readServicweId, e.globalData.readCharacteristicsId = t.data.readCharacteristicsId, 
                e.globalData.notifyServicweId = t.data.notifyServicweId, e.globalData.notifyCharacteristicsId = t.data.notifyCharacteristicsId, 
                wx.hideLoading(), console.log("dwadwadadwadadw"), wx.switchTab({
                    url: "../index/index"
                });
            },
            fail: function() {
                console.log("失败");
            }
        });
    }
});