# HG changeset patch # User Dennis Fink # Date 2016-03-15 20:02:10 # Node ID 35503d1d83e733c321ed17927ffd7edbab0d845a # Parent 152c56e9733a930c60725fd4204adcf737f0fcd9 Update diff --git a/spaceapi/schema/sensors.json b/spaceapi/schema/sensors.json --- a/spaceapi/schema/sensors.json +++ b/spaceapi/schema/sensors.json @@ -1,1 +1,543 @@ -{"people_now_present": {"required": ["value"], "type": "object", "properties": {"names": {"type": "array", "items": {"type": "string"}}, "location": {"type": "string"}, "description": {"type": "string"}, "value": {"type": "number"}, "name": {"type": "string"}}}, "wind": {"required": ["properties", "location"], "type": "object", "properties": {"location": {"type": "string"}, "description": {"type": "string"}, "properties": {"required": ["speed", "gust", "direction", "elevation"], "type": "object", "properties": {"speed": {"required": ["value", "unit"], "type": "object", "properties": {"unit": {"enum": ["m/s", "km/h", "kn"]}, "value": {"type": "number"}}}, "elevation": {"required": ["value", "unit"], "type": "object", "properties": {"unit": {"enum": ["m"]}, "value": {"type": "number"}}}, "direction": {"required": ["value", "unit"], "type": "object", "properties": {"unit": {"enum": ["\u00b0"]}, "value": {"type": "number"}}}, "gust": {"required": ["value", "unit"], "type": "object", "properties": {"unit": {"enum": ["m/s", "km/h", "kn"]}, "value": {"type": "number"}}}}}, "name": {"type": "string"}}}, "total_member_count": {"required": ["value"], "type": "object", "properties": {"location": {"type": "string"}, "description": {"type": "string"}, "value": {"type": "number"}, "name": {"type": "string"}}}, "barometer": {"required": ["value", "unit", "location"], "type": "object", "properties": {"description": {"type": "string"}, "location": {"type": "string"}, "unit": {"enum": ["hPA"]}, "value": {"type": "number"}, "name": {"type": "string"}}}, "humidity": {"required": ["value", "unit", "location"], "type": "object", "properties": {"description": {"type": "string"}, "location": {"type": "string"}, "unit": {"enum": ["%"]}, "value": {"type": "number"}, "name": {"type": "string"}}}, "account_balance": {"required": ["value", "unit"], "type": "object", "properties": {"description": {"type": "string"}, "location": {"type": "string"}, "unit": {"enum": ["BTC", "EUR", "USD", "GBP"]}, "value": {"type": "number"}, "name": {"type": "string"}}}, "radiation": {"beta": {"type": "array", "items": {"required": ["value", "unit"], "type": "object", "properties": {"description": {"type": "string"}, "dead_time": {"type": "number"}, "unit": {"enum": ["cpm", "r/h", "\u00b5Sv/h", "mSv/a", "\u00b5Sv/a"]}, "conversion_factor": {"type": "number"}, "location": {"type": "string"}, "value": {"type": "number"}, "name": {"type": "string"}}}}, "gamma": {"type": "array", "items": {"required": ["value", "unit"], "type": "object", "properties": {"description": {"type": "string"}, "dead_time": {"type": "number"}, "unit": {"enum": ["cpm", "r/h", "\u00b5Sv/h", "mSv/a", "\u00b5Sv/a"]}, "conversion_factor": {"type": "number"}, "location": {"type": "string"}, "value": {"type": "number"}, "name": {"type": "string"}}}}, "type": "object", "beta_gamma": {"type": "array", "items": {"required": ["value", "unit"], "type": "object", "properties": {"description": {"type": "string"}, "dead_time": {"type": "number"}, "unit": {"enum": ["cpm", "r/h", "\u00b5Sv/h", "mSv/a", "\u00b5Sv/a"]}, "conversion_factor": {"type": "number"}, "location": {"type": "string"}, "value": {"type": "number"}, "name": {"type": "string"}}}}, "alpha": {"type": "array", "items": {"required": ["value", "unit"], "type": "object", "properties": {"description": {"type": "string"}, "dead_time": {"type": "number"}, "unit": {"enum": ["cpm", "r/h", "\u00b5Sv/h", "mSv/a", "\u00b5Sv/a"]}, "conversion_factor": {"type": "number"}, "location": {"type": "string"}, "value": {"type": "number"}, "name": {"type": "string"}}}}}, "power_consumption": {"required": ["value", "unit", "location"], "type": "object", "properties": {"description": {"type": "string"}, "location": {"type": "string"}, "unit": {"enum": ["mW", "W", "VA"]}, "value": {"type": "number"}, "name": {"type": "string"}}}, "door_locked": {"required": ["value", "location"], "type": "object", "properties": {"location": {"type": "string"}, "description": {"type": "string"}, "value": {"type": "boolean"}, "name": {"type": "string"}}}, "temperature": {"required": ["value", "unit", "location"], "type": "object", "properties": {"description": {"type": "string"}, "location": {"type": "string"}, "unit": {"enum": ["\u00b0C", "\u00b0F", "K", "\u00b0De", "\u00b0N", "\u00b0R", "\u00b0R\u00e9", "\u00b0R\u00f8"]}, "value": {"type": "number"}, "name": {"type": "string"}}}, "beverage_supply": {"required": ["value", "unit"], "type": "object", "properties": {"description": {"type": "string"}, "location": {"type": "string"}, "unit": {"enum": ["btl", "crt"]}, "value": {"type": "number"}, "name": {"type": "string"}}}, "network_connections": {"required": ["value"], "type": "object", "properties": {"description": {"type": "string"}, "type": {"enum": ["wifi", "cable", "spacenet"]}, "location": {"type": "string"}, "machines": {"type": "array", "items": {"type": "object", "properties": {"mac": {"type": "string"}, "name": {"type": "string"}}}}, "value": {"type": "number"}, "name": {"type": "string"}}}} \ No newline at end of file +{ + "account_balance" : { + "type" : "object", + "required" : [ + "value", + "unit" + ], + "properties" : { + "value" : { + "type" : "number" + }, + "name" : { + "type" : "string" + }, + "location" : { + "type" : "string" + }, + "unit" : { + "enum" : [ + "BTC", + "EUR", + "USD", + "GBP" + ] + }, + "description" : { + "type" : "string" + } + } + }, + "people_now_present" : { + "properties" : { + "location" : { + "type" : "string" + }, + "names" : { + "type" : "array", + "items" : { + "type" : "string" + } + }, + "description" : { + "type" : "string" + }, + "name" : { + "type" : "string" + }, + "value" : { + "type" : "number" + } + }, + "required" : [ + "value" + ], + "type" : "object" + }, + "temperature" : { + "properties" : { + "name" : { + "type" : "string" + }, + "value" : { + "type" : "number" + }, + "description" : { + "type" : "string" + }, + "location" : { + "type" : "string" + }, + "unit" : { + "enum" : [ + "°C", + "°F", + "K", + "°De", + "°N", + "°R", + "°Ré", + "°Rø" + ] + } + }, + "required" : [ + "value", + "unit", + "location" + ], + "type" : "object" + }, + "barometer" : { + "properties" : { + "name" : { + "type" : "string" + }, + "value" : { + "type" : "number" + }, + "unit" : { + "enum" : [ + "hPA" + ] + }, + "location" : { + "type" : "string" + }, + "description" : { + "type" : "string" + } + }, + "required" : [ + "value", + "unit", + "location" + ], + "type" : "object" + }, + "radiation" : { + "beta" : { + "type" : "array", + "items" : { + "properties" : { + "location" : { + "type" : "string" + }, + "unit" : { + "enum" : [ + "cpm", + "r/h", + "µSv/h", + "mSv/a", + "µSv/a" + ] + }, + "description" : { + "type" : "string" + }, + "conversion_factor" : { + "type" : "number" + }, + "dead_time" : { + "type" : "number" + }, + "value" : { + "type" : "number" + }, + "name" : { + "type" : "string" + } + }, + "type" : "object", + "required" : [ + "value", + "unit" + ] + } + }, + "alpha" : { + "type" : "array", + "items" : { + "properties" : { + "unit" : { + "enum" : [ + "cpm", + "r/h", + "µSv/h", + "mSv/a", + "µSv/a" + ] + }, + "location" : { + "type" : "string" + }, + "description" : { + "type" : "string" + }, + "dead_time" : { + "type" : "number" + }, + "conversion_factor" : { + "type" : "number" + }, + "name" : { + "type" : "string" + }, + "value" : { + "type" : "number" + } + }, + "type" : "object", + "required" : [ + "value", + "unit" + ] + } + }, + "gamma" : { + "items" : { + "type" : "object", + "required" : [ + "value", + "unit" + ], + "properties" : { + "description" : { + "type" : "string" + }, + "conversion_factor" : { + "type" : "number" + }, + "dead_time" : { + "type" : "number" + }, + "unit" : { + "enum" : [ + "cpm", + "r/h", + "µSv/h", + "mSv/a", + "µSv/a" + ] + }, + "location" : { + "type" : "string" + }, + "value" : { + "type" : "number" + }, + "name" : { + "type" : "string" + } + } + }, + "type" : "array" + }, + "beta_gamma" : { + "items" : { + "properties" : { + "location" : { + "type" : "string" + }, + "unit" : { + "enum" : [ + "cpm", + "r/h", + "µSv/h", + "mSv/a", + "µSv/a" + ] + }, + "dead_time" : { + "type" : "number" + }, + "conversion_factor" : { + "type" : "number" + }, + "description" : { + "type" : "string" + }, + "value" : { + "type" : "number" + }, + "name" : { + "type" : "string" + } + }, + "required" : [ + "value", + "unit" + ], + "type" : "object" + }, + "type" : "array" + }, + "type" : "object" + }, + "network_connections" : { + "required" : [ + "value" + ], + "type" : "object", + "properties" : { + "machines" : { + "items" : { + "properties" : { + "name" : { + "type" : "string" + }, + "mac" : { + "type" : "string" + } + }, + "type" : "object" + }, + "type" : "array" + }, + "value" : { + "type" : "number" + }, + "name" : { + "type" : "string" + }, + "type" : { + "enum" : [ + "wifi", + "cable", + "spacenet" + ] + }, + "location" : { + "type" : "string" + }, + "description" : { + "type" : "string" + } + } + }, + "beverage_supply" : { + "required" : [ + "value", + "unit" + ], + "type" : "object", + "properties" : { + "name" : { + "type" : "string" + }, + "value" : { + "type" : "number" + }, + "unit" : { + "enum" : [ + "btl", + "crt" + ] + }, + "location" : { + "type" : "string" + }, + "description" : { + "type" : "string" + } + } + }, + "door_locked" : { + "properties" : { + "description" : { + "type" : "string" + }, + "location" : { + "type" : "string" + }, + "value" : { + "type" : "boolean" + }, + "name" : { + "type" : "string" + } + }, + "type" : "object", + "required" : [ + "value", + "location" + ] + }, + "humidity" : { + "properties" : { + "name" : { + "type" : "string" + }, + "value" : { + "type" : "number" + }, + "unit" : { + "enum" : [ + "%" + ] + }, + "location" : { + "type" : "string" + }, + "description" : { + "type" : "string" + } + }, + "required" : [ + "value", + "unit", + "location" + ], + "type" : "object" + }, + "wind" : { + "properties" : { + "name" : { + "type" : "string" + }, + "description" : { + "type" : "string" + }, + "properties" : { + "type" : "object", + "required" : [ + "speed", + "gust", + "direction", + "elevation" + ], + "properties" : { + "speed" : { + "required" : [ + "value", + "unit" + ], + "type" : "object", + "properties" : { + "unit" : { + "enum" : [ + "m/s", + "km/h", + "kn" + ] + }, + "value" : { + "type" : "number" + } + } + }, + "direction" : { + "type" : "object", + "required" : [ + "value", + "unit" + ], + "properties" : { + "unit" : { + "enum" : [ + "°" + ] + }, + "value" : { + "type" : "number" + } + } + }, + "gust" : { + "required" : [ + "value", + "unit" + ], + "type" : "object", + "properties" : { + "value" : { + "type" : "number" + }, + "unit" : { + "enum" : [ + "m/s", + "km/h", + "kn" + ] + } + } + }, + "elevation" : { + "type" : "object", + "required" : [ + "value", + "unit" + ], + "properties" : { + "unit" : { + "enum" : [ + "m" + ] + }, + "value" : { + "type" : "number" + } + } + } + } + }, + "location" : { + "type" : "string" + } + }, + "type" : "object", + "required" : [ + "properties", + "location" + ] + }, + "total_member_count" : { + "properties" : { + "description" : { + "type" : "string" + }, + "location" : { + "type" : "string" + }, + "value" : { + "type" : "number" + }, + "name" : { + "type" : "string" + } + }, + "required" : [ + "value" + ], + "type" : "object" + }, + "power_consumption" : { + "required" : [ + "value", + "unit", + "location" + ], + "type" : "object", + "properties" : { + "name" : { + "type" : "string" + }, + "value" : { + "type" : "number" + }, + "location" : { + "type" : "string" + }, + "unit" : { + "enum" : [ + "mW", + "W", + "VA" + ] + }, + "description" : { + "type" : "string" + } + } + } +} diff --git a/spaceapi/sensors.py b/spaceapi/sensors.py --- a/spaceapi/sensors.py +++ b/spaceapi/sensors.py @@ -8,14 +8,13 @@ import jsonschema from flask import Blueprint, jsonify, request, abort, current_app from .auth import httpauth -#from .active import save_last_state from .utils import first, fuzzy_list_find, ActiveStatus sensors_views = Blueprint('sensors', __name__) ALLOWED_SENSORS_KEYS = json.load( open(resource_filename('spaceapi', 'schema/sensors.json'), - encoding='utf-8') + encoding='utf-8') ) RADIATON_SUBKEYS = frozenset(('alpha', 'beta', 'gamma', 'beta_gamma')) diff --git a/spaceapi/templates/base.html b/spaceapi/templates/base.html --- a/spaceapi/templates/base.html +++ b/spaceapi/templates/base.html @@ -8,8 +8,14 @@ {% block body %}
- {% block navbar %} - {% endblock %} +
{% block content %} diff --git a/spaceapi/templates/index.html b/spaceapi/templates/index.html --- a/spaceapi/templates/index.html +++ b/spaceapi/templates/index.html @@ -2,10 +2,10 @@ {% block content %} {% if status['state']['open'] %} - -

Yes, we're open!

+ +

Yes, we're open!

{% else %} - -

Sorry, we're closed!

+ +

Sorry, we're closed!

{% endif %} {% endblock %} diff --git a/spaceapi/templates/open.html b/spaceapi/templates/open.html --- a/spaceapi/templates/open.html +++ b/spaceapi/templates/open.html @@ -1,10 +1,10 @@ {% extends "base.html" %} {% block content %} -
-
- - -
-
+
+
+ + +
+
{% endblock %} diff --git a/spaceapi/utils.py b/spaceapi/utils.py --- a/spaceapi/utils.py +++ b/spaceapi/utils.py @@ -12,8 +12,6 @@ if not os.path.exists(default_json_file) elif not os.path.isfile(default_json_file): raise RuntimeError('default.json is not a file!') -_default_json = {} - class Singleton: @@ -32,33 +30,24 @@ class Singleton: class ActiveStatus(Singleton, dict): def reload(self): - global _default_json with open(default_json_file, encoding='utf-8') as f: - _default_json = json.load(f) + self.update(json.load(f)) if os.path.exists(last_state_file) and os.path.isfile(last_state_file): with open(last_state_file, encoding='utf-8') as f: - self.update(json.load(f)) - - if os.path.getmtime(last_state_file) \ - < os.path.getmtime(default_json_file): - backup_open = self['state']['open'] - backup_lastchange = self['state']['lastchange'] - backup_sensors = self['sensors'] + last_state_json = json.load(f) - self.update(_default_json) - - self['state']['open'] = backup_open - self['state']['lastchange'] = backup_lastchange - self['sensors'].update(backup_sensors) - else: - self.update(_default_json) + self['state'] = last_state_json['state'] + self['sensors'].update(last_state_json['sensors']) def save_last_state(self): with open(last_state_file, mode='w', encoding='utf-8') as f: - json.dump(self, f, sort_keys=True) + last_state = {} + last_state['state'] = self['state'] + last_state['sensors'] = self['sensors'] + json.dump(last_state, f, sort_keys=True) def request_wants_json(): diff --git a/spaceapi/views.py b/spaceapi/views.py --- a/spaceapi/views.py +++ b/spaceapi/views.py @@ -32,8 +32,10 @@ def reload(): def open(): if request.method == 'POST': if 'close' in request.form: - return set_new_state('open', False) + set_new_state('open', False) + return redirect(url_for('root.index')) elif 'open' in request.form: - return set_new_state('open', True) + set_new_state('open', True) + return redirect(url_for('root.index')) return render_template('open.html')