Attribute VB_Name = "basMain"
Option Explicit

'**********************************************************************************************************
' NAME:  Main.BAS
' TITLE: Weigh-Tronix Main File
' DESCR: This is the main Visual Basic file for the Administration program and contains constants,
'        types and functions common to the application.
' NOTES: 1)
'
'**********************************************************************************************************

'----------------------------------------------------------------------------------------------------------
' Constants
'----------------------------------------------------------------------------------------------------------

Public Const CLASS_SCALE = "Scale"
Public Const PROGID_SCALE = "WTScaleSO.clsWTSCL"
Public Const DEFAULT_LOG_FILE = "c:\OposAdmin.log"

'----------------------------------------------------------------------------------------------------------
' Type Definitions
'----------------------------------------------------------------------------------------------------------

' Type:  CDeviceInstance
' Desc:  This type holds all the information that identifies a device instance.
Public Type CDeviceInstance
    szDevName As String
End Type

'----------------------------------------------------------------------------------------------------------
' Globals
'----------------------------------------------------------------------------------------------------------
' All our Device Instances are kept in a global array with global variables indicate the count, max
' count and an index into the array for the current Device Instance.

Public arrInstances() As CDeviceInstance    'Collection of scales for the Scale Configuration tab
Public sizInstances As Integer              'The total number of device instances we can support
Public devClass As String


Public Sub SetLogFilePath(Optional szValue As String)
    
    Dim ret
    Dim szProviderKey   As String
    Dim szVendor        As String
    Dim valName         As String
    
    szProviderKey = REG_OPOSPROVIDER_KEY
    szVendor = "Weigh-Tronix"
    valName = "LogFile"
    
    ret = ExistOPOSRegKey(szProviderKey, szVendor)
    
    If Not (Len(szValue) < 1 And ret <> ERROR_FILE_NOT_FOUND) Then
    
        If (ret = ERROR_FILE_NOT_FOUND) Then
            'The key path doesn't exist in the registry, so create it
            ret = CreateOPOSRegKey(szProviderKey, szVendor)
            If (ret <> ERROR_SUCCESS) Then
                MsgBox "Error trying to set the LogFile path."
                Exit Sub
            End If
        End If
        
        If (Len(szValue) < 1) Then
            'Set the value of the key to the default
            ret = SetOPOSRegValue(szProviderKey, szVendor, valName, DEFAULT_LOG_FILE, "")
        Else
            'Set the value of the key to the value passed in
            ret = SetOPOSRegValue(szProviderKey, szVendor, valName, szValue, "")
        End If
    End If
    
    'Display the key's value in the log file text box
    winMain.lblLogFile.Caption = GetOPOSRegValue(szProviderKey, szVendor, valName, DEFAULT_LOG_FILE)

End Sub

Public Function AddInstance(devName As String) As Boolean

    'Creates a new scale with a name input by the user
    'Returns TRUE on success.
    'Notes: 1)The conditions for a new instance default to a scale that has a display and may be zeroed
    '         from the application. These are basic capabilities of all Weigh-Tronix POS scales and can
    '         still be overriden by reading the attributes directly from the scale itself during when
    '         the device is enabled.
    '       2)The old PV name for the SO was "ScaleSO.DLL"
    '       3)The model number will be used to refer to the serial protocol that is required.
    '       4)The ServiceObjectVersion and ServiceObjectDescription are not necessarily known at
    '         the ti9me of an add instance. It cannot be determined until the object is opened and
    '         actually read from the object. The "TBD's" are just place holders.

    Dim i           As Integer
    Dim ret
    Dim devId       As String
    Dim devServiceObject  As String
    Dim devServiceObjectVersion As String
    Dim devServiceObjectDescription As String
    Dim devDesc     As String
    Dim devAdminVersion As String
    Dim devModelNum As String
    
    AddInstance = False
    
    'Check for valid names
    If (Len(devName) < 1) Then
        MsgBox "Invalid Device Name.", vbExclamation, "New Instance Error"
        Exit Function
    End If
    
    'Make sure that a log file path has been entered into the registry.
    'If not, create a key and enter the default path.
    SetLogFilePath
        
    'Instantiate a scale:
    devId = PROGID_SCALE
    devServiceObject = "WTScaleSO.DLL"                  '(see note 2)
    devServiceObjectVersion = "TBD (after Open)"        '(see note 4) temporary value
    devServiceObjectDescription = "TBD (after Open)"    '(see note 4) temporary value
    devDesc = "Weigh-Tronix Scale OPOS Device Profile, created on " & Now() & "."
    devAdminVersion = CStr(winMain.mlng_AdminVersion)
    devModelNum = "SCP-02"                              '(see note 3)
    
    'Need to check that device name is unique (case sensitivity applied), ' first in the Reg...
    If (ExistOPOSRegDevice(devClass, LCase(devName)) = 1) Then
        MsgBox "A Device Instance with that name already exists.", vbExclamation, "New Instance Error"
        Exit Function
    End If
    
    'Create the device instance key, and set its values.
    'Each device has its own properties in the registry.
    ret = CreateOPOSRegKey((devClass), LCase(devName))
    
    'If create is unsuccessful, give error message:
    If (ret <> ERROR_SUCCESS) Then
        MsgBox "Error creating new device instance [" & Str(ret) & "]"
        Exit Function
    End If
        
    'Load default values for the new profile name.
    ret = SetOPOSRegValue(devClass, devName, "", devId, "")
    ret = SetOPOSRegValue(devClass, devName, "ServiceObject", devServiceObject, "")
    ret = SetOPOSRegValue(devClass, devName, "ServiceObjectVersion", devServiceObjectVersion, "")
    ret = SetOPOSRegValue(devClass, devName, "ServiceObjectDescription", devServiceObjectDescription, "")
    
    ret = SetOPOSRegValue(devClass, devName, "AdminVersion", devAdminVersion, "")
    ret = SetOPOSRegValue(devClass, devName, "Description", devDesc, "")
   'ret = SetOPOSRegValue(devClass, devName, "LogVerbosity", "2", "Logging verbosity level")
    ret = SetOPOSRegValue(devClass, devName, "ModelNumber", devModelNum, "")
    ret = SetOPOSRegValue(devClass, devName, "SerialPort", "COM1", "")
    ret = SetOPOSRegValue(devClass, devName, "BaudRate", "9600", "")
    ret = SetOPOSRegValue(devClass, devName, "DataBits", "7", "")
    ret = SetOPOSRegValue(devClass, devName, "Parity", "Even", "")
    
    ret = SetOPOSRegValue(devClass, devName, "Initial_MaximumWeight", "0", "")
    ret = SetOPOSRegValue(devClass, devName, "Initial_WeightUnits", "4", "")
    ret = SetOPOSRegValue(devClass, devName, "Initial_CapDisplay", "YES", "")
    ret = SetOPOSRegValue(devClass, devName, "Initial_CapDisplayText", "NO", "")
    ret = SetOPOSRegValue(devClass, devName, "Initial_CapPriceCalculating", "NO", "")
    ret = SetOPOSRegValue(devClass, devName, "Initial_CapTareWeight", "NO", "")
    ret = SetOPOSRegValue(devClass, devName, "Initial_CapZeroScale", "YES", "")
    ret = SetOPOSRegValue(devClass, devName, "Initial_MaxDisplayTextChars", "0", "")
    
    'Add the new item to the collection of device instances.
    winMain.lstProfiles.AddItem devName
                              
    'Make the new device the current device.
    winMain.lstProfiles.Text = devName
    
    AddInstance = True
    
End Function

Public Function RemoveInstance() As Boolean

    Dim i, ndx As Integer
    Dim ret
    Dim devName As String
    
    'Removes the currently selected scale from the registry key and our drop down list box on the
    'Scale Profile tab.
    'Returns true on success.
    
    RemoveInstance = False
        
    devName = winMain.lstProfiles.Text
    
    If MsgBox("Are you sure you want to delete " & winMain.lstProfiles.Text & "?", vbDefaultButton2 + vbYesNo, "Delete Device Instance") = vbYes Then
        'Remove scale from the registry
        ret = DeleteOPOSRegKey(UCase(devClass), UCase(devName))
        If (ret <> ERROR_SUCCESS) Then
            MsgBox "Error trying to delete device " & devName & ".", vbExclamation, "Delete Error"
            Exit Function
        End If
        
        'Remove scale from the drop-down lists
        ndx = winMain.lstProfiles.ListIndex
        winMain.lstProfiles.RemoveItem ndx
        winMain.lstProfiles.ListIndex = winMain.lstProfiles.ListCount - 1
    End If
    
    RemoveInstance = True
    
End Function

Public Sub EnableAddControls(X As Boolean)

    'Enables the following controls when the form is loaded.
    
    winMain.lstProfiles.Enabled = X
    winMain.btnAdd.Enabled = X
    
End Sub

Public Sub EnableAddRemoveControls(X As Boolean)

    'Enables the following controls when a device has been selected from the lstProfiles drop-down
    'box or when a device is disabled by the user.
    'Disables these controls when a device is enabled by the user.

    winMain.lstProfiles.Enabled = X
    winMain.btnAdd.Enabled = X
    winMain.btnRemove.Enabled = X
   
End Sub

Public Sub EnableConfigAndRemoveControls(X As Boolean)

    'Enables the following controls when a device has been selected from the lstProfiles drop-down
    'box or a device is disabled by the user.
    'Disables these controls when a device is enabled by the user.

    winMain.lstSerialPort.Enabled = X
    winMain.lstBaudRate.Enabled = X
    winMain.lstDataBits.Enabled = X
    winMain.lstParity.Enabled = X
    'winMain.lstVerbosity.Enabled = X
    winMain.btnSetLogFile.Enabled = X
    winMain.lblLogFile.Enabled = X
    winMain.btnRemove.Enabled = X
    
End Sub

Public Sub EnableSetLogFile(X As Boolean)

    'Enables the SetLogFile button when a device has been select from the lstProfiles drop-down box,
    'or when a device is disabled by the user.
    'Disables the SetLogFile button when a device is disabled by the user.

    winMain.btnSetLogFile.Enabled = X

End Sub

