Files
HPIoTWebsite/src/app/configs/configs.component.ts

79 lines
2.9 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, FormArray } from '@angular/forms';
import { Observable } from 'rxjs';
import {map, startWith} from 'rxjs/operators';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Component({
selector: 'app-configs',
templateUrl: './configs.component.html',
styleUrls: ['./configs.component.scss']
})
export class ConfigsComponent implements OnInit {
constructor(private builder: FormBuilder, private http: HttpClient) { }
get certificateIDInput() { return this.configForm.get('certificateID'); }
get devicesArray() { return this.configForm.get('devices') as FormArray; }
configDownloaded = {};
configForm = this.builder.group({
certificateID: '',
devices: new FormArray([])
});
// Setup an API call to get the list of certificate IDs from DDB to store in options
options: string[] = ['1217f08d31bbc1e9f70aabb700fb33878ccc1ec6caa38c5efb8c9f2709c7a444',
'1370cf88050a5d9bc5aeef22f2b9756224c080c89f703eefeee8c9081dc6424e', '1e1f59fb7ac397487ceddbd65ff3ecb39e021997c1926317d076b2fe1f8a9614'];
filteredOptions: Observable<string[]>;
ngOnInit() {
this.filteredOptions = this.certificateIDInput.valueChanges.pipe(
startWith(''),
map(value => this._filter(value))
);
}
plcArray(i) { return this.configForm.get('devices.' + i + '.plcdata') as FormArray; }
private _filter(value: string): string[] {
const filterValue = value.toLowerCase();
return this.options.filter(option => option.toLowerCase().includes(filterValue));
}
onSelect(e){
// console.log(e.target.innerText.slice(1, -1));
this.configForm.controls.certificateID.setValue(e.target.innerText.slice(1, -1));
this.onCertificateChange();
}
buildForms() {
const numberOfDevices = Object.keys(this.configDownloaded).length - 2;
console.log(numberOfDevices);
if (this.devicesArray.length < numberOfDevices) {
for (let i = this.devicesArray.length; i < numberOfDevices; i++) {
this.devicesArray.push(this.builder.group({
appname: 'hpiot',
certificateID: '',
company: '',
location: i,
field: '',
devicetype: '',
plcdata: new FormArray([]),
modbusdata: new FormArray([]),
currentdata: new FormArray([]),
voltagedata: new FormArray([])
}));
}
}
}
onCertificateChange() {
// console.log(this.certificateIDInput.value);
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
const config = this.http.get('https://4ax24ru9ra.execute-api.us-east-1.amazonaws.com/Gamma/HPIoTgetConfig/?certificateID='
+ this.certificateIDInput.value, httpOptions);
config.subscribe(
data => {this.configDownloaded = data; console.log(this.configDownloaded); this.buildForms(); },
(err) => console.log(err)
);
}
}