kitchendDevice/components/bluetooth_food.vue

445 lines
9.6 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view class="weightPages">
<view class="table">
<view class="text">
<image src="/static/zhong.png"></image>
<text @click="openBluetoothAdapter">{{bletipstext}}</text>
</view>
<view class="duan" @click="handleBack" v-if="isShow&&isConnection == 2">
{{$t('Disconnect')}}
</view>
</view>
<!-- -->
<view class="weight-wrap">
<!-- 蓝牙称重 -->
<view class="weight" @click="inputDialogToggle">
<text class="val">{{weight == '' ? '0.0':weight}}</text>
<text class="unit">{{unitConversion(unit)}}</text>
</view>
</view>
<!-- -->
<view class="groupbtn">
<view class="btn danwei">
<view class="lan border-bottom">
<view class="right">
<picker mode="selector" :range="unitList" range-key="name" @change="changleUnits"
:value="unitListIndex">
<view class="uni-input">
{{$t('UnitBtn')}}
</view>
</picker>
</view>
</view>
</view>
<view class="btn addbtn size14" @click="handlesub" v-if="btnType==2">{{$t('Save')}}</view>
<view class="btn addbtn" @click="handleAddFood" v-if="btnType==1">+</view>
<view class="btn qingling" @click="handleqingling">{{$t('resetBtn')}}</view>
</view>
<!-- -->
<view>
<uni-popup ref="popup" type="dialog">
<uni-popup-dialog mode="input" :title="$t('Weight')" :placeholder="$t('RecordWeight')" @close="close"
@confirm="confirm"></uni-popup-dialog>
</uni-popup>
</view>
</view>
</template>
<script>
import {
mapState
} from "vuex";
let myTime
let nextCnt = 0
export default {
data() {
return {
kcal: 0,
unit: '',
// weight: "",
inputDialog: false,
unitList: [{
name: this.$t("g"),
id: '00',
unit: "g"
}, {
name: this.$t("oz"),
id: "08",
unit: "oz"
},
{
name: this.$t("lb"),
id: "03",
unit: "lb"
}, {
name: this.$t("ml"),
id: "05",
unit: "ml"
}
],
unitListIndex: 0,
units: ['kg', 'g', 'st:lb', 'lb', 'g', 'ml', 'Waterml',
'milkml', 'oz', 'floz', 'lboz'
]
}
},
props: {
weightKcal: {
type: Number,
default: 0 //当前测量食物每100g含的kcal
},
btnType: {
type: Number,
default: 1 //1添加食材2保存测量
},
},
computed: {
...mapState(["bleValue", "isBluetoothTyle", "countFoodInfo"]),
weight() {
let that = this
let kcal = (Number(that.weightKcal) / 100 * that.bleValue.countWeight).toFixed(2)
that.unit = that.bleValue.unit
that.kcal = that.convertToGrams(kcal, that.bleValue.unit).toFixed(2)
return this.bleValue.countWeight
},
isConnection() {
return this.bleValue.isConnectStatus ? this.bleValue.isConnectStatus : 1
},
isShow() {
return this.bleValue.serviceId != '' ? true : false
},
bletipstext() {
return this.bleValue.bleTipsText ? this.bleValue.bleTipsText : this.$t('ConnectionTimeout')
}
},
mounted() {
let that = this
uni.onBluetoothAdapterStateChange(function(res) {
that.$store.commit("changeBluetooth", res.available);
})
},
destroyed() {
// this.isConnection = 1
// this.closeBLEConnection()
// this.closeBluetoothAdapter()
},
watch: {
isBluetoothTyle: function() {
let that = this
if (!that.isBluetoothTyle) {
that.handleBack()
}
}
},
methods: {
// 初始化蓝牙
openBluetoothAdapter() {
let that = this
if (that.isConnection == 0) return
that.kcal = ""
that.$store.commit('changeBluetoothValue', {
deviceId: "",
serviceId: "",
notify: '',
write: '',
unit: "g",
type: 1,
countWeight: "",
bleTipsText: that.$t('SearchBluetooth'),
isConnectStatus: 0,
})
that.$ble.openBluetoothAdapter()
},
changleUnits(e) {
let that = this
let unit = that.unitList[e.detail.value].unit
if (that.isShow && that.unit != unit) {
that.handletoggleUnit(that.unitConversion2(unit))
}
that.unitListIndex = [e.detail.value]
that.$store.commit('changeBluetoothValue', {
unit: unit
})
console.log("单位切换", unit, that.unit, that.unitListIndex)
},
unitConversion2(unit) {
if (unit == 'kg') {
return 0x00
} else if (unit == '斤') {
return 0x01
} else if (unit == 'st:lb') {
return 0x02
} else if (unit == 'lb') {
return 0x03
} else if (unit == 'g') {
return 0x04
} else if (unit == 'ml') {
return 0x05
} else if (unit == 'Waterml') {
return 0x06
} else if (unit == 'milkml') {
return 0x07
} else if (unit == 'oz') {
return 0x08
} else if (unit == 'floz') {
return 0x09
} else if (unit == 'lboz') {
return 0x0A
}
return unit
},
handletoggleUnit(unit) {
let that = this
let checksum = 0;
const bytes = [0xC5, 0x03, 0x05, 0x11]
bytes[4] = unit
for (let i = 0; i < bytes.length; i++) {
checksum ^= bytes[i];
}
bytes[5] = checksum
that.sendData(new Uint8Array(bytes).buffer)
},
handleqingling() {
let that = this
let str = "C503071100D0"
let buf = new Uint8Array(str.match(/[\da-f]{2}/gi).map(function(h) {
return parseInt(h, 16)
}))
that.sendData(buf.buffer)
},
sendData(buffer) {
let that = this
uni.writeBLECharacteristicValue({
deviceId: that.bleValue.deviceId,
serviceId: that.bleValue.serviceId,
characteristicId: that.bleValue.write,
value: buffer,
success: res => {
console.log('下发指令成功', res.errMsg)
},
fail: res => {
console.log("下发指令失败", res);
},
})
},
// 保存测量结果
handlesub() {
let that = this
console.log("测量保存", that.weight, that.unit, that.kcal)
if (Number(that.weight) > 0) {
that.$emit("handleBle", that.weight, that.unit, that.kcal)
} else {
that.$tools.msg(that.$t('reset'))
}
},
handleBack() {
let that = this
that.$store.commit("changeBluetoothValue", {
bleTipsText: that.$t('ConnectionTimeout'),
isConnectStatus: 1
})
that.$ble.stopBluetoothDevicesDiscovery() //取消蓝牙搜索
that.$ble.closeBLEConnection(that.bleValue.deviceId)
that.$ble.closeBluetoothAdapter()
},
convertToGrams(value, fromUnit) {
const conversionFactors = {
'lb': 453.59237, // 1磅 = 453.59237克
'oz': 28.349523125, // 1盎司 = 28.349523125克
'kg': 1000, // 1公斤 = 1000克
'g': 1,
'ml': 1,
};
if (!conversionFactors.hasOwnProperty(fromUnit)) {
return ''
}
return value * conversionFactors[fromUnit];
},
unitConversion(unit) {
if (unit == 'kg') {
return this.$t("kg")
} else if (unit == 'g') {
return this.$t("g")
} else if (unit == 'lb') {
return this.$t("lb")
} else if (unit == 'oz') {
return this.$t("oz")
} else if (unit == 'ml') {
return this.$t("ml")
}
return unit
},
// 添加食物
handleAddFood() {
uni.navigateTo({
url: "/pageTwo/count/search?index=0&time=" + this.countFoodInfo.date
})
},
inputDialogToggle() {
this.$refs.popup.open()
},
confirm(value) {
console.log(value)
this.$store.commit("changeBluetoothValue", {
countWeight: value,
unit: this.unitList[this.unitListIndex].unit
})
this.$refs.popup.close()
},
close() {
this.$refs.popup.close()
},
},
}
</script>
<style scoped lang="scss">
image {
width: 22px;
height: 22px;
}
.more {
padding: 6rpx 10rpx;
border-radius: 12rpx;
color: #fff;
background-color: #f0ae43;
}
.weightPages {
display: flex;
flex-wrap: wrap;
flex-direction: column;
position: relative;
background: #fff;
border-radius: 20rpx;
justify-content: space-around;
.weight-wrap {
display: flex;
justify-content: center;
align-items: center;
background: #fff;
color: #666;
font-size: 16px;
text-align: center;
height: 60px;
margin: 10px 0;
.weight,
.kcal {
display: flex;
justify-content: center;
align-items: center;
width: 70%;
padding: 30rpx 0;
border-radius: 20rpx;
background-color: #F8F8F8;
}
.weight {
.val {
font-size: 54rpx;
color: #F0AE43;
margin: 0 !important;
}
.unit {
padding: 10rpx;
margin-left: 30rpx;
font-size: 28rpx;
color: #fff;
border-radius: 8rpx;
background-color: #F0AE43;
}
}
}
.tips {
font-size: 24rpx;
text-align: center;
}
.groupbtn {
margin-top: 15px;
display: flex;
align-items: center;
justify-content: space-between;
.btn {
width: 30%;
color: $maincolor;
text-align: center;
height: 40px;
line-height: 40px;
border-radius: 10px;
border: 1px solid $maincolor;
background: #fff;
margin: 0;
}
.addbtn {
width: 30%;
color: #fff;
font-size: 45px;
line-height: 38px;
background: $maincolor;
}
}
.table {
width: 100%;
font-size: 14px;
align-items: center;
padding: 5px 0;
border-radius: 5px;
display: flex;
justify-content: space-between;
.text {
color: #8284f0;
display: flex;
align-items: center;
}
image {
width: 22px;
height: 22px;
margin-right: 5px;
margin-left: -5px;
}
}
.image {
width: 1120rpx;
height: 1120rpx;
margin: auto;
image {
width: 100%;
height: 100%;
}
}
.duan {
width: fit-content;
background: linear-gradient(-90deg, #d4f5c4, #a7d5e4 80%, );
border-radius: 5px;
text-align: center;
padding: 3px 10px;
font-size: 12px;
}
.tips {
margin-top: 30rpx;
margin-left: 30rpx;
display: flex;
color: #999;
}
}
</style>