-
Notifications
You must be signed in to change notification settings - Fork 1
/
CardReader.cs
352 lines (280 loc) · 10.4 KB
/
CardReader.cs
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using ClassSockets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
namespace SecureServer
{
public class CardReader
{
public string IP { get; set; }
public bool IsConnected = false;
public string ControllerID { get; set; }
public int PlaneID { get; set; }
public int ERID { get; set; }
ClassSockets.ClientSocket ClientSocket;
ClassSockets.ServerSocket ServerScoket;
System.Threading.Timer tmr;
public CardReader(string controllerid,string ip,int ERID,int PlaneID)
{
this.IP = ip;
this.ControllerID = controllerid;
this.ERID = ERID;
this.PlaneID = PlaneID;
ClientSocket = new ClassSockets.ClientSocket();
ServerScoket = new ClassSockets.ServerSocket();
ServerScoket.OnRead += new ServerSocket.ConnectionDelegate(Server_OnRead);
if (ServerScoket.Active())
Console.WriteLine("Card Reader Server Socket is Listening!");
tmr= new System.Threading.Timer(OneSecTask);
tmr.Change(0, 1000);
}
void OneSecTask(object a)
{
try
{
int status= ClientSocket.ReadAllState(1,IP);
if (status == -2)
this.IsConnected = false;
else
this.IsConnected = true;
// System.Collections.BitArray ba=new System.Collections.BitArray(new int[]{status});
// if (ba.Get(0))
// Console.WriteLine("開門");
// else
// Console.WriteLine("關門");
// if (ba.Get(1))
// Console.WriteLine("外部按鍵按下");
// else
// Console.WriteLine("外部按鍵放開");
//if (ba.Get(2))
// Console.WriteLine("開門超時");
// else
// Console.WriteLine("無開門超時");
//if (ba.Get(3))
// Console.WriteLine("外力破壞");
//else
// Console.WriteLine("無外力破壞");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message + "," + ex.StackTrace);
}
}
private void Server_OnRead(Socket soc)
{
byte[] SRbuf = ServerScoket.ReceivedBytes;
if (SRbuf != null)
{
if (SRbuf.Length != 25)
return;
string SRhexstr = BitConverter.ToString(SRbuf);
CardReaderEventReport rpt = new CardReaderEventReport(SRbuf);
Console.WriteLine(rpt);
if (rpt.Status == (int)CardReaderStatusEnum.開鎖 ||
rpt.Status == (int)CardReaderStatusEnum.按鈕開門 ||
rpt.Status == (int)CardReaderStatusEnum.密碼開門 || rpt.Status == (int)CardReaderStatusEnum.系統開門 ||
rpt.Status == (int)CardReaderStatusEnum.異常入侵 )
{
SecureDBEntities1 db = new SecureDBEntities1();
db.tblEngineRoomLog.Add(
new tblEngineRoomLog()
{
ControlID = ControllerID,
ABA = rpt.CardNo.ToString(),
StartTime = DateTime.Now,
TypeID = 8,
Memo = rpt.StatusString,
TypeCode = (short)rpt.Status,
ERNo=this.PlaneID.ToString()
}
);
db.SaveChanges();
}
if (rpt.Status == (int)CardReaderStatusEnum.號碼錯誤 ||
rpt.Status == (int)CardReaderStatusEnum.卡號連續錯誤 ||
rpt.Status == (int)CardReaderStatusEnum.外力破壞
)
{
SecureDBEntities1 db = new SecureDBEntities1();
db.tblEngineRoomLog.Add(
new tblEngineRoomLog()
{
ControlID = ControllerID,
ABA = rpt.CardNo.ToString(),
StartTime = DateTime.Now,
TypeID = 8,
Memo = rpt.StatusString,
TypeCode = (short)rpt.Status,
ERNo = this.PlaneID.ToString()
}
);
db.SaveChanges();
}
// SRhexstr = SRhexstr.Replace("-", " ");
// ShowSMsg((soc.RemoteEndPoint.ToString()).Substring(0, soc.RemoteEndPoint.ToString().IndexOf(':')) + " ::: Received data" + " -> " + SRhexstr + "\r");
}
}
public void ForceOpenDoor()
{
int res= ClientSocket.WriteOpenDoorCommand(1, IP, 2);
if (res == -2)
{
IsConnected = false;
throw new Exception("Connection error!");
}
if (res >= 1)
throw new Exception("cmd error!");
if (res == -1)
throw new Exception("ID error!");
IsConnected = true;
}
public void SetDateTime(DateTime dt)
{
int res = ClientSocket.TimeCorrection(1, IP, (byte)(dt.Year % 100), (byte)dt.Month,(byte) dt.Day,(byte) dt.DayOfWeek,(byte) dt.Hour,(byte) dt.Minute,(byte) dt.Second);
if (res == -2)
{
IsConnected = false;
throw new Exception("Connection error!");
}
if (res >= 1)
throw new Exception("cmd error!");
if (res == -1)
throw new Exception("ID error!");
IsConnected = true;
}
public void WriteCardReaderID(int id)
{
int res = ClientSocket.WriteID(1, IP, (byte)id);
if (res == -2)
{
IsConnected = false;
throw new Exception("Connection error!");
}
if (res >= 1)
throw new Exception("cmd error!");
if (res == -1)
throw new Exception("ID error!");
IsConnected = true;
}
public void DeleteAllCard()
{
int res = ClientSocket.WriteCardAllDelete(1, IP);
if (res == -2)
{
IsConnected = false;
throw new Exception("Connection error!");
}
if (res >= 1)
throw new Exception("cmd error!");
if (res == -1)
throw new Exception("ID error!");
IsConnected = true;
}
public void AddVirturalCard(string cardno)
{
int cardid = int.Parse(cardno);
int res = ClientSocket.WriteAddCard(1, IP, cardid / 65536, cardid % 65536,
2, 0, 1, 1);
if (res == -2)
{
IsConnected = false;
throw new Exception("Connection error!");
}
if (res >= 1)
throw new Exception("cmd error!");
if (res == -1)
throw new Exception("ID error!");
IsConnected = true;
}
public void AddCard(string cardno)
{
uint cardid = uint.Parse(cardno);
int res= ClientSocket.WriteAddCard(1, IP, (int)cardid / 65536, (int)cardid % 65536,
0, 0, 1,1);
if (res == -2)
{
IsConnected = false;
throw new Exception("Connection error!");
}
if (res >= 1)
throw new Exception("cmd error!");
if (res == -1)
throw new Exception("ID error!");
IsConnected = true;
}
public void DeleteCard(string cardno)
{
int cardid = int.Parse(cardno);
int res = ClientSocket.WriteAddCard(1, IP, cardid / 65536, cardid % 65536,
0, 0, 1, 2);
if (res == -2)
{
IsConnected = false;
throw new Exception("Connection error!");
}
if (res >= 1)
throw new Exception("cmd error!");
if (res == -1)
throw new Exception("ID error!");
IsConnected = true;
}
public void SetSuperOpenDoorPassword(int password)
{
int res = ClientSocket.WriteOpenDoorPassword(1, IP, password);
if (res == -2)
{
IsConnected = false;
throw new Exception("Connection error!");
}
if (res >= 1)
throw new Exception("cmd error!");
if (res == -1)
throw new Exception("ID error!");
IsConnected = true;
}
public void SetOpenDoorDetectionAlarmTime(int sec)
{
int res = ClientSocket.WriteOpenDoorDetectionAlarmTime(1,IP,(byte)sec);
if (res == -2)
{
IsConnected = false;
throw new Exception("Connection error!");
}
if (res >= 1)
throw new Exception("cmd error!");
if (res == -1)
throw new Exception("ID error!");
IsConnected = true;
}
public void SetOpenDoorTimeoutDetectionTime(int sec)
{
int res = ClientSocket.WriteOpenDoorTimeoutDetectionTime(1, IP, (byte)sec);
if (res == -2)
{
IsConnected = false;
throw new Exception("Connection error!");
}
if (res >= 1)
throw new Exception("cmd error!");
if (res == -1)
throw new Exception("ID error!");
IsConnected = true;
}
public void SetOpenDoorAutoCloseTime(int sec)
{
int res = ClientSocket.WriteOpenDoorACKTime(1, IP, (byte)sec);
if (res == -2)
{
IsConnected = false;
throw new Exception("Connection error!");
}
if (res >= 1)
throw new Exception("cmd error!");
if (res == -1)
throw new Exception("ID error!");
IsConnected = true;
}
}
}