Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 75 additions & 59 deletions src/renderer/screens/workspace-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,72 @@ const WorkspaceScreen = () => {
})
})

project.data.pous.forEach((pou) => {
if (pou.type !== 'program') return

const instances = project.data.configuration.resource.instances
const programInstance = instances.find((inst) => inst.program === pou.data.name)

if (programInstance) {
const functionBlockInstances = pou.data.variables.filter((variable) => variable.type.definition === 'derived')

functionBlockInstances.forEach((fbInstance) => {
const fbTypeName = fbInstance.type.value.toUpperCase()

let fbVariables:
| Array<{ name: string; class: string; type: { definition: string; value: string } }>
| undefined

const standardFB = StandardFunctionBlocks.pous.find(
(fb: { name: string }) => fb.name.toUpperCase() === fbTypeName,
)
if (standardFB) {
fbVariables = standardFB.variables
} else {
const customFB = project.data.pous.find(
(p) => p.type === 'function-block' && p.data.name.toUpperCase() === fbTypeName,
)
if (customFB && customFB.type === 'function-block') {
fbVariables = customFB.data.variables as Array<{
name: string
class: string
type: { definition: string; value: string }
}>
}
}

if (fbVariables) {
const boolOutputs = fbVariables.filter(
(v) =>
(v.class === 'output' || v.class === 'inOut') &&
v.type.definition === 'base-type' &&
v.type.value.toUpperCase() === 'BOOL',
)

boolOutputs.forEach((outputVar) => {
const debugPath = `RES0__${programInstance.name.toUpperCase()}.${fbInstance.name.toUpperCase()}.${outputVar.name.toUpperCase()}`
const index = debugVariableIndexes.get(debugPath)

if (index !== undefined) {
const blockVarName = `${fbInstance.name}.${outputVar.name}`
variableInfoMap.set(index, {
pouName: programInstance.name,
variable: {
name: blockVarName,
type: { definition: 'base-type', value: 'bool' },
class: 'local',
location: '',
documentation: '',
debug: false,
},
})
}
})
}
})
}
})

variableInfoMapRef.current = variableInfoMap

const pollVariables = async () => {
Expand All @@ -193,6 +259,7 @@ const WorkspaceScreen = () => {
const { project: currentProject } = useOpenPLCStore.getState()

const debugVariableKeys = new Set<string>()

currentProject.data.pous.forEach((pou) => {
if (pou.type !== 'program') return
pou.data.variables
Expand Down Expand Up @@ -227,75 +294,24 @@ const WorkspaceScreen = () => {
})
})
}
}

if (currentPou && currentPou.data.body.language === 'ld') {
const instances = currentProject.data.configuration.resource.instances
const programInstance = instances.find((inst) => inst.program === currentPou.data.name)

if (programInstance) {
const functionBlockInstances = currentPou.data.variables.filter(
(variable) => variable.type.definition === 'derived',
)

functionBlockInstances.forEach((fbInstance) => {
const fbTypeName = fbInstance.type.value.toUpperCase()

let fbVariables:
| Array<{ name: string; class: string; type: { definition: string; value: string } }>
| undefined

const standardFB = StandardFunctionBlocks.pous.find(
(fb: { name: string }) => fb.name.toUpperCase() === fbTypeName,
)
if (standardFB) {
fbVariables = standardFB.variables
} else {
const customFB = currentProject.data.pous.find(
(pou) => pou.type === 'function-block' && pou.data.name.toUpperCase() === fbTypeName,
)
if (customFB && customFB.type === 'function-block') {
fbVariables = customFB.data.variables as Array<{
name: string
class: string
type: { definition: string; value: string }
}>
Array.from(variableInfoMapRef.current!.entries()).forEach(([_, varInfo]) => {
if (
varInfo.pouName === programInstance.name &&
varInfo.variable.name.startsWith(`${fbInstance.name}.`)
) {
const compositeKey = `${varInfo.pouName}:${varInfo.variable.name}`
debugVariableKeys.add(compositeKey)
}
}

if (fbVariables) {
const boolOutputs = fbVariables.filter(
(v) =>
(v.class === 'output' || v.class === 'inOut') &&
v.type.definition === 'base-type' &&
v.type.value.toUpperCase() === 'BOOL',
)

boolOutputs.forEach((outputVar) => {
const debugPath = `RES0__${programInstance.name.toUpperCase()}.${fbInstance.name.toUpperCase()}.${outputVar.name.toUpperCase()}`
const index = debugVariableIndexes.get(debugPath)

if (index !== undefined) {
const blockVarName = `${fbInstance.name}.${outputVar.name}`
const compositeKey = `${programInstance.name}:${blockVarName}`
debugVariableKeys.add(compositeKey)

if (!variableInfoMapRef.current?.has(index)) {
variableInfoMapRef.current?.set(index, {
pouName: programInstance.name,
variable: {
name: blockVarName,
type: { definition: 'base-type', value: 'bool' },
class: 'local',
location: '',
documentation: '',
debug: false,
},
})
}
}
})
}
})
})
Comment on lines 305 to 315
Copy link

Copilot AI Oct 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nested loop creates O(n×m) complexity where the outer loop iterates over function block instances and the inner loop iterates over all variable entries. Consider building a Map keyed by POU name and function block instance name during initialization to avoid this nested iteration during polling.

Copilot uses AI. Check for mistakes.
}
}
Expand Down