-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKM4K.py
executable file
·163 lines (139 loc) · 4.41 KB
/
KM4K.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
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
#!/usr/bin/python3
import nfc
import binascii
import sqlite3
import sys
import RPi.GPIO as GPIO
import time
import rb303 as servo
import requests
import json
import os
suica = nfc.clf.RemoteTarget("212F")
suica.sensf_req = bytearray.fromhex("0000030000")
def sql_add(cur, name, idm):
cur.execute("INSERT INTO users (name, idm) VALUES (?, ?)", (name, idm))
def sql_del(res):
for row in res:
print(row)
def add_nfc(cur):
name = input("name> ")
print("Touch your Suica")
idm = read_nfc()
if idm:
cur.execute("SELECT * FROM users WHERE idm=?", (idm,))
res = cur.fetchall()
if len(res) > 0:
print("This key has already registered.")
else:
sql_add(cur, name, idm)
print("Registered (idm:" + idm.decode() + ")")
def delete_nfc(cur):
name = input("name> ")
cur.execute("SELECT * FROM users WHERE name=?", (name,))
res = cur.fetchall()
if len(res) == 0:
print("Unregistered name:" + name)
else:
# sql_del(res)
cur.execute("DELETE FROM users WHERE name=?", (name,))
print("Deleted (name:" + name + ")")
def read_nfc():
while True:
with nfc.ContactlessFrontend("usb") as clf:
target = clf.sense(suica, iterations=3, interval=1.0)
while target:
tag = nfc.tag.activate(clf, target)
tag.sys = 3
idm = binascii.hexlify(tag.idm)
return idm
def check_card_manager(idm):
url = os.environ["VERIFY_API_URL"]
payload = json.dumps({
"idm": idm
})
headers = {
'X-Api-Key': os.environ["API_KEY"],
'Content-Type': 'application/json'
}
try:
response = requests.request("GET", url, headers=headers, data=payload)
status = json.loads(response.text)
if status['verified'] is not None and status['verified']:
return True
return False
except Exception as e:
print(e)
return False
def start_system(cur, isopen, okled_pin, ngled_pin):
while True:
idm = read_nfc()
if idm:
# Card Managerで登録されているか確認
isRegisteredSSO = check_card_manager(idm.decode())
print("is registered sso", isRegisteredSSO)
cur.execute("SELECT * FROM users WHERE idm=?", (idm,))
res = cur.fetchall()
if len(res) > 0 or isRegisteredSSO:
print("Registered (idm:" + idm.decode() + ")")
GPIO.output(okled_pin, GPIO.HIGH)
time.sleep(0.1)
GPIO.output(okled_pin, GPIO.LOW)
time.sleep(0.1)
GPIO.output(okled_pin, GPIO.HIGH)
time.sleep(0.1)
GPIO.output(okled_pin, GPIO.LOW)
if not isopen:
servo.open()
isopen = not isopen
print("open")
else:
servo.lock()
isopen = not isopen
print("lock")
time.sleep(1.7)
else:
print("Unregistered (idm:" + idm.decode() + ")")
GPIO.output(ngled_pin, GPIO.HIGH)
time.sleep(0.1)
GPIO.output(ngled_pin, GPIO.LOW)
time.sleep(0.1)
GPIO.output(ngled_pin, GPIO.HIGH)
time.sleep(0.1)
GPIO.output(ngled_pin, GPIO.LOW)
time.sleep(1.7)
def main(argv):
mode = 2
dbname = "database.db"
conn = sqlite3.connect(dbname)
cur = conn.cursor()
isopen = False
okled_pin = 19
ngled_pin = 26
servo.reset()
GPIO.setmode(GPIO.BCM)
GPIO.setup(okled_pin, GPIO.OUT)
GPIO.setup(ngled_pin, GPIO.OUT)
if len(argv) == 2:
mode = int(argv[1])
try:
cur.execute(
"CREATE TABLE IF NOT EXISTS users (name TEXT NOT NULL, idm BLOB NOT NULL, date TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP)"
)
if mode == 0:
print("Add User")
add_nfc(cur)
elif mode == 1:
print("Delete User")
delete_nfc(cur)
else:
print("Welcome to Koken Kagi System")
start_system(cur, isopen, okled_pin, ngled_pin)
except Exception as e:
print("An error has occured!")
print(e)
finally:
conn.commit()
conn.close()
if __name__ == "__main__":
main(sys.argv)