ShareFolderWatcher.cs
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using HalconDotNet;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
public class ShareFolderWatcher : IDisposable
{
Thread thread;
bool run = false;
string backupfolder = "\\image\\VJBackup";
public string FolderName;
public ShareFolderWatcher(string foldername)
{
FolderName = foldername;
Directory.CreateDirectory(backupfolder);
LogUtil.info("[ShareFolderWatcher] " + "set:" + foldername);
}
public bool Start(out string errmsg)
{
errmsg = "";
try
{
if (!Directory.Exists(FolderName))
{
errmsg = $"Directory not exists:{FolderName}";
LogUtil.error("[ShareFolderWatcher] " + errmsg);
return false;
}
}
catch (Exception ex)
{
errmsg = ex.ToString();
LogUtil.error("[ShareFolderWatcher] " + errmsg);
return false;
}
thread = new Thread(new ThreadStart(Watcher));
thread.Start();
LogUtil.error("[ShareFolderWatcher] 文件监听启动成功");
return true;
}
DateTime lastUpdateTime = DateTime.MinValue;
Dictionary<string, int> CountList = new Dictionary<string, int>();
public DateTime LastUpdateTime { get => lastUpdateTime; }
public int LastQty = 0;
public string LastSN = "";
void Watcher()
{
run = true;
while (run)
{
Thread.Sleep(1000);
try
{
DirectoryInfo directoryInfo = new DirectoryInfo(FolderName);
var fis = directoryInfo.GetFiles("*.csv").Where(x => x.LastWriteTime > lastUpdateTime).ToList();
fis.Sort((a, b) => b.LastWriteTime.CompareTo(a.LastWriteTime));
if (fis.Count > 0)
lastUpdateTime = fis[0].LastWriteTime;
else
continue;
lock (CountList)
{
foreach (var fi in fis)
{
var file = fi.FullName;
var localfile = Path.Combine(backupfolder, Path.GetFileName(file));
File.Copy(file, localfile, true);
try
{
File.Delete(file);
}
catch (Exception ex) {
LogUtil.error("[ShareFolderWatcher] " + ex.ToString());
}
var ft = File.ReadAllLines(localfile);
if (ft.Length >= 2 && !string.IsNullOrWhiteSpace(ft[1]))
{
var datas = ft[1].Split(',');
if (datas.Length == 3 && int.TryParse(datas[2], out int qty))
{
LogUtil.info($"读取到VJ点料结果:{datas[1]},{qty}");
if (!CountList.ContainsKey(datas[1]))
CountList.Add(datas[1], qty);
else
CountList[datas[1]] = qty;
LastQty = qty;
LastSN = datas[1];
}
}
}
}
}
catch (Exception ex)
{
LogUtil.error("[ShareFolderWatcher] " + ex.ToString());
}
}
}
public bool GetQty(string barcode, out int qty)
{
if (barcode.Count(c => c == '#') == 3)
{
barcode = barcode.Split('#')[1].Substring(1);
}
lock (CountList)
{
if (CountList.TryGetValue(barcode, out qty))
{
CountList.Remove(barcode);
return true;
}
}
qty = 0;
return false;
}
public void Dispose()
{
run = false;
}
}