From c24981a0e338f5c3a726352f3a4dcbe3e3a1d670 Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Thu, 9 Jan 2020 00:04:50 +0000 Subject: [PATCH 01/10] reset database. --- app/readings.db | Bin 40960 -> 40960 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/app/readings.db b/app/readings.db index 3c42e9242dce31415bc63c0fe1f0bf102191b304..5083ba19190dda489e5bb0abaad57bae0b62243e 100644 GIT binary patch delta 405 zcmZoTz|?SnX@WE(*F+g-My`zs{qlmA3I+yN21Zt<26|=&#%4xFn*`qTV~Co9M5WQy zn(;C)Ffj7pW#GTNSy13S|HJ?mT}4I~21j8d10w@n14CT{3@!O-X{75ir=uwI9LMn%P+pwYWe3J+md7(qhUp7M;AqS>n;E)|;SvwoC=1EJ1Ut_NF}n%x zW!JJ~H?Uh+_CO(HX%S5bOWpSgNPT$V|Ldpp;4k&v+e*uGMf1Gdu6x2#))iG%o(iET z%C@{`-ZKi>+R*Di8WAzvL7H0R#|0 z009ILKmdXNN1&fhZftF-SEpY6!)LeQUba5E`O!n>u<(U*-L|yHxMG^+v1nX?(tf zp98&Epu9OOWyXW$@1%db@@k;GF)VMyg5~dIf2;C?-dxpwDDuWY009ILKmY**5I_I{ z1Q0*~frtt`Ruf7&W$LD$)s3uP5Js_N+a;l=QVo;8ZXI4J44l6|nR565}4 zDUx!@!yA@##PZA)Oa2O@m<=z}5s6tVcH(k{j5_r_WYpNqHyK>~@9%HO#s5W1?(d-g zk89VX{y(Y*mfQ#+fB*srAbg|F`-6 zf2u!EAb HzyJRYMoJ4C From 95cb4754289f8cb3f009c2547232c7f0fc1b4957 Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Thu, 9 Jan 2020 14:35:26 +0000 Subject: [PATCH 02/10] add security token checks when adding new reading. --- app/readings.db | Bin 40960 -> 40960 bytes app/services/post_services.py | 22 ++++++++++++++-------- app/swagger.yml | 6 ++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/app/readings.db b/app/readings.db index 5083ba19190dda489e5bb0abaad57bae0b62243e..f3d6b7a86402687a6a69adc490cef3e1b2e37422 100644 GIT binary patch delta 72 zcmZoTz|?SnX@WE(_e2?IM(&LXOZYjM`1dpLKj7cLSx{gzzoI-73xgx0k%6J5uAzah Zp{0VMxs`#5m8rR&0T_sHeju;V006o$5+eWr delta 29 lcmZoTz|?SnX@WE(*F+g-My`zsOZYdlDE#K%{9m520RWUt3M~Kt diff --git a/app/services/post_services.py b/app/services/post_services.py index 6617b1f..18c6dd2 100644 --- a/app/services/post_services.py +++ b/app/services/post_services.py @@ -16,16 +16,22 @@ 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) +device_check_token = "QWERTYuiopasdfghjklzxcvbnm_1234567890" + +def add_latest_reading(meter, info): + if info["token"] == device_check_token: + reading = {"reading":info["reading"], "time":info["time"]} + 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) + return make_response("Invalid token.", 400) def log_status_change(device, status): + # NEED TO ADD TOKEN. if device == 1: return add_status_change_to_device1(status) elif device == 2: diff --git a/app/swagger.yml b/app/swagger.yml index efa76b4..a627543 100644 --- a/app/swagger.yml +++ b/app/swagger.yml @@ -69,6 +69,12 @@ paths: This represents the amount of light the light meter recorded. This is the most important piece of data you will post in this data-object. + token: + type: string + example: it-is-not-password-if-you-are-wondering + description: >- + This is basically a token to check the info. sent to + the server is from a valid machine. responses: 201: description: >- From d377ef4eca2516e10daff2ac548c7a6706ac519c Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Thu, 9 Jan 2020 14:44:07 +0000 Subject: [PATCH 03/10] add secirity token checks to status change A.P.I. call. --- app/readings.db | Bin 40960 -> 40960 bytes app/services/post_services.py | 32 +++++++++++++++++--------------- app/swagger.yml | 6 ++++++ 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/app/readings.db b/app/readings.db index f3d6b7a86402687a6a69adc490cef3e1b2e37422..ab91286eeee0e7f8cb670a0a1ffbbf4e2b77df5b 100644 GIT binary patch delta 218 zcmZoTz|?SnX@WE(*F+g-My`zsOZYdlD7@w8H#D&_F|sl-)-yCRGc&f_r0@|<%)}V0 cn8Q1MAr!qPCi!V;n}`-R#jct(VKbm101gK_Z~y=R delta 277 zcmZoTz|?SnX@WE(_e2?IM(&LXOZYjM`1dpLKj7cLSx{gzzoR@83xgx0k%6J5uAzah zp{0VMxs`#5m8rR&0T_tOF#;8E7#SED=o%R68dxeA7+4t?S(zH>nHd- The current status of the device you would like to log. The two status types are "on" and "off". + token: + type: string + example: it-is-not-password-if-you-are-wondering + description: >- + This is basically a token to check the info. sent to + the server is from a valid machine. responses: 201: description: >- From b01178a711c730d95220da0e7e8c745c117d2380 Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Thu, 9 Jan 2020 14:57:21 +0000 Subject: [PATCH 04/10] add try-blocks to post requests. --- app/app.py | 2 +- app/services/post_services.py | 58 +++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/app/app.py b/app/app.py index 2d39258..3441d21 100644 --- a/app/app.py +++ b/app/app.py @@ -21,4 +21,4 @@ def robots(): return render_template("robots.txt") if __name__ == "__main__": - app.run(host="0.0.0.0", debug=True) + app.run(host="0.0.0.0", debug=False) diff --git a/app/services/post_services.py b/app/services/post_services.py index 2706b9c..0d3a1ea 100644 --- a/app/services/post_services.py +++ b/app/services/post_services.py @@ -19,34 +19,40 @@ the same directory as this: /services/. device_check_token = "QWERTYuiopasdfghjklzxcvbnm_1234567890" def add_latest_reading(meter, info): - if info["token"] == device_check_token: - reading = {"reading":info["reading"], "time":info["time"]} - 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) - return make_response("Invalid token.", 400) + try: + if info["token"] == device_check_token: + reading = {"reading":info["reading"], "time":info["time"]} + 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) + return make_response("Invalid token.", 400) + except: + return make_response("The data you sent was invalid or incorrectly formatted.", 400) def log_status_change(device, info): - if info["token"] == device_check_token: - status = {"time":info["time"], "status":info["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) - return make_response("Invalid token.", 400) + try: + if info["token"] == device_check_token: + status = {"time":info["time"], "status":info["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) + return make_response("Invalid token.", 400) + except: + return make_response("The data you sent was invalid or incorrectly formatted.", 400) ''' Nitty-Gritty Functions From 13e2f5d3ba970ad4cae7c632e509238976811d74 Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Thu, 9 Jan 2020 15:03:27 +0000 Subject: [PATCH 05/10] add 400-error info. to PUT request in YAML file. --- app/swagger.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/swagger.yml b/app/swagger.yml index fd5671a..cdbbb2a 100644 --- a/app/swagger.yml +++ b/app/swagger.yml @@ -79,6 +79,11 @@ paths: 201: description: >- The reading was successfully added to the database. + 400: + description: >- + The data sent to the sever was invalid or incorrectly + formatted. The most likely reasons are invalid light-meter + Id. specified and incorrect datetime format used. /readings/latest/{light_meter}: get: @@ -265,6 +270,11 @@ paths: 201: description: >- The status update was successfully added to the database. + 400: + description: >- + The data sent to the sever was invalid or incorrectly + formatted. The most likely reasons are invalid device Id. + specified and incorrect datetime format used. /status/latest/{device}: get: From 3e80738d32043681da593dc1fed8a02fadd02d6c Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Thu, 9 Jan 2020 15:08:33 +0000 Subject: [PATCH 06/10] add 400-error responses to GET requests in YAML file. --- app/swagger.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/swagger.yml b/app/swagger.yml index cdbbb2a..3d1c372 100644 --- a/app/swagger.yml +++ b/app/swagger.yml @@ -137,6 +137,9 @@ paths: This is the actual reading taken from the specified light meter, at the time specified in this response body (I.E. JSON object). + 400: + description: >- + The light-meter Id. specified in invalid. /readings/all/{light_meter}: get: @@ -188,6 +191,9 @@ paths: This is the actual reading taken from the specified light meter, at the time specified in this response body (I.E. JSON object). + 400: + description: >- + The light-meter Id. specified in invalid. /readings/all: get: @@ -326,6 +332,9 @@ paths: type: string example: on description: The current status of the device. + 400: + description: >- + The device Id. specified in invalid. /status/all/{device}: get: @@ -376,6 +385,9 @@ paths: example: off description: >- The status of the device after the change occurred. + 400: + description: >- + The device Id. specified in invalid. /status/all: get: From b4d9ca04faae014700435cb99d373e7781d925e0 Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Thu, 9 Jan 2020 15:09:06 +0000 Subject: [PATCH 07/10] reset database. --- app/readings.db | Bin 40960 -> 40960 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/app/readings.db b/app/readings.db index ab91286eeee0e7f8cb670a0a1ffbbf4e2b77df5b..0cb277458b94af1c041277debf1d061339bd1ef2 100644 GIT binary patch delta 194 zcmZoTz|?SnX~QRZK2s|L3o8>NJtK2N3sdt=0-yL%gv<=F2|2vy7ev-*W|W_nwuwkl NW2|aP5j6pt003@XH9P Date: Thu, 9 Jan 2020 15:18:40 +0000 Subject: [PATCH 08/10] add get latest (all) device status to YAML (A.P.I.) file. --- app/api.py | 3 +++ app/swagger.yml | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/app/api.py b/app/api.py index e7b6f14..37ac134 100644 --- a/app/api.py +++ b/app/api.py @@ -32,3 +32,6 @@ def get_all_status_log(device): def get_all_status_logs_for_every_device(): return get_services.get_all_status_changes_from_database() + +def get_current_status_for_all_devices(): + return get_services.get_latest_status_for_all_devices() diff --git a/app/swagger.yml b/app/swagger.yml index 3d1c372..3633052 100644 --- a/app/swagger.yml +++ b/app/swagger.yml @@ -413,3 +413,28 @@ paths: schema: type: object additionalProperties: True + + /status/latest: + get: + operationId: api.get_current_status_for_all_devices + tags: + - Request Device Status + summary: >- + Returns the last recorded status change of every device in the + database. + description: >- + Use this U.R.L. to retrieve the last recorded status change of + every device used in this project. There is no example of the + data returned because I can't seem to replicate it as a YAML + schema which is understood by Swagger. Because of this. the + return type is registered as a free-form object. To see what + the data looks like when it is returned, I recommend you use + the "Try it out!" button to see a working example. + responses: + 200: + description: >- + The last recorded status change of every device in this + project was retrieved successfully. + schema: + type: object + additionalProperties: True From 427d263812895203946ff9d7c59b5ba107ec343b Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Thu, 9 Jan 2020 15:21:36 +0000 Subject: [PATCH 09/10] fix typo in YAML file. --- app/swagger.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/swagger.yml b/app/swagger.yml index 3633052..ba20231 100644 --- a/app/swagger.yml +++ b/app/swagger.yml @@ -342,7 +342,7 @@ paths: tags: - Request Device Status summary: >- - Returns every status changed logged by the specified device. + Returns every status change 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 From ba85b560ea5c8e809c8b00e9a7b9920ebc6c21f2 Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Thu, 9 Jan 2020 15:44:41 +0000 Subject: [PATCH 10/10] add tim e of request info. to home. --- app/app.py | 5 +++-- app/static/styles/main.css | 9 +++++++-- app/swagger.yml | 2 +- app/templates/home.html | 3 ++- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/app.py b/app/app.py index 3441d21..5d39a02 100644 --- a/app/app.py +++ b/app/app.py @@ -1,4 +1,5 @@ from flask import Flask, render_template +from datetime import datetime import connexion import config from services import get_services @@ -12,8 +13,8 @@ app.add_api("swagger.yml") @app.route("/") def home (): data = get_services.get_latest_status_for_all_devices() - print(data) - return render_template("home.html", data=data) + time = datetime.now() + return render_template("home.html", data=data, time=time) @app.route("/robots.txt") @app.route("/robots") diff --git a/app/static/styles/main.css b/app/static/styles/main.css index 8e43d12..ef51345 100644 --- a/app/static/styles/main.css +++ b/app/static/styles/main.css @@ -40,6 +40,7 @@ main div .status-bar { padding: 20px; border-top: 2px solid #424242; border-bottom: 2px solid #424242; + margin: 5px 0; } .meter { @@ -60,7 +61,11 @@ main div .status-bar .meter img { } -main div h2 { - font-size: 16px; +main div .server-header, +main div .server-time { + margin: 0; } +main div .server-time { + font-size: 12px; +} diff --git a/app/swagger.yml b/app/swagger.yml index ba20231..ea7275f 100644 --- a/app/swagger.yml +++ b/app/swagger.yml @@ -2,7 +2,7 @@ swagger: "2.0" info: description: >- The 'Return to Ritherdon' project, by Nicola Ellis, is a two year art residency at Ritherdon & Co Ltd, a manufacturer of metal enclosures based in Darwen, Lancashire U.K. This website is part of the many outcomes produced throughout the duration of project. - version: "1.0.0 - Alpha" + version: "1.0.0 - Beta" title: Return to Ritherdon Project A.P.I. consumes: - application/json diff --git a/app/templates/home.html b/app/templates/home.html index 4ff8af3..24de9ed 100644 --- a/app/templates/home.html +++ b/app/templates/home.html @@ -9,7 +9,8 @@

Return to Ritherdon Project

-

Artwork Status

+

Artwork Status

+

Time on Server: {{ time }}