Ejemplo n.º 1
0
def insert_user(username, password):
    mp = MysqlOp()
    mp.op_sql('INSERT INTO user (username, password) VALUES (%s, %s)',
              (username, user.hash_pwd(password)))
    return mp.op_sql(
        'INSERT INTO user_roles (user_id, role_id) VALUES (%s, %s)',
        (mp.cur.lastrowid, 2))
Ejemplo n.º 2
0
def insert_sensor_data(temperature: str, humidity: str, uuid: str, fire: bool,
                       illumination: bool, solid: bool):
    logger.info('insert_sensor_data')
    return MysqlOp().op_sql(
        'INSERT INTO sensor_data (hardware_uuid, temperature, humidity, is_fire, is_dry, is_illum) VALUES (%s, %s, %s, %s, %s, %s)',
        (uuid, temperature, humidity, int(fire), int(solid),
         int(illumination)))
Ejemplo n.º 3
0
def get_sensor_pagination(page, size, ordered, where_sql, *args):
    logger.info('get_sensor_pagination')
    return MysqlOp().select_all(
        f"SELECT sensor_data.id, hardware_uuid, temperature, humidity, is_fire, is_dry, is_illum, record_time, `name` "
        f"FROM sensor_data LEFT JOIN hardware ON sensor_data.hardware_uuid = hardware.uuid "
        f" {where_sql} ORDER BY {ordered} ASC LIMIT %s OFFSET %s",
        (*args, size, (page - 1) * size))
Ejemplo n.º 4
0
def count_total_user_hardware(where_sql, *args):
    logger.info('count_total_user_hardware')
    return MysqlOp().select_one(
        f'SELECT COUNT(`user_id`) as len from user_hardware '
        f'LEFT JOIN `user` ON `user`.id = user_hardware.user_id '
        f'LEFT JOIN hardware ON hardware.uuid = user_hardware.hardware_uuid '
        f'{where_sql}', (*args, ))
Ejemplo n.º 5
0
def get_user_pagination(page, size, where_sql, *args):
    logger.info('get_user_pagination')
    return MysqlOp().select_all(
        f"SELECT `user`.id, username, role.`name` FROM `user` "
        f"LEFT JOIN user_roles ON `user`.id = user_roles.user_id "
        f"LEFT JOIN role ON user_roles.role_id = role.id "
        f"{where_sql} LIMIT %s OFFSET %s", (*args, size, (page - 1) * size))
Ejemplo n.º 6
0
def get_rfid_pagination(page, size, ordered, where_sql, *args):
    logger.info('get_sensor_pagination')
    return MysqlOp().select_all(
        f"SELECT username, log_time, hardware_uuid, `name` FROM RFID_log "
        f"LEFT JOIN `user` ON RFID_log.user_id = `user`.id "
        f"LEFT JOIN hardware ON RFID_log.hardware_uuid = hardware.uuid "
        f"{where_sql} ORDER BY {ordered} ASC LIMIT %s OFFSET %s",
        (*args, size, (page - 1) * size))
Ejemplo n.º 7
0
def get_hardware_pagination_by_username(username, page, size, ordered,
                                        where_sql, *args):
    logger.info('get_hardware_pagination_by_username')
    return MysqlOp().select_all(
        f'SELECT * FROM hardware WHERE uuid IN '
        f'(SELECT hardware_uuid FROM user_hardware WHERE user_id = (SELECT id FROM `user` WHERE username = %s)) {where_sql} '
        f'ORDER BY {ordered} ASC LIMIT %s OFFSET %s',
        (username, *args, size, (page - 1) * size))
Ejemplo n.º 8
0
def get_sensor_pagination_by_username(username, page, size, ordered, where_sql, *args):
    logger.info('get_hardware_pagination_by_username')
    return MysqlOp().select_all(
        f'SELECT sensor_data.id, hardware_uuid, temperature, humidity, is_fire, is_dry, is_illum, record_time, `name` '
        f'FROM sensor_data LEFT JOIN hardware ON sensor_data.hardware_uuid = hardware.uuid'
        f' WHERE hardware_uuid IN (SELECT hardware_uuid FROM user_hardware '
        f'WHERE user_id = (SELECT id FROM `user` WHERE username = %s)) {where_sql} '
        f'ORDER BY {ordered} ASC LIMIT %s OFFSET %s',
        (username, *args, size, (page - 1) * size))
Ejemplo n.º 9
0
def get_rfid_pagination_by_username(username, page, size, ordered, where_sql,
                                    *args):
    logger.info('get_hardware_pagination_by_username')
    return MysqlOp().select_all(
        f'SELECT username, log_time, hardware_uuid, `name` FROM RFID_log '
        f'LEFT JOIN `user` ON RFID_log.user_id = `user`.id '
        f'LEFT JOIN hardware ON RFID_log.hardware_uuid = hardware.uuid '
        f'WHERE hardware_uuid IN (SELECT hardware_uuid '
        f'FROM user_hardware WHERE user_id = (SELECT id FROM `user` WHERE username = %s)) {where_sql} '
        f'ORDER BY {ordered} ASC LIMIT %s OFFSET %s',
        (username, *args, size, (page - 1) * size))
Ejemplo n.º 10
0
def get_hardware_pagination(page, size, ordered, where_sql, *args):
    logger.info('get_hardware_pagination')
    return MysqlOp().select_all(
        f"SELECT * FROM hardware {where_sql} ORDER BY {ordered} ASC LIMIT %s OFFSET %s",
        (*args, size, (page - 1) * size))
Ejemplo n.º 11
0
def update_threshold_by_uuid(uuid, temperature_limit, humidity_limit):
    logger.info('update_threshold_by_uuid')
    return MysqlOp().op_sql(
        'UPDATE hardware SET temperature_limit = %s, humidity_limit = %s WHERE uuid = %s',
        (temperature_limit, humidity_limit, uuid))
Ejemplo n.º 12
0
def update_by_rid(rid, id):
    return MysqlOp().op_sql(
        'UPDATE user_roles SET role_id = %s WHERE user_id = %s', (rid, id))
Ejemplo n.º 13
0
def count_total_role(where_sql, *args):
    return MysqlOp().select_one(
        f'SELECT COUNT(`id`) as len from role {where_sql}', (*args, ))
Ejemplo n.º 14
0
def get_user_hardware_pagination(page, size, where_sql, *args):
    return MysqlOp().select_all(
        f"SELECT `user`.id , hardware.`name`, hardware.uuid, `user`.username FROM user_hardware "
        f"LEFT JOIN `user` ON `user`.id = user_hardware.user_id "
        f"LEFT JOIN hardware ON hardware.uuid = user_hardware.hardware_uuid "
        f"{where_sql} LIMIT %s OFFSET %s", (*args, size, (page - 1) * size))
Ejemplo n.º 15
0
def update_hardware_by_id(id, name, humidity_limit, temperature_limit):
    logger.info('update_hardware_by_id')
    return MysqlOp().op_sql(
        'UPDATE hardware SET `name` = %s, temperature_limit = %s, humidity_limit = %s WHERE id = %s',
        (name, temperature_limit, humidity_limit, id))
Ejemplo n.º 16
0
from db import MysqlOp

a = MysqlOp().select_one(
    "SELECT id, username FROM `user` WHERE username = '******' AND id IN (SELECT user_roles.user_id FROM user_roles WHERE user_roles.role_id IN (SELECT role.id FROM role WHERE role.`name` = 'admin'))"
)
print(a)
Ejemplo n.º 17
0
def get_role_pagination(page, size, where_sql, *args):
    return MysqlOp().select_all(
        f'SELECT * FROM role {where_sql} LIMIT %s OFFSET %s',
        (*args, size, (page - 1) * size))
Ejemplo n.º 18
0
def add_role(name):
    mp = MysqlOp()
    return mp.op_sql('INSERT INTO role (`name`) VALUES (%s)',
                     (name)), mp.cur.lastrowid
Ejemplo n.º 19
0
def insert_hardware(uuid: str):
    logger.info('insert_hardware')
    return MysqlOp().op_sql('INSERT INTO hardware (uuid) VALUES (%s)', (uuid))
Ejemplo n.º 20
0
def get_id_by_uuid(uuid: str):
    logger.info('get_id_by_uuid')
    return MysqlOp().select_one('SELECT id FROM hardware WHERE uuid = %s',
                                (uuid))
Ejemplo n.º 21
0
def count_total(where_sql, *args):
    logger.info('count_total')
    return MysqlOp().select_one(
        f'SELECT COUNT(`id`) as len from hardware {where_sql}', (*args, ))
Ejemplo n.º 22
0
def delete_user_hardware_by_user_id(id):
    logger.info('delete_user_hardware_by_user_id')
    return MysqlOp().op_sql('DELETE FROM user_hardware WHERE user_id = %s',
                            (id))
Ejemplo n.º 23
0
def get_all_hardware_list():
    return MysqlOp().select_all('SELECT uuid, `name` FROM hardware')
Ejemplo n.º 24
0
def get_sensor_data_hourly(uuid):
    return MysqlOp().select_all(
        'SELECT temperature, humidity, record_time FROM sensor_data WHERE hardware_uuid = %s AND id > ((SELECT MAX(id) FROM sensor_data) - 100)',
        (uuid))
Ejemplo n.º 25
0
def insert_user_hardware_data(id, uuid):
    logger.info('insert_user_hardware')
    return MysqlOp().op_sql(
        'INSERT INTO user_hardware (user_id, hardware_uuid) VALUES (%s, %s)',
        (id, uuid))
Ejemplo n.º 26
0
def update_user_by_id(username, id):
    return MysqlOp().op_sql('UPDATE `user` SET username = %s WHERE id = %s',
                            (username, id))
Ejemplo n.º 27
0
def count_total(where_sql, *args):
    return MysqlOp().select_one(
        f'SELECT COUNT(`id`) as len from sensor_data {where_sql}', (*args, ))
Ejemplo n.º 28
0
def delete_user_by_id(id):
    return MysqlOp().op_sql('DELETE FROM user WHERE id = %s', (id))
Ejemplo n.º 29
0
def insert_rfid_log(user_id, hardware_uuid):
    logger.info('insert_rfid_log')
    return MysqlOp().op_sql(
        'INSERT INTO RFID_log (hardware_uuid, user_id) VALUES (%s, %s)',
        (hardware_uuid, user_id))
Ejemplo n.º 30
0
def select_role_id_by_userid(name):
    mp = MysqlOp()
    return mp.select_one('SELECT role.id as id FROM role WHERE `name` = %s',
                         (name, ))