ShareFolderWatcher.cs 4.1 KB
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;
    }
}