2025-03-12 11:16:31 +01:00
|
|
|
// tasmota.service.ts
|
|
|
|
|
import { Injectable } from '@angular/core';
|
2025-03-12 15:01:18 +01:00
|
|
|
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
2025-03-12 11:16:31 +01:00
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
|
import { map } from 'rxjs/operators';
|
2025-03-12 15:01:18 +01:00
|
|
|
import { environment } from '../environments/environment';
|
2025-03-12 11:16:31 +01:00
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class TasmotaService {
|
2025-03-12 15:01:18 +01:00
|
|
|
private baseUrl = environment.production
|
|
|
|
|
? ''
|
|
|
|
|
: '';
|
|
|
|
|
|
|
|
|
|
private SOCKET1_PATH = this.baseUrl + '/tasmota1';
|
|
|
|
|
private SOCKET2_PATH = this.baseUrl + '/tasmota2';
|
|
|
|
|
|
|
|
|
|
private httpOptions = {
|
|
|
|
|
headers: new HttpHeaders({
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
// Weitere Header falls nötig
|
|
|
|
|
})
|
|
|
|
|
};
|
2025-03-12 11:16:31 +01:00
|
|
|
|
|
|
|
|
constructor(private http: HttpClient) { }
|
|
|
|
|
|
2025-03-12 15:01:18 +01:00
|
|
|
getSocketStatus(socketPath: string): Observable<boolean> {
|
|
|
|
|
return this.http.get<any>(`${socketPath}/cm?cmnd=Power`, this.httpOptions)
|
2025-03-12 11:16:31 +01:00
|
|
|
.pipe(
|
|
|
|
|
map(response => response.POWER === 'ON')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-12 15:01:18 +01:00
|
|
|
toggleSocket(socketPath: string, currentStatus: boolean): Observable<boolean> {
|
|
|
|
|
const command = currentStatus ? 'Power%20Off' : 'Power%20On';
|
|
|
|
|
return this.http.get<any>(`${socketPath}/cm?cmnd=${command}`, this.httpOptions)
|
2025-03-12 11:16:31 +01:00
|
|
|
.pipe(
|
|
|
|
|
map(response => response.POWER === 'ON')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-12 15:01:18 +01:00
|
|
|
get socket1Path(): string {
|
|
|
|
|
return this.SOCKET1_PATH;
|
2025-03-12 11:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-12 15:01:18 +01:00
|
|
|
get socket2Path(): string {
|
|
|
|
|
return this.SOCKET2_PATH;
|
2025-03-12 11:16:31 +01:00
|
|
|
}
|
|
|
|
|
}
|