﻿Imports System.Text

Public Class BasicLogger
    '----------------------------------------
    ' Declaration of global variables
    '----------------------------------------
    Dim g_appid As Short = 0                                ' ID for application access

    Private Sub BasicLogger_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '----------------------------------------
        ' Update the initial display of device name
        '----------------------------------------
        textBox_DeviceName.Text = "AIO000"
        '----------------------------------------
        ' Create the combobox for screen size
        '----------------------------------------
        comboBox_OpenMode.Items.Add("Minimize")
        comboBox_OpenMode.Items.Add("Normal")
        comboBox_OpenMode.Items.Add("Maximize")
        '----------------------------------------
        ' Change the selection of screen size combobox to Minimize
        '----------------------------------------
        comboBox_OpenMode.SelectedIndex = 0
    End Sub

    '================================================================================
    ' It is the process when the LoggerOpenApplication button is clicked
    ' Launch the app and enable access
    '================================================================================
    Private Sub button_LoggerOpenApplication_Click(sender As Object, e As EventArgs) Handles button_LoggerOpenApplication.Click
        Dim device_name As String                         ' Device name
        Dim openmode As UShort                            ' Screen size
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string

        '----------------------------------------
        ' Get device name from textbox
        '----------------------------------------
        device_name = textBox_DeviceName.Text
        '----------------------------------------
        ' Get screen size from combobox
        '----------------------------------------
        openmode = comboBox_OpenMode.SelectedIndex
        '----------------------------------------
        ' Initialize the application
        '----------------------------------------
        ret = LoggerOpenApplication(device_name, g_appid, openmode)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        LoggerGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("LoggerOpenApplication = {0} : {1} ", ret, error_string)
    End Sub

    '================================================================================
    ' It is the process when the LoggerSetDeviceName button is clicked
    ' Set the device name
    '================================================================================
    Private Sub button_LoggerSetDeviceName_Click(sender As Object, e As EventArgs) Handles button_LoggerSetDeviceName.Click
        Dim device_name As String                         ' Device name
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string

        '----------------------------------------
        ' Get device name from textbox
        '----------------------------------------
        device_name = textBox_DeviceName.Text
        '----------------------------------------
        ' Set the device name
        '----------------------------------------
        ret = LoggerSetDeviceName(g_appid, device_name)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        LoggerGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("LoggerSetDeviceName = {0} : {1} ", ret, error_string)
    End Sub

    '================================================================================
    ' It is the process when the ... button is clicked
    ' Displays a dialog for selecting a configuration file
    '================================================================================
    Private Sub button_SettingSelectFile_Click(sender As Object, e As EventArgs) Handles button_SettingSelectFile.Click
        '----------------------------------------
        ' Do Not check if the file name exists
        '----------------------------------------
        openFileDialog1.CheckFileExists = False
        '----------------------------------------
        ' If the Then file path exists, Set the file path To the initial path In the dialog.
        '----------------------------------------
        If textBox_SettingFile.Text.Length > 0 Then
            openFileDialog1.InitialDirectory = textBox_SettingFile.Text
            openFileDialog1.FileName = openFileDialog1.SafeFileName
            '----------------------------------------
            ' If the file path does not exist, set MyDocuments to the initial path in the dialog
            '----------------------------------------
        Else
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
            openFileDialog1.FileName = ""
        End If
        '----------------------------------------
        '  Display the selected path
        '----------------------------------------
        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            textBox_SettingFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName)
        End If
    End Sub

    '================================================================================
    ' It is the process when the ... button is clicked
    ' Displays a dialog for selecting a configuration file
    '================================================================================
    Private Sub button_ArSelectFile_Click(sender As Object, e As EventArgs) Handles button_ArSelectFile.Click
        '----------------------------------------
        ' Do Not check if the file name exists
        '----------------------------------------
        openFileDialog1.CheckFileExists = False
        '----------------------------------------
        ' If the Then file path exists, Set the file path To the initial path In the dialog.
        '----------------------------------------
        If textBox_SettingFile.Text.Length > 0 Then
            openFileDialog1.InitialDirectory = textBox_ArbitraryDataFile.Text
            openFileDialog1.FileName = openFileDialog1.SafeFileName
            '----------------------------------------
            ' If the file path does not exist, set MyDocuments to the initial path in the dialog
            '----------------------------------------
        Else
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
            openFileDialog1.FileName = ""
        End If
        '----------------------------------------
        ' Display the selected path
        '----------------------------------------
        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            textBox_ArbitraryDataFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName)
        End If
    End Sub

    '================================================================================
    ' It is the process when the LoggerImportSettingFile button is clicked
    ' Import the settings file
    '================================================================================
    Private Sub button_LoggerImportSettingFile_Click(sender As Object, e As EventArgs) Handles button_LoggerImportSettingFile.Click
        Dim filepath As String                            ' File path
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim status As UInteger = 0                        ' For status storage

        '----------------------------------------
        ' Get setting file path from the textbox
        '----------------------------------------
        filepath = textBox_SettingFile.Text
        '----------------------------------------
        ' Read settings from the ini file
        '----------------------------------------
        ret = LoggerImportSettingFile(g_appid, filepath, status)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        LoggerGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("LoggerImportSettingFile = {0} : {1} {2}status=0x{3:X16}", ret, error_string, vbCrLf, status)
    End Sub

    '================================================================================
    ' It is the process when the LoggerSaveDataFile button is clicked
    ' Saving the data file
    '================================================================================
    Private Sub button_LoggerSaveDataFile_Click(sender As Object, e As EventArgs) Handles button_LoggerSaveDataFile.Click
        Dim filepath As String                            ' File path
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string

        '----------------------------------------
        ' Get setting file path from the textbox
        '----------------------------------------
        filepath = textBox_ArbitraryDataFile.Text
        '----------------------------------------
        ' Data file saving
        '----------------------------------------
        ret = LoggerSaveDataFile(g_appid, filepath, 0, 0)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        LoggerGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("LoggerSaveDataFile = {0} : {1} ", ret, error_string)
    End Sub

    '================================================================================
    ' It is the process when the Logging Start button is clicked
    ' Start the application input and output
    '================================================================================
    Private Sub button_LoggerStartOperation_Click(sender As Object, e As EventArgs) Handles button_LoggerStartOperation.Click
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim status As UInteger = 0                        ' For status storage

        '----------------------------------------
        ' Close file
        '----------------------------------------
        ret = LoggerGraphWindowOperation(g_appid, CType(LOGGER_PARAM_GRAPH_CLOSE, UShort), CType(LOGGER_PARAM_FILE, UShort), 0)
        If Not ret = 0 Then
            '----------------------------------------
            ' Get error string
            '----------------------------------------
            LoggerGetErrorString(ret, error_string)
            '----------------------------------------
            ' Display error code and message
            '----------------------------------------
            textBox_ReturnNum.Text = String.Format("LoggerGraphWindowOperation = {0} : {1} ", ret, error_string)
            Exit Sub
        End If
        '----------------------------------------
        ' Specify the buffer mode (No mode)
        '----------------------------------------
        ret = LoggerSetAcquiredMode(g_appid, CType(LOGGER_PARAM_MODE_BASIC, UShort))
        If Not ret = 0 Then
            '----------------------------------------
            ' Get error string
            '----------------------------------------
            LoggerGetErrorString(ret, error_string)
            '----------------------------------------
            ' Display error code and message
            '----------------------------------------
            textBox_ReturnNum.Text = String.Format("LoggerSetAcquiredMode = {0} : {1} ", ret, error_string)
            Exit Sub
        End If
        '----------------------------------------
        ' Start the application input and output
        '----------------------------------------
        ret = LoggerStartOperation(g_appid, status)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        LoggerGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("LoggerStartOperation = {0} : {1} {2}status = {3}", ret, error_string, vbCrLf, status)
    End Sub

    '================================================================================
    ' It is the process when the LoggerGetApplicationStatus button is clicked
    ' Get the device status
    '================================================================================
    Private Sub button_LoggerGetApplicationStatus_Click(sender As Object, e As EventArgs) Handles button_LoggerGetApplicationStatus.Click
        Dim status_string As String = ""                  ' Status string
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim status As UInteger = LOGGER_STATUS_STOP       ' For status storage

        '----------------------------------------
        ' Get the device status
        '----------------------------------------
        ret = LoggerGetApplicationStatus(g_appid, status)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        LoggerGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("LoggerGetApplicationStatus = {0} : {1} ", ret, error_string)
        '----------------------------------------
        ' If the status acquisition function is completed successfully
        '----------------------------------------
        If ret = 0 Then
            '----------------------------------------
            ' Get status string
            '----------------------------------------
            Select Case status
                Case CType(LOGGER_STATUS_STOP, UInteger)
                    status_string = "Stop"
                Case CType(LOGGER_STATUS_START_TRG, UInteger)
                    status_string = "Wait the start trigger"
                Case CType(LOGGER_STATUS_BUSY, UInteger)
                    status_string = "Operating"
                Case Else
                    status_string = "Unknown status"
            End Select
            '----------------------------------------
            ' Display the status
            '----------------------------------------
            textBox_Status.Text = String.Format("{0} ({1})", status, status_string)
        End If
    End Sub

    '================================================================================
    ' It is the process when the Logging Stop button is clicked
    ' Stop the application input and output
    '================================================================================
    Private Sub button_LoggerStopOperation_Click(sender As Object, e As EventArgs) Handles button_LoggerStopOperation.Click
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string

        '----------------------------------------
        ' Stop the application input and output
        '----------------------------------------
        ret = LoggerStopOperation(g_appid)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        LoggerGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("LoggerStopOperation = {0} : {1} ", ret, error_string)
    End Sub

    '================================================================================
    ' It is the process when the LoggerCloseApplication button is clicked
    ' Stop the app and disable access
    '================================================================================
    Private Sub button_LoggerCloseApplication_Click(sender As Object, e As EventArgs) Handles button_LoggerCloseApplication.Click
        Dim status As UInteger = 0                        ' For status storage
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim status_string As String                       ' Status string

        '----------------------------------------
        ' Stop the application and disable access
        '----------------------------------------
        ret = LoggerCloseApplication(g_appid, status)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        LoggerGetErrorString(ret, error_string)
        If Not ret = 0 Then
            '----------------------------------------
            ' Display error code and message
            '----------------------------------------
            textBox_ReturnNum.Text = String.Format("LoggerCloseApplication = {0} : {1} ", ret, error_string)
            Exit Sub
        End If
        '----------------------------------------
        ' Get status string
        '----------------------------------------
        Select Case status
            Case CType(LOGGER_STATUS_SUCCESS, UInteger)
                status_string = "Successful completion"
            Case CType(LOGGER_STATUS_FAILURE, UInteger)
                status_string = "C-LOGGER exit error"
            Case Else
                status_string = "Unknown status"
        End Select
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("LoggerCloseApplication = {0} : {1} {2}status = {3} : {4}", ret, error_string, vbCrLf, status, status_string)
    End Sub

    '================================================================================
    ' It is the process when the Close button is clicked
    ' Exit the application
    '================================================================================
    Private Sub button_Close_Click(sender As Object, e As EventArgs) Handles button_Close.Click
        Dim status As UInteger = 0                        ' For status storage

        '----------------------------------------
        ' Close the file(Considering that C-LOGGER may have exited, do not check the result)
        '----------------------------------------
        LoggerGraphWindowOperation(g_appid, CType(LOGGER_PARAM_GRAPH_CLOSE, UShort), CType(LOGGER_PARAM_FILE, UShort), 0)
        '----------------------------------------
        ' Stop the application and disable access
        '----------------------------------------
        LoggerCloseApplication(g_appid, status)
        '----------------------------------------
        ' Exit the dialog
        '----------------------------------------
        Me.Close()
    End Sub
End Class
