def check_uuid(uuid: str): if uuid is None: raise ContentEmptyException() id = get_id_by_uuid(uuid) if id is None: raise DataNotFoundException() return response_success('success', id)
def getDailySensorData(): uuid = request.args.get('uuid', None) logger.info(uuid) if uuid is None: raise ContentEmptyException() return response_success('success', get_sensor_data_hourly(uuid))
def update_hardware(id): name = request.json.get('name', None) up = request.json.get('up', None) uuid = request.json.get('uuid', None) humidity_limit = request.json.get('humidity_limit', None) temperature_limit = request.json.get('temperature_limit', None) if name is None or humidity_limit is None or up is None \ or uuid is None or temperature_limit is None \ or temperature_limit is None: raise ContentEmptyException() is_succ = update_hardware_by_id(id, name, humidity_limit, temperature_limit) if not is_succ: raise DBException() mqtt_client.publish( 'setup_threshold', { 'uuid': uuid, 'temperature_limit': temperature_limit, 'humidity_limit': humidity_limit }) return response_success( 'success', { 'id': id, 'up': up, 'uuid': uuid, 'name': name, 'humidity_limit': str(humidity_limit), 'temperature_limit': str(temperature_limit) })
def add_role(): name = request.json.get('name', None) if name is None: raise ContentEmptyException() is_succ, id = user_dao.add_role(name) if not is_succ: raise DBException() return response_success('success', {'id': id, 'name': name})
def insert_user_hardware(): id = request.json.get('id', None) uuidd = request.json.get('uuid', None) if id is None or uuidd is None: raise ContentEmptyException() is_succ = insert_user_hardware_data(id, uuidd) if not is_succ: raise DBException() return response_success('success', None)
def update_role(id): name = request.json.get('name', None) if name is None: raise ContentEmptyException() is_succ = user_dao.update_role_by_id(id, name) if not is_succ: raise DBException() return response_success('success', {'id': id, 'name': name})
def gen_code(): auth = request.headers['auth'] if auth is None: raise ContentEmptyException() now = datetime.now() spa = b64decode(auth).decode().split('/') if now.hour != int(spa[0]) or now.minute != int(spa[1]): raise UnAuthorizedException() id = uuid.uuid1().__str__() eff_row = insert_hardware(id) if eff_row != 1: raise DBException() return response_success('success', id)
def update_user(id): username = request.json.get('username', None) name = request.json.get('name', None) if username is None or name is None: raise ContentEmptyException() rid = user_dao.select_role_id_by_userid(name)['id'] is_succ1 = user_dao.update_by_rid(rid, id) is_succ2 = user_dao.update_user_by_id(username, id) if not is_succ1 or not is_succ2: raise DBException() return response_success('success', { 'id': id, 'username': username, 'name': name })
def register(): username = request.json.get('username', None) password = request.json.get('password', None) admin_name = request.json.get('adminName', None) if len(password) < 6: raise DataNotSatisfyException() if username is None or password is None or admin_name is None: raise ContentEmptyException() admin = user_dao.select_admin_by_username(admin_name) if admin is None: raise AdminNameErrorException() is_succ = user_dao.insert_user(username, password) if not is_succ: raise DBException() return response_success("success", None)
def change_password(): claims = get_jwt_claims() real_name = claims['username'] username = request.json.get('username', None) password = request.json.get('password', None) if real_name != username: raise UnAuthorizedException() if len(password) < 7: raise DataNotSatisfyException() if username is None or password is None: raise ContentEmptyException() is_succ = user_dao.update_password_by_username(username, password) if not is_succ: raise DBException() return response_success('success', None)
def login(): username = request.json.get('username', None) password = request.json.get('password', None) if len(password) < 6: raise DataNotSatisfyException() if username is None or password is None: raise ContentEmptyException() data = user_dao.select_user_by_username(username) if data is None: raise DataNotFoundException() if not user.check_pwd(password, data['password']): raise PasswordErrorException() roles = list( map(lambda x: x['name'], user_dao.select_roles_by_userid(data['id']))) token = create_access_token(identity=user.User(data['username'], roles)) return response_success('success', {'token': token})