from flask import make_response, abort from config import db from models.meters import (Meter1, Meter1Schema, Meter2, Meter2Schema, Meter3, Meter3Schema) from models.devices import (Device1, Device1Schema, Device2, Device2Schema, Device3, Device3Schema, Device4, Device4Schema, Device5, Device5Schema, Device6, Device6Schema) ''' Post Services Note ====================================================================== The functions in this file are for storing the readings taken from the light meters in Ritherdon, and log changes in state for any of the devices. It you are wanting to retrieve data from the (this) server, you will need to head to the /get_services.py/ file. It should be in the same directory as this: /services/. ''' def add_latest_reading(meter,reading): if meter == 1: return add_reading_to_meter1(reading) elif meter == 2: return add_reading_to_meter2(reading) elif meter == 3: return add_reading_to_meter3(reading) return make_response("Meter Id. not recognised. Must be between 1 and 3.", 400) def log_status_change(device, status): if device == 1: return add_status_change_to_device1(status) elif device == 2: return add_status_change_to_device2(status) elif device == 3: return add_status_change_to_device3(status) elif device == 4: return add_status_change_to_device4(status) elif device == 5: return add_status_change_to_device5(status) elif device == 6: return add_status_change_to_device6(status) return make_response("Device Id. not recognised. Must be between 1 and 6.", 400) ''' Nitty-Gritty Functions ====================================================================== The functions above are basically "header" functions. It makes it easier to have multiple files open at once and see what this module provides function-wise. The functions below do the real work in this file. Please keep the "public" functions about this comment and defer the main work to here. The "public" functions should be as thin as possible to make them as scanable as possible. ''' reading_message = "Reading successfully stored in database." status_message = "Status change successfully logged in database." def add_reading_to_meter1(the_reading): schema = Meter1Schema() new_reading = schema.load(the_reading, session=db.session) db.session.add(new_reading) db.session.commit() return make_response(reading_message, 201) def add_reading_to_meter2(the_reading): schema = Meter2Schema() new_reading = schema.load(the_reading, session=db.session) db.session.add(new_reading) db.session.commit() return make_response(reading_message, 201) def add_reading_to_meter3(the_reading): schema = Meter3Schema() new_reading = schema.load(the_reading, session=db.session) db.session.add(new_reading) db.session.commit() return make_response(reading_message, 201) def add_status_change_to_device1(status): schema = Device1Schema() new_status = schema.load(status, session=db.session) db.session.add(new_status) db.session.commit() return make_response(status_message, 201) def add_status_change_to_device2(status): schema = Device2Schema() new_status = schema.load(status, session=db.session) db.session.add(new_status) db.session.commit() return make_response(status_message, 201) def add_status_change_to_device3(status): schema = Device3Schema() new_status = schema.load(status, session=db.session) db.session.add(new_status) db.session.commit() return make_response(status_message, 201) def add_status_change_to_device4(status): schema = Device4Schema() new_status = schema.load(status, session=db.session) db.session.add(new_status) db.session.commit() return make_response(status_message, 201) def add_status_change_to_device5(status): schema = Device5Schema() new_status = schema.load(status, session=db.session) db.session.add(new_status) db.session.commit() return make_response(status_message, 201) def add_status_change_to_device6(status): schema = Device6Schema() new_status = schema.load(status, session=db.session) db.session.add(new_status) db.session.commit() return make_response(status_message, 201)