-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidate.ino
83 lines (69 loc) · 2.25 KB
/
Validate.ino
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
boolean validateCard( String number ) {
// Card validation chain
// 1. Check most recent cached card
// 2. Check local list of master cards
// 3. Check remote server for card
// 4. Fail validation
Serial.println( "." );
if ( validateCache( number ) ) {
Serial.println( F( "Card validate cache" ) );
return true;
} else if ( validateLocal( number ) ) {
Serial.println( F( "Card validated locally" ) );
return true;
} else if ( validateRemote( number, deviceID ) ) {
Serial.println( F( "Card validated remotely" ) );
return true;
}
// Always return false
Serial.println( F( "Card invalid" ) );
return false;
}
boolean validateCache( String number ) {
// Check to see if current card is the previous card and it was valid
if ( number == previous_card_number && previous_card_valid ) {
Serial.println( F( "No change in valid card" ) );
return true;
}
// Always return false
return false;
}
boolean validateLocal( String number ) {
// Itterate through master cards list
for ( int i = 0; i < sizeof( masterCards ) / sizeof( int ); i++ ) {
// Get current card and prepare both strings for comparison
String s = masterCards[i];
number.toLowerCase();
s.toLowerCase();
// Check is both cards match and return true
if ( number.compareTo( s ) == 0 ) return true;
}
// Always return false
return false;
}
boolean validateRemote( String number, String device ) {
// Enable CS to Ethernet shield
EnableSPI_Ethernet();
// Connect to Nodebot
if ( client.connect( nodebotServer, port ) ) {
Serial.println( F( "Connected to server." ) );
// Send device ID and card number to Nodebot
client.println( device );
client.println( number );
// Wait for response from Nodebot, disconnection or timeout
netTimeout.reset();
while( client.connected() && client.available() == 0 && ! netTimeout.check() ) {}
// If there is data process it and disconnect
if ( client.available() ) {
byte data = client.read();
client.stop();
if ( data == 0x31 ) return true;
return false;
}
} else {
Serial.println( F( "Connection Error" ) );
}
// Always disconnect the client and return false
client.stop();
return false;
}