initialize
This commit is contained in:
149
interface/asm/dotnet/ItacTools/DirectoryMonitor.cs
Normal file
149
interface/asm/dotnet/ItacTools/DirectoryMonitor.cs
Normal file
@@ -0,0 +1,149 @@
|
||||
#region Namespace
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Xml.Serialization;
|
||||
#endregion
|
||||
|
||||
namespace com.itac.mes.tools
|
||||
{
|
||||
public class DeserializedEventArgs
|
||||
{
|
||||
public DeserializedEventArgs(object deserializedObject) { DeserializedObject = deserializedObject; }
|
||||
public object DeserializedObject { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Observes the directory local ./xml.
|
||||
/// Whenever a new file (only *.xml files) was read the content is interpreted as xml.
|
||||
/// The (simplified) first tag is treated as a simple typeName. If this simpleType name was
|
||||
/// added as a type to the local map (typeDict) the xml stream will be deserialized.
|
||||
/// If deserialisation was successful the instance is reported to the event handler.
|
||||
///
|
||||
/// </summary>
|
||||
public class DirectoryMonitor
|
||||
{
|
||||
|
||||
private bool _isShutdown = false;
|
||||
private Dictionary<string, Type> typeDict;
|
||||
|
||||
public delegate void DeserializedObjectEventHandler(object sender, object deserializedObject);
|
||||
|
||||
public event DeserializedObjectEventHandler deserializedObjectEvent;
|
||||
protected void raiseObjectDeserialized(object deserializedObject)
|
||||
{
|
||||
if (deserializedObjectEvent != null) deserializedObjectEvent(this, deserializedObject);
|
||||
}
|
||||
|
||||
public void addType(Type typeToSerialize)
|
||||
{
|
||||
if (typeDict == null)
|
||||
{
|
||||
typeDict = new Dictionary<string, Type>();
|
||||
}
|
||||
typeDict.Add(typeToSerialize.Name, typeToSerialize);
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
var thread = new Thread(Run);
|
||||
thread.Start();
|
||||
}
|
||||
|
||||
public void Run()
|
||||
{
|
||||
Thread.CurrentThread.Name = "DirectoryMonitor";
|
||||
while (!_isShutdown)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Directory.GetFiles() returns an array of strings which are not just
|
||||
// the file/directory name but the whole path to that file folder.
|
||||
string[] fileList = Directory.GetFiles("./xml");
|
||||
for (int i = 0; i < fileList.Length; i++)
|
||||
{
|
||||
if (File.Exists(fileList[i]))
|
||||
{
|
||||
//File Info is a Class which extend FileSystemInfo class.
|
||||
FileInfo finfo = new FileInfo(fileList[i]);
|
||||
//finfo.Length returns the File size.
|
||||
if (fileList[i].ToLower().EndsWith("xml"))
|
||||
{
|
||||
LogHandler.log(Constants.LOGGER, TraceEventType.Information, "FILE: " + fileList[i] + " :Size>" + finfo.Length);
|
||||
if (File.Exists(fileList[i]))
|
||||
{
|
||||
using (StreamReader myFile = new StreamReader(fileList[i], System.Text.Encoding.Default))
|
||||
{
|
||||
string sContent = myFile.ReadToEnd();
|
||||
|
||||
// solange weiter lesen, bis eine Zeile erkannt wird, die nicht mit <? oder <!-- anfängt
|
||||
int startOffset = sContent.IndexOf("<");
|
||||
sContent = sContent.Substring(startOffset);
|
||||
while (sContent.StartsWith("<?") || sContent.StartsWith("<!--"))
|
||||
{
|
||||
startOffset = sContent.IndexOf("<", 1);
|
||||
sContent = sContent.Substring(startOffset);
|
||||
}
|
||||
|
||||
myFile.BaseStream.Seek(startOffset, SeekOrigin.Begin);
|
||||
myFile.BaseStream.Position = startOffset;
|
||||
|
||||
byte[] byteArray = Encoding.ASCII.GetBytes(sContent);
|
||||
var stream = new MemoryStream(byteArray);
|
||||
// convert stream to string
|
||||
var reader = new StreamReader(stream);
|
||||
|
||||
myFile.BaseStream.Seek(startOffset, SeekOrigin.Begin);
|
||||
myFile.BaseStream.Position = startOffset;
|
||||
// root tag is simple class Name
|
||||
try
|
||||
{
|
||||
String typeName = sContent.Substring(sContent.IndexOf("<") + 1, sContent.IndexOf(">") - 1);
|
||||
if (typeDict.ContainsKey(typeName))
|
||||
{
|
||||
Type type = typeDict[typeName];
|
||||
XmlSerializer serializer = new XmlSerializerFactory().CreateSerializer(type);
|
||||
object deserializedObject = serializer.Deserialize(reader);
|
||||
raiseObjectDeserialized(deserializedObject);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LogHandler.log(Constants.LOGGER, TraceEventType.Error, "error processing xml file, ", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
finfo.Delete();
|
||||
}
|
||||
else
|
||||
{
|
||||
LogHandler.log(Constants.LOGGER, TraceEventType.Information, "FILE: " + fileList[i] + " has unsupported extension");
|
||||
}
|
||||
}
|
||||
}
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LogHandler.log(Constants.LOGGER, TraceEventType.Error, "error processing xml file, " + e.Message);
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string getFileContent(string v)
|
||||
{
|
||||
string sContent = "";
|
||||
try
|
||||
{
|
||||
|
||||
}
|
||||
catch (Exception) { }
|
||||
return sContent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user