1
0
Fork 0
Browse Source

merge of unstable into stable

stable
Craig Oates 4 years ago
parent
commit
747f6b847c
  1. 3
      app/api.py
  2. 7
      app/app.py
  3. BIN
      app/readings.db
  4. 58
      app/services/post_services.py
  5. 9
      app/static/styles/main.css
  6. 63
      app/swagger.yml
  7. 3
      app/templates/home.html

3
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()

7
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")
@ -21,4 +22,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)

BIN
app/readings.db

Binary file not shown.

58
app/services/post_services.py

@ -16,29 +16,43 @@ 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 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)
def add_latest_reading(meter, info):
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):
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

9
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;
}

63
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
@ -69,10 +69,21 @@ 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: >-
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:
@ -126,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:
@ -177,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:
@ -249,10 +266,21 @@ paths:
description: >-
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: >-
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:
@ -304,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:
@ -311,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
@ -354,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:
@ -379,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

3
app/templates/home.html

@ -9,7 +9,8 @@
<h1>Return to Ritherdon Project</h1>
</div>
<h2>Artwork Status</h2>
<h2 class="server-header">Artwork Status</h2>
<p class="server-time">Time on Server: {{ time }}</p>
<div class="status-bar">