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

168 lines
7.8 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.FJC.Entity.FileCopy;
namespace ProcessFileSyncService.FileSynchronizers
{
public class LaserScoringFileSynchronizer:FileSynchronizer
{
string _File_Copy_Path = "C:\\";
System.Threading.Timer timer;
public LaserScoringFileSynchronizer(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_TYPE_CODE = this.Machine.EQUIPMENT_TYPE_CODE,
RECORD_KEY = Guid.NewGuid().ToString(),
RECORD_READFLAG = "0",
RECORD_COPY_DATETIME = DateTime.Parse("1975-1-1"),
FILE_LASTWRITETIME = DateTime.Parse("1975-1-1"),
IsNewInfo = true
};
}
using (IdentityScope idscope = new IdentityScope(this.Machine.USERNAME, this.Machine.IPADDRESS, this.Machine.PASSWORD))
{
System.IO.DirectoryInfo rootdir = new System.IO.DirectoryInfo(Machine.EQUIPMENT_DIRECTORY);
var daydirPrefix=fcr.RECORD_COPY_DATETIME.ToString("yyyyMMdd*");
var daydirs= rootdir.EnumerateDirectories(daydirPrefix);
while (daydirs.Count() > 0)
{
List<System.IO.FileInfo> newfiles = new List<System.IO.FileInfo>();
foreach (var dir in daydirs.OrderBy(p => p.CreationTime))
{
var copydirpath = dir.FullName.ToUpper().Replace(Machine.EQUIPMENT_DIRECTORY.ToUpper(), _File_Copy_Path.ToUpper());
if (!System.IO.Directory.Exists(copydirpath))
{
System.IO.Directory.CreateDirectory(copydirpath);
}
fcr.RECORD_EQUIPMENT_DIRECTORY = dir.FullName;
fcr.RECORD_COPY_DATETIME = dir.CreationTime.Date;
IEnumerable<System.IO.FileInfo> files = dir.EnumerateFiles("*." + Machine.TERMINALPORT, System.IO.SearchOption.AllDirectories);
var newfiles_sub = (from file in files
where file.LastWriteTime > fcr.FILE_LASTWRITETIME.AddSeconds(1)
orderby file.LastWriteTime ascending
select file).ToList();
newfiles.AddRange(newfiles_sub);
}
foreach (var file in newfiles.OrderBy(p=>p.LastWriteTime))
{
//if ((fcr.RECORD_FILENAME + "").ToUpper().Equals(file.Name.ToUpper()))
//{
// break;
//}
//string filecopy = string.Format("{0}\\{1}", copydirpath.TrimEnd('\\'), file.Name);
string filecopy = file.FullName.ToUpper().Replace(Machine.EQUIPMENT_DIRECTORY.ToUpper(), _File_Copy_Path.ToUpper());
System.IO.Directory.CreateDirectory(filecopy.Replace(file.Name.ToUpper(),""));
file.CopyTo(filecopy, true);
Console.WriteLine(string.Format("{3:HH:mm:ss}:{0}({1}) {2} CopyFile {4}", this.Machine.MACHINENAME, this.Machine.MACHINECODDE, this.GetType().Name, DateTime.Now, file.Name));
StringBuilder sql = new StringBuilder();
sql.AppendFormat("INSERT INTO [T_PA_{0}]VALUES(NEWID(),'','{4}','','','{1}','{0}','',GETDATE(),GETDATE(),'{2}','{3}')", this.Machine.MACHINECODDE, this.Machine.MACHINENAME, filecopy, file.Name, file.Name.Replace("." + Machine.TERMINALPORT, ""));
sql.AppendLine("");
//sql.AppendLine("GO");
fcr.RECORD_FILENAME = file.Name;
fcr.FILE_LASTWRITETIME = file.LastWriteTime;
using (var session = QMFrameWork.Data.AppDataFactory.CreateMainSession())
{
session.ExecuteSql(sql.ToString());
}
if (fcr.IsNewInfo)
{
fcrbll.Insert(fcr);
fcr.IsNewInfo = false;
}
else
{
fcrbll.Update(fcr);
}
}
daydirPrefix = fcr.RECORD_COPY_DATETIME.AddDays(1).ToString("yyyyMMdd*");
daydirs = rootdir.EnumerateDirectories(daydirPrefix);
if (daydirs.Count() == 0 && fcr.RECORD_COPY_DATETIME<DateTime.Now.Date)
{
fcr.RECORD_COPY_DATETIME = DateTime.Now.Date;
daydirPrefix = fcr.RECORD_COPY_DATETIME.ToString("yyyyMMdd*");
daydirs = rootdir.EnumerateDirectories(daydirPrefix);
}
}
}
}
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();
}
}
}