add write to databasse and some api

This commit is contained in:
2024-05-25 16:11:10 +03:00
commit 082b1bc9d2
8 changed files with 125 additions and 0 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

0
README.MD Normal file
View File

43
app.py Normal file
View File

@@ -0,0 +1,43 @@
from flask import Flask, render_template, request, redirect, url_for
from flask_migrate import Migrate
from models import db, Data
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///asuserver.db"
app.jinja_env.auto_reload = True
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.secret_key = '6523e58bc0eec42c31b9635d5e0dfc23b6d119b73e633bf3a5284c79bb4a1ede'
db.init_app(app)
migrate = Migrate(app, db)
def createDb():
with app.app_context():
db.create_all()
@app.route("/")
def index():
return "12312312"
@app.route("/api/getaverageperminute")
def getaverageperhour():
query = db.session.query(Data).order_by(Data.addTime.desc()).limit(6).all()
print(query)
temp1Data = []
temp2Data = []
hum1Data = []
hum2Data = []
for data in query:
temp1Data.append(data.temp1)
temp2Data.append(data.temp2)
hum1Data.append(data.hum1)
hum2 , Data.append(data.hum1)
return str("last_200_records_sorted")
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port='3800')

48
getDataFromArduino.py Normal file
View File

@@ -0,0 +1,48 @@
import serial
from datetime import datetime
import json
from models import Data, db
from app import app
# Open the COM port
ser = serial.Serial('COM14', 9600, timeout=1)
# Check if the port is open
if ser.is_open:
print(f"Connected to {ser.name}")
try:
while True:
# Read data from the COM port
if ser.in_waiting > 0:
data = ser.readline().decode('utf-8').rstrip()
parsed = json.loads(data)
rawDataTime = parsed["dataTime"]
parsedDataTime = str(rawDataTime).split("-")
#datetime(2012, 3, 3, 10, 10, 10)
dataTime =datetime(int(parsedDataTime[0]),
int(parsedDataTime[1]),
int(parsedDataTime[2]),
int(parsedDataTime[3]),
int(parsedDataTime[4]),
int(parsedDataTime[5]),
)
with app.app_context():
dt = db.session.add(Data(temp1=float(parsed["temp1"]),
vlaz1=float(parsed["hum1"]),
temp2=float(parsed["temp2"]),
vlaz2=float(parsed["hum2"]),
addTime=dataTime))
db.session.commit()
except KeyboardInterrupt:
print("Exiting...")
finally:
# Close the COM port
ser.close()
print("Serial port closed")

13
models.py Normal file
View File

@@ -0,0 +1,13 @@
from flask_sqlalchemy import SQLAlchemy
import datetime
db = SQLAlchemy()
class Data(db.Model):
id = db.Column(db.Integer, primary_key=True)
temp1 = db.Column(db.Float, nullable=False)
vlaz1 = db.Column(db.Float, nullable=False)
temp2 = db.Column(db.Float, nullable=False)
vlaz2 = db.Column(db.Float, nullable=False)
addTime = db.Column(db.DateTime, nullable=False)

2
static/js/jquery-3.7.1.min.js vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

10
templates/index.html Normal file
View File

@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Метостанция </title>
</head>
<body>
</body>
</html>