diff --git a/apps/atrad/__init__.py b/apps/atrad/__init__.py new file mode 100644 index 0000000..81831c6 --- /dev/null +++ b/apps/atrad/__init__.py @@ -0,0 +1,2 @@ +from . import mqtt +mqtt.client.loop_start() \ No newline at end of file diff --git a/apps/atrad/admin.py b/apps/atrad/admin.py new file mode 100644 index 0000000..4e2323f --- /dev/null +++ b/apps/atrad/admin.py @@ -0,0 +1,6 @@ +from django.contrib import admin +from .models import ATRADConfiguration + +# Register your models here. + +admin.site.register(ATRADConfiguration) diff --git a/apps/atrad/apps.py b/apps/atrad/apps.py new file mode 100644 index 0000000..43d6707 --- /dev/null +++ b/apps/atrad/apps.py @@ -0,0 +1,6 @@ +#from django.apps import AppConfig + + +#class AtradConfig(AppConfig):# +# default_auto_field = 'django.db.models.BigAutoField' +# name = 'atrad' diff --git a/apps/atrad/files.py b/apps/atrad/files.py new file mode 100644 index 0000000..a9d150f --- /dev/null +++ b/apps/atrad/files.py @@ -0,0 +1,19 @@ +import json + +def read_json_file(fp): + + kwargs = {} + + json_data = fp + data = json.load(json_data) + json_data.close() + + topic = data["topic"][0][1] + + kwargs['topic'] = topic + + return kwargs + + +def write_json_file(filename): + pass \ No newline at end of file diff --git a/apps/atrad/forms.py b/apps/atrad/forms.py new file mode 100644 index 0000000..7929874 --- /dev/null +++ b/apps/atrad/forms.py @@ -0,0 +1,27 @@ +from django import forms +from apps.main.models import Device +from .models import ATRADConfiguration + +class ATRADConfigurationForm(forms.ModelForm): + + def __init__(self, *args, **kwargs): + super(ATRADConfigurationForm, self).__init__(*args, **kwargs) + instance = getattr(self, 'instance', None) + + if instance and instance.pk: + devices = Device.objects.filter(device_type__name='atrad') + if instance.experiment: + self.fields['experiment'].widget.attrs['disabled'] = 'disabled' + self.fields['device'].widget.choices = [(device.id, device) for device in devices] + + def clean(self): + return + + class Meta: + model = ATRADConfiguration + exclude = ('type', 'parameters', 'status', 'author', 'hash') + + +class UploadFileForm(forms.Form): + title = forms.CharField(label='Extension Type', widget=forms.TextInput(attrs={'readonly':'readonly'})) + file = forms.FileField() diff --git a/apps/atrad/models.py b/apps/atrad/models.py new file mode 100644 index 0000000..8157708 --- /dev/null +++ b/apps/atrad/models.py @@ -0,0 +1,175 @@ +from django.db import models +from apps.main.models import Configuration +from apps.main.utils import Params +from django.core.validators import MinValueValidator, MaxValueValidator + +from .files import read_json_file +import requests +# Create your models here. validators=[MinValueValidator(62.5e6), MaxValueValidator(450e6)] + +class ATRADConfiguration(Configuration): + + topic = models.PositiveIntegerField(verbose_name='Topic',validators=[MaxValueValidator(10)], default = 0) + + def verify_frequencies(self): + + return True + + + def status_device(self): + + ip=self.device.ip_address + port=self.device.port_address + + route = "http://" + str(ip) + ":" + str(port) + "/status/" + try: + r = requests.get(route, timeout=0.7) + except Exception as e: + self.device.status = 0 + self.device.save() + self.message = 'Could not read TX status: ' + str(e) + return False + + response = r.json() + self.device.status = response['status'] + self.message = response['message'] + self.device.save() + + if response['components_status']==0: + return False + + return True + + + def start_device(self): + + ip=self.device.ip_address + port=self.device.port_address + + #---Device must be configured + if not self.device.status == 2: + self.message = 'TX Device must be configured.' + return False + #---Frequencies from form + post_data = self.parms_to_dict() + route = "http://" + str(ip) + ":" + str(port) + "/write/" + + try: + r = requests.post(route, post_data, timeout=0.7) + except Exception as e: + self.message = "Could not start TX device. "+str(e) + return False + + response = r.json() + if response['status']==1: + self.device.status = 1 + self.device.save() + self.message = response['message'] + return False + + self.device.status = response['status'] + self.device.save() + self.message = response['message'] + + return True + + + def stop_device(self): + + ip=self.device.ip_address + port=self.device.port_address + + if self.device.status == 2: #Configured + self.message = 'TX device is already stopped.' + return False + + post_data = {"topic":0} + route = "http://" + str(ip) + ":" + str(port) + "/write/" + + try: + r = requests.post(route, post_data, timeout=0.7) + except Exception as e: + self.message = "Could not write TX parameters. "+str(e) + self.device.status = 0 + self.device.save() + return False + + response = r.json() + status = response['status'] + if status == 1: + self.device.status = status + self.device.save() + self.message = 'Could not stop TX device.' + return False + + self.message = 'TX device has been stopped successfully.' + self.device.status = 2 + self.device.save() + + return True + + + def read_device(self): + + ip=self.device.ip_address + port=self.device.port_address + + route = "http://" + str(ip) + ":" + str(port) + "/read/" + try: + frequencies = requests.get(route,timeout=0.7) + except: + self.message = "Could not read TX parameters from this device" + return None + + frequencies = frequencies.json() + if frequencies: + frequencies = frequencies.get("Frequencies") + topic = frequencies.get("topic") + + parms = {'topic': topic} + + self.message = "TX parameters have been successfully read" + return parms + else: + self.message = "Error reading TX parameters" + return None + + + def write_device(self): + + ip=self.device.ip_address + port=self.device.port_address + + #---Frequencies from form + parms = self.parms_to_dict()['configurations'] + for parm in parms['allIds']: + byid = parm + frequencies = parms['byId'][byid] + post_data = {} + for data in frequencies: + if data in ['topic']: + post_data[data] = frequencies[data] + + route = "http://" + str(ip) + ":" + str(port) + "/write/" + print (post_data) + try: + r = requests.post(route, post_data, timeout=0.7) + except: + self.message = "Could not write TX parameters" + self.device.status = 0 + self.device.save() + return False + + response = r.json() + self.message = response['message'] + self.device.status = response['status'] + self.device.save() + + if self.device.status==1: + return False + + return True + + + class Meta: + db_table = 'atrad_configurations' \ No newline at end of file diff --git a/apps/atrad/mqtt.py b/apps/atrad/mqtt.py new file mode 100644 index 0000000..0e80604 --- /dev/null +++ b/apps/atrad/mqtt.py @@ -0,0 +1,38 @@ +import paho.mqtt.client as mqtt +from radarsys import settings +from radarsys.socketconfig import sio as sio +import numpy as np + +def on_connect(mqtt_client, userdata, flags, rc): + if rc == 0: + print('Connected successfully') + mqtt_client.subscribe('atrad/test3') + else: + print('Bad connection. Code:', rc) + +def maxima_temp(trs): + np_array = [np.array(i) for i in trs] + temps = [max(i[i<40]) for i in np_array] + return max(temps) + +def on_message(mqtt_client, userdata, msg): + print(f'Received message on topic: {msg.topic} with payload: {msg.payload}', flush=True) + trsi = [[],[],[],[]] + mensaje = str(msg.payload) + datos = [i for i in mensaje[21:-1].split("*")] + status=''.join([datos[i][3] for i in [0,1,2,3]]) + for trs,i in zip(datos,[0,1,2,3]) : + trsi[i]= [int(i) for i in trs[1:-1].split(",")] + potencias = [trsi[0][34],trsi[0][36],trsi[2][32],trsi[2][34]] + tmax = maxima_temp(trsi) + sio.emit('test', data={'time':mensaje[2:21],'num':trsi[0][0],'pow':potencias,'tmax':str(tmax),'status':status}) + +client = mqtt.Client() +client.on_connect = on_connect +client.on_message = on_message +client.username_pw_set(settings.MQTT_USER, settings.MQTT_PASSWORD) +client.connect( + host=settings.MQTT_SERVER, + port=settings.MQTT_PORT, + keepalive=settings.MQTT_KEEPALIVE +) \ No newline at end of file diff --git a/apps/atrad/templates/atrad_conf.html b/apps/atrad/templates/atrad_conf.html new file mode 100644 index 0000000..0a87d6e --- /dev/null +++ b/apps/atrad/templates/atrad_conf.html @@ -0,0 +1,288 @@ +{% extends "dev_conf.html" %} +{% block extra-head %} + +{% endblock %} + +{% block extra-content %} + + + +
Tx1 | ++ | Sin envio de datos |
+
---|---|---|
Tx2 | ++ | Sin envio de datos |
+
+ {% lorem %} +
+{% endblock %} + +{% block sidebar%} + +{% endblock %} diff --git a/apps/atrad/templates/index_atrad.html b/apps/atrad/templates/index_atrad.html new file mode 100644 index 0000000..283a4c8 --- /dev/null +++ b/apps/atrad/templates/index_atrad.html @@ -0,0 +1,46 @@ +{% extends "base.html" %} +{% load django_bootstrap5 %} +{% block mainactive %}active{% endblock %} + +{% block content-title %}DEVICE CGS{% endblock %} +{% block content-suptitle %}CLOCK GENERATOR AND SYNCHRONIZER{% endblock %} + +{% block content %} ++ Ingresar Frecuencias +
+ + + + + {% if form.is_multipart %} + + + +