Jonathan Jenne ea8ed48d25 15-08-23
2023-08-15 13:27:50 +02:00

267 lines
8.9 KiB
VB.net

Imports System.ComponentModel
Imports System.Runtime.Remoting.Messaging
Imports DevExpress.XtraEditors.ViewInfo
Imports DigitalData.Modules.Config
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Partial Public Class frmMain
Private LogConfig As LogConfig
Private Logger As Logger
Private ConfigManager As ConfigManager(Of Config)
Private Database As MSSQLServer
Private Sync As ISync
Private FilesProcessed As Integer = 0
Private ErrorsOccurred As Integer = 0
Public Enum LogLevel
Info
Warning
[Error]
End Enum
Private Class LogLine
Public Message As String
Public Level As LogLevel
Public CreatedWhen As Date = Now
Public Overrides Function ToString() As String
Return $"{CreatedWhen.ToShortDateString} {CreatedWhen.ToShortTimeString} {Message}"
End Function
End Class
Public Sub New()
InitializeComponent()
End Sub
Private Async Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
Try
LogConfig = New LogConfig(LogConfig.PathType.CustomPath, IO.Path.Combine(Application.StartupPath, "Log"))
ConfigManager = New ConfigManager(Of Config)(LogConfig, Application.StartupPath)
LogConfig.Debug = ConfigManager.Config.Debug
Logger = LogConfig.GetLogger()
AddInfoEntry("Application started.")
AddInfoEntry("Version: {0}", Application.ProductVersion)
AddDivider()
Database = New MSSQLServer(LogConfig, ConfigManager.Config.ConnectionString)
Sync = InitializeModule(ConfigManager.Config.ActiveModule)
' Load Form Title from Module
Text = Sync.Name
TrayIcon.Text = Sync.Name
If Sync Is Nothing Then
AddWarnEntry("ActiveModule '{0}' is not implemented!", ConfigManager.Config.ActiveModule)
Exit Sub
End If
If Database.DBInitialized = False Then
AddWarnEntry("Database could not be initialized. Please check connection string.")
Exit Sub
End If
btnForceSync.Enabled = True
If ConfigManager.Config.Autostart And Sync.TestConfigIsComplete() Then
btnForceSync.Enabled = False
Await Sync.Run()
btnForceSync.Enabled = True
End If
If ConfigManager.Config.TimerIntervalMin > 0 Then
AddInfoEntry("Timer Interval: {0} min", ConfigManager.Config.TimerIntervalMin.ToString)
SyncTimer.Interval = ConfigManager.Config.TimerIntervalMin * 60 * 1_000
StartTimer()
Else
AddInfoEntry("Timer Interval: Off", ConfigManager.Config.TimerIntervalMin.ToString)
End If
Catch ex As Exception
Logger.Error(ex)
AddErrorEntry($"Error while loading the application: {ex.Message}")
End Try
End Sub
Private Function InitializeModule(pActiveModule As String) As ISync
Dim oSync As ISync
Select Case pActiveModule
Case "slt"
oSync = New slt.sltSync(LogConfig, Database, ConfigManager.Config)
Case "Sharepoint"
oSync = New Sharepoint.SharepointSync(LogConfig, Database, ConfigManager.Config)
Case Else
Return Nothing
End Select
AddHandler oSync.OnLogEntry, AddressOf Sync_OnLogEntry
AddHandler oSync.OnFileError, AddressOf Sync_OnFileError
AddHandler oSync.OnFileProcessed, AddressOf Sync_OnFileProcessed
Return oSync
End Function
Private Sub Sync_OnFileProcessed(sender As Object, e As String)
FilesProcessed += 1
txtFilesProcessed.Caption = String.Format("{0} Dateien", FilesProcessed)
End Sub
Private Sub Sync_OnFileError(sender As Object, e As String)
ErrorsOccurred += 1
txtErrorsOccurred.Caption = String.Format("{0} Fehler", ErrorsOccurred)
End Sub
Private Sub Sync_OnLogEntry(sender As Object, e As Tuple(Of String, BaseModule.LogLevel))
Select Case e.Item2
Case BaseModule.LogLevel.Info
AddInfoEntry(e.Item1)
Case BaseModule.LogLevel.Warn
AddWarnEntry(e.Item1)
Case BaseModule.LogLevel.Error
AddErrorEntry(e.Item1)
End Select
End Sub
Private Async Function frmMain_Closing(sender As Object, e As CancelEventArgs) As Threading.Tasks.Task Handles Me.Closing
Try
If Sync IsNot Nothing AndAlso Sync.IsLoggedIn Then
AddInfoEntry("Logging out..")
Await Sync.Cleanup()
End If
Catch ex As Exception
Logger.Error(ex)
AddWarnEntry($"Error while closing the application: {ex.Message}")
End Try
End Function
Private Sub StartTimer()
If Sync.TestConfigIsComplete() = False Then
AddInfoEntry("Configuration is incomplete. Stopping.")
AddDivider()
Else
AddInfoEntry("Starting timer..")
btnStopSync.Enabled = True
btnSyncStart.Enabled = False
SyncTimer.Enabled = True
End If
End Sub
Private Sub StopTimer()
AddInfoEntry("Stopping timer..")
btnStopSync.Enabled = False
btnSyncStart.Enabled = True
SyncTimer.Enabled = False
End Sub
Private Async Function Timer_Elapsed(sender As Object, e As System.EventArgs) As Threading.Tasks.Task Handles SyncTimer.Tick
btnForceSync.Enabled = False
Await Sync.Run()
btnForceSync.Enabled = True
End Function
Private Sub AddInfoEntry(pMessage As String, ParamArray pArgs As Object())
Logger.Info(pMessage, pArgs)
Dim oItem = New LogLine With {
.Message = String.Format(pMessage, pArgs),
.Level = LogLevel.Info
}
ListBoxControl1.Items.Add(oItem)
ListBoxControl1.MakeItemVisible(ListBoxControl1.Items.Count - 1)
End Sub
Private Sub AddWarnEntry(pMessage As String, ParamArray pArgs As Object())
Logger.Info(pMessage, pArgs)
Dim oItem = New LogLine With {
.Message = String.Format(pMessage, pArgs),
.Level = LogLevel.Warning
}
ListBoxControl1.Items.Add(oItem)
End Sub
Private Sub AddErrorEntry(pMessage As String, ParamArray pArgs As Object())
Logger.Info(pMessage, pArgs)
Dim oItem = New LogLine With {
.Message = String.Format(pMessage, pArgs),
.Level = LogLevel.Error
}
ListBoxControl1.Items.Add(oItem)
End Sub
Private Sub AddDivider()
ListBoxControl1.Items.Add(New LogLine With {
.Message = "-------------------------------------",
.Level = LogLevel.Info
})
End Sub
Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
Try
Dim oPath = LogConfig.LogDirectory
Process.Start("explorer.exe", oPath)
Catch ex As Exception
Logger.Error(ex)
End Try
End Sub
Private Sub btnSyncStart_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSyncStart.ItemClick
StartTimer()
End Sub
Private Sub btnStopSync_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnStopSync.ItemClick
StopTimer()
End Sub
Private Sub btnToggleWindow_Click(sender As Object, e As EventArgs) Handles btnToggleWindow.Click
ToggleWindow()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Close()
End Sub
Private Sub TrayIcon_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles TrayIcon.MouseDoubleClick
ToggleWindow()
End Sub
Private Sub ToggleWindow()
If Visible = True Then
Hide()
Else
WindowState = FormWindowState.Minimized
Show()
WindowState = FormWindowState.Normal
End If
End Sub
Private Async Sub btnForceSync_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnForceSync.ItemClick
btnForceSync.Enabled = False
Await Sync.Run()
btnForceSync.Enabled = True
End Sub
Private Sub ListBoxControl1_DrawItem(sender As Object, e As DevExpress.XtraEditors.ListBoxDrawItemEventArgs) Handles ListBoxControl1.DrawItem
Dim oItem As LogLine = DirectCast(e.Item, LogLine)
Select Case oItem.Level
Case LogLevel.Warning
e.Appearance.ForeColor = Color.DarkOrange
e.Appearance.FontStyleDelta = FontStyle.Bold
Case LogLevel.Error
e.Appearance.ForeColor = Color.DarkRed
e.Appearance.FontStyleDelta = FontStyle.Bold
End Select
End Sub
End Class