|
|
|
|
|
|
|
|
import { ElementRef, Injectable } from '@angular/core'; |
|
|
|
|
|
|
|
|
import { Injectable } from '@angular/core'; |
|
|
|
|
|
|
|
|
@Injectable({ |
|
|
@Injectable({ |
|
|
providedIn: 'root', |
|
|
providedIn: 'root', |
|
|
|
|
|
|
|
|
private alertInterval: any; |
|
|
private alertInterval: any; |
|
|
private alertDuration: number = 10000; |
|
|
private alertDuration: number = 10000; |
|
|
private audio = new Audio(); |
|
|
private audio = new Audio(); |
|
|
|
|
|
private isPlaying: boolean = false; |
|
|
|
|
|
|
|
|
constructor() { |
|
|
constructor() { |
|
|
this.audio.src = 'assets/sound/alarm_5m.mp3'; |
|
|
this.audio.src = 'assets/sound/alarm_5m.mp3'; |
|
|
this.audio.load(); |
|
|
this.audio.load(); |
|
|
|
|
|
this.audio.onended = () => { |
|
|
|
|
|
// Khi phát hết âm thanh, đặt biến isPlaying về false |
|
|
|
|
|
this.isPlaying = false; |
|
|
|
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
playSound(): void { |
|
|
playSound(): void { |
|
|
this.audio.play().catch((error) => { |
|
|
|
|
|
console.error('Error playing audio:', error); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
if (!this.isPlaying) { |
|
|
|
|
|
this.audio.play().then(() => { |
|
|
|
|
|
this.isPlaying = true; |
|
|
|
|
|
this.alertInterval = setInterval(() => { |
|
|
|
|
|
this.stopAlarm(); |
|
|
|
|
|
}, this.alertDuration); |
|
|
|
|
|
}).catch((error) => { |
|
|
|
|
|
console.error('Error playing audio:', error); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
stopSound(): void { |
|
|
stopSound(): void { |
|
|
if (this.audio) { |
|
|
if (this.audio) { |
|
|
this.audio.pause(); |
|
|
this.audio.pause(); |
|
|
this.audio.currentTime = 0; |
|
|
this.audio.currentTime = 0; |
|
|
|
|
|
this.isPlaying = false; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
startAlarm( |
|
|
|
|
|
state5: boolean, |
|
|
|
|
|
isReady: boolean, |
|
|
|
|
|
state1: boolean, |
|
|
|
|
|
state2: boolean, |
|
|
|
|
|
): void { |
|
|
|
|
|
if (state5 && isReady && (state1 || state2)) { |
|
|
|
|
|
|
|
|
startAlarm( isTurnOn: boolean, isReady: boolean, fireArm: boolean, fenceArm: boolean): void { |
|
|
|
|
|
if (isTurnOn && isReady && (fireArm || fenceArm)) { |
|
|
this.playSound(); |
|
|
this.playSound(); |
|
|
this.alertInterval = setInterval(() => { |
|
|
|
|
|
this.stopAlarm(); |
|
|
|
|
|
}, this.alertDuration); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clearInterval(this.alertInterval); |
|
|
clearInterval(this.alertInterval); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
simulateClick(element: HTMLElement) { |
|
|
|
|
|
element.click(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |