完善 命令发送 初步

This commit is contained in:
3321822538@qq.com 2024-10-24 18:00:12 +08:00
parent b3da673f96
commit f6c4e7d835
2 changed files with 342 additions and 116 deletions

View File

@ -6,12 +6,12 @@
<image class="bj" src="https://api.ccttiot.com/smartmeter/img/static/urqUoIyCCEBykZ2oG8eO" mode=""></image>
<view class="list">
<view class="list_val" v-for="(values, key, index) in list" :key="key">
<view class="lt" @click="btnitem(key)">
<view class="lt" @click="btnitem(key,index,values)">
<view class="one">{{ formattedTime(values[0], values[1]) }}</view>
<view class="two">P{{ index + 1 }} <text></text> {{ formatTime(values[2]) }}</view>
</view>
<view class="rt">
<u-switch v-model="values[3]" active-color="#48893B" inactive-color="#BDBCBC"></u-switch>
<u-switch v-model="values[3]" @change="btnchange(key,index,values)" active-color="#48893B" inactive-color="#BDBCBC"></u-switch>
</view>
</view>
</view>
@ -22,7 +22,7 @@
设置浇水
</view>
<view class="ts">
距离下次浇水时间还有20小时41分
距离下次浇水时间还有{{xctime}}
</view>
<view class="shifen" @click="show = true">
<text>{{hour}}</text> <text>{{minutekq}}</text>
@ -48,10 +48,13 @@
<!-- 定时时长 -->
<u-picker v-model="shows" mode="time" :params="params" @confirm="confirms"></u-picker>
<view class="mask" style="z-index: 9999;" v-if="kgflag"></view>
</view>
</template>
<script>
var xBlufi = require("@/components/blufi/xBlufi.js")
export default {
data() {
return {
@ -86,7 +89,14 @@
hour:'--',
minutekq:'--',
list:{},
checkedStatus: {}
checkedStatus: {},
deviceindex:'',
devicemiao:'',
deviceflag:'',
devicehour:'',
deviceminute:'',
kgflag:false,
xctime:''
}
},
//
@ -105,22 +115,115 @@
}
},
onLoad(option) {
this.list = JSON.parse(option.list)
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;
}
}
}
console.log(this.list);
// this.list = JSON.parse(option.list)
// 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;
// }
// }
// }
// console.log(this.list);
},
onShow() {
xBlufi.initXBlufi(1)
xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent)
uni.getNetworkType({
success(res) {
if (res.networkType !== 'none') {
uni.getConnectedBluetoothDevices({
success(res) {
setTimeout(()=> {
xBlufi.notifySendCustomData({
customData: "11get"
})
},1000)
},
fail(err) {
console.error('获取已连接蓝牙设备信息失败:',err)
}
})
} else {
console.log('手机未连接网络')
}
}
})
},
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 + '@'
uni.getNetworkType({
success(res) {
if (res.networkType !== 'none') {
uni.getConnectedBluetoothDevices({
success(res) {
xBlufi.notifySendCustomData({
customData: cucun
})
},
fail(err) {
console.error('获取已连接蓝牙设备信息失败:',err)
}
})
} else {
console.log('手机未连接网络')
}
}
})
setTimeout(()=>{
this.kgflag = false
},1000)
},
//
btnitem(key,index,values){
this.deviceindex = index
this.timeflag = true
this.hour = values[0]
this.minutekq = values[1]
const totalSeconds = values[2]
const minutes = Math.floor(totalSeconds / 60)
const seconds = totalSeconds % 60
const formattedMinutes = minutes < 10 ? '0' + minutes : minutes.toString()
const formattedSeconds = seconds < 10 ? '0' + seconds : seconds.toString()
this.minute = formattedMinutes
this.second = formattedSeconds
if (this.list.hasOwnProperty(key)) {
const array = this.list[key]
const lastElement = array[array.length - 1]
if (lastElement) {
this.deviceflag = 1
} else {
this.deviceflag = 0
}
}
},
formattedTime(minutes, seconds) {
//
const formattedMinutes = String(minutes).padStart(2, '0');
@ -134,29 +237,164 @@
const remainingSeconds = seconds % 60;
return `${minutes}${remainingSeconds < 10 ? '0' : ''}${remainingSeconds}`;
},
//
btnitem(){
this.timeflag = true
},
//
btnqx(){
this.timeflag = false
this.hour = '--'
this.minutekq = '--'
this.minute = '--'
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 + '@'
uni.getNetworkType({
success(res) {
if (res.networkType !== 'none') {
uni.getConnectedBluetoothDevices({
success(res) {
xBlufi.notifySendCustomData({
customData: cucun
})
setTimeout(()=> {
xBlufi.notifySendCustomData({
customData: "11get"
})
},1000)
},
fail(err) {
console.error('获取已连接蓝牙设备信息失败:',err)
}
})
} else {
console.log('手机未连接网络')
}
}
})
},
//
confirm(e){
this.hour = e.hour
this.minutekq = e.minute
this.devicehour = e.hour
this.deviceminute = e.minute
},
//
confirms(e){
this.minute = e.minute
this.second = e.second
}
this.devicemiao = Number(this.minute) * 60 + Number(this.second)
console.log(this.devicemiao);
},
funListenDeviceMsgEvent: function(options) {
switch (options.type) {
case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
if (!options.result) {
uni.showModal({
title: '很抱歉提醒你!',
content: '小程序与设备异常断开',
showCancel: false,
//
success: function(res) {
uni.hideLoading()
uni.removeStorage({key:'device_key'})
}
})
}
break;
case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
console.log("1收到设备发来的自定义数据结果", options.data)
const inputString = options.data
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]); //
}
} else {
if (!prevTime || (now.getTime() - timeDate.getTime()) < (now.getTime() - prevTime.getTime())) {
prevTimeDiff = timeDifference(now, timeDate); //
prevTime = timeDate;
}
}
}
}
//
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;
}
}
}
break
}
},
}
}

View File

@ -57,13 +57,13 @@
</image> 手动浇水
</view>
<view class="shengyu">
剩余5分钟
剩余{{ sdminutes ? sdminutes + ':' : '' }}{{ sdseconds ? sdseconds : '--' }}
</view>
</view>
</view>
<view class="deviceweek">
<view class="weeklist">
<view class="kg" v-if="ver_data.p_set5[3] == 1">
<view class="kg" v-if="ver_data.p_set0[3] == 0">
OFF
</view>
<view class="kg" style="color: #48893B;" v-else>
@ -78,7 +78,7 @@
</view>
</view>
<view class="weeklist">
<view class="kg" v-if="ver_data.p_set4[3] == 1">
<view class="kg" v-if="ver_data.p_set1[3] == 0">
OFF
</view>
<view class="kg" style="color: #48893B;" v-else>
@ -93,7 +93,7 @@
</view>
</view>
<view class="weeklist">
<view class="kg" v-if="ver_data.p_set3[3] == 1">
<view class="kg" v-if="ver_data.p_set2[3] == 0">
OFF
</view>
<view class="kg" style="color: #48893B;" v-else>
@ -108,7 +108,7 @@
</view>
</view>
<view class="weeklist">
<view class="kg" v-if="ver_data.p_set2[3] == 1">
<view class="kg" v-if="ver_data.p_set3[3] == 0">
OFF
</view>
<view class="kg" style="color: #48893B;" v-else>
@ -123,7 +123,7 @@
</view>
</view>
<view class="weeklist">
<view class="kg" v-if="ver_data.p_set1[3] == 1">
<view class="kg" v-if="ver_data.p_set4[3] == 0">
OFF
</view>
<view class="kg" style="color: #48893B;" v-else>
@ -138,7 +138,7 @@
</view>
</view>
<view class="weeklist">
<view class="kg" v-if="ver_data.p_set0[3] == 1">
<view class="kg" v-if="ver_data.p_set5[3] == 0">
OFF
</view>
<view class="kg" style="color: #48893B;" v-else>
@ -225,7 +225,7 @@
<view class="toptit">
选择浇水时长
</view>
<view class="container" @click="showjs = true">
<view class="container" @click="btnshowjs">
<view class="">{{minute}}</view>
<text>:</text>
<view class="">{{second}}</view>
@ -250,19 +250,15 @@
<script>
var xBlufi = require("@/components/blufi/xBlufi.js")
import TimePicker from '@/components/TimePicker/index.vue'
export default {
components: {
TimePicker
},
data() {
return {
one:false,
ones:true,
two:false,
twos:true,
yschecked: '',
etchecked: '',
yschecked: true,
etchecked:true,
kgflag: false,
activeshu: 0,
xuanzeflag: false,
@ -291,7 +287,9 @@
xctime:'--',
sctimejs:'--',
xctimesc:'--',
xctimesj:''
xctimesj:'',
sdminutes:'',
sdseconds:''
}
},
//
@ -332,11 +330,15 @@
},
onShow() {
this.getinfo()
},
mounted() {
this.getshuju()
},
methods: {
//
btnshowjs(){
this.showjs = true
},
formattedTime(minutes, seconds) {
//
const formattedMinutes = String(minutes).padStart(2, '0');
@ -379,74 +381,7 @@
}
})
},
kq(){
uni.getNetworkType({
success(res) {
if (res.networkType !== 'none') {
uni.getConnectedBluetoothDevices({
success(res) {
setTimeout(()=> {
xBlufi.notifySendCustomData({
customData:'11open'
})
},1500)
},
fail(err) {
console.error('获取已连接蓝牙设备信息失败:',err)
}
})
} else {
console.log('手机未连接网络')
}
}
})
},
gb(){
uni.getNetworkType({
success(res) {
if (res.networkType !== 'none') {
uni.getConnectedBluetoothDevices({
success(res) {
setTimeout(()=> {
xBlufi.notifySendCustomData({
customData:'11close'
})
},1500)
},
fail(err) {
console.error('获取已连接蓝牙设备信息失败:',err)
}
})
} else {
console.log('手机未连接网络')
}
}
})
},
hq(){
uni.getNetworkType({
success(res) {
if (res.networkType !== 'none') {
uni.getConnectedBluetoothDevices({
success(res) {
setTimeout(()=> {
xBlufi.notifySendCustomData({
customData:'11get'
})
},1500)
},
fail(err) {
console.error('获取已连接蓝牙设备信息失败:',err)
}
})
} else {
console.log('手机未连接网络')
}
}
})
},
//
//
btnyushui(){
let that = this
uni.getNetworkType({
@ -471,6 +406,7 @@
}
})
},
//
btnyushuis(){
let that = this
uni.getNetworkType({
@ -495,7 +431,7 @@
}
})
},
//
//
btnertong(){
let that = this
uni.getNetworkType({
@ -520,6 +456,7 @@
}
})
},
//
btnertongs(){
let that = this
uni.getNetworkType({
@ -570,6 +507,7 @@
btnqx(){
this.sdjsflag = false
},
//
btnqd(){
if(this.minute == '--' || this.second == '--'){
uni.showToast({
@ -588,6 +526,7 @@
xBlufi.notifySendCustomData({
customData: "11time@" + that.jstime + '@'
})
that.startTimer(that.jstime)
that.sdjsflag = false
that.kgflag = true
},1500)
@ -603,8 +542,32 @@
})
}
},
//
startTimer(totalSeconds) {
let remainingSeconds = totalSeconds
console.log(remainingSeconds,'000');
const timer = setInterval(() => {
//
const minutes = Math.floor(remainingSeconds / 60).toString()
const secs = (remainingSeconds % 60).toString()
//
this.sdseconds = secs.padStart(2, '0')
// 0
this.sdminutes = minutes.padStart(2, '0')
//
remainingSeconds--;
// 0
if (remainingSeconds <= 0) {
clearInterval(timer)
this.btnkq()
this.sdminutes = ''
this.sdseconds = ''
}
}, 1000)
},
//
//
btnkq() {
let that = this
uni.getNetworkType({
@ -681,7 +644,7 @@
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
if (options.result) {
let devicesarr = options.data
console.log(devicesarr,'111')
// console.log(devicesarr,'111')
devicesarr.forEach(device => {
const mac = device.name.substring(4)
if (device.name.slice(5, 17) == this.mac) {
@ -740,10 +703,12 @@
showObject.showArray = value.split(',').map(Number);
} else if (key.startsWith('p_set')) {
const numbers = value.split(',').map(Number);
pSetObjects[key] = numbers;
pSetObjects[key] = numbers
}
})
this.ver_data = pSetObjects //
console.log(this.ver_data,'002002002')
this.showobj = showObject //
if(this.showobj.showArray[1] == 1){
this.yschecked = false
@ -814,12 +779,13 @@
this.sctimejs = timesc.slice(0,-3)
let xcjs = nextTime.toTimeString().split(' ')[0]
this.xctimesj = xcjs.slice(0,-3)
console.log(xcjs,'121212',this.formattedTime(this.ver_data.p_set5[0],this.ver_data.p_set5[1]))
// console.log(':', nextTimeDiff ? `${nextTimeDiff.hours}${nextTimeDiff.minutes}` : '');
// console.log(':', nextTime ? nextTime.toTimeString().split(' ')[0] : '');
// console.log(':', nextWaterDuration || '');
// console.log(':', prevTimeDiff ? `${prevTimeDiff.hours}${prevTimeDiff.minutes}` : '');
// console.log(':', prevTime ? prevTime.toTimeString().split(' ')[0] : '')
break
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
@ -843,8 +809,30 @@
}
},
//
getshuju(){
const intervalId = setInterval(() => {
uni.getNetworkType({
success(res) {
if (res.networkType !== 'none') {
uni.getConnectedBluetoothDevices({
success(res) {
setTimeout(()=> {
xBlufi.notifySendCustomData({
customData: "11get"
})
},1000)
},
fail(err) {
console.error('获取已连接蓝牙设备信息失败:',err)
}
})
}
}
})
console.log("定时器每分钟执行一次");
}, 60000)
}
}
}