You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
8.9 KiB

Imports System.ServiceProcess
Imports System.Threading
Imports System.IO
Imports System.Reflection
Imports System.IO.File
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports System.ComponentModel
Imports System
Imports System.SystemException
Public Class Service1
#Region "Deklarationen"
Dim Watch_Directory As String
Dim FileWatch As New FileSystemWatcher()
Dim LogFileOK As String = ""
Dim LogFileNOK As String = ""
Dim LogFileDebug As String = ""
Dim WithEvents WatchTimer As New Timers.Timer()
#End Region
Protected Overrides Sub OnStart(ByVal args() As String)
'FileOpen(1, "D:\tssettings\Edoka\edkb14\Logs\Test.txt", OpenMode.Output)
'WriteLine(1, "Start")
Globals.Param = New Parameters
Dim hh, mm, ss As String
hh = Param.Startzeit.Substring(0, 2)
mm = Param.Startzeit.Substring(3, 2)
ss = Param.Startzeit.Substring(6, 2)
Globals.Startzeit = New Date(Now.Year, Now.Month, Now.Day, hh, mm, ss)
Globals.Endzeit = Globals.Startzeit.AddHours(Param.Laufzeit)
LogFileOK = Param.LogDir + Format(Now, "ddMMyyyy hhmmss") + "_OK.txt"
LogFileNOK = Param.LogDir + Format(Now, "ddMMyyyy hhmmss") + "_NOK.txt"
LogFileDebug = Param.LogDir + Format(Now, "ddMMyyyy hhmmss") + "_Debug.txt"
Globals.LogEntry = New Log(LogFileOK, LogFileNOK, LogFileDebug, Param.DebugMode)
LogEntry.Writelog(Log.Logtype.Debug, "Start EDKB14")
LogEntry.Writelog(Log.Logtype.Debug, "Parameter:")
LogEntry.Writelog(Log.Logtype.Debug, "Connectionstring EDOKA: " + Param.connectionstring)
LogEntry.Writelog(Log.Logtype.Debug, "Connectionstring Journale: " + Param.connectionstring_Journale)
LogEntry.Writelog(Log.Logtype.Debug, "Anz Dokumente:" + Param.AnzDokumente.ToString)
LogEntry.Writelog(Log.Logtype.Debug, "WorkDir: " + Param.WorkDir)
LogEntry.Writelog(Log.Logtype.Debug, "DSRDir: " + Param.DSRDir)
LogEntry.Writelog(Log.Logtype.Debug, "DokType Briefvorlage: " + Param.DokTypeBriefvorlage.ToString)
LogEntry.Writelog(Log.Logtype.Debug, "PSDir: " + Param.PSDir)
LogEntry.Writelog(Log.Logtype.Debug, "PDFDir " + Param.PDFDir)
LogEntry.Writelog(Log.Logtype.Debug, "LogDir: " + Param.LogDir)
LogEntry.Writelog(Log.Logtype.Debug, "PS Printer :" + Param.PSPrinter)
LogEntry.Writelog(Log.Logtype.Debug, "Distiller In Out Dirs:" + Param.Use_PDFInOutDir.ToString)
LogEntry.Writelog(Log.Logtype.Debug, "EDKB08 Dir: " + Param.EDKB08Dir)
LogEntry.Writelog(Log.Logtype.Debug, "Wait Loop:" + Param.WaitLoop.ToString)
LogEntry.Writelog(Log.Logtype.Debug, "Oracle ConString: " + Param.OracleConnectionString)
LogEntry.Writelog(Log.Logtype.Debug, "Adresse aus Oracle: " + Param.AdresseAbOracle.ToString)
LogEntry.Writelog(Log.Logtype.Debug, "Startzeit: " + Param.Startzeit.ToString)
LogEntry.Writelog(Log.Logtype.Debug, "Laufzeit in Std: " + Param.Laufzeit.ToString)
LogEntry.Writelog(Log.Logtype.Debug, "Mail Durchlauf: " + Param.Mailadresse.ToString)
LogEntry.Writelog(Log.Logtype.Debug, "Timer-Intervall in Millisekunden: " + Param.TimerIntevall.ToString)
LogEntry.Writelog(Log.Logtype.Debug, "CheckOffice 2010: " + Param.CheckOffice2010.ToString)
Globals.LogData.Columns.Add("Timestamp")
Globals.LogData.Columns.Add("Partner")
Globals.LogData.Columns.Add("VV")
Globals.LogData.Columns.Add("Banklagernd")
Me.WatchTimer.Interval = Globals.Param.TimerIntevall
Me.WatchTimer.Enabled = True
LogEntry.Writelog(Log.Logtype.Debug, "Berechnete Endzeit:" + Globals.Endzeit.ToString)
LogEntry.Writelog(Log.Logtype.Debug, "------------------------------------------------------------")
Exit Sub
Me.Watch_Directory = Param.DSRDir
If Not Init_Filewatcher() Then
LogEntry.Writelog(Log.Logtype.NOK, "Filewacher konnte nicht initialisiert werden")
End If
LogEntry.Writelog(Log.Logtype.Debug, "Init Filewatcher durch")
Me.Start_Watching()
'FileClose(1)
End Sub
Protected Overrides Sub OnStop()
' Hier Code zum Ausführen erforderlicher Löschvorgänge zum Beenden des Dienstes einfügen.
End Sub
#Region "Filewatcher"
Private Function Init_Filewatcher() As Boolean
Try
FileWatch.Path = Watch_Directory
FileWatch.IncludeSubdirectories = False
FileWatch.Filter = "*.xml"
Return True
Catch ex As Exception
LogEntry.Writelog(Log.Logtype.NOK, "Init Filewacher: " + ex.Message)
Return False
End Try
End Function
'''<summary>Eventhandler des FileWatching-Objektes aktivieren</summary>
Private Function Start_Watching() As Boolean
Try
AddHandler FileWatch.Created, New FileSystemEventHandler(AddressOf OnFileEvent)
FileWatch.EnableRaisingEvents = True
LogEntry.Writelog(Log.Logtype.Debug, "Filewatch Event-Handler initialisiert: " + Me.Watch_Directory)
Return True
Catch ex As Exception
LogEntry.Writelog(Log.Logtype.Debug, "Fehler bei der Event-Initialisierung " + ex.Message)
Return False
End Try
End Function
Dim EventStopped As Boolean = False
'''<summary>Aktivitäten im Inputverzeichnis verarbeiten</summary>
'''<remarks>Wird eine Datei mit der Endung .IND angeliefert, wird der Eventhandler
'''gestoppt und die anstehenden Dokumente verarbeitet.
'''
'''Nach abgeschlossener Verarbeitung wird der Eventhandler wieder
'''eingeschaltet</remarks>
'''<param name="source"></param>
'''<param name="e"></param>
Private Sub OnFileEvent(ByVal source As Object, ByVal e As FileSystemEventArgs)
If EventStopped = True Then
LogEntry.Writelog(Log.Logtype.Debug, "Neue Datei " + e.FullPath)
LogEntry.Writelog(Log.Logtype.Debug, "Event Stoped")
End If
Exit Sub
If UCase(Microsoft.VisualBasic.Right(e.FullPath, 4)) = ".XML" Then
FileWatch.EnableRaisingEvents = False
EventStopped = True
LogEntry.Writelog(Log.Logtype.Debug, "Neue Datei " + e.FullPath)
'ServiceIcon.Text = "Working..."
Threading.Thread.Sleep(2000)
ModMain.Main()
EventStopped = False
FileWatch.EnableRaisingEvents = True
End If
End Sub
Public Function WriteToEventLog(ByVal Entry As String, _
Optional ByVal AppName As String = "EDKB14WS", _
Optional ByVal EventType As _
EventLogEntryType = EventLogEntryType.Information, _
Optional ByVal LogName As String = "EDKB14WS") As Boolean
'*************************************************************
'PURPOSE: Write Entry to Event Log using VB.NET
'PARAMETERS: Entry - Value to Write
' AppName - Name of Client Application. Needed
' because before writing to event log, you must
' have a named EventLog source.
' EventType - Entry Type, from EventLogEntryType
' Structure e.g., EventLogEntryType.Warning,
' EventLogEntryType.Error
' LogName: Name of Log (System, Application;
' Security is read-only) If you
' specify a non-existent log, the log will be
' created
'RETURNS: True if successful, false if not
'EXAMPLES:
'1. Simple Example, Accepting All Defaults
' WriteToEventLog "Hello Event Log"
'2. Specify EventSource, EventType, and LogName
' WriteToEventLog("Danger, Danger, Danger", "MyVbApp", _
' EventLogEntryType.Warning, "System")
'
'NOTE: EventSources are tightly tied to their log.
' So don't use the same source name for different
' logs, and vice versa
'******************************************************
Dim objEventLog As New EventLog()
Try
'Register the App as an Event Source
If Not objEventLog.SourceExists(AppName) Then
objEventLog.CreateEventSource(AppName, LogName)
End If
objEventLog.Source = AppName
'WriteEntry is overloaded; this is one
'of 10 ways to call it
objEventLog.WriteEntry(Entry, EventType)
Return True
Catch Ex As Exception
Return False
End Try
End Function
#End Region
Private Sub WatchTimer_Elap()
End Sub
Private Sub WatchTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles WatchTimer.Elapsed
LogEntry.Writelog(Log.Logtype.Debug, "Timer Elapsed")
If Globals.In_Runtime Then
Me.WatchTimer.Enabled = False
ModMain.Main()
End If
Me.WatchTimer.Enabled = True
End Sub
End Class