diff --git a/daq_sample/sampleData.py b/daq_sample/sampleData.py index 1df38fb..51fe2ab 100644 --- a/daq_sample/sampleData.py +++ b/daq_sample/sampleData.py @@ -15,7 +15,7 @@ import requests import json # DEFAULTS -db_address = "localhost:5000" +db_address = "10.10.10.10:5000" db_url = "https://{}".format(db_address) scan_rate = 30 # seconds save_all = "test" # use True, False, or any string diff --git a/web_db/Dockerfile.rpi b/web_db/Dockerfile.rpi index 484d9a8..95c6a6e 100644 --- a/web_db/Dockerfile.rpi +++ b/web_db/Dockerfile.rpi @@ -1,23 +1,23 @@ -FROM hypriot/rpi-node:latest +FROM patrickjmcd/rpi-python3:latest -RUN apt-get -y update && apt-get install -y apt-utils dialog vim git +RUN apt-get -y update COPY mysql-install.sh /tmp/mysql-install.sh -COPY taglogger_db_structure.sql /tmp/taglogger_db_structure.sql RUN chmod +x /tmp/mysql-install.sh && /tmp/mysql-install.sh RUN mkdir /root/tag-logger -COPY www /root/tag-logger/www +COPY flask /root/tag-logger/flask + +COPY mysql-connector-python-2.1.4 /tmp/mysql +RUN cd /tmp/mysql && python setup.py install && cd ~ COPY startup.sh /root/startup.sh RUN chmod +x /root/startup.sh -RUN npm install -g bower pm2 sails --silent -RUN cd /root/tag-logger/www && npm install && bower install --allow-root && cd / +RUN pip install flask flask-restless flask-sqlalchemy pyopenssl RUN apt-get clean RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* +RUN service mysql restart && python /root/tag-logger/flask/setupdb.py + CMD '/root/startup.sh' - - - diff --git a/web_db/Dockerfile.ubuntu b/web_db/Dockerfile.ubuntu index 2e5927d..56dfefb 100644 --- a/web_db/Dockerfile.ubuntu +++ b/web_db/Dockerfile.ubuntu @@ -1,23 +1,23 @@ -FROM node:latest +FROM python:latest -RUN apt-get -y update && apt-get install -y apt-utils dialog vim git +RUN apt-get -y update COPY mysql-install.sh /tmp/mysql-install.sh -COPY taglogger_db_structure.sql /tmp/taglogger_db_structure.sql RUN chmod +x /tmp/mysql-install.sh && /tmp/mysql-install.sh RUN mkdir /root/tag-logger -COPY www /root/tag-logger/www +COPY flask /root/tag-logger/flask + +COPY mysql-connector-python-2.1.4 /tmp/mysql +RUN cd /tmp/mysql && python setup.py install && cd ~ COPY startup.sh /root/startup.sh RUN chmod +x /root/startup.sh -RUN npm install -g bower pm2 sails --silent -RUN cd /root/tag-logger/www && npm install && bower install --allow-root && cd / +RUN pip install flask flask-restless flask-sqlalchemy pyopenssl RUN apt-get clean RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* +RUN service mysql restart && python /root/tag-logger/flask/setupdb.py + CMD '/root/startup.sh' - - - diff --git a/web_db/flask/app/__init__.py b/web_db/flask/app/__init__.py index 7b480a6..7ee9a50 100644 --- a/web_db/flask/app/__init__.py +++ b/web_db/flask/app/__init__.py @@ -1,17 +1,31 @@ # project/__init__.py -from flask import Flask, render_template, request, session, send_from_directory, jsonify +import os +from flask import Flask, render_template, request, session, send_from_directory, jsonify, url_for, flash, redirect, Response from flask_sqlalchemy import SQLAlchemy +from werkzeug.utils import secure_filename from sqlalchemy import and_ +import mysql.connector +UPLOAD_FOLDER = '/Users/patrickjmcd/Henry_Pump/tagserver/web_db/flask/app/docs' +ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']) app = Flask('app', static_url_path='') app.config.update( - DEBUG=True, - SQLALCHEMY_DATABASE_URI='sqlite:///../database.db', - ) + DEBUG=True, + SQLALCHEMY_DATABASE_URI='mysql+mysqlconnector://website:henrypump@127.0.0.1/poconsole' + # SQLALCHEMY_DATABASE_URI='sqlite:///../database.db', +) +app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER +app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 +app.secret_key = 'henry_pump' db = SQLAlchemy(app) +def allowed_file(filename): + return '.' in filename and \ + filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS + + @app.route('/', defaults={'path': ''}) @app.route('/') def catch_all(path): @@ -22,7 +36,7 @@ from .datalogger.models import * @app.route('/api/latest') def get_latest_tag_vals(): - res = db.engine.execute('SELECT v1.id as id, v1.created_on as dtime, t.id as t_id, t.name as name, t.tag as tag, v1.value as value, t.units as units, t.description as description, t.min_expected as min_expected, t.max_expected as max_expected FROM tag_vals v1 INNER JOIN tags t ON t.id = v1.tag_id WHERE v1.id = (SELECT v2.id FROM tag_vals v2 WHERE v2.tag_id = v1.tag_id ORDER BY v2.id DESC LIMIT 1) ORDER BY t.id') + res = db.engine.execute('SELECT v1.id as id, v1.created_on as dtime, t.id as t_id, t.name as name, t.tag as tag, v1.value as value, t.units as units, t.description as description, t.min_expected as min_expected, t.max_expected as max_expected FROM tag_vals v1 INNER JOIN tags t ON t.id = v1.tag_id WHERE v1.id = (SELECT v2.id FROM tag_vals v2 WHERE v2.tag_id = v1.tag_id ORDER BY v2.id DESC LIMIT 1) ORDER BY t.id') lat = res.fetchall() latest_tags = list(map(latest_to_obj, lat)) return jsonify(latest_tags) @@ -39,4 +53,71 @@ def get_tag_vals_between(ids, start, end): def get_multiple_tags(ids): ids = ids.split(',') res = Tag.query.filter(Tag.id.in_(ids)).all() - return jsonify([i.serialize for i in res]) \ No newline at end of file + return jsonify([i.serialize for i in res]) + +@app.route('/doc/upload', methods=['POST']) +def upload_file(): + # check if the post request has the file part + if 'file' not in request.files: + flash('No file part') + return redirect("/#/docs") + file = request.files['file'] + # if user does not select file, browser also + # submit a empty part without filename + if file.filename == '': + flash('No selected file') + return redirect("/#/docs") + if file and allowed_file(file.filename): + filename = secure_filename(file.filename) + file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) + d = Doc(name=filename) + db.session.add(d) + db.session.commit() + return redirect(url_for('uploaded_file', + filename=filename)) + return redirect("/#/docs") + +@app.route('/docs/') +def uploaded_file(filename): + return send_from_directory(app.config['UPLOAD_FOLDER'], + filename) + +@app.route('/csv/all') +def get_csv_all(): + csv_string = "datetime," + all_tags = [i.serialize for i in Tag.query.all()] + all_tag_names = [x['name'] for x in all_tags] + for x in all_tag_names: + csv_string += "{},".format(x) + csv_string += "\n" + + all_vals = [i.serialize for i in Tag_val.query.all()] + val_objs = [{'value': x['value'], 'tag_name': x['tag']['name'], 'datetime': x['created_on']} for x in all_vals] + for v in val_objs: + tag_ind = all_tag_names.index(v['tag_name']) + csv_string += "{},".format(v['datetime']) + "," * tag_ind + "{},".format(v['value']) + "," * (len(all_tag_names) - tag_ind) + "\n" + return Response( + csv_string, + mimetype="text/csv", + headers={"Content-disposition": + "attachment; filename=datadump.csv"}) + +@app.route('/csv/') +def get_csv_selected(ids): + csv_string = "datetime," + all_tags = [i.serialize for i in Tag.query.filter(Tag.id.in_(ids)).all()] + all_tag_names = [x['name'] for x in all_tags] + for x in all_tag_names: + csv_string += "{},".format(x) + csv_string += "\n" + + all_vals = [i.serialize for i in Tag_val.query.filter(Tag_val.tag_id.in_(ids)).all()] + val_objs = [{'value': x['value'], 'tag_name': x['tag']['name'], 'datetime': x['created_on']} for x in all_vals] + for v in val_objs: + tag_ind = all_tag_names.index(v['tag_name']) + csv_string += "{},".format(v['datetime']) + "," * tag_ind + "{},".format(v['value']) + "," * (len(all_tag_names) - tag_ind) + "\n" + return Response( + csv_string, + mimetype="text/csv", + headers={"Content-disposition": + "attachment; filename=datadump{}.csv".format(ids.replace(",","-"))}) diff --git a/web_db/flask/app/datalogger/models.py b/web_db/flask/app/datalogger/models.py index 9265a06..7b784ae 100644 --- a/web_db/flask/app/datalogger/models.py +++ b/web_db/flask/app/datalogger/models.py @@ -82,9 +82,7 @@ class Device(db.Model): class Doc(db.Model): __tablename__ = "docs" id = db.Column(db.Integer, primary_key=True) - name = db.Column(db.String(128)) - location = db.Column(db.String(256)) - description = db.Column(db.String(256)) + name = db.Column(db.String(256)) created_on = db.Column(db.DateTime(), default=datetime.utcnow) updated_on = db.Column(db.DateTime(), default=datetime.utcnow, onupdate=datetime.utcnow) diff --git a/web_db/flask/app/docs/LL_Cool_J_by_Cambria_Harkey_12263.jpg b/web_db/flask/app/docs/LL_Cool_J_by_Cambria_Harkey_12263.jpg new file mode 100644 index 0000000..754057d Binary files /dev/null and b/web_db/flask/app/docs/LL_Cool_J_by_Cambria_Harkey_12263.jpg differ diff --git a/web_db/flask/app/static/index.html b/web_db/flask/app/static/index.html index 4f8f73f..6d22eba 100644 --- a/web_db/flask/app/static/index.html +++ b/web_db/flask/app/static/index.html @@ -11,10 +11,10 @@ - +