Add automatic controller propagation for dualpath machines

When a PC is assigned to control equipment that has a dualpath relationship,
the controller is now automatically propagated to the dualpath partner machine.

Changes:
- includes/db_helpers.asp: Add PropagateControllerToDualpathMachines() and
  PropagateControllerFromDualpathMachine() helper functions
- savemachine_direct.asp: Call propagation on new equipment creation
- savemachineedit.asp: Call propagation when editing equipment relationships
- api.asp: Add PropagateControllerToDualpathMachinesAPI() for PowerShell API

Also includes one-time fix script (sql/fix_dualpath_controller_relationships.sql)
that fixes 140 backwards relationships (Equipment->PC to PC->Equipment) and
propagates controllers to 30 dualpath machines that were missing them.

Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
cproudlock
2025-12-10 20:04:01 -05:00
parent 30ea1bb42f
commit f8083be467
5 changed files with 407 additions and 0 deletions

View File

@@ -263,4 +263,172 @@ Function GetRecordCount(conn, tableName, whereClause, params)
GetRecordCount = 0
End If
End Function
'-----------------------------------------------------------------------------
' FUNCTION: PropagateControllerToDualpathMachines
' PURPOSE: When a PC controls a machine, also assign it to dualpath'd machines
' PARAMETERS:
' conn (ADODB.Connection) - Database connection object
' equipmentMachineid (Integer) - The equipment machine being controlled
' pcMachineid (Integer) - The PC that controls the equipment
' RETURNS: Integer - Number of relationships created
' USAGE: Call after creating a "Controls" relationship
'-----------------------------------------------------------------------------
Function PropagateControllerToDualpathMachines(conn, equipmentMachineid, pcMachineid)
On Error Resume Next
Dim rsDP, rsDPCheck, controlsTypeID, dualpathMachineId, cnt
cnt = 0
' Get Controls relationship type ID
Set rsDP = conn.Execute("SELECT relationshiptypeid FROM relationshiptypes WHERE relationshiptype = 'Controls'")
If rsDP.EOF Then
PropagateControllerToDualpathMachines = 0
rsDP.Close
Set rsDP = Nothing
Exit Function
End If
controlsTypeID = CLng(rsDP("relationshiptypeid"))
rsDP.Close
Set rsDP = Nothing
' Find all machines with dualpath relationship to this equipment
Set rsDP = conn.Execute("SELECT related_machineid FROM machinerelationships mr " & _
"JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid " & _
"WHERE mr.machineid = " & CLng(equipmentMachineid) & " " & _
"AND rt.relationshiptype = 'Dualpath' AND mr.isactive = 1")
Do While Not rsDP.EOF
dualpathMachineId = CLng(rsDP("related_machineid"))
' Check if this dualpath machine already has a Controls relationship with this PC
Set rsDPCheck = conn.Execute("SELECT relationshipid FROM machinerelationships " & _
"WHERE machineid = " & CLng(pcMachineid) & " " & _
"AND related_machineid = " & dualpathMachineId & " " & _
"AND relationshiptypeid = " & controlsTypeID & " AND isactive = 1")
If rsDPCheck.EOF Then
' Create Controls relationship: PC -> Dualpath Machine
Dim cmdDP
Set cmdDP = Server.CreateObject("ADODB.Command")
cmdDP.ActiveConnection = conn
cmdDP.CommandText = "INSERT INTO machinerelationships (machineid, related_machineid, relationshiptypeid, isactive) VALUES (?, ?, ?, 1)"
cmdDP.Parameters.Append cmdDP.CreateParameter("@pcid", 3, 1, , CLng(pcMachineid))
cmdDP.Parameters.Append cmdDP.CreateParameter("@equipid", 3, 1, , dualpathMachineId)
cmdDP.Parameters.Append cmdDP.CreateParameter("@reltypeid", 3, 1, , controlsTypeID)
cmdDP.Execute
Set cmdDP = Nothing
cnt = cnt + 1
End If
rsDPCheck.Close
Set rsDPCheck = Nothing
rsDP.MoveNext
Loop
rsDP.Close
Set rsDP = Nothing
PropagateControllerToDualpathMachines = cnt
End Function
'-----------------------------------------------------------------------------
' FUNCTION: PropagateControllerFromDualpathMachine
' PURPOSE: When creating a dualpath relationship, copy controller from partner
' PARAMETERS:
' conn (ADODB.Connection) - Database connection object
' machineId1 (Integer) - First machine in dualpath
' machineId2 (Integer) - Second machine in dualpath
' RETURNS: Integer - Number of relationships created
' USAGE: Call after creating a dualpath relationship
'-----------------------------------------------------------------------------
Function PropagateControllerFromDualpathMachine(conn, machineId1, machineId2)
On Error Resume Next
Dim rsCtrl, rsCheck, controlsTypeID, pcMachineid, cnt
cnt = 0
' Get Controls relationship type ID
Set rsCtrl = conn.Execute("SELECT relationshiptypeid FROM relationshiptypes WHERE relationshiptype = 'Controls'")
If rsCtrl.EOF Then
PropagateControllerFromDualpathMachine = 0
rsCtrl.Close
Set rsCtrl = Nothing
Exit Function
End If
controlsTypeID = CLng(rsCtrl("relationshiptypeid"))
rsCtrl.Close
Set rsCtrl = Nothing
' Check if machine1 has a controller, copy to machine2 if machine2 doesn't have one
Set rsCtrl = conn.Execute("SELECT machineid FROM machinerelationships " & _
"WHERE related_machineid = " & CLng(machineId1) & " " & _
"AND relationshiptypeid = " & controlsTypeID & " AND isactive = 1")
If Not rsCtrl.EOF Then
pcMachineid = CLng(rsCtrl("machineid"))
rsCtrl.Close
Set rsCtrl = Nothing
' Check if machine2 already has this controller
Set rsCheck = conn.Execute("SELECT relationshipid FROM machinerelationships " & _
"WHERE machineid = " & pcMachineid & " AND related_machineid = " & CLng(machineId2) & " " & _
"AND relationshiptypeid = " & controlsTypeID & " AND isactive = 1")
If rsCheck.EOF Then
' Create: PC -> Controls -> machine2
Dim cmdCtrl
Set cmdCtrl = Server.CreateObject("ADODB.Command")
cmdCtrl.ActiveConnection = conn
cmdCtrl.CommandText = "INSERT INTO machinerelationships (machineid, related_machineid, relationshiptypeid, isactive) VALUES (?, ?, ?, 1)"
cmdCtrl.Parameters.Append cmdCtrl.CreateParameter("@pcid", 3, 1, , pcMachineid)
cmdCtrl.Parameters.Append cmdCtrl.CreateParameter("@equipid", 3, 1, , CLng(machineId2))
cmdCtrl.Parameters.Append cmdCtrl.CreateParameter("@reltypeid", 3, 1, , controlsTypeID)
cmdCtrl.Execute
Set cmdCtrl = Nothing
cnt = cnt + 1
End If
rsCheck.Close
Set rsCheck = Nothing
Else
rsCtrl.Close
Set rsCtrl = Nothing
End If
' Now check if machine2 has a controller, copy to machine1 if machine1 doesn't have one
Set rsCtrl = conn.Execute("SELECT machineid FROM machinerelationships " & _
"WHERE related_machineid = " & CLng(machineId2) & " " & _
"AND relationshiptypeid = " & controlsTypeID & " AND isactive = 1")
If Not rsCtrl.EOF Then
pcMachineid = CLng(rsCtrl("machineid"))
rsCtrl.Close
Set rsCtrl = Nothing
' Check if machine1 already has this controller
Set rsCheck = conn.Execute("SELECT relationshipid FROM machinerelationships " & _
"WHERE machineid = " & pcMachineid & " AND related_machineid = " & CLng(machineId1) & " " & _
"AND relationshiptypeid = " & controlsTypeID & " AND isactive = 1")
If rsCheck.EOF Then
' Create: PC -> Controls -> machine1
Dim cmdCtrl2
Set cmdCtrl2 = Server.CreateObject("ADODB.Command")
cmdCtrl2.ActiveConnection = conn
cmdCtrl2.CommandText = "INSERT INTO machinerelationships (machineid, related_machineid, relationshiptypeid, isactive) VALUES (?, ?, ?, 1)"
cmdCtrl2.Parameters.Append cmdCtrl2.CreateParameter("@pcid", 3, 1, , pcMachineid)
cmdCtrl2.Parameters.Append cmdCtrl2.CreateParameter("@equipid", 3, 1, , CLng(machineId1))
cmdCtrl2.Parameters.Append cmdCtrl2.CreateParameter("@reltypeid", 3, 1, , controlsTypeID)
cmdCtrl2.Execute
Set cmdCtrl2 = Nothing
cnt = cnt + 1
End If
rsCheck.Close
Set rsCheck = Nothing
Else
rsCtrl.Close
Set rsCtrl = Nothing
End If
PropagateControllerFromDualpathMachine = cnt
End Function
%>