def login(): if session.get('logged_in'): return redirect(url_for('dashboard')) else: form = LoginForm() if form.validate_on_submit(): try: cognito.admin_initiate_auth( AuthFlow='ADMIN_NO_SRP_AUTH', AuthParameters={ 'USERNAME': form.username.data, 'PASSWORD': form.password.data }, ClientId='2v3e93opl3akulijav5eusliov', UserPoolId='eu-west-1_g70ijojs5') session['logged_in'] = True log_util.log_debug( __name__, "User Logged in correctly, username: {}".format( form.username.data)) return redirect(url_for('dashboard')) except (cognito.exceptions.NotAuthorizedException, cognito.exceptions.UserNotFoundException) as e: log_util.log_error( __name__, "User not authenticate, username: {}. Error: {}".format( form.username.data, e)) return render_template('login.html', title='Login', error="Incorrect username or password.", form=form) return render_template('login.html', title='Login', form=form)
def post_max_data(data): try: new_data = { "id": "id_smartgarden", "Items": [data] } core.publish_max_data(new_data) except: import sys log_util.log_error(__name__, sys.exc_info()[0]) log_util.log_error(__name__, sys.exc_info()[1])
def changeStatus(status): try: log_util.log_debug( __name__, "Received status change request, sending to DynamoDB: {}".format( str(status))) repository_dynamo.post_status(status) return status except: log_util.log_error(__name__, sys.exc_info()[0]) log_util.log_error(__name__, sys.exc_info()[1]) return None
def status(): try: data = jsonc.data_to_json(repository_dynamo.get_status()) loaded_data = jsonc.json.loads(data) log_util.log_debug( __name__, "Getting status from DynamoDB: {}".format(str(loaded_data))) return jsonify(loaded_data) except: log_util.log_error(__name__, sys.exc_info()[0]) log_util.log_error(__name__, sys.exc_info()[1]) return None
def api_getChartData(): if request.method == 'GET': try: data = jsonc.data_to_json(repository_dynamo.get_chart_data()) loaded_data = jsonc.json.loads(data) log_util.log_debug( __name__, "Getting chart data from DynamoDB: {}".format( str(loaded_data))) return jsonify(loaded_data) except: log_util.log_error(__name__, sys.exc_info()[0]) log_util.log_error(__name__, sys.exc_info()[1]) return None
def post_status(status): try: now = datetime.datetime.now() new_item = { "id": "id_status", 'datetimeid': now.isoformat(), 'status': str(status) } core.publish_status(new_item) except: import sys log_util.log_error(__name__, sys.exc_info()[0]) log_util.log_error(__name__, sys.exc_info()[1])
def api_getMaxData(): if request.method == 'POST': try: data = jsonc.json.loads(request.data) if data['moisture'] is not '': log_util.log_debug( __name__, "Received max data, sending to DynamoDB: {}".format( str(data))) repository_dynamo.post_max_data(data) return str(data) except: log_util.log_error(__name__, sys.exc_info()[0]) log_util.log_error(__name__, sys.exc_info()[1]) return None elif request.method == 'GET': try: data = jsonc.data_to_json(repository_dynamo.get_max_data()) loaded_data = jsonc.json.loads(data) log_util.log_debug( __name__, "Getting max data from DynamoDB: {}".format(str(loaded_data))) return jsonify(loaded_data) except: log_util.log_error(__name__, sys.exc_info()[0]) log_util.log_error(__name__, sys.exc_info()[1]) return None
def get_max_data(): try: dynamodb = boto3.resource('dynamodb', region_name='eu-west-1') table = dynamodb.Table('smartgarden_maxdata') response = table.query(KeyConditionExpression='id = :id_smartgarden', ExpressionAttributeValues={ ':id_smartgarden': 'id_smartgarden'}, ScanIndexForward=False ) items = response['Items'] n = 1 # get latest data data = items[:n] return data[0] except: import sys log_util.log_error(__name__, sys.exc_info()[0]) log_util.log_error(__name__, sys.exc_info()[1])
def get_chart_data(): try: dynamodb = boto3.resource('dynamodb', region_name='eu-west-1') table = dynamodb.Table('smartgarden_readings') response = table.query(KeyConditionExpression='id = :id_smartgarden', ExpressionAttributeValues={ ':id_smartgarden': 'id_smartgarden' }, ScanIndexForward=False, Limit=20 ) items = response['Items'] n = 15 # limit to last 15 items data = items[:n] data_reversed = data[::-1] return data_reversed except: import sys log_util.log_error(__name__, sys.exc_info()[0]) log_util.log_error(__name__, sys.exc_info()[1])