本文最后更新于 60 天前,其中的信息可能已经有所发展或是发生改变。
using com.Synjones.IService;
using com.Synjones.Model.Models;
using com.Synjones.Model.ResultModel;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Sunny.UI;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Vurdalakov.UsbDevicesDotNet;
namespace com.Synjones.Winform
{
public partial class collectForm : UITitlePage
{
private readonly IWorkStationService _workstationService;
private readonly IConfigService _configService;
private readonly IDocService _docService;
public static AvConfig config;
public static Model.Models.AvWorkStation Workstation = null;
public collectForm()
{
InitializeComponent();
_workstationService = Program.serviceProvider.GetRequiredService<IWorkStationService>();
_configService = Program.serviceProvider.GetRequiredService<IConfigService>();
_docService = Program.serviceProvider.GetRequiredService<IDocService>();
int collectId = File.ReadAllText("collect.txt").ToInt();
if (collectId != 0)
{
List<Model.Models.AvWorkStation> listworkstation = _workstationService.GetWorkStationList(Form1.userInfo).data;
if (listworkstation != null)
{
if (listworkstation.Any(p => p.WId == collectId))
{
Workstation = listworkstation.Where(p => p.WId == collectId).FirstOrDefault();
//开启HTTP服务和采集
InitDisk();
}
}
}
cbAnJian.Visible = false;
cbAnJian.CheckedChanged += CbAnJian_CheckedChanged;
}
//public static void setaaa() {
// flowLayoutPanel1.Controls.RemoveByKey("remove");
//}
private void CbAnJian_CheckedChanged(object sender, EventArgs e)
{
//if (flowLayoutPanel1.Controls.Count > 0) {
// UIMessageTip.ShowWarning("采集过程不允许修改");
// UICheckBox ck = (UICheckBox)sender;
// ck.Checked = !ck.Checked;
//}
//flowLayoutPanel1.Controls.RemoveByKey("remove");
}
/// <summary>
/// 拷贝安监报
/// </summary>
/// <param name="doc"></param>
private void anjianCopy(AvDoc p)
{
config = _configService.GetConfig(Form1.userInfo).data;
if (!Directory.Exists(config.AnJianBaoSavePath))
Directory.CreateDirectory(config.AnJianBaoSavePath);
if (File.Exists(p.DPath + p.DSourceFile))
{
string username = $"安监报({p.DUserName}{p.DUserNumber})-{p.DShouJiaoLuNumber}-{p.DCatNumber}";
if (p.DUserNumber.Contains("US")) //用户组,处理下admin-admin,王新星-432725 变成 adminadmin-王新星432725
{
string[] userarr = p.DUserName.Split(",");
List<string> listuser = new List<string>();
userarr.ForEach(a =>
{
string[] temp = a.Split('-');
listuser.Add($"{temp[0]}{temp[1]}");
});
username = $"安监报({string.Join("-", listuser.ToArray())})-{p.DShouJiaoLuNumber}-{p.DCatNumber}";
}
string newfilepath = config.AnJianBaoSavePath;
if (!string.IsNullOrEmpty(p.DUserDepName))
{
newfilepath = Path.Combine(newfilepath, p.DUserDepName);
}
newfilepath = Path.Combine(newfilepath, p.DFileShootingTime.Value.ToString("yyyyMMdd"));
newfilepath = Path.Combine(newfilepath, username);
if (!Directory.Exists(newfilepath))
Directory.CreateDirectory(newfilepath);
newfilepath = Path.Combine(newfilepath, p.DNote);
//string newfilepath= config.AnJianBaoSavePath+
if (!File.Exists(newfilepath))
File.Copy(p.DPath + p.DSourceFile, newfilepath);
p.DAnJianIsCopy = true;
_docService.UpdateDoc(p, Form1.userInfo);
}
}
public static ConcurrentQueue<AvDoc> UploadQueue = new ConcurrentQueue<AvDoc>(); //待上传文件队列
public object lockUpload = new object();
CancellationTokenSource _cancelSource = new CancellationTokenSource();
private void InitDisk()
{
lbPath.Text = "存储路径:" + Workstation.WsFilePaeh;
DriveInfo drive = DriveInfo.GetDrives().Where(p => p.Name == Workstation.WsFilePaeh.Substring(0, Workstation.WsFilePaeh.IndexOf("\\") + 1).ToUpper()).FirstOrDefault();
if (drive != null)
{
long total = drive.TotalSize / 1024 * 1024 * 1024;
Double free = drive.TotalFreeSpace / Convert.ToDouble(1024 * 1024 * 1024);
lbKongJian.Text = "剩余空间:" + free.ToString("0.00") + "GB";
_cancelSource.Cancel();
_cancelSource = new CancellationTokenSource();
Task.Run(async () =>
{
while (!this.Disposing)
{
if (UploadQueue.Count() > 0)
{
lock (lockUpload)
{
AvDoc doc;
if (UploadQueue.TryDequeue(out doc))
{
anjianCopy(doc);
}
}
}
await Task.Delay(10000);
}
}, _cancelSource.Token);
Task.Run(async () =>
{
//备份安监文件
while (!this.Disposing)
{
try
{
List<AvDoc> listdoc = _docService.GetDocList(Workstation.WsCode).data;
config = _configService.GetConfig(Form1.userInfo).data;
listdoc.ForEach(p =>
{
if (p.DAnJianType.Value == 1 && !p.DAnJianIsCopy.Value)
{
if (!UploadQueue.Any(a => a.DId == p.DId))
UploadQueue.Enqueue(p);
//anjianCopy(p);
//if (!Directory.Exists(config.AnJianBaoSavePath))
// Directory.CreateDirectory(config.AnJianBaoSavePath);
//if (File.Exists(p.DPath + p.DSourceFile))
//{
// File.Copy(p.DPath + p.DSourceFile, config.AnJianBaoSavePath + p.DSourceFile.Substring(p.DSourceFile.LastIndexOf("\\")));
// p.DAnJianIsCopy = true;
//_docService.UpdateDoc(p, Form1.userInfo);
//}
}
});
}
catch { }
await Task.Delay(60000);
}
}, _cancelSource.Token);
Task.Run(async () =>
{
//删除文件和记录
while (!this.IsDisposed)
{
config = _configService.GetConfig(Form1.userInfo).data;
if (config.SourceSaveDay != 0 && config.RecordSaveDay != 0)
{
List<AvDoc> listdoc = _docService.GetDocList(Workstation.WsCode).data;
if (listdoc.Count > 0)
{
//文件
listdoc.ForEach(p =>
{
if ((DateTime.Now - p.DUploadTime.Value).TotalDays > config.SourceSaveDay)
{
if (File.Exists(p.DPath + p.DSourceFile))
{
File.Delete(p.DPath + p.DSourceFile);
}
}
});
//记录
listdoc.ForEach(p =>
{
if ((DateTime.Now - p.DUploadTime.Value).TotalDays > config.RecordSaveDay)
{
if (File.Exists(p.DPath + p.DSourceFile))
{
File.Delete(p.DPath + p.DSourceFile);
}
_docService.DeleteDoc(p.DId.ToString(), Form1.userInfo);//删除记录
}
});
}
await Task.Delay(86400000); //一天执行一次
}
else
{
await Task.Delay(300000); //6分钟
}
}
}, _cancelSource.Token);
Task.Run(() =>
{
//Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string hostUrl = $"http://*:{Workstation.WsWebAdd}";
var host = new WebHostBuilder()
.UseKestrel()
.UseUrls(hostUrl)
.UseWebRoot("htdocs")
.UseStartup<BoxCollect.Startup>()
.Build();
try
{
host.Run();
}
catch (System.Net.Sockets.SocketException ex)
{
this.Invoke(new Action(() =>
{
UIMessageBox.ShowError($"服务启动失败,{Workstation.WsWebAdd}端口被占用!");
}));
Application.Exit();
}
catch (Exception ex)
{
this.Invoke(new Action(() =>
{
UIMessageBox.ShowError($"服务启动失败,{ex.Message}");
}));
}
}, _cancelSource.Token).ConfigureAwait(false);
}
else
{
lbKongJian.Text = "剩余空间:未找到路径";
}
}
private void uiButton1_Click(object sender, EventArgs e)
{
FormControl.selectCollect select = new FormControl.selectCollect();
List<Model.Models.AvWorkStation> listworkstation = _workstationService.GetWorkStationList(Form1.userInfo).data;
if (listworkstation != null)
{
listworkstation.ForEach(p =>
{
select.cbCollect.Items.Add(p);
});
select.cbCollect.DisplayMember = "WsName";
select.cbCollect.ValueMember = "WId";
}
select.Workstation = Workstation;
select.TopMost = true;
select.ShowDialog();
if (select.IsOK)
{
this.TopMost = true;
InfoResult result = _workstationService.UpdateWorkStation(select.Workstation, Form1.userInfo);
if (result.code == "000000")
{
Workstation = select.Workstation;
File.WriteAllText("collect.txt", Workstation.WId.ToString());
UIMessageBox.ShowInfo("修改成功,即将重启");
//Init();
Application.Restart();
}
else
{
UIMessageTip.ShowError(result.msg);
}
}
select.Dispose();
//select.cbCollect.Items.Add()
}
}
}