def get_dashboard_data(self, params): try: if 'start_date' not in params and 'end_date' not in params: return Response.input_error( description="start_date and end_date required") dashboard_data = dict() date_format = "%d/%m/%Y %H:%M:%S" # dates_in_range = Utilities.get_dates_in_range(params['start_date'], params['end_date']) for measurement_type in Measurement.Type.to_list(): measurement_data = Measurement.objects( Q(created_at__gte=datetime.strptime( params['start_date'] + ":00", date_format)) & Q(created_at__lte=datetime.strptime( params['end_date'] + ":59", date_format)) & Q(type=measurement_type)) data_values = [] time_values = [] for data in measurement_data: data_values.append(float(data.value)) time_values.append( datetime.strftime(data.created_at, "%H:%M")) dashboard_data[measurement_type.lower()] = { 'labels': time_values, 'values': data_values } return Response.ok_response(data=dashboard_data) except Exception as ex: return Response.input_error( description="An error occurred: {}".format(str(ex)))
def add_measurement(self, request_data): try: if request_data.get('type') not in Measurement.Type.to_list(): return Response.input_error(description="Type must be one of {}".format(Measurement.Type.to_list())) measurement = Measurement(type=request_data.get('type'), value=request_data.get('value')).save() application.socketio.emit('measurement_updated', measurement.to_dict_dashboard()) return Response.ok_response() except Exception as ex: return Response.input_error(description="An error occurred: {}".format(str(ex)))
def update_sensor_values(self, request_data): try: for key, value in request_data.items(): params = { 'type': key.upper(), 'value': value } ApiCalls().request_api(req_params=params, method='get', route='/control') return Response.ok_response() except Exception as ex: return Response.input_error(description="An error occurred: {}".format(str(ex)))
def get_settings_data(self, request_data): try: return ApiCalls().request_api(req_params=request_data, method='post', route='/settings') except Exception as ex: return Response.input_error( description="An error occurred: {}".format(str(ex)))
def logout(): request_id = Utilities.generate_session_id() logger = Logger(request_id).event("LOGOUT") try: session.pop('user_data', None) return redirect(url_for('auth_bp.login_page')) except Exception as ex: raise ex return Response.input_error(description="An error occurred: {}".format(str(ex)))
def login(): request_id = Utilities.generate_session_id() logger = Logger(request_id).event("LOGIN") try: params = json.loads(request.data.decode('utf-8')) response = ApiCalls().request_api(req_params=params, route='/login') logger.event("Login response: {}".format(response)) if response.get('code') == '00': logger.event("Login successful") session['user_data'] = response.get('data') return response except Exception as ex: raise ex return Response.input_error(description="An error occurred: {}".format(str(ex)))