From fc3fe18b0e66d458a6c057f3bc34a61fe3afb10d Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Wed, 8 Jan 2020 22:30:25 +0000 Subject: [PATCH] implement get all status changes (per device) A.P.I. call. --- app/api.py | 6 +++ app/services/get_services.py | 77 +++++++++++++++++++++++++++++++++--- app/swagger.yml | 54 ++++++++++++++++++++++++- 3 files changed, 129 insertions(+), 8 deletions(-) diff --git a/app/api.py b/app/api.py index 34b1f08..e7b6f14 100644 --- a/app/api.py +++ b/app/api.py @@ -26,3 +26,9 @@ def get_all_readings_for_every_meter(): def get_latest_device_status(device): return get_services.get_latest_status(device) + +def get_all_status_log(device): + return get_services.get_all_status_logs_from_table(device) + +def get_all_status_logs_for_every_device(): + return get_services.get_all_status_changes_from_database() diff --git a/app/services/get_services.py b/app/services/get_services.py index 0e73cbf..98b3643 100644 --- a/app/services/get_services.py +++ b/app/services/get_services.py @@ -41,19 +41,37 @@ def get_all_readings_from_database(): def get_latest_status(device): if device == 1: - return get_d1_latest(); + return get_d1_latest() elif device == 2: - return get_d2_latest(); + return get_d2_latest() elif device == 3: - return get_d3_latest(); + return get_d3_latest() elif device == 4: - return get_d4_latest(); + return get_d4_latest() elif device == 5: - return get_d5_latest(); + return get_d5_latest() elif device == 6: - return get_d6_latest(); + return get_d6_latest() return make_response(bad_device_id_message, 400) +def get_all_status_logs_from_table(device): + if device == 1: + return get_all_status_logs_for_d1() + elif device == 2: + return get_all_status_logs_for_d2() + elif device == 3: + return get_all_status_logs_for_d3() + elif device == 4: + return get_all_status_logs_for_d4() + elif device == 5: + return get_all_status_logs_for_d5() + elif device == 6: + return get_all_status_logs_for_d6() + return make_response(bad_device_id_message, 400) + +def get_all_status_changes_from_database(): + return get_all_status_logs() + ''' The Nitty-Gritty Functions ====================================================================== @@ -133,3 +151,50 @@ def get_d6_latest(): status = Device6.query.order_by(Device6.id.desc()).first() device_schema = Device6Schema() return device_schema.dump(status) + +def get_all_status_logs_for_d1(): + status_logs = Device1.query.order_by(Device1.id.desc()).all() + schema = Device1Schema(many=True) + data = schema.dump(status_logs) + return data + +def get_all_status_logs_for_d2(): + status_logs = Device2.query.order_by(Device2.id.desc()).all() + schema = Device2Schema(many=True) + data = schema.dump(status_logs) + return data + +def get_all_status_logs_for_d3(): + status_logs = Device3.query.order_by(Device3.id.desc()).all() + schema = Device3Schema(many=True) + data = schema.dump(status_logs) + return data + +def get_all_status_logs_for_d4(): + status_logs = Device4.query.order_by(Device4.id.desc()).all() + schema = Device4Schema(many=True) + data = schema.dump(status_logs) + return data + +def get_all_status_logs_for_d5(): + status_logs = Device5.query.order_by(Device5.id.desc()).all() + schema = Device5Schema(many=True) + data = schema.dump(status_logs) + return data + +def get_all_status_logs_for_d6(): + status_logs = Device6.query.order_by(Device6.id.desc()).all() + schema = Device6Schema(many=True) + data = schema.dump(status_logs) + return data + +def get_all_status_changes_from_database(): + d1 = get_all_status_logs_for_d1() + d2 = get_all_status_logs_for_d2() + d3 = get_all_status_logs_for_d3() + d4 = get_all_status_logs_for_d4() + d5 = get_all_status_logs_for_d5() + d6 = get_all_status_logs_for_d6() + logs = {"device 1": d1, "device 2": d2, "device 3": d3, + "device 4": d4, "device 5": d5, "device 6": d6} + return logs diff --git a/app/swagger.yml b/app/swagger.yml index d7afbeb..245e556 100644 --- a/app/swagger.yml +++ b/app/swagger.yml @@ -135,7 +135,7 @@ paths: summary: >- Returns every reading taken by the specified light meter. description: >- - Use this U.R.L. to retrieve the all the reading from the + Use this U.R.L. to retrieve all the reading from the light meter you specified (in the U.R.L.). At the time of writing, the project has only three light meters and are labelled 1-3. @@ -302,5 +302,55 @@ paths: U.T.C. format. status: type: string - example: balshsdkjk + example: on description: The current status of the device. + + /status/all/{device}: + get: + operationId: api.get_all_status_log + tags: + - Request Device Status + summary: >- + Returns every status changed logged by the specified device. + description: >- + Use this U.R.L. to retrieve every change in the specified + devices state. At the time of writing, the project has six + devices and are labelled 1 to 6. Devices (light-meters) 1 to 3 + are located in the Ritherdon factory and 4 to 6 (relays) in the + gallery. The light-meters and relays are paired like so, 1->4, + 2->5 and 3->6. + parameters: + - name: device + in: path + description: >- + The Id. of the device you are retrieving the status change + log for. The Id. should be a number between 1 and 6. + type: integer + required: True + responses: + 200: + description: >- + If the server can successfully retrieve all the status logs + for the specified device, you should receive an array of + JSON objects like the one in the example below. It should + include the database Id. of the logged status change, the + time the status change was logged and the status itself. + schema: + type: object + properties: + id: + type: integer + example: 3 + description: >- + This is the database Id. of the logged status change. + time: + type: string + example: 2019-10-19 17:23:23 + description: >- + The time and date the status change was logged. The + A.P.I. has standardised on the U.T.C. format. + status: + type: string + example: off + description: >- + The status of the device after the change occurred.