-
Notifications
You must be signed in to change notification settings - Fork 1
/
cards.py
117 lines (101 loc) · 2.96 KB
/
cards.py
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
from module.base import CardReader, CardWriter, CardWiper, BaseCard, CardWiper
import RPi.GPIO as GPIO
import MFRC522
import signal
import sys
class CardBalanceReader(CardReader):
def __init__(self):
super(CardBalanceReader, self).__init__()
def get_card(self):
print("Please place card to reader")
while self.card is None:
self.scan_card()
uid = self.get_uid()
return uid or None
def get_current_balance(self):
amount = 0
if self.card is None:
self.get_card()
block = self.read_sector()
block = block.get('block')
for i in reversed(block):
if i == 0:
break
else:
amount += i
print("Current Balance: {}".format(amount))
return amount
time.sleep(1) # added a time interrupt as a solution on the rapid card scanning issue
class CardBalanceAdder(CardWriter, CardBalanceReader):
def __init__(self):
super(CardBalanceAdder, self).__init__()
def get_deposit(self):
while True:
try:
deposit = int(raw_input("Input your desired deposit... "))
break
except ValueError:
raise Except("Deposit must be a whole number, not in decimal or letters")
return deposit or None
def add_balance(self, deposit=None):
while deposit is None or deposit == 0:
deposit = self.get_deposit()
amount = 0
if self.card is None:
self.get_card()
block = self.read_sector()
block = block.get('block')
for i in reversed(block):
if i == 0:
break
else:
amount += i
new_amount = amount + deposit
self.write_sector(amount=new_amount)
print("New Balance: {}".format(new_amount))
return new_amount
class CardBalanceReducer(CardWriter, CardBalanceReader):
def __init__(self):
super(CardBalanceReducer, self).__init__()
def check_card_present(obj):
if obj.card is None:
return False
return True
def scan_card(self):
# print("Scanning for a card...")
(status, TagType) = self.MFReader.MFRC522_Request(self.MFReader.PICC_REQIDL)
if status == self.MFReader.MI_OK:
print("Card detected")
self.card = 1
return True
return None
def get_uid(self):
(status, uid) = self.MFReader.MFRC522_Anticoll()
if status == self.MFReader.MI_OK:
self.uid = uid
return {"uid": uid}
return None
#def get_custom_fee(self):
# while True:
# try:
# fee = int(raw_input("Input your desired fee ..."))
# break
# except ValueError:
# raise Except("Fee must be in whole number")
# return fee or None
# Reduce current amount
def reduce_balance(self, fee=None):
#while fee is None or fee == 0:
# fee = self.get_custom_fee()
current_balance = self.get_current_balance()
if current_balance < fee:
print("Ooops! Insufficient balance!")
return
new_balance = current_balance - fee
self.write_sector(amount=new_balance)
print("New Balance: {}".format(new_balance))
time.sleep(3)
return new_balance
class CardBalanceWiper(CardWiper, CardBalanceReader):
def __init__(self):
super(CardBalanceWiper, self).__init__()