Option Strict Off
Option Explicit On 
Imports System.Runtime.InteropServices

Friend Class frmMirror
	Inherits System.Windows.Forms.Form
#Region "Windows tH[ fUCiɂĐꂽR[h"
	Public Sub New()
		MyBase.New()
		If m_vb6FormDefInstance Is Nothing Then
			If m_InitializingDefInstance Then
				m_vb6FormDefInstance = Me
			Else
				Try 
					'X^[gAbv tH[ɂẮAŏɍ쐬ꂽCX^XCX^XɂȂ܂B
					If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
						m_vb6FormDefInstance = Me
					End If
				Catch
				End Try
			End If
		End If
		'̌ĂяóAWindows tH[ fUCiŕKvłB
		InitializeComponent()
	End Sub
	'Form ́AR|[lgꗗɌ㏈s邽߂ dispose I[o[Ch܂B
	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
	'Windows tH[ fUCiŕKvłB
	Private components As System.ComponentModel.IContainer
	Public ToolTip1 As System.Windows.Forms.ToolTip
	Public WithEvents Timer1 As System.Windows.Forms.Timer
	Public WithEvents lblAllow As System.Windows.Forms.Label
	Public WithEvents lblDrive2 As System.Windows.Forms.Label
	Public WithEvents lblDrive1 As System.Windows.Forms.Label
	Public WithEvents LedDrive2 As System.Windows.Forms.Label
	Public WithEvents LedDrive1 As System.Windows.Forms.Label
	Public WithEvents lblSmartState As System.Windows.Forms.Label
	' : ȉ̃vV[W Windows tH[ fUCiŕKvłB
	'Windows tH[ fUCigĕύXł܂B
	'R[h GfB^gďCȂłB
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmMirror))
        Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.lblAllow = New System.Windows.Forms.Label
        Me.lblDrive2 = New System.Windows.Forms.Label
        Me.lblDrive1 = New System.Windows.Forms.Label
        Me.LedDrive2 = New System.Windows.Forms.Label
        Me.LedDrive1 = New System.Windows.Forms.Label
        Me.lblSmartState = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'Timer1
        '
        Me.Timer1.Interval = 1
        '
        'lblAllow
        '
        Me.lblAllow.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom), System.Windows.Forms.AnchorStyles)
        Me.lblAllow.BackColor = System.Drawing.SystemColors.Control
        Me.lblAllow.Cursor = System.Windows.Forms.Cursors.Default
        Me.lblAllow.Font = New System.Drawing.Font("lr oSVbN", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.lblAllow.ForeColor = System.Drawing.SystemColors.ControlText
        Me.lblAllow.Location = New System.Drawing.Point(98, 40)
        Me.lblAllow.Name = "lblAllow"
        Me.lblAllow.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.lblAllow.Size = New System.Drawing.Size(45, 19)
        Me.lblAllow.TabIndex = 3
        Me.lblAllow.Text = "Label1"
        Me.lblAllow.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'lblDrive2
        '
        Me.lblDrive2.BackColor = System.Drawing.SystemColors.Control
        Me.lblDrive2.Cursor = System.Windows.Forms.Cursors.Default
        Me.lblDrive2.ForeColor = System.Drawing.SystemColors.ControlText
        Me.lblDrive2.Location = New System.Drawing.Point(144, 8)
        Me.lblDrive2.Name = "lblDrive2"
        Me.lblDrive2.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.lblDrive2.Size = New System.Drawing.Size(73, 17)
        Me.lblDrive2.TabIndex = 2
        Me.lblDrive2.Text = "DRIVE2"
        Me.lblDrive2.TextAlign = System.Drawing.ContentAlignment.TopCenter
        '
        'lblDrive1
        '
        Me.lblDrive1.BackColor = System.Drawing.SystemColors.Control
        Me.lblDrive1.Cursor = System.Windows.Forms.Cursors.Default
        Me.lblDrive1.ForeColor = System.Drawing.SystemColors.ControlText
        Me.lblDrive1.Location = New System.Drawing.Point(24, 8)
        Me.lblDrive1.Name = "lblDrive1"
        Me.lblDrive1.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.lblDrive1.Size = New System.Drawing.Size(73, 17)
        Me.lblDrive1.TabIndex = 1
        Me.lblDrive1.Text = "DRIVE1"
        Me.lblDrive1.TextAlign = System.Drawing.ContentAlignment.TopCenter
        '
        'LedDrive2
        '
        Me.LedDrive2.BackColor = System.Drawing.Color.Green
        Me.LedDrive2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.LedDrive2.Location = New System.Drawing.Point(144, 32)
        Me.LedDrive2.Name = "LedDrive2"
        Me.LedDrive2.Size = New System.Drawing.Size(73, 33)
        Me.LedDrive2.TabIndex = 4
        '
        'LedDrive1
        '
        Me.LedDrive1.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(128, Byte), CType(0, Byte))
        Me.LedDrive1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.LedDrive1.Location = New System.Drawing.Point(24, 32)
        Me.LedDrive1.Name = "LedDrive1"
        Me.LedDrive1.Size = New System.Drawing.Size(73, 33)
        Me.LedDrive1.TabIndex = 5
        '
        'lblSmartState
        '
        Me.lblSmartState.BackColor = System.Drawing.SystemColors.Control
        Me.lblSmartState.Cursor = System.Windows.Forms.Cursors.Default
        Me.lblSmartState.ForeColor = System.Drawing.SystemColors.ControlText
        Me.lblSmartState.Location = New System.Drawing.Point(0, 72)
        Me.lblSmartState.Name = "lblSmartState"
        Me.lblSmartState.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.lblSmartState.Size = New System.Drawing.Size(240, 15)
        Me.lblSmartState.TabIndex = 0
        Me.lblSmartState.Text = "Label1"
        Me.lblSmartState.TextAlign = System.Drawing.ContentAlignment.TopCenter
        '
        'frmMirror
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.BackColor = System.Drawing.SystemColors.Control
        Me.ClientSize = New System.Drawing.Size(244, 88)
        Me.Controls.Add(Me.lblAllow)
        Me.Controls.Add(Me.lblSmartState)
        Me.Controls.Add(Me.lblDrive2)
        Me.Controls.Add(Me.lblDrive1)
        Me.Controls.Add(Me.LedDrive2)
        Me.Controls.Add(Me.LedDrive1)
        Me.Cursor = System.Windows.Forms.Cursors.Default
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Location = New System.Drawing.Point(3, 22)
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "frmMirror"
        Me.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Text = "Mirror State"
        Me.ResumeLayout(False)

    End Sub
#End Region 
#Region "AbvO[h EBU[h̃T|[g R[h"
	Private Shared m_vb6FormDefInstance As frmMirror
	Private Shared m_InitializingDefInstance As Boolean
	Public Shared Property DefInstance() As frmMirror
		Get
			If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
				m_InitializingDefInstance = True
				m_vb6FormDefInstance = New frmMirror()
				m_InitializingDefInstance = False
			End If
			DefInstance = m_vb6FormDefInstance
		End Get
		Set
			m_vb6FormDefInstance = Value
		End Set
	End Property
#End Region 
	
	'*****************************************************************************
    '   t@C F frmMirror.vb
	'
	'   @\ F ~[Xe[^X\@\
	'
	'
    ' Copyright (C) NEC Corporation 2009
	'*****************************************************************************
	
	
	
	
	'*****************************************************************************
	' HDD擾p萔`
	'*****************************************************************************
	
	' RETURNXe[^X
	Private Const FCSRASAPI_SUCCESS As Short = &H0s
	Private Const FCSRASAPI_DRVERR As Short = &H1s
	
	
	' HDD̃ANZXw
	Private Const HDDSTS_PRI_MAS As Short = &H10s ' vC}}X^w
	Private Const HDDSTS_PRI_SRV As Short = &H20s ' vC}X[uw
	Private Const HDDSTS_SEC_MAS As Short = &H30s ' ZJ_}X^w
	Private Const HDDSTS_SEC_SRV As Short = &H40s ' ZJ_X[uw
	
	Private Const HDDSTS_MRR_DRV1 As Short = &H1s ' ~[hCuPw
	Private Const HDDSTS_MRR_DRV2 As Short = &H2s ' ~[hCuQw
	
	' HDD󋵂ޯď (IOCTL_FCSOFTRAS_GET_HDDINF)
	Private Const HDDCONN_SMARTERR_BIT As Byte = &H1s ' SMARTG[  0:   1:ُ
	Private Const HDDCONN_CONNECT_BIT As Byte = &H40s ' HDDڑ      0:ڑ 1:ڑL
	Private Const HDDCONN_MIRROR_BIT As Byte = &H80s ' װ      0:ڑ 1:ڑL
	
	' HDDð (IOCTL_FCSOFTRAS_GET_HDDINF)
	Private Const HDDSTS_NORMAL As Byte = &H0s ' 
	Private Const HDDSTS_ERR_DRV1 As Byte = &H10s ' Drive1̏ (ײإϽ or װ_1)
	Private Const HDDSTS_ERR_DRV2 As Byte = &H11s ' Drive2̏ (ײإڰ or װ_2)
	Private Const HDDSTS_ERR_DRV3 As Byte = &H12s ' Drive3̏ (إϽ)
	Private Const HDDSTS_ERR_DRV4 As Byte = &H13s ' Drive4̏ (إڰ)
	
	Private Const HDDSTS_RECON_1 As Byte = &H20s ' ްč\z(Drive2Drive1ֺ߰)
	Private Const HDDSTS_RECON_2 As Byte = &H21s ' ްč\z(Drive1Drive2ֺ߰)
	Private Const HDDSTS_RECON_SKIP_1 As Byte = &H30s ' غݔ(Drive1Drive2ްč\z)
	Private Const HDDSTS_RECON_SKIP_2 As Byte = &H31s ' غݔ(Drive2Drive1ްč\z)

    Private Const HDDSTS_PATROLRD As Byte = &H50      ' ۰ذޒ

    '*****************************************************************************
	' \tgRASI/F`
	'*****************************************************************************
	Private Declare Function fcsoftrasOpen Lib "FcSrasapiP.dll"  Alias "fcsoftrasOpenP"() As Integer
	Private Declare Function fcsoftrasClose Lib "FcSrasapiP.dll"  Alias "fcsoftrasCloseP"() As Integer
    Private Declare Function fcsoftrasGetMirrorStatus Lib "FcSrasapiP.dll" Alias "fcsoftrasGetMirrorStatusP" (ByVal DriveNum As Byte, <MarshalAs(UnmanagedType.SafeArray)> ByRef buff() As Byte) As Integer
	Private Declare Function fcsoftrasGetMirrorReconProgress Lib "FcSrasapiP.dll"  Alias "fcsoftrasGetMirrorReconProgressP"(ByVal DriveNum As Byte, ByRef buff As Byte) As Integer
	
	'****************************************************
    '   ֐ F frmMirror_Load
	'   @\   Fʂ̋N()
	'      F Ȃ
	'   ߂l F Ȃ
	'****************************************************
	Private Sub frmMirror_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		' ƃtH_st@CtH_ɕύX DLLǂݏo
        ChDrive(Application.StartupPath)
        ChDir(Application.StartupPath)

		' \tgRAS̃Cu
		Call fcsoftrasOpen()
		' ݂̏Ԃ擾Ɖʂւ̔f
		Call subSetMirrorState()
		
		' ʍXVp̃^C}[ݒ
		Timer1.Interval = 30000 ' 30bݒ
		Timer1.Enabled = True ' L
		
	End Sub
	
	'****************************************************
    '   ֐ F frmMirror_Closed
	'   @\   Fʂ̏I(㏈)
	'      F Ȃ
	'   ߂l F Ȃ
	'****************************************************
    Private Sub frmMirror_Closed(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Closed
        ' \tgRAS̃CuI
        Call fcsoftrasClose()
    End Sub

    '****************************************************
    '   ֐ F Timer1_Tick
    '   @\   F^C}[ɂʍXV
    '      F Ȃ
    '   ߂l F Ȃ
    '****************************************************
    Private Sub Timer1_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer1.Tick
        ' ݂̏Ԃ擾Ɖʂւ̔f
        Call subSetMirrorState()
    End Sub

    '****************************************************
    '   ֐ F subSetMirrorState
    '   @\   F~[̏Ԃ擾Aʂ̍XVsȂ
    '      F Ȃ
    '   ߂l F Ȃ
    '****************************************************
    Private Sub subSetMirrorState()
        Dim buff(2) As Byte ' ~[Xe[^Xۑp
        Dim Ret As Short ' ߂lۑp
        Dim Progress As Byte ' i󋵕ۑp

        ' ~[Xe[^X̎擾
        Ret = fcsoftrasGetMirrorStatus(HDDSTS_PRI_MAS, buff)

        ' ~[Xe[^X擾H
        If FCSRASAPI_SUCCESS = Ret Then

            With lblSmartState
                ' ~[Xe[^X̏󋵂ɂʂXV
                Select Case buff(0)

                    ' 
                Case HDDSTS_NORMAL
                        .Text = "Normal" ' ڍ׏ݒ
                        LedDrive1.BackColor = Color.Lime ' hCu1 LEDݒ()
                        LedDrive2.BackColor = Color.Lime ' hCu2 LEDݒ()
                        lblAllow.Text = "" ' č\zp\

                        ' hCuPG[
                    Case HDDSTS_ERR_DRV1
                        .Text = "DRIVE1 failure" ' ڍ׏ݒ
                        LedDrive1.BackColor = Color.Red ' hCu1 LEDݒ()
                        LedDrive2.BackColor = Color.Lime ' hCu2 LEDݒ()
                        lblAllow.Text = "" ' č\zp\

                        ' hCuQG[
                    Case HDDSTS_ERR_DRV2
                        .Text = "DRIVE2 failure" ' ڍ׏ݒ
                        LedDrive1.BackColor = Color.Lime ' hCu1 LEDݒ()
                        LedDrive2.BackColor = Color.Red ' hCu2 LEDݒ()
                        lblAllow.Text = "" ' č\zp\

                        ' f[^č\z(DRIVE1DRIVE2)
                    Case HDDSTS_RECON_1
                        ' i̎擾
                        If FCSRASAPI_SUCCESS = fcsoftrasGetMirrorReconProgress(HDDSTS_PRI_MAS, Progress) Then
                            ' ڍ׏ݒ(i܂)
                            .Text = "Currently copying(DRIVE1<-DRIVE2)" & Progress & "%"

                            ' i̎擾s
                        Else
                            ' ڍ׏ݒ
                            .Text = "Currently copying(DRIVE1<-DRIVE2)"
                        End If
                        LedDrive1.BackColor = Color.Orange ' hCu1 LEDݒ()
                        LedDrive2.BackColor = Color.Lime ' hCu2 LEDݒ()
                        lblAllow.Text = "<<"

                        ' f[^č\z(DRIVE1DRIVE2)
                    Case HDDSTS_RECON_2
                        ' i̎擾
                        If FCSRASAPI_SUCCESS = fcsoftrasGetMirrorReconProgress(HDDSTS_PRI_MAS, Progress) Then
                            ' ڍ׏ݒ(i܂)
                            .Text = "Currently copying(DRIVE1->DRIVE2)" & Progress & "%"

                            ' i̎擾s
                        Else
                            ' ڍ׏ݒ
                            .Text = "Currently copying(DRIVE1->DRIVE2)"
                        End If

                        LedDrive1.BackColor = Color.Lime ' hCu1 LEDݒ()
                        LedDrive2.BackColor = Color.Orange ' hCu2 LEDݒ()
                        lblAllow.Text = ">>" ' č\zp\

                        ' XLbvR
                    Case HDDSTS_RECON_SKIP_1
                        .Text = "Skip recon(DRIVE1)"
                        LedDrive1.BackColor = Color.Lime ' hCu1 LEDݒ()
                        LedDrive2.BackColor = Color.Lime ' hCu2 LEDݒ()
                        lblAllow.Text = "" ' č\zp\

                        ' XLbvR
                    Case HDDSTS_RECON_SKIP_2
                        ' ڍ׏ݒ
                        .Text = "Skip recon(DRIVE2)"
                        LedDrive1.BackColor = Color.Lime ' hCu1 LEDݒ()
                        LedDrive2.BackColor = Color.Lime ' hCu2 LEDݒ()
                        lblAllow.Text = "" ' č\zp\

                        ' pg[[h
                    Case HDDSTS_PATROLRD
                        ' i̎擾
                        If FCSRASAPI_SUCCESS = fcsoftrasGetMirrorReconProgress(HDDSTS_PRI_MAS, Progress) Then
                            ' ڍ׏ݒ(i܂)
                            .Text = "Patrol reading " & Progress & "%"

                            ' i̎擾s
                        Else
                            ' ڍ׏ݒ
                            .Text = "Patrol reading"
                        End If
                        LedDrive1.BackColor = Color.Lime ' hCu1 LEDݒ()
                        LedDrive2.BackColor = Color.Lime ' hCu2 LEDݒ()
                        lblAllow.Text = ""                   ' č\zp\

                End Select
            End With
        Else
            ' ~[Xe[^X̎擾ŝŖ̏󋵂\
            lblSmartState.Text = "None mirror board"
            lblAllow.Text = ""
            LedDrive1.BackColor = Color.White
            LedDrive2.BackColor = Color.White
        End If
    End Sub
End Class