Skip to content

Commit 9fc64c4

Browse files
Aflezen gegevens over voltages #518
1 parent a5a8e29 commit 9fc64c4

File tree

10 files changed

+247
-5
lines changed

10 files changed

+247
-5
lines changed

dsmr_backend/services/backend.py

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ def get_capabilities(capability=None):
4040
phase_currently_delivered_l2__isnull=False,
4141
phase_currently_delivered_l3__isnull=False
4242
).exists(),
43+
'voltage': ElectricityConsumption.objects.filter(
44+
phase_voltage_l1__isnull=False,
45+
).exists(),
4346
'gas': GasConsumption.objects.exists(),
4447
'weather': WeatherSettings.get_solo().track and TemperatureReading.objects.exists()
4548
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Generated by Django 2.2.5 on 2019-09-26 17:00
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('dsmr_consumption', '0011_track_phase_voltages'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='electricityconsumption',
15+
name='phase_voltage_l1',
16+
field=models.DecimalField(db_index=True, decimal_places=1, default=None, help_text='Current voltage for phase L1 (in V)', max_digits=4, null=True),
17+
),
18+
migrations.AlterField(
19+
model_name='electricityconsumption',
20+
name='phase_voltage_l2',
21+
field=models.DecimalField(db_index=True, decimal_places=1, default=None, help_text='Current voltage for phase L2 (in V)', max_digits=4, null=True),
22+
),
23+
migrations.AlterField(
24+
model_name='electricityconsumption',
25+
name='phase_voltage_l3',
26+
field=models.DecimalField(db_index=True, decimal_places=1, default=None, help_text='Current voltage for phase L3 (in V)', max_digits=4, null=True),
27+
),
28+
]

dsmr_consumption/models/consumption.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,24 @@ class ElectricityConsumption(models.Model):
8787
default=None,
8888
max_digits=4,
8989
decimal_places=1,
90-
help_text=_("Current voltage for phase L1 (in V)")
90+
help_text=_("Current voltage for phase L1 (in V)"),
91+
db_index=True
9192
)
9293
phase_voltage_l2 = models.DecimalField(
9394
null=True,
9495
default=None,
9596
max_digits=4,
9697
decimal_places=1,
97-
help_text=_("Current voltage for phase L2 (in V)")
98+
help_text=_("Current voltage for phase L2 (in V)"),
99+
db_index=True
98100
)
99101
phase_voltage_l3 = models.DecimalField(
100102
null=True,
101103
default=None,
102104
max_digits=4,
103105
decimal_places=1,
104-
help_text=_("Current voltage for phase L3 (in V)")
106+
help_text=_("Current voltage for phase L3 (in V)"),
107+
db_index=True
105108
)
106109

107110
def __sub__(self, other):

dsmr_frontend/forms.py

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class DashboardElectricityConsumptionForm(forms.Form):
2828
delivered = forms.BooleanField(required=False, initial=False)
2929
returned = forms.BooleanField(required=False, initial=False)
3030
phases = forms.BooleanField(required=False, initial=False)
31+
voltage = forms.BooleanField(required=False, initial=False)
3132
latest_delta_id = forms.IntegerField(required=False, initial=None)
3233

3334
def __init__(self, *args, **kwargs):
@@ -51,6 +52,9 @@ def clean_returned(self):
5152
def clean_phases(self):
5253
return self._clean_type('phases', 'multi_phases')
5354

55+
def clean_voltage(self):
56+
return self._clean_type('voltage', 'voltage')
57+
5458

5559
class DashboardNotificationReadForm(forms.Form):
5660
notification_id = forms.IntegerField()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
$(document).ready(function(){
2+
3+
var echarts_voltage_graph = echarts.init(document.getElementById('echarts-voltage-graph'));
4+
var echarts_voltage_initial_options = {
5+
color: [
6+
voltage_l1_color,
7+
voltage_l2_color,
8+
voltage_l3_color
9+
],
10+
tooltip : {
11+
trigger: 'axis',
12+
axisPointer: {
13+
type: 'shadow',
14+
label: {
15+
show: true
16+
}
17+
}
18+
},
19+
calculable : true,
20+
grid: {
21+
top: '12%',
22+
left: '1%',
23+
right: '2%',
24+
containLabel: true
25+
},
26+
xAxis: [
27+
{
28+
type : 'category',
29+
boundaryGap: false,
30+
data : null
31+
}
32+
],
33+
yAxis: [
34+
{
35+
type : 'value'
36+
}
37+
],
38+
dataZoom: [
39+
{
40+
show: true,
41+
start: 0,
42+
end: 100
43+
},
44+
{
45+
type: 'inside',
46+
start: 0,
47+
end: 100
48+
}
49+
],
50+
};
51+
52+
/* These settings should not affect the updates and reset the zoom on each update. */
53+
var echarts_voltage_update_options = {
54+
xAxis: [
55+
{
56+
type : 'category',
57+
boundaryGap: false,
58+
data : null
59+
}
60+
],
61+
series : [
62+
{
63+
smooth: true,
64+
name: 'Volt (L1)',
65+
type: 'line',
66+
data: null
67+
},
68+
{
69+
smooth: true,
70+
name: 'Volt (L2)',
71+
type: 'line',
72+
data: null
73+
},
74+
{
75+
smooth: true,
76+
name: 'Volt (L3)',
77+
type: 'line',
78+
data: null
79+
},
80+
]
81+
};
82+
83+
echarts_voltage_graph.showLoading('default', echarts_loading_options);
84+
85+
/* Init graph. */
86+
$.get(echarts_voltage_graph_url, function (xhr_data) {
87+
echarts_voltage_graph.hideLoading();
88+
89+
/* Adjust default zooming to the number of default items we want to display. */
90+
var zoom_percent = 100 - (dashboard_graph_width / xhr_data.read_at.length * 100);
91+
echarts_voltage_initial_options.dataZoom[0].start = zoom_percent;
92+
echarts_voltage_graph.setOption(echarts_voltage_initial_options);
93+
94+
/* Different set of options, to prevent the dataZoom being reset on each update. */
95+
echarts_voltage_update_options.xAxis[0].data = xhr_data.read_at;
96+
echarts_voltage_update_options.series[0].data = xhr_data.phase_voltage.l1;
97+
echarts_voltage_update_options.series[1].data = xhr_data.phase_voltage.l2;
98+
echarts_voltage_update_options.series[2].data = xhr_data.phase_voltage.l3;
99+
echarts_voltage_graph.setOption(echarts_voltage_update_options);
100+
101+
var latest_delta_id = xhr_data.latest_delta_id;
102+
103+
/* Update graph data from now on. */
104+
setInterval(function () {
105+
$.get(echarts_voltage_graph_url + "&latest_delta_id=" + latest_delta_id, function (xhr_data) {
106+
/* Ignore empty sets. */
107+
if (xhr_data.read_at.length == 0)
108+
{
109+
return;
110+
}
111+
112+
/* Delta update. */
113+
for (var i = 0 ; i < xhr_data.read_at.length ; i++)
114+
{
115+
echarts_voltage_update_options.xAxis[0].data.push(xhr_data.read_at[i]);
116+
echarts_voltage_update_options.series[0].data.push(xhr_data.phase_voltage.l1[i]);
117+
echarts_voltage_update_options.series[1].data.push(xhr_data.phase_voltage.l2[i]);
118+
echarts_voltage_update_options.series[2].data.push(xhr_data.phase_voltage.l3[i]);
119+
}
120+
121+
latest_delta_id = xhr_data.latest_delta_id;
122+
echarts_voltage_graph.setOption(echarts_voltage_update_options);
123+
});
124+
}, echarts_voltage_graph_interval * 1000);
125+
});
126+
127+
/* Responsiveness. */
128+
$(window).resize(function() {
129+
echarts_voltage_graph.resize();
130+
});
131+
});

dsmr_frontend/templates/dsmr_frontend/dashboard.html

+28
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,22 @@
177177
</div>
178178
{% endif %}
179179

180+
{% if capabilities.voltage %}
181+
<div class="row" id="voltage_holder">
182+
<div class="col-md-12">
183+
<div class="panel">
184+
<header class="panel-heading">
185+
{% trans "Phase voltages in the past 24 hours" %}
186+
</header>
187+
<div class="panel-body">
188+
<small>{% blocktrans %}Scroll or pinch the graph to zoom in and out. Drag the graph left or right, or use the horizontal scrollbar below the graph.{% endblocktrans %}</small>
189+
<div id="echarts-voltage-graph" style="width: 100%; height: 300px;"></div>
190+
</div>
191+
</div>
192+
</div>
193+
</div>
194+
{% endif %}
195+
180196
{% if capabilities.gas %}
181197
<div class="row">
182198
<div class="col-md-12">
@@ -255,6 +271,18 @@
255271
<script type="text/javascript" src="{% static 'dsmr_frontend/js/dsmr-reader/dashboard/phases.js' %}?r=v{{ dsmr_version }}"></script>
256272
{% endif %}
257273

274+
{% if capabilities.voltage %}
275+
<script type="text/javascript">
276+
var echarts_voltage_graph_url = "{% url 'frontend:dashboard-xhr-electricity' %}?voltage=True";
277+
var voltage_l1_color = "{{ frontend_settings.phase_delivered_l1_color }}";
278+
var voltage_l2_color = "{{ frontend_settings.phase_delivered_l2_color }}";
279+
var voltage_l3_color = "{{ frontend_settings.phase_delivered_l3_color }}";
280+
281+
var echarts_voltage_graph_interval = 5;
282+
</script>
283+
<script type="text/javascript" src="{% static 'dsmr_frontend/js/dsmr-reader/dashboard/voltage.js' %}?r=v{{ dsmr_version }}"></script>
284+
{% endif %}
285+
258286
{% if capabilities.gas %}
259287
<script type="text/javascript">
260288
var echarts_gas_graph_url = "{% url 'frontend:dashboard-xhr-gas' %}";

dsmr_frontend/tests/webinterface/test_dashboard.py

+34-2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def test_dashboard_xhr_electricity(self, now_mock):
123123
phase_currently_returned_l1=2.5,
124124
phase_currently_returned_l2=2.25,
125125
phase_currently_returned_l3=2.75,
126+
phase_voltage_l1=230,
126127
)
127128
ElectricityConsumption.objects.create(
128129
read_at=timezone.now() - timezone.timedelta(hours=1),
@@ -138,6 +139,7 @@ def test_dashboard_xhr_electricity(self, now_mock):
138139
phase_currently_returned_l1=3.5,
139140
phase_currently_returned_l2=3.25,
140141
phase_currently_returned_l3=3.75,
142+
phase_voltage_l1=230,
141143
)
142144

143145
response = self.client.get(
@@ -168,7 +170,8 @@ def test_dashboard_xhr_electricity(self, now_mock):
168170
'l1': [3500.0, 2500.0],
169171
'l2': [3250.0, 2250.0],
170172
'l3': [3750.0, 2750.0],
171-
}
173+
},
174+
'phase_voltage': {'l1': [], 'l2': [], 'l3': []},
172175
}
173176
)
174177

@@ -249,7 +252,36 @@ def test_dashboard_xhr_electricity(self, now_mock):
249252
'l1': [3500.0],
250253
'l2': [3250.0],
251254
'l3': [3750.0],
252-
}
255+
},
256+
'phase_voltage': {'l1': [], 'l2': [], 'l3': []},
257+
}
258+
)
259+
260+
# Voltages
261+
response = self.client.get(
262+
reverse('{}:dashboard-xhr-electricity'.format(self.namespace)),
263+
data={'voltage': True, 'latest_delta_id': old_latest_delta_id}
264+
)
265+
self.assertEqual(response.status_code, 200, response.content)
266+
json_content = json.loads(response.content.decode("utf8"))
267+
self.assertEqual(
268+
json_content,
269+
{
270+
'latest_delta_id': json_content['latest_delta_id'], # Not hardcoded due to DB backend differences.
271+
'read_at': ['Sat 23:00'],
272+
'currently_delivered': [],
273+
'currently_returned': [],
274+
'phases_delivered': {
275+
'l1': [],
276+
'l2': [],
277+
'l3': [],
278+
},
279+
'phases_returned': {
280+
'l1': [],
281+
'l2': [],
282+
'l3': [],
283+
},
284+
'phase_voltage': {'l1': [230], 'l2': [0.0], 'l3': [0.0]},
253285
}
254286
)
255287

dsmr_frontend/views/dashboard.py

+10
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ def get(self, request): # noqa: C901
9191
'l2': [],
9292
'l3': [],
9393
},
94+
'phase_voltage': {
95+
'l1': [],
96+
'l2': [],
97+
'l3': [],
98+
},
9499
}
95100

96101
# Optional delta.
@@ -126,6 +131,11 @@ def get(self, request): # noqa: C901
126131
data['phases_returned']['l2'].append(float(current.phase_currently_returned_l2 or 0) * 1000)
127132
data['phases_returned']['l3'].append(float(current.phase_currently_returned_l3 or 0) * 1000)
128133

134+
if form.cleaned_data.get('voltage'):
135+
data['phase_voltage']['l1'].append(float(current.phase_voltage_l1 or 0))
136+
data['phase_voltage']['l2'].append(float(current.phase_voltage_l2 or 0))
137+
data['phase_voltage']['l3'].append(float(current.phase_voltage_l3 or 0))
138+
129139
data['latest_delta_id'] = current.id
130140

131141
return JsonResponse(data)
89 Bytes
Binary file not shown.

dsmrreader/locales/nl/LC_MESSAGES/django.po

+3
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,9 @@ msgstr "Scroll of knijp de grafiek om in- en uit te zoomen. Sleep de grafiek naa
871871
msgid "Recent distribution of electricity phases (Watt) in the past 24 hours"
872872
msgstr "Recente distributie elektriciteitsfasen (Watt) in de afgelopen 24 uur"
873873

874+
msgid "Phase voltages in the past 24 hours"
875+
msgstr "Fase voltages in de afgelopen 24 uur"
876+
874877
msgid "Recent gas consumed (m<sup>3</sup>) in the past 24 hours"
875878
msgstr "Recent gasverbruik (m<sup>3</sup>) in de afgelopen 24 uur"
876879

0 commit comments

Comments
 (0)