<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">设备固件版本号{{bluetoothversion}}</text>
<!-- 升级描述 -->
<view class="title-sub">设备固件升级中</view>
<!-- 升级按钮 -->
<button class="btn" v-if="!pressShow" @click.stop="clickUpgradation()">立即升级</button>
<!-- <button class="btn" v-if="!pressShow" @click.stop="upgradeCompleted()">升级完成</button> -->
<!-- <button class="btn" v-if="!pressShow" @click.stop="toUpLoadFile()">发送数据</button> -->
<!-- 下载进度 -->
<view class="sche-wrap" v-else>
<!-- 更新包下载中 -->
<view class="center fs14 bold c_19" style="margin:10px 0px;" v-if="pressValue==100">
数据传输完成,设备固件升级中,请等待<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="请输测试入次数"
v-model="upgradesNumber" />
</view>
<view class=""><u-button @click="startUpgrade" size="default" type="success">开始测试</u-button> </view>
<view class="me-fx-row-c bold fs18" style="padding-top:10px;"> 次数统计:{{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
},
// 弹窗宽度,可以是数值(rpx),百分比,auto等
width: {
type: [Number, String],
default: 600
},
// 是否显示Modal
value: {
type: Boolean,
default: false
},
borderRadius: {
type: [Number, String],
default: 16
},
// 是否允许点击遮罩关闭modal
maskCloseAble: {
type: Boolean,
default: false
},
// 给一个负的margin-top,往上偏移,避免和键盘重合的情况
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('升级成功状态码val---',val)
if(val==1){//升级成功
//请求版本号,对比
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:'数据传输进度: ',
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: {
//开始测试
startUpgrade(){
const that=this;
if(this.upgradesNumber==''){
that.$u.func.showToast('请输入测试次数')
return false;
}
if(that.statistics>=this.upgradesNumber){
that.$u.func.showToast('测试完成')
uni.showModal({
title:'温馨提示',
content:`测试完成-共测试${that.statistics}次`,
});
return false;
}
console.log('开始测试次数----',this.upgradesNumber);
const upgradationzl=this.$instructionList.upgradation;
const interupgradation=this.$agrFuc.getSendData(upgradationzl);
writeBLECharacteristicValue(interupgradation).then(res=>{
uni.hideLoading()
const v1=agreementFuncversionInfo(res);//设备版本号
this.bluetoothversion=v1
console.log('获取设备版本号---',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值 转16进制 32位
const baseSize=this.$agrFuc.transNumber(that.upgradList[index].fileSize) //文件大小
const code=[19];//功能码从 13 的十六进制
console.log('baseContent---1-',encryption)
const baseContent=[...code,...baseSize,...encryption] //功能码, 文件大小 , 32个m5数据 组合
console.log('baseContent---1-',baseContent)
const upgradationData=this.$agrFuc.getSendData(baseContent);
console.log('upgradationData---1-',upgradationData)
writeBLECharacteristicValue(upgradationData).then(res=>{
console.log('是否升级成功---',res);
console.log('是否升级成功-3-',res[3]);
uni.hideLoading();
const codeStats=res[3];
if(codeStats=='0'){//升级模式准备就绪 00 就是升级允许
console.log('升级模式准备就绪 00 就是升级允许--',)
that.toUpLoadFile(index)
}else{
that.upgradeError(codeStats)
}
}).catch(err=>{
uni.hideLoading()
console.log('-是否升级失败 操作失败--',err);
})
},
/*** 下载升级 下次文件 */
toUpLoadFile(index){
console.log('下载升级 下次文件 ---');
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('下载升级文件-res-',res.data);
if(res.statusCode==200){
const file=res.data;//文件 arrayBuffer 格式
const filesize=that.upgradList[index].fileSize;//文件总大小
const chunkSize=128;//每次切片数量 (app每次 256字节) (小程序 支持128字节)
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('下载升级文件-err-',err)
})
},
//持续发送数据,需要做 断点 续传
bluetoothContinuation(){
const that=this;
this.uploadChunk().then(res=>{
this.circulateNum=0;// 初始化
if(res=='success'){ //全部上传成功
this.upgradationtext='传输完成,设备固件升级中,请等待'
//判断升级是否完成
// setTimeout(()=>{
// that.upgradeCompleted()
// },2000)
}else{
//上传成功,继续上传
console.log('上传成功,继续上传--',res)
setTimeout(()=>{ // (app 每隔 50毫秒) (小程序 每隔 20毫秒)
this.bluetoothContinuation()
},20)
}
}).catch(err=>{
//发送失败 循环10次发送
this.circulateNum= this.circulateNum+1
if(this.circulateNum==10){
console.log('上传失败,继续上传---')
this.bluetoothContinuation()
}else{
uni.showModal({
title:'温馨提示',
content:'升级失败,请断开蓝牙,重新连接升级或联系管理员',
success: function (res){
if (res.confirm) {
that.pressValue=0
that.blueIndex=0
that.iscolse=true;
that.$emit('popupClose',false)
console.log('用户点击确定');
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
// this.$u.func.showToast('上传失败');
}
})
},
//传送数据处理
uploadChunk(){
const appStore =this.$store.getters;
console.log(`第${this.blueIndex}次`)
const bluetoothData= this.splitFileData[this.blueIndex];//每次切片的数据
return new Promise((resolve,reject) =>{
//总长度 大于 成功次数 ,相反的话,就说明发送成功
// 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)// 下载 进度
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('指令发送sueccess--',res)
resolve(this.blueIndex);
},
fail: (err) => {
console.log('指令发送err-err--',err)
reject(err)
}
})
resolve(this.blueIndex);
}else{
//上传成功
resolve('success')
}
})
},
//判断升级是否完成
upgradeCompleted(){
console.log('--判断升级是否完成-1-')
const that=this;
// 设备版本号和服务器版本相同 升级完成
const upgradationzl=this.$instructionList.upgradation;
const interupgradation=this.$agrFuc.getSendData(upgradationzl);
this.$store.commit('Bluetooth/set_versionInfo','');
console.log('--判断升级是否完成-2-')
writeBLECharacteristicValue(interupgradation).then(res=>{
console.log('--版本号--',res)
const versionInfo=agreementFuncversionInfo(res)
uni.hideLoading()
console.log('升级完成---',versionInfo)
const v1=versionInfo.slice(1);//设备返回 的 版本号
const v2=that.versionIndex.slice(1);//服务器放回的版本号 1.2
console.log('升级完成-版本号--',v1,v2)
if(Number(v1)==Number(v2)){
that.$u.func.showToast('升级成功')
that.pressValue=0
that.blueIndex=0
that.iscolse=true;
that.upgradationtext='数据传输进度'
that.statistics=that.statistics+1
that.startUpgrade();
}
}).catch(err=>{
// 升级失败
console.log('err---',err);
uni.hideLoading()
this.$u.func.showToast('传输异常断开,请断开蓝天,重新连接更新')
})
},
//升级失败
upgradeError(code){
if(code){
if(code==2){
this.$u.func.showToast('MD5校验失败,')
}else if(code==3){
this.$u.func.showToast('容量不足')
}else if(code==4){
this.$u.func.showToast(' 升级超时')
}
this.pressValue=0
this.blueIndex=0
this.iscolse=true;
this.$emit('popupClose',false)
}
},
// 点击遮罩关闭modal,设置v-model的值为false,否则无法第二次弹起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>
小程序 OTA升级 压力测试
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一、循环压力测试准备:添加察看结果树+添加聚合报告+添加线程组+新建HTTP请求 1.1添加察看结果树:右键Tes...
- 零基础学Load Impact 压力测试教程[Load Impact 免费网站压力测试、性能测试] 在线进行网站压...
- JMeter基于Java开发,需要系统有安装JDK环境。解压后进入bin目录,点击jmeter.bat 1、添加线...