-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
336 lines (264 loc) · 10.4 KB
/
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import os
import platform
import sys
import xmlrpc.client
from time import strftime, localtime, sleep
import apsw
import psutil
from PySide2 import QtCore, QtGui
from PySide2.QtCore import QTimer, QObject, Signal
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
from watchdog.events import FileSystemEventHandler, FileSystemEvent
from watchdog.observers import Observer
title = "*RFID Badge* - attendance system terminal"
version = "Version 0.7a - 25/08/2022"
# Odoo Data
odooIP = "your_ip" # docker odoo ip on this pc
odooDATA = "your_db"
odooUSER = "your_user"
odooPASS = "your_password"
odooPORT = "8069"
odooURL = "http://" + odooIP
# timers intervals
trigger_clock = 500
trigger_odooping = 60000
trigger_odooauth = 240000
trigger_rfid_process = 40000
# rfid parameters
rfid_watchdog_path = "/opt/RFID_EVENTS/"
# apsw - Sqlite3 db path and file name.
apsw_db_file = "/your_path/db.db"
# initialize Sqlite DB
db = apsw.Connection(apsw_db_file)
cursor = db.cursor()
class Odoo():
def __init__(self):
self.DATA = odooDATA # db name
self.USER = odooUSER # email address
self.PASS = odooPASS # password
self.PORT = odooPORT # port
self.URL = odooURL # base url
self.URL_COMMON = "{}:{}/xmlrpc/2/common".format(
self.URL, self.PORT)
self.URL_OBJECT = "{}:{}/xmlrpc/2/object".format(
self.URL, self.PORT)
def authenticateOdoo(self):
self.ODOO_COMMON = xmlrpc.client.ServerProxy(self.URL_COMMON)
self.ODOO_OBJECT = xmlrpc.client.ServerProxy(self.URL_OBJECT)
self.UID = self.ODOO_COMMON.authenticate(
self.DATA
, self.USER
, self.PASS
, {})
def trigger_attendance_byRFIDCard(self, rfid_code):
result = self.ODOO_OBJECT.execute_kw(
self.DATA
, self.UID
, self.PASS
, 'hr.employee'
, 'register_attendance'
, [[rfid_code]])
return result
class clock(QObject):
timetick = Signal(str)
def __init__(self):
super().__init__()
# Define timer.
self.timer = QTimer()
self.timer.setInterval(trigger_clock) # msecs 100 = 1/10th sec
self.timer.timeout.connect(self.update_time)
self.timer.start()
def update_time(self):
# Pass the current time to QML.
curr_time = strftime("%H:%M:%S", localtime())
self.timetick.emit(curr_time)
class OdooMachinePINGCheck(QObject):
isOdooUp = Signal(bool)
def __init__(self):
super().__init__()
# Define timer.
self.timer = QTimer()
self.timer.setInterval(trigger_odooping) # msecs 100 = 1/10th sec
self.timer.timeout.connect(self.pingThatServer)
self.timer.start()
def pingThatServer(self):
current_os = platform.system().lower()
status = ""
if current_os == "windows":
parameter = "-n"
else:
parameter = "-c"
ip = odooIP
exit_code = os.system(f"ping {parameter} 1 -w2 {ip} > /dev/null 2>&1")
if exit_code:
status = False
else:
status = True
self.isOdooUp.emit(status)
class OdooAuthCheck(QObject):
isOdooAuth = Signal(bool)
def __init__(self):
super().__init__()
# Define timer.
self.timer = QTimer()
self.timer.setInterval(trigger_odooauth) # msecs 100 = 1/10th sec
self.timer.timeout.connect(self.tryOdooAuth)
self.timer.start()
def tryOdooAuth(self):
od = Odoo()
status = True
try:
od.authenticateOdoo()
print("Authenticating")
except:
print("Login FAILED")
status = False
self.isOdooAuth.emit(status)
class RfidProcessCheck(QObject):
isRfidReaderRunning = Signal(bool)
def __init__(self):
super().__init__()
# Define timer.
self.timer = QTimer()
self.timer.setInterval(trigger_rfid_process) # msecs 100 = 1/10th sec
self.timer.timeout.connect(self.checkIfProcessRunning)
self.timer.start()
def checkIfProcessRunning(self):
status = False
for proc in psutil.pids():
try:
if ("rfid").lower() in psutil.Process(proc).cmdline()[1].lower():
print("Rfid reader process FOUND - returning true!")
print(psutil.Process(proc).cmdline()[1])
status = True
self.isRfidReaderRunning.emit(status)
return status
except:
pass
print("no rfid process detected - return FALSE")
self.isRfidReaderRunning.emit(status)
return status
# watchdog classes to handle the rfid hardware loop
class Bridge(QObject):
created = Signal(FileSystemEvent)
class Handler(FileSystemEventHandler):
def __init__(self):
super().__init__()
self.bridge = Bridge()
def on_created(self, event):
self.bridge.created.emit(event)
class rfidCheck(QObject):
rfidCheckResult = Signal(bool)
rfidAction = Signal(str)
rfidCardID = Signal(str)
rfidDate = Signal(str)
rfidReset = Signal(bool)
def __init__(self):
# Define timer that start after a login and stops with reset method invoked
self.reset_timer = QTimer()
self.reset_timer.setInterval(2000) # msecs 100 = 1/10th sec
self.reset_timer.timeout.connect(self.resetRfidArea)
#self.timer.start()
super().__init__()
self.handler = Handler()
self.handler.bridge.created.connect(self.handle_created)
self.observer = Observer()
self.observer.schedule(self.handler, rfid_watchdog_path, recursive=True)
self.observer.start()
def handle_created(self, event):
status = False
# STEP 0 = check for invalid filename
if len(str(event.src_path)) != 52:
status = False
return status
#print("handle_created triggered")
# print(f"created {event.src_path}")
# print(event.event_type)
# STEP 1 extract ID card string from event
sampled = str(event.src_path)[17:]
card_id = sampled[:12]
event_date = sampled[13:] #dt_string = now.strftime("%Y_%m_%d-%I:%M:%S_%p")
print("Card ID: " + card_id)
print("Event Date : " + event_date)
# STEP 2 trigger Odoo attendance method
od = Odoo()
data = False
try:
od.authenticateOdoo()
print("Odoo Auth OK")
data = (od.trigger_attendance_byRFIDCard(card_id))
print(data)
except:
#If Odoo upload fails, we rename the event file and update sqlite db
print("Odoo Event upload FAILED")
os.rename(str(event.src_path),(str(event.src_path)+"_UPLOAD_FAIL"))
sql="INSERT INTO attendance (card, date, uploaded) VALUES(?,?,?)"
cursor.execute(sql, (card_id, event_date, 0))
status = False
self.rfidCheckResult.emit(status)
self.rfidAction.emit("ODOO ERROR")
self.rfidCardID.emit(card_id)
self.rfidDate.emit(event_date)
self.reset_timer.start()
return status
# STEP 3 If we are HERE Odoo upload is successful, but the login could have failed. Do Data Checks
if str(data.get("error_message"))=="": # odoo login successfull
action = data.get("action")
print("The employee with card id "+card_id+" has registered this action: "+action)
print("Uploading succesful login to sqlite db and renaming file")
os.rename(str(event.src_path),(str(event.src_path)+"_OK"))
sql="INSERT INTO attendance (card, date, uploaded) VALUES(?,?,?)"
cursor.execute(sql, (card_id, event_date, 1))
status = True
self.rfidCheckResult.emit(status)
self.rfidAction.emit(action)
self.rfidCardID.emit(card_id)
self.rfidDate.emit(event_date)
self.reset_timer.start()
return status
else: # invalid card ID
error = data.get("error_message")
action = data.get("action")
print("CARD ID NOT VALID - LOGIN FAILED - "+error)
os.rename(str(event.src_path),(str(event.src_path)+"_LOGIN_FAIL"))
# NOT registering to sqlite the invalid logins
status = False
self.rfidCheckResult.emit(status)
self.rfidAction.emit("CARD ERROR")
self.rfidCardID.emit("* * *")
self.rfidDate.emit(event_date)
self.reset_timer.start()
return status
def resetRfidArea(self):
self.reset_timer.stop()
self.rfidAction.emit("WAITING CARD")
self.rfidCardID.emit("- - -")
self.rfidDate.emit("- - -")
self.rfidReset.emit(True)
return
# end of classes
# start main methods and process
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.quit.connect(app.quit)
engine.load('main.qml')
# Define our backend object, which we pass to QML
running_clock = clock()
odoopingcheck = OdooMachinePINGCheck()
odooauthcheck = OdooAuthCheck()
rfidCheck = rfidCheck()
rfid_process_check = RfidProcessCheck()
engine.rootObjects()[0].setProperty('title', title)
engine.rootObjects()[0].setProperty('version', version)
engine.rootObjects()[0].setProperty('running_clock', running_clock)
engine.rootObjects()[0].setProperty('odoopingcheck', odoopingcheck)
engine.rootObjects()[0].setProperty('odooauthcheck', odooauthcheck)
engine.rootObjects()[0].setProperty('rfid_process_check', rfid_process_check)
engine.rootObjects()[0].setProperty('rfidCheck', rfidCheck)
# Initial call to trigger first update. Must be after the setProperty to connect signals.
running_clock.update_time()
odoopingcheck.pingThatServer()
odooauthcheck.tryOdooAuth()
rfid_process_check.checkIfProcessRunning()
sys.exit(app.exec_())