You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.7 KiB
85 lines
2.7 KiB
4 years ago
|
from flask import make_response, abort
|
||
|
from config import db
|
||
|
from models.meters import (Meter1, Meter1Schema, Meter2, Meter2Schema,
|
||
|
Meter3, Meter3Schema)
|
||
|
|
||
|
'''
|
||
|
Get Services Note
|
||
|
======================================================================
|
||
|
The functions in this file are for retrieving data stored at the (this)
|
||
|
server. If you want to store any readings taken with the light meters
|
||
|
at Ritherdon, you will need to head to the /post_services.py/ file.
|
||
|
It should be in the same directory at this: /services/.
|
||
|
'''
|
||
|
|
||
|
def get_in_mem_reading(meter):
|
||
|
# Need to refactor in use an in-mem database.
|
||
|
# Currently grabbing from database.
|
||
|
if meter == 1:
|
||
|
return get_m1_latest()
|
||
|
elif meter == 2:
|
||
|
return get_m2_latest()
|
||
|
elif meter == 3:
|
||
|
return get_m3_latest()
|
||
|
|
||
|
def get_all_readings_from_table(name):
|
||
|
if name == 1:
|
||
|
return get_all_readings_for_meter1()
|
||
|
elif name == 2:
|
||
|
return get_all_readings_for_meter2()
|
||
|
elif name == 3:
|
||
|
return get_all_readings_for_meter3()
|
||
|
|
||
|
def get_all_readings_from_database():
|
||
|
return get_all_readings()
|
||
|
|
||
|
'''
|
||
|
The Nitty-Gritty Functions
|
||
|
======================================================================
|
||
|
The functions below are the main functions within this file. The files
|
||
|
above act as "header functions" for the methods in /api.py/. I find it
|
||
|
easier to see what the method names are when this file and /api.py/
|
||
|
are open side-by-side. At the very least it reduces the amount I need
|
||
|
to scroll up and down the file to find what I am after.
|
||
|
'''
|
||
|
|
||
|
def get_m1_latest():
|
||
|
reading = Meter1.query.order_by(Meter1.id.desc()).first()
|
||
|
meter_schema = Meter1Schema()
|
||
|
return meter_schema.dump(reading)
|
||
|
|
||
|
def get_m2_latest():
|
||
|
reading = Meter2.query.order_by(Meter2.id.desc()).first()
|
||
|
meter_schema = Meter2Schema()
|
||
|
return meter_schema.dump(reading)
|
||
|
|
||
|
def get_m3_latest():
|
||
|
reading = Meter3.query.order_by(Meter3.id.desc()).first()
|
||
|
meter_schema = Meter3Schema()
|
||
|
return meter_schema.dump(reading)
|
||
|
|
||
|
def get_all_readings_for_meter1():
|
||
|
readings = Meter1.query.order_by(Meter1.id.desc()).all()
|
||
|
schema = Meter1Schema(many=True)
|
||
|
data = schema.dump(readings)
|
||
|
return data
|
||
|
|
||
|
def get_all_readings_for_meter2():
|
||
|
readings = Meter2.query.order_by(Meter2.id.desc()).all()
|
||
|
schema = Meter2Schema(many=True)
|
||
|
data = schema.dump(readings)
|
||
|
return data
|
||
|
|
||
|
def get_all_readings_for_meter3():
|
||
|
readings = Meter3.query.order_by(Meter3.id.desc()).all()
|
||
|
schema = Meter3Schema(many=True)
|
||
|
data = schema.dump(readings)
|
||
|
return data
|
||
|
|
||
|
def get_all_readings():
|
||
|
m1 = get_all_readings_for_meter1()
|
||
|
m2 = get_all_readings_for_meter2()
|
||
|
m3 = get_all_readings_for_meter3()
|
||
|
readings = {"meter1": m1, "meter2": m2, "meter3": m3}
|
||
|
return readings
|