Browse Source

update send push

pull/15/head
parent
commit
86294e52cb
3 changed files with 31 additions and 26 deletions
  1. +28
    -21
      src/app/modules/homepage/homepage/home-page.component.ts
  2. +1
    -3
      src/app/modules/homepage/security-system-details/security-system-details.component.ts
  3. +2
    -2
      src/app/shared/services/mqtt-client.service.ts

+ 28
- 21
src/app/modules/homepage/homepage/home-page.component.ts View File

@@ -1,6 +1,8 @@
import {Component, OnDestroy, OnInit} from '@angular/core';
import {SocketService} from "../../../shared/services/socket.service";
import {Subscription} from "rxjs";
import {Subscription, Subject} from "rxjs";
import {MqttClientService} from "../../../shared/services/mqtt-client.service";
import {takeUntil} from "rxjs/operators";

@Component({
selector: 'app-homepage',
@@ -16,43 +18,48 @@ export class HomePageComponent implements OnInit,OnDestroy{
time: 30,
sound: 0,
};
private statusSubscription?: Subscription;
isConnected: boolean = false;
constructor(private socketService$: SocketService,) {
this.statusSubscription = this.socketService$.status$.subscribe(
(isConnected) => {
isConnected = false;
private unsubscribeAll = new Subject();
constructor(
private socketService$: SocketService,
private mqtt$: MqttClientService
) {
this.mqtt$.status$.pipe(takeUntil(this.unsubscribeAll)).subscribe(
(isConnected: boolean) => {
this.isConnected = isConnected;
this.mqtt$.getLog();
this.mqtt$.sendPublish({ id: '0', type: 'get', whistletime: ''});
this.mqtt$.sendPublish({ id: '0', type: 'get', arlamtime: ''});
this.mqtt$.messages$.pipe(takeUntil(this.unsubscribeAll)).subscribe((message: any) => {
console.log(message);
if (message.id == '0' && message.whistletime) {
this.whistle.time = Number(message.whistletime);
}
if (message.id == '0' && message.arlamtime) {
this.alarm.time = Number(message.arlamtime);
}
});
},
);
}
ngOnInit() {
this.whistle.time = parseInt(localStorage.getItem('whistletime') ?? '10');
if (!localStorage.getItem('whistletime')) {
localStorage.setItem('whistletime', '10');
}
this.alarm.time = parseInt(localStorage.getItem('alarmtime') ?? '30');
if (!localStorage.getItem('alarmtime')) {
localStorage.setItem('alarmtime', '30');
}
}

emitSetting(type: string, event: any){
if (this.isConnected) {
console.log(event);
if (type === 'whistle') {
this.whistle.time = event;
localStorage.setItem('whistletime', event);
this.socketService$.sendMessage({ id: '0', type: 'set', whistletime: event.toString() });
this.mqtt$.sendPublish({ id: '0', type: 'set', whistletime: event.toString()});
} else if (type === 'alarm') {
this.alarm.time = event;
localStorage.setItem('alarmtime', event);
this.socketService$.sendMessage({ id: '0', type: 'set', arlamtime: event.toString() });
this.mqtt$.sendPublish({ id: '0', type: 'set', arlamtime: event.toString() });
}
}
}

ngOnDestroy(): void {
if (this.statusSubscription) {
this.statusSubscription.unsubscribe();
}
this.unsubscribeAll.next('');
this.unsubscribeAll.complete();
}
}

+ 1
- 3
src/app/modules/homepage/security-system-details/security-system-details.component.ts View File

@@ -68,9 +68,7 @@ export class SecuritySystemDetailsComponent implements OnInit, OnDestroy {
toggleState1() {
this.switchArm = !this.switchArm;
let str = {id: '0', type: 'cmd', state1: this.switchArm.toString()};
this.mqtt$.sendPublish(str).subscribe((res: any) => {
console.log('oke data', res);
});
this.mqtt$.sendPublish(str);

}


+ 2
- 2
src/app/shared/services/mqtt-client.service.ts View File

@@ -86,8 +86,8 @@ export class MqttClientService {
this.client?.unsafePublish("isoft/node 4/in4", JSON.stringify(data), {qos: 0} as IPublishOptions);
}
// 发送消息
sendPublish(data: any): Observable<any> | any {
return this.client?.publish("isoft/node 4/in4", JSON.stringify(data), {qos: 0} as IPublishOptions);
sendPublish(data: any) {
this.client?.unsafePublish("isoft/node 4/in4", JSON.stringify(data), {qos: 0} as IPublishOptions);
}
// 断开连接
destroyConnection() {

Loading…
Cancel
Save