fix db; Adding ENV Variable

This commit is contained in:
Luca Lutz 2022-11-04 19:07:35 +01:00
parent 2c2c990e2a
commit a19a4e1f06
2 changed files with 6 additions and 18 deletions

View File

@ -22,19 +22,19 @@ def has_permission(user_permissions, machine_id):
return False
def gen_display_name(user):
# display names must be not longer than 8 chrs
if 'firstName' in user.keys() and 'lastName' in user.keys():
full_name = f'{user["firstName"]} {user["lastName"]}'
if len(full_name) > 8:
full_name = 'f{user["firstName"][0]}.{user["lastName"][:6]}'
display_name = f'{user["firstName"][0]}.{user["lastName"][:6]}'
else:
try:
display_name = user['username'][:8]
except KeyError:
print('user has no username')
return 'Error'
return(display_name)
def handle_request(msg, client):
print('')
@ -48,19 +48,12 @@ def handle_request(msg, client):
MqttHandler.print_to_display(reader_id, 16, fabcard_id)
return
# TODO Database
# - get machine_id from database
# - get last_user from database
# - get machine_status from database, is 0 for off, 1 for on
# - get plug_id from database
db_data = SQLHandler.get_machine_data(reader_id)
#print(data)
machine_id = db_data["machine_id"]
last_user = data["last_user"]
machine_status = data["machine_status"]
plug_id = data["plug_id"]
last_user = db_data["last_user"]
machine_status = db_data["machine_status"]
plug_id = db_data["plug_id"]
try:
user_permissions = json.loads(user['attributes']['FabPermissions'][0])
@ -82,7 +75,6 @@ def handle_request(msg, client):
MqttHandler.switch_plug(plug_id, 1)
MqttHandler.print_to_display(reader_id, 20, f'Login\n{display_name}')
else:
print("user1"+last_user+"user2"+username)
if not (username == last_user or KeycloakHandler.user_is_privileged(username)):
MqttHandler.print_to_display(reader_id, 9, last_user)
return
@ -90,10 +82,7 @@ def handle_request(msg, client):
MqttHandler.switch_plug(plug_id, 0)
MqttHandler.print_to_display(reader_id, 20, f'Bitte anmelden')
# TODO Update Database:
# - last user
# - machine_status
SQLHandler.update_machine(reader_id, last_user, machine_status)
SQLHandler.update_machine(reader_id, username, machine_status)
MqttHandler.publish(f'/FabLogging/{plug_id}/USER', username)

View File

@ -79,5 +79,4 @@ class SQLHandler:
machines = json.loads(Config.machines)
for machine in machines:
SQLHandler.cursor.execute("INSERT INTO readerplug (reader_id, plug_id, machine_id, machine_status, last_user) VALUES (%s, %s, %s, False, 'no_user');", (machine[0],machine[1],machine[2]))
print("-------------")
SQLHandler.conn.commit()