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-09-17 13:00:11 +02:00
|
|
|
import { TasmotaSocket } from './socket.interface'; // NEU: Import des Interfaces
|
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
|
|
|
|
|
? ''
|
|
|
|
|
: '';
|
|
|
|
|
|
2025-09-17 13:00:11 +02:00
|
|
|
// private SOCKET1_PATH = this.baseUrl + '/tasmota1';
|
|
|
|
|
// private SOCKET2_PATH = this.baseUrl + '/tasmota2';
|
|
|
|
|
|
|
|
|
|
// NEU: Ein öffentliches Array, das alle zu steuernden Steckdosen und ihre Konfiguration enthält
|
|
|
|
|
public sockets: TasmotaSocket[] = [
|
|
|
|
|
// { id: '1', name: 'Aktiv Kohle Lüfter', path: this.baseUrl + '/tasmota1', status: false },
|
|
|
|
|
// { id: '2', name: 'Bambu P1P', path: this.baseUrl + '/tasmota2', status: false }
|
|
|
|
|
// anstatt der baseurl die adressen der stecker aus dem reverseproxy eingefügt
|
|
|
|
|
{ id: '1', name: 'Aktiv Kohle Lüfter', path: 'http://stecker1.neugasse.lan', status: false },
|
2026-06-24 12:39:15 +02:00
|
|
|
{ id: '2', name: 'Bambu P1P', path: 'http://stecker2.neugasse.lan', status: false }
|
2025-09-17 13:00:11 +02:00
|
|
|
// Hier können weitere Steckdosen-Objekte hinzugefügt werden
|
|
|
|
|
];
|
2025-03-12 15:01:18 +01:00
|
|
|
|
|
|
|
|
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-09-17 13:00:11 +02:00
|
|
|
// ALT: Die Getter `socket1Path` und `socket2Path` werden entfernt,
|
|
|
|
|
// da die Pfade nun direkt aus dem `sockets`-Array im Service oder in der Komponente bezogen werden.
|
|
|
|
|
/*
|
|
|
|
|
get socket1Path(): string { return this.SOCKET1_PATH; }
|
|
|
|
|
get socket2Path(): string { return this.SOCKET2_PATH; }
|
|
|
|
|
*/
|
2025-03-12 11:16:31 +01:00
|
|
|
}
|