浇花器

This commit is contained in:
3321822538@qq.com 2025-04-21 17:59:16 +08:00
parent 08a9c27934
commit f9341f88ee
3 changed files with 455 additions and 263 deletions

View File

@ -3,6 +3,10 @@
<u-navbar :is-back="true" title='定时管理' title-color="#000" :border-bottom="false" :background="bgc" id="navbar">
</u-navbar>
<image class="bj" src="https://api.ccttiot.com/smartmeter/img/static/urqUoIyCCEBykZ2oG8eO" mode=""></image>
<!-- 加载提示 -->
<u-loading-page :loading="isLoading" loading-text="数据同步中..."></u-loading-page>
<view class="" v-if="pre == 'WATER'">
<view class="list" v-if="list.length > 0 || list != null">
<view class="list_val" v-for="(values, key, index) in list" :key="key">
@ -33,7 +37,7 @@
</view>
<view class="shifen" @click="shows = true">
<text>{{minute}}</text> <text>{{second}}</text>
</view>
</view>
<view class="anniu">
<view class="qx" @click="btnqx">
取消
@ -45,9 +49,9 @@
</view>
<view class="mask" v-if="timeflag"></view>
<!-- 定时时间 -->
<u-picker v-model="show" mode="time" :params="paramss" @confirm="confirm"></u-picker>
<u-picker v-model="show" mode="time" :params="paramss" :defaultTime="defaultTime" @confirm="confirm"></u-picker>
<!-- 定时时长 -->
<u-picker v-model="shows" mode="time" :params="params" @confirm="confirms"></u-picker>
<u-picker v-model="shows" mode="time" :params="params" :defaultTime="defaultTimeLength" @confirm="confirms"></u-picker>
<view class="mask" style="z-index: 9999;" v-if="kgflag"></view>
</view>
@ -124,7 +128,20 @@
shi:'',
fen:'',
miao:'',
tian:''
tian: '',
isLoading: false, //
retryCount: 0, //
maxRetries: 3, //
lastOperation: null, //
//
defaultTime: {
hour: '00',
minute: '00'
},
defaultTimeLength: {
minute: '00',
second: '00'
},
}
},
//
@ -181,35 +198,83 @@
},
methods: {
//
btnchange(key, index, values) {
this.kgflag = true
this.btnitem(key, index, values)
this.btnqx()
this.deviceindex = index
this.devicehour = values[0]
this.deviceminute = values[1]
this.devicemiao = values[2]
if (this.list.hasOwnProperty(key)) {
const array = this.list[key]
const lastIndex = array.length - 1
const lastElement = array[lastIndex]
const lastElements = array[lastIndex]
array[lastIndex] = !lastElement
if (lastElements) {
this.deviceflag = 0
} else {
this.deviceflag = 1
}
}
let cucun = '11num' + this.deviceindex + '@' + this.devicehour + '@' + this.deviceminute + '@' + this
.devicemiao + '@' + this.deviceflag + '@'
xBlufi.notifySendCustomData({
customData: cucun
})
//
showLoading() {
this.isLoading = true;
},
//
hideLoading() {
this.isLoading = false;
setTimeout(() => {
this.kgflag = false
}, 1000)
this.isLoading = false;
}, 500);
},
//
async retryOperation() {
if (this.retryCount < this.maxRetries) {
this.retryCount++;
await this.delay(1000);
if (this.lastOperation) {
this.lastOperation();
}
} else {
this.hideLoading();
uni.showToast({
title: '操作失败,请重试',
icon: 'none'
});
this.retryCount = 0;
}
},
//
delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
//
async btnchange(key, index, values) {
try {
this.showLoading();
this.kgflag = true;
this.btnitem(key, index, values);
this.btnqx();
this.deviceindex = index;
this.devicehour = values[0] || 0; //
this.deviceminute = values[1] || 0;
this.devicemiao = values[2] || 0;
if (this.list.hasOwnProperty(key)) {
const array = this.list[key];
const lastIndex = array.length - 1;
const lastElement = array[lastIndex];
array[lastIndex] = !lastElement;
this.deviceflag = lastElement ? 0 : 1;
}
let cucun = '11num' + this.deviceindex + '@' + this.devicehour + '@' + this.deviceminute + '@' + this.devicemiao + '@' + this.deviceflag + '@';
this.lastOperation = () => {
xBlufi.notifySendCustomData({
customData: cucun
});
};
this.lastOperation();
await this.delay(1000);
xBlufi.notifySendCustomData({
customData: "11get"
});
setTimeout(() => {
this.kgflag = false;
this.hideLoading();
}, 2000);
} catch (error) {
console.error('btnchange error:', error);
this.retryOperation();
}
},
//
@ -259,65 +324,96 @@
this.second = '--'
},
//
btnqd() {
this.timeflag = false
this.hour = '--'
this.minutekq = '--'
this.minute = '--'
this.second = '--'
let cucun = '11num' + this.deviceindex + '@' + this.devicehour + '@' + this.deviceminute + '@' + this.devicemiao + '@' + this.deviceflag + '@'
xBlufi.notifySendCustomData({
customData: cucun
})
setTimeout(() => {
async btnqd() {
try {
this.showLoading();
this.timeflag = false;
this.hour = '--';
this.minutekq = '--';
this.minute = '--';
this.second = '--';
let cucun = '11num' + this.deviceindex + '@' + this.devicehour + '@' + this.deviceminute + '@' + this.devicemiao + '@' + this.deviceflag + '@';
this.lastOperation = () => {
xBlufi.notifySendCustomData({
customData: cucun
});
};
this.lastOperation();
await this.delay(1000);
xBlufi.notifySendCustomData({
customData: "11get"
})
}, 1000)
},
btnbc(){
if(this.shi >= 24){
uni.showToast({
title: '最大值为23时',
icon: 'none',
duration:2000
})
}else if(this.fen >= 60){
uni.showToast({
title: '最大值为59分钟',
icon: 'none',
duration:2000
})
}else{
this.timeflag = false
let cucun = '11num0' + '@' + this.shi + '@' + this.fen + '@' + this.miao + '@' + this.tian + '@'
xBlufi.notifySendCustomData({
customData: cucun
})
uni.showToast({
title: '保存成功',
icon: 'success',
duration:2000
})
});
setTimeout(() => {
this.hideLoading();
}, 2000);
} catch (error) {
console.error('btnqd error:', error);
this.retryOperation();
}
},
//
async btnbc() {
try {
if(this.shi >= 24){
uni.showToast({
title: '最大值为23时',
icon: 'none',
duration:2000
});
return;
} else if(this.fen >= 60){
uni.showToast({
title: '最大值为59分钟',
icon: 'none',
duration:2000
});
return;
}
this.showLoading();
this.timeflag = false;
let cucun = '11num0' + '@' + this.shi + '@' + this.fen + '@' + this.miao + '@' + this.tian + '@';
this.lastOperation = () => {
xBlufi.notifySendCustomData({
customData: "11get"
})
}, 1000)
customData: cucun
});
};
this.lastOperation();
await this.delay(1000);
xBlufi.notifySendCustomData({
customData: "11get"
});
uni.showToast({
title: '保存成功',
icon: 'success',
duration: 2000
});
setTimeout(() => {
this.hideLoading();
}, 2000);
} catch (error) {
console.error('btnbc error:', error);
this.retryOperation();
}
},
//
confirm(e) {
this.hour = e.hour
this.minutekq = e.minute
this.devicehour = e.hour
this.deviceminute = e.minute
this.hour = e.hour || '00'
this.minutekq = e.minute || '00'
this.devicehour = e.hour || '00'
this.deviceminute = e.minute || '00'
},
//
confirms(e) {
this.minute = e.minute
this.second = e.second
this.devicemiao = Number(this.minute) * 60 + Number(this.second)
this.minute = e.minute || '00'
this.second = e.second || '00'
this.devicemiao = (Number(this.minute) * 60 + Number(this.second)) || 0
console.log(this.devicemiao)
},
@ -325,117 +421,71 @@
switch (options.type) {
case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
if (!options.result) {
uni.showToast({
title: '蓝牙连接异常',
icon: 'none'
});
}
break;
case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
console.log("1收到设备发来的自定义数据结果", options.data)
const inputString = options.data.slice(0, -1) + ";"
console.log(inputString, '12121212100')
if (this.pre == 'WATER') {
const pairs = inputString.split(';')
const showObject = {}
const pSetObjects = {}
pairs.forEach(pair => {
const [key, value] = pair.split(':')
if (key === 'show') {
showObject.showArray = value.split(',').map(Number)
} else if (key.startsWith('p_set')) {
const numbers = value.split(',').map(Number)
pSetObjects[key] = numbers
}
})
this.list = pSetObjects //
const ver_Data = this.list
function secondsToMinutesAndSeconds(seconds) {
const minutes = Math.floor(seconds / 60)
const remainingSeconds = seconds % 60
return `${minutes}${remainingSeconds}`
}
//
const now = new Date()
// Date
function timeArrayToDate(timeArray) {
const [hour, minute, second] = timeArray.slice(0, 3)
const date = new Date()
date.setHours(hour, minute, second, 0) // 0
return date
}
//
function timeDifference(date1, date2) {
const diffMs = Math.abs(date1 - date2)
const diffSeconds = Math.floor(diffMs / 1000)
const diffMinutes = Math.floor(diffSeconds / 60)
const diffHours = Math.floor(diffMinutes / 60)
const remainingMinutes = diffMinutes % 60
return {
hours: diffHours,
minutes: remainingMinutes
}
}
// ver_Data
let nextTimeDiff = null
let nextTime = null
let prevTimeDiff = null
let prevTime = null
let nextWaterDuration = null //
for (const key in ver_Data) {
if (ver_Data.hasOwnProperty(key) && ver_Data[key][3] === 1) {
const timeArray = ver_Data[key]
const timeDate = timeArrayToDate(timeArray)
//
const diff = timeDifference(timeDate, now)
//
if (timeDate > now) {
if (!nextTime || diff.hours * 60 + diff.minutes < nextTimeDiff.hours * 60 +
nextTimeDiff.minutes) {
nextTimeDiff = diff
nextTime = timeDate
nextWaterDuration = secondsToMinutesAndSeconds(timeArray[2]) //
try {
console.log("收到设备发来的自定义数据结果:", options.data);
const inputString = options.data.slice(0, -1) + ";";
if (this.pre == 'WATER') {
const pairs = inputString.split(';');
const pSetObjects = {};
pairs.forEach(pair => {
if (!pair) return;
const [key, value] = pair.split(':');
if (key && key.startsWith('p_set') && value) {
try {
const numbers = value.split(',').map(num => {
const parsed = parseInt(num);
return isNaN(parsed) ? 0 : parsed;
});
pSetObjects[key] = numbers;
} catch (e) {
console.error('数据解析错误:', e);
}
} else {
if (!prevTime || (now.getTime() - timeDate.getTime()) < (now.getTime() -
prevTime.getTime())) {
prevTimeDiff = timeDifference(now, timeDate) //
prevTime = timeDate
}
});
if (Object.keys(pSetObjects).length > 0) {
this.list = pSetObjects;
for (let key in this.list) {
if (this.list.hasOwnProperty(key)) {
let arr = this.list[key];
arr[arr.length - 1] = arr[arr.length - 1] === 1;
}
}
}
}
//
this.xctime = `${nextTimeDiff.hours}${nextTimeDiff.minutes}`
for (let key in this.list) {
if (this.list.hasOwnProperty(key)) {
//
let arr = this.list[key]
//
if (arr[arr.length - 1] === 0) {
arr[arr.length - 1] = false
} else if (arr[arr.length - 1] === 1) {
arr[arr.length - 1] = true
}
} else {
const input = options.data.slice(0, -1) + ";"
//
const parts = input.replace(";", "").split(":")
//
const timeParts = parts[1].split(",")
//
this.csbobj = {
hour: parseInt(timeParts[0]), //
minute: parseInt(timeParts[1]), //
second: parseInt(timeParts[2]), //
day: parseInt(timeParts[3]) //
}
console.log(this.csbobj);
this.shi = this.csbobj.hour
this.fen = this.csbobj.minute
this.miao = this.csbobj.second
this.tian = this.csbobj.day
}
} else {
const input = options.data.slice(0, -1) + ";"
//
const parts = input.replace(";", "").split(":")
//
const timeParts = parts[1].split(",")
//
this.csbobj = {
hour: parseInt(timeParts[0]), //
minute: parseInt(timeParts[1]), //
second: parseInt(timeParts[2]), //
day: parseInt(timeParts[3]) //
}
console.log(this.csbobj);
this.shi = this.csbobj.hour
this.fen = this.csbobj.minute
this.miao = this.csbobj.second
this.tian = this.csbobj.day
this.hideLoading();
} catch (error) {
console.error('数据处理错误:', error);
this.retryOperation();
}
break
}

View File

@ -26,12 +26,12 @@
MAC{{item.mac == undefined ? item.name.slice(-12) : item.mac}}
</view>
</view>
<view class="add" @click="btnadd(item)" v-if="item.userId == undefined">
添加
</view>
<view class="add" style="color: #ccc;border: 1px solid #ccc;" v-else>
<view class="add" style="color: #ccc;border: 1px solid #ccc;" v-if="item.userId">
已添加
</view>
<view class="add" @click="btnadd(item)" v-else>
添加
</view>
</view>
</view>
</view>
@ -56,6 +56,18 @@
</view>
</view>
<!-- 自定义名称弹框 -->
<u-popup v-model="showNameDialog" mode="center" border-radius="14" width="600rpx">
<view class="custom-name-dialog">
<view class="dialog-title">设备名称</view>
<u-input v-model="customDeviceName" placeholder="请输入设备名称" />
<view class="dialog-btns">
<view class="btn cancel" @click="showNameDialog = false">取消</view>
<view class="btn confirm" @click="confirmAddDevice">确定</view>
</view>
</view>
</u-popup>
</view>
</template>
@ -77,7 +89,10 @@
userid: '',
arr: '',
jiaohuaqi: [],
getpre:[]
getpre: [],
showNameDialog: false,
customDeviceName: '',
currentDevice: null,
}
},
//
@ -124,19 +139,34 @@
}
}
})
}
}
})
},
//
//
btnadd(e) {
console.log(e, '0101');
let mac = e.slice(-12)
this.currentDevice = e;
this.customDeviceName = e.modelName || '未知设备'; // 使
this.showNameDialog = true;
},
//
confirmAddDevice() {
if (!this.customDeviceName.trim()) {
uni.showToast({
title: '请输入设备名称',
icon: 'none'
});
return;
}
let mac = this.currentDevice.name.slice(-12);
let data = {
mac: mac,
userId: this.userid,
pre: e.slice(0, 5)
pre: this.currentDevice.pre,
deviceName: this.customDeviceName
}
console.log(data);
this.$u.post(`/app/bandDevice`, data).then((res) => {
if (res.code == 200) {
uni.showToast({
@ -144,6 +174,7 @@
icon: 'success',
duration: 3000
})
this.showNameDialog = false;
setTimeout(() => {
uni.navigateBack()
}, 2000)
@ -156,75 +187,73 @@
}
})
},
getmodel() {
this.$u.get(`/app/getAllModelList`).then(res => {
if (res.code == 200) {
this.getpre = res.data;
}
});
},
getpipei(pre) {
// undefined
return this.getpre.find(item => item.pre == pre) || {
modelName: '未知型号',
picture: ''
};
},
btnss() {
uni.showLoading({
title: '搜索中...',
mask: true
});
this.jiaohuaqi = [];
this.flag = false;
const that = this;
xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent);
xBlufi.notifyStartDiscoverBle({ 'isStart': true });
setTimeout(() => {
xBlufi.notifyStartDiscoverBle({ 'isStart': false });
if (that.devicesList.length > 0) {
const data = { mac: that.arr };
this.$u.post(`/app/getExistListByMacs`, data).then(res => {
if (res.code == 200) {
// MAC
const macMap = new Map(res.data.map(item => [item.mac, item]));
that.devicesList.forEach(val => {
const mac = val.name.slice(-12);
if (macMap.has(mac)) {
that.jiaohuaqi.push(macMap.get(mac));
console.log(that.jiaohuaqi,'1111');
} else {
const pre = val.name.slice(0, 5);
const matched = that.getpipei(pre);
that.jiaohuaqi.push({
name: mac,
modelName: matched.modelName,
modelPicture: matched.picture
});
console.log(that.jiaohuaqi,'2222');
}
});
uni.hideLoading();
} else {
uni.hideLoading();
}
});
} else {
uni.hideLoading();
}
that.flag = true;
}, 3000);
},
getmodel() {
this.$u.get(`/app/getAllModelList`).then(res => {
if (res.code == 200) {
this.getpre = res.data
}
})
},
getpipei(pre) {
// undefined
return this.getpre.find(item => item.pre == pre) || {
modelName: '未知型号',
picture: ''
};
},
btnss() {
uni.showLoading({
title: '搜索中...',
mask: true
});
this.jiaohuaqi = [];
this.flag = false;
const that = this;
xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent);
xBlufi.notifyStartDiscoverBle({ 'isStart': true });
setTimeout(() => {
xBlufi.notifyStartDiscoverBle({ 'isStart': false });
if (that.devicesList.length > 0) {
const data = { mac: that.arr };
that.$u.post(`/app/getExistListByMacs`, data)
.then(res => {
if (res.code === 200) {
// MAC
const updatedDevicesList = that.devicesList.map(item => {
const matchedData = res.data.find(val => item.name.slice(-12) === val.mac);
return matchedData ? { ...item, ...matchedData } : item;
});
updatedDevicesList.forEach(val => {
const mac = val.name.slice(-12);
const pre = val.name.slice(0, 5);
const matched = that.getpipei(pre);
const userid = val.userId || null;
that.jiaohuaqi.push({
name: mac,
modelName: matched.modelName,
modelPicture: matched.picture,
pre: pre,
userId: userid
});
console.log(that.jiaohuaqi, '2222');
});
}
})
.catch(error => {
console.error('请求失败:', error);
})
.finally(() => {
uni.hideLoading();
that.flag = true;
});
} else {
uni.hideLoading();
that.flag = true;
}
}, 3000);
},
//
funListenDeviceMsgEvent: function(options) {
@ -281,6 +310,12 @@
</script>
<style lang="less">
/deep/ .u-input__input{
border: 1px solid #ccc;
border-radius: 10rpx;
padding-left: 10rpx;
box-sizing: border-box;
}
/deep/ .u-title {
margin-bottom: 22rpx;
}
@ -414,4 +449,41 @@
padding-left: 48rpx;
box-sizing: border-box;
}
.custom-name-dialog {
background: #fff;
padding: 40rpx;
.dialog-title {
font-size: 32rpx;
color: #333;
text-align: center;
margin-bottom: 30rpx;
}
.dialog-btns {
display: flex;
justify-content: space-between;
margin-top: 40rpx;
.btn {
width: 240rpx;
height: 80rpx;
line-height: 80rpx;
text-align: center;
border-radius: 40rpx;
font-size: 28rpx;
&.cancel {
background: #f5f5f5;
color: #666;
}
&.confirm {
background: #48893B;
color: #fff;
}
}
}
}
</style>

View File

@ -370,7 +370,8 @@
imgflag:true,
ver:'',
pre:'',
csbobj:{}
csbobj:{},
disconnectTimer: null,
}
},
//
@ -423,10 +424,12 @@
onHide() {
//
this.clearTimer()
this.clearDisconnectTimer()
},
onUnload() {
//
this.clearTimer()
this.clearDisconnectTimer()
},
methods: {
//
@ -560,6 +563,7 @@
//
btnyushui() {
this.handleUserAction()
this.one = false
this.ones = true
if(this.vardataflag != 3){
@ -583,6 +587,7 @@
},
//
btnyushuis() {
this.handleUserAction()
this.one = false
this.ones = true
if(this.vardataflag != 3){
@ -606,6 +611,7 @@
},
//
btnertong() {
this.handleUserAction()
this.two = false
this.twos = true
if(this.vardataflag != 3){
@ -629,6 +635,7 @@
},
//
btnertongs() {
this.handleUserAction()
this.two = false
this.twos = true
if(this.vardataflag != 3){
@ -664,6 +671,7 @@
//
confirm(e) {
this.handleUserAction()
this.minute = e.minute
this.second = e.second
this.jstime = Number(e.second) + Number(e.minute * 60)
@ -671,6 +679,7 @@
//
btntime() {
this.handleUserAction()
if(this.vardataflag != 3){
uni.showToast({
title: '蓝牙未连接',
@ -687,6 +696,7 @@
//
btnqx() {
this.handleUserAction()
this.sdjsflag = false
this.sdminutes = ''
this.sdseconds = ''
@ -696,6 +706,7 @@
},
//
btnqd() {
this.handleUserAction()
if (this.minute == '--' || this.second == '--') {
uni.showToast({
title: '请选择浇水时长',
@ -743,6 +754,7 @@
//
btnkq() {
this.handleUserAction()
console.log('关闭')
if(this.vardataflag != 3){
uni.showToast({
@ -770,6 +782,7 @@
},
//
btngb() {
this.handleUserAction()
console.log('开启')
if(this.vardataflag != 3){
uni.showToast({
@ -873,6 +886,7 @@
case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
if (!options.result) {
this.vardataflag = 1
this.clearDisconnectTimer()
}
break;
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
@ -885,11 +899,13 @@
if (options.result == true){
this.datalist = '1'
this.vardataflag = 3
this.initDisconnectTimer()
xBlufi.notifyInitBleEsp32({
deviceId: this.deviceId
})
}else{
this.vardataflag = 1
this.clearDisconnectTimer()
}
break
case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
@ -1085,6 +1101,60 @@
this.xctimesj = nextTime ? nextTime.toTimeString().slice(0, 5) : '无下次浇水时间';
console.log('下次浇水时间', this.xctimesj, '距离下次浇水时间', this.xctime, '上次浇水时间', this.sctimejs);
},
//
initDisconnectTimer() {
this.clearDisconnectTimer()
this.disconnectTimer = setTimeout(() => {
if(this.vardataflag === 3) {
console.log('一分钟无操作,断开蓝牙连接')
this.disconnectBluetooth()
}
}, 60000) // 60 = 1
},
//
resetDisconnectTimer() {
this.initDisconnectTimer()
},
//
clearDisconnectTimer() {
if(this.disconnectTimer) {
clearTimeout(this.disconnectTimer)
this.disconnectTimer = null
}
},
//
disconnectBluetooth() {
if(this.deviceId) {
wx.closeBLEConnection({
deviceId: this.deviceId,
success: () => {
console.log('蓝牙连接已断开')
this.vardataflag = 1
this.datalist = ''
uni.showToast({
title: '蓝牙已断开连接',
icon: 'none',
duration: 2000
})
}
})
xBlufi.notifyConnectBle({
isStart: false,
deviceId: this.deviceId,
name: this.name
})
}
},
//
handleUserAction() {
if(this.vardataflag === 3) {
this.resetDisconnectTimer()
}
},
}
}
</script>