Files
new-landing-page-homenas/src/app/tasmota.service.ts
T

61 lines
2.2 KiB
TypeScript
Raw Normal View History

// tasmota.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { environment } from '../environments/environment';
import { TasmotaSocket } from './socket.interface'; // NEU: Import des Interfaces
@Injectable({
providedIn: 'root'
})
export class TasmotaService {
private baseUrl = environment.production
? ''
: '';
// 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 }
// Hier können weitere Steckdosen-Objekte hinzugefügt werden
];
private httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
// Weitere Header falls nötig
})
};
constructor(private http: HttpClient) { }
getSocketStatus(socketPath: string): Observable<boolean> {
return this.http.get<any>(`${socketPath}/cm?cmnd=Power`, this.httpOptions)
.pipe(
map(response => response.POWER === 'ON')
);
}
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)
.pipe(
map(response => response.POWER === 'ON')
);
}
// 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; }
*/
}