BufferSlotsManger.cs
4.6 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 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;
}
}
}