Improve transductor measurements collect

parent 3855060d
from django.contrib import admin
from .models import Transductor, TransductorManager, Measurements
from .models import Transductor, TransductorManager
class TransductorInLine(admin.StackedInline):
model = Transductor
extra = 0
class TransductorManagerAdmin(admin.ModelAdmin):
fieldset = [
(None, {'fields': ['description']}),
......
......@@ -2,8 +2,9 @@ from django import forms
from .models import Transductor
class PostForm(forms.ModelForm):
class Meta:
model = Transductor
fields = ('serie_number', 'ip_address', 'description',)
\ No newline at end of file
fields = ('serie_number', 'ip_address', 'description',)
......@@ -7,7 +7,6 @@ from math import sqrt
import socket
import struct
import sys
import time
import thread
......@@ -79,40 +78,81 @@ class CommunicationProtocol(models.Model):
def start_data_collection(self):
new_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
register_start_address = 68
messages_to_send = []
total_registers = 2
initial_register = 68
last_register = 93
while(initial_register < last_register):
if(initial_register == 86):
initial_register = initial_register + 2
else:
packaged_message = struct.pack("2B", 0x01, 0x03) + struct.pack(">2H", initial_register, total_registers)
crc = self._computate_crc(packaged_message)
message_send = struct.pack("2B", 0x01, 0x03) + struct.pack(">2H", register_start_address, total_registers)
packaged_message = packaged_message + crc
crc = self._computate_crc(message_send)
messages_to_send.append(packaged_message)
message_send = message_send + crc
initial_register = initial_register + 2
alarm = VoltageObserver()
try:
thread.start_new_thread(self._thread_data_collection, (new_socket, message_send, alarm))
thread.start_new_thread(self._thread_data_collection, (new_socket, messages_to_send, alarm))
except:
print "Can't create thread"
return True
def _thread_data_collection(self, socket, message_send, alarm):
i = 0
while(i < 10):
socket.sendto(message_send, (self.transductor.ip_address, self.port))
def _thread_data_collection(self, socket, messages_to_send, alarm):
messages = []
for i in range(len(messages_to_send)):
socket.sendto(messages_to_send[i], (self.transductor.ip_address, self.port))
message_received = socket.recvfrom(256)
value = self._get_value_from_response_message(message_received[0])
alarm.observe('new data received', alarm.verify_voltage)
Event('new data received', value)
messages.append(message_received[0])
# alarm.observe('new data received', alarm.verify_voltage)
# Event('new data received', value)
# self.transductor.measurements_set.create(voltage_a=value, collection_date=timezone.now())
collection_time = timezone.now()
self._create_measurements_from_data_collected(messages, collection_time)
def _create_measurements_from_data_collected(self, messages, collection_time):
data = Measurements()
self.transductor.measurements_set.create(voltage_a=value, collection_date=timezone.now())
data.transductor = self.transductor
i = i + 1
time.sleep(1)
data.voltage_a = float("{0:.3f}".format(self._get_float_value_from_response_message(messages[0])))
data.voltage_b = float("{0:.3f}".format(self._get_float_value_from_response_message(messages[1])))
data.voltage_c = float("{0:.3f}".format(self._get_float_value_from_response_message(messages[2])))
def _get_value_from_response_message(self, message_received_data):
data.current_a = float("{0:.3f}".format(self._get_float_value_from_response_message(messages[3])))
data.current_b = float("{0:.3f}".format(self._get_float_value_from_response_message(messages[4])))
data.current_c = float("{0:.3f}".format(self._get_float_value_from_response_message(messages[5])))
data.active_power_a = float("{0:.3f}".format(self._get_float_value_from_response_message(messages[6])))
data.active_power_b = float("{0:.3f}".format(self._get_float_value_from_response_message(messages[7])))
data.active_power_c = float("{0:.3f}".format(self._get_float_value_from_response_message(messages[8])))
data.reactive_power_a = float("{0:.3f}".format(self._get_float_value_from_response_message(messages[9])))
data.reactive_power_b = float("{0:.3f}".format(self._get_float_value_from_response_message(messages[10])))
data.reactive_power_c = float("{0:.3f}".format(self._get_float_value_from_response_message(messages[11])))
data.apparent_power_a = float("{0:.3f}".format(sqrt(data.active_power_a**2 + data.reactive_power_a**2)))
data.apparent_power_b = float("{0:.3f}".format(sqrt(data.active_power_b**2 + data.reactive_power_b**2)))
data.apparent_power_c = float("{0:.3f}".format(sqrt(data.active_power_c**2 + data.reactive_power_c**2)))
data.collection_date = collection_time
data.save()
def _get_float_value_from_response_message(self, message_received_data):
n_bytes = struct.unpack("1B", message_received_data[2])[0]
msg = bytearray(message_received_data[3:-2])
......@@ -172,6 +212,10 @@ class Measurements(models.Model):
reactive_power_b = models.FloatField(default=None)
reactive_power_c = models.FloatField(default=None)
apparent_power_a = models.FloatField(default=None)
apparent_power_b = models.FloatField(default=None)
apparent_power_c = models.FloatField(default=None)
collection_date = models.DateTimeField('date published')
def __str__(self):
......@@ -183,22 +227,17 @@ class Measurements(models.Model):
def calculate_total_reactive_power(self):
return (self.reactive_power_a + self.reactive_power_b + self.reactive_power_c)
def calculate_apparent_power_phase_a(self):
apparent_power = sqrt((self.active_power_a**2 + self.reactive_power_a**2))
return '{0:.3f}'.format(apparent_power)
def calculate_total_apparent_power(self):
ap_phase_a = self.apparent_power_a
ap_phase_b = self.apparent_power_b
ap_phase_c = self.apparent_power_c
ap_total = (ap_phase_a + ap_phase_b + ap_phase_c)
def calculate_apparent_power_phase_b(self):
apparent_power = sqrt((self.active_power_b**2 + self.reactive_power_b**2))
return '{0:.3f}'.format(apparent_power)
return '{0:.3f}'.format(ap_total)
def calculate_apparent_power_phase_c(self):
apparent_power = sqrt((self.active_power_c**2 + self.reactive_power_c**2))
return '{0:.3f}'.format(apparent_power)
@receiver(post_save, sender=Transductor)
def transductor_saved(sender, instance, **kwargs):
if instance.data_collection:
print "true"
# instance.communicationprotocol_set.first().restart_data_collection()
else:
instance.communicationprotocol_set.first().start_data_collection()
\ No newline at end of file
if not instance.data_collection:
instance.communicationprotocol_set.first().start_data_collection()
......@@ -57,9 +57,10 @@
</tr>
<tr>
<td>Apparent Power</td>
<td>{{ data.calculate_apparent_power_phase_a }}</td>
<td>{{ data.calculate_apparent_power_phase_b }}</td>
<td>{{ data.calculate_apparent_power_phase_c }}</td>
<td>{{ data.apparent_power_a }}</td>
<td>{{ data.apparent_power_b }}</td>
<td>{{ data.apparent_power_c }}</td>
<td>{{ data.calculate_total_apparent_power }}</td>
</tr>
</tbody>
</table>
......
......@@ -9,4 +9,4 @@ urlpatterns = [
url(r'^new$', views.new, name='new'),
url(r'^(?P<pk>[0-9]+)/edit/$', views.edit, name='edit'),
url(r'^(?P<pk>[0-9]+)/delete/$', views.delete, name='delete'),
]
\ No newline at end of file
]
from django.views import generic
from .models import *
from .models import Transductor, TransductorManager
from django.shortcuts import get_object_or_404, render, redirect
from django.http import HttpResponseRedirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
......@@ -7,12 +7,13 @@ from django.core.urlresolvers import reverse
from .forms import PostForm
from django.utils import timezone
class IndexView(generic.ListView):
template_name = 'data_reader/index.html'
context_object_name = 'transductors_list'
template_name = 'data_reader/index.html'
context_object_name = 'transductors_list'
def get_queryset(self):
return Transductor.objects.all()
def get_queryset(self):
return Transductor.objects.all()
def detail(request, transductor_id):
......@@ -20,7 +21,7 @@ def detail(request, transductor_id):
transductor = get_object_or_404(Transductor, pk=transductor_id)
data_list = Transductor.objects.get(id=transductor_id).measurements_set.all()
paginator = Paginator(data_list, 3)
paginator = Paginator(data_list, 4)
page = request.GET.get('page')
try:
......@@ -32,6 +33,7 @@ def detail(request, transductor_id):
return render(request, template_name, {'data_list': data, 'transductor': transductor})
def new(request):
if request.method == "POST":
form = PostForm(request.POST)
......@@ -49,6 +51,7 @@ def new(request):
form = PostForm()
return render(request, 'data_reader/new.html', {'form': form})
def edit(request, pk):
post = get_object_or_404(Transductor, pk=pk)
if request.method == "POST":
......@@ -62,6 +65,9 @@ def edit(request, pk):
form = PostForm(instance=post)
return render(request, 'data_reader/new.html', {'form': form})
def delete(request, pk):
transductor = get_object_or_404(Transductor, pk=pk).delete()
return HttpResponseRedirect(reverse('data_reader:index'))
\ No newline at end of file
transductor = get_object_or_404(Transductor, pk=pk)
transductor.delete()
return HttpResponseRedirect(reverse('data_reader:index'))
from django.shortcuts import render
def index(request):
template_name = 'home/index.html'
return render(request, template_name)
\ No newline at end of file
return render(request, template_name)
......@@ -7,7 +7,8 @@
{% block body %}
<div class="page-header">
<div class="starter-template">
<h1>Data from {{content.transductor.ip_address}}</h1>
<h1>{{content.title}}</h1>
<h2>Data from {{content.transductor.description.title}}</h1>
</div>
</div>
{% if data_list %}
......
......@@ -6,6 +6,7 @@ from django.shortcuts import get_object_or_404
import plugins
def index(request):
return render(request, 'index.html')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment