-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht-main.py
141 lines (108 loc) · 3.83 KB
/
t-main.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
import mysql.connector
from mysql.connector import Error
import json
import threading
from numpy import insert
from zk import ZK
from matplotlib.pyplot import table
from mysql.connector import pooling
def create_connection(
host_name,
user_name,
user_password,
):
"""connecting to the SQL server"""
connection = None
try:
connection = mysql.connector.connect(
host=host_name,
user=user_name,
passwd=user_password,
)
print("Connection to MySQL DB successful")
except Error as e:
print(f"The error '{e}' occurred")
return connection
connection = create_connection(
"localhost",
"root",
"Tejusp@08",
)
"""Creating a database"""
def run_query(connection, query):
cursor = connection.cursor()
try:
cursor.execute(query)
print(query, "run successfully")
except Error as e:
print(f"The error '{e}' occurred")
create_database_query = "CREATE DATABASE IF NOT EXISTS trial1;"
run_query(connection, create_database_query)
"""connecting to the database in server"""
def make_connection(host_name, user_name, user_password, db_name):
connection = None
try:
connection = mysql.connector.connect(
host=host_name, user=user_name, passwd=user_password, database=db_name
)
print("Connection to MySQL DB ", db_name, " successful")
except Error as e:
print(f"The error '{e}' occurred")
return connection
"""CREATE TABLE """
connection = make_connection("localhost", "root", "Tejusp@08", "trial1")
create_table_query = "CREATE TABLE IF NOT EXISTS USERS1 (UID VARCHAR(20), Name VARCHAR(100), Time TIMESTAMP(0), PRIMARY KEY(UID) )"
run_query(connection, create_table_query)
"""ADD VALUES TO TABLE FROM JSON FILE"""
with open("users.json") as f:
u = json.load(f)
f.close()
for val in u:
print(val, u[val], "123123" "\n")
add_values = f"INSERT INTO USERS1 VALUES ('{val}','{u[val]}','123123')"
run_query(connection, add_values)
def add_data(Uid, Name, Time):
add_data_query = f"INSERT INTO USERS1 VALUES ('{Uid}','{Name}','{Time}')"
run_query(connection, add_data_query)
add_data("001212", "Tejus", "1221")
"""ADDING LIVE CAPTURE TO THE DATABASE"""
conn1 = None
ips = [f"172.24.10.10{i}" for i in range(1, 5)]
def capture(ip):
zk = ZK(ip, port=4370, timeout=5, password=0, force_udp=False, ommit_ping=False)
# db.add_connection()
# db_conn = db.get_connection()
try:
conn1 = zk.fconnect()
print(f"Starting live capture {ip}")
for attendance in conn1.live_capture():
print(f"{ip}: {attendance}")
# l.write(f"{ip}: {attendance}\n")
if attendance is None:
pass
else:
try:
# attendance_log.append(attendance)
print(
f"UID:{attendance.user_id} name:{u[attendance.user_id]} time:{attendance.timestamp} status:{attendance.status} punch:{attendance.punch} device:{ip}"
)
add_data(
attendance.user_id, u[attendance.user_id], attendance.timestamp
)
except:
print(
f"UID:{attendance.user_id} name:{attendance.user_id} time:{attendance.timestamp} status:{attendance.status} punch:{attendance.punch} device:{ip}\n"
)
add_data(
attendance.user_id, u[attendance.user_id], attendance.timestamp
)
except Exception as e:
print(f"Process terminate : {e}")
finally:
if conn1:
conn1.disconnect()
# capture(ips[0])
for ip in ips:
threading.Thread(target=capture, args=(ip,)).start()
connection.commit()
connection.close()