Skip to content

Commit

Permalink
Merge pull request #176 from jlab-sensing/hotfix-integration-and-demo
Browse files Browse the repository at this point in the history
Integrate sensor with backend for live demo
  • Loading branch information
jmadden173 authored Feb 24, 2024
2 parents 6664491 + a2aa715 commit cd13358
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 425 deletions.
2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ RUN chown -R app:app $APP_HOME
# change to the app user
USER app
WORKDIR /home/app/
ENTRYPOINT ["./backend/entrypoint.sh", "-p"]
ENTRYPOINT ["./backend/entrypoint.sh", "-p"]
12 changes: 4 additions & 8 deletions backend/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ def create_app(debug: bool = False) -> Flask:
from .resources.cell_id import Cell_Id
from .resources.power_data import Power_Data
from .resources.teros_data import Teros_Data
from .resources.power_data_protobuf import Power_Data_Protobuf
from .resources.teros_data_protobuf import Teros_Data_Protobuf
from .resources.sensor_data import Sensor_Data
from .resources.sensor_data_ttn import Measurement_Upink
from .resources.sensor_data_wifi import Measurement_Direct
from .resources.cell import Cell
from .resources.session import Session_r
from .resources.users_data import User_Data
Expand All @@ -61,11 +60,8 @@ def create_app(debug: bool = False) -> Flask:
api.add_resource(Cell_Id, "/cell/id")
api.add_resource(Power_Data, "/power/", "/power/<int:cell_id>")
api.add_resource(Teros_Data, "/teros/", "/teros/<int:cell_id>")
api.add_resource(
Power_Data_Protobuf, "/power-proto", "/power-proto/<int:sensor_id>"
)
api.add_resource(Teros_Data_Protobuf, "/teros-proto", "/teros-proto/<int:cell_id>")
api.add_resource(Sensor_Data, "/sensor", "/sensor/<int:sensor_id>")
api.add_resource(Measurement_Direct, "/sensor/")
api.add_resource(Measurement_Upink, "/ttn/")
api.add_resource(Session_r, "/session")
api.add_resource(User_Data, "/user")
app.register_blueprint(auth, url_prefix="/api")
Expand Down
136 changes: 0 additions & 136 deletions backend/api/database/getters.py

This file was deleted.

101 changes: 18 additions & 83 deletions backend/api/database/models/power_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,55 +65,8 @@ def add_protobuf_power_data(logger_id, cell_id, ts, v, i):
db.session.commit()
return power_data

def get_power_data(
cell_id,
resample="hour",
startTime=datetime.now() - relativedelta(months=1),
endTime=datetime.now(),
):
"""gets power data aggregated by attributes"""
data = []

resampled = (
db.select(
db.func.date_trunc(resample, PowerData.ts).label("ts"),
db.func.avg(PowerData.voltage).label("voltage"),
db.func.avg(PowerData.current).label("current"),
)
.where(PowerData.cell_id == cell_id)
# .filter((PowerData.ts > startTime) & (PowerData.ts < endTime))
.group_by(db.func.date_trunc(resample, PowerData.ts))
.subquery()
)

# adj_units = db.select(
# resampled.c.ts.label("ts"),
# (resampled.c.voltage * 1e3).label("voltage"),
# (resampled.c.current * 1e6).label("current"),
# ).subquery()

stmt = db.select(
resampled.c.ts.label("ts"),
(resampled.c.voltage * 1e3).label("voltage"),
(resampled.c.current * 1e6).label("current"),
(resampled.c.voltage * resampled.c.current * 1e6).label("power"),
).order_by(resampled.c.ts)

for row in db.session.execute(stmt):
data.append(
{
"ts": row.ts,
"v": row.voltage,
"i": row.current,
"p": row.power,
}
)

return data

def get_power_data_obj(
cell_id,
resample="hour",
start_time=datetime.now() - relativedelta(months=1),
end_time=datetime.now(),
stream=False,
Expand All @@ -125,45 +78,27 @@ def get_power_data_obj(
"i": [],
"p": [],
}
if not stream:
resampled = (
db.select(
db.func.date_trunc(resample, PowerData.ts).label("ts"),
db.func.avg(PowerData.voltage).label("voltage"),
db.func.avg(PowerData.current).label("current"),
)
.where((PowerData.cell_id == cell_id))
# .filter((PowerData.ts >= startTime, PowerData.ts <= endTime))
.filter((PowerData.ts.between(start_time, end_time)))
.group_by(db.func.date_trunc(resample, PowerData.ts))
.subquery()
)
else:
resampled = (
db.select(
PowerData.ts,
PowerData.voltage,
PowerData.current,
)
.where((PowerData.cell_id == cell_id))
.filter((PowerData.ts.between(start_time, end_time)))
.subquery()
)

# adj_units = db.select(
# resampled.c.ts.label("ts"),
# resampled.c.voltage.label("voltage"),
# resampled.c.current.label("current"),
# ).subquery()
stmt = (
db.select(
PowerData.ts.label("ts"),
PowerData.voltage.label("voltage"),
PowerData.current.label("current"),
)
.where(PowerData.cell_id == cell_id)
.filter((PowerData.ts.between(start_time, end_time)))
.subquery()
)

stmt = db.select(
resampled.c.ts.label("ts"),
(resampled.c.voltage * 1e3).label("voltage"),
(resampled.c.current * 1e6).label("current"),
(resampled.c.voltage * resampled.c.current * 1e6).label("power"),
).order_by(resampled.c.ts)
# expected units are mV, uA, and uW
adj_units = db.select(
stmt.c.ts.label("ts"),
stmt.c.voltage.label("voltage") * 1e-3,
stmt.c.current.label("current") * 1e-6,
(stmt.c.voltage * stmt.c.current * 1e-6).label("power")
).order_by(stmt.c.ts)

for row in db.session.execute(stmt):
for row in db.session.execute(adj_units):
data["timestamp"].append(row.ts)
data["v"].append(row.voltage)
data["i"].append(row.current)
Expand Down
72 changes: 19 additions & 53 deletions backend/api/database/models/teros_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def add_teros_data(cell_name, ts, vwc, raw_vwc, temp, ec, water_pot):
return teros_data

@staticmethod
def add_protobuf_power_data(cell_id, ts, vwc, raw_vwc, temp, ec, water_pot):
def add_protobuf_teros_data(cell_id, ts, vwc, raw_vwc, temp, ec, water_pot):
cur_cell = Cell.query.filter_by(id=cell_id).first()
if cur_cell is None:
return None
Expand All @@ -64,66 +64,32 @@ def add_protobuf_power_data(cell_id, ts, vwc, raw_vwc, temp, ec, water_pot):
db.session.commit()
return teros_data

def get_teros_data(cell_id, resample="hour"):
"""gets teros data aggregated by attributes"""
data = []

stmt = (
db.select(
func.date_trunc(resample, TEROSData.ts).label("ts"),
func.avg(TEROSData.vwc).label("vwc"),
func.avg(TEROSData.temp).label("temp"),
func.avg(TEROSData.ec).label("ec"),
)
.where(TEROSData.cell_id == cell_id)
.group_by(func.date_trunc(resample, TEROSData.ts))
.order_by(func.date_trunc(resample, TEROSData.ts))
)

for row in db.session.execute(stmt):
data.append(
{
"ts": row.ts,
"vwc": row.vwc,
"temp": row.temp,
"ec": row.ec,
}
)
return data

def get_teros_data_obj(
cell_id,
resample="hour",
start_time=datetime.now() - relativedelta(months=1),
end_time=datetime.now(),
stream=False,
):
"""gets teros data as a list of objects"""
data = {"timestamp": [], "vwc": [], "temp": [], "ec": []}
if not stream:
stmt = (
db.select(
func.date_trunc(resample, TEROSData.ts).label("ts"),
func.avg(TEROSData.vwc).label("vwc"),
func.avg(TEROSData.temp).label("temp"),
func.avg(TEROSData.ec).label("ec"),
)
.where(TEROSData.cell_id == cell_id)
.filter((TEROSData.ts.between(start_time, end_time)))
.group_by(func.date_trunc(resample, TEROSData.ts))
.order_by(func.date_trunc(resample, TEROSData.ts))
)
else:
stmt = (
db.select(
TEROSData.ts,
TEROSData.vwc,
TEROSData.temp,
TEROSData.ec,
)
.where(TEROSData.cell_id == cell_id)
.filter((TEROSData.ts.between(start_time, end_time)))
data = {
"timestamp": [],
"vwc": [],
"temp": [],
"ec": []
}

# VWC stored in decimal, converted to percentage
stmt = (
db.select(
TEROSData.ts.label("ts"),
(TEROSData.vwc * 100).label("vwc"),
TEROSData.temp.label("temp"),
TEROSData.ec.label("ec"),
)
.where(TEROSData.cell_id == cell_id)
.filter((TEROSData.ts.between(start_time, end_time)))
.order_by(TEROSData.ts)
)

for row in db.session.execute(stmt):
data["timestamp"].append(row.ts)
Expand Down
Loading

0 comments on commit cd13358

Please sign in to comment.