|
楼主 |
发表于 2023-8-4 17:13:18
|
显示全部楼层
BQ3568前端实现报警代码如下,点击开启后,有人靠近会报警,记录靠近次数,靠近会有图片与文字同步变化:
- import router from '@ohos.router'
- import promptAction from '@ohos.promptAction'
- @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 oneTable {
- @State message: string = ''
- @State imageBgColorA: number = 0
- @StorageProp('currentBreakpoint') currentBreakpoint: string = 'sm'
- @State jingbao: number = 0
- @State cishu: number = 0
- private timer: number = 0
- @Builder IngredientItem() {
- Stack({ alignContent: Alignment.BottomStart }) {
- Image(this.jingbao ? $r("app.media.ren_youren") : $r("app.media.ren_wuren0"))
- .backgroundColor(`rgba(255, 255, 255, ${this.imageBgColorA})`)
- .objectFit(ImageFit.Contain)
- Text(this.jingbao ? "有人靠近" :"无人靠近" )
- .align(Alignment.Center).width('50%').fontSize(25).height('10%')
- }
- .height(this.currentBreakpoint == 'lg' ? 166 : 280)
- Column() {
- Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center }) {
- Column() {
- Row() {
- Text("人体感应")
- .fontSize(18)
- .fontWeight(FontWeight.Bold)
- .layoutWeight(1)
- .align(Alignment.Start)
- Row() {
- Button('开', { type: ButtonType.Normal, stateEffect: true })
- .borderRadius(8)
- .backgroundColor(0x317aff)
- .width(90)
- .onClick(() => {
- promptAction.showToast({
- message: '人体感应已打开',
- duration: 1500
- });
- 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: 'rentiganyingon'
- //此处省略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 = ""
- /* clearInterval(this.timer)
- this.timer = setTimeout(() => {
- }, 500)*/
- for (let i = 0; i < dataView.byteLength; ++i) {
- str += String.fromCharCode(dataView.getUint8(i))
- }
- console.log("on connect received:" + str)
- if(Boolean(str)){
- this.cishu ++;
- this.jingbao = 1;
- clearInterval(this.timer)
- this.timer = setTimeout(() => {
- this.jingbao = 0
- }, 2000)
- }
- // else{
- // }
- });
- })
- Blank()
- Button('关', { type: ButtonType.Normal, stateEffect: true })
- .borderRadius(8)
- .backgroundColor(0x317aff)
- .width(90)
- .onClick(() => {
- promptAction.showToast({
- message: '人体感应已关闭',
- duration: 1500
- });
- 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: 'rentiganyingoff'
- //此处省略encoding, 默认为utf-8编码格式
- }, err => {
- if (err) {
- console.log('send fail' + JSON.stringify(err));
- return;
- }
- console.log('send success');
- })
- })
- })
- }
- .width('100%')
- .layoutWeight(2)
- }
- .padding(20)
- .margin({ bottom: 20 })
- Row(){
- Text('报警日志:')
- .align(Alignment.Center)
- .width('40%')
- Text(this.cishu.toString()+"次")
- .width('40%').align(Alignment.Center)
- }
- }
- }
- }.height('50%')
- .backgroundColor(Color.White)
- }
- build() {
- Column() {
- this.IngredientItem()
- }
- }
- }
- @Entry
- @Component
- struct fengshan {
- build() {
- Scroll() {
- Column() {
- PageTitle()
- Swiper() {
- oneTable()
- }
- .clip(new Rect().width('100%').height('100%').radiusWidth(15).radiusHeight(15))
- .itemSpace(20)
- .height(630)
- .indicatorStyle({ selectedColor: Color.Green })
- .margin({ top: 10, right: 10, left: 10 })
- }
- .alignItems(HorizontalAlign.Center)
- }
- .backgroundColor('#EDF2F5')
- .height('100%')
- .align(Alignment.Top)
- }
- }
复制代码 |
|