Files
Go-To-bed/Client/src/app/services/networkRequests.service.ts
2022-01-02 05:46:33 +01:00

38 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Injectable } from '@angular/core';
import { ToastController } from '@ionic/angular';
import { NetRequest } from '../gotobed.models';
import { HTTP } from '@awesome-cordova-plugins/http/ngx';
import { HttpErrorResponse } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class NetworkRequestsService {
constructor(private readonly httpClient: HTTP, public toastController: ToastController) { }
public sendCommand(command: string, address: string, port: number, connectionProtocol?: string,) {
const request: NetRequest = {
command,
};
this.httpClient.setDataSerializer('json');
this.httpClient.setServerTrustMode('nocheck');
this.httpClient.post(encodeURI(`${connectionProtocol ?? 'http'}://${address + ':' + port}/commandbridge`), request, {
contentType: 'text/html;'
}).catch(response => {
this.showErrorMessage(response);
});
}
async showErrorMessage(errorMessage: HttpErrorResponse ) {
const toast = await this.toastController.create({
message: errorMessage.name + ': ' + errorMessage.message,
duration: 4000,
color: 'danger',
position: 'top'
});
toast.present();
}
}