183 lines
6.8 KiB
C#
183 lines
6.8 KiB
C#
//
|
|
// Class: transmitter
|
|
// Current CLR: 4.0.30319.34209
|
|
// System: Microsoft Visual Studio 10.0
|
|
// Author: dani
|
|
// Created: 7/14/2015 7:39:29 AM
|
|
//
|
|
// Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved.
|
|
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Xml.Serialization;
|
|
using bsmd.database;
|
|
using log4net;
|
|
|
|
namespace bsmd.hisnord
|
|
{
|
|
public class transmitter
|
|
{
|
|
private static readonly ILog _log = LogManager.GetLogger(typeof(transmitter));
|
|
private static int? processId; // Achtung, das müsste getrennt behandelt werden Test <-> Livesystem!
|
|
private static int? processTestId;
|
|
|
|
public static void CallTransmitter(bool useTest)
|
|
{
|
|
string rootDir = useTest ? Properties.Settings.Default.TestTransmitterRoot : Properties.Settings.Default.LiveTransmitterRoot;
|
|
|
|
if(!useTest && processId.HasValue)
|
|
{
|
|
_log.InfoFormat("Transmitter process {0} still running, aborting call", processId);
|
|
return;
|
|
}
|
|
|
|
if(useTest && processTestId.HasValue)
|
|
{
|
|
_log.InfoFormat("Transmitter process (Test system) {0} still running, aborting call", processId);
|
|
return;
|
|
}
|
|
|
|
ProcessStartInfo startInfo = new ProcessStartInfo(Path.Combine(rootDir, Properties.Settings.Default.Transmitter));
|
|
startInfo.WorkingDirectory = rootDir;
|
|
startInfo.RedirectStandardError = true;
|
|
startInfo.RedirectStandardOutput = true;
|
|
startInfo.RedirectStandardInput = false;
|
|
startInfo.UseShellExecute = false;
|
|
// der Transmitter schickt alles was im Ausgabe-Verzeichnis ist
|
|
// damit das gut geht schicken wir die Nachrichten einzeln und arbeiten jeweils das
|
|
// Ergebnis ab
|
|
|
|
using (Process transmitterProcess = new Process())
|
|
{
|
|
if (useTest)
|
|
{
|
|
transmitterProcess.Exited += TransmitterTestProcess_Exited;
|
|
}
|
|
else
|
|
{
|
|
transmitterProcess.Exited += TransmitterProcess_Exited;
|
|
}
|
|
|
|
transmitterProcess.ErrorDataReceived += TransmitterProcess_ErrorDataReceived;
|
|
transmitterProcess.OutputDataReceived += TransmitterProcess_OutputDataReceived;
|
|
|
|
transmitterProcess.StartInfo = startInfo;
|
|
transmitterProcess.EnableRaisingEvents = true;
|
|
transmitterProcess.Start();
|
|
|
|
transmitterProcess.BeginErrorReadLine();
|
|
transmitterProcess.BeginOutputReadLine();
|
|
|
|
int aProcessId = transmitterProcess.Id;
|
|
|
|
if (useTest)
|
|
{
|
|
processTestId = aProcessId;
|
|
}
|
|
else
|
|
{
|
|
processId = aProcessId;
|
|
}
|
|
|
|
// _log.DebugFormat("started {0}", transmitterProcess.ProcessName);
|
|
int timeout = Properties.Settings.Default.BatchTimeoutMins * 1000 * 60; // convert to ms
|
|
if (!transmitterProcess.WaitForExit((timeout == 0) ? int.MaxValue : timeout))
|
|
{
|
|
_log.WarnFormat("Transmitter process not exited within {0} minute", Properties.Settings.Default.BatchTimeoutMins);
|
|
// dauert zu lang, beende Prozess..
|
|
try
|
|
{
|
|
transmitterProcess.Kill();
|
|
_log.Info("Transmitter killed");
|
|
if (useTest) processTestId = null;
|
|
else processId = null;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_log.WarnFormat("Killing Transmitter failed: {0}", e.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void TransmitterProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
|
|
{
|
|
if(!e.Data.IsNullOrEmpty())
|
|
_log.Debug(e.Data);
|
|
}
|
|
|
|
private static void TransmitterProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
|
|
{
|
|
if(!e.Data.IsNullOrEmpty())
|
|
_log.Error(e.Data);
|
|
}
|
|
|
|
private static void TransmitterTestProcess_Exited(object sender, EventArgs e)
|
|
{
|
|
_log.Debug("Transmitter process (test) exited");
|
|
processTestId = null;
|
|
}
|
|
|
|
private static void TransmitterProcess_Exited(object sender, EventArgs e)
|
|
{
|
|
_log.Debug("Transmitter process exited");
|
|
processId = null;
|
|
}
|
|
|
|
public static result GetResult(string filenameFullPath, bool useTest)
|
|
{
|
|
// now we should read the response message...
|
|
string resultFilename = string.Format("{0}.result.xml", Path.GetFileName(filenameFullPath));
|
|
|
|
string resultDir = Path.Combine(useTest ? Properties.Settings.Default.TestTransmitterRoot : Properties.Settings.Default.LiveTransmitterRoot,
|
|
Properties.Settings.Default.ResultDir);
|
|
|
|
string resultFullPath = Path.Combine(resultDir, resultFilename);
|
|
return result.ReadResult(resultFullPath);
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// class to read transmitter result xml files
|
|
/// </summary>
|
|
[Serializable]
|
|
public class result
|
|
{
|
|
public result() { }
|
|
public int code { get; set; }
|
|
public string message { get; set; }
|
|
public string detail { get; set; }
|
|
|
|
/// <summary>
|
|
/// create result items from file
|
|
/// </summary>
|
|
public static result ReadResult(string filename)
|
|
{
|
|
result aResult = null;
|
|
try
|
|
{
|
|
XmlSerializer serializer = new XmlSerializer(typeof(result));
|
|
if (!File.Exists(filename))
|
|
{
|
|
_log.WarnFormat("Expected file {0} does not exist!", filename);
|
|
}
|
|
else
|
|
{
|
|
using (FileStream fs = new FileStream(filename, FileMode.Open))
|
|
{
|
|
aResult = (result)serializer.Deserialize(fs);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.ErrorFormat("Exception deserializing transmitter result: {0}", ex.Message);
|
|
}
|
|
return aResult;
|
|
}
|
|
}
|
|
}
|
|
}
|