|
| 1 | +<template> |
| 2 | + <v-dialog v-model="jsonInsertDialog" width="860" persistent> |
| 3 | + <v-card> |
| 4 | + <v-card-title>Insert {{ type }} from JSON</v-card-title> |
| 5 | + <v-divider></v-divider> |
| 6 | + <v-card-text class="bg-card pa-3"> |
| 7 | + <v-card> |
| 8 | + <v-textarea |
| 9 | + v-model="jsonInput" |
| 10 | + :error-messages="jsonInputErrors" |
| 11 | + variant="outlined" |
| 12 | + rows="10" |
| 13 | + hide-details |
| 14 | + @update:model-value="clearErrorMessages" /> |
| 15 | + </v-card> |
| 16 | + </v-card-text> |
| 17 | + <v-divider></v-divider> |
| 18 | + <v-card-actions> |
| 19 | + <v-spacer></v-spacer> |
| 20 | + <v-btn @click="closeDialog">Cancel</v-btn> |
| 21 | + <v-btn color="primary" @click="insertJson">Save</v-btn> |
| 22 | + </v-card-actions> |
| 23 | + </v-card> |
| 24 | + </v-dialog> |
| 25 | +</template> |
| 26 | + |
| 27 | +<script lang="ts" setup> |
| 28 | + import type { JsonValue } from '@aas-core-works/aas-core3.0-typescript/jsonization'; |
| 29 | + import { jsonization, types as aasTypes } from '@aas-core-works/aas-core3.0-typescript'; |
| 30 | + import { computed, ref, watch } from 'vue'; |
| 31 | + import { useRouter } from 'vue-router'; |
| 32 | + import { useAASRepositoryClient } from '@/composables/Client/AASRepositoryClient'; |
| 33 | + import { useSMRepositoryClient } from '@/composables/Client/SMRepositoryClient'; |
| 34 | + import { useAASStore } from '@/store/AASDataStore'; |
| 35 | + import { useNavigationStore } from '@/store/NavigationStore'; |
| 36 | + import { extractEndpointHref } from '@/utils/AAS/DescriptorUtils'; |
| 37 | + import { base64Decode, base64Encode } from '@/utils/EncodeDecodeUtils'; |
| 38 | +
|
| 39 | + const props = defineProps<{ |
| 40 | + modelValue: boolean; |
| 41 | + type: 'Submodel' | 'SubmodelElement'; |
| 42 | + parentElement?: any; |
| 43 | + }>(); |
| 44 | +
|
| 45 | + const emit = defineEmits<{ |
| 46 | + (event: 'update:modelValue', value: boolean): void; |
| 47 | + }>(); |
| 48 | +
|
| 49 | + // Vue Router |
| 50 | + const router = useRouter(); |
| 51 | +
|
| 52 | + // Stores |
| 53 | + const aasStore = useAASStore(); |
| 54 | + const navigationStore = useNavigationStore(); |
| 55 | +
|
| 56 | + // Composables |
| 57 | + const { postSubmodel, postSubmodelElement } = useSMRepositoryClient(); |
| 58 | + const { putAas } = useAASRepositoryClient(); |
| 59 | +
|
| 60 | + // Data |
| 61 | + const jsonInsertDialog = ref(false); |
| 62 | + const jsonInput = ref<string | null>(null); |
| 63 | + const jsonInputErrors = ref<string[]>([]); |
| 64 | +
|
| 65 | + // Computed Properties |
| 66 | + const selectedAAS = computed(() => aasStore.getSelectedAAS); // Get the selected AAS from Store |
| 67 | + const submodelRepoUrl = computed(() => navigationStore.getSubmodelRepoURL); |
| 68 | +
|
| 69 | + watch( |
| 70 | + () => props.modelValue, |
| 71 | + (value) => { |
| 72 | + jsonInsertDialog.value = value; |
| 73 | + } |
| 74 | + ); |
| 75 | +
|
| 76 | + watch( |
| 77 | + () => jsonInsertDialog.value, |
| 78 | + (value) => { |
| 79 | + emit('update:modelValue', value); |
| 80 | + } |
| 81 | + ); |
| 82 | +
|
| 83 | + function insertJson(): void { |
| 84 | + if (!jsonInput.value || !isValidJson(jsonInput.value)) { |
| 85 | + jsonInputErrors.value = ['Invalid JSON input']; |
| 86 | + return; |
| 87 | + } |
| 88 | +
|
| 89 | + // Parse JSON to Submodel/SubmodelElement |
| 90 | + if (props.type === 'Submodel') { |
| 91 | + insertSubmodel(JSON.parse(jsonInput.value)); |
| 92 | + } else { |
| 93 | + insertSubmodelElement(JSON.parse(jsonInput.value)); |
| 94 | + } |
| 95 | + } |
| 96 | +
|
| 97 | + async function insertSubmodel(json: JsonValue): Promise<void> { |
| 98 | + // Parse JSON to Submodel |
| 99 | + const instanceOrError = jsonization.submodelFromJsonable(json); |
| 100 | + if (instanceOrError.error !== null) { |
| 101 | + navigationStore.dispatchSnackbar({ |
| 102 | + status: true, |
| 103 | + timeout: 4000, |
| 104 | + color: 'error', |
| 105 | + btnColor: 'buttonText', |
| 106 | + text: 'Error parsing Submodel: ' + instanceOrError.error, |
| 107 | + }); |
| 108 | + return; |
| 109 | + } |
| 110 | + const submodel = instanceOrError.mustValue(); |
| 111 | +
|
| 112 | + // Create Submodel |
| 113 | + await postSubmodel(submodel); |
| 114 | + // Add Submodel Reference to AAS |
| 115 | + await addSubmodelReferenceToAas(submodel); |
| 116 | + // Fetch and dispatch Submodel |
| 117 | + const path = submodelRepoUrl.value + '/' + base64Encode(submodel.id); |
| 118 | + const aasEndpoint = extractEndpointHref(selectedAAS.value, 'AAS-3.0'); |
| 119 | + router.push({ query: { aas: aasEndpoint, path: path } }); |
| 120 | +
|
| 121 | + closeDialog(); |
| 122 | + navigationStore.dispatchTriggerTreeviewReload(); |
| 123 | + } |
| 124 | +
|
| 125 | + async function insertSubmodelElement(json: JsonValue): Promise<void> { |
| 126 | + const instanceOrError = jsonization.submodelElementFromJsonable(json); |
| 127 | + if (instanceOrError.error !== null) { |
| 128 | + navigationStore.dispatchSnackbar({ |
| 129 | + status: true, |
| 130 | + timeout: 4000, |
| 131 | + color: 'error', |
| 132 | + btnColor: 'buttonText', |
| 133 | + text: 'Error parsing SubmodelElement: ' + instanceOrError.error, |
| 134 | + }); |
| 135 | + return; |
| 136 | + } |
| 137 | + const submodelElement = instanceOrError.mustValue(); |
| 138 | +
|
| 139 | + if (props.parentElement.modelType === 'Submodel') { |
| 140 | + // Create the property on the parent Submodel |
| 141 | + await postSubmodelElement(submodelElement, props.parentElement.id); |
| 142 | +
|
| 143 | + const aasEndpoint = extractEndpointHref(selectedAAS.value, 'AAS-3.0'); |
| 144 | +
|
| 145 | + // Navigate to the new property |
| 146 | + router.push({ |
| 147 | + query: { |
| 148 | + aas: aasEndpoint, |
| 149 | + path: props.parentElement.path + '/submodel-elements/' + submodelElement.idShort, |
| 150 | + }, |
| 151 | + }); |
| 152 | + } else { |
| 153 | + // Extract the submodel ID and the idShortPath from the parentElement path |
| 154 | + const splitted = props.parentElement.path.split('/submodel-elements/'); |
| 155 | + const submodelId = base64Decode(splitted[0].split('/submodels/')[1]); |
| 156 | + const idShortPath = splitted[1]; |
| 157 | +
|
| 158 | + // Create the property on the parent element |
| 159 | + await postSubmodelElement(submodelElement, submodelId, idShortPath); |
| 160 | +
|
| 161 | + const aasEndpoint = extractEndpointHref(selectedAAS.value, 'AAS-3.0'); |
| 162 | +
|
| 163 | + // Navigate to the new property |
| 164 | + if (props.parentElement.modelType === 'SubmodelElementCollection') { |
| 165 | + router.push({ |
| 166 | + query: { |
| 167 | + aas: aasEndpoint, |
| 168 | + path: props.parentElement.path + '.' + submodelElement.idShort, |
| 169 | + }, |
| 170 | + }); |
| 171 | + } |
| 172 | + } |
| 173 | +
|
| 174 | + closeDialog(); |
| 175 | + navigationStore.dispatchTriggerTreeviewReload(); |
| 176 | + } |
| 177 | +
|
| 178 | + async function addSubmodelReferenceToAas(submodel: aasTypes.Submodel): Promise<void> { |
| 179 | + if (selectedAAS.value === null) return; |
| 180 | + const localAAS = { ...selectedAAS.value }; |
| 181 | + const instanceOrError = jsonization.assetAdministrationShellFromJsonable(localAAS); |
| 182 | + if (instanceOrError.error !== null) { |
| 183 | + console.error('Error parsing AAS: ', instanceOrError.error); |
| 184 | + return; |
| 185 | + } |
| 186 | + const aas = instanceOrError.mustValue(); |
| 187 | + // Create new SubmodelReference |
| 188 | + const submodelReference = new aasTypes.Reference(aasTypes.ReferenceTypes.ExternalReference, [ |
| 189 | + new aasTypes.Key(aasTypes.KeyTypes.Submodel, submodel.id), |
| 190 | + ]); |
| 191 | + // Check if Submodels are null |
| 192 | + if (aas.submodels === null || aas.submodels === undefined) { |
| 193 | + aas.submodels = [submodelReference]; |
| 194 | + localAAS.submodels = [jsonization.toJsonable(submodelReference)]; |
| 195 | + } else { |
| 196 | + aas.submodels.push(submodelReference); |
| 197 | + localAAS.submodels.push(jsonization.toJsonable(submodelReference)); |
| 198 | + } |
| 199 | + await putAas(aas); |
| 200 | +
|
| 201 | + // Update AAS in Store |
| 202 | + aasStore.dispatchSelectedAAS(localAAS); |
| 203 | + } |
| 204 | +
|
| 205 | + function isValidJson(jsonString: string): boolean { |
| 206 | + try { |
| 207 | + JSON.parse(jsonString); |
| 208 | + return true; |
| 209 | + } catch { |
| 210 | + return false; |
| 211 | + } |
| 212 | + } |
| 213 | +
|
| 214 | + function closeDialog(): void { |
| 215 | + clearForm(); |
| 216 | + jsonInsertDialog.value = false; |
| 217 | + } |
| 218 | +
|
| 219 | + function clearForm(): void { |
| 220 | + jsonInput.value = null; |
| 221 | + } |
| 222 | +
|
| 223 | + function clearErrorMessages(): void { |
| 224 | + jsonInputErrors.value = []; |
| 225 | + } |
| 226 | +</script> |
0 commit comments