mirror of
https://gitlab.com/sfz.aalen/infra/fabaccess.git
synced 2025-03-11 22:41:48 +01:00
DB part 2
This commit is contained in:
parent
8b10defed2
commit
2c2c990e2a
@ -21,6 +21,8 @@ services:
|
||||
MQTT_PASSWORD: ${MQTT_PASSWORD:?err}
|
||||
MQTT_BROKER: ${MQTT_BROKER:?err}
|
||||
MQTT_CLIENT: ${MQTT_CLIENT:?err}
|
||||
# Various
|
||||
MACHINES: ${MACHINES:?err}
|
||||
restart: unless-stopped
|
||||
|
||||
db:
|
||||
|
@ -22,4 +22,6 @@ class Config:
|
||||
db_user_name = _read_from_env('DB_USERNAME')
|
||||
db_password = _read_from_env('DB_PASSWORD')
|
||||
db_database = _read_from_env('DB_DATABASE')
|
||||
db_port = int(_read_from_env('DB_PORT','5432'))
|
||||
db_port = int(_read_from_env('DB_PORT','5432'))
|
||||
|
||||
machines = _read_from_env('MACHINES')
|
@ -35,7 +35,7 @@ class KeycloakHandler:
|
||||
|
||||
@staticmethod
|
||||
def user_is_privileged(username):
|
||||
groups = KeycloakHandler.admin.get_user_groups(user_id=username)
|
||||
groups = KeycloakHandler.admin.get_user_groups(user_id=KeycloakHandler.admin.get_user_id(username))
|
||||
groups = [group['name'] for group in groups]
|
||||
|
||||
if 'Mentoren' in groups:
|
||||
|
@ -41,7 +41,8 @@ def handle_request(msg, client):
|
||||
print(f'Received `{msg.payload.decode()}` from `{msg.topic}` topic')
|
||||
fabcard_id = json.loads(msg.payload.decode())['UID']
|
||||
reader_id = msg.topic.split('/')[-1]
|
||||
|
||||
|
||||
KeycloakHandler.login()
|
||||
user = KeycloakHandler.get_user_by_card_id(fabcard_id)
|
||||
if not user:
|
||||
MqttHandler.print_to_display(reader_id, 16, fabcard_id)
|
||||
@ -53,12 +54,13 @@ def handle_request(msg, client):
|
||||
# - get machine_status from database, is 0 for off, 1 for on
|
||||
# - get plug_id from database
|
||||
|
||||
print(SQLHandler.get_machine_data(reader_id))
|
||||
db_data = SQLHandler.get_machine_data(reader_id)
|
||||
#print(data)
|
||||
|
||||
machine_id = 'space.foo.lazerspacer'
|
||||
last_user = 'foo.bar'
|
||||
machine_status = 0 # or 1
|
||||
plug_id = 'lazerspacer'
|
||||
machine_id = db_data["machine_id"]
|
||||
last_user = data["last_user"]
|
||||
machine_status = data["machine_status"]
|
||||
plug_id = data["plug_id"]
|
||||
|
||||
try:
|
||||
user_permissions = json.loads(user['attributes']['FabPermissions'][0])
|
||||
@ -75,11 +77,12 @@ def handle_request(msg, client):
|
||||
username = user['username']
|
||||
display_name = gen_display_name(user)
|
||||
|
||||
if machine_status == 0:
|
||||
if not machine_status:
|
||||
print(f'Turn Plug {plug_id} on')
|
||||
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,6 +93,7 @@ def handle_request(msg, client):
|
||||
# TODO Update Database:
|
||||
# - last user
|
||||
# - machine_status
|
||||
SQLHandler.update_machine(reader_id, last_user, machine_status)
|
||||
MqttHandler.publish(f'/FabLogging/{plug_id}/USER', username)
|
||||
|
||||
|
||||
@ -101,7 +105,6 @@ def main():
|
||||
MqttHandler.connect_mqtt()
|
||||
MqttHandler.subscribe("/rfid_reader/#")
|
||||
SQLHandler.init_db()
|
||||
|
||||
MqttHandler.loop()
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -1,5 +1,6 @@
|
||||
import psycopg2
|
||||
from psycopg2 import sql, extensions
|
||||
import json
|
||||
|
||||
from config import Config
|
||||
|
||||
@ -25,16 +26,24 @@ class SQLHandler:
|
||||
# - get last_user from database
|
||||
# - get machine_status from database, is 0 for off, 1 for on
|
||||
# - get plug_id from database
|
||||
SQLHandler.cursor.execute("SELECT machine_id,last_user,machine_status,plug_id FROM readerplug WHERE reader_id = %s;", (reader_id,))
|
||||
SQLHandler.cursor.execute("SELECT * FROM readerplug WHERE readerplug.reader_id = %s;", (reader_id,))
|
||||
data = [row for row in SQLHandler.cursor.fetchall()]
|
||||
print(data)
|
||||
return {
|
||||
'machine_id': data[0],
|
||||
'last_user': data[1],
|
||||
'machine_status': data[2],
|
||||
'plug_id': data[3]
|
||||
}
|
||||
if(len(data) > 0):
|
||||
return {
|
||||
'machine_id': data[0][2],
|
||||
'last_user': data[0][4],
|
||||
'machine_status': data[0][3],
|
||||
'plug_id': data[0][0]
|
||||
}
|
||||
else:
|
||||
print("No maching Card Reader found in db")
|
||||
return "Error"
|
||||
|
||||
@staticmethod
|
||||
def update_machine(reader_id,last_user,machine_status):
|
||||
SQLHandler.cursor.execute("UPDATE readerplug SET machine_status = %s WHERE readerplug.reader_id = %s", (False if machine_status else True,reader_id))
|
||||
SQLHandler.cursor.execute("UPDATE readerplug SET last_user = %s WHERE readerplug.reader_id = %s", (last_user,reader_id))
|
||||
SQLHandler.conn.commit()
|
||||
|
||||
@staticmethod
|
||||
def init_db():
|
||||
@ -62,4 +71,13 @@ class SQLHandler:
|
||||
)")
|
||||
else:
|
||||
print("Found Table -> Using existing one")
|
||||
SQLHandler.conn.commit()
|
||||
|
||||
SQLHandler.cursor.execute("SELECT * FROM readerplug;")
|
||||
if(len(SQLHandler.cursor.fetchall()) < 1):
|
||||
print("Found no machines in table, adding machines from config")
|
||||
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()
|
Loading…
x
Reference in New Issue
Block a user