Attribute VB_Name = "basScale"
Option Explicit

'**********************************************************************************************************
' NAME:  Scale.BAS
' TITLE: General module file for OPOS applications.
' DESCR: This file defines some helper routines that are specific to the scale driver.
' NOTES: 1)
'
'**********************************************************************************************************

'Check box setting definitions:
Private Const BOX_UNCHECKED = 0
Private Const BOX_CHECKED = 1
Private Const BOX_GRAYED = 2


Sub LoadListBox(ListBoxName As String, devClass As String)

    ' Desc: This routine will load up the profile for all the device instances that represent a scale into
    ' our global collection from the registry.
    
    Dim i               As Integer
    Dim devInst         As String
    Dim devDefault      As String
    Dim curDevice       As Integer
    
    i = 0
    
    ' Get each registry key in this device class
    Do
        devInst = GetOPOSRegDevices(devClass, i)
        devDefault = GetOPOSRegValue(devClass, devInst, "", "")
        
        If (Len(devDefault) > 0 And Len(devInst) > 0) Then
            winMain.lstProfiles.AddItem (devInst)
        End If
        i = i + 1
    Loop While (Len(devInst) > 0)
    
    If (winMain.lstProfiles.ListCount > 0) Then
        winMain.lstProfiles.ListIndex = 0
    End If
    
End Sub
 
Public Function GetSerialPort(ByVal devName As String, Optional ByVal sDefaultValue As String = "COM1") As String
    
    'Gets the value of the serial port for the current scale
    'from the registry
    
    GetSerialPort = GetOPOSRegValue(devClass, devName, "SerialPort", sDefaultValue)
    
End Function

Public Function GetBaudRate(ByVal devName As String, Optional ByVal sDefaultValue As String = "9600") As String

    'Gets the value of the baud rate for the current scale
    'from the registry
    
    GetBaudRate = GetOPOSRegValue(devClass, devName, "BaudRate", sDefaultValue)
    
End Function

Public Function GetDataBits(ByVal devName As String, Optional ByVal sDefaultValue As String = "7") As String

    'Gets the value of the data bits for the current scale
    'from the registry
    
    GetDataBits = GetOPOSRegValue(devClass, devName, "DataBits", sDefaultValue)
    
End Function

Function GetParity(ByVal devName As String, Optional ByVal sDefaultValue As String = "Even") As String
    
    'Note: the default value of "Space" is the true equivalent of "None" as used in the scale.
    
    'Gets the value of the parity for the current scale
    'from the registry
    
    GetParity = GetOPOSRegValue(devClass, devName, "Parity", sDefaultValue)

End Function

Function GetServiceObject(ByVal devName As String, Optional ByVal sDefaultValue As String = "") As String
    
    'Gets the value of the service object for the current scale from the registry
    
    GetServiceObject = GetOPOSRegValue(devClass, devName, "ServiceObject", sDefaultValue)

End Function

Function GetServiceObjectVersion(ByVal devName As String, Optional ByVal sDefaultValue As String = "") As String
    
    'Gets the value of the service object version for the current scale from the registry
    
    GetServiceObjectVersion = GetOPOSRegValue(devClass, devName, "ServiceObjectVersion", sDefaultValue)

End Function

Function GetServiceObjectDescription(ByVal devName As String, Optional ByVal sDefaultValue As String = "") As String
    
    'Gets the value of the service object description for the current scale from the registry
    
    GetServiceObjectDescription = GetOPOSRegValue(devClass, devName, "ServiceObjectDescription", sDefaultValue)

End Function

Function GetAdminVersion(ByVal devName As String, Optional ByVal sDefaultValue As String = "") As String
    
    'Gets the value of the Admin program version for the current scale profile in the registry
    
    GetAdminVersion = GetOPOSRegValue(devClass, devName, "AdminVersion", sDefaultValue)

End Function

Function GetModel(ByVal devName As String, Optional ByVal sDefaultValue As String = "") As String
    
    'Gets the value of the model for the current scale from the registry
    
    GetModel = GetOPOSRegValue(devClass, devName, "ModelNumber", sDefaultValue)

End Function

Function GetDescription(ByVal devName As String, Optional ByVal sDefaultValue As String = "") As String
    
    'Gets the value of the description for the current scale from the registry
    
    GetDescription = GetOPOSRegValue(devClass, devName, "Description", sDefaultValue)

End Function

Function GetProgID(ByVal devName As String, Optional ByVal sDefaultValue As String = "") As String
    
    'Gets the value of the (default) i.e. the ProgID for the current scale
    'from the registry
    'Note: Following the convention used to access other values, you would think that we should pass
    '      the parameter as "(Default)" or maybe "Default", but it works by sending null string as
    '      the parameter.
    
    GetProgID = GetOPOSRegValue(devClass, devName, "", sDefaultValue)

End Function

Function GetInitialMaximumWeight(ByVal devName As String, Optional ByVal sDefaultValue As String = "0") As String
    
    'Gets the value of the initial MaximumWeight attribute for the current scale from the registry
    
    GetInitialMaximumWeight = GetOPOSRegValue(devClass, devName, "Initial_MaximumWeight", sDefaultValue)

End Function

Function GetInitialMaxDisplayTextChars(ByVal devName As String, Optional ByVal sDefaultValue As String = "0") As String
    
    'Gets the value of the initial MaxDisplayTextChars attribute for the current scale from the registry
    
    GetInitialMaxDisplayTextChars = GetOPOSRegValue(devClass, devName, "Initial_MaxDisplayTextChars", sDefaultValue)

End Function

Function GetInitialWeightUnits(ByVal devName As String, Optional ByVal sDefaultValue As String = "4") As String
    
    'Gets the "initial assumed" value of the WeightUnits attribute for the current scale from the registry
    'It will be a single character string: "1"=GM, "2"=KG, "3"=OZ, "4"=LB
    
    GetInitialWeightUnits = GetOPOSRegValue(devClass, devName, "Initial_WeightUnits", sDefaultValue)

End Function

Function GetInitialCapDisplay(ByVal devName As String, Optional ByVal sDefaultValue As String = "YES") As String
    
    'Gets the "initial assumed" value of the CapDisplay attribute for the current scale from the registry
    'It will be a boolean string: "YES" or "NO"
    
    GetInitialCapDisplay = GetOPOSRegValue(devClass, devName, "Initial_CapDisplay", sDefaultValue)

End Function

Function GetInitialCapDisplayText(ByVal devName As String, Optional ByVal sDefaultValue As String = "NO") As String
    
    'Gets the "initial assumed" value of the CapDisplayText attribute for the current scale from the registry
    'It will be a boolean string: "YES" or "NO"
    
    GetInitialCapDisplayText = GetOPOSRegValue(devClass, devName, "Initial_CapDisplayText", sDefaultValue)

End Function

Function GetInitialCapPriceCalculating(ByVal devName As String, Optional ByVal sDefaultValue As String = "NO") As String
    
    'Gets the "initial assumed" value of the CapPriceCalculating attribute for the current scale from the registry
    'It will be a boolean string: "YES" or "NO"
    
    GetInitialCapPriceCalculating = GetOPOSRegValue(devClass, devName, "Initial_CapPriceCalculating", sDefaultValue)

End Function

Function GetInitialCapTareWeight(ByVal devName As String, Optional ByVal sDefaultValue As String = "NO") As String
    
    'Gets the "initial assumed" value of the CapTareWeight attribute for the current scale from the registry
    'It will be a boolean string: "YES" or "NO"
    
    GetInitialCapTareWeight = GetOPOSRegValue(devClass, devName, "Initial_CapTareWeight", sDefaultValue)

End Function

Function GetInitialCapZeroScale(ByVal devName As String, Optional ByVal sDefaultValue As String = "YES") As String
    
    'Gets the "initial assumed" value of the CapZeroScale attribute for the current scale from the registry
    'It will be a boolean string: "YES" or "NO"
    
    GetInitialCapZeroScale = GetOPOSRegValue(devClass, devName, "Initial_CapZeroScale", sDefaultValue)

End Function

Function GetLogFileName(ByVal devName As String, Optional ByVal sDefaultValue As String = DEFAULT_LOG_FILE) As String

    'Gets the value of the log file for Weigh-Tronix devices
    'from the registry

    Dim szProviderKey   As String
    Dim szVendor        As String
    Dim valName         As String
    
    szProviderKey = REG_OPOSPROVIDER_KEY
    szVendor = "Weigh-Tronix"
    valName = "LogFile"
    
    GetLogFileName = GetOPOSRegValue(szProviderKey, szVendor, valName, DEFAULT_LOG_FILE)
    
End Function

Function GetVerbosity(ByVal devName As String, Optional ByVal sDefaultValue As String = "2") As String

    'Gets the value of the log file verbosity for the current scale
    'from the registry
    
    GetVerbosity = GetOPOSRegValue(devClass, devName, "LogVerbosity", sDefaultValue)
    
End Function

Public Function VerifySerialPort(ByVal sPort As String) As Integer

    'Verifies that the com port returned from the registry is valid.

    Dim iport As Integer
    
    VerifySerialPort = 0  '' Serial Port defaults to COM1
    
    If (InStr(UCase(sPort), "COM") > 0) Then
        iport = Mid(sPort, 4)
        If (Val(iport) >= 1 And Val(iport) <= 10) Then
            VerifySerialPort = Val(iport) - 1
        End If
    End If
    
End Function

Public Function VerifyBaudRate(sBaud) As Integer

    'Verifies that the serial port returned from the registry is valid

    Select Case (sBaud)
        Case "1200":
            VerifyBaudRate = 0
        Case "2400":
            VerifyBaudRate = 1
        Case "4800":
            VerifyBaudRate = 2
        Case "9600":
            VerifyBaudRate = 3
        Case "19200":
            VerifyBaudRate = 4
        Case Else
            VerifyBaudRate = 3          'baud rate defaults to 9600
    End Select
    
End Function

Public Function VerifyDataBits(ByVal sDataBits As String) As Integer

    Select Case (sDataBits)
        Case "7":
            VerifyDataBits = 0
        Case "8":
            VerifyDataBits = 1
        Case Else
            VerifyDataBits = 0          'databits defaults to 7
    End Select
    
End Function

Public Function VerifyParity(ByVal sParity As String) As Integer

    'Verifies that the parity returned from the registry is valid
    
    sParity = LCase(sParity)
    
    Select Case (sParity)
        Case "space":
            VerifyParity = 0
        Case "odd":
            VerifyParity = 1
        Case "even":
            VerifyParity = 2
        Case Else
            VerifyParity = 2            'parity defaults to even
    End Select
    
End Function

Public Function VerifyInitialWeightUnits(ByVal sInitUoM As String) As Integer

    'Verifies that the Initial_WeightUnits returned from the registry is valid and then returns a
    'numeric value that is used as an index into a list box.
     
    Select Case (sInitUoM)
        Case "Grams":
            VerifyInitialWeightUnits = 0            'GM = "1"
        Case "Kilograms":
            VerifyInitialWeightUnits = 1            'KG = "2"
        Case "Ounces":
            VerifyInitialWeightUnits = 2            'OZ = "3"
        Case "Pounds":
            VerifyInitialWeightUnits = 3            'LB = "4"
        Case Else
            VerifyInitialWeightUnits = 3            'Initial u-o-m defaults to pounds (ie "4")
    End Select
    
End Function

Public Function ConvertYesNo(ByVal sYesNo As String) As Long

    'Input: "YES" returns BOX_CHECKED
    '       "NO"  returns BOX_UNCHECKED
    
    If (sYesNo = "YES") Then
        ConvertYesNo = BOX_CHECKED
    Else
        ConvertYesNo = BOX_UNCHECKED
    End If
    
End Function

Function VerifyVerbosity(sz)
    
    'Verifies that the log file verbosity returned from the
    'registry is valid
    
    Dim iVerbosity

    iVerbosity = Val(Left(sz, 1))

    If iVerbosity >= 0 And iVerbosity < 5 Then
        VerifyVerbosity = iVerbosity
    End If

End Function
