1
0
Fork 0
A REST-API built with Flask and Python. Its main purpose is to receive the readings from the light meters welding booths in the Ritherdon factory and make them available for consumption by the relay-controllers in the gallery.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 

86 lines
2.8 KiB

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/.
'''
bad_meter_id_message = "Meter Id. not recognised. Must be between 1 and 3."
def get_latest_reading(meter):
if meter == 1:
return get_m1_latest()
elif meter == 2:
return get_m2_latest()
elif meter == 3:
return get_m3_latest()
return make_response(bad_meter_id_message, 400)
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()
return make_response(bad_meter_id_message, 400)
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