Skip to content
Snippets Groups Projects
Commit da79bb7d authored by Mattia Mancini's avatar Mattia Mancini
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
README 0 → 100644
# PMT-VIEW
Web app do display live the PMT sensor
output.
To execute it run
```
bokeh serve pmt-view --show
```
A web browser will open with the live monitor of the Joules/Watts usage.
To configure the type of sensor to use
or the device number please edit `config.py` in `pmt-view`
File added
# Configuration file for PMT-VIEW
# Name of the sensor see pmt documentation for more details
SENSOR = "rapl"
# Device ID
DEVICE_ID = 0
from argparse import ArgumentParser
import pmt
import time
from bokeh.plotting import figure
from bokeh.io import show
from bokeh.embed import file_html
from bokeh.io import curdoc
from bokeh.models import ColumnDataSource
import config
def parse_args():
parser = ArgumentParser(description="PMT display sensors")
parser.add_argument("--sensor", default=config.SENSOR)
parser.add_argument("--device", default=config.DEVICE_ID, type=int)
return parser.parse_args()
def main(bokeh=False):
args = parse_args()
sensor = pmt.get_pmt(args.sensor, device_id=args.device)
disp = Display(sensor, args.sensor)
disp.draw()
disp.loop()
class Display:
def __init__(self, sensor, sensor_name):
self.figure = figure(width=900, height=500)
self.sensor_name = sensor_name
self.datasource = ColumnDataSource(
data={
"Joules": [0],
"Watts": [0],
"Average watts": [0],
"Average joules": [0],
"Seconds": [0],
}
)
self.sensor = sensor
self.state = None
self.cur_second = 0
def draw(self):
self.figure.line(
x="Seconds",
y="Joules",
color="red",
line_width=3.0,
source=self.datasource,
legend_label="Joules",
)
self.figure.line(
x="Seconds",
y="Watts",
color="blue",
line_width=3.0,
source=self.datasource,
legend_label="Watts",
)
self.figure.line(
x="Seconds",
y="Average watts",
color="lightblue",
line_width=3.0,
source=self.datasource,
legend_label="Average watts",
)
self.figure.line(
x="Seconds",
y="Average joules",
color="orange",
line_width=3.0,
source=self.datasource,
legend_label="Average joules",
)
def callback(self):
if self.state is None:
self.state = self.sensor.read()
time.sleep(1)
n_state = self.sensor.read()
self.cur_second += pmt.seconds(self.state, n_state)
row = {
"Joules": [
pmt.joules(self.state, n_state),
],
"Watts": [pmt.joules(self.state, n_state)],
"Average watts": [
pmt.joules(self.state, n_state) / pmt.seconds(self.state, n_state)
],
"Average joules": [
pmt.watts(self.state, n_state) / pmt.seconds(self.state, n_state)
],
"Seconds": [self.cur_second],
}
self.datasource.stream(row)
self.state = n_state
def loop(self):
curdoc().template_variables["sensor"] = self.sensor_name
curdoc().add_periodic_callback(lambda: self.callback(), 1000)
curdoc().add_root(self.figure)
if __name__ == "__main__":
main()
elif __name__.startswith("bokeh_app"):
main(bokeh=True)
{% extends base %}
{% block preamble %}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style>
.main {
background-color: darkmagenta;
border-radius: 1rem;
margin-top: .5rem;
text-shadow: .2rem .2rem gray;
font-weight: bold;
color: white;
font: arial;
}
.figures {
margin: auto
}
</style>
{% endblock %}
{% block contents %}
<div class="container-fluid">
<nav class="navbar main shadow-sm">
<div class="navbar-brand">
<h1>PMT - {{sensor}}</h2>
</div>
</nav>
{% for root in roots %}
<div class='card me-2 mt-2 shadow-sm'>
<div class="card-body figures">
{{ embed(root) }}
</div>
{% endfor %}
</div>
{% endblock %}
\ No newline at end of file
bokeh
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment