'************************************************************************************************************************
' File: PMACSfaultlocation.vb
' Programmer: Ting Chan
' Date: June 28, 2005
'
' Description:
' This form is the Fault Location window for the PMACS client GUI of the MARS project.
' This form is the control and display form for Fault Location estimation
' This form allows the user run the fault location algorithm to determine the location
' of a backbone cable fault
' Fault location requires the shore power supply to be operating at a low voltage, the
' operator will have a chance to confirm before running it.
'
' Revision History:
'
'***********************************************************************************************************************
Friend Class PMACSfaultlocation
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()
        If m_vb6FormDefInstance Is Nothing Then
            If m_InitializingDefInstance Then
                m_vb6FormDefInstance = Me
            Else
                Try
                    'For the start-up form, the first instance created is the default instance.
                    If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
                        m_vb6FormDefInstance = Me
                    End If
                Catch
                End Try
            End If
        End If
        'This call is required by the Windows Form Designer.
        InitializeComponent()
        'This form is an MDI child.
        'This code simulates the VB6 
        ' functionality of automatically
        ' loading and showing an MDI
        ' child's parent.
        Me.MdiParent = Project1.PMACSstartup.DefInstance.DefInstance
        Project1.PMACSstartup.DefInstance.DefInstance.Show()

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents FLRun As System.Windows.Forms.Button
    Friend WithEvents FLStop As System.Windows.Forms.Button
    Public WithEvents ShoreI As System.Windows.Forms.TextBox
    Public WithEvents ShoreV As System.Windows.Forms.TextBox
    Public WithEvents Label2 As System.Windows.Forms.Label
    Public WithEvents Label1 As System.Windows.Forms.Label
    Public WithEvents Label3 As System.Windows.Forms.Label
    Public WithEvents FLOutput As System.Windows.Forms.TextBox
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents FaultLocationStatus As System.Windows.Forms.Label
    Public WithEvents DateTime As System.Windows.Forms.Label
    Friend WithEvents Label21 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.FLRun = New System.Windows.Forms.Button
        Me.FLStop = New System.Windows.Forms.Button
        Me.ShoreI = New System.Windows.Forms.TextBox
        Me.ShoreV = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label1 = New System.Windows.Forms.Label
        Me.FLOutput = New System.Windows.Forms.TextBox
        Me.Label3 = New System.Windows.Forms.Label
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.FaultLocationStatus = New System.Windows.Forms.Label
        Me.DateTime = New System.Windows.Forms.Label
        Me.Label21 = New System.Windows.Forms.Label
        Me.Panel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'FLRun
        '
        Me.FLRun.BackColor = System.Drawing.Color.Silver
        Me.FLRun.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.FLRun.ForeColor = System.Drawing.Color.Black
        Me.FLRun.Location = New System.Drawing.Point(352, 472)
        Me.FLRun.Name = "FLRun"
        Me.FLRun.Size = New System.Drawing.Size(208, 72)
        Me.FLRun.TabIndex = 0
        Me.FLRun.Text = "Start Fault Location Process"
        '
        'FLStop
        '
        Me.FLStop.BackColor = System.Drawing.Color.Silver
        Me.FLStop.Enabled = False
        Me.FLStop.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.FLStop.Location = New System.Drawing.Point(672, 472)
        Me.FLStop.Name = "FLStop"
        Me.FLStop.Size = New System.Drawing.Size(208, 72)
        Me.FLStop.TabIndex = 1
        Me.FLStop.Text = "Stop Fault Location Process"
        '
        'ShoreI
        '
        Me.ShoreI.AcceptsReturn = True
        Me.ShoreI.AutoSize = False
        Me.ShoreI.BackColor = System.Drawing.SystemColors.Window
        Me.ShoreI.Cursor = System.Windows.Forms.Cursors.IBeam
        Me.ShoreI.Font = New System.Drawing.Font("Arial", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.ShoreI.ForeColor = System.Drawing.SystemColors.WindowText
        Me.ShoreI.Location = New System.Drawing.Point(880, 208)
        Me.ShoreI.MaxLength = 0
        Me.ShoreI.Name = "ShoreI"
        Me.ShoreI.ReadOnly = True
        Me.ShoreI.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.ShoreI.Size = New System.Drawing.Size(120, 24)
        Me.ShoreI.TabIndex = 7
        Me.ShoreI.Text = "N/A"
        Me.ShoreI.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'ShoreV
        '
        Me.ShoreV.AcceptsReturn = True
        Me.ShoreV.AutoSize = False
        Me.ShoreV.BackColor = System.Drawing.SystemColors.Window
        Me.ShoreV.Cursor = System.Windows.Forms.Cursors.IBeam
        Me.ShoreV.Font = New System.Drawing.Font("Arial", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.ShoreV.ForeColor = System.Drawing.Color.Black
        Me.ShoreV.Location = New System.Drawing.Point(440, 208)
        Me.ShoreV.MaxLength = 0
        Me.ShoreV.Name = "ShoreV"
        Me.ShoreV.ReadOnly = True
        Me.ShoreV.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.ShoreV.Size = New System.Drawing.Size(120, 24)
        Me.ShoreV.TabIndex = 6
        Me.ShoreV.Text = "N/A"
        Me.ShoreV.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'Label2
        '
        Me.Label2.BackColor = System.Drawing.Color.Transparent
        Me.Label2.Cursor = System.Windows.Forms.Cursors.Default
        Me.Label2.Font = New System.Drawing.Font("Arial", 13.5!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.ForeColor = System.Drawing.Color.Yellow
        Me.Label2.Location = New System.Drawing.Point(672, 200)
        Me.Label2.Name = "Label2"
        Me.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Label2.Size = New System.Drawing.Size(200, 48)
        Me.Label2.TabIndex = 5
        Me.Label2.Text = "Shore Station Current (Measured)"
        '
        'Label1
        '
        Me.Label1.BackColor = System.Drawing.Color.Transparent
        Me.Label1.Cursor = System.Windows.Forms.Cursors.Default
        Me.Label1.Font = New System.Drawing.Font("Arial", 13.5!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.ForeColor = System.Drawing.Color.Yellow
        Me.Label1.Location = New System.Drawing.Point(232, 200)
        Me.Label1.Name = "Label1"
        Me.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Label1.Size = New System.Drawing.Size(200, 48)
        Me.Label1.TabIndex = 4
        Me.Label1.Text = "Shore Station Voltage (Measured)"
        '
        'FLOutput
        '
        Me.FLOutput.AcceptsReturn = True
        Me.FLOutput.AutoSize = False
        Me.FLOutput.BackColor = System.Drawing.SystemColors.Window
        Me.FLOutput.Cursor = System.Windows.Forms.Cursors.IBeam
        Me.FLOutput.Font = New System.Drawing.Font("Arial", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.FLOutput.ForeColor = System.Drawing.SystemColors.WindowText
        Me.FLOutput.Location = New System.Drawing.Point(672, 296)
        Me.FLOutput.MaxLength = 0
        Me.FLOutput.Name = "FLOutput"
        Me.FLOutput.ReadOnly = True
        Me.FLOutput.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.FLOutput.Size = New System.Drawing.Size(120, 24)
        Me.FLOutput.TabIndex = 9
        Me.FLOutput.Text = "N/A"
        Me.FLOutput.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'Label3
        '
        Me.Label3.BackColor = System.Drawing.Color.Transparent
        Me.Label3.Cursor = System.Windows.Forms.Cursors.Default
        Me.Label3.Font = New System.Drawing.Font("Arial", 13.5!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label3.ForeColor = System.Drawing.Color.Yellow
        Me.Label3.Location = New System.Drawing.Point(424, 296)
        Me.Label3.Name = "Label3"
        Me.Label3.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Label3.Size = New System.Drawing.Size(232, 24)
        Me.Label3.TabIndex = 8
        Me.Label3.Text = "Estimated Fault Location"
        '
        'Panel1
        '
        Me.Panel1.BackColor = System.Drawing.Color.Silver
        Me.Panel1.Controls.Add(Me.FaultLocationStatus)
        Me.Panel1.Location = New System.Drawing.Point(336, 72)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(568, 56)
        Me.Panel1.TabIndex = 10
        '
        'FaultLocationStatus
        '
        Me.FaultLocationStatus.BackColor = System.Drawing.Color.Black
        Me.FaultLocationStatus.Font = New System.Drawing.Font("Microsoft Sans Serif", 20.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.FaultLocationStatus.ForeColor = System.Drawing.Color.White
        Me.FaultLocationStatus.Location = New System.Drawing.Point(8, 8)
        Me.FaultLocationStatus.Name = "FaultLocationStatus"
        Me.FaultLocationStatus.Size = New System.Drawing.Size(552, 40)
        Me.FaultLocationStatus.TabIndex = 0
        Me.FaultLocationStatus.Text = "Fault Location"
        Me.FaultLocationStatus.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'DateTime
        '
        Me.DateTime.BackColor = System.Drawing.Color.Transparent
        Me.DateTime.Cursor = System.Windows.Forms.Cursors.Default
        Me.DateTime.Font = New System.Drawing.Font("Arial", 20.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.DateTime.ForeColor = System.Drawing.Color.Yellow
        Me.DateTime.Location = New System.Drawing.Point(792, 16)
        Me.DateTime.Name = "DateTime"
        Me.DateTime.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.DateTime.Size = New System.Drawing.Size(464, 33)
        Me.DateTime.TabIndex = 599
        Me.DateTime.Text = "Date"
        Me.DateTime.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'Label21
        '
        Me.Label21.BackColor = System.Drawing.Color.FromArgb(CType(224, Byte), CType(224, Byte), CType(224, Byte))
        Me.Label21.Location = New System.Drawing.Point(784, 8)
        Me.Label21.Name = "Label21"
        Me.Label21.Size = New System.Drawing.Size(480, 48)
        Me.Label21.TabIndex = 600
        '
        'PMACSfaultlocation
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.Black
        Me.ClientSize = New System.Drawing.Size(1272, 990)
        Me.Controls.Add(Me.DateTime)
        Me.Controls.Add(Me.Label21)
        Me.Controls.Add(Me.Panel1)
        Me.Controls.Add(Me.FLOutput)
        Me.Controls.Add(Me.ShoreI)
        Me.Controls.Add(Me.ShoreV)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.FLStop)
        Me.Controls.Add(Me.FLRun)
        Me.Name = "PMACSfaultlocation"
        Me.Text = "PMACS Fault Location"
        Me.Panel1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region
#Region "Upgrade Support "
    Private Shared m_vb6FormDefInstance As PMACSfaultlocation
    Private Shared m_InitializingDefInstance As Boolean
    Public Shared Property DefInstance() As PMACSfaultlocation
        Get
            If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
                m_InitializingDefInstance = True
                m_vb6FormDefInstance = New PMACSfaultlocation
                m_InitializingDefInstance = False
            End If
            DefInstance = m_vb6FormDefInstance
        End Get
        Set(ByVal Value As PMACSfaultlocation)
            m_vb6FormDefInstance = Value
        End Set
    End Property
#End Region

#Region "Run Fault Location"
    Private Sub FLRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FLRun.Click
        Dim response As Integer 'jbe: work variable debug only.
        Dim FaultLocationOutput As Double
        Dim ShoreStationState As Integer
        Dim ONOFFresult As String
        Dim ws As New edu.washington.apl.pmacs.load_service  'work variable.

        'confirm with user if he/she wants to run fault location
        response = MsgBox("To run Fault Location, PMACS will have to lower the shore voltage output below normal operating condition. Continue?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "Confirm Operation")
        If response = MsgBoxResult.Yes Then
            FLRun.Enabled = False
            FLStop.Enabled = True

            'don't load science node data from server 
            wantPauseLoadDataThread = True

            'pause event updating timer since we will temporily stop from 
            'Loading data from server
            PMACSmain.DefInstance.TimerUpdateEvents.Enabled = False

            'set a flag that tells fault location to run
            'the fault location algorithm will run until
            'the flag is reset
            RunFaultLocation = True

            'display on the window telling the user Fault Location is running
            FaultLocationStatus.Text = "Fault Location is running"
            FaultLocationStatus.ForeColor = System.Drawing.Color.Lime

            If Shore.ShoreState = 0 Then
                'if the shore power supply is off, turn it ON
                ShoreStationState = 1
                ONOFFresult = ws.setSPSState(ShoreStationState)
                If ONOFFresult = "ok" Then
                    'shore power supply has been turned ON
                    MsgBox("Shore Power Supply is now ON.")
                Else
                    RunFaultLocation = False
                    FLStop.Enabled = False
                    FLRun.Enabled = True
                    FaultLocationStatus.Text = "Fault Location is not running"
                    MsgBox("Server is Busy at this moment. Try running Fault Location at another time.")
                End If
            End If
        End If
    End Sub
#End Region

#Region "Load Fault Location Window"
    Private Sub PMACSfaultlocation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'RunFaultLocation = False
        If LoginType = 1 Then
            If RunFaultLocation Then
                FaultLocationStatus.Text = "Fault Location is running"
                FaultLocationStatus.ForeColor = System.Drawing.Color.Lime
                FLRun.Enabled = False
                FLStop.Enabled = True
            Else
                FaultLocationStatus.Text = "Fault Location is not running"
                FaultLocationStatus.ForeColor = System.Drawing.Color.Red
                FLRun.Enabled = True
                FLStop.Enabled = False
            End If
        Else
            FLRun.Enabled = False
            FLStop.Enabled = False
            FaultLocationStatus.Text = "Fault Location is disabled for Remote Clients"
        End If
    End Sub
#End Region

#Region "Stop Fault Location"
    Private Sub FLStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FLStop.Click
        RunFaultLocation = False
        FLStop.Enabled = False
        FLRun.Enabled = True
        FaultLocationStatus.Text = "Fault Location is not running"
        FaultLocationStatus.ForeColor = System.Drawing.Color.Red
        PMACSmain.DefInstance.TimerUpdateEvents.Enabled = True
    End Sub
#End Region

End Class
