小程序 OTA升級(jí) 壓力測(cè)試

<template>
    <view>
        <u-popup :zoom="zoom"  mode="center" :popup="false" z-index="99999" v-model="value" :length="width"
            :mask-close-able="maskCloseAble" :border-radius="borderRadius"
            :negative-top="negativeTop">

            <view class="page-height">
                <view class="update-wrap">
                    <image :src="wxhsImg('/imgbg.png')" class="top-img"></image>
                    </image>
                    <view class="content">
                        <text class="title">設(shè)備固件版本號(hào){{bluetoothversion}}</text>
                        <!-- 升級(jí)描述 -->
                        <view class="title-sub">設(shè)備固件升級(jí)中</view>
                        <!-- 升級(jí)按鈕 --> 
                        <button class="btn" v-if="!pressShow" @click.stop="clickUpgradation()">立即升級(jí)</button>
                        <!-- <button class="btn" v-if="!pressShow" @click.stop="upgradeCompleted()">升級(jí)完成</button> -->
                        <!-- <button class="btn" v-if="!pressShow" @click.stop="toUpLoadFile()">發(fā)送數(shù)據(jù)</button> -->
                    
                        <!-- 下載進(jìn)度 -->
                        <view class="sche-wrap" v-else> 
                            <!-- 更新包下載中 -->
                            <view class="center fs14 bold c_19" style="margin:10px 0px;" v-if="pressValue==100">
                                數(shù)據(jù)傳輸完成,設(shè)備固件升級(jí)中,請(qǐng)等待<u-loading :show="true" color="#19be6b"></u-loading>
                            </view>
                            <view v-if="pressValue!=100">
                                  <view class="fs12 c_57" >{{upgradationtext}} {{pressValue}}%</view>
                            </view>
                          
                            <view class="sche-bg">
                                <view class="sche-bg-jindu" :style="{'width':pressValue+'%'}"></view>
                            </view>
                        </view>
                        
                        <view>
                           
                            <view>
                                <input
                                type="number"
                                class="upgr"
                                style="width:400rpx;"
                                placeholder="請(qǐng)輸測(cè)試入次數(shù)"
                                v-model="upgradesNumber" />
                            </view>
                         
                            <view class=""><u-button  @click="startUpgrade" size="default" type="success">開(kāi)始測(cè)試</u-button> </view>
                            <view class="me-fx-row-c bold fs18" style="padding-top:10px;"> 次數(shù)統(tǒng)計(jì):{{statistics}} </view>
                        </view>
                         
                    </view>
                </view>

                <view class="iconBtn" v-if="iscolse">
                    <span @click.stop="popupClose">  
                        <u-icon name="close-circle" color="#bdb9b9" size="58" ></u-icon>
                    </span>
                </view>

            </view>
        </u-popup>
    </view>
</template>

<script>
    import {wxhsImg} from '@/utils/index.js'
    import store from '@/store';
    import {
        mapGetters,
        mapMutations
    } from 'vuex'
    import {bluetoothRequest} from '@/api/bluetoothque.js'
    import {
        writeBLECharacteristicValue,
        onBLECharacteristicValueChange,
        instructioNencapsulation,
    } from '@/Bluetooth/bluetoothData.js'
    
    import {agreementFuncversionInfo} from '@/Bluetooth/InstructionMethod.js'
    import func from '@/utils/func.js'
    import {devUrl} from '@/common/setting'

    
    export default {
        props: {
            zoom: {
                type: Boolean,
                default: true
            },
            // 彈窗寬度瓤介,可以是數(shù)值(rpx)陨舱,百分比,auto等
            width: {
                type: [Number, String],
                default: 600
            },
            // 是否顯示Modal
            value: {
                type: Boolean,
                default: false
            },
            borderRadius: {
                type: [Number, String],
                default: 16
            },
            // 是否允許點(diǎn)擊遮罩關(guān)閉modal
            maskCloseAble: {
                type: Boolean,
                default: false
            },
            // 給一個(gè)負(fù)的margin-top澜沟,往上偏移,避免和鍵盤(pán)重合的情況
            negativeTop: {
                type: [String, Number],
                default: 0
            }
        },
        components: {
  
        },
        watch:{
            'versionInformation':{
                handler(val,old){
                    console.log('-watch-versionInformation-----',val);
                    if(val){
                        this.newVision=val.version
                    }
                    
                },
                immediate:true
            },
            'upgradeResults':{
               handler(val,old){
                    console.log('升級(jí)成功狀態(tài)碼val---',val)
                   if(val==1){//升級(jí)成功  
                         //請(qǐng)求版本號(hào)逾一,對(duì)比
                        this.upgradeCompleted()
                   }else if(val!=0){
                       this.upgradeError(val)
                   }
               },
               immediate:true 
            }
        },
        data() {
            return {
                wxhsImg:wxhsImg, 
                pressShow:false,
                iscolse:true,
                pressValue:0,
                splitFileData:[],
                blueIndex:0,
                circulateNum:0,
                newVision:'',
                upgradationtext:'數(shù)據(jù)傳輸進(jìn)度: ',
                upgradesNumber:'',
                statistics:0,
                bluetoothversion:'',
                versionIndex:'',
                upgradList:[
                    {
                        'version':'V1.0',
                        'fileSize':35172,
                        'encryption':'0f283da541b0cc9061a4844f881b486c',
                    },{
                        'version':'V1.1',
                        'fileSize':35172,
                        'encryption':'4470103f83a83d1d245507415f0fc673',
                    }
                ]
            }
        },
        computed: {
           ...mapGetters([
               "versionInformation",
               "upgradeResults",
           ])
        },
        created() {
            
        },
        mounted() {
            
        },
        beforeDestroy() {
 
        },
        methods: {
            //開(kāi)始測(cè)試 
            startUpgrade(){
                
                const that=this;
                if(this.upgradesNumber==''){
                    that.$u.func.showToast('請(qǐng)輸入測(cè)試次數(shù)')
                    return false;
                }
                
                if(that.statistics>=this.upgradesNumber){
                    that.$u.func.showToast('測(cè)試完成')
                    uni.showModal({
                                title:'溫馨提示',
                                content:`測(cè)試完成-共測(cè)試${that.statistics}次`,
                                });
                    return false;
                }
                
                console.log('開(kāi)始測(cè)試次數(shù)----',this.upgradesNumber);
                const upgradationzl=this.$instructionList.upgradation;
                const interupgradation=this.$agrFuc.getSendData(upgradationzl);
                
                writeBLECharacteristicValue(interupgradation).then(res=>{
                        uni.hideLoading()
                      const v1=agreementFuncversionInfo(res);//設(shè)備版本號(hào)
                      this.bluetoothversion=v1
                      console.log('獲取設(shè)備版本號(hào)---',v1)
                      if(v1.slice(1)=='1.1'){
                        that.clickUpgradation(0)
                      }else{
                         that.clickUpgradation(1)  
                      }
                }).catch(err=>{
                    
                })
                
                 
            },
            clickUpgradation(index){
                const that=this;
                const encryption=this.$agrFuc.charToHex(that.upgradList[index].encryption)//md5值 轉(zhuǎn)16進(jìn)制  32位
                const baseSize=this.$agrFuc.transNumber(that.upgradList[index].fileSize) //文件大小
                const code=[19];//功能碼從 13 的十六進(jìn)制 
                console.log('baseContent---1-',encryption)
                const  baseContent=[...code,...baseSize,...encryption]  //功能碼, 文件大小 恶导, 32個(gè)m5數(shù)據(jù) 組合
                console.log('baseContent---1-',baseContent)
                const upgradationData=this.$agrFuc.getSendData(baseContent);
                console.log('upgradationData---1-',upgradationData)
                writeBLECharacteristicValue(upgradationData).then(res=>{
                        console.log('是否升級(jí)成功---',res);
                        console.log('是否升級(jí)成功-3-',res[3]);
                        uni.hideLoading();
                        const codeStats=res[3];
                         if(codeStats=='0'){//升級(jí)模式準(zhǔn)備就緒 00 就是升級(jí)允許
                              console.log('升級(jí)模式準(zhǔn)備就緒 00 就是升級(jí)允許--',)
                              that.toUpLoadFile(index)
                         }else{
                             that.upgradeError(codeStats)
                         }
                 }).catch(err=>{
                       uni.hideLoading()
                       console.log('-是否升級(jí)失敗 操作失敗--',err);
               
                })
            },
            /*** 下載升級(jí) 下次文件 */
            toUpLoadFile(index){
                console.log('下載升級(jí) 下次文件 ---');
                const that=this;
                that.versionIndex=that.upgradList[index].version
                const url=`upload/files/carFragrance/update/${that.upgradList[index].version}.bin`;
              //  const url=`upload/files/carFragrance/update/V1.0.bin`;
                const method='GET';
                const responseType='arraybuffer';
                bluetoothRequest(url,method,responseType).then(res=>{
                       console.log('下載升級(jí)文件-res-',res.data);
                      if(res.statusCode==200){
                        const file=res.data;//文件  arrayBuffer 格式
                        const filesize=that.upgradList[index].fileSize;//文件總大小
                        const chunkSize=128;//每次切片數(shù)量   (app每次 256字節(jié))  (小程序 支持128字節(jié))
                        this.splitFileData=that.$u.func.arrbefsplitFil(file,chunkSize)
                        console.log('arrbefsplitFil ----', this.splitFileData);
                        that.pressShow=true; 
                        that.iscolse=false;
                        that.bluetoothContinuation();
                      }else{
                          this.$u.func.showToast('下載文件失敗')
                      }
                }).catch(err=>{
                         this.$u.func.showToast('下載文件失敗')
                         console.log('下載升級(jí)文件-err-',err)
                })
            },
            //持續(xù)發(fā)送數(shù)據(jù)崭篡,需要做 斷點(diǎn) 續(xù)傳
            bluetoothContinuation(){
                const that=this;
                  this.uploadChunk().then(res=>{
                        this.circulateNum=0;// 初始化
                        if(res=='success'){ //全部上傳成功
                            this.upgradationtext='傳輸完成,設(shè)備固件升級(jí)中吧秕,請(qǐng)等待'
                            //判斷升級(jí)是否完成
                            // setTimeout(()=>{
                            //     that.upgradeCompleted()
                            // },2000)
                        }else{
                            //上傳成功琉闪,繼續(xù)上傳
                            console.log('上傳成功,繼續(xù)上傳--',res)
                            setTimeout(()=>{ // (app 每隔 50毫秒)  (小程序 每隔 20毫秒)
                                  this.bluetoothContinuation()
                            },20)
                        }
                  }).catch(err=>{
                      //發(fā)送失敗  循環(huán)10次發(fā)送
                        this.circulateNum= this.circulateNum+1
                        if(this.circulateNum==10){
                            console.log('上傳失敗,繼續(xù)上傳---')
                            this.bluetoothContinuation()
                        }else{
                            uni.showModal({
                                title:'溫馨提示',
                                content:'升級(jí)失敗,請(qǐng)斷開(kāi)藍(lán)牙,重新連接升級(jí)或聯(lián)系管理員',
                                success: function (res){
                                    if (res.confirm) {
                                        that.pressValue=0
                                        that.blueIndex=0
                                        that.iscolse=true;
                                        that.$emit('popupClose',false)
                                        console.log('用戶(hù)點(diǎn)擊確定');
                                    } else if (res.cancel) {
                                        console.log('用戶(hù)點(diǎn)擊取消');
                                    }
                                }
                            });
                            // this.$u.func.showToast('上傳失敗');
                        }
                  })
            },
            //傳送數(shù)據(jù)處理 
            uploadChunk(){
                const appStore =this.$store.getters;
                console.log(`第${this.blueIndex}次`)
                const bluetoothData= this.splitFileData[this.blueIndex];//每次切片的數(shù)據(jù)
                return new Promise((resolve,reject) =>{
                        //總長(zhǎng)度 大于  成功次數(shù) 砸彬,相反的話(huà)颠毙,就說(shuō)明發(fā)送成功
                        // if(this.splitFileData.length>this.blueIndex){
                            if(bluetoothData){
                                    console.log('bluetoothData-----',bluetoothData);
                                    this.blueIndex=this.blueIndex+1;//記錄上傳的位置
                                    this.pressValue=parseInt((this.blueIndex/this.splitFileData.length)*100)// 下載 進(jìn)度
                                    let typedArray = new Uint8Array(bluetoothData);
                                     
                                        buffer1 = typedArray.buffer;
                                          console.log('typedArray---',typedArray)
                                          console.log('buffer1---',buffer1)
                                    // let  buffer1 = bluetoothData
                                    uni.writeBLECharacteristicValue({
                                                   deviceId: appStore.deviceId,
                                                   serviceId: appStore.servicesUuid,
                                                   characteristicId: appStore.characteristicId,
                                                   value: buffer1,
                                                   success: (res) => {
                                                        console.log('指令發(fā)送sueccess--',res)
                                                         resolve(this.blueIndex);
                                                   },
                                                   fail: (err) => {
                                                        console.log('指令發(fā)送err-err--',err)
                                                       reject(err)
                                                   }
                                })
                             
                             
                             resolve(this.blueIndex);
                        }else{
                            //上傳成功
                             resolve('success')
                        } 
                })
            },
            //判斷升級(jí)是否完成
            upgradeCompleted(){
                console.log('--判斷升級(jí)是否完成-1-')
                const that=this;
                // 設(shè)備版本號(hào)和服務(wù)器版本相同 升級(jí)完成 
                const upgradationzl=this.$instructionList.upgradation;
                const interupgradation=this.$agrFuc.getSendData(upgradationzl);
                this.$store.commit('Bluetooth/set_versionInfo','');
                console.log('--判斷升級(jí)是否完成-2-')
                writeBLECharacteristicValue(interupgradation).then(res=>{
                    console.log('--版本號(hào)--',res)
                    const  versionInfo=agreementFuncversionInfo(res)
                    uni.hideLoading()
                    console.log('升級(jí)完成---',versionInfo)
                    const v1=versionInfo.slice(1);//設(shè)備返回 的 版本號(hào)
                     const v2=that.versionIndex.slice(1);//服務(wù)器放回的版本號(hào)  1.2
                    console.log('升級(jí)完成-版本號(hào)--',v1,v2)
                     if(Number(v1)==Number(v2)){
                        that.$u.func.showToast('升級(jí)成功')
                        that.pressValue=0
                        that.blueIndex=0
                        that.iscolse=true;
                        that.upgradationtext='數(shù)據(jù)傳輸進(jìn)度'
                        that.statistics=that.statistics+1
                        that.startUpgrade();
  
                     } 
                }).catch(err=>{
                    // 升級(jí)失敗 
                    console.log('err---',err);
                    uni.hideLoading()
                    this.$u.func.showToast('傳輸異常斷開(kāi),請(qǐng)斷開(kāi)藍(lán)天,重新連接更新')
                })
            },
            //升級(jí)失敗
            upgradeError(code){
                if(code){
                    if(code==2){
                        this.$u.func.showToast('MD5校驗(yàn)失敗,')
                    }else if(code==3){
                        this.$u.func.showToast('容量不足')
                    }else if(code==4){
                        this.$u.func.showToast(' 升級(jí)超時(shí)')
                    }
                    this.pressValue=0
                    this.blueIndex=0
                    this.iscolse=true;
                    this.$emit('popupClose',false)
                }
             
                    
           
                    
            },
            // 點(diǎn)擊遮罩關(guān)閉modal砂碉,設(shè)置v-model的值為false蛀蜜,否則無(wú)法第二次彈起modal
            popupClose() {
                this.$emit('popupClose', false);
            },
        }
    };
</script>

<style lang="scss" scoped>
    .page-height {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        //background-color: rgba($color: #000000, $alpha: .7);
    }

    .update-wrap {
        width: 580rpx;
        border-radius: 18rpx;
        position: relative;
        display: flex;
        flex-direction: column;
        background-color: #ffffff;
        padding: 290rpx 30rpx 0;

        .top-img {
            position: absolute;
            left: 0;
            width: 100%;
            height: 246rpx;
            top: 20rpx;
        }

        .content {
            display: flex;
            flex-direction: column;
            align-items: center;
            padding-bottom: 40rpx;

            .title {
                font-size: 32rpx;
                font-weight: bold;
                color: #6526f3;
            }

            .title-sub {
                text-align: center;
                font-size: 24rpx;
                color: #666666;
                padding: 30rpx 0;
            }

            .btn {
                width: 460rpx;
                display: flex;
                align-items: center;
                justify-content: center;
                color: #ffffff;
                font-size: 30rpx;
                height: 80rpx;
                line-height: 80rpx;
                border-radius: 100px;
                background-color: #6526f3;
                margin-top: 20rpx;
            }
        }
    }
    
    .upgr{
        margin:10px 0px;border:2px solid green;padding:10px;border-radius:10rpx;
    }

    .iconBtn {
        padding-bottom: 30rpx;
    }
    
    .sche-wrap {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: flex-end;
            padding: 10rpx 50rpx 0;
    
            .sche-wrap-text {
                font-size: 24rpx;
                color: #666;
                margin-bottom: 20rpx;
            }
            
            .c_57{
                color:#5775e7;
            }
    
            .sche-bg {
                position: relative;
                background-color: #cccccc;
                height: 30rpx;
                border-radius: 100px;
                width: 480rpx;
                display: flex;
                align-items: center;
                margin-top:10rpx;
    
                .sche-bg-jindu {
                    position: absolute;
                    left: 0;
                    top: 0;
                    height: 30rpx;
                    min-width: 30rpx;
                    border-radius: 100px;
                    background: #6526f3;
                    // background: url(./img/round.png) #5775e7 center right 4rpx no-repeat;
                    background-size: 26rpx 26rpx;
                }
            }
    
            .down-text {
                font-size: 24rpx;
                color: #5674e5;
                margin-top: 16rpx;
            }
        }
    
</style>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市增蹭,隨后出現(xiàn)的幾起案子滴某,更是在濱河造成了極大的恐慌,老刑警劉巖滋迈,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件霎奢,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡饼灿,警方通過(guò)查閱死者的電腦和手機(jī)幕侠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)碍彭,“玉大人晤硕,你說(shuō)我怎么就攤上這事”蛹桑” “怎么了舞箍?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)皆疹。 經(jīng)常有香客問(wèn)我创译,道長(zhǎng),這世上最難降的妖魔是什么墙基? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任软族,我火速辦了婚禮,結(jié)果婚禮上残制,老公的妹妹穿的比我還像新娘立砸。我一直安慰自己,他們只是感情好初茶,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布颗祝。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪螺戳。 梳的紋絲不亂的頭發(fā)上搁宾,一...
    開(kāi)封第一講書(shū)人閱讀 51,631評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音倔幼,去河邊找鬼盖腿。 笑死,一個(gè)胖子當(dāng)著我的面吹牛损同,可吹牛的內(nèi)容都是我干的翩腐。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼膏燃,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼茂卦!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起组哩,我...
    開(kāi)封第一講書(shū)人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤搔啊,失蹤者是張志新(化名)和其女友劉穎黄娘,沒(méi)想到半個(gè)月后栽惶,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體胖腾,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年幕袱,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了暴备。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡们豌,死狀恐怖涯捻,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情望迎,我是刑警寧澤障癌,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站辩尊,受9級(jí)特大地震影響涛浙,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜摄欲,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一轿亮、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧胸墙,春花似錦我注、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)励七。三九已至,卻和暖如春奔缠,著一層夾襖步出監(jiān)牢的瞬間掠抬,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工校哎, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留两波,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓贬蛙,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親谚攒。 傳聞我的和親對(duì)象是個(gè)殘疾皇子阳准,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容