Class2.cs
4.0 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
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Asa.RFID
{
/// <summary>
/// 读取所有RFID
/// </summary>
public class ReadAll2 : IReadAll
{
private HiStation[] _device;
private Dictionary<string, string> _id; //RFID编号
private string _logName;
public event IReadAll.ReceivedEvent Received;
public event IReadAll.ReceiveBufferEvent ReceiveBuffer;
/// <summary>
/// 读取所有RFID
/// </summary>
/// <param name="logName">日志名称</param>
public ReadAll2(string logName = "HFReader")
{
_logName = logName;
_id = new Dictionary<string, string>();
if (Common.log == null)
Common.log = log4net.LogManager.GetLogger(logName);
}
public void Start(int port)
{
}
/// <summary>
/// 开启
/// </summary>
/// <param name="ip"></param>
public void Start(string[] ip)
{
try
{
_id.Clear();
_device = new HiStation[ip.Length];
for (int i = 0; i < _device.Length; i++)
{
_id.Add(ip[i], "000");
_device[i] = new HiStation(_logName) { IP = ip[i] };
_device[i].Connected += ReadAll_Connected;
_device[i].Received += ReadAll_Received;
_device[i].Open();
}
Common.log.Info("Server Start");
}
catch (Exception ex)
{
Common.log.Error("Start", ex);
}
}
/// <summary>
/// 停止
/// </summary>
public void Stop()
{
if (_device != null)
{
for (int i = 0; i < _device.Length; i++)
{
try
{
_device[i].TriggerMode(false);
System.Threading.Thread.Sleep(50);
_device[i].Close();
}
catch (Exception ex)
{
Common.log.Error("Stop", ex);
}
}
}
_device = null;
Common.log.Info("Server Stop");
}
/// <summary>
/// 读取ID号
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public string Read(string ip)
{
if (_id.TryGetValue(ip, out string value))
{
Common.log.Info("[" + ip + "]Read " + value);
return value;
}
else
{
Common.log.Info("[" + ip + "]Read 没有找到");
return "000";
}
}
/// <summary>
/// 清除缓存
/// </summary>
/// <param name="ip"></param>
public void Clear(string ip)
{
if (_id.ContainsKey(ip))
{
_id[ip] = "000";
Common.log.Info("[" + ip + "]Clear");
}
else
{
Common.log.Info("[" + ip + "]Clear 没有找到");
}
}
private void ReadAll_Connected(string ip)
{
int index = -1;
foreach (var ss in _id.Keys)
{
index++;
if (ss == ip)
break;
}
if (index != -1)
_device[index].TriggerMode(true);
}
private void ReadAll_Received(string ip, string uid, string data)
{
Common.log.Debug("Received IP=" + ip + " uid=" + uid + " data=" + data);
if (_id[ip] == data)
{
}
else
{
_id[ip] = data;
Received?.Invoke(ip, data);
}
}
}
}