Browse Source

commit

pull/14/head
PhamY0601 1 year ago
parent
commit
4b33242034
4 changed files with 33 additions and 19 deletions
  1. +3
    -1
      src/app/app.component.html
  2. +14
    -5
      src/app/app.component.ts
  3. +1
    -1
      src/app/modules/overview/map/map.component.ts
  4. +15
    -12
      src/app/shared/services/alarm-sound.service.ts

+ 3
- 1
src/app/app.component.html View File

<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>



+ 14
- 5
src/app/app.component.ts View File

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

+ 1
- 1
src/app/modules/overview/map/map.component.ts View File

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

+ 15
- 12
src/app/shared/services/alarm-sound.service.ts View File

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

} }

Loading…
Cancel
Save