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(): def get_all_status_logs_for_every_device():
return get_services.get_all_status_changes_from_database() 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 flask import Flask, render_template
from datetime import datetime
import connexion import connexion
import config import config
from services import get_services from services import get_services
@ -12,8 +13,8 @@ app.add_api("swagger.yml")
@app.route("/") @app.route("/")
def home (): def home ():
data = get_services.get_latest_status_for_all_devices() data = get_services.get_latest_status_for_all_devices()
print(data) time = datetime.now()
return render_template("home.html", data=data) return render_template("home.html", data=data, time=time)
@app.route("/robots.txt") @app.route("/robots.txt")
@app.route("/robots") @app.route("/robots")
@ -21,4 +22,4 @@ def robots():
return render_template("robots.txt") return render_template("robots.txt")
if __name__ == "__main__": 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/. the same directory as this: /services/.
''' '''
def add_latest_reading(meter,reading): device_check_token = "QWERTYuiopasdfghjklzxcvbnm_1234567890"
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): def add_latest_reading(meter, info):
if device == 1: try:
return add_status_change_to_device1(status) if info["token"] == device_check_token:
elif device == 2: reading = {"reading":info["reading"], "time":info["time"]}
return add_status_change_to_device2(status) if meter == 1:
elif device == 3: return add_reading_to_meter1(reading)
return add_status_change_to_device3(status) elif meter == 2:
elif device == 4: return add_reading_to_meter2(reading)
return add_status_change_to_device4(status) elif meter == 3:
elif device == 5: return add_reading_to_meter3(reading)
return add_status_change_to_device5(status) return make_response("Meter Id. not recognised. Must be between 1 and 3.", 400)
elif device == 6: return make_response("Invalid token.", 400)
return add_status_change_to_device6(status) except:
return make_response("Device Id. not recognised. Must be between 1 and 6.", 400) 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 Nitty-Gritty Functions

9
app/static/styles/main.css

@ -40,6 +40,7 @@ main div .status-bar {
padding: 20px; padding: 20px;
border-top: 2px solid #424242; border-top: 2px solid #424242;
border-bottom: 2px solid #424242; border-bottom: 2px solid #424242;
margin: 5px 0;
} }
.meter { .meter {
@ -60,7 +61,11 @@ main div .status-bar .meter img {
} }
main div h2 { main div .server-header,
font-size: 16px; 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: info:
description: >- 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. 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. title: Return to Ritherdon Project A.P.I.
consumes: consumes:
- application/json - application/json
@ -69,10 +69,21 @@ paths:
This represents the amount of light the light meter This represents the amount of light the light meter
recorded. This is the most important piece of data recorded. This is the most important piece of data
you will post in this data-object. 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: responses:
201: 201:
description: >- description: >-
The reading was successfully added to the database. 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}: /readings/latest/{light_meter}:
get: get:
@ -126,6 +137,9 @@ paths:
This is the actual reading taken from the specified This is the actual reading taken from the specified
light meter, at the time specified in this response light meter, at the time specified in this response
body (I.E. JSON object). body (I.E. JSON object).
400:
description: >-
The light-meter Id. specified in invalid.
/readings/all/{light_meter}: /readings/all/{light_meter}:
get: get:
@ -177,6 +191,9 @@ paths:
This is the actual reading taken from the specified This is the actual reading taken from the specified
light meter, at the time specified in this response light meter, at the time specified in this response
body (I.E. JSON object). body (I.E. JSON object).
400:
description: >-
The light-meter Id. specified in invalid.
/readings/all: /readings/all:
get: get:
@ -249,10 +266,21 @@ paths:
description: >- description: >-
The current status of the device you would like to log. The current status of the device you would like to log.
The two status types are "on" and "off". 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: responses:
201: 201:
description: >- description: >-
The status update was successfully added to the database. 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}: /status/latest/{device}:
get: get:
@ -304,6 +332,9 @@ paths:
type: string type: string
example: on example: on
description: The current status of the device. description: The current status of the device.
400:
description: >-
The device Id. specified in invalid.
/status/all/{device}: /status/all/{device}:
get: get:
@ -311,7 +342,7 @@ paths:
tags: tags:
- Request Device Status - Request Device Status
summary: >- summary: >-
Returns every status changed logged by the specified device. Returns every status change logged by the specified device.
description: >- description: >-
Use this U.R.L. to retrieve every change in the specified Use this U.R.L. to retrieve every change in the specified
devices state. At the time of writing, the project has six devices state. At the time of writing, the project has six
@ -354,6 +385,9 @@ paths:
example: off example: off
description: >- description: >-
The status of the device after the change occurred. The status of the device after the change occurred.
400:
description: >-
The device Id. specified in invalid.
/status/all: /status/all:
get: get:
@ -379,3 +413,28 @@ paths:
schema: schema:
type: object type: object
additionalProperties: True 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> <h1>Return to Ritherdon Project</h1>
</div> </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"> <div class="status-bar">