Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | 119x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 4x 6x 6x 6x 6x 6090x 6084x 5924x 6x 226x 226x 1x 1x 3x 3x 2x 2x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 2x 2x 2x 2x 2x 115x | import { Component, inject, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
import { FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
import { MessageService } from 'primeng/api';
import { MessageModule } from 'primeng/message';
import { NgxJsonViewerModule } from 'ngx-json-viewer';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { Configuration } from '../../models/configuration';
import { LoaderComponent } from '../loader/loader.component';
import { FileUploadComponent } from '../file-upload/file-upload.component';
import { saveFile } from '../../utils/utils';
@Component({
selector: 'app-configuration',
imports: [NgxJsonViewerModule, LoaderComponent, FormsModule, ReactiveFormsModule, FileUploadComponent, MessageModule, RouterLink],
templateUrl: './configuration.component.html',
styleUrl: './configuration.component.scss'
})
export class ConfigurationComponent implements OnInit {
private twinpadApiService = inject(TwinpadApiService);
private route = inject(ActivatedRoute);
private router = inject(Router);
private formBuilder = inject(FormBuilder);
private messageService = inject(MessageService);
@ViewChild("configFileUpload") configFileUpload: FileUploadComponent;
@ViewChild("petriFileUpload") petriFileUpload: FileUploadComponent;
@ViewChild("pidFileUpload") pidFileUpload: FileUploadComponent;
configurationId: string;
configuration: Configuration;
configurationFile: JSON;
editMode: boolean = false;
xlsxConfigurationForm: FormGroup;
formSubmitted: boolean = false;
ngOnInit(): void {
this.configurationId = this.route.snapshot.params['configuration_id'] || "new";
this.initForm();
if (this.configurationId !== "new") {
this.loadConfiguration();
}
}
initForm() {
this.xlsxConfigurationForm = this.formBuilder.group({
configName: ["", [Validators.required, Validators.minLength(4)]],
configExcel: [undefined, [Validators.required]],
petriJson: [undefined],
pidJson: [undefined],
});
}
loadConfiguration() {
this.twinpadApiService.getConfig(this.configurationId).subscribe({
'next': value => {
this.configuration = value;
this.xlsxConfigurationForm.patchValue({configName: this.configuration.config_name});
this.configurationFile = JSON.parse(JSON.stringify(
this.configuration,
(key, value) => {
if (key === "in_use_by_devices") return undefined; // Don't include 'in_use_by_devices' in JSON representation
else if (key === "id") return undefined;
return value;
}, 2));
this.router.navigate(["/configurations", this.configurationId]);
},
'error': _ => {
this.messageService.add({severity:'error', summary: 'Loading config', detail: 'No configuration found'});
this.router.navigate(['/configurations']);
}
});
}
isInvalid(controlName: string): boolean {
const control = this.xlsxConfigurationForm.get(controlName);
return (control?.invalid && (control.touched || this.formSubmitted)) ?? true;
}
updateConfigFile(newFile: File | undefined) {
this.xlsxConfigurationForm.patchValue({configExcel: newFile});
this.xlsxConfigurationForm.get("configExcel")?.updateValueAndValidity();
}
updatePetriFile(newFile: File | undefined) {
this.xlsxConfigurationForm.patchValue({petriJson: newFile});
this.xlsxConfigurationForm.get("petriJson")?.updateValueAndValidity();
}
updateSchematicFile(newFile: File | undefined) {
this.xlsxConfigurationForm.patchValue({pidJson: newFile});
this.xlsxConfigurationForm.get("pidJson")?.updateValueAndValidity();
}
addConfiguration() {
Iif (this.xlsxConfigurationForm.invalid) {
this.messageService.add({ severity: 'error', summary: 'Nice try', detail: "https://www.youtube.com/watch?v=dQw4w9WgXcQ" });
return;
}
const configName = this.xlsxConfigurationForm.get("configName")?.value;
this.twinpadApiService.createConfig(
configName,
this.xlsxConfigurationForm.get("configExcel")?.value,
this.xlsxConfigurationForm.get("petriJson")?.value,
this.xlsxConfigurationForm.get("pidJson")?.value
).subscribe({
next: configId => {
this.messageService.add({ severity: 'success', summary: 'Configuration creation successful', detail: `Configuration ${configName} has been created` });
this.configurationId = configId;
this.loadConfiguration();
}
});
}
switchEditMode() {
/*
Multiple options:
For configs with no received_at property (created by users and not deployed yet):
- save this config (changes config_id)
- save as another config
For deployed configs:
- save as another config (not yet deployed)
- deploy and save (the "save" part is done when receiving the config back from the device, but still need to save it as standalone config)
*/
this.editMode = !this.editMode;
}
exportToJson() {
Iif (this.configuration === undefined || this.configuration === null) {
return;
}
this.twinpadApiService.getConfigJson(this.configurationId).subscribe({
next: blob => {
saveFile(document, blob, `${this.configuration.config_name ?? "config"}.json`);
},
error: error => {
console.error('Error downloading the blob', error);
}
});
}
saveConfiguration(saveAsNew: boolean = true) {
const configName = this.xlsxConfigurationForm.get("configName")?.value;
this.twinpadApiService.updateConfig(
this.configurationId,
configName,
this.xlsxConfigurationForm.get("petriJson")?.value,
this.xlsxConfigurationForm.get("pidJson")?.value,
saveAsNew,
).subscribe({
next: configId => {
this.messageService.add({ severity: 'success', summary: 'Configuration edit successful', detail: `Configuration ${configName} has been edited` });
this.configurationId = configId;
this.loadConfiguration();
}
});
}
deleteConfiguration() {
// Not possible when config has been deployed and is in
}
}
|