'************************************************************************************************************************
' File: WriteAndAlert.vb              Programmer: Ting Chan      Date: July 12, 2005
'
' Description:
' This form provides modules to write system data to text files on local hard disk
' The text files are:   Bus
'                       EventLog
'                       ExtLoad
'                       IntLoad
'                       MVConverter
'                       Sensor
'                       ShorePowerSupply
'                       StateEstimation
'                       AlertMessage
'
' Routines in this file:
' WriteDataExtload()
' WriteDataIntload()
' WriteDataBus()
' WriteDataSensor()
' WriteDataMVConverter()
' WriteDataShorePowerSupply()
' WriteDataAlertMessages()
' WriteDataOperations()
' WriteDataSE()
' ShowMessage()
'
' Revision History:
'
'************************************************************************************************************************

Option Strict Off
Option Explicit On 

Imports System.Web.Mail  'tc: Needed for sending an email with VB.net
Module WriteAndAlert
#Region "Write External Load Data to a file"
    '*************************************************************************
    'This sub-rountine write current external load data into a file.
    'New data is written to the file after it has been received from the server.
    'The data file is named ExtLoad and will be saved in a folder under the 
    'current directory, the foler will be named by month and date.
    '**************************************************************************
    Public Sub WriteDataExtload(ByVal ExtLoad As edu.washington.apl.pmacs.loadListType)
        Dim MyDirExist As String    'check if directory already exist
        Dim MyDirName As String     'string for desired directory
        Dim MyDirFileName As String
        Dim MyCurDir As String      'current directory
        Dim MyDay As Integer        'store the day in the month
        Dim MyFileName As String    'desired file name
        Dim MyMonth As Integer      'store the month
        Dim MyMonthName As String   'store month name
        Dim MyData As String        'string for data to be written in the data file
        Dim myString As String
        Dim MyYear As Integer
        Dim i As Integer            'working variable

        MyData = ""                 'initialize data to empty string

        MyYear = Year(Now)
        MyMonth = Month(Now)        'get current month
        MyMonthName = MonthName(MyMonth, True)  'get name of current month
        MyDay = Microsoft.VisualBasic.Day(Now)  'get the day in a month
        'MyMinute = Minute(Now)
        MyCurDir = CurDir()         'get current directory
        'MyDirName = MyCurDir + "\" + MyMonthName + CStr(MyDay)
        MyDirName = BaseDir + "\" + CStr(MyYear) + "\" + MyMonthName + CStr(MyDay) 'name of desire directory
        '                                                        with month and day
        'check to see if desired directory already exist
        MyDirExist = Dir(MyDirName, vbDirectory)
        If MyDirExist = "" Then     'directory does not exist, make it
            MkDir(MyDirName)
            ChDir(MyDirName)        'set current directory to desired one
        End If

        'file name for external load data
        MyFileName = "ExtLoad"

        MyDirFileName = MyDirName + "\" + MyFileName
        MyDirExist = Dir(MyDirFileName, vbDirectory)
        If MyDirExist = "" Then     'directory does not exist
            MyData = "             Date and Time" + "  Unix Seconds" + " Micro-Seconds" + "           Load Name" + " Port" + "             Bus" + "           Error" + " Current" + " Current Limit" + " Switch" + " Deadface" + " Breaker"
            For i = 0 To 14
                MyData = MyData + "           Load Name" + " Port" + "             Bus" + "           Error" + " Current" + " Current Limit" + " Switch" + " Deadface" + " Breaker"
            Next
            MyData = MyData + vbCrLf
        End If

        'store the external load data from server onto MyData
        'store time in seconds and microseconds
        myString = "           " + ExtUTCTime
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 25)
        myString = "           " + CStr(ExtLoad.tsecs)
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 13)
        myString = "           " + CStr(ExtLoad.tusecs)
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 13)
        For i = 0 To 15
            myString = "                       " + ExtLoad.data(i).name
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 19)        'store external load name
            myString = "           " + CStr(ExtLoad.data(i).port)
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 4)  'store externalload port
            myString = "           " + ExtLoad.data(i).bus
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 15) 'store the bus for the external load
            If ExtLoad.data(i).error = "" Then
                myString = "                  " + "n/a"
                MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 15) 'when there is no error, the string is sometimes empty
            Else
                myString = "                  " + ExtLoad.data(i).error
                MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 15)       'store the error string for the external load
            End If
            myString = "                " + CStr(ExtLoad.data(i).current)
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 7)   'store the external load current
            myString = "                " + CStr(ExtLoad.data(i).current_limit)
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 13) 'store the external load current limit
            myString = "                " + CStr(ExtLoad.data(i).switch)
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 6)    'store the status of switch
            myString = "                " + CStr(ExtLoad.data(i).deadface)
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 8)  'store the status of deadface
            myString = "                " + CStr(ExtLoad.data(i).breaker)
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 7)   'store the status of breaker signal
        Next i
        'MyData = MyData + " " + CStr(ExtLoad.tsecs)
        'MyData = MyData + " " + CStr(ExtLoad.tusecs)
        'For i = 0 To 15
        'MyData = MyData + " " + ExtLoad.data(i).name        'store external load name
        'MyData = MyData + " " + CStr(ExtLoad.data(i).port)  'store externalload port
        'MyData = MyData + " " + ExtLoad.data(i).bus         'store the bus for the external load
        'If ExtLoad.data(i).error = "" Then
        'MyData = MyData + " " + "n/a"                   'when there is no error, the string is sometimes empty
        'Else
        '    MyData = MyData + " " + ExtLoad.data(i).error       'store the error string for the external load
        'End If
        'MyData = MyData + " " + CStr(ExtLoad.data(i).current)   'store the external load current
        'MyData = MyData + " " + CStr(ExtLoad.data(i).current_limit) 'store the external load current limit
        'MyData = MyData + " " + CStr(ExtLoad.data(i).switch)    'store the status of switch
        'MyData = MyData + " " + CStr(ExtLoad.data(i).deadface)  'store the status of deadface
        'MyData = MyData + " " + CStr(ExtLoad.data(i).breaker)   'store the status of breaker signal
        'Next i

        'open the file for writing external load data
        FileOpen(10, MyFileName, OpenMode.Append)
        PrintLine(10, MyData)   'write external load data
        FileClose(10)
    End Sub
#End Region

#Region "Write Internal Load Data to a file"
    '*************************************************************************
    'This sub-rountine write current internal load data into a file.
    'New data is written to the file after it has been received from the server.
    'The data file is named IntLoad and will be saved in a folder under the 
    'current directory, the foler will be named by month and date.
    '**************************************************************************
    Public Sub WriteDataIntload(ByVal IntLoad As edu.washington.apl.pmacs.loadListType)
        Dim MyDirExist As String    'check if directory already exist
        Dim MyDirName As String     'string for desired directory
        Dim MyCurDir As String      'current directory
        Dim MyDay As Integer        'store the day in the month
        Dim MyFileName As String    'desired file name
        Dim MyMonth As Integer      'store the month
        Dim MyMonthName As String   'store month name
        Dim MyData As String        'string for data to be written in the data file
        'Dim MyMinute As Integer
        Dim MyDirFileName As String
        Dim myString As String
        Dim MyYear As Integer
        Dim i As Integer            'working variable

        MyData = ""                 'initialize data to empty string

        MyYear = Year(Now)
        MyMonth = Month(Now)        'get current month
        MyMonthName = MonthName(MyMonth, True)  'get name of current month
        MyDay = Microsoft.VisualBasic.Day(Now)  'get the day in a month
        'MyMinute = Minute(Now)
        MyCurDir = CurDir()         'get current directory
        'MyDirName = MyCurDir + "\" + MyMonthName + CStr(MyDay)
        MyDirName = BaseDir + "\" + CStr(MyYear) + "\" + MyMonthName + CStr(MyDay) 'name of desire directory

        'check to see if desired directory already exist
        MyDirExist = Dir(MyDirName, vbDirectory)
        If MyDirExist = "" Then     'directory does not exist, make it
            MkDir(MyDirName)
            ChDir(MyDirName)        'set current directory to desired one
        End If

        'file name for internal load data
        MyFileName = "IntLoad"

        MyDirFileName = MyDirName + "\" + MyFileName
        MyDirExist = Dir(MyDirFileName, vbDirectory)
        If MyDirExist = "" Then     'directory does not exist
            MyData = "             Date and Time" + "  Unix Seconds" + " Micro-Seconds" + "           Load Name" + " Port" + "             Bus" + "           Error" + " Current" + " Current Limit" + " Switch" + " Deadface" + " Breaker"
            For i = 0 To 14
                MyData = MyData + "           Load Name" + " Port" + "             Bus" + "           Error" + " Current" + " Current Limit" + " Switch" + " Deadface" + " Breaker"
            Next
            MyData = MyData + vbCrLf
        End If

        'store the internal load data from server onto MyData
        'store time in seconds and microseconds
        myString = "           " + IntUTCTime
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 25)
        myString = "           " + CStr(IntLoad.tsecs)
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 13)
        myString = "           " + CStr(IntLoad.tusecs)
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 13)
        For i = 0 To 15
            myString = "                       " + IntLoad.data(i).name
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 19)        'store external load name
            myString = "           " + CStr(IntLoad.data(i).port)
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 4)  'store externalload port
            myString = "           " + IntLoad.data(i).bus
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 15) 'store the bus for the external load
            If IntLoad.data(i).error = "" Then
                myString = "                  " + "n/a"
                MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 15) 'when there is no error, the string is sometimes empty
            Else
                myString = "                  " + IntLoad.data(i).error
                MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 15)       'store the error string for the external load
            End If
            myString = "                " + CStr(IntLoad.data(i).current)
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 7)   'store the external load current
            myString = "                " + CStr(IntLoad.data(i).current_limit)
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 13) 'store the external load current limit
            myString = "                " + CStr(IntLoad.data(i).switch)
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 6)    'store the status of switch
            myString = "                " + CStr(IntLoad.data(i).deadface)
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 8)  'store the status of deadface
            myString = "                " + CStr(IntLoad.data(i).breaker)
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 7)   'store the status of breaker signal
        Next i

        ''open the file for writing internal load data
        FileOpen(11, MyFileName, OpenMode.Append)
        PrintLine(11, MyData)   'write internal load data
        FileClose(11)
    End Sub
#End Region

#Region "Write Low Voltage Bus Data to a file"
    '*************************************************************************
    'This sub-rountine write current low voltage bus data into a file.
    'New data is written to the file after it has been received from the server.
    'The data file is named Bus and will be saved in a folder under the 
    'current directory, the foler will be named by month and date.
    '**************************************************************************
    Public Sub WriteDataBus(ByVal Bus As edu.washington.apl.pmacs.busListType)
        Dim MyDirExist As String    'check if directory already exist
        Dim MyDirName As String     'string for desired directory
        Dim MyCurDir As String      'current directory
        Dim MyDay As Integer        'store the day in the month
        Dim MyFileName As String    'desired file name
        Dim MyMonth As Integer      'store the month
        Dim MyMonthName As String   'store month name
        Dim MyData As String        'string for data to be written in the data file
        'Dim MyMinute As Integer
        Dim MyDirFileName As String
        Dim myString As String
        Dim MyYear As Integer
        Dim i As Integer            'working variable

        MyData = ""                 'initialize data to empty string

        MyYear = Year(Now)
        MyMonth = Month(Now)        'get current month
        MyMonthName = MonthName(MyMonth, True)  'get name of current month
        MyDay = Microsoft.VisualBasic.Day(Now)  'get the day in a month
        'MyMinute = Minute(Now)
        MyCurDir = CurDir()         'get current directory
        'MyDirName = MyCurDir + "\" + MyMonthName + CStr(MyDay)
        MyDirName = BaseDir + "\" + CStr(MyYear) + "\" + MyMonthName + CStr(MyDay) 'name of desire directory 
        '                                                                           with month and day
        'check to see if desired directory already exist
        MyDirExist = Dir(MyDirName, vbDirectory)
        If MyDirExist = "" Then     'directory does not exist, make it
            MkDir(MyDirName)
            ChDir(MyDirName)        'set current directory to desired one
        End If

        'file name for Bus data
        MyFileName = "Bus"

        MyDirFileName = MyDirName + "\" + MyFileName
        MyDirExist = Dir(MyDirFileName, vbDirectory)
        If MyDirExist = "" Then     'directory does not exist
            MyData = "             Date and Time" + "  Unix Seconds" + " Micro-Seconds" + "       Bus Name" + " Voltage" + " Current"
            For i = 0 To 4
                MyData = MyData + "       Bus Name" + " Voltage" + " Current"
            Next
            MyData = MyData + vbCrLf
        End If

        'store the bus data from server onto MyData
        'store time in seconds and microseconds
        myString = "           " + BusUTCTime
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 25)
        myString = "           " + CStr(Bus.tsecs)
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 13)
        myString = "           " + CStr(Bus.tusecs)
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 13)
        For i = 0 To 5  'store bus data for all 6 low voltage busses
            myString = "           " + Bus.data(i).name
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 14)
            myString = "           " + CStr(Bus.data(i).voltage)   'store bus voltage
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 7)
            myString = "           " + CStr(Bus.data(i).current)   'store bus current
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 7)
        Next i
        'MyData = MyData + vbCrLf

        'open the file for write bus data
        FileOpen(12, MyFileName, OpenMode.Append)
        PrintLine(12, MyData)   'write bus data onto file
        FileClose(12)
    End Sub
#End Region

#Region "Write Engineering Sensor Data to a file"
    '*************************************************************************
    'This sub-rountine write current engineering sensor data into a file.
    'New data is written to the file after it has been received from the server.
    'The data file is named Bus and will be saved in a folder under the 
    'current directory, the foler will be named by month and date.
    '**************************************************************************
    Public Sub WriteDataSensor(ByVal Sensor As edu.washington.apl.pmacs.sensorListType)
        Dim MyDirExist As String    'check if directory already exist
        Dim MyDirName As String     'string for desired directory
        Dim MyCurDir As String      'current directory
        Dim MyDay As Integer        'store the day in the month
        Dim MyFileName As String    'desired file name
        Dim MyMonth As Integer      'store the month
        Dim MyMonthName As String   'store month name
        Dim MyData As String        'string for data to be written in the data file
        'Dim MyMinute As Integer
        Dim MyDirFileName As String
        Dim myString As String
        Dim MyYear As Integer
        Dim i As Integer            'working variable

        MyData = ""                 'initialize data to empty string

        MyYear = Year(Now)
        MyMonth = Month(Now)        'get current month
        MyMonthName = MonthName(MyMonth, True)  'get name of current month
        MyDay = Microsoft.VisualBasic.Day(Now)  'get the day in a month
        'MyMinute = Minute(Now)
        MyCurDir = CurDir()         'get current directory
        'MyDirName = MyCurDir + "\" + MyMonthName + CStr(MyDay)
        MyDirName = BaseDir + "\" + CStr(MyYear) + "\" + MyMonthName + CStr(MyDay) 'name of desire directory
        '                                                                           with month and day
        'check to see if desired directory already exist
        MyDirExist = Dir(MyDirName, vbDirectory)
        If MyDirExist = "" Then     'directory does not exist, make it
            MkDir(MyDirName)
            ChDir(MyDirName)        'set current directory to desired one
        End If

        'file name for engineering sensor data
        MyFileName = "Sensor"

        MyDirFileName = MyDirName + "\" + MyFileName
        MyDirExist = Dir(MyDirFileName, vbDirectory)
        If MyDirExist = "" Then     'directory does not exist
            MyData = "             Date and Time" + "  Unix Seconds" + " Micro-Seconds" + "        Sensor Name" + "              Value" + "         Unit"
            For i = 0 To 24
                MyData = MyData + "        Sensor Name" + "              Value" + "         Unit"
            Next
            MyData = MyData + vbCrLf
        End If

        'store the engineering sensor data from server onto MyData
        'store time in seconds and microseconds
        myString = "           " + SensorUTCTime
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 25)
        myString = "           " + CStr(Sensor.tsecs)
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 13)
        myString = "           " + CStr(Sensor.tusecs)
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 13)
        For i = 0 To 25
            myString = "           " + Sensor.data(i).name 'store sensor name
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 17)
            myString = "                        " + CStr(Sensor.data(i).value)  'store sensor measurement
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 18)
            myString = "           " + Sensor.data(i).units    'store the unit for the sensor measurement
            MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 13)
        Next i

        'open the file for writing engineering sensor data
        FileOpen(13, MyFileName, OpenMode.Append)
        PrintLine(13, MyData)   'write engineering sensor data
        FileClose(13)
    End Sub
#End Region

#Region "Write MV Converter Data to a file"
    '*************************************************************************
    'This sub-rountine write current MV Converter data into a file.
    'New data is written to the file after it has been received from the server.
    'The data file is named Bus and will be saved in a folder under the 
    'current directory, the foler will be named by month and date.
    '**************************************************************************
    Public Sub WriteDataMVConverter(ByVal MVConverter As edu.washington.apl.pmacs.mvType)
        Dim MyDirExist As String    'check if directory already exist
        Dim MyDirName As String     'string for desired directory
        Dim MyCurDir As String      'current directory
        Dim MyDay As Integer        'store the day in the month
        Dim MyFileName As String    'desired file name
        Dim MyMonth As Integer      'store the month
        Dim MyMonthName As String   'store month name
        Dim MyData As String        'string for data to be written in the data file
        'Dim MyMinute As Integer
        Dim MyYear As Integer
        Dim i As Integer            'working variable

        MyData = ""                 'initialize data to empty string

        MyYear = Year(Now)
        MyMonth = Month(Now)        'get current month
        MyMonthName = MonthName(MyMonth, True)  'get name of current month
        MyDay = Microsoft.VisualBasic.Day(Now)  'get the day in a month
        'MyMinute = Minute(Now)
        MyCurDir = CurDir()         'get current directory
        'MyDirName = MyCurDir + "\" + MyMonthName + CStr(MyDay)
        MyDirName = BaseDir + "\" + CStr(MyYear) + "\" + MyMonthName + CStr(MyDay) 'name of desire directory
        '                                                                           with month and day
        'check to see if desired directory already exist
        MyDirExist = Dir(MyDirName, vbDirectory)
        If MyDirExist = "" Then     'directory does not exist, make it
            MkDir(MyDirName)
            ChDir(MyDirName)        'set current directory to desired one
        End If

        'file name for MV converter data
        MyFileName = "MVConverter"

        'store the engineering sensor data from server onto MyData
        'store time in seconds and microseconds
        MyData = MyData + " " + CStr(MVConverter.tsecs)
        MyData = MyData + " " + CStr(MVConverter.tusecs)
        MyData = MyData + " " + CStr(MVConverter.current)   'store converter input current
        MyData = MyData + " " + CStr(MVConverter.voltage)   'store converter input voltage
        MyData = MyData + " " + CStr(MVConverter.current_diff)      'store converter differential current
        MyData = MyData + " " + CStr(MVConverter.aworking)  'store converter A status
        MyData = MyData + " " + CStr(MVConverter.aok)       'store converter A fault status
        MyData = MyData + " " + CStr(MVConverter.i400a)     'store converter A output current
        MyData = MyData + " " + CStr(MVConverter.v400a)     'store converter A output voltage
        For i = 0 To 5
            MyData = MyData + " " + CStr(MVConverter.astages(i))    'store each of the 6 converter A stage voltages
        Next
        MyData = MyData + " " + CStr(MVConverter.bworking)  'store converter B status
        MyData = MyData + " " + CStr(MVConverter.bok)       'store converter B fault status
        MyData = MyData + " " + CStr(MVConverter.i400b)     'store converter B output current
        MyData = MyData + " " + CStr(MVConverter.v400b)     'store converter B output voltage
        For i = 0 To 5
            MyData = MyData + " " + CStr(MVConverter.bstages(i))    'store each of the 6 converter B stage voltages
        Next

        'MyData = MyData + vbCrLf

        'open the file for writing MV converter data
        FileOpen(14, MyFileName, OpenMode.Append)
        PrintLine(14, MyData)   'write MV Converter data
        FileClose(14)
    End Sub
#End Region

#Region "Write Shore Power Supply Data to a file"
    Public Sub WriteDataShorePowerSupply(ByVal ShoreData As edu.washington.apl.pmacs.spsType, ByVal ShoreTime As String)
        Dim MyDirExist As String    'check if directory already exist
        Dim MyDirName As String     'string for desired directory
        Dim MyCurDir As String      'current directory
        Dim MyDay As Integer        'store the day in the month
        Dim MyFileName As String    'desired file name
        Dim MyMonth As Integer      'store the month
        Dim MyMonthName As String   'store month name
        Dim MyData As String        'string for data to be written in the data file
        'Dim MyMinute As Integer
        Dim MyDirFileName As String
        Dim myString As String
        Dim MyYear As Integer
        Dim i As Integer            'working variable

        MyData = ""                 'initialize data to empty string

        MyYear = Year(Now)
        MyMonth = Month(Now)        'get current month
        MyMonthName = MonthName(MyMonth, True)  'get name of current month
        MyDay = Microsoft.VisualBasic.Day(Now)  'get the day in a month
        'MyMinute = Minute(Now)
        MyCurDir = CurDir()         'get current directory
        'MyDirName = MyCurDir + "\" + MyMonthName + CStr(MyDay)
        MyDirName = BaseDir + "\" + CStr(MyYear) + "\" + MyMonthName + CStr(MyDay) 'name of desire directory
        '                                                                           with month and day
        'check to see if desired directory already exist
        MyDirExist = Dir(MyDirName, vbDirectory)
        If MyDirExist = "" Then     'directory does not exist, make it
            MkDir(MyDirName)
            ChDir(MyDirName)        'set current directory to desired one
        End If

        'file name for MV converter data
        MyFileName = "ShorePowerSupply"

        MyDirFileName = MyDirName + "\" + MyFileName
        MyDirExist = Dir(MyDirFileName, vbDirectory)
        If MyDirExist = "" Then     'directory does not exist
            MyData = "             Date and Time" + "   Status" + "    Current" + "    Voltage" + "    Ramping Time Constant"
            MyData = MyData + vbCrLf
        End If

        'store the engineering sensor data from server onto MyData
        'store time in seconds and microseconds
        myString = "           " + ShoreTime
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 25)
        myString = "           " + CStr(ShoreData.state)    'store shore state
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 8)
        myString = "           " + CStr(ShoreData.current)  'store shore output current
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 10)
        myString = "           " + CStr(ShoreData.voltage)  'store shore output voltage
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 10)
        myString = "                          " + CStr(ShoreData.tc)   'store shore power supply ramping constant
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 24)

        'open the file for writing shore power supply data
        FileOpen(15, MyFileName, OpenMode.Append)
        PrintLine(15, MyData)   'write shore power supply data
        FileClose(15)
    End Sub
#End Region

#Region "Write Alert Message to a file"
    '*************************************************************************
    'This sub-rountine writes new alert messages to a file.
    'New data is written to the file after it is being displayed on screen.
    'The data file is named AlertMessage and will be saved in a folder under the 
    'current directory, the foler will be named by month and date.
    '**************************************************************************
    Public Sub WriteDataAlertMessages(ByVal myMessage As String)
        Dim MyDirExist As String    'check if directory already exist
        Dim MyDirName As String     'string for desired directory
        Dim MyDirFileName As String
        Dim MyCurDir As String      'current directory
        Dim MyDay As Integer        'store the day in the month
        Dim MyFileName As String    'desired file name
        Dim MyMonth As Integer      'store the month
        Dim MyMonthName As String   'store month name
        Dim MyData As String        'string for data to be written in the data file
        'Dim MyMinute As Integer
        Dim myString As String
        Dim MyYear As Integer
        Dim i As Integer            'working variable

        MyData = ""                 'initialize data to empty string

        MyYear = Year(Now)
        MyMonth = Month(Now)        'get current month
        MyMonthName = MonthName(MyMonth, True)  'get name of current month
        MyDay = Microsoft.VisualBasic.Day(Now)  'get the day in a month
        'MyMinute = Minute(Now)
        MyCurDir = CurDir()         'get current directory
        'MyDirName = MyCurDir + "\" + MyMonthName + CStr(MyDay)
        MyDirName = BaseDir + "\" + CStr(MyYear) + "\" + MyMonthName + CStr(MyDay) 'name of desire directory
        '                                                                           with month and day

        'check to see if desired directory already exist
        MyDirExist = Dir(MyDirName, vbDirectory)
        If MyDirExist = "" Then     'directory does not exist, make it
            MkDir(MyDirName)
            ChDir(MyDirName)        'set current directory to desired one
        End If

        'file name for events log data
        MyFileName = "AlertMessage"

        MyDirFileName = MyDirName + "\" + MyFileName
        'store the alert message onto MyData
        'store date and time
        MyData = MyData + myMessage

        'open the file for writing alert messages
        FileOpen(18, MyFileName, OpenMode.Append)
        PrintLine(18, MyData)   'write alert message
        FileClose(18)
    End Sub
#End Region

#Region "Write Operations Log Data to a file"
    '*************************************************************************
    'This sub-rountine write current Operations Log data into a file.
    'New data is written to the file after it has been received from the server.
    'The data file is named Operations and will be saved in a folder under the 
    'current directory, the foler will be named by month and date.
    '**************************************************************************
    Public Sub WriteDataOperations(ByVal Row As DataRow, ByVal mySec As Integer, ByVal myMicrosec As Integer)
        Dim MyDirExist As String    'check if directory already exist
        Dim MyDirName As String     'string for desired directory
        Dim MyDirFileName As String
        Dim MyCurDir As String      'current directory
        Dim MyDay As Integer        'store the day in the month
        Dim MyFileName As String    'desired file name
        Dim MyMonth As Integer      'store the month
        Dim MyMonthName As String   'store month name
        Dim MyData As String        'string for data to be written in the data file
        'Dim MyMinute As Integer
        Dim myString As String
        Dim MyYear As Integer
        Dim i As Integer            'working variable

        MyData = ""                 'initialize data to empty string

        MyYear = Year(Now)
        MyMonth = Month(Now)        'get current month
        MyMonthName = MonthName(MyMonth, True)  'get name of current month
        MyDay = Microsoft.VisualBasic.Day(Now)  'get the day in a month
        'MyMinute = Minute(Now)
        MyCurDir = CurDir()         'get current directory
        'MyDirName = MyCurDir + "\" + MyMonthName + CStr(MyDay)
        MyDirName = BaseDir + "\" + CStr(MyYear) + "\" + MyMonthName + CStr(MyDay) 'name of desire directory
        '                                                                           with month and day

        'check to see if desired directory already exist
        MyDirExist = Dir(MyDirName, vbDirectory)
        If MyDirExist = "" Then     'directory does not exist, make it
            MkDir(MyDirName)
            ChDir(MyDirName)        'set current directory to desired one
        End If

        'file name for events log data
        MyFileName = "Event Log"

        MyDirFileName = MyDirName + "\" + MyFileName

        'MsgBox(BaseDir)
        'MsgBox(MyDirFileName)
        MyDirExist = Dir(MyDirFileName, vbDirectory)
        If MyDirExist = "" Then     'directory does not exist
            MyData = "             Date and Time" + "  Unix Seconds" + " Micro-Seconds" + "           Load ID/Bus ID" + "              Type" + "  Old Status" + "  New Status" + "  Old Current Limit" + "  New Current Limit" + "                        Description"
            MyData = MyData + vbCrLf
        End If

        'store the operations data onto MyData
        'store date and time
        myString = "              " + CStr(Row.Item("Date"))
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 25)
        myString = "               " + CStr(mySec)
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 13)
        myString = "               " + CStr(myMicrosec)
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 13)

        myString = "                                  " + Row.Item("Load")
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 24) 'store external load id or bus id or converter id
        myString = "           " + Row.Item("Type")
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 17) 'store type of operation
        myString = "           " + Row.Item("Old Status")
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 11) 'store the old status
        myString = "           " + Row.Item("New Status")
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 11) 'store the new status
        myString = "                " + Row.Item("Old Current Limit")
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 18) 'store the old current limit
        myString = "                " + Row.Item("New Current Limit")
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 18) 'store the new current limit
        myString = "                " + Row.Item("Description")
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 34) 'store description

        'open the file for writing operations data
        FileOpen(16, MyFileName, OpenMode.Append)
        PrintLine(16, MyData)   'write operations data
        FileClose(16)
    End Sub
#End Region

#Region "Write State Estimation Data to a file"
    '*************************************************************************
    'This sub-rountine write state estimation output data into a file.
    'The data file is named StateEstimation and will be saved in a folder under the 
    'current directory, the foler will be named by month and date.
    '**************************************************************************
    Public Sub WriteDataSE(ByVal SERow As DataRow)
        Dim MyDirExist As String    'check if directory already exist
        Dim MyDirName As String     'string for desired directory
        Dim MyDirFileName As String
        Dim MyCurDir As String      'current directory
        Dim MyDay As Integer        'store the day in the month
        Dim MyFileName As String    'desired file name
        Dim MyMonth As Integer      'store the month
        Dim MyMonthName As String   'store month name
        Dim MyData As String        'string for data to be written in the data file
        'Dim MyMinute As Integer
        Dim myString As String
        Dim MyYear As Integer
        Dim i As Integer            'working variable

        MyData = ""                 'initialize data to empty string

        MyYear = Year(Now)

        MyMonth = Month(Now)        'get current month
        MyMonthName = MonthName(MyMonth, True)  'get name of current month
        MyDay = Microsoft.VisualBasic.Day(Now)  'get the day in a month
        'MyMinute = Minute(Now)
        MyCurDir = CurDir()         'get current directory
        'MyDirName = MyCurDir + "\" + MyMonthName + CStr(MyDay)
        MyDirName = BaseDir + "\" + CStr(MyYear) + "\" + MyMonthName + CStr(MyDay)  'name of desire directory
        '                                                        with month and day
        'check to see if desired directory already exist
        MyDirExist = Dir(MyDirName, vbDirectory)
        If MyDirExist = "" Then     'directory does not exist, make it
            MkDir(MyDirName)
            ChDir(MyDirName)        'set current directory to desired one
        End If

        'file name for external load data
        MyFileName = "StateEstimation"

        MyDirFileName = MyDirName + "\" + MyFileName
        MyDirExist = Dir(MyDirFileName, vbDirectory)
        If MyDirExist = "" Then     'directory does not exist
            MyData = "             Date and Time" + "  Estimated Shore Voltage" + "  Estimated Shore Current" + "  Estimated Science Node Voltage" + "  Estimated Science Node Current"
            MyData = MyData + vbCrLf
        End If

        'store the operations data onto MyData
        'store date and time
        myString = "              " + CStr(SERow.Item("Date"))
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 25)

        myString = "                             " + SERow.Item("SESSVoltage")
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 22) + " V" 'store estimated Shore voltage
        myString = "                             " + SERow.Item("SESSCurrent")
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 22) + " A" 'store estimated shore current
        myString = "                             " + SERow.Item("SESNVoltage")
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 29) + " V" 'store estimated science node voltage
        myString = "                             " + SERow.Item("SESNCurrent")
        MyData = MyData + " " + Microsoft.VisualBasic.Right(myString, 29) + " A" 'store estimated science node current

        'open the file for writing operations data
        FileOpen(17, MyFileName, OpenMode.Append)
        PrintLine(17, MyData)   'write operations data
        FileClose(17)
    End Sub
#End Region

#Region "Display error message"
    'Subroutine to display a message in a customized message box
    'unlike the default VB.net MsgBox, the customized message box
    'would allow the user to ignore it and continue.
    Public Sub ShowMessage(ByVal MsgBody As String)
        Dim myPrompt As String
        Dim myPromptNew As String
        Dim myPromptAll As String

        'inlcude in the message box previous un-read messages.
        'if the message box is not closed, all message displayed 
        'on it will be treated as new messages
        myPrompt = MyMsgBoxForm.DefInstance.Label1.Text
        'display individual message number
        'myPrompt = myPrompt + "Message #" + CStr(MessageCount + 1) + vbCrLf
        myPromptNew = "Message #" + CStr(MessageCount + 1) + vbCrLf
        'display the message body and put a line under to seperate it from next message
        myPromptNew = myPromptNew + MsgBody
        myPromptNew = myPromptNew + "===================================================" + vbCrLf

        myPromptAll = myPrompt + myPromptNew
        'show how many new messages are displayed in the message box 
        MyMsgBoxForm.DefInstance.Label2.Text = "You have " + CStr(MessageCount + 1) + " new message(s)!"
        MyMsgBoxForm.DefInstance.Text = "New Message"

        'show the message in the customized message box
        'MyMsgBoxForm.DefInstance.Label1.Text = myPrompt
        MyMsgBoxForm.DefInstance.Label1.Text = myPromptAll

        'write most current message to text file
        Call WriteDataAlertMessages(myPromptNew)

        'user have the option to scroll to the bottom to display the most recent message 
        If MessageBottom Then
            MyMsgBoxForm.DefInstance.Label1.SelectionStart = Len(myPrompt)
            MyMsgBoxForm.DefInstance.Label1.ScrollToCaret()  'tc: Show bottom line in textbox.
        End If

        '**************************************************************************
        ' tc: module for sending an email alert message to some email address
        '**************************************************************************
        'the SMTP server is not set, the default
        'SMTP server wil be used. 
        Dim oMsg As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage

        'sens an email message to describe the alert 
        oMsg.From = "tingc@u.washington.edu"
        oMsg.To = "tingc@ee.washington.edu"
        oMsg.Subject = "PMACS Alert Message"
        oMsg.Body = MsgBody
        oMsg.Priority = MailPriority.High
        'SmtpMail.Send(oMsg)

        'increment the message counter so we know how many messahes are there
        MessageCount = MessageCount + 1

        'show the message box on the screen and set focus to it
        MyMsgBoxForm.DefInstance.Show()
        MyMsgBoxForm.DefInstance.Focus()

        'postion the message on the middle of the screen
        MyMsgBoxForm.DefInstance.Width = 560
        MyMsgBoxForm.DefInstance.Height = 344
        MyMsgBoxForm.DefInstance.WindowState = FormWindowState.Normal
        MyMsgBoxForm.DefInstance.Left = 360
        MyMsgBoxForm.DefInstance.Top = 364
    End Sub
#End Region
End Module
