Newer
Older
' Copyright 2014 European Union.
' Licensed under the EUPL (the 'Licence');
'
' * You may not use this work except in compliance with the Licence.
' * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl
' * Unless required by applicable law or agreed to in writing,
' software distributed under the Licence is distributed on an "AS IS" basis,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'
' See the LICENSE.txt for the specific language governing permissions and limitations.

Michael KRISPER
committed
Imports System.ComponentModel
Imports System.IO
Imports System.Text
Imports vectolic
Public Module VECTO_Global

Michael KRISPER
committed
Public COREvers As String = "NOT FOUND"
Public Const LicSigAppCode As String = "VECTO-Release-0093C61E0A2E4BFA9A7ED7E729C56AE4"
Public MyAppPath As String
Public MyConfPath As String

Markus Quaritsch
committed

Michael KRISPER
committed
'to ensure correct format for backgroundworker thread

Michael KRISPER
committed
Public Cfg As Configuration
'Public sKey As csKey
Public ReadOnly FileFormat As Encoding = Encoding.UTF8

Michael KRISPER
committed
Public Lic As cLicense
'Public VSUM As cVSUM

Markus Quaritsch
committed
'Public DEV As cDEV
Public ProgBarCtrl As ProgressbarControl

Michael KRISPER
committed
''' <summary>
''' Converts engine speed and torque to power.
''' </summary>
''' <param name="nU">engine speed</param>
''' <param name="M">Torque</param>
''' <returns>Power</returns>
''' <remarks></remarks>
Public Function nMtoPe(nU As Double, M As Double) As Double
Return (nU * 2 * Math.PI / 60) * M / 1000

Michael KRISPER
committed
Private LOGstream As StreamWriter
Public Function StartLog() As Boolean
Try
LOGstream = My.Computer.FileSystem.OpenTextFileWriter(MyAppPath & "LOG.txt", True, FileFormat)
LOGstream.AutoFlush = True
WriteToLog(MessageType.Normal, "Starting Session " & Now)
WriteToLog(MessageType.Normal, "VECTO " & VECTOvers)
Catch ex As Exception
Return False
End Try

Michael KRISPER
committed
Dim logfDetail As FileInfo
'Start new log if file size limit reached

Michael KRISPER
committed
If File.Exists(MyAppPath & "LOG.txt") Then
'File size check
logfDetail = My.Computer.FileSystem.GetFileInfo(MyAppPath & "LOG.txt")

Michael KRISPER
committed
If logfDetail.Length / (2 ^ 20) > Cfg.LogSize Then
WriteToLog(MessageType.Normal, "Starting new logfile")

Michael KRISPER
committed
If File.Exists(MyAppPath & "LOG_backup.txt") Then File.Delete(MyAppPath & "LOG_backup.txt")
File.Move(MyAppPath & "LOG.txt", MyAppPath & "LOG_backup.txt")
Catch ex As Exception
BackUpError = True
End Try
WriteToLog(MessageType.Err, "Failed to backup logfile! (" & MyAppPath & "LOG_backup.txt)")
WriteToLog(MessageType.Normal, "Logfile restarted. Old log saved to LOG_backup.txt")
Public Function CloseLog() As Boolean
Try
WriteToLog(MessageType.Normal, "Closing Session " & Now)
LOGstream.Close()
Catch ex As Exception
Return False
End Try
Public Function WriteToLog(MsgType As MessageType, Msg As String) As Boolean
Case MessageType.Err
Case MessageType.Warn
MsgTypeStr = "Warning"
Case Else
MsgTypeStr = "-"
End Select
Try
LOGstream.WriteLine(Now.ToString("yyyy/MM/dd-HH:mm:ss") & vbTab & MsgTypeStr & vbTab & Msg)
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
#Region "File path functions"
'When no path is specified, then insert either HomeDir or MainDir Special-folders

Michael KRISPER
committed
Public Function fFileRepl(file As String, Optional ByVal MainDir As String = "") As String
Dim ReplPath As String
'Trim Path
file = Trim(file)
'If empty file => Abort
If file = "" Then Return ""
file = Replace(file, Constants.DefVehPath & "\", MyAppPath & "Default Vehicles\", 1, -1,

Michael KRISPER
committed
CompareMethod.Text)
file = Replace(file, Constants.HomePath & "\", MyAppPath, 1, -1, CompareMethod.Text)
'Replace - Determine folder
If MainDir = "" Then
ReplPath = MyAppPath
Else
ReplPath = MainDir
End If
' "..\" => One folder-level up
Do While ReplPath.Length > 0 AndAlso Left(file, 3) = "..\"
ReplPath = fPathUp(ReplPath)
file = file.Substring(3)
Loop
Else
Return file
End If
End Function
'Path one-level-up "C:\temp\ordner1\" >> "C:\temp\"

Michael KRISPER
committed
Private Function fPathUp(Pfad As String) As String
Dim x As Integer
Pfad = Pfad.Substring(0, Pfad.Length - 1)
x = Pfad.LastIndexOf("\", StringComparison.Ordinal)

Michael KRISPER
committed
If x = -1 Then Return ""
Return Pfad.Substring(0, x + 1)
End Function
'File name without the path "C:\temp\TEST.txt" >> "TEST.txt" oder "TEST"

Markus Quaritsch
committed
Public Function GetFilenameWithoutPath(file As String, includeFileExtension As Boolean) As String _
'GetFilenameWithoutPath
Dim x As Integer
x = file.LastIndexOf("\", StringComparison.Ordinal) + 1
file = Right(file, Len(file) - x)
If Not includeFileExtension Then
x = file.LastIndexOf(".", StringComparison.Ordinal)
If x > 0 Then file = Left(file, x)
'Filename without extension "C:\temp\TEST.txt" >> "C:\temp\TEST"
'Filename without path if Path = WorkDir or MainDir

Markus Quaritsch
committed
Public Function GetFilenameWithoutDirectory(file As String, Optional ByVal mainDir As String = "") As String _
'GetFilenameWithoutDirectory
If UCase(GetPath(file)) = UCase(path) Then file = GetFilenameWithoutPath(file, True)
'Path alone "C:\temp\TEST.txt" >> "C:\temp\"
' "TEST.txt" >> ""

Markus Quaritsch
committed
Public Function GetPath(file As String) As String 'GetPath
Dim x As Integer
If file Is Nothing OrElse file.Length < 3 OrElse file.Substring(1, 2) <> ":\" Then Return ""
x = file.LastIndexOf("\", StringComparison.Ordinal)
Return Left(file, x + 1)
'Extension alone "C:\temp\TEST.txt" >> ".txt"

Markus Quaritsch
committed
Public Function GetExtension(file As String) As String 'GetExtension
Dim x As Integer

Michael KRISPER
committed
If x = -1 Then
Module Constants
'Public ReadOnly AUX As AuxiliaryKey
Public Const HomePath As String = "<HOME>"
Public Const DefVehPath As String = "<VEHDIR>"
Public Const NoFile As String = "<NOFILE>"
'Public Sub New()

Markus Quaritsch
committed
' AUX = New AuxiliaryKey
'End Sub
' ReSharper disable once ClassNeverInstantiated.Global

Markus Quaritsch
committed
End Module