|
楼主 |
发表于 2023-8-2 17:55:41
|
显示全部楼层
烟感系统暂时告一段落最终代码:
import socket from '@ohos.net.socket';
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%')
}
}
@Entry
@Component
struct taideng {
@State imageBgColorA: number = 0
@State ledon: boolean = false
@State recv: string = '0'
@State time: boolean = false
@State time1: boolean = false
@State jingbao: boolean =false
@State timer: number = 0
@State timer2: number = 0
@State timer3: number = 0
@State timer4: number = 0
@State yanwu: number = 0
@State yuzhi: number = 25
@StorageProp('currentBreakpoint') currentBreakpoint: string = 'sm'
// tcp = socket.constructTCPSocketInstance();
build() {
Scroll() {
Column() {
PageTitle()
Stack({ alignContent: Alignment.BottomStart }) {
Image(this.ledon ? $r("app.media.icon_fraction_smoke_online") : $r("app.media.icon_fraction_smoke_offline"))
.backgroundColor(`rgba(255, 255, 255, ${this.imageBgColorA})`)
.objectFit(ImageFit.Contain)
}
.height(this.currentBreakpoint == 'lg' ? 166 : 280)
List() {
ListItem() {
Column() {
Button({ type: ButtonType.Normal, stateEffect: false }) {
Row() {
Text(this.ledon ? "已打开" : "已关闭")
.fontSize(25)
.margin({ left: 15, right: 15 })
.fontWeight(FontWeight.Bold)
.align(Alignment.Center)
.width('50%')
Image(this.ledon ? $r("app.media.icon_switch_on") : $r("app.media.icon_switch_off"))
.objectFit(ImageFit.Contain)
.align(Alignment.Center)
.width('50%')
.onClick(() => {
if (!this.ledon) {
this.ledon = true
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: 'yanwuon'
//此处省略encoding, 默认为utf-8编码格式
}, err => {
if (err) {
console.log('send fail' + JSON.stringify(err));
return;
}
console.log('send yanwuon 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.yanwu = Number(str)
});
if(this.yanwu >= 21){
this.jingbao= true;
}
else
{this.jingbao= false;}
// if(this.yanwu >= this.yuzhi){
//
// this.jingbao= true;
//
// globalThis.tcp.connect({ address: globalThis.mixeraddr, timeout: 6000 }, () => {
//
// console.log('connect success');
// globalThis.tcp.send({
// data: 'baojing on'
// //此处省略encoding, 默认为utf-8编码格式
// }, err => {
// if (err) {
// console.log('send fail' + JSON.stringify(err));
// return;
// }
// console.log('send baojing on success');
// })
// })
// }
// clearInterval(this.timer4)
// this.timer4 = 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: this.yuzhi.toString()
// //此处省略encoding, 默认为utf-8编码格式
// }, err => {
// if (err) {
// console.log('send fail' + JSON.stringify(err));
// return;
// }
// console.log('send yanwuon success');
// })
// });
//
// }, 1500)
}, 2500)
}
else{
this.ledon =false
clearInterval(this.timer2);
}
})
}
}
.borderRadius(8)
.backgroundColor('#ffeee1e1')
.width('95%')
.height('12%')
.margin({ left: 15, right: 15 })
Divider().color(Color.White).strokeWidth(5)
Row(){
Text("烟雾浓度:")
.height('15%')
.fontSize(25)
.fontWeight(FontWeight.Bold)
.layoutWeight(1)
.align(Alignment.Center)
.width('50%')
Text(this.yanwu.toString()+"ppm")
.height('15%')
.fontSize(25)
.fontWeight(FontWeight.Bold)
.layoutWeight(1)
.align(Alignment.Center)
.width('50%')
}
.backgroundColor('#ffeee1e1')
.width('95%')
Divider().color(Color.White).strokeWidth(5)
Row(){
Column(){
Text("实时监测:")
.fontSize(25)
.fontWeight(FontWeight.Bold)
.layoutWeight(1)
.align(Alignment.Center)
.width('100%')
/* .onChange(()=>{
// clearInterval(this.timer4)
// this.timer4 = setInterval(() => {
if(this.yanwu >= this.yuzhi){
this.jingbao= true;
globalThis.tcp.connect({ address: globalThis.mixeraddr, timeout: 6000 }, () => {
console.log('connect success');
globalThis.tcp.send({
data: 'baojing on'
//此处省略encoding, 默认为utf-8编码格式
}, err => {
if (err) {
console.log('send fail' + JSON.stringify(err));
return;
}
console.log('send baojing on success');
})
})
}
// }, 1500)
})*/
}.width('50%')
TextInput({
placeholder: "输入阈值(默认25ppm)",
})
.width('50%')
.onChange((value)=>{
this.yuzhi = Number(value)
})
}
.borderRadius(8)
.backgroundColor('#ffeee1e1')
.width('95%')
.height('12%')
.margin({ left: 15, right: 15 })
.align(Alignment.Center)
Image(this.jingbao? $r("app.media.icon_device_alert") : null)
.height('15%').align(Alignment.Center).objectFit(ImageFit.Contain)
}
}
}
}
.alignItems(HorizontalAlign.Center)
}
.backgroundColor('#EDF2F5')
.height('100%')
.align(Alignment.Top)
}
}
|
|