more of the previous
parent
a945e620e1
commit
fc5bb08a0d
File diff suppressed because it is too large
Load Diff
|
|
@ -73,7 +73,7 @@ CREATE TABLE `DataBlock` (
|
||||||
-- id of the owner of this file. Ownership is not transferable because ids may collide,
|
-- id of the owner of this file. Ownership is not transferable because ids may collide,
|
||||||
-- but the owner could be null for global scope
|
-- but the owner could be null for global scope
|
||||||
`owner_id` TEXT,
|
`owner_id` TEXT,
|
||||||
`visibility` ENUM('public', 'private', 'unlisted') NOT NULL DEFAULT 'public',
|
`visibility` TEXT CHECK( `visibility` IN ('public','private','unlisted') ) NOT NULL DEFAULT 'public',
|
||||||
-- This is an indexable field to filter out different types of datablocks
|
-- This is an indexable field to filter out different types of datablocks
|
||||||
`category` TEXT,
|
`category` TEXT,
|
||||||
`date_created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
`date_created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,16 @@ def parse_data(device: dict):
|
||||||
device['data'] = json.loads(device['data'])
|
device['data'] = json.loads(device['data'])
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/get_all_users')
|
||||||
|
def get_all_users():
|
||||||
|
"""Returns a list of all devices and details associated with them."""
|
||||||
|
values = db.query("SELECT * FROM `User`;")
|
||||||
|
values = [dict(v) for v in values]
|
||||||
|
for v in values:
|
||||||
|
parse_data(v)
|
||||||
|
return values
|
||||||
|
|
||||||
|
|
||||||
@router.get('/get_all_devices')
|
@router.get('/get_all_devices')
|
||||||
def get_all_devices():
|
def get_all_devices():
|
||||||
"""Returns a list of all devices and details associated with them."""
|
"""Returns a list of all devices and details associated with them."""
|
||||||
|
|
@ -92,22 +102,22 @@ def get_device_by_pairing_code_dict(pairing_code: str) -> dict | None:
|
||||||
def get_user_for_device(hw_id: str) -> dict:
|
def get_user_for_device(hw_id: str) -> dict:
|
||||||
values = db.query("""SELECT * FROM `UserDevice` WHERE `hw_id`=:hw_id;""", {'hw_id': hw_id})
|
values = db.query("""SELECT * FROM `UserDevice` WHERE `hw_id`=:hw_id;""", {'hw_id': hw_id})
|
||||||
if len(values) == 1:
|
if len(values) == 1:
|
||||||
user = dict(values[0])
|
user_id = dict(values[0])['user_id']
|
||||||
parse_data(user)
|
user = get_user_dict(user_id=user_id)
|
||||||
return user
|
|
||||||
# create new user instead
|
|
||||||
else:
|
else:
|
||||||
|
# create new user instead
|
||||||
user = create_user(hw_id)
|
user = create_user(hw_id)
|
||||||
|
parse_data(user)
|
||||||
|
return user
|
||||||
|
|
||||||
|
|
||||||
# creates a user with a device autoattached
|
# creates a user with a device autoattached
|
||||||
def create_user(hw_id: str) -> dict | None:
|
def create_user(hw_id: str) -> dict | None:
|
||||||
user_id = uuid.uuid4()
|
user_id = str(uuid.uuid4())
|
||||||
if not db.insert("""INSERT INTO `User`(id) VALUES (:user_id);
|
if not db.insert("""INSERT INTO `User`(id) VALUES (:user_id);""", {'user_id': user_id}):
|
||||||
""", {'user_id': user_id}):
|
|
||||||
return None
|
return None
|
||||||
if not db.insert("""INSERT INTO `UserDevice`(user_id, hw_id) VALUES (:user_id, :hw_id);
|
if not db.insert("""INSERT INTO `UserDevice`(user_id, hw_id) VALUES (:user_id, :hw_id); """,
|
||||||
""", {'user_id': user_id, 'hw_id': hw_id}):
|
{'user_id': user_id, 'hw_id': hw_id}):
|
||||||
return None
|
return None
|
||||||
return get_user_for_device(hw_id)
|
return get_user_for_device(hw_id)
|
||||||
|
|
||||||
|
|
@ -119,26 +129,29 @@ def create_device(hw_id: str):
|
||||||
|
|
||||||
|
|
||||||
@router.get('/device/get_data/{hw_id}')
|
@router.get('/device/get_data/{hw_id}')
|
||||||
def get_state(request: fastapi.Request, hw_id: str):
|
def get_state(request: Request, response: Response, hw_id: str):
|
||||||
"""Gets the device state"""
|
"""Gets the device state"""
|
||||||
|
|
||||||
devices = db.query("""
|
devices = db.query("""
|
||||||
SELECT * FROM `Device` WHERE `hw_id`=:hw_id;
|
SELECT * FROM `Device` WHERE `hw_id`=:hw_id;
|
||||||
""", {'hw_id': hw_id})
|
""", {'hw_id': hw_id})
|
||||||
if len(devices) == 0:
|
if len(devices) == 0:
|
||||||
|
response.status_code = status.HTTP_404_NOT_FOUND
|
||||||
return {'error': "Can't find device with that id."}
|
return {'error': "Can't find device with that id."}
|
||||||
block = dict(devices[0])
|
block = dict(devices[0])
|
||||||
if 'data' in block and block['data'] is not None:
|
if 'data' in block and block['data'] is not None:
|
||||||
block['data'] = json.loads(block['data'])
|
block['data'] = json.loads(block['data'])
|
||||||
|
|
||||||
|
user = get_user_for_device(hw_id)
|
||||||
|
|
||||||
room_key: str = f"{devices[0]['current_app']}_{devices[0]['current_room']}"
|
room_key: str = f"{devices[0]['current_app']}_{devices[0]['current_room']}"
|
||||||
room_data = get_data(room_key)
|
room_data = get_data(response, key=room_key, user_id=user['id'])
|
||||||
|
|
||||||
if "error" in room_data:
|
if "error" in room_data:
|
||||||
set_data(request, data={}, key=room_key, modified_by=None, category="room")
|
set_data(request, data={}, key=room_key, modified_by=None, category="room")
|
||||||
room_data = get_data(room_key)
|
room_data = get_data(response, key=room_key, user_id=user['id'])
|
||||||
|
|
||||||
return {'device': block, 'room': room_data}
|
return {'device': block, 'room': room_data, 'user': user}
|
||||||
|
|
||||||
|
|
||||||
@router.post('/device/set_data/{hw_id}')
|
@router.post('/device/set_data/{hw_id}')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue