Option Strict Off
Option Explicit On 
Module Variables
    '***********************************************
    ' variables for maximum # of rows for data grids
    '***********************************************
    Public ShoreMaxRowNumber As Integer
    Public ScienceMaxRowNumber As Integer
    Public SEMaxRowNumber As Integer

    '**********************************************
    ' working variables for load management
    '**********************************************
    Public TotalAvailablePower As Double

    '***********************************************
    'tc: global integer to show number of new messages
    '*************************************************
    Public MessageCount As Integer
    Public MessageBottom As Boolean     'global flag to indicate whether or not to scroll to bottom

    'tc: global flags to show whether it's the first same type of events on the message box
    Public FirstEventFlagNPC As Boolean
    Public FirstEventFlagNPCState As Boolean
    Public FirstEventFlagExtLoad As Boolean
    Public FirstEventFlagIntLoad As Boolean
    Public FirstEventFlagBus As Boolean
    Public FirstEventFlagSensor As Boolean
    Public FirstEventFlagMV As Boolean
    Public FirstEventFlagShore As Boolean
    Public FirstEventFlagGF400 As Boolean
    Public FirstEventFlagGF48 As Boolean
    Public FirstEventFlagGFCom As Boolean
    Public FirstEventFlagGFUnknown As Boolean
    Public FirstEventFlagGF As Boolean

    'global flag to show whether fault location is running
    Public RunFaultLocation As Boolean


    Public wantPauseLoadDataThread As Boolean 'tc new: if true it tells the LoadDataworker thread to not 
    '                                          do anything for a while.

    Public wantPauseGfCheckThread As Boolean 'jbe new: If true it tells the GFworker thread to not
    '                                         do anything for a while.  Useful when the user wants to examine
    '                                         something of concern and
    '                                         wants to temporarily stop the Ground Fault testing.

    'jbe: Global integer which when non-zero indicates that one of the threads is executing a critical section of code.  
    '     Only one critical section of code should be allowed to run at any time.  This was needed for thread 
    '     synchronization to prevent errors such as the big red 'X' in the datagrids and incomplete repaint 
    '     of controls on the forms.
    Public lockCriticalCodeCount As Integer 'jbe new 04-06-2005 

    '***************************************************************************************************************
    '*** jbe: START OF GLOBAL VARIABLES RELATED TO GROUND FAULT CHECK CHILD THREAD HAND SHAKING ********************


    'jbe: Global flag for use with the child thread that does the ground fault checking.
    '     When this flag is true the Ground Fault timer event handler routine will set this
    '     set flag wantGFchildThreadToInvokeSoapMethod true (see below) and then also set 
    '     wantToInvokeSoapGroundFaultCheckMethod to false (to prevent the SOAP method from being
    '     re-invoked before the return values have been processed by the program).  This is
    '     a sort of 'hand-shaking' process to communicate between the parent thread and the child
    '     thread which was added in an attempt to eliminate possible errors due to thread conflicts.
    Public wantToInvokeSoapGroundFaultCheckMethod As Boolean  'jbe new 04-06-2005 

    'jbe: New 04-06-2005. Second global flag for use with the child thread that does the ground fault checking.
    '     When this flag is false it means the parent thread wants the child thread 
    '     to just wait in an idle loop until the parent thread needs it to do something.  
    '     When this flag is true the parent thread wants the child thread to invoke the SOAP Ground
    '     Fault Check method and the parent thread will set other global variables to tell the child
    '     what input parameters to use.  
    Public wantGFchildThreadToInvokeSoapMethod As Boolean  'jbe new 04-06-2005 

    Public globalWantUpdateGfTestSwitchGraphics As Boolean  'jbe new 04-06-2005 

    'jbe: Global flag which if true means the Ground Fault child thread has gotten
    '     return values from the SOAP method to do the Ground Fault check.
    Public GroundFaultChildThreadHasGotNewValues As Boolean  'jbe new 04-06-2005 

    Public globalHaveCalledGroundFaultTimerTickOnce As Boolean  'jbe new 04-06-2005 

    ' jbe: Global counter from 1 to 5 to indcate which of the 5 busses is being
    '      tested next for Ground Fault current.  Used to keep track of which
    '      voltage bus to get ground fault current measuremenst 
    '      next from the SOAP method from within the child worker thread.      
    Public globalGFbusSoapRotateCounter As Integer 'jbe new 04-06-2005  

    'jbe: Global to store the previous value of globalGFbusSoapRotateCounter.
    Public globalLastGFbusSoapRotateCounter As Integer 'jbe new 04-06-2005  

    'jbe: Global to store most recent Ground Fault current measurement.
    Public globalGFCurrent As Integer 'jbe new 04-06-2005  

    '*** jbe: END OF GLOBAL VARIABLES RELATED TO GROUND FAULT CHECK CHILD THREAD HAND SHAKING ********************
    '*************************************************************************************************************

    '*************************************************************************************************************
    '*** jbe: START OF GLOBAL VARIABLES RELATED TO LOAD DATA CHILD THREAD HAND SHAKING ***************************

    'jbe: Global flag which if true indicates that the timer event handler routine that
    '     handshakes with the child thread to get SOAP data has not been called
    '     even once.  I am doing it this way to make sure that the child thread
    '     does not try to get any SOAP data until the timer event handler routine
    '     TimerLoadData_Tick() has been called before the child process does anything.
    Public globalHaveCalledTimerLoadDataTick As Boolean    'jbe new 04-06-2005 

    'jbe: New 04-06-2005. Global flag for use with the child thread that does the load data SOAP methods.
    '     When this flag is false it means the parent thread wants the child thread 
    '     to just wait in an idle loop until the parent thread requests the child to do something.  
    '     When this flag is true the parent thread wants the child thread to invoke a SOAP method and
    '     the parent thread will set other global variables to tell the child what SOAP method to 
    '     invoke and what input paramters to use.
    Public wantToInvokeSoapLoadDataMethod As Boolean    'jbe new 04-06-2005

    'jbe: Global flag to indicate that the child thread that gets general SOAP data has
    '     received new values after waiting for the call to one of the SOAP methods. 
    Public ChildThreadHasGotNewValues As Boolean    'jbe new 04-06-2005

    'tc: Global flag to indicate that the child thread that gets Science Node SOAP data has
    '    received new values after waiting for the call to one of the SOAP methods. 
    Public ChildThreadHasGotNewScienceValues As Boolean    'tc new 05-25-05

    'tc: Global flag to indicate that the child thread that gets Shore Station SOAP data has
    '    received new values after waiting for the call to one of the SOAP methods. 
    Public ChildThreadHasGotNewShoreValues As Boolean    'tc new 05-25-05


    '*************************************************************************************************
    '*************************************************************************************************
    '*************************************************************************************************
    'tc: Global flag to indicate the state of the NPC
    'New 06-21-2005
    'indicates the NPC change type
    Public NPCChangeType As Integer

    'indicates whether the NPC has change
    Public NPCChange As Boolean

    'indicates whether the NPC is On or Off
    Public NPCOldState As Integer
    Public NPCState As Integer

    'Indicates whether server can communicate with NPC
    Public globalNPCOldStatus As Boolean
    Public globalNPCStatus As Boolean

    'Global flag to indicate whether each of the SOAP method gets values
    'tc: new 05-26-2005
    Public GotExtLoadData As Boolean
    Public GotIntLoadData As Boolean
    Public GotBusData As Boolean
    Public GotSensorData As Boolean
    Public GotMVData As Boolean
    Public GotShoreData As Boolean
    Public GotGFData As Boolean

    'jbe: New 04-06-2005. Global flag for use with the child thread that does the Ground Fault checking stuff.
    '     When this flag is false it means the parent thread wants the child thread 
    '     to just wait in an idle loop until the parent thread needs it to do something.  
    '     When this flag is true the parent thread wants the child thread to invoke a SOAP method and.
    '     the parent thread will set other global variables to telling the child what SOAP method to 
    '     invoke and what input parameters to use.
    Public wantToInvokeSoapGfMeasureMethod As Boolean  'jbe new 04-06-2005

    ' jbe: Global count which rotates from 1 to 5.  Used to keep track of what SOAP data type set to get
    '      next from the SOAP method from within the child worker thread.  Allowed values are as follows.
    '      1 means we want to get external load data, 
    '      2 means we want to get internal load data, 
    '      3 means we want get bus data
    '      4 means we want engineering sensors data
    '      5 means we want MV converter data
    '      6 means we want shore power supply data  'tc
    '      Note that ground fault data is handled by a different child thread so is
    '      not in the above list presently.
    Public globalRequestSoapRotateCount As Integer 'jbe new 04-06-2005

    'jbe: Global integer to save the last value of globalRequestSoapRotateCount
    '     for which data was successfully returned to the child thread.
    '     Usually this will just be one less than globalRequestSoapRotateCount 
    '     with wrap around, but we will explicitly use a separate variable to 
    '     make the code more obvious.
    Public globalLastReqestSoapCount As Integer 'jbe new 04-06-2005

    'jbe: Structure of type loadListType, for storing external load values in a child thread. 
    '     Global structure to tempoarily hold the return load values from the SOAP getLoads()
    '     method when called with argument "/1/external".  These values will later
    '     be transferred to local variables by routine TimerLoadData_Tick().
    '     Used to temporarily hold the data in the child before transferring it to the parent thread.
    Public globalExtLoadListType As edu.washington.apl.pmacs.loadListType 'jbe new 04-06-2005

    'jbe: Structure of type loadListType, for storing external load values in a child thread. 
    '     Global structure to tempoarily hold the return load values from the SOAP getLoads()
    '     method when called with argument "/1/internal".  These values will later
    '     be transferred to local variables by routine TimerLoadData_Tick().
    '     Used to temporarily hold the data in the child before transferring it to the parent thread.
    Public globalIntLoadListType As edu.washington.apl.pmacs.loadListType 'jbe new 04-06-2005

    'jbe: Structure of type busListType for storing the bus data.   
    '     Used to temporarily hold the data in the child before transferring it to the parent thread.
    Public globalBusListType As edu.washington.apl.pmacs.busListType   'jbe new 04-06-2005    

    'jbe: Structure of type globalSensorListType for storing the Sensor data.   
    '     Used to temporarily hold the data in the child before transferring it to the parent thread.
    Public globalSensorListType As edu.washington.apl.pmacs.sensorListType 'jbe new 04-06-2005

    'jbe: Structure of type mvType  for storing the Medium Voltage converter data.   
    '     Used to temporarily hold the data in the child before transferring it to the parent thread.
    Public globalMVType As edu.washington.apl.pmacs.mvType               'jbe new 04-06-2005

    'tc: Structure of type SpsType  for storing the shore power supply data.   
    '     Used to temporarily hold the data in the child before transferring it to the parent thread.
    Public globalSpsType As edu.washington.apl.pmacs.spsType             'tc new 04-28-2005

    '*** jbe: END OF GLOBAL VARIABLES RELATED TO LOAD DATA CHILD THREAD HAND SHAKING *****************************
    '*************************************************************************************************************

    Public BaseDir As String    'tells the base directory
    Public LoginType As Integer 'tells what kind of user is logged in
    Public counter As Integer   'sets the counter for ground fault
    Public activeFlag As Boolean    'shows whether or not a control is active
    Public SNV(300) As Double   'debug item use to store science node 10KV level voltage
    Public SNI(300) As Double   'debug item use to store science node 10KV level current
    Public SNDate(300) As String 'debug item use to store science node date
    Public SNTime(300) As String 'debug item use to store science node time
    Public SSV(300) As Double   'debug item use to store shore station 10KV level voltage
    Public SSI(300) As Double   'debug item use to store shore station 10KV level current
    Public SSDate(300) As String 'debug item use to store shore station date
    Public SSTime(300) As String 'debug item use to store shore station time
    Public SSSize As Integer    'debug item use to store shore station data size
    Public SNSize As Integer    'debug item use to science node data size
    Public Shore As ShoreStation    'to store shore station data like volatge, current, limits
    Public Science As ScienceNode   'to store shore station data like volatge, current
    Public Inttsecs As Integer     'to store time information from internal load
    Public Inttusecs As Integer    'to store microsecond information from internal load
    Public Exttsecs As Integer     'to store time information from external load
    Public Exttusecs As Integer    'to store microsecond information from external load
    Public Bustsecs As Integer     'to store time iformation from bus
    Public Bustusecs As Integer    'to store microsecond information from bus
    Public Sensortsecs As Integer  'to store time information from engineering sensor
    Public Sensortusecs As Integer 'to store microsecond information from engineering sensor
    Public MVConvertertsecs As Integer  'to store time information from MV converter
    Public MVConvertertusecs As Integer 'to store microsecond information from MV converter
    Public ExtUTCTime As String     'UTC Time stamp of external load data
    Public IntUTCTime As String     'UTC Time stamp of internal load data
    Public BusUTCTime As String     'UTC Time stamp of bus data
    Public SensorUTCTime As String  'UTC Time stamp of sensor data
    Public MVConverterUTCTime As String 'UTC Time stamp of MV Converter data
    Public SystemUTCTime As String      'UTC Time stamp of the system time
    Public GFCurrent400V As Integer     'to store 400V bus ground fault current measurement
    Public GFCurrent400VR As Integer    'to store 400V Return bus ground fault current measurement
    Public GFCurrent48V As Integer      'to store 48V bus ground fault current measurement
    Public GFCurrent48VR As Integer     'to store 48V Return bus ground fault current measurement

    Public GF400VReady As Boolean
    Public GF400VRReady As Boolean
    Public GF48VReady As Boolean
    Public GF48VRReady As Boolean

    Public GF400V As Boolean        'true if 400V bus is faulted
    Public GF400VR As Boolean       'true if 400V return bus is faulted
    Public GF48V As Boolean         'true if 48V bus is faulted
    Public GF48VR As Boolean        'true if 48V return bus is faulted
    Public Sensors(25) As Sensor        'to store engineering sensor data form server
    Public LowVoltageBus(5) As Bus      'to store low voltage bus data from 400V, 48V (external), 48V (Internal), +12V, -12V and 5V
    Public Loads(7) As Load         'to store external load data from server
    Public IntLoads48(6) As IntLoad48   'to store 48V internal load data from server
    Public IntLoads12(6) As IntLoad12   'to store 12V internal load data from server
    Public IntLoads5(1) As IntLoad5     'to store 5V internal load data from server
    Public PowerWet As Double           'store wet housing power
    Public PowerDry As Double           'store dry housing power
    Public MVConverters(1) As MVConverter   'to store MV converter (10KV-400V) data from server
    Public Mat As New MatLib            'type for state estimation matrix computation
    Public RL As Double             'to store backbone cable resistance
    Public H(2, 2) As Double 'define a matrix H with dimensions (4x4)
    Public R(2, 2) As Double 'define a matrix R as the weighting matrix, just identity matrix for now
    'Public Z_meas(,) As Double 'define a vector Z_meas as the measurement data Vss, Iss, Vsn, Isn
    Public x_est(,) As Double

    'jbe: Variables (objects) that are accessible from all routines for data grid.

    'jbe: Declare a DataTable object which will be used to
    '     hold the table of Shore Data items that appear in the columns 
    '     (like Date, Time, Voltage, Current).  
    Public TableShoreData As DataTable

    Public TableShoreDataDum As DataTable 'jbe: Kluge for dummy datagrid table to allow a header of two rows.

    'jbe: Declare a DataTable object which will be used to
    '     hold the table of Science Data items that appear in the columns 
    '     (like Date, Time, Voltage, Current).  
    Public TableScienceData As DataTable

    Public TableScienceDataDum As DataTable 'jbe: Kluge for dummy datagrid table to allow a header of two rows.

    'jbe: Declare a DataSet object to hold a DataTable (will be bound
    '     to TableShoreData in the code below). 
    Public DatasetForShoreData As New DataSet
    Public DatasetForShoreDataDum As New DataSet 'jbe: Kluge for dummy datagrid table to allow a header of two rows.

    'jbe: Declare a DataSet object to hold a DataTable (will be bound
    '     to TableScienceData in the code below). 
    Public DatasetForScienceData As New DataSet
    Public DatasetForScienceDataDum As New DataSet 'jbe: Kluge for dummy datagrid table to allow a header of two rows.

    Public TableAlarmData As DataTable
    Public TableAlarmDataDum As DataTable 'tc: Kluge for dummy datagrid table to allow a header of two rows.
    'tc: Declare a DataSet object to hold a DataTable (will be bound
    '     to TableAlarmData in the code below). 
    Public DatasetForAlarmData As New DataSet
    Public DatasetForAlarmDataDum As New DataSet 'tc: Kluge for dummy datagrid table to allow a header of two rows.

    Public TableSEData As DataTable
    Public TableSEDataDum As DataTable 'tc: Kluge for dummy datagrid table to allow a header of two rows.
    'tc: Declare a DataSet object to hold a DataTable (will be bound
    '     to TableSEData in the code below). 
    Public DatasetForSEData As New DataSet
    Public DatasetForSEDataDum As New DataSet 'SE: Kluge for dummy datagrid table to allow a header of two rows.



    'variables for multi-threading
    Public busStringForGroundFaultMeasure As String 'jbe: Global string containing the name of one of the 4 busses
    '                                                such as "v400", "v400r", "v48", "v48r", "test".
    '                                                Used in call to SOAP method to get the Ground Fault current.

    Public gotNewGfMeasurement As Boolean   'jbe new: True if independent thread has gotten a
    '                                       new Ground Fault current measurement. 
    Public mostRecentGFCurrent As Integer   'jbe new: Integer used to transfer Ground Fault current
    '                                       value from the worker thread to the main parent thread.
    Public wantAbortGfCheckThread As Boolean 'jbe new: If true it tells the GF worker thread to abort itself.

End Module