Imports System.Collections.ObjectModel Imports System.ComponentModel Imports System.Data Imports System.Data.SqlClient Imports System.Runtime.CompilerServices Imports System.Windows.Threading Imports Prism.Commands Imports Prism.Mvvm Imports RMG_Strategist Public Class MainViewModel : Implements INotifyPropertyChanged Dim _OverviewTabActive As Boolean Dim _StrategyTabActive As Boolean Dim _Connection As Boolean ' Dim _Vers As String = "Disconnected" Dim _activeViewModel As Object Dim _LoadingProgress As Integer Dim _ProgressBarVisibility As Boolean Dim _DBRawTable As DataTable Dim _DBObservationTimer As Threading.Timer '--- Child View Models Dim _Overview As New OverviewViewModel() Dim _Strategy As New StrategyViewModel() '--- Commands --- Dim _EndCommand As DelegateCommand Dim _OptionsCommand As DelegateCommand Dim _ConnectCommand As DelegateCommand Dim _DisconnectCommand As DelegateCommand Dim _RefreshTestCommand As DelegateCommand #Region "INotifyPropertyChanged Implementation" Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Private Sub NotifyPropertyChanged( Optional ByVal propertyName As String = Nothing) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName)) End Sub #End Region #Region "Properties" Public Property OverViewTabActive As Boolean Get Return _OverviewTabActive End Get Set(value As Boolean) _OverviewTabActive = value If _OverviewTabActive Then Me.ActiveViewModel = Overview End If NotifyPropertyChanged() End Set End Property Public Property StrategyTabActive As Boolean Get Return _StrategyTabActive End Get Set(value As Boolean) _StrategyTabActive = value If StrategyTabActive Then Me.ActiveViewModel = Strategy End If NotifyPropertyChanged() End Set End Property Public Property EndCommand As DelegateCommand Get Return _EndCommand End Get Set(value As DelegateCommand) _EndCommand = value End Set End Property Public Property ActiveViewModel As Object Get Return _activeViewModel End Get Set(value As Object) _activeViewModel = value NotifyPropertyChanged() End Set End Property Public Property OptionsCommand As DelegateCommand Get Return _OptionsCommand End Get Set(value As DelegateCommand) _OptionsCommand = value NotifyPropertyChanged() End Set End Property Public Property ConnectCommand As DelegateCommand Get Return _ConnectCommand End Get Set(value As DelegateCommand) _ConnectCommand = value NotifyPropertyChanged() End Set End Property Public Property Vers As String Get Return _Vers End Get Set(value As String) _Vers = value NotifyPropertyChanged() End Set End Property Public Property Connection As Boolean Get Return _Connection End Get Set(value As Boolean) _Connection = value NotifyPropertyChanged() End Set End Property Public Property DisconnectCommand As DelegateCommand Get Return _DisconnectCommand End Get Set(value As DelegateCommand) _DisconnectCommand = value NotifyPropertyChanged() End Set End Property Public Property LoadingProgress As Integer Get Return _LoadingProgress End Get Set(value As Integer) _LoadingProgress = value NotifyPropertyChanged() End Set End Property Public Property ProgressBarVisibility As Boolean Get Return _ProgressBarVisibility End Get Set(value As Boolean) _ProgressBarVisibility = value NotifyPropertyChanged() End Set End Property Public Property RefreshTestCommand As DelegateCommand Get Return _RefreshTestCommand End Get Set(value As DelegateCommand) _RefreshTestCommand = value NotifyPropertyChanged() End Set End Property Public Property Overview As OverviewViewModel Get Return _Overview End Get Set(value As OverviewViewModel) _Overview = value NotifyPropertyChanged() End Set End Property Public Property Strategy As StrategyViewModel Get Return _Strategy End Get Set(value As StrategyViewModel) _Strategy = value NotifyPropertyChanged() End Set End Property #End Region Public Sub New() EndCommand = New DelegateCommand(AddressOf CloseApp) OptionsCommand = New DelegateCommand(AddressOf ShowOptions) ConnectCommand = New DelegateCommand(AddressOf ConnectDatabase) DisconnectCommand = New DelegateCommand(AddressOf DisconnectDatabase) RefreshTestCommand = New DelegateCommand(AddressOf RefreshPlot) ' _DBObservationTimer = New System.Threading.Timer(AddressOf WatchDB, Nothing, Threading.Timeout.Infinite, 3000) _DBRawTable = New DataTable() LoadingProgress = 0 Overview = New OverviewViewModel() Strategy = New StrategyViewModel() OverViewTabActive = False StrategyTabActive = True Me.ActiveViewModel = Strategy End Sub Private Sub RefreshPlot() _Strategy.Model.InvalidatePlot(True) End Sub Private Sub WatchDB(state As Object) '_DBObservationTimer.Change(Threading.Timeout.Infinite, Threading.Timeout.Infinite) Dim dt As DataTable = query_DataTable("SELECT * FROM timingdata") Dim finalDT As DataTable = _DBRawTable 'finalDT.Merge(dt) 'finalDT.AcceptChanges() 'finalDT.Merge(_DBRawTable) 'finalDT = finalDT.GetChanges() If finalDT IsNot Nothing Then _DBRawTable = dt '_Overview.OverviewTable = query_DataTable("select * from timingdata") _DBRawTable = query_DataTable("SELECT * FROM timingdata") FillDataEntities(_DBRawTable) End If _Strategy.RefreshPlot() '_DBObservationTimer.Change(0, 3000) End Sub Private Sub DisconnectDatabase() DBcon.Close() If DBcon.State <> ConnectionState.Open Then b_connected = False Connection = False Vers = "Disconnected" End If End Sub Private Sub ShowOptions() Dim OptionWindow As New Options OptionWindow.Show() End Sub Private Sub CloseApp() 'Dim res As MessageBoxResult = MessageBox.Show("Are you sure you want to quit " & AppName & "?", "Quit", MessageBoxButton.YesNo, MessageBoxImage.Question) 'If res = MessageBoxResult.Yes Then ' Application.Current.MainWindow.Close() 'End If Application.Current.MainWindow.Close() End Sub Private Sub ConnectDatabase() 'Try ' DBcon.ConnectionString = "Server=" & My.Settings.DatabaseIP & "; Database=" & database & "; User Id=" & My.Settings.DBUsername & "; Password=" & My.Settings.DBPassword & ";" ' DBcon.Open() 'Catch ' MessageBox.Show("An unexpected error occured when trying to connect to database. Please check connection settings and availabilty of databases", "Connection error", MessageBoxButton.OK, MessageBoxImage.Error) 'End Try 'If (DBcon.State <> System.Data.ConnectionState.Open) Then ' MessageBox.Show("Connection not open!") ' Connection = False 'Else ' MessageBox.Show("Connection open!") ' Connection = True 'End If If My.Settings.DBUsername = "" Or My.Settings.DBPassword = "" Then MsgBox("Please fill al Fields") Else dns = "Data Source=" & My.Settings.DatabaseIP & ";database=tracknote_test;UID=tracknote;pwd=tracknote;Allow User Variables=True" DBcon.ConnectionString = dns SQLCmd.Connection = DBcon b_connected = True Connection = True Vers = query_string("select ver_date from admin") 'ada = New MySqlDataAdapter("select ugroup, userid, logonname, password from users where logonname = '" & TB_UNAME.Text & "' ", con) 'Try ' con.Open() ' Table = New Data.DataTable ' ada.Fill(Table) ' con.Close() ' If Table.Rows.Count = 1 Then ' ugroup = CStr(Table.Rows(0).Item(0)) ' userid = CStr(Table.Rows(0).Item(1)) ' logonname = CStr(Table.Rows(0).Item(2)) ' password = CStr(Table.Rows(0).Item(3)) ' Else ' MsgBox("The password or username you have entered is wrong." & vbNewLine & "Pleas try again..") ' Exit Sub ' End If ' If password <> TB_PW.Text Then ' trys = trys + 1 ' If trys = 3 Then ' MsgBox("Too many wrong trys, good bye...") ' Me.Close() ' End If ' MsgBox("The password or username you have entered is wrong." & vbNewLine & "Pleas try again..") ' Exit Sub ' End If ' inifile.WertSchreiben("Usersettings", "username", TB_UNAME.Text) ' inifile.WertSchreiben("Usersettings", "last selected database", database) 'Catch ex As Exception ' con.Close() ' MessageBox.Show("Connection failed, " & ex.Message) ' LogMessage("Error: " & ex.Message) ' Exit Sub 'End Try 'LogMessage("User " & TB_UNAME.Text & " with userid " & userid & " successfully connected to " & database & ", usergroup is " & ugroup) 'databasename = serverip & " and " & database 'check_sw_version() 'If b_iniwrite Then ' inifile.WertSchreiben("Usersettings", "username", TB_UNAME.Text) ' inifile.WertSchreiben("Usersettings", "last selected database", database) ' If RB_MANUAL.Checked Then inifile.WertSchreiben("Usersettings", "manual server ip", MTB_MANUIP.Text) ' If RB_HOMESERVER.Checked Then inifile.WertSchreiben("Usersettings", "homeserverip", TB_HOMESERVERIP.Text) ' If RB_TRACKSERVER.Checked Then inifile.WertSchreiben("Usersettings", "trackserverip", TB_TRACKSERVERIP.Text) ' Form2_Main.Show() ' Form3_eventbrowser.Show() ' Hide() 'Else ' Close() 'End If Dim dt As New DataTable() Task.Factory.StartNew(Sub() '_Overview.OverviewTable = query_DataTable("select * from timingdata") _DBRawTable = query_DataTable("select * from timingdata") FillDataEntities(_DBRawTable) End Sub) 'If dt IsNot Nothing Then ' _Overview.OverviewTable = dt 'End If End If End Sub Private Sub FillDataEntities(dt As Data.DataTable) 'Create all available Car entities If dt IsNot Nothing Then LstCars = ExtractCarEntities(dt) Overview.OverviewCollection = LstCars '_DBObservationTimer.Change(0, 3000) End If End Sub Private Function ExtractCarEntities(dt As Data.DataTable) As ObservableCollection(Of Car) Dim Cars As New ObservableCollection(Of Car) Try ProgressBarVisibility = True Dim currentCar As Car Dim maxRows As Integer = dt.Rows.Count Dim rowCount As Integer = 0 'For Each row As DataRow In dt.Rows ' Dim startNo As String = row("StartNo") ' If (Cars.Where(Function(x) x.StartNo = startNo)).Count = 0 Then ' currentCar = New Car() ' currentCar.StartNo = row("StartNo") ' currentCar.DriverShort = row("Short") ' currentCar.DriverLastName = row("LastName") ' currentCar.DriverFirstName = row("FirstName") ' currentCar.Team = row("Team") ' currentCar.CarName = row("Car") ' currentCar.LstLaps.Add(AssignLap(row)) ' Cars.Add(currentCar) ' Else ' Cars.First(Function(x) x.StartNo = startNo).LstLaps.Add(AssignLap(row)) ' End If ' rowCount = rowCount + 1 ' LoadingProgress = CType(Math.Round((rowCount * 100) \ maxRows, 0), Integer) 'Next Dim table As DataTable = query_DataTable("SELECT DISTINCT(StartNo), Short, LastName, FirstName, Team, Car FROM timingdata ORDER BY LastName ASC") maxRows = dt.Rows.Count For Each row As DataRow In table.Rows 'Dim startNo As String = row("StartNo") 'If (Cars.Where(Function(x) x.StartNo = startNo)).Count = 0 Then currentCar = New Car() currentCar.StartNo = row("StartNo") currentCar.DriverShort = row("Short") currentCar.DriverLastName = row("LastName") currentCar.DriverFirstName = row("FirstName") currentCar.Team = row("Team") currentCar.CarName = row("Car") Cars.Add(currentCar) Next For Each car As Car In Cars maxRows = Cars.Count Dim timeTable As DataTable = query_DataTable("SELECT Lap, Time, LapTime, TimeS1, TimeS2, TimeS3, InPit, Cancelled, PitStopDuration, PitInTime, LapText FROM timingdata WHERE StartNo = '" & car.StartNo & "'") For Each dr As DataRow In timeTable.Rows Dim lap As New Lap() lap.LapNo = dr("Lap") lap.DayTime = Convert.ToDateTime(dr("Time")) lap.TimeS1 = dr("TimeS1") lap.TimeS2 = dr("TimeS2") lap.TimeS3 = dr("TimeS3") 'lap.InPit = ConvertStringBool(dr("InPit")) lap.Cancelled = dr("Cancelled") lap.PitStopDuration = dr("PitStopDuration") lap.PitInTime = Convert.ToDateTime(dr("PitInTime")) lap.PitInTime = Convert.ToDateTime(dr("PitInTime")) lap.LapText = dr("LapText") car.LstLaps.Add(lap) Next rowCount = rowCount + 1 LoadingProgress = CType(Math.Round((rowCount * 100) \ maxRows, 0), Integer) Next ProgressBarVisibility = False Return Cars Catch ex As Exception LogMessage("Error when trying to create Car entities from datatable: " & ex.Message) ProgressBarVisibility = False Return Nothing End Try End Function 'Private Function AssignLap(dr As DataRow) As Lap ' Dim currentLap As New Lap() ' If dr IsNot Nothing Then ' 'If Not Integer.TryParse(dr("Lap"), currentLap.LapNo) Then ' ' currentLap.LapNo = 0 ' 'End If ' If dr("Lap") IsNot DBNull.Value Then ' currentLap.LapNo = dr("Lap") ' End If ' If Not Date.TryParse(dr("Time").ToString(), currentLap.DayTime) Then ' currentLap.DayTime = Nothing ' End If ' If dr("LapTime") IsNot DBNull.Value Then ' currentLap.LapTime = dr("LapTime") ' End If ' If dr("TimeS1") IsNot DBNull.Value Then ' currentLap.TimeS1 = dr("TimeS1") ' End If ' If dr("TimeS2") IsNot DBNull.Value Then ' currentLap.TimeS2 = dr("TimeS2") ' End If ' If dr("TimeS3") IsNot DBNull.Value Then ' currentLap.TimeS3 = dr("TimeS3") ' End If ' If Not Date.TryParse(dr("PitInTime").ToString(), currentLap.PitInTime) Then ' currentLap.PitInTime = Nothing ' End If ' If Not TimeSpan.TryParse(dr("PitStopDuration").ToString(), currentLap.PitStopDuration) Then ' currentLap.PitStopDuration = Nothing ' End If ' currentLap.Cancelled = ConvertStringBool(dr("Cancelled")) ' currentLap.InPit = ConvertStringBool("InPit") ' currentLap.LapText = dr("LapText").ToString() ' End If ' Return currentLap 'End Function Private Function ConvertStringBool(bool As String) As Boolean Dim result As Boolean = False Try Select Case bool Case "J" Or "j" result = 1 Case "N" Or "n" result = 0 End Select Return result Catch ex As Exception Return result LogMessage("Error during conversion of boolean string: " & ex.Message) End Try End Function End Class