BufferSlotsManger.cs 4.6 KB
using Newtonsoft.Json;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceLibrary
{
    class BufferSlotsManger
    {
        Dictionary<string, BufferSlotInfo> bufferSlotInfos = new Dictionary<string, BufferSlotInfo>();
        string DeviceName;
        public BufferSlotsManger(string devicename)
        {
            DeviceName = devicename;
            LoadData();
            for (int i = 0; i < 8; i++)
            {
                string sloatkey = DeviceName + "_B0" + (i + 1);
                if (!bufferSlotInfos.ContainsKey(sloatkey))
                {
                    bufferSlotInfos.Add(sloatkey, new BufferSlotInfo(sloatkey));
                }
            }
        }
        public string GetFreeSlot()
        {
            lock (bufferSlotInfos)
            {
                var slot = bufferSlotInfos.Values.ToList().Find(s => !s.HasReel);
                return slot?.Name;
            }
        }
        public string GetInstoreSlot(out JobInfo jobInfo)
        {
            lock (bufferSlotInfos)
            {                
                var slot = bufferSlotInfos.Values.ToList().Find(s => s.HasReel && s.IsInstore);
                jobInfo = slot?.JobInfo.Clone();
                return slot?.Name;
            }
        }
        public string GetOutstoreSlot(out JobInfo jobInfo)
        {
            lock (bufferSlotInfos)
            {
                var slot = bufferSlotInfos.Values.ToList().Find(s => s.HasReel && !s.IsInstore);
                jobInfo = slot?.JobInfo.Clone();
                return slot?.Name;
            }
        }
        public void SetSlotInfo(string name, bool isinstore, JobInfo jobInfo)
        {
            lock (bufferSlotInfos)
            {
                bufferSlotInfos[name].IsInstore = isinstore;
                bufferSlotInfos[name].JobInfo = jobInfo.Clone();
                bufferSlotInfos[name].HasReel = true;
            }
            SaveData();
        }
        public void ClearSlot(string name)
        {
            lock (bufferSlotInfos)
            {
                bufferSlotInfos[name].HasReel = false;
            }
            SaveData();

        }
        void LoadData() {
            string filename = $"Config\\BufferSlotsList_{DeviceName}.temp";
            if (File.Exists(filename))
            {
                bool isreadok = false;
                try
                {
                    var tl = File.ReadAllText(filename);
                    bufferSlotInfos = JsonConvert.DeserializeObject<Dictionary<string, BufferSlotInfo>>(tl);

                    isreadok = bufferSlotInfos != null;
                    if (!isreadok)
                        LogUtil.error("启动时缓存料盘信息加载失败:"+DeviceName);
                }
                catch (Exception ex)
                {
                    LogUtil.error("启动时缓存料盘信息加载失败:" + DeviceName + ex);
                }
                if (isreadok)
                    return;
                try
                {
                    var tl = File.ReadAllText(filename+"~");
                    bufferSlotInfos = JsonConvert.DeserializeObject<Dictionary<string, BufferSlotInfo>>(tl);
                    if (bufferSlotInfos == null)
                    {
                        LogUtil.error("启动时备份缓存料盘加载失败:"+DeviceName);
                        bufferSlotInfos = new Dictionary<string, BufferSlotInfo>();
                    }
                }
                catch (Exception ex)
                {
                    LogUtil.error("启动时托盘备份信息加载失败:" + ex);
                    bufferSlotInfos = new Dictionary<string, BufferSlotInfo>();
                }

            }
        }
        void SaveData() {
            string filename = $"Config\\BufferSlotsList_{DeviceName}.temp";
            try
            {
                var TL = bufferSlotInfos.Where(t => t.Value.HasReel).ToDictionary(a => a.Key, a => a.Value);
                ConfigHelper.Config.FileSave(JsonConvert.SerializeObject(TL), filename+"~");
                ConfigHelper.Config.FileSave(JsonConvert.SerializeObject(TL), filename);
            }
            catch (Exception ex)
            {
                LogUtil.error("托盘信息保存失败:" + ex);
            }
        }
    }
    class BufferSlotInfo {
        public string Name;
        public bool HasReel;
        public bool IsInstore;
        public JobInfo JobInfo;
        public BufferSlotInfo(string name) {
            Name = name;
        }
    }
}