Skip to content

Commit

Permalink
added common chart, light chart, current thermostat state
Browse files Browse the repository at this point in the history
  • Loading branch information
Korzhak committed Apr 22, 2019
1 parent 6e331af commit c914bf3
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 65 deletions.
7 changes: 7 additions & 0 deletions static/css/custom.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.light-color {
padding-left: 14px;
border: 1px solid #999;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}
259 changes: 204 additions & 55 deletions templates/ts/index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,116 @@
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
{# <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.css">#}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="{% static "css/custom.css" %}">
</head>
<body>

<div class="container">
<div class="jumbotron mt-3">
<h4>Останнє оновлення</h4>
<hr>
<li>Стан термостату:
{% if last_record.thermostat_state %}
<span class="badge badge-success">Увімкнено</span>
{% else %}
<span class="badge badge-secondary">Вимкнено</span>
{% endif %}
</li>

<li>Виконувана робота:
{% if last_record.current_state == "1" %}
<span class="badge badge-danger">Нагрівання</span>
{% elif last_record.current_state == "2" %}
<span class="badge badge-primary">Охолодження</span>
{% else %}
<span class="badge badge-secondary">Не працює</span>
{% endif %}
</li>
<hr>
<li>Температура зараз:
<span class="badge badge-info">{{ last_record.temp }}°C</span>
</li>
<li>Встановлена температура:
<span class="badge badge-info">{{ last_record.set_temp }}°C</span>
</li>
<hr>
<li>CO2 зараз:
<span class="badge badge-dark">{{ last_record.co2 }}</span>
</li>
<li>Встановлений рівень CO2:
<span class="badge badge-dark">{{ last_record.set_co2 }}</span>
</li>
<hr>
<li>Освітленість:
{% if last_record.light %}
<span class="badge badge-warning">Увімкнено</span>
{% else %}
<span class="badge badge-secondary">Вимкнено</span>
{% endif %}
</li>
<li>Колір встановленого освітлення:
<span class="light-color"
style="background-color: rgb({{ last_record.light_R }}, {{ last_record.light_G }}, {{ last_record.light_B }})"
>&nbsp;</span>
</li>

{# <h1>Bottom Navbar example</h1>#}
{# <p class="lead">This example is a quick exercise to illustrate how the bottom navbar works.</p>#}
{# <a class="btn btn-lg btn-primary" href="/docs/4.3/components/navbar/" role="button">View navbar docs »</a>#}
</div>
</div>

<div class="container">
<div class="col-md-12">
<div><h3>Загальний графік</h3></div>
<canvas id="temperatureChart"></canvas>
</div>
<div class="col-md-6">
<canvas id="co2Chart"></canvas>
</div>
<div class="col-md-6">
<div><h3>Освітленість</h3></div>
<canvas id="lightChart"></canvas>
</div>

</div>


<nav class="navbar fixed-bottom navbar-expand-sm navbar-dark bg-dark">
<a class="navbar-brand" href="#">FreakSpace</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse"
aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
<li class="nav-item dropup">
<a class="nav-link dropdown-toggle" href="#" id="dropdown10" data-toggle="dropdown" aria-haspopup="true"
aria-expanded="false">Dropup</a>
<div class="dropdown-menu" aria-labelledby="dropdown10">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
</div>
</nav>

<canvas id="myChart"></canvas>

{# {% for item in object_list %}#}
{# <li>{{ item.thermostat_state }}</li>#}
Expand All @@ -25,68 +126,116 @@
{# <li>{{ item.time }}</li>#}
{# {% endfor %}#}
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script>
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'line',

// The data for our dataset
data: {
datasets: [{
label: 'Температура по-факту',
{#backgroundColor: 'rgb(255, 99, 132)',#}
borderColor: 'rgb(255, 99, 132)',
data: [
{% for item in qset %}
{{ item.temp }},
{% endfor %}
],
{#yAxisID: 'left-y-axis'#}
}, {
label: 'Встановлена температура',
<script>
var temperatureElement = document.getElementById('temperatureChart').getContext('2d');
var temp_chart = new Chart(temperatureElement, {
// The type of chart we want to create
type: 'line',

// The data for our dataset
data: {
datasets: [{
label: 'Температура по-факту',
{#backgroundColor: 'rgb(255, 99, 132)',#}
borderColor: 'rgb(255, 99, 132)',
data: [
{% for item in qset %}
{{ item.temp }},
{% endfor %}
],
{#yAxisID: 'left-y-axis'#}
}, {
label: 'Встановлена температура',
{#backgroundColor: 'rgb(255, 99, 132)',#}
borderColor: 'rgb(111, 34, 132)',
data: [
{% for item in qset %}
{{ item.set_temp }},
{% endfor %}
],
{#yAxisID: 'right-y-axis'#}
},
{
label: 'Стан освітленості',
{#backgroundColor: 'rgb(255, 99, 132)',#}
borderColor: 'rgb(111, 34, 132)',
borderColor: 'rgb(13, 44, 99)',
data: [
{% for item in qset %}
{{ item.set_temp }},
{% if item.light %}
1,
{% else %}
0,
{% endif %}
{% endfor %}
],
{#yAxisID: 'right-y-axis'#}
yAxisID: 'right-y-axis'
}
],
labels: [
{% for item in qset %}
"{{ item.time }}",
{% endfor %}
]
},
// Configuration options go here
options: {
scales: {
yAxes: [{
id: 'left-y-axis',
type: 'linear',
position: 'left'
}, {
label: 'Освітленість',
{#backgroundColor: 'rgb(255, 99, 132)',#}
borderColor: 'rgb(11, 255, 12)',
data: [
{% for item in qset %}
{{ item.light.to_bytes }},
{% endfor %}
],
{#yAxisID: 'right-y-axis'#}
}],
labels: [
id: 'right-y-axis',
type: 'linear',
position: 'right'
}]
}
}
});

</script>
<script>

var lightElement = document.getElementById('lightChart').getContext('2d');
var light_chart = new Chart(lightElement, {
// The type of chart we want to create
type: 'line',

// The data for our dataset
data: {
datasets: [{
label: 'Стан освітленості',
{#backgroundColor: 'rgb(255, 99, 132)',#}
borderColor: 'rgb(13, 44, 99)',
data: [
{% for item in qset %}
"{{ item.time }}",
"{{ item.light.to_bytes }}",
{% endfor %}
]
},
],
{#yAxisID: 'left-y-axis'#}
}],
labels: [
{% for item in qset %}
"{{ item.time }}",
{% endfor %}
]
},

// Configuration options go here
options: {
scales: {
yAxes: [{
id: 'left-y-axis',
type: 'linear',
position: 'left'
}, {
id: 'right-y-axis',
type: 'linear',
position: 'right'
}]
}
}
});
</script>
// Configuration options go here
options: {}
});
</script>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>


{# <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.js" type="text/javascript"></script>#}
Expand Down
7 changes: 5 additions & 2 deletions thermostat_web/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,17 @@

USE_L10N = True

USE_TZ = False
USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)

CRONJOBS = [
('*/1 * * * *', 'arduino.serial_read.get_data_from_box', '>> /tmp/schedule_job.log')
]
22 changes: 14 additions & 8 deletions ts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ class DataListView(ListView):
context_object_name = "qset"
model = LogThermostat
template_name = 'ts/index.html'
queryset = LogThermostat.objects.all()

def get_context_data(self, *, object_list=None, **kwargs):
data = super().get_context_data(**kwargs)
data['last_record'] = self.queryset.last()
return data

def get_queryset(self):
start_date_text = self.request.GET.get("start_date")
Expand All @@ -29,12 +35,12 @@ def get_queryset(self):
start_date = datetime.today() - timedelta(days=1)
end_date = datetime.now()

data = LogThermostat.objects.filter(time__range=(start_date, end_date))
if len(data) >= 30:
step = len(data) // 30
buff = 0
while buff <= len(data):
if buff % step:
data.e
return
data = self.queryset.filter(time__range=(start_date, end_date))
# if len(data) >= 30:
# step = len(data) // 30
# buff = 0
# while buff <= len(data):
# if buff % step:
# data.e
return data

0 comments on commit c914bf3

Please sign in to comment.