Commit 42da4364 authored by Nelso Jost's avatar Nelso Jost

FIX main map sensor values

parent 9d66a990
......@@ -6,5 +6,7 @@
*.pyc
.gunicorn.conf
settings.ini.bk
settings.ini
Session.vim
pid
__pycache__
import requests
BASE_URL = 'http://localhost:5000'
def get_boardhash(board_id, user_password):
requests.post()
def post_rawsensordata(board_hash, sen)
......@@ -149,6 +149,8 @@ class BoardInsertForm(Form):
class SensorListItemForm(Form):
# nickname = SelectField('Apelido', choices=[
# (s['name'], s['name']) for s in config.DB_INIT['SENSORS']], coerce=str)
nickname = StringField('Apelido', validators=[
Required(), Length(1, 20), Regexp('^[A-Za-z][A-Za-z0-9_]*$', 0,
"Apelidos devem possuir apenas letras letras, números, "
......
......@@ -85,7 +85,7 @@ def insert():
board_insert_form = BoardInsertForm()
if request.method == 'POST':
pprint(request.form)
# pprint(request.form)
# got any button click? if so, the btn will appear on the request.form
if 'btn_cancel' in request.form:
return redirect(url_for('main.index'))
......@@ -96,7 +96,6 @@ def insert():
return redirect(url_for('board.main', id=board.id))
mu_map = json.dumps({m.name: m.unities for m in measurements.ls})
return render_template('board/insert.html', board_form=board_insert_form,
mu_map=mu_map, sensor_form=SensorListItemForm())
......
......@@ -38,17 +38,27 @@ class DBInitCommand(Command):
Sensor.import_from_json(*db_init['SENSORS'])
CSV_FILENAME = path_here('data/26-Fev-2013_-_04-Mar-2013.log')
CSV_SEP = '\t'
with open(CSV_FILENAME) as f:
for line in f:
l = line.strip()
if l.startswith('datetime'):
sensor_names = [x for x in l.split(CSV_SEP)
if x != '#' and not x.startswith('datetime')]
Board.add(nickname='PezziProto', user=user_admin,
longitude='-51.098163127899156',
latitude='-30.085322758845493',
exposition='outdoor', default_sensors=True)
exposition='outdoor',
sensors=sensor_names)
db.session.commit()
CSV_FILENAME = path_here('data/26-Fev-2013_-_04-Mar-2013.log')
print('\nImporting data from:\n {}'.format(CSV_FILENAME))
boards.get(nickname = 'PezziProto').import_csv_sensor_data(
filename=CSV_FILENAME, sep='\t', debug=True)
filename=CSV_FILENAME, CSV_SEP='\t', debug=True)
print('Writing changes on the disc..', end='')
db.session.commit()
......
......@@ -17,7 +17,7 @@ from flask import url_for
import csv
from datetime import datetime
import random
class Board(db.Model, BaseMixin):
__tablename__ = 'boards'
......@@ -55,14 +55,23 @@ class Board(db.Model, BaseMixin):
def exposition_name(self):
return self.exposition.name if self.exposition else None
@property
def ls_sensors(self):
return self.sensors.all()
@property
def ls_rawdata(self):
return self.rawdata.all()
@property
def last_rawdata_json(self):
d = {}
last_data = self.rawdata.order_by(RawSensorData.datetime.desc())\
.first()
d['data'] = last_data.json if last_data else None
d['datetime'] = last_data.datetime.strftime("%Y-%m-%d %H:%M:%S")
d['board_url'] = url_for('board.main', id=self.id)
d['sensors'] = []
d['sensors'] = {}
for bs in self.sensors:
s = bs.json
s.update(**bs.sensor.json)
......@@ -70,7 +79,10 @@ class Board(db.Model, BaseMixin):
s['unity'] = bs.sensor.unitymeasurement.unity.json
s['url'] = url_for('board.sensor',
id=self.id, sensor_nickname=bs.sensor.name)
d['sensors'].append(s)
s['value'] = last_data.sensor_values[bs.id]
d['sensors'][bs.id] = s
d['sensor_order'] = [s.id for s in self.sensors.order_by('order').all()]
return d
@classmethod
......@@ -140,7 +152,12 @@ class Board(db.Model, BaseMixin):
Default: ISO similar
'''
RawSensorData.add(datetime=datetime.strptime(dt, dt_format),
board_id=self.id, sensor_values=sensor_values)
board_id=self.id, sensor_values=sensor_values)
def add_randomdata(self):
RawSensorData.add(datetime=datetime.now(), board_id=self.id,
sensor_values={s.id: str(random.randint(0, 100))
for s in self.sensors.all()})
@classmethod
......@@ -148,8 +165,8 @@ class Board(db.Model, BaseMixin):
''' Recursively delete all information related to this board.
'''
board = cls.get(key)
self.rawdata.delete()
self.sensors.delete()
board.rawdata.delete()
board.sensors.delete()
db.session.delete(board)
......
......@@ -74,6 +74,6 @@ th, td {
}
.ol-popup-closer:after {
content: "x";
}
}
......@@ -144,30 +144,23 @@ var setup_label_content = function(feature)
+ String(feature.board.id)
+ ') <hr style="margin:0em"> <table>';
d.sensors.forEach(
function (s) {
val = (d.data != null ? d.data.sensor_values[s.id] : null);
d.sensor_order.forEach(
function (id) {
var sensor = d.sensors[id];
res += ('<tr>' +
'<td>{ms}</td><td>{sp}={sp}</td><td>{val}</td>' +
'</tr>').supplant({
ms: s.name,
sp : sp,
val: String(val) + sp + s.unity.label
//val: (val != null ? String(val) + sp + s.unity.label
//: '(null)')
})});
'<td>{name}</td><td>{sp}={sp}</td><td>{val}{sp}{unity}</td>' +
'</tr>').supplant({name: sensor.name, sp : sp,
unity: sensor.unity.label,
val: String(sensor.value)});
});
res += '</table>';
res += '<hr style="margin:0em">';
if (d.data != null) {
res += '<tr><td colspan="3">{dt}</td></tr>'.supplant({
dt: d.data.datetime.replace(' ', sp).replace('T', sp)});
if (d.datetime != null) {
res += '<tr><td colspan="3">{dt}</td></tr> (last)'.supplant({
dt: d.datetime.replace(' ', sp).replace('T', sp)});
}
res += ' (last data)';
label_content.innerHTML = res;
}
......@@ -176,27 +169,29 @@ var setup_popup_content = function(feature)
{
var res = '';
var d = feature.board.last_rawdata_json;
console.log("baka", feature)
res += '<table class="table">';
if (d.data != null) {
if (d.datetime != null) {
res += '<tr><td colspan="2" class="popup-dt-col"><b>{dt}</b></td></tr>'
.supplant({
dt: d.data.datetime.replace(' ', sp).replace('T', sp)});
dt: d.datetime.replace(' ', sp).replace('T', sp)});
}
d.sensors.forEach(
function (s) {
val = (d.data != null ? d.data.sensor_values[s.id] : null);
d.sensor_order.forEach(
function (id) {
var sensor = d.sensors[id];
res += ('<tr class="tr">' +
'<td class="td"><a href="{url}">{measurement}</a></td>' +
'<td class="td"><a href="{url}">{measurement}{sp}({name})</a></td>' +
'<td class="td"><code>{value}</code></td>' +
'</tr>').supplant({
sp: sp,
url: s.url,
measurement: s.measurement.label_br.replace(' ', sp),
value: (val == null ? '(null)' : val + sp + s.unity.label)
url: sensor.url,
name: sensor.name,
measurement: sensor.measurement.label_br.replace(' ', sp),
value: String(sensor.value) + sp + sensor.unity.label
});
}
);
......
......@@ -10,8 +10,8 @@
{% block scripts %}
{{ super() }}
<script src="http://openlayers.org/en/v3.13.1/build/ol.js" type="text/javascript"></script>
<script src="{{ url_for('static', filename='js/validator.js') }}"></script>
<script src="http://openlayers.org/en/v3.13.1/build/ol.js" type="text/javascript"></script>
<script src="{{ url_for('static', filename='js/validator.js') }}"></script>
<!-- <script src="{{ url_for('static', filename='js/jquery.validate.min.js') }}"></script> -->
<!-- <script src="{{ url_for('static', filename='js/messages_pt_BR.js') }}"></script> -->
<!-- <script src="{{ url_for('static', filename='js/jquery.validationEngine.js') }}"></script> -->
......@@ -21,9 +21,9 @@
var image_pin_filename = '{{ url_for('static', filename='images/red-pin.svg') }}';
var mu_map = {{ mu_map|safe }};
var sensors = {{ board_form.sensors|safe }};
console.log(sensors);
var $nickname = $($.parseHTML(
......@@ -36,12 +36,12 @@
'{{ sensor_form.unity_label(class="form-control") }}')[0]);
var $description = $($.parseHTML(
'{{ sensor_form.description(class="form-control") }}')[0]);
'{{ sensor_form.description(class="form-control") }}')[0]);
</script>
<script src="{{ url_for('static', filename='js/mapinsert.js') }}"></script>
<script src="{{ url_for('static', filename='js/mapinsert.js') }}"></script>
<script src="{{ url_for('static', filename='js/managesensors.js') }}"></script>
<script src="{{ url_for('static', filename='js/validateboard.js') }}"></script>
{% endblock %}
{% block content %}
......@@ -56,9 +56,9 @@
{{ board_form.csrf_token }}
<div class="form-group
{% if board_form.board_nickname.errors %}has-error{% endif %}">
{% if board_form.board_nickname.errors %}has-error{% endif %}">
{{ board_form.board_nickname.label(class="col-xs-3 control-label") }}
<div class="col-xs-5 selectContainer">
<div class="col-xs-5 selectContainer">
{{ board_form.board_nickname(class="form-control", autofocus=True) }}
<!-- <span class="glyphicon form-control-feedback" aria-hidden="true"></span> -->
<div class="help-block with-errors"></div>
......@@ -78,15 +78,15 @@
</div>
<div class="form-group
{% if board_form.board_exposition.errors %}has-error{% endif %}">
{% if board_form.board_exposition.errors %}has-error{% endif %}">
{{ board_form.board_exposition.label(class="col-xs-3 control-label") }}
<div class="col-xs-5 selectContainer">
{{ board_form.board_exposition(class="form-control", style="width: 30%") }}
<!-- <span class="glyphicon form-control-feedback" aria-hidden="true"></span> -->
<div class="help-block with-errors"></div>
<div class="help-block with-errors"></div>
{% if board_form.board_exposition.errors %}
<p class="help-block">{{ board_form.board_exposition.errors|join(', ') }}</p>
{% endif %}
{% endif %}
</div>
</div>
......@@ -97,16 +97,16 @@
{% if board_form.board_latitude.errors %}
<p class="help-block">
{{ board_form.board_latitude.errors|join(', ') }}</p>
{% endif %}
{% endif %}
<div id="map" class="map"></div>
<!-- <div id="popup" class="ol-popup"> -->
<!-- <a href="#" id="popup-closer" class="ol-popup-closer"></a> -->
<!-- <div id="popup-content"></div> -->
<!-- </div> -->
<div class="row">
<div class="col-xs-1">
<div class="row">
<div class="col-xs-1">
{{ board_form.board_longitude.label(class="control-label") }}
</div>
<div class="col-xs-5 ">
......@@ -116,12 +116,12 @@
{{ board_form.board_latitude.label(class="control-label") }}
</div>
<div class="col-xs-5 ">
{{ board_form.board_latitude(class="form-control") }}
</div>
{{ board_form.board_latitude(class="form-control") }}
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Sensores</h3>
......@@ -141,10 +141,10 @@
</tbody>
</table>
<div class="panel-body">
<button type="button" name="btn_add_sensor"
class="btn btn-default"
class="btn btn-default"
onclick="add_sensor_row()">
<span class="glyphicon glyphicon-plus" ></span>
</button>
......
This diff is collapsed.
......@@ -26,6 +26,7 @@ from app.utils import FakeLogger as fakelog
from app.models import *
from app.dbmanage import DBInitCommand
from flask import url_for
from flask.ext.script import Manager, Shell
from flask_migrate import Migrate, MigrateCommand
......@@ -45,9 +46,28 @@ for n in ('commit', 'rollback', 'flush'):
manager.add_command("shell", Shell(make_context=
lambda: {k: v for k, v in globals().items()}))
# @manager.command
# def list_routes():
# print(app.url_map)
@manager.command
def list_routes():
print(app.url_map)
def listroutes():
import urllib
output = []
for rule in app.url_map.iter_rules():
options = {}
for arg in rule.arguments:
options[arg] = "[{0}]".format(arg)
methods = ','.join(rule.methods)
# url = url_for(rule.endpoint, **options)
# line = urllib.parse.unquote("{:50s} {:20s} {}".format(rule.endpoint, methods, url))
line = urllib.parse.unquote("{:50s} {:30s} {:20s}".format(str(rule), methods, rule.endpoint))
output.append(line)
for line in sorted(output):
print(line)
if __name__ == '__main__':
manager.run()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment