Module DUALSONIC
    Public UserInfo As UserIDType
    Public UserPermition As Integer
    Public RunMode As FORM_TYPE
    Public LOGIN_STATE As LOGIN_STATUS

    Public Structure UserIDType
        Public UserID As String
        Public UserPW As String
        Public IDType As String
        Public ExtPermition As String
    End Structure
    Public Enum USER_ID_TYPE
        ADMIN = 0
        TD_IQC
        TD_WRITE
        PRODUCT_AGING
        PRODUCT_WRITE
        PRODUCT_READ
        QC_READ_WRITE
        QC_SHIPMENT

        MAX_USER_ID_TYPE
    End Enum
    Public Enum USER_PERMITION_TYPE
        ADMIN = 0
        NORMAL
        MANAGER

        MAX_USER_PERMITION_TYPE
    End Enum
    Public Enum FORM_TYPE
        WRITE_MODE = 0
        READ_MODE
        READ_DETIL_MODE

        MAX_FORM_TYPE
    End Enum
    Public Structure PRODUCT_CONF
        Public CONF_MODEL As DS_MODEL
        Public CONF_FACE_RSHOT As Long
        Public CONF_FACE_TSHOT As Long
        Public CONF_EYE_RSHOT As Long
        Public CONF_EYE_TSHOT As Long
        Public CONF_BODY_RSHOT As Long
        Public CONF_BODY_TSHOT As Long
        Public CONF_ABLOAD As Integer
        Public CONF_AGENCY As Integer
    End Structure

    Public Structure PRODUCT_SN
        Public MODEL As DS_MODEL
        Public TYPE As DS_TYPE
        Public YEAR As String
        Public MONTH As String
        Public SN As String
        Public DATESN As String
    End Structure
    Public Enum DS_STATUS
        INIT = 0
        STANDBY
        INPUT
        CONNECT
        READY
        WIRTTING
        REWIRTTING
        COMPLETE
        MAX_DS_STATUS_NUM
    End Enum
    Public Enum LOGIN_STATUS
        INIT_PROGRAM = 0
        CHECK_IN_FAIL
        CHECK_IN_PROGRAM
        ENTER_PROGRAM
        CHECK_IN_SETUP_FAIL
        CHECK_IN_SETUP
        ENTER_SETUP
        MAX_LOGIN_STATUS_NUM
    End Enum
    Public Enum DS_MODEL
        DS_MODEL_NONE = 0
        DS_PRO = 1
        DS_LUX
        DS_ALPHA = 4
        MAX_DS_MODEL_NUM
    End Enum
    Public Enum DS_TYPE
        DS_DEV = 0
        DS_FACE
        DS_EYE
        DS_BODY
        MAX_DS_TYPE_NUM
        DS_TYPE_NONE = 9
    End Enum
    Public Enum DS_PRODUCT
        DS_MODEL_NONE = 0
        DS_PRO_DEV = 10
        DS_PRO_FACE
        DS_PRO_EYE
        DS_PRO_BODY
        DS_LUX_DEV = 20
        DS_LUX_FACE
        DS_LUX_EYE
        DS_LUX_BODY
        DS_ALPHA_DEV = 40
        DS_ALPHA_FACE
        DS_ALPHA_EYE
        DS_ALPHA_BODY
        MAX_DS_PRODUCT_NUM
    End Enum
    Public Enum DIFF_MATCH
        NOT_DIFFERENT = 0
        MODEL_DIFFERENT
        SN_DIFFERENT
        DATE_DIFFERENT

        MAX_DIFF_MATCH_NUM
    End Enum

    Public STATUS As DS_STATUS
    Public Conf_DS_Set As PRODUCT_CONF
    Public DIFF_MATCH_FLAG(DIFF_MATCH.MAX_DIFF_MATCH_NUM) As Boolean
    Public DS_SN(DS_TYPE.MAX_DS_TYPE_NUM) As PRODUCT_SN
    Public LOAD_FLAG(DS_TYPE.MAX_DS_TYPE_NUM) As Boolean
    Public WR_COMPLETE_FLAG(DS_TYPE.MAX_DS_TYPE_NUM) As Boolean
    Public ConnectStatus(DS_TYPE.MAX_DS_TYPE_NUM) As Boolean
    Public EnableBarCodeTxbToggle(DS_TYPE.MAX_DS_TYPE_NUM) As Boolean
    Public ReTryDisable As Boolean = False ''재실행 여부 확인 변수

    ''정보 쓰기 후 수신 값 동일 여부 확인 변수 
    Public ReceiveComplete As Boolean       ''모든 정보쓰기 데이터 수신이 완료 되었는지 확인(True:수신, False:미수신)
    Public DiffCompComplete As Boolean       ''모든 정보쓰기 성공여부 확인(True:성공, False:실패)
    Public DBuploadComplete As Boolean       ''모든 정보쓰기 DB 업로드 성공여부 확인(True:성공, False:실패)
    Public DBAssyuploadComplete As Boolean       ''모든 정보쓰기 DB 업로드 성공여부 확인(True:성공, False:실패)
    Public ReceiveReturnVal(DS_TYPE.MAX_DS_TYPE_NUM) As Boolean       ''정보쓰기 리턴값이 수신이 되었는지 확인(True:수신됨, False:수신안됨)
    Public DiffCompReturnVal(DS_TYPE.MAX_DS_TYPE_NUM) As Boolean    ''송신 데이터와 수신데이터가 같은지 확인(True:일치, False:불일치)
    Public UploadDBComplete(DS_TYPE.MAX_DS_TYPE_NUM) As Boolean     ''DB 업로드가 정상적으로 완료되었는지 확인(True:성공, False:실패)
    Public ReturnValue(DS_TYPE.MAX_DS_TYPE_NUM, 9) As String        ''정보쓰기 수신데이터
    Public SendValue(DS_TYPE.MAX_DS_TYPE_NUM, 9) As String          ''정보쓰기 송신데이터

    Public Function GetStatus() As DS_STATUS
        Return STATUS
    End Function
    Public Sub SetStatus(Val As DS_STATUS)
        STATUS = Val
    End Sub
    Public Function GetLoginStatus() As LOGIN_STATUS
        Return LOGIN_STATE
    End Function
    Public Sub SetLoginStatus(Val As LOGIN_STATUS)
        LOGIN_STATE = Val
    End Sub
    Public Sub Reset_All_DS_Reg()
        For cnt = DS_TYPE.DS_DEV To DS_TYPE.DS_BODY
            Reset_DS_Reg(cnt)
        Next
    End Sub
    Public Sub Reset_DS_Reg(cnt As DS_TYPE)
        With DS_SN(cnt)
            .MODEL = DS_MODEL.DS_MODEL_NONE
            .TYPE = DS_TYPE.DS_TYPE_NONE
            .YEAR = ""
            .MONTH = ""
            .SN = ""
            .DATESN = ""
        End With

        LOAD_FLAG(cnt) = False
        EnableBarCodeTxbToggle(cnt) = False
        ReceiveReturnVal(cnt) = False
        DiffCompReturnVal(cnt) = False
        UploadDBComplete(cnt) = False
        ReTryDisable = False

        For i = 0 To 9
            ReturnValue(cnt, i) = ""
            SendValue(cnt, i) = ""
        Next
    End Sub
    Public Sub Reset_Return_Reg(cnt As DS_TYPE)
        ReceiveReturnVal(cnt) = False
        DiffCompReturnVal(cnt) = False
        UploadDBComplete(cnt) = False
        ReTryDisable = False

        For i = 0 To 9
            ReturnValue(cnt, i) = ""
            SendValue(cnt, i) = ""
        Next
    End Sub
    Public Sub Reset_All_Connect()
        For cnt = DS_TYPE.DS_DEV To DS_TYPE.DS_BODY
            ConnectStatus(cnt) = False
        Next
    End Sub
    Public Function Run_ReadBarcodeParsing(StrBarcord As String) As String
        Dim tmpModel As DS_MODEL
        Dim tmpType As DS_TYPE
        Dim tmpYear, tmpMonth, tmpSN_HIGH, tmpSN_LOW, tmpSN, tmpDateSN As String
        Dim tmpModelType As Integer
        Dim OverlapFlag As Boolean = False
        Dim DiffFlag As Boolean = False

        tmpModelType = Val(Mid(StrBarcord, 1, 2))
        tmpModel = Val(Mid(StrBarcord, 1, 1))
        tmpType = Val(Mid(StrBarcord, 2, 1))
        tmpYear = Mid(StrBarcord, 3, 2)
        tmpMonth = Mid(StrBarcord, 5, 2)
        tmpSN_LOW = Mid(StrBarcord, 7, 3)
        tmpSN_HIGH = Mid(StrBarcord, 10, 3)
        tmpSN = tmpSN_HIGH & tmpSN_LOW
        tmpDateSN = Mid(StrBarcord, 3, 10)



        ''모델 유효성 체크
        If tmpModel <> Conf_DS_Set.CONF_MODEL Then
            DIFF_MATCH_FLAG(DIFF_MATCH.MODEL_DIFFERENT) = True
            DiffFlag = True
        End If

        Return tmpType
    End Function
    Public Function RunSN_Parsing(StrBarcord As String) As String
        Dim tmpModel As DS_MODEL
        Dim tmpType As DS_TYPE
        Dim tmpYear, tmpMonth, tmpSN_HIGH, tmpSN_LOW, tmpSN, tmpDateSN As String
        Dim tmpModelType As Integer
        Dim OverlapFlag As Boolean = False
        Dim DiffFlag As Boolean = False

        tmpModelType = Val(Mid(StrBarcord, 1, 2))
        tmpModel = Val(Mid(StrBarcord, 1, 1))
        tmpType = Val(Mid(StrBarcord, 2, 1))
        tmpYear = Mid(StrBarcord, 3, 2)
        tmpMonth = Mid(StrBarcord, 5, 2)
        tmpSN_LOW = Mid(StrBarcord, 7, 3)
        tmpSN_HIGH = Mid(StrBarcord, 10, 3)
        tmpSN = tmpSN_HIGH & tmpSN_LOW
        tmpDateSN = Mid(StrBarcord, 3, 10)

        ''활성화 기기 유효성 체크
        If MainForm.chkDeviceUse(tmpType).Checked = False Then Return (tmpType & "," & "DISABLE")
        If ConnectStatus(tmpType) And MainForm.txbInfo_Barcord(tmpType).BackColor <> Color.Red Then Return (tmpType & "," & "INSERT_DIVICE")

        ''모델 유효성 체크
        If tmpModel <> Conf_DS_Set.CONF_MODEL Then
            DIFF_MATCH_FLAG(DIFF_MATCH.MODEL_DIFFERENT) = True
            DiffFlag = True
        End If

        LOAD_FLAG(tmpType) = False
        DS_SN(tmpType).MODEL = DS_MODEL.DS_MODEL_NONE
        DS_SN(tmpType).TYPE = DS_TYPE.DS_TYPE_NONE
        DS_SN(tmpType).YEAR = ""
        DS_SN(tmpType).MONTH = ""
        DS_SN(tmpType).SN = ""
        DS_SN(tmpType).DATESN = ""

        For cnt = DS_TYPE.DS_DEV To DS_TYPE.DS_BODY
            If LOAD_FLAG(cnt) = True Then
                OverlapFlag = True
            End If
        Next

        ''현재 입력된 모델 유효성 체크
        If tmpModel <> Conf_DS_Set.CONF_MODEL Then
            DIFF_MATCH_FLAG(DIFF_MATCH.MODEL_DIFFERENT) = True
            DiffFlag = True
        End If

        ''활성화된 입력값 유효성 체크
        For cnt = DS_TYPE.DS_DEV To DS_TYPE.DS_BODY

            ''MODEL 체크
            If DS_SN(cnt).MODEL <> DS_MODEL.DS_MODEL_NONE Then
                If DS_SN(cnt).MODEL <> Conf_DS_Set.CONF_MODEL Then
                    DIFF_MATCH_FLAG(DIFF_MATCH.MODEL_DIFFERENT) = True
                    DiffFlag = True
                End If
            End If

            ''SN 체크
            If DS_SN(cnt).SN <> "" Then
                If DS_SN(cnt).SN <> tmpSN And OverlapFlag Then
                    DIFF_MATCH_FLAG(DIFF_MATCH.SN_DIFFERENT) = True
                    DiffFlag = True
                End If
            End If

            ''제조일자 체크
            If DS_SN(cnt).DATESN <> "" Then
                If (DS_SN(cnt).YEAR <> tmpYear Or DS_SN(cnt).MONTH <> tmpMonth) And OverlapFlag Then
                    DIFF_MATCH_FLAG(DIFF_MATCH.DATE_DIFFERENT) = True
                    DiffFlag = True
                End If
            End If

        Next

        If MainForm.txbInfo_Barcord(tmpType).BackColor = Color.Red And MainForm.txbInfo_Barcord(tmpType).Text = "N/A" Then
            Return (tmpType & "," & "INSERT_DIVICE")
        End If

        DS_SN(tmpType).MODEL = tmpModel
        DS_SN(tmpType).TYPE = tmpType
        DS_SN(tmpType).YEAR = tmpYear
        DS_SN(tmpType).MONTH = tmpMonth
        DS_SN(tmpType).SN = tmpSN
        DS_SN(tmpType).DATESN = tmpDateSN

        LOAD_FLAG(tmpType) = True
        'LOAD_OKNG_FLAG(tmpType) = True

        If DiffFlag = True Then
            LOAD_FLAG(tmpType) = False
            Return (tmpType & "," & "DIFFERENT")
        End If

        Return tmpType
    End Function
    Public Function IsComplete() As String
        Dim CheckFlag As Boolean = True
        Dim CompModel As String = ""
        Dim CompSN As String = ""
        Dim CompDATESN As String = ""

        Try
            ''활성화된 기기 타입에 모두 로딩이 되었는지 확인
            For cnt = DS_TYPE.DS_DEV To DS_TYPE.DS_BODY
                If MainForm.chkDeviceUse(cnt).Checked = True And LOAD_FLAG(cnt) = False Then
                    CheckFlag = False
                    Exit For
                End If
            Next

            '' If CheckFlag = True Then SetStatus(DS_STATUS.CONNECT)

            ''활성화된 기기 연결이 되었는지 확인
            For cnt = DS_TYPE.DS_DEV To DS_TYPE.DS_BODY
                If MainForm.chkDeviceUse(cnt).Checked = True And ConnectStatus(cnt) = False Then
                    CheckFlag = False
                    Exit For
                End If
            Next

            ''기기 시리얼번호가 같은지 확인
            For cnt = DS_TYPE.DS_DEV To DS_TYPE.DS_BODY
                If MainForm.chkDeviceUse(cnt).Checked = True Then
                    If CompModel = "" Then
                        CompModel = DS_SN(cnt).MODEL
                    Else
                        If CompModel <> DS_SN(cnt).MODEL Then CheckFlag = False
                    End If

                    If CompSN = "" Then
                        CompSN = DS_SN(cnt).SN
                    Else
                        If CompSN <> DS_SN(cnt).SN Then CheckFlag = False
                    End If

                    If CompDATESN = "" Then
                        CompDATESN = DS_SN(cnt).DATESN
                    Else
                        If CompDATESN <> DS_SN(cnt).DATESN Then CheckFlag = False
                    End If
                End If
            Next

            If CheckFlag = True And GetStatus() <= DS_STATUS.READY Then
                SetStatus(DS_STATUS.READY)
                Return "COMPLETE"
            Else
                Return "NOT COMPLETE"
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
            Return "NOT COMPLETE"
        End Try

    End Function
End Module
