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

    Name        :   GaN_Services

    Description :   Handler program for all RDS services.

    Notes       :   Application errors should be logged using the Error Services module. There are a few methodological
                    assumptions built into way errors are managed which are important to understand in order to properly
                    work with Error Services:

                    - The term 'top' refers to the originating procedure of a call stack and the term 'bottom' refers to
                      the last routine (or the current routine) within a call stack. Within the OpenInsight Debugger
                      this will appear backwards since the originating procedure always appears at the bottom of the
                      list and the current routine appears at the top of the list. We are using this orientation because
                      it is common to refer to the process of calling other procedures as 'drilling down'.

                    - The reason for defining the orientation of the call stack is because Error_Services allows for
                      multiple error conditions to be appended to an original error. In most cases this will happen when
                      a procedure at the bottom of the stack generates an error condition and then returns to its
                      calling procedure. This higher level procedure can optionally add more information relevant to
                      itself. This continues as the call stack 'bubbles' its way back to the top to where the
                      originating procedure is waiting.

                    - Native OpenInsight commands that handle errors (e.g., Set_Status, Set_FSError, Set_EventStatus)
                      preserve their error state until explicitly cleared. This can hinder the normal execution of code
                      since subsequent procedures (usually SSPs) will fail if a pre-existing error condition exists.
                      Our philosophy is that error conditions should automatically be cleared before a new procedure
                      is executed to avoid this problem. However, the nature of Basic+ does not make this easy to
                      automate for any given stored procedure. Therefore, if a stored procedure wants to conform to our
                      philosophy then it should include a call into the 'Clear' service request at the top of the
                      program. Alternatively this can be done through a common insert (see SERVICE_SETUP for example.)

                    - Service modules will use the SERVICE_SETUP insert and therefore automatically clear out any
                      error conditions that were set before.

    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)
        01/11/19    djs     Original programmer.

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler

$Insert MSG_EQUATES
$insert RTI_DEBUG_COMMON
$insert SRPMail_Inserts
$insert LOGICAL
$insert SERVICE_SETUP
$insert RDS_EQUATES
$insert PROD_SPEC_EQUATES
$insert RDS_LAYER_EQUATES
$insert PRS_LAYER_EQUATES
$insert RDS_TEST_EQUATES
$insert TOOL_CLASS_EQUATES
$insert TEST_POINT_MAP_EQUATES
$insert CLEAN_INSP_EQUATES
$insert REACT_RUN_EQUATES
$insert REACTOR_EQUATES
$insert WO_MAT_QA_EQUATES
$insert PRS_STAGE_EQUATES
$insert WO_LOG_EQUATES
$insert WM_OUT_EQUATES
$insert REACT_UTIL_EQU
$insert WO_MAT_EQUATES
$insert RLIST_EQUATES
$insert REACT_MODE_EQUATES
$insert LSL_USERS_EQUATES
$insert TOOL_EQUATES

AutoDisplayErrors   = False$     ; // Set this to True$ when debugging so all errors will automatically display.

Declare subroutine  SRP_Stopwatch, Error_Services, obj_Tables, Metrology_Services, obj_RDS_Test, SRP_JSON
Declare subroutine  RTI_Set_Debugger, Database_Services, Btree.Extract, Extract_SI_Keys, Obj_WO_Mat, Obj_WO_Mat_Log
Declare subroutine  Logging_Services, Set_Status
Declare function    SRP_Sort_Array, Metrology_Services, RenameFile, obj_RDS_Test, obj_Test_Point_Map, Database_Services
Declare function    Work_Order_Services, SRP_JSON, Logging_Services, Environment_Services, SRP_Trim, Error_Services
Declare function    SRPSendMail, Memberof, Obj_WO_Mat, Obj_WO_Mat_Log, SQL_Services

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

Return Response else ''


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Service Parameter Options
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Options BOOLEAN     = True$, False$


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

//----------------------------------------------------------------------------------------------------------------------
// GetRunInfo
//
// Input:
//      GaNRunID - [Required]
//
// Output:
//      Returns the following information from the Wafer Track database:
//          - GaN Recipe
//          - GaN Susc Serial Number
//          - Pocket Numbers
//          - Wafer Scribes
//          - Sattelite Serial Numbers
//          - Reactor Number
// Output Format:
//      Rows are @FM delimited, Columns are @VM delimited
//
// Note:
//      This service cannot run directly on the EC domain due to the fact that the Wafer Track database
//      is outside of the EC domain. This service will run on the App server. If this information is needed
//      on the DB server, then a queue service would need to be developed in order to faciliate the process.
//
//----------------------------------------------------------------------------------------------------------------------
Service GetRunInfo(GaNRunID)
    
    Response     = ''
    WaferTrackDB = Environment_Services('GetWaferTrackProductionPath')
    Query = 'Declare @RunNumber   varchar(50) ' |
          : "Set @RunNumber      = '":GaNRunID:"' " |
          : 'SELECT ' |
          :     '[Recipe] ' |
          :     ',[Platter S/N] ' |
          :     ',[Pocket_Number] ' |
          :     ',[Wafer_Lot] ' |
          :     ',[Satellite S/N] ' |
          : 'FROM [G4Wafers_01].[dbo].[Prerun Info] ' |
          : 'WHERE [Run Number] =  @RunNumber ' |
          : 'ORDER BY [Pocket_Number]'
          
    GaNRunData = SQL_Services('GetDataRows', WaferTrackDB, Query, 10)
    
    If GaNRunData NE '' then
        Convert @FM to @VM in GaNRunData
        Convert @RM to @FM in GaNRunData

        For each Row in GaNRunData using @FM setting fPos
          Temp       = Row<0,2>
          Reactor    = Field(Temp, ' ', 1)
          // Trim the 'R' in front of Reactor number
          Reactor[1, 1] = ''
          PlatterSN  = Field(Temp, ' ', 2)
          GaNRunData<fPos, 2> = PlatterSN
          GaNRunData<fPos, 6> = Reactor
        Next Row
        
        Response = GaNRunData
    end

end service


//----------------------------------------------------------------------------------------------------------------------
// GetToolList
//
// Output:
//      Returns a @FM delimited array of GaN related tools.
//
//----------------------------------------------------------------------------------------------------------------------
Service GetToolList
    
    GaNToolQuery = "SELECT TOOL WITH TOOL_TYPE EQ 'G5' OR WITH TOOL_TYPE EQ 'G5+' OR WITH TOOL_TYPE EQ 'char' " |
                 : "OR WITH TOOL_TYPE EQ 'GaN' OR WITH TOOL_TYPE EQ 'Transfer'"
    Set_Status(0)
    RList(GaNToolQuery, TARGET_SAVELIST$, 'GAN_TOOL_LIST', 'Current List of GaN Tools', False$)
    GaNToolList = Database_Services('ReadDataRow', 'SYSLISTS', 'GAN_TOOL_LIST')
    // Remove field containing list description
    GaNToolList = Delete(GaNToolList, 1, 0, 0)
    errCode = ''
    If Get_Status(errCode) then
        ErrorMsg = 'Error retrieving GaN tool list from the ':Service:' module. Error code':errCode
        Error_Services('Add', ErrorMsg)       
    end 
    Response  = GaNToolList
    
end service


//----------------------------------------------------------------------------------------------------------------------
// GetReactor
//
//
//----------------------------------------------------------------------------------------------------------------------
Service GetReactor(WorkOrderNo)
    
    If (WorkOrderNo NE '') then
        If RowExists('WO_LOG', WorkOrderNo) then
            Reactor = ''
            PVDesc = Xlate('WO_LOG', WorkOrderNo, 'PROD_VER_DESC', 'X')
            Begin Case
                Case Indexc(PVDesc, 'G5+', 1)
                    Reactor = 69
                Case Indexc(PVDesc, 'G5', 1)
                    Reactor = 71
                Case Otherwise$
                    Error_Services('Add', 'Error in ':Service:'. PROD_VER_DESC does not match any GaN reactors.')
            End Case
        end else
            Error_Services('Add', 'Error in ':Service:'. WO_LOG record ':WorkOrderNo:' does not exist.')
        end
    end
    Response = Reactor
    
end service


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


