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

357 lines
15 KiB

using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using QMAPP.FJC.Entity.Basic;
using QMAPP.FJC.BLL.Basic;
using QMAPP.FJC.Entity.FileCopy;
using QMAPP.FJC.BLL.FileCopy;
using System.Net.NetworkInformation;
namespace AutoFileCopyNet
{
///<summary>
/// 模块编号:AutoFileCopyNet.AutoFileCopy
/// 作 用:根据设备信息,从设备共享目录复制文件
/// 作 者:pxbibm
/// 编写日期:2013-08-28
public static class AutoFileCopy
{
/// <summary>
/// 实现从共享目录复制文件
/// </summary>
/// <param name="equipmentType">设备类型</param>
/// <param name="toDir">文件存储目录</param>
/// <param name="localDriverPath">本地映射驱动名称</param>
/// <param name="netUser">网络用户</param>
/// <param name="userPwd">密码</param>
/// <param name="lastWriteTime">最近读写时间,根据此条件搜索该修改时间大于该时间的文件</param>
/// <param name="fileRenamePrefix">文件从命名的前缀</param>
/// <param name="errorMessageHead">错误消息头</param>
public static void AutoFileCopyFromEquipmentByType(MachineInfo info, string equipmentType, string server_ip, string toDir, string localDriverPath, string fileRenamePrefix, string errorMessageHead,string conn_way)
{
try
{
#region "循环从设备复制文件"
String localDir = String.Empty;
String remotePath = String.Empty;
int status = -1;
localDir = toDir + "\\" + equipmentType + "\\" + info.MACHINECODDE + "\\";//本地存储路径
remotePath = info.EQUIPMENT_DIRECTORY;//文件所在目录
string connectIp = "\\\\" + server_ip + "\\" + remotePath;
if (string.IsNullOrEmpty(server_ip))
{
connectIp = info.IPADDRESS + remotePath;//设备IP
}
//判断连接方式
if ("0".Equals(conn_way))//打开即时断开
{
if (System.IO.Directory.Exists(localDriverPath))
{
WNet.Disconnect(localDriverPath);
}
}
if (!System.IO.Directory.Exists(localDriverPath))
{
status = WNetConnect(connectIp, localDriverPath, info.USERNAME, info.PASSWORD);
}
else
{
status = (int)ERROR_ID.ERROR_SUCCESS;
}
if (status == (int)ERROR_ID.ERROR_SUCCESS)
{
if (info.EQUIPMENT_TYPE_CODE.ToString() == equipmentType)
{
DirHandler.CopyDir(localDriverPath , localDir, info.MACHINECODDE, info.EQUIPMENT_TYPE_CODE, info.PID);
}
}
else
{
LogFileAccess.WriteLogFile("网络创建网络连接失败" + "---" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
#endregion
}
catch (Exception ee)
{
LogFileAccess.WriteLogFile("网络创建连接复制文件抓取失败" + ee.Message + ee.Source + "---" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
finally
{
if ("0".Equals(conn_way))//打开即时断开
{
WNet.Disconnect(localDriverPath);
}
}
}
/// <summary>
/// 实现从共享目录复制文件
/// </summary>
/// <param name="equipmentType">设备类型</param>
/// <param name="toDir">文件存储目录</param>
/// <param name="localDriverPath">本地映射驱动名称</param>
/// <param name="netUser">网络用户</param>
/// <param name="userPwd">密码</param>
public static void AutoFileCopyFromEquipmentByType(MachineInfo info, string equipmentType, string server_ip, string toDir, string localDriverPath, string conn_way)
{
FTPHelper ftpHelper = new FTPHelper(info.IPADDRESS, info.EQUIPMENT_DIRECTORY, info.USERNAME, info.PASSWORD);
string toPath = toDir + "\\" + equipmentType + "\\" + info.MACHINECODDE + "\\";
List<FileStruct> list = ftpHelper.GetLoadListFiles(toPath);//本地存储路径);
LogFileAccess.WriteLogFile("海天数据文件复制:" + list.Count+"-----------" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
if (list.Count > 0)
{
DirHandler.UpdateDir(list, toPath, info.MACHINECODDE, info.EQUIPMENT_TYPE_CODE, info.PID);
}
}
/// <summary>
/// 连接共享网络
/// </summary>
/// <param name="remotePath">远程共享网络路径</param>
/// <param name="localDriverPath">本地网络路径</param>
/// <param name="equipment_ip">远程共享网络IP</param>
/// <param name="netUser">用户名</param>
/// <param name="userPwd">密码</param>
/// <returns></returns>
private static int WNetConnect(string remotePath, string localDriverPath, string netUser, string userPwd)
{
int connci = 0;
int status = -1;
string conn_frequency = GetConfigurationSettings.conn_frequency;
if (!string.IsNullOrEmpty(conn_frequency) && conn_frequency == "UNLIMIT")
{
while (status != (int)ERROR_ID.ERROR_SUCCESS)
{
status = WNet.Connect(remotePath, localDriverPath, netUser, userPwd);
}
}
else
{
int limitcount = 10;
if (!string.IsNullOrEmpty(conn_frequency))
{
limitcount = int.Parse(conn_frequency);
}
while (status != (int)ERROR_ID.ERROR_SUCCESS && connci < limitcount)
{
connci++;
status = WNet.Connect(remotePath, localDriverPath, netUser, userPwd);
}
}
return status;
}
/// <summary>
/// 实现从共享目录,搜索某目录,并复制该目录下的文件
/// </summary>
/// <param name="equipmentType">设备类型</param>
/// <param name="toDir">文件存储目录</param>
/// <param name="localDriverPath">本地映射驱动名称</param>
/// <param name="netUser">网络用户</param>
/// <param name="userPwd">密码</param>
/// <param name="keyWords">搜索目录的关键字</param>
/// <param name="searchOption">搜索参数,只搜索顶层目录或所有目录</param>
/// <param name="fileRenamePrefix">文件从命名的前缀</param>
/// <param name="errorMessageHead">错误消息头</param>
public static void AutoDirCopyFromEquipmentByType(String equipmentType,string equipment_ip, String toDir, String localDriverPath, String netUser, String userPwd, String keyWords, System.IO.SearchOption searchOption, String fileRenamePrefix, String errorMessageHead)
{
try
{
MachineInfoBLL bllMachine = new MachineInfoBLL();
FileCopyRecordBLL bll = new FileCopyRecordBLL();
FileCopyRecord record = new FileCopyRecord();
//从数据库读取设备信息
List<MachineInfo> list = bllMachine.GetAllList(new MachineInfo { PROCESSTYPE = equipmentType, IPADDRESS = equipment_ip });
#region "循环从设备复制文件"
if (list.Count > 0)
{
String localDir = String.Empty;
String remotePath = String.Empty;
int status = -1;
foreach (MachineInfo eq in list)
{
localDir = toDir + eq.MACHINECODDE;
remotePath = "";//目录
if (status != (int)ERROR_ID.ERROR_SUCCESS)
{
status = WNetConnect(remotePath, localDriverPath, netUser, userPwd);
//LogFileAccess.WriteLogFile(eq.EQUIPMENT_IP+"激光切数据文件拷贝复制文件服务...网络连接状态" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo) + "...|" + status);
}
if (status == (int)ERROR_ID.ERROR_SUCCESS)
{
if (eq.PROCESSTYPE == equipmentType)
{
DirHandler.CopyDir(remotePath, localDir, keyWords, System.IO.SearchOption.TopDirectoryOnly, fileRenamePrefix, eq.MACHINECODDE, eq.PROCESSTYPE, localDir, eq.PID);
}
}
}
}
#endregion
}
catch (Exception ex)
{
LogFileAccess.WriteLogFile("网络共享文件抓取失败" + ex.Message + ex.Source + "---" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
finally
{
WNet.Disconnect(localDriverPath);
}
}
/// <summary>
/// 实现从共享目录删除
/// </summary>
/// <param name="equipmentType">设备类型</param>
/// <param name="toDir">文件存储目录</param>
/// <param name="localDriverPath">本地映射驱动名称</param>
/// <param name="netUser">网络用户</param>
/// <param name="userPwd">密码</param>
/// <param name="lastWriteTime">最近读写时间,根据此条件搜索该修改时间大于该时间的文件</param>
/// <param name="fileRenamePrefix">文件从命名的前缀</param>
/// <param name="errorMessageHead">错误消息头</param>
public static void AutoFileDelete(MachineInfo info, string server_ip, string localDriverPath, string fileRenamePrefix, string errorMessageHead, string conn_way
,string delDay,string startTime,string endTime,string delTime,string fileBakPath)
{
try
{
#region "循环从设备复制文件"
String localDir = String.Empty;
String remotePath = String.Empty;
int status = -1;
remotePath = info.EQUIPMENT_DIRECTORY;//文件所在目录
string connectIp = server_ip + "\\" + remotePath;
if (string.IsNullOrEmpty(server_ip))
{
connectIp = info.IPADDRESS + "\\" + remotePath;//设备IP
}
//判断连接方式
if ("0".Equals(conn_way))//打开即时断开
{
if (System.IO.Directory.Exists(localDriverPath))
{
WNet.Disconnect(localDriverPath);
}
}
if (!System.IO.Directory.Exists(localDriverPath))
{
status = WNetConnect("\\\\" + connectIp, localDriverPath, info.USERNAME, info.PASSWORD);
}
else
{
status = (int)ERROR_ID.ERROR_SUCCESS;
}
if (status == (int)ERROR_ID.ERROR_SUCCESS)
{
//判断是否需要删除文件
if (DateTime.Now.DayOfWeek.ToString("D").Equals(delDay)
&& DateTime.Now.Hour >= Convert.ToInt32(startTime)
&& DateTime.Now.Hour <= Convert.ToInt32(endTime))
{
int time = 0;
while (!AutoFileCopy.Delete_file(localDriverPath + "\\", fileBakPath))
{
time++;
if (time >= Convert.ToInt32(delTime))
{
break;
}
}
}
}
else
{
LogFileAccess.WriteLogFile("网络创建网络连接失败" + "---" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
#endregion
}
catch (Exception ee)
{
LogFileAccess.WriteLogFile("网络创建连接复制文件抓取失败" + ee.Message + ee.Source + "---" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
finally
{
if ("0".Equals(conn_way))//打开即时断开
{
WNet.Disconnect(localDriverPath);
}
}
}
/// <summary>
/// 判断IP是否联通
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static bool Check_PingIp(string ip)
{
Ping ping = new Ping();
PingReply pr = ping.Send(ip,1000);
if (pr.Status == IPStatus.Success)
return true;
else
return false;
//Ping ping = new Ping();
//PingOptions poptions = new PingOptions();
//poptions.DontFragment = true;
//string data = string.Empty;
//byte[] buffer =System.Text. Encoding.ASCII.GetBytes(data);
//int timeout = 1200;
//PingReply reply = ping.Send(System.Net.IPAddress.Parse(ip),timeout,buffer,poptions);
//if (reply.Status == IPStatus.Success)
// return true;
//else
// return false;
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static bool Delete_file(string directoryPath,string fileBakPath)
{
bool b = false;
if (!System.IO.Directory.Exists(fileBakPath))
{
System.IO.Directory.CreateDirectory(fileBakPath);
}
if (System.IO.Directory.GetFiles(directoryPath).Length > 0)
{
foreach (string var in System.IO.Directory.GetFiles(directoryPath))
{
System.IO.FileInfo file = new System.IO.FileInfo(var);
try
{
file.MoveTo(fileBakPath + "\\" + file.Name.ToLower().Replace(".csv", "_"+DateTime.Now.ToString("yyyyMMddHHmm")) +".csv");
b = true;
LogFileAccess.WriteLogFile("文件" + file.Name + "备份成功" + "---" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
catch (Exception)
{
b = false;
LogFileAccess.WriteLogFile("文件" + file.Name + "删除失败" + "---" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
}
return b;
}
}
}