diff --git a/app/api.py b/app/api.py index ceb9ba1..34b1f08 100644 --- a/app/api.py +++ b/app/api.py @@ -23,3 +23,6 @@ def get_all_readings(light_meter): def get_all_readings_for_every_meter(): return get_services.get_all_readings_from_database() + +def get_latest_device_status(device): + return get_services.get_latest_status(device) diff --git a/app/readings.db b/app/readings.db index b555a97..4b7ad32 100644 Binary files a/app/readings.db and b/app/readings.db differ diff --git a/app/readings.db-journal b/app/readings.db-journal deleted file mode 100644 index 290d5f9..0000000 Binary files a/app/readings.db-journal and /dev/null differ diff --git a/app/services/get_services.py b/app/services/get_services.py index ec43e87..0e73cbf 100644 --- a/app/services/get_services.py +++ b/app/services/get_services.py @@ -2,6 +2,9 @@ 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) ''' Get Services Note @@ -13,6 +16,7 @@ It should be in the same directory at this: /services/. ''' bad_meter_id_message = "Meter Id. not recognised. Must be between 1 and 3." +bad_device_id_message = "Device Id. not recognised. Must be between 1 and 6." def get_latest_reading(meter): if meter == 1: @@ -35,14 +39,29 @@ def get_all_readings_from_table(name): def get_all_readings_from_database(): return get_all_readings() +def get_latest_status(device): + if device == 1: + return get_d1_latest(); + elif device == 2: + return get_d2_latest(); + elif device == 3: + return get_d3_latest(); + elif device == 4: + return get_d4_latest(); + elif device == 5: + return get_d5_latest(); + elif device == 6: + return get_d6_latest(); + return make_response(bad_device_id_message, 400) + ''' 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. +above act as "header functions" to be called by the functions 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(): @@ -84,3 +103,33 @@ def get_all_readings(): m3 = get_all_readings_for_meter3() readings = {"meter1": m1, "meter2": m2, "meter3": m3} return readings + +def get_d1_latest(): + status = Device1.query.order_by(Device1.id.desc()).first() + device_schema = Device1Schema() + return device_schema.dump(status) + +def get_d2_latest(): + status = Device2.query.order_by(Device2.id.desc()).first() + device_schema = Device2Schema() + return device_schema.dump(status) + +def get_d3_latest(): + status = Device3.query.order_by(Device3.id.desc()).first() + device_schema = Device3Schema() + return device_schema.dump(status) + +def get_d4_latest(): + status = Device4.query.order_by(Device4.id.desc()).first() + device_schema = Device4Schema() + return device_schema.dump(status) + +def get_d5_latest(): + status = Device5.query.order_by(Device5.id.desc()).first() + device_schema = Device5Schema() + return device_schema.dump(status) + +def get_d6_latest(): + status = Device6.query.order_by(Device6.id.desc()).first() + device_schema = Device6Schema() + return device_schema.dump(status) diff --git a/app/swagger.yml b/app/swagger.yml index 205c78a..d7afbeb 100644 --- a/app/swagger.yml +++ b/app/swagger.yml @@ -12,6 +12,8 @@ produces: basePath: /api paths: +# Light-Meter A.P.I. Calls +# ==================================================================== /readings/add/{light_meter}: post: operationId: api.post_a_reading @@ -72,55 +74,6 @@ paths: description: >- The reading was successfully added to the database. - /status/update/{device}: - post: - operationId: api.post_a_status_change - tags: - - Log Device Status Change - summary: >- - Logs a change in the status of the specified device. - description: >- - This is mostly to check if the devices included in this project - are turned on, off or an error has occured and they are - unreachable/crashed. - parameters: - - name: device - in: path - description: >- - The Id. of the device with the change in status. 1, 2 and 3 - refer to the light-meters in Ritherdon and 4, 5 and 6 - refer to the relays in the gallery. The pairing of each - meter and relay is as follows: 1->4, 2->5 and 3->6. - type: integer - required: True - - name: the_status_change - in: body - description: >- - The status change and the time the change occurred. - required: True - schema: - type: object - properties: - time: - type: string - format: date-time - example: 2019-10-19 17:04:57 - description: >- - The date and time the change in status occurred. Make - sure to use the U.T.C. time format. This is because - the A.P.I. has made a design decision to standardise - on it. - status: - type: string - example: "on" - description: >- - The current status of the device you would like to log. - The two status types are "on" and "off". - responses: - 201: - description: >- - The status update was successfully added to the database. - /readings/latest/{light_meter}: get: operationId: api.get_latest @@ -146,7 +99,7 @@ paths: 200: description: >- If the server can successfully retrieve the latest reading - for the specified light meter, you should receive a JASON + for the specified light meter, you should receive a JSON object like the one below. It should include the Id. of the light meter it was taken with, the time the reading was taken and the reading itself. @@ -249,3 +202,105 @@ paths: schema: type: object additionalProperties: True + +# Device A.P.I. Calls +# ==================================================================== + /status/update/{device}: + post: + operationId: api.post_a_status_change + tags: + - Log Device Status Change + summary: >- + Logs a change in the status of the specified device. + description: >- + This is mostly to check if the devices included in this project + are turned on, off or an error has occured and they are + unreachable/crashed. + parameters: + - name: device + in: path + description: >- + The Id. of the device with the change in status. 1, 2 and 3 + refer to the light-meters in Ritherdon and 4, 5 and 6 + refer to the relays in the gallery. The pairing of each + meter and relay is as follows: 1->4, 2->5 and 3->6. + type: integer + required: True + - name: the_status_change + in: body + description: >- + The status change and the time the change occurred. + required: True + schema: + type: object + properties: + time: + type: string + format: date-time + example: 2019-10-19 17:04:57 + description: >- + The date and time the change in status occurred. Make + sure to use the U.T.C. time format. This is because + the A.P.I. has made a design decision to standardise + on it. + status: + type: string + example: "on" + description: >- + The current status of the device you would like to log. + The two status types are "on" and "off". + responses: + 201: + description: >- + The status update was successfully added to the database. + + /status/latest/{device}: + get: + operationId: api.get_latest_device_status + tags: + - Request Device Status + summary: >- + Returns the current status of the specified device. + description: >- + Use this U.R.L. to retrieve the current status of the device + you specified (in the U.R.L.). At the time of writing, the + project has six devices in total which are split equally + between Ritherdon and the gallery. Devices 1 to 3 (light-meters) + refer to the devices in Ritherdon and 4 to 6 (relays) are in + the gallery. Each light-meter and relay should pair up in the + following ways: 1->4, 2->5 and 3->6. + parameters: + - name: device + in: path + description: >- + This is the Id. of the device which you are retrieving the + current status for. The Id. should consist of a number + between 1 and 6, at the time of writing. + type: integer + required: True + responses: + 200: + description: >- + If the server can successfully retrieve the current status + for the specified device, you should receive a JSON object + like the one below. It should include the Id. of the + device, the current status and the time it was logged. + schema: + type: object + properties: + id: + type: integer + example: 2 + description: >- + The is the Id. of the device. + time: + type: string + example: 2019-10-19 17:04:57 + description: >- + The time the status change was logged in the + database. The A.P.I. has standardised on the + U.T.C. format. + status: + type: string + example: balshsdkjk + description: The current status of the device.