天津投入产出系统后端
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.

158 lines
6.9 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.FJC.Entity.FileCopy;
namespace ProcessFileSyncService.FileSynchronizers
{
public class HaitianInjectionFileSynchronizer:FileSynchronizer
{
string _File_Copy_Path = "C:\\";
System.Threading.Timer timer;
public HaitianInjectionFileSynchronizer(QMAPP.FJC.Entity.Basic.MachineInfo machien)
: base(machien)
{
_File_Copy_Path = System.Configuration.ConfigurationManager.AppSettings["File_Copy_Path"];
if (!string.IsNullOrWhiteSpace(System.Configuration.ConfigurationManager.AppSettings["File_Copy_Path_" + this.Machine.MACHINECODDE]))
{
_File_Copy_Path = System.Configuration.ConfigurationManager.AppSettings["File_Copy_Path_" + this.Machine.MACHINECODDE];
}
timer = new System.Threading.Timer(new System.Threading.TimerCallback(Tick));
}
private void Tick(object obj)
{
try
{
QMAPP.FJC.BLL.FileCopy.FileCopyRecordBLL fcrbll = new QMAPP.FJC.BLL.FileCopy.FileCopyRecordBLL();
FileCopyRecord fcr = fcrbll.GetLastRecord(this.Machine.MACHINECODDE);
if (fcr == null)
{
fcr = new FileCopyRecord
{
EQUIPMENT_INFO_KEY = this.Machine.PID,
FILE_HOSTNAME = this.Machine.IPADDRESS,
RECORD_EQUIPMENT_CODE = this.Machine.MACHINECODDE,
RECORD_EQUIPMENT_DIRECTORY = this.Machine.EQUIPMENT_DIRECTORY,
RECORD_EQUIPMENT_TYPE_CODE = this.Machine.EQUIPMENT_TYPE_CODE,
RECORD_KEY = Guid.NewGuid().ToString(),
RECORD_READFLAG = "0",
FILE_LASTWRITETIME = DateTime.Parse("1975-1-1"),
IsNewInfo = true
};
}
StringBuilder sql = new StringBuilder();
using (IdentityScope idscope = new IdentityScope(this.Machine.USERNAME, this.Machine.IPADDRESS, this.Machine.PASSWORD))
{
IEnumerable<System.IO.FileInfo> files = Common.FileHelper.GetFileInfos(Machine.EQUIPMENT_DIRECTORY);
var newfiles = (from file in files
where file.LastWriteTime > fcr.FILE_LASTWRITETIME.AddSeconds(1)
select file).ToList();
foreach (var file in newfiles)
{
string filecopy = string.Format("{0}\\{1}", _File_Copy_Path.TrimEnd('\\'), file.Name);
file.CopyTo(filecopy,true);
using (System.IO.StreamReader sreader = new System.IO.StreamReader(filecopy, System.Text.Encoding.UTF8))
{
Console.WriteLine(string.Format("{3:HH:mm:ss}:{0}({1}) {2} ReadFile {4}", this.Machine.MACHINENAME, this.Machine.MACHINECODDE, this.GetType().Name, DateTime.Now, file.Name));
long read_pos = 0;
if (file.Name.Equals((fcr.RECORD_FILENAME + ""), StringComparison.CurrentCultureIgnoreCase))
{
read_pos = fcr.RECORD_FILE_LENGTH;
}
else
{
fcr.RECORD_KEYWORDS = "";
}
sreader.BaseStream.Position = read_pos;
while (!sreader.EndOfStream)
{
var line = sreader.ReadLine();
if (line.StartsWith("_.sv_Scrap [ ];"))
{
fcr.RECORD_KEYWORDS = line;
}
else
{
if (!string.IsNullOrWhiteSpace(fcr.RECORD_KEYWORDS))
{
Console.WriteLine(line);
sql.AppendFormat("INSERT INTO [T_PA_{0}]VALUES(NEWID(),'','','','','{1}','{0}','',GETDATE(),GETDATE(),'{2}','{3}')", this.Machine.MACHINECODDE, this.Machine.MACHINENAME, fcr.RECORD_KEYWORDS, line);
sql.AppendLine("");
//sql.AppendLine("GO");
}
}
}
fcr.RECORD_FILENAME = file.Name;
fcr.FILE_LASTWRITETIME = file.LastWriteTime;
fcr.RECORD_FILE_LENGTH = sreader.BaseStream.Length;
fcr.RECORD_COPY_DATETIME = DateTime.Now;
}
System.IO.File.Delete(filecopy);
}
}
if (sql.Length > 0)
{
using (var session = QMFrameWork.Data.AppDataFactory.CreateMainSession())
{
session.ExecuteSql(sql.ToString());
}
}
if (fcr.IsNewInfo)
{
fcrbll.Insert(fcr);
}
else
{
fcrbll.Update(fcr);
}
}
catch(Exception ex)
{
Console.WriteLine(string.Format("{3:HH:mm:ss}:{0}({1}) {2} Exception ------------------------- \r\n {4} \r\n---------------------End", this.Machine.MACHINENAME, this.Machine.MACHINECODDE, this.GetType().Name, DateTime.Now, ex.Message));
QMFrameWork.Log.LogManager.LogHelper.Error(
new QMFrameWork.Log.LogInfo { ErrorInfo = ex, Info = ex.Message });
}
finally
{
if (timer != null)
{
timer.Change(10000, System.Threading.Timeout.Infinite);
}
}
}
public override void Start()
{
if (timer != null)
{
timer.Change(10000, System.Threading.Timeout.Infinite);
}
base.Start();
}
public override void Stop()
{
if (timer != null)
{
timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
}
base.Stop();
}
public override void Dispose()
{
if (timer != null)
{
timer.Dispose();
timer = null;
}
base.Dispose();
}
}
}