| <router-outlet></router-outlet> | <router-outlet></router-outlet> | ||||
| <audio #audioPlayer src="assets/sound/alarm2.mp3" loop muted style="display: none"></audio> | |||||
| <button #clickButton (click)="alarmSoundService$.playSound()">Click me to play sound</button> | |||||
| import {Component, ElementRef, OnInit, ViewChild} from '@angular/core'; | |||||
| import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core'; | |||||
| import {SocketService} from "./shared/services/socket.service"; | import {SocketService} from "./shared/services/socket.service"; | ||||
| import {AlarmSoundService} from "./shared/services/alarm-sound.service"; | import {AlarmSoundService} from "./shared/services/alarm-sound.service"; | ||||
| templateUrl: './app.component.html', | templateUrl: './app.component.html', | ||||
| styleUrls: ['./app.component.scss'] | styleUrls: ['./app.component.scss'] | ||||
| }) | }) | ||||
| export class AppComponent implements OnInit{ | |||||
| export class AppComponent implements OnInit, AfterViewInit { | |||||
| title = 'Iot-web-ui'; | title = 'Iot-web-ui'; | ||||
| @ViewChild('audioPlayer', { static: true }) audioPlayer!: ElementRef<HTMLAudioElement>; | |||||
| @ViewChild('clickButton') clickButton!: ElementRef<HTMLButtonElement>; | |||||
| constructor(private socketService$: SocketService, | constructor(private socketService$: SocketService, | ||||
| private alarmSoundService$: AlarmSoundService) { | |||||
| public alarmSoundService$: AlarmSoundService,) { | |||||
| } | } | ||||
| ngOnInit() { | ngOnInit() { | ||||
| this.socketService$.connect(); | this.socketService$.connect(); | ||||
| this.alarmSoundService$.setSound(this.audioPlayer); | |||||
| } | |||||
| ngAfterViewInit(): void { | |||||
| console.log('click'); | |||||
| this.simulateButtonClick(); | |||||
| } | |||||
| simulateButtonClick() { | |||||
| this.alarmSoundService$.simulateClick(this.clickButton.nativeElement); | |||||
| } | } | ||||
| } | } |
| this.state2 = message.state2 === '0'; // 1 OFF // alarm 1h | this.state2 = message.state2 === '0'; // 1 OFF // alarm 1h | ||||
| this.state5 = message.state5 === '1'; // 1 ON, 0 OFF | this.state5 = message.state5 === '1'; // 1 ON, 0 OFF | ||||
| this.isReady = message.ready === '1'; | this.isReady = message.ready === '1'; | ||||
| this.alarmSoundService$.startAlarm(this.state5, this.isReady, this.state1, this.state2); | |||||
| // this.alarmSoundService$.startAlarm(true, true, true, this.state2); | |||||
| this.updateIcons(); | this.updateIcons(); | ||||
| } | } | ||||
| } | } |
| providedIn: 'root' | providedIn: 'root' | ||||
| }) | }) | ||||
| export class AlarmSoundService { | export class AlarmSoundService { | ||||
| private audioPlayer!: HTMLAudioElement; | |||||
| private alertInterval: any; | private alertInterval: any; | ||||
| private alertDuration: number = 30000; | private alertDuration: number = 30000; | ||||
| constructor() { } | |||||
| setSound(audioPlayer: ElementRef<HTMLAudioElement>): void { | |||||
| this.audioPlayer = audioPlayer.nativeElement; | |||||
| private audio= new Audio(); | |||||
| constructor() { | |||||
| this.audio.src = 'assets/sound/alarm2.mp3'; | |||||
| this.audio.load(); | |||||
| } | } | ||||
| playSound(): void { | playSound(): void { | ||||
| if (this.audioPlayer) { | |||||
| this.audioPlayer.play().catch(error => console.error('Error playing sound:', error)); | |||||
| } | |||||
| this.audio.play().catch(error => { | |||||
| console.error('Error playing audio:', error); | |||||
| }); | |||||
| } | } | ||||
| stopSound(): void { | stopSound(): void { | ||||
| if (this.audioPlayer) { | |||||
| this.audioPlayer.pause(); | |||||
| this.audioPlayer.currentTime = 0; | |||||
| if (this.audio) { | |||||
| this.audio.pause(); | |||||
| this.audio.currentTime = 0; | |||||
| } | } | ||||
| } | } | ||||
| this.stopSound() | this.stopSound() | ||||
| clearInterval(this.alertInterval); | clearInterval(this.alertInterval); | ||||
| } | } | ||||
| simulateClick(element: HTMLElement) { | |||||
| element.click(); | |||||
| } | |||||
| } | } |