Skip to content

Commit 284bed6

Browse files
committed
Updated internal DLL and changed code conventions and comments
1 parent d148f30 commit 284bed6

11 files changed

+124
-111
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cards/__pycache__

Cards/CardDetails.py

-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ class CardDetails:
22
"""Card Details Class"""
33
def __init__(self, user_id):
44
self.user_id = user_id
5-

Cards/CardReader.py

+36-35
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
1-
from Cards.ReaderSettings import ReaderSettings
2-
from Cards.ReaderCredentials import ReaderCredentials
3-
from Cards.NativeBaseDriver import *
4-
from Cards.CardDetails import CardDetails
5-
from Cards.CardTapResponse import CardTapResponse
1+
from cards.ReaderSettings import ReaderSettings
2+
from cards.ReaderCredentials import ReaderCredentials
3+
from cards.NativeBaseDriver import *
4+
from cards.CardDetails import CardDetails
5+
from cards.CardTapResponse import CardTapResponse
6+
67

78
class ReaderStatus:
8-
"""Card Tap Error Enum"""
9-
Disconnected = 1
10-
Connected = 2
11-
AlreadyInUse = 3
9+
"""Card Tap Error Enum"""
10+
Disconnected = 1
11+
Connected = 2
12+
AlreadyInUse = 3
13+
1214

1315
class CardReader:
14-
def __init__(self, reader_settings, reader_credentials):
15-
self.__instance = c_void_p(0)
16-
self.reader_credentials = reader_credentials
17-
self.on_card_tap = None
18-
self.on_status_change = None
19-
20-
if reader_settings is None:
21-
self.reader_settings = ReaderSettings(get_devices_list()[0])
22-
else:
23-
self.reader_settings = reader_settings
24-
25-
create_instance_by_name(addressof(self.__instance), self.reader_settings.device_name, self.reader_credentials.api_key)
26-
27-
def __del__(self):
28-
destroy_instance(self.__instance)
29-
30-
def listen(self):
31-
run_on_card_present(self.__instance, self.__get_internal_card_tap_handler(), self.on_status_change)
32-
33-
def __get_internal_card_tap_handler(self):
34-
def internal_card_tap_handler(card_tap_response):
35-
response = CardTapResponse(card_tap_response.isSuccess, card_tap_response.errorCode, CardDetails(card_tap_response.uid[:24].decode()))
36-
37-
self.on_card_tap(response)
38-
39-
return internal_card_tap_handler
16+
def __init__(self, reader_settings, reader_credentials):
17+
self.reader_credentials = reader_credentials
18+
self.on_card_tap = None
19+
self.on_status_change = None
20+
21+
if reader_settings is None:
22+
self.reader_settings = ReaderSettings(get_devices_list()[0])
23+
else:
24+
self.reader_settings = reader_settings
25+
26+
self.__instance = create_instance_by_name(self.reader_settings.device_name, self.reader_credentials.api_key)
27+
28+
def __del__(self):
29+
destroy_instance(self.__instance)
30+
31+
def listen(self):
32+
run_on_card_present(self.__instance, self.__get_internal_card_tap_handler(), self.on_status_change)
33+
34+
def __get_internal_card_tap_handler(self):
35+
def internal_card_tap_handler(card_tap_response):
36+
response = CardTapResponse(card_tap_response.isSuccess, card_tap_response.errorCode, CardDetails(card_tap_response.uid[:24].decode()))
37+
38+
self.on_card_tap(response)
39+
40+
return internal_card_tap_handler

Cards/CardTapResponse.py

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
class CardTapError:
2-
"""Card Tap Error Enum"""
3-
InternetError = 1
4-
ApiKeyInvalid = 2
5-
TransactionTokenInvalid = 3
6-
UserNotAssociatedWithCardReader = 4
7-
NoSuchTPID = 5
8-
TransactionTokenMissing = 6
9-
TransactionTokenDoesntExist = 7
10-
TransactionTokenAlreadyUsed = 8
11-
TransactionTokenAlreadyValidated = 9
12-
2+
"""Card Tap Error Enum"""
3+
InternetError = 1
4+
ApiKeyInvalid = 2
5+
TransactionTokenInvalid = 3
6+
UserNotAssociatedWithCardReader = 4
7+
NoSuchTPID = 5
8+
TransactionTokenMissing = 6
9+
TransactionTokenDoesntExist = 7
10+
TransactionTokenAlreadyUsed = 8
11+
TransactionTokenAlreadyValidated = 9
12+
13+
1314
class CardTapResponse:
14-
"""Card Tap Response Class"""
15-
def __init__(self, is_success, error, card_details):
16-
self.is_success = is_success
17-
self.error = error
18-
self.card_details = card_details
15+
"""Card Tap Response Class"""
16+
def __init__(self, is_success, error, card_details):
17+
self.is_success = is_success
18+
self.error = error
19+
self.card_details = card_details

Cards/CardsBase.dll

-433 KB
Binary file not shown.

Cards/NativeBaseDriver.py

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from ctypes import *
22

3+
34
class DevicesList(Structure):
45
_fields_ = [("names", POINTER(c_char_p)),
56
("count", c_int)]
67

8+
79
class CardTapResponse(Structure):
810
_fields_ = [("isSuccess", c_int),
911
("errorCode", c_int),
@@ -19,41 +21,34 @@ class CardTapResponse(Structure):
1921
freeDevicesList = CardsBase.freeDevicesList
2022

2123
getDevicesList.restype = DevicesList
22-
createInstanceByName.restype = c_int
24+
createInstanceByName.restype = c_void_p
2325
destroyInstance.restype = c_int
2426
runOnCardPresent.restype = None
2527
freeDevicesList.restype = None
2628

2729
CARD_TAP_FUNC = WINFUNCTYPE(None, CardTapResponse)
2830
STATUS_CHANGE_FUNC = WINFUNCTYPE(None, c_int)
2931

32+
3033
def get_devices_list():
3134
devices_list_struct = getDevicesList()
3235
devices_list = []
3336

3437
for i in range(devices_list_struct.count):
35-
devices_list.append(devices_list_struct.names[i].decode())
38+
devices_list.append(devices_list_struct.names[i].decode())
3639

3740
freeDevicesList(devices_list_struct)
38-
41+
3942
return devices_list
4043

41-
# instance = addressof, a pointer to the instance (address of c_void_p)
42-
# device_name = str
43-
# api_key = str
44-
def create_instance_by_name(instance, device_name, api_key):
45-
return createInstanceByName(instance, c_char_p(device_name.encode()), c_char_p(api_key.encode()))
4644

47-
# instance = c_void_p
48-
def destroy_instance(instance):
45+
def create_instance_by_name(device_name, api_key):
46+
return createInstanceByName(c_char_p(device_name.encode()), c_char_p(api_key.encode()))
47+
48+
49+
def destroy_instance(instance):
4950
return destroyInstance(instance)
5051

51-
# instance = c_void_p
52-
# card_tap_handler = CARD_TAP_FUNC
53-
# status_change_handler = STATUS_CHNAGE_FUNC
52+
5453
def run_on_card_present(instance, card_tap_handler, status_change_handler):
5554
runOnCardPresent(instance, CARD_TAP_FUNC(card_tap_handler), STATUS_CHANGE_FUNC(status_change_handler))
56-
57-
58-
59-

Cards/ReaderCredentials.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class ReaderCredentials:
22
"""Reader Credentials Class"""
33
def __init__(self, api_key):
4-
self.api_key = api_key
4+
self.api_key = api_key

Cards/ReaderSettings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class ReaderSettings:
22
"""Reader Settings Class"""
33
def __init__(self, device_name):
4-
self.device_name = device_name
4+
self.device_name = device_name

Cards/__init__.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from Cards.CardReader import ReaderStatus, CardReader
2-
from Cards.ReaderCredentials import ReaderCredentials
3-
from Cards.ReaderSettings import ReaderSettings
4-
from Cards.CardTapResponse import CardTapResponse, CardTapError
1+
from cards.CardReader import ReaderStatus, CardReader
2+
from cards.ReaderCredentials import ReaderCredentials
3+
from cards.ReaderSettings import ReaderSettings
4+
from cards.CardTapResponse import CardTapResponse, CardTapError
55

6-
__all__ = ['ReaderStatus' ,'CardReader', 'ReaderCredentials', 'ReaderSettings', 'CardTapResponse']
6+
7+
__all__ = ['ReaderStatus', 'CardReader', 'ReaderCredentials', 'ReaderSettings', 'CardTapResponse']

CardsBase.dll

429 KB
Binary file not shown.

demo.py

+50-35
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,58 @@
1-
from Cards import *
1+
from cards import *
22

3-
4-
#Handle Card Tap event. This event is raised when a user taps his phone on the reader.
5-
#Note that Cards must be installed on the phone.
3+
4+
"""Handles Card Tap event.
5+
This event is raised when a user taps his phone on the reader.
6+
"""
67
def card_tap_handler(card_info):
7-
if not card_info.is_success:
8-
print('Error reading card, Error: {0}'.format(card_info.error))
9-
return
10-
11-
print('Card read, user id: {0}'.format(card_info.card_details.user_id))
12-
13-
'''Your code goes here!
14-
Do whatever you want with the accepted User ID!
15-
16-
-----------------------
17-
Example: Open the door, if the user is authorized
18-
-----------------------
19-
if YourSystem.IsAuthorizedToOpenDoor(card_info.card_details.user_id, Doors.Hallway):
20-
YourSystem.OpenDoor(Doors.Hallway)
21-
22-
-----------------------
23-
Example: Remove balance
24-
----------------------
25-
YourSystem.Users.ChangeBalance(card_info.card_details.user_id, -10);'''
26-
27-
#Handles reader status change.
8+
if not card_info.is_success:
9+
print('Error reading card, Error: %d' % card_info.error)
10+
return
11+
12+
print('Card read, user id: %s' % card_info.card_details.user_id)
13+
14+
'''
15+
Your code goes here!
16+
Do whatever you want with the accepted User ID!
17+
18+
-----------------------
19+
Example: Open the door, if the user is authorized
20+
-----------------------
21+
if YourSystem.IsAuthorizedToOpenDoor(card_info.card_details.user_id, Doors.Hallway):
22+
YourSystem.OpenDoor(Doors.Hallway)
23+
24+
-----------------------
25+
Example: Remove balance
26+
----------------------
27+
YourSystem.Users.ChangeBalance(card_info.card_details.user_id, -10);
28+
'''
29+
30+
31+
"""Handles reader status change."""
2832
def status_change_handler(status):
29-
if status is ReaderStatus.Disconnected:
30-
print('Card reader has been disconnected')
31-
elif status is ReaderStatus.Connected:
32-
print('Card reader has been connected')
33+
if status is ReaderStatus.Disconnected:
34+
print('Card reader has been disconnected')
35+
elif status is ReaderStatus.Connected:
36+
print('Card reader has been connected')
37+
else:
38+
print('Error: unknown status change')
39+
40+
41+
def main():
42+
# Initialize the ReaderCredentials and ReaderSettings objects with wanted / needed parameters
43+
reader_credentials = ReaderCredentials('ABCD1234ABCD1234ABCD1234')
44+
reader_settings = ReaderSettings('ACS - ACR122U PICC Interface')
3345

46+
# Initialize the CardReader object with ReaderCredentials and ReaderSettings.
47+
card_reader = CardReader(reader_settings, reader_credentials)
3448

35-
reader_credentials = ReaderCredentials('ABCD1234ABCD1234ABCD1234')
36-
reader_settings = ReaderSettings('ACS - ACR122U PICC Interface')
49+
# Set the on_card_tap and on_status_change events
50+
card_reader.on_card_tap = card_tap_handler
51+
card_reader.on_status_change = status_change_handler
3752

38-
card_reader = CardReader(reader_settings, reader_credentials)
53+
# Start listening
54+
card_reader.listen()
3955

40-
card_reader.on_card_tap = card_tap_handler
41-
card_reader.on_status_change = status_change_handler
4256

43-
card_reader.listen()
57+
if __name__ == "__main__":
58+
main()

0 commit comments

Comments
 (0)