-
Notifications
You must be signed in to change notification settings - Fork 2
feat(dist):final scd touches #647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat(dist)/assign-ln-to-ied
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import type { SetAttributes } from '@openscd/oscd-api' | ||
| import type { | ||
| EqFunctionTemplate, | ||
| FunctionTemplate, | ||
| LNodeTemplate | ||
| } from '@/headless/common-types' | ||
| import { getDocumentAndEditor } from '../utils' | ||
| import { bayStore } from '@/headless/stores' | ||
|
|
||
| type UpdateBayLNodesParams = { | ||
| scdBay: Element | ||
| lNodes: LNodeTemplate[] | ||
| iedName: string | ||
| sourceFunction: EqFunctionTemplate | FunctionTemplate | ||
| equipmentUuid?: string | ||
| } | ||
|
|
||
| function findMatchingLNodeElement( | ||
| scdBay: Element, | ||
| lNode: LNodeTemplate, | ||
| sourceFunction: EqFunctionTemplate | FunctionTemplate, | ||
| equipmentUuid?: string | ||
| ): Element | null { | ||
| let functionElements: Element[] = [] | ||
|
|
||
| // EqFunctions are inside ConductingEquipment | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this also looks really complex, perhaps break this down into smaller chunks? |
||
| if (equipmentUuid) { | ||
| const match = bayStore.equipmentMatches.find( | ||
| (m) => m.templateEquipment.uuid === equipmentUuid | ||
| ) | ||
|
|
||
| if (match) { | ||
| const targetEquipment = match.scdElement | ||
| const eqFunctions = Array.from( | ||
| targetEquipment.querySelectorAll('EqFunction') | ||
| ) | ||
| functionElements = eqFunctions.filter((eqFunc) => { | ||
| const name = eqFunc.getAttribute('name') | ||
| return name === sourceFunction.name | ||
| }) | ||
| } | ||
| } else { | ||
| const functions = Array.from( | ||
| scdBay.querySelectorAll(':scope > Function') | ||
| ) | ||
| functionElements = functions.filter((func) => { | ||
| const name = func.getAttribute('name') | ||
| return name === sourceFunction.name | ||
| }) | ||
| } | ||
|
|
||
| for (const functionElement of functionElements) { | ||
| const lnodeElements = Array.from( | ||
| functionElement.querySelectorAll('LNode') | ||
| ) | ||
|
|
||
| const matches = lnodeElements.filter((element) => { | ||
| const lnClass = element.getAttribute('lnClass') | ||
| const lnType = element.getAttribute('lnType') | ||
| const lnInst = element.getAttribute('lnInst') | ||
|
|
||
| return ( | ||
| lnClass === lNode.lnClass && | ||
| lnType === lNode.lnType && | ||
| lnInst === lNode.lnInst | ||
| ) | ||
| }) | ||
|
|
||
| const unassignedMatch = matches.find( | ||
| (element) => !element.getAttribute('iedName') | ||
| ) | ||
| if (unassignedMatch) { | ||
| return unassignedMatch | ||
| } | ||
|
|
||
| if (matches.length > 0) { | ||
| return matches[0] | ||
| } | ||
| } | ||
|
|
||
| return null | ||
| } | ||
|
|
||
| export function updateBayLNodeIedNames({ | ||
| scdBay, | ||
| lNodes, | ||
| iedName, | ||
| sourceFunction, | ||
| equipmentUuid | ||
| }: UpdateBayLNodesParams): SetAttributes[] { | ||
| const edits: SetAttributes[] = [] | ||
|
|
||
| for (const lNode of lNodes) { | ||
| const lnodeElement = findMatchingLNodeElement( | ||
| scdBay, | ||
| lNode, | ||
| sourceFunction, | ||
| equipmentUuid | ||
| ) | ||
| if (!lnodeElement) { | ||
| continue | ||
| } | ||
|
|
||
| const currentIedName = lnodeElement.getAttribute('iedName') | ||
| if (currentIedName) { | ||
| continue | ||
| } | ||
| edits.push({ | ||
| element: lnodeElement, | ||
| attributes: { | ||
| iedName | ||
| }, | ||
| attributesNS: {} | ||
| }) | ||
| } | ||
|
|
||
| return edits | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,13 +3,20 @@ import type { | |
| FunctionTemplate, | ||
| LNodeTemplate | ||
| } from '../common-types' | ||
| import type { Insert, SetAttributes } from '@openscd/oscd-api' | ||
| import { pluginGlobalStore } from '@oscd-plugins/core-ui-svelte' | ||
| import { createLNodesInAccessPoint } from '../ied/create-lNode-in-access-point' | ||
| import { updateBayLNodeIedNames } from '../ied/update-bay-lnodes' | ||
| import { applyBayTypeSelection } from '../matching' | ||
| import { bayStore } from './bay.store.svelte' | ||
| import { bayTypesStore } from './bay-types.store.svelte' | ||
| import { equipmentMatchingStore } from './equipment-matching.store.svelte' | ||
|
|
||
| type DraggedItem = { | ||
| type: 'equipmentFunction' | 'functionTemplate' | 'lNode' | ||
| lNodes: LNodeTemplate[] | ||
| sourceFunction: EqFunctionTemplate | FunctionTemplate | ||
| equipmentName?: string | ||
| equipmentUuid?: string | ||
| } | ||
|
|
||
| class UseDndStore { | ||
|
|
@@ -41,22 +48,87 @@ class UseDndStore { | |
| } | ||
|
|
||
| try { | ||
| createLNodesInAccessPoint({ | ||
| const hasAssignedBayType = !!bayStore.assigendBayType | ||
| const hasSelectedBay = !!bayStore.selectedBay | ||
| const validation = equipmentMatchingStore.validationResult | ||
|
|
||
| let didApplyBayType = false | ||
|
|
||
| if (!hasAssignedBayType && hasSelectedBay) { | ||
| const requiresManualMatching = | ||
| validation?.requiresManualMatching ?? false | ||
|
|
||
| const hasValidAutoSelection = | ||
| !!bayTypesStore.selectedBayType && | ||
| !!validation?.isValid && | ||
| !requiresManualMatching | ||
|
|
||
| const hasPendingManualSelection = | ||
| !!bayStore.pendingBayTypeApply && requiresManualMatching | ||
|
|
||
| if (hasPendingManualSelection) { | ||
| bayTypesStore.selectedBayType = bayStore.pendingBayTypeApply | ||
| } | ||
|
|
||
| if (hasValidAutoSelection || hasPendingManualSelection) { | ||
| applyBayTypeSelection(bayStore.selectedBay) | ||
| bayStore.assigendBayType = bayTypesStore.selectedBayType | ||
| bayStore.pendingBayTypeApply = null | ||
| equipmentMatchingStore.clearValidationResult() | ||
| didApplyBayType = true | ||
| } | ||
| } | ||
|
|
||
| const allEdits: (Insert | SetAttributes)[] = [] | ||
|
|
||
| const iedEdits = createLNodesInAccessPoint({ | ||
| sourceFunction: functionFromSSD, | ||
| lNodes, | ||
| iedName: targetSIedName, | ||
| accessPoint: targetAccessPoint | ||
| }) | ||
| allEdits.push(...iedEdits) | ||
|
|
||
| if (bayStore.scdBay && (hasAssignedBayType || didApplyBayType)) { | ||
| const bayEdits = updateBayLNodeIedNames({ | ||
| scdBay: bayStore.scdBay, | ||
| lNodes, | ||
| iedName: targetSIedName, | ||
| sourceFunction: functionFromSSD, | ||
| equipmentUuid: this.draggedItem.equipmentUuid | ||
| }) | ||
| allEdits.push(...bayEdits) | ||
| } | ||
|
|
||
| if (allEdits.length > 0) { | ||
| const editor = pluginGlobalStore.editor | ||
| if (!editor) { | ||
| throw new Error('No editor found') | ||
| } | ||
|
|
||
| const functionName = functionFromSSD.name | ||
| const lnodeInfo = | ||
| lNodes.length === 1 | ||
| ? `${lNodes[0].lnClass}` | ||
| : `${lNodes.length} LNodes` | ||
|
|
||
| const title = didApplyBayType | ||
| ? `Apply BayType and assign ${lnodeInfo} from ${functionName} to IED ${targetSIedName}` | ||
| : `Assign ${lnodeInfo} from ${functionName} to IED ${targetSIedName}` | ||
|
|
||
| editor.commit( | ||
| allEdits, | ||
| didApplyBayType ? { squash: true } : { title } | ||
| ) | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks really complex, lets break this down into multiple functions with more descriptive names, so we know whats happening |
||
| } catch (error) { | ||
| console.error('[DnD] Error creating LNodes:', error) | ||
| } | ||
|
|
||
| this.handleDragEnd() | ||
| } | ||
|
|
||
| get currentDraggedItem() { | ||
| return this.draggedItem | ||
| } | ||
| } | ||
|
|
||
| export const dndStore = new UseDndStore() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i need to change this back to your version