EDataManager.cs
4.3 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
136
137
138
139
140
141
142
143
144
145
146
using Asa;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace OnlineStore.DeviceLibrary
{
public class EDataManager
{
/// <summary>
/// key=设备DeviceId,value=料盘信息
/// </summary>
private static ConcurrentDictionary<int, EquipDataInfo> dataMap = new ConcurrentDictionary<int, EquipDataInfo>();
static EDataManager()
{
}
public static List<EquipDataInfo> getDataList()
{
return new List<EquipDataInfo>(dataMap.Values);
}
public static EquipDataInfo GetEuipParam(int deviceId)
{
if (dataMap.ContainsKey(deviceId))
{
return dataMap[deviceId];
}
return null;
}
public static void UpdateParam(int deviceId, int reelType = 0, InOutParam param = null)
{
if (reelType.Equals(0) || param == null)
{
EquipDataInfo data = null;
dataMap.TryRemove(deviceId, out data);
}
else
{
EquipDataInfo data = new EquipDataInfo(deviceId, reelType, param);
if (dataMap.ContainsKey(deviceId))
{
dataMap[deviceId] = data;
}
else
{
dataMap.TryAdd(deviceId, data);
}
}
SaveMapToFile();
}
private static string FilePath = "";
public static void InitFileData()
{
dataMap = new ConcurrentDictionary<int, EquipDataInfo>();
FilePath = Application.StartupPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_TrayList);
if (File.Exists(FilePath))
{
LogUtil.info("开始加载文件设备料盘信息:" + FilePath);
string[] lines = FileEncoding.GetFileLines(FilePath);
foreach (string line in lines)
{
EquipDataInfo data = JsonHelper.DeserializeJsonToObject<EquipDataInfo>(line);
if (data != null && (data.inouParam != null) && (data.inouParam.WareCode != "") && data.ReelType > 0)
{
LogUtil.info("加载到设备料盘信息:" + data.ToStr());
dataMap.TryAdd(data.DeviceId, data);
}
}
LogUtil.info("设备料盘加载完成");
}
}
public static void SaveMapToFile()
{
try
{
List<EquipDataInfo> trayList = new List<EquipDataInfo>(dataMap.Values);
List<string> lineList = new List<string>();
foreach (EquipDataInfo tray in trayList)
{
string line = JsonHelper.SerializeObject(tray);
if (!string.IsNullOrEmpty(line))
{
lineList.Add(line);
}
}
FileEncoding.WritteFile(FilePath, lineList.ToArray());
}
catch (Exception ex)
{
LogUtil.error("SaveTrayToFile出错:" + ex.ToString());
}
}
}
public class EquipDataInfo
{
public EquipDataInfo()
{
this.DeviceId = 0;
this.inouParam = null;
this.ReelType = 0;
}
public EquipDataInfo(int id, int reelType, InOutParam param)
{
this.ReelType = reelType;
this.DeviceId = id;
this.inouParam = param;
}
public int DeviceId { get; set; }
public InOutParam inouParam { get; set; }
/// <summary>
/// 1=入库,2=出库
/// </summary>
public int ReelType { get; set; }
public string ToStr()
{
if (inouParam == null)
{
return "设备[" + DeviceId + "]=null";
}
return "设备[" + DeviceId + "]=" + inouParam.ToStr();
}
}
}