Function FlatFinder_Services(@Service, @Params)
/***********************************************************************************************************************

    This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without written
    permission from SRP Computer Solutions, Inc.

    Name        :   FlatFinder_Services

    Description :   Handler program for all module related services.

    Notes       :   The generic parameters should contain all the necessary information to process the services. Often
                    this will be information like the data Record and Key ID.

    Parameters  :
        Service         [in] -- Name of the service being requested
        Param1-10   [in/out] -- Additional request parameter holders
        Response       [out] -- Response to be sent back to the Controller (MCP) or requesting procedure

    Metadata    :

    History     :   (Date, Initials, Notes)
        08/29/17    dmb     Original programmer. - [EPIOI-8]
        03/29/18    dmb     Update logic to ignore preceeding 0s in RDS Key IDs. Update all WriteDataRow service calls
                            to ignore all locks.

***********************************************************************************************************************/

#pragma precomp SRP_PreCompiler

$insert LOGICAL
$insert SERVICE_SETUP
$insert RDS_EQUATES
$insert WO_LOG_EQUATES
$insert WM_OUT_EQUATES

Equ Tab$   to \09\
Equ CRLF$  to \0D0A\
Equ LF$    to \0A\
Equ Comma$ to ','

Declare subroutine  SRP_Stopwatch, Error_Services, FlatFinder_Services, Database_Services, Logging_Services
Declare function    SRP_Array, FlatFinder_Services, Database_Services, Environment_Services, Logging_Services

LogPath     = Environment_Services('GetApplicationRootPath') : '\LogFiles\FlatFinder'
LogDate     = Oconv(Date(), 'D4/')
LogTime     = Oconv(Time(), 'MTS')
LogFileName = LogDate[7, 4] : '-' : LogDate[1, 2] : '-' : LogDate[4, 2] : ' FlatFinder Import Log.csv'
Headers     = 'Logging DTM':@FM:'ToolID':@FM:'Operation':@FM:'Datetime':@FM:'LotID':@FM:'CassNo':@FM:'Wafer Count':@FM:'Import Result'
objLog      = Logging_Services('NewLog', LogPath, LogFileName, CRLF$, Comma$, Headers, '', False$, False$)
LoggingDTM  = LogDate : ' ' : LogTime   ; // Logging DTM

GoToService else
    Error_Services('Set', Service : ' is not a valid service request within the ' : ServiceModule : ' services module.')
end

Return Response else ''


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Services
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//----------------------------------------------------------------------------------------------------------------------
// ImportFlatFinderFiles
//
// Looks for available flatfinder CSV files that are ready to be imported into the RDS table.
//----------------------------------------------------------------------------------------------------------------------
Service ImportFlatFinderFiles()
    
    hSysLists   = Database_Services('GetTableHandle', 'SYSLISTS')
    Lock hSysLists, ServiceKeyID then
        DataPath    = Environment_Services('GetApplicationRootPath') : '\FlatFinder\Data\'
        BackupPath  = Environment_Services('GetApplicationRootPath') : '\FlatFinder\BackupFiles\'
        InitDir DataPath : '*.csv'
        FileList    = DirList()
        For Each File in FileList using @FM
            OSRead FlatFinderData from DataPath : File then
                BackupDate      = Oconv(Date(), 'D4/')
                BackupTime      = Oconv(Time(), 'MTS')
                Convert ':' to '-' in BackupTime
                BackupFileName  = BackupDate[7, 4] : '-' : BackupDate[1, 2] : '-' : BackupDate[4, 2] : '-' : BackupTime : ' ' : File
                If Count(File, '-') LE 1 then
                    OSWrite FlatFinderData to BackupPath : BackupFileName
                end
                Swap CRLF$ with '' in FlatFinderData
                Swap ',' with @FM in FlatFinderData
                ToolID         = Trim(FlatFinderData<2>)
                Operation      = Trim(FlatFinderData<3>)
                Datetime       = IConv(Trim(FlatFinderData<4>), 'DT')
                ReferenceKeyID = Trim(FlatFinderData<5>)
                CassetteNo     = Trim(FlatFinderData<6>)
                WaferCount     = Trim(FlatFinderData<7>)
                
                Result     = False$
                LogData    = LoggingDTM
                LogData<2> = ToolID
                LogData<3> = Operation
                LogData<4> = Trim(FlatFinderData<4>)
                LogData<5> = ReferenceKeyID
                LogData<6> = CassetteNo
                LogData<7> = WaferCount
                
                If ReferenceKeyID[1, 2] EQ '1T' then
                    Convert 'O' to '' in ReferenceKeyID
                    Swap '1T' with '' in ReferenceKeyID                      
                    If Index(ReferenceKeyID, '.', 1) then
                        // A dot means this is a WorkOrderNo.Cassette value. Need to create a WM_OUT Key ID.
                        WorkOrderNo = ReferenceKeyID[1, '.']
                        WOStepKeyID = ReferenceKeyID[Col2() + 1, '.']
                        CassetteNo  = ReferenceKeyID[Col2() + 1, '.']
                        WMOutKeyID  = WorkOrderNo : '*' : WOStepKeyID : '*' : CassetteNo
                        WMOutRow    = Database_Services('ReadDataRow', 'WM_OUT', WMOutKeyID)
                        If Error_Services('NoError') then
                            WMOutRow<WM_OUT_FLATFINDER_DTM$>            = DateTime
                            WMOutRow<WM_OUT_FLATFINDER_WAFER_CNT$>      = WaferCount
                            Database_Services('WriteDataRow', 'WM_OUT', WMOutKeyID, WMOutRow, True$, False$, True$)
                        end
                    end else
                        // No dot in the reference key means this is an RDS Key ID.
                        Transfer ReferenceKeyID to RDSKeyID
                        If Num(RDSKeyID) then RDSKeyID = RDSKeyID + 0
                        RDSRow      = Database_Services('ReadDataRow', 'RDS', RDSKeyID)
                        If Error_Services('NoError') then
                            RDSRow<RDS_FLATFINDER_DTM$>         = DateTime
                            RDSRow<RDS_FLATFINDER_WAFER_CNT$>   = WaferCount
                            Database_Services('WriteDataRow', 'RDS', RDSKeyID, RDSRow, True$, False$, True$)
                        end
                    end
                    If Error_Services('NoError') then
                        Result = 'Success'
                    end else
                        Result = 'Failure: ':Error_Services('GetMessage')
                    end
                end else
                    // Throw error - either invalid barcode scanned or user manually entered data
                    Result = 'Failure: ReferenceKeyID "':ReferenceKeyID:'" is invalid.'
                end
                
                LogData<8> = Result
                Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
                
                OSDelete DataPath : File
            end
        Next File

        Unlock hSysLists, ServiceKeyID else Null
    end

end service


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



