|
楼主 |
发表于 2023-7-31 17:06:41
|
显示全部楼层
想实现BQ3568间隔1s获取一次值,则需要间隔1s向hi3861发送指令。此处使用定时器setTimeout()与setInterval(),前者延时s秒执行一次,后者间隔s秒重复执行,开始时都用重复执行,发现TCP传输会有阻塞,然后先用setTimeout()滞后湿度获取500ms,实现实时变化。
dev代码:
import router from '@ohos.router'
@Component
struct PageTitle {
build() {
Row() {
Image($r('app.media.back'))
.width(20)
.height(20)
.onClick(() => {
router.back()
})
Text("窗帘")
.fontSize(22)
.margin({ left: 20 })
}
.padding(12)
.width('100%')
}
}
@Component
struct FoodImageDisplay {
@State imageBgColorA: number = 0
@StorageProp('currentBreakpoint') currentBreakpoint: string = 'sm'
build() {
Stack({ alignContent: Alignment.BottomStart }) {
Image($r("app.media.icon_humidifier"))
.backgroundColor(`rgba(255, 255, 255, ${this.imageBgColorA})`)
.objectFit(ImageFit.Contain)
}
.height(this.currentBreakpoint == 'lg' ? 166 : 280)
}
}
@Component
struct oneTable {
@State message: string = ''
@State wendu: string = '0'
@State shidu: string = '0'
private timer: number = 0
private timer1: number = 0
private timer2: number = 0
@State j :number =0
@Builder IngredientItem() {
Column() {
Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.SpaceEvenly }) {
Column() {
Row() {
Column(){
Text("室内温度")
.fontSize(18)
.fontWeight(FontWeight.Bold)
.layoutWeight(1)
.align(Alignment.Start)
Blank()
Text(this.wendu)
.onAppear(()=>{
clearInterval(this.timer)
this.timer = setInterval(() => {
globalThis.tcp.connect({ address: globalThis.mixeraddr , timeout: 6000 }, () => {
// 电脑ip
// tcp.connect({ address: { address: '192.168.2.125', port: 3861, family: 1 }, timeout: 6000 }, () => {
console.log('connect success');
globalThis.tcp.send({
data: 'wendu'
//此处省略encoding, 默认为utf-8编码格式
}, err => {
if (err) {
console.log('send fail' + JSON.stringify(err));
return;
}
console.log('send success');
})
})
//接收数据:
globalThis.tcp.on('message', value => {
console.log("on message")
let buffer = value.message
let dataView = new DataView(buffer)
let str = ""
for (let i = 0; i < dataView.byteLength; ++i) {
str += String.fromCharCode(dataView.getUint8(i))
}
console.log("on connect received:" + str)
this.wendu = JSON.stringify(str)+ "℃"
});
}, 1500)
})
}
Blank(80)
Column(){
Text("室内湿度")
.fontSize(18)
.fontWeight(FontWeight.Bold)
.layoutWeight(1)
.align(Alignment.Start)
Blank()
Text(this.shidu)
.onAppear(()=>{
clearInterval(this.timer1)
this.timer1 = setTimeout(() => {
clearInterval(this.timer2)
this.timer2 = setInterval(() => {
globalThis.tcp.connect({ address: globalThis.mixeraddr , timeout: 6000 }, () => {
// 电脑ip
// tcp.connect({ address: { address: '192.168.2.125', port: 3861, family: 1 }, timeout: 6000 }, () => {
console.log('connect success');
globalThis.tcp.send({
data: 'shidu'
//此处省略encoding, 默认为utf-8编码格式
}, err => {
if (err) {
console.log('send fail' + JSON.stringify(err));
return;
}
console.log('send success');
})
})
//接收数据:
globalThis.tcp.on('message', value => {
console.log("on message")
let buffer = value.message
let dataView = new DataView(buffer)
let str = ""
for (let i = 0; i < dataView.byteLength; ++i) {
str += String.fromCharCode(dataView.getUint8(i))
}
console.log("on connect received:" + str)
this.shidu = JSON.stringify(str) + "%rh"
});
}, 1500)
}, 500)
})
.onDisAppear(()=>{
clearInterval(this.timer2);
clearInterval(this.timer);
})
}
}.height('40%')
Divider().strokeWidth(10).color(Color.White)
Text("温湿度加湿器")
.fontSize(18)
.fontWeight(FontWeight.Bold)
.layoutWeight(1)
.align(Alignment.Start)
.height('20%')
Divider().strokeWidth(10).color(Color.White)
Row() {
Button('开', { type: ButtonType.Normal, stateEffect: true })
.borderRadius(8)
.backgroundColor(0x317aff)
.width(90)
.onClick(() => {
this.message = '智能加湿器已打开'
globalThis.tcp.connect({ address: globalThis.mixeraddr , timeout: 6000 }, () => {
// 电脑ip
// tcp.connect({ address: { address: '192.168.2.125', port: 3861, family: 1 }, timeout: 6000 }, () => {
console.log('connect success');
globalThis.tcp.send({
data: 'jiashiqikai'
//此处省略encoding, 默认为utf-8编码格式
}, err => {
if (err) {
console.log('send fail' + JSON.stringify(err));
return;
}
console.log('send success');
})
})
})
Blank(20)
Button('关', { type: ButtonType.Normal, stateEffect: true })
.borderRadius(8)
.backgroundColor(0x317aff)
.width(90)
.onClick(() => {
this.message = '智能加湿器已关闭'
globalThis.tcp.connect({ address: globalThis.mixeraddr , timeout: 6000 }, () => {
// 电脑ip
// tcp.connect({ address: { address: '192.168.2.125', port: 3861, family: 1 }, timeout: 6000 }, () => {
console.log('connect success');
globalThis.tcp.send({
data: 'jiashiqiguan'
//此处省略encoding, 默认为utf-8编码格式
}, err => {
if (err) {
console.log('send fail' + JSON.stringify(err));
return;
}
console.log('send success');
})
})
})
}
.width('60%')
.layoutWeight(2)
.height('15%')
Divider().strokeWidth(10).color(Color.White)
Text(this.message)
.height('15%')
.fontSize(18)
.fontWeight(FontWeight.Bold)
.layoutWeight(1)
.align(Alignment.Start)
}
.height('50%')
}
}
.backgroundColor(Color.White)
.width('96%')
}
build() {
Column() {
this.IngredientItem()
}
}
}
@Preview
@Entry
@Component
struct fengshan {
build() {
Scroll() {
Column() {
PageTitle()
FoodImageDisplay()
Swiper() {
oneTable()
}
.clip(new Rect().width('100%').height('100%').radiusWidth(15).radiusHeight(15))
.itemSpace(20)
.height(380)
.indicatorStyle({ selectedColor: Color.Green })
.margin({ top: 10, right: 10, left: 10 })
}
.alignItems(HorizontalAlign.Center)
}
.backgroundColor('#EDF2F5')
.height('100%')
.align(Alignment.Top)
}
}
|
|