Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 0 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 3 additions & 25 deletions src/renderer/components/_atoms/graphical-editor/fbd/variable.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useDebugCompositeKey } from '@hooks/use-debug-composite-key'
import * as Popover from '@radix-ui/react-popover'
import { useOpenPLCStore } from '@root/renderer/store'
import { PLCVariable } from '@root/types/PLC'
Expand Down Expand Up @@ -58,33 +59,10 @@ const VariableElement = (block: VariableProps) => {
project: {
data: { pous, dataTypes },
},
workspace: {
isDebuggerVisible,
debugVariableIndexes,
debugVariableValues,
debugForcedVariables,
fbSelectedInstance,
fbDebugInstances,
},
workspace: { isDebuggerVisible, debugVariableIndexes, debugVariableValues, debugForcedVariables },
workspaceActions: { setDebugForcedVariables },
} = useOpenPLCStore()

// Helper to get composite key with FB instance context
const getCompositeKey = (variableName: string): string => {
const currentPou = pous.find((p) => p.data.name === editor.meta.name)
if (currentPou?.type === 'function-block') {
const fbTypeKey = currentPou.data.name.toUpperCase()
const selectedKey = fbSelectedInstance.get(fbTypeKey)
if (selectedKey) {
const instances = fbDebugInstances.get(fbTypeKey) || []
const selectedInstance = instances.find((inst) => inst.key === selectedKey)
if (selectedInstance) {
return `${selectedInstance.programName}:${selectedInstance.fbVariableName}.${variableName}`
}
}
}
return `${editor.meta.name}:${variableName}`
}
const getCompositeKey = useDebugCompositeKey()

const inputVariableRef = useRef<
HTMLTextAreaElement & {
Expand Down
41 changes: 12 additions & 29 deletions src/renderer/components/_atoms/graphical-editor/ladder/coil.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useDebugCompositeKey } from '@hooks/use-debug-composite-key'
import * as Popover from '@radix-ui/react-popover'
import {
DefaultCoil,
Expand Down Expand Up @@ -126,33 +127,10 @@ export const Coil = (block: CoilProps) => {
},
ladderFlows,
ladderFlowActions: { updateNode },
workspace: {
isDebuggerVisible,
debugVariableValues,
debugVariableIndexes,
debugForcedVariables,
fbSelectedInstance,
fbDebugInstances,
},
workspace: { isDebuggerVisible, debugVariableValues, debugVariableIndexes, debugForcedVariables },
workspaceActions: { setDebugForcedVariables },
} = useOpenPLCStore()

// Helper to get composite key with FB instance context
const getCompositeKey = (variableName: string): string => {
const currentPou = pous.find((p) => p.data.name === editor.meta.name)
if (currentPou?.type === 'function-block') {
const fbTypeKey = currentPou.data.name.toUpperCase()
const selectedKey = fbSelectedInstance.get(fbTypeKey)
if (selectedKey) {
const instances = fbDebugInstances.get(fbTypeKey) || []
const selectedInstance = instances.find((inst) => inst.key === selectedKey)
if (selectedInstance) {
return `${selectedInstance.programName}:${selectedInstance.fbVariableName}.${variableName}`
}
}
}
return `${editor.meta.name}:${variableName}`
}
const getCompositeKey = useDebugCompositeKey()

const coil = DEFAULT_COIL_TYPES[data.variant]
const [coilVariableValue, setCoilVariableValue] = useState<string>(data.variable.name)
Expand All @@ -164,7 +142,14 @@ export const Coil = (block: CoilProps) => {
}

const compositeKey = getCompositeKey(data.variable.name)
const value = debugVariableValues.get(compositeKey)
const isForced = debugForcedVariables.has(compositeKey)

// When forced, use immediate value from debugForcedVariables (no 200ms poll delay)
const value = isForced
? debugForcedVariables.get(compositeKey)
? '1'
: '0'
: debugVariableValues.get(compositeKey)

if (value === undefined) {
return undefined
Expand All @@ -173,10 +158,8 @@ export const Coil = (block: CoilProps) => {
const isTrue = value === '1' || value.toUpperCase() === 'TRUE'
const displayState = data.variant === 'negated' ? !isTrue : isTrue

const isForced = debugForcedVariables.has(compositeKey)
if (isForced) {
const forcedValue = debugForcedVariables.get(compositeKey)
return forcedValue ? '#80C000' : '#4080FF'
return displayState ? '#80C000' : '#4080FF'
}

return displayState ? '#00FF00' : '#0464FB'
Expand Down
41 changes: 12 additions & 29 deletions src/renderer/components/_atoms/graphical-editor/ladder/contact.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useDebugCompositeKey } from '@hooks/use-debug-composite-key'
import * as Popover from '@radix-ui/react-popover'
import {
DefaultContact,
Expand Down Expand Up @@ -93,33 +94,10 @@ export const Contact = (block: ContactProps) => {
},
ladderFlows,
ladderFlowActions: { updateNode },
workspace: {
isDebuggerVisible,
debugVariableValues,
debugVariableIndexes,
debugForcedVariables,
fbSelectedInstance,
fbDebugInstances,
},
workspace: { isDebuggerVisible, debugVariableValues, debugVariableIndexes, debugForcedVariables },
workspaceActions: { setDebugForcedVariables },
} = useOpenPLCStore()

// Helper to get composite key with FB instance context
const getCompositeKey = (variableName: string): string => {
const currentPou = pous.find((p) => p.data.name === editor.meta.name)
if (currentPou?.type === 'function-block') {
const fbTypeKey = currentPou.data.name.toUpperCase()
const selectedKey = fbSelectedInstance.get(fbTypeKey)
if (selectedKey) {
const instances = fbDebugInstances.get(fbTypeKey) || []
const selectedInstance = instances.find((inst) => inst.key === selectedKey)
if (selectedInstance) {
return `${selectedInstance.programName}:${selectedInstance.fbVariableName}.${variableName}`
}
}
}
return `${editor.meta.name}:${variableName}`
}
const getCompositeKey = useDebugCompositeKey()

const contact = DEFAULT_CONTACT_TYPES[data.variant]
const [contactVariableValue, setContactVariableValue] = useState<string>(data.variable.name)
Expand All @@ -131,7 +109,14 @@ export const Contact = (block: ContactProps) => {
}

const compositeKey = getCompositeKey(data.variable.name)
const value = debugVariableValues.get(compositeKey)
const isForced = debugForcedVariables.has(compositeKey)

// When forced, use immediate value from debugForcedVariables (no 200ms poll delay)
const value = isForced
? debugForcedVariables.get(compositeKey)
? '1'
: '0'
: debugVariableValues.get(compositeKey)

if (value === undefined) {
return undefined
Expand All @@ -140,10 +125,8 @@ export const Contact = (block: ContactProps) => {
const isTrue = value === '1' || value.toUpperCase() === 'TRUE'
const displayState = data.variant === 'negated' ? !isTrue : isTrue

const isForced = debugForcedVariables.has(compositeKey)
if (isForced) {
const forcedValue = debugForcedVariables.get(compositeKey)
return forcedValue ? '#80C000' : '#4080FF'
return displayState ? '#80C000' : '#4080FF'
}

return displayState ? '#00FF00' : '#0464FB'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useDebugCompositeKey } from '@hooks/use-debug-composite-key'
import * as Popover from '@radix-ui/react-popover'
import { useOpenPLCStore } from '@root/renderer/store'
import { RungLadderState } from '@root/renderer/store/slices'
Expand Down Expand Up @@ -65,6 +66,7 @@ const VariableElement = (block: VariableProps) => {
workspace: { isDebuggerVisible, debugVariableIndexes, debugForcedVariables },
workspaceActions: { setDebugForcedVariables },
} = useOpenPLCStore()
const getCompositeKey = useDebugCompositeKey()

const inputVariableRef = useRef<
HTMLTextAreaElement & {
Expand Down Expand Up @@ -280,7 +282,7 @@ const VariableElement = (block: VariableProps) => {

if (!data.variable.name) return

const compositeKey = `${editor.meta.name}:${data.variable.name}`
const compositeKey = getCompositeKey(data.variable.name)
const variableIndex = debugVariableIndexes.get(compositeKey)

if (variableIndex === undefined) return
Expand All @@ -302,7 +304,7 @@ const VariableElement = (block: VariableProps) => {

if (!data.variable.name) return

const compositeKey = `${editor.meta.name}:${data.variable.name}`
const compositeKey = getCompositeKey(data.variable.name)
const variableIndex = debugVariableIndexes.get(compositeKey)

if (variableIndex === undefined) return
Expand All @@ -324,7 +326,7 @@ const VariableElement = (block: VariableProps) => {

if (!data.variable.name) return

const compositeKey = `${editor.meta.name}:${data.variable.name}`
const compositeKey = getCompositeKey(data.variable.name)
const variableIndex = debugVariableIndexes.get(compositeKey)

if (variableIndex === undefined) return
Expand Down Expand Up @@ -352,7 +354,7 @@ const VariableElement = (block: VariableProps) => {
return
}

const compositeKey = `${editor.meta.name}:${data.variable.name}`
const compositeKey = getCompositeKey(data.variable.name)
const variableIndex = debugVariableIndexes.get(compositeKey)

if (variableIndex === undefined) {
Expand Down Expand Up @@ -446,7 +448,7 @@ const VariableElement = (block: VariableProps) => {
const variableType = getVariableType()
const isBoolVariable = variableType?.toUpperCase() === 'BOOL'

const compositeKey = `${editor.meta.name}:${data.variable.name}`
const compositeKey = getCompositeKey(data.variable.name)
const isForced = debugForcedVariables.has(compositeKey)
const forcedValue = debugForcedVariables.get(compositeKey)

Expand Down
29 changes: 4 additions & 25 deletions src/renderer/components/_molecules/graphical-editor/fbd/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useDebugCompositeKey } from '@hooks/use-debug-composite-key'
import { CustomFbdNodeTypes, customNodeTypes } from '@root/renderer/components/_atoms/graphical-editor/fbd'
import { BlockNode } from '@root/renderer/components/_atoms/graphical-editor/fbd/block'
import { getVariableRestrictionType } from '@root/renderer/components/_atoms/graphical-editor/utils'
Expand Down Expand Up @@ -47,36 +48,14 @@ export const FBDBody = ({ rung, nodeDivergences = [], isDebuggerActive = false }
modals,
modalActions: { closeModal, openModal },
snapshotActions: { addSnapshot },
workspace: { isDebuggerVisible, debugVariableValues, debugForcedVariables, fbSelectedInstance, fbDebugInstances },
workspace: { isDebuggerVisible, debugVariableValues, debugForcedVariables },
} = useOpenPLCStore()
const getCompositeKey = useDebugCompositeKey()

const pous = project.data.pous

const pouRef = pous.find((pou) => pou.data.name === editor.meta.name)

// Get FB instance context for function block POUs
const fbInstanceContext = useMemo(() => {
if (!pouRef || pouRef.type !== 'function-block') return null
const fbTypeKey = pouRef.data.name.toUpperCase() // Canonical key for map lookups
const selectedKey = fbSelectedInstance.get(fbTypeKey)
if (!selectedKey) return null
const instances = fbDebugInstances.get(fbTypeKey) || []
return instances.find((inst) => inst.key === selectedKey) || null
}, [pouRef, fbSelectedInstance, fbDebugInstances])

// Helper to get composite key for variable lookup, handling FB instance context
const getCompositeKey = useCallback(
(variableName: string): string => {
if (fbInstanceContext) {
// For FB POUs, transform to instance context: main:MOTOR_SPEED0.varName
return `${fbInstanceContext.programName}:${fbInstanceContext.fbVariableName}.${variableName}`
}
// For programs, use standard format: pouName:varName
return `${editor.meta.name}:${variableName}`
},
[fbInstanceContext, editor.meta.name],
)

const [rungLocal, setRungLocal] = useState<FBDRungState>(rung)
const [dragging, setDragging] = useState(false)

Expand Down Expand Up @@ -132,7 +111,7 @@ export const FBDBody = ({ rung, nodeDivergences = [], isDebuggerActive = false }

// For program POUs, verify the program instance exists
// For FB POUs, skip this check since getCompositeKey already handles instance context
if (!fbInstanceContext) {
if (pouRef?.type !== 'function-block') {
const instances = project.data.configuration.resource.instances
const programInstance = instances.find((inst: { program: string }) => inst.program === editor.meta.name)
if (!programInstance) return undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useDebugCompositeKey } from '@hooks/use-debug-composite-key'
import { getVariableRestrictionType } from '@root/renderer/components/_atoms/graphical-editor/utils'
import { useOpenPLCStore } from '@root/renderer/store'
import type { RungLadderState } from '@root/renderer/store/slices'
Expand Down Expand Up @@ -76,35 +77,13 @@ export const RungBody = ({ rung, className, nodeDivergences = [], isDebuggerActi
searchQuery,
searchActions: { setSearchNodePosition },
snapshotActions: { addSnapshot },
workspace: { isDebuggerVisible, debugVariableValues, fbSelectedInstance, fbDebugInstances },
workspace: { isDebuggerVisible, debugVariableValues },
} = useOpenPLCStore()
const getCompositeKey = useDebugCompositeKey()

const pouRef = project.data.pous.find((pou) => pou.data.name === editor.meta.name)
const nodeTypes = useMemo(() => customNodeTypes, [])

// Get FB instance context for function block POUs
const fbInstanceContext = useMemo(() => {
if (!pouRef || pouRef.type !== 'function-block') return null
const fbTypeKey = pouRef.data.name.toUpperCase() // Canonical key for map lookups
const selectedKey = fbSelectedInstance.get(fbTypeKey)
if (!selectedKey) return null
const instances = fbDebugInstances.get(fbTypeKey) || []
return instances.find((inst) => inst.key === selectedKey) || null
}, [pouRef, fbSelectedInstance, fbDebugInstances])

// Helper to get composite key for variable lookup, handling FB instance context
const getCompositeKey = useCallback(
(variableName: string): string => {
if (fbInstanceContext) {
// For FB POUs, transform to instance context: main:MOTOR_SPEED0.varName
return `${fbInstanceContext.programName}:${fbInstanceContext.fbVariableName}.${variableName}`
}
// For programs, use standard format: pouName:varName
return `${editor.meta.name}:${variableName}`
},
[fbInstanceContext, editor.meta.name],
)

const [rungLocal, setRungLocal] = useState<RungLadderState>(rung)
const [dragging, setDragging] = useState(false)

Expand Down Expand Up @@ -158,7 +137,7 @@ export const RungBody = ({ rung, className, nodeDivergences = [], isDebuggerActi

// For program POUs, verify the program instance exists
// For FB POUs, skip this check since getCompositeKey already handles instance context
if (!fbInstanceContext) {
if (pouRef?.type !== 'function-block') {
const instances = project.data.configuration.resource.instances
const programInstance = instances.find((inst) => inst.program === editor.meta.name)
if (!programInstance) return undefined
Expand Down
1 change: 1 addition & 0 deletions src/renderer/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './use-compiler'
export * from './use-debug-composite-key'
export * from './use-store-selectors'
Loading