|
|
|
@@ -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(); |
|
|
|
} |
|
|
|
} |