First version of board load/save

Rafael
Rafael M. Martins 7 months ago
parent 1012c25b34
commit 71864dc610
  1. 1
      py/Boards/Charlie/First-example-board.json
  2. 55
      py/README.md
  3. 57
      py/main.py

@ -0,0 +1 @@
{"Name": "First example board", "Description": "This is the first example board we have in the database.", "Hus": "Charlie", "col-left": [{"order": 0, "id": "parallel1", "text": "A parallel coordinates plot", "api_url": "..."}], "col-center": [{"order": 0, "id": "grid1", "text": "A grid plot", "api_url": "..."}], "col-right": [{"order": 0, "id": "horizon1", "text": "A horizon plot", "api_url": "..."}]}

@ -147,16 +147,17 @@ Returns data formatted for the 'yearly' horizon view.
## Saved Flexiboards
### /boards/Hus_ID
### /boards
A short summary of all the boards that are saved.
* 'hus' [Mandatory]: the house for which you want the boards listed.
[
{
Board_ID [int],
Filename [String],
Name [String],
Description [String],
URL [String] // https://localhost:8000/h=Hus&b=Board_ID
Desc [String]
}
]
@ -164,9 +165,12 @@ A short summary of all the boards that are saved.
Method: POST
Every time we call save, it creates a new board.
**ID is auto generated.** // nome -> lowercase -> replace special characters to -
**Name must be unique.**
IMPORTANT: You need to send the Header: 'Content-Type: application/json'
Every time we call save, it creates a new board.
* ID is auto generated. // name -> replace all non-alphanumeric characters with -
* Name must be unique, or else the board will be overwritten.
{
Name [String],
@ -191,30 +195,17 @@ Every time we call save, it creates a new board.
] // same as above
}
### /boards/[Board_ID]
### /boards/load
Loads all the detailed information for the board with Board_ID.
Loads a saved board.
{
Board_ID [int],
Name [String],
Description [String],
Hus [String],
col-left: [
{
order [int],
id [String],
text [String],
api_url [int]
},
{
...
}
],
col-center: [
...
], // same as above
col-right: [
...
] // same as above
}
* filename [String]: To know the filename, check the list in "/boards".
The format is the same that was provided in "/boards/save" (above).
# Todo
* Define the desired time for some of the plots instead of taking the first one available.
- Use the hour only as a filter; take the first available measurement on that hour.
* Instead of returning an actual measurement, return a modified value: min/max/avg.

@ -4,6 +4,9 @@ import sqlite3
from datetime import date, timedelta
from flask_cors import CORS
import re
import json
import os
import glob
DB_NAMES = {
@ -491,3 +494,57 @@ def horizon_daily():
out[sensor_name] = [x[2] for x in sensor_data_sorted]
return out
@app.route('/boards')
def boards():
hus = request.args.get('hus', 'Charlie')
all_files = glob.glob(f"Boards/{hus}/*.json")
output = []
for filename in all_files:
with open(filename, 'r') as f:
_id = os.path.basename(filename)
obj = json.load(f)
output.append(dict(Filename=_id, Name=obj['Name'], Desc=obj['Description']))
return output
@app.route('/boards/save', methods=['POST'])
def boards_save():
b = request.json
if not b:
return "Failed; no board was sent via POST."
hus = b['Hus']
try:
os.mkdir(f'Boards/{hus}')
except FileExistsError:
# No problem
pass
_id = re.sub('[^0-9a-zA-Z]', '-', b['Name'])
with open(f'Boards/{hus}/{_id}.json', 'w') as f:
json.dump(b, f)
return 'OK'
@app.route('/boards/load')
def boards_load():
hus = request.args.get('hus')
filename = request.args.get('filename')
if not filename or not hus:
return "ERROR: the parameters 'hus' and 'filename' are both mandatory."
fullpath = f'Boards/{hus}/{filename}'
try:
with open(fullpath, 'r') as f:
output = json.load(f)
except FileNotFoundError:
return "ERROR: specified hus/file was not found."
return output

Loading…
Cancel
Save