@@ -1,69 +1,176 | |||||
1 | from django.db import models |
|
1 | from django.db import models | |
2 | from apps.main.models import Configuration |
|
2 | from apps.main.models import Configuration | |
3 | #from json_field import JSONField |
|
3 | #from json_field import JSONField | |
4 | from django.core.validators import MinValueValidator, MaxValueValidator |
|
4 | from django.core.validators import MinValueValidator, MaxValueValidator | |
5 |
|
5 | |||
6 |
|
6 | |||
7 | from apps.main.models import Device, Experiment |
|
7 | from apps.main.models import Device, Experiment | |
8 |
|
8 | |||
9 | from files import read_json_file |
|
9 | from files import read_json_file | |
10 | # Create your models here. validators=[MinValueValidator(62.5e6), MaxValueValidator(450e6)] |
|
10 | # Create your models here. validators=[MinValueValidator(62.5e6), MaxValueValidator(450e6)] | |
11 |
|
11 | |||
12 | class CGSConfiguration(Configuration): |
|
12 | class CGSConfiguration(Configuration): | |
13 |
|
13 | |||
14 | freq0 = models.IntegerField(verbose_name='Frequency 0',validators=[MinValueValidator(0), MaxValueValidator(450e6)], blank=True, null=True) |
|
14 | freq0 = models.IntegerField(verbose_name='Frequency 0',validators=[MinValueValidator(0), MaxValueValidator(450e6)], blank=True, null=True) | |
15 | freq1 = models.IntegerField(verbose_name='Frequency 1',validators=[MinValueValidator(0), MaxValueValidator(450e6)], blank=True, null=True) |
|
15 | freq1 = models.IntegerField(verbose_name='Frequency 1',validators=[MinValueValidator(0), MaxValueValidator(450e6)], blank=True, null=True) | |
16 | freq2 = models.IntegerField(verbose_name='Frequency 2',validators=[MinValueValidator(0), MaxValueValidator(450e6)], blank=True, null=True) |
|
16 | freq2 = models.IntegerField(verbose_name='Frequency 2',validators=[MinValueValidator(0), MaxValueValidator(450e6)], blank=True, null=True) | |
17 | freq3 = models.IntegerField(verbose_name='Frequency 3',validators=[MinValueValidator(0), MaxValueValidator(450e6)], blank=True, null=True) |
|
17 | freq3 = models.IntegerField(verbose_name='Frequency 3',validators=[MinValueValidator(0), MaxValueValidator(450e6)], blank=True, null=True) | |
18 | #jfreqs = JSONField(default={"frequencies":[{"f0":freq0,"f1":freq1,"f2":freq2,"f3":freq3}]}, blank=True) |
|
18 | #jfreqs = JSONField(default={"frequencies":[{"f0":freq0,"f1":freq1,"f2":freq2,"f3":freq3}]}, blank=True) | |
19 |
|
19 | |||
20 |
|
20 | |||
21 | def verify_frequencies(self): |
|
21 | def verify_frequencies(self): | |
22 |
|
22 | |||
23 | return True |
|
23 | return True | |
24 |
|
24 | |||
25 |
|
25 | |||
26 | def update_from_file(self, fp): |
|
26 | def update_from_file(self, fp): | |
27 |
|
27 | |||
28 | kwargs = read_json_file(fp) |
|
28 | kwargs = read_json_file(fp) | |
29 |
|
29 | |||
30 | if not kwargs: |
|
30 | if not kwargs: | |
31 | return False |
|
31 | return False | |
32 |
|
32 | |||
33 | self.freq0 = kwargs['freq0'] |
|
33 | self.freq0 = kwargs['freq0'] | |
34 | self.freq1 = kwargs['freq1'] |
|
34 | self.freq1 = kwargs['freq1'] | |
35 | self.freq2 = kwargs['freq2'] |
|
35 | self.freq2 = kwargs['freq2'] | |
36 | self.freq3 = kwargs['freq3'] |
|
36 | self.freq3 = kwargs['freq3'] | |
37 |
|
37 | |||
38 | return True |
|
38 | return True | |
39 |
|
39 | |||
40 | def parms_to_dict(self): |
|
40 | def parms_to_dict(self): | |
41 |
|
41 | |||
42 | parameters = {} |
|
42 | parameters = {} | |
43 |
|
43 | |||
44 | if self.freq0 == None or self.freq0 == '': |
|
44 | if self.freq0 == None or self.freq0 == '': | |
45 | parameters['freq0'] = 0 |
|
45 | parameters['freq0'] = 0 | |
46 | else: |
|
46 | else: | |
47 | parameters['freq0'] = self.freq0 |
|
47 | parameters['freq0'] = self.freq0 | |
48 |
|
48 | |||
49 | if self.freq1 == None or self.freq1 == '': |
|
49 | if self.freq1 == None or self.freq1 == '': | |
50 | parameters['freq1'] = 0 |
|
50 | parameters['freq1'] = 0 | |
51 | else: |
|
51 | else: | |
52 | parameters['freq1'] = self.freq1 |
|
52 | parameters['freq1'] = self.freq1 | |
53 |
|
53 | |||
54 | if self.freq2 == None or self.freq2 == '': |
|
54 | if self.freq2 == None or self.freq2 == '': | |
55 | parameters['freq2'] = 0 |
|
55 | parameters['freq2'] = 0 | |
56 | else: |
|
56 | else: | |
57 | parameters['freq2'] = self.freq2 |
|
57 | parameters['freq2'] = self.freq2 | |
58 |
|
58 | |||
59 | if self.freq3 == None or self.freq3 == '': |
|
59 | if self.freq3 == None or self.freq3 == '': | |
60 | parameters['freq3'] = 0 |
|
60 | parameters['freq3'] = 0 | |
61 | else: |
|
61 | else: | |
62 | parameters['freq3'] = self.freq3 |
|
62 | parameters['freq3'] = self.freq3 | |
63 |
|
63 | |||
64 |
|
64 | |||
65 | return parameters |
|
65 | return parameters | |
66 |
|
66 | |||
67 |
|
67 | |||
|
68 | def status_device(self): | |||
|
69 | ||||
|
70 | import requests | |||
|
71 | ||||
|
72 | ip=self.device.ip_address | |||
|
73 | port=self.device.port_address | |||
|
74 | ||||
|
75 | route = "http://" + str(ip) + ":" + str(port) + "/status/ad9548" | |||
|
76 | try: | |||
|
77 | r = requests.get(route) | |||
|
78 | except: | |||
|
79 | self.device.status = 0 | |||
|
80 | self.device.save() | |||
|
81 | return self.device.status | |||
|
82 | ||||
|
83 | response = str(r.text) | |||
|
84 | response = response.split(";") | |||
|
85 | icon = response[0] | |||
|
86 | status = response[-1] | |||
|
87 | ||||
|
88 | print icon, status | |||
|
89 | #"icon" could be: "alert" or "okay" | |||
|
90 | if "alert" in icon: | |||
|
91 | if "Starting Up" in status: #No Esta conectado | |||
|
92 | self.device.status = 0 | |||
|
93 | else: | |||
|
94 | self.device.status = 1 | |||
|
95 | elif "okay" in icon: | |||
|
96 | self.device.status = 3 | |||
|
97 | else: | |||
|
98 | self.device.status = 1 | |||
|
99 | ||||
|
100 | self.message = status | |||
|
101 | self.device.save() | |||
|
102 | ||||
|
103 | ||||
|
104 | return self.device.status | |||
|
105 | ||||
|
106 | ||||
|
107 | def read_device(self): | |||
|
108 | ||||
|
109 | import requests | |||
|
110 | ||||
|
111 | ip=self.device.ip_address | |||
|
112 | port=self.device.port_address | |||
|
113 | ||||
|
114 | route = "http://" + str(ip) + ":" + str(port) + "/frequencies/" | |||
|
115 | try: | |||
|
116 | frequencies = requests.get(route) | |||
|
117 | ||||
|
118 | except: | |||
|
119 | self.message = "Could not read CGS parameters from this device" | |||
|
120 | return None | |||
|
121 | ||||
|
122 | frequencies = frequencies.json() | |||
|
123 | frequencies = frequencies.get("Frecuencias") | |||
|
124 | f0 = frequencies.get("f0") | |||
|
125 | f1 = frequencies.get("f1") | |||
|
126 | f2 = frequencies.get("f2") | |||
|
127 | f3 = frequencies.get("f3") | |||
|
128 | ||||
|
129 | parms = {'freq0': f0, | |||
|
130 | 'freq1': f1, | |||
|
131 | 'freq2': f2, | |||
|
132 | 'freq3': f3} | |||
|
133 | ||||
|
134 | self.message = "" | |||
|
135 | return parms | |||
|
136 | ||||
|
137 | ||||
|
138 | def write_device(self): | |||
|
139 | ||||
|
140 | import requests | |||
|
141 | ||||
|
142 | ip=self.device.ip_address | |||
|
143 | port=self.device.port_address | |||
|
144 | ||||
|
145 | #---Frequencies from form | |||
|
146 | f0 = self.freq0 | |||
|
147 | f1 = self.freq1 | |||
|
148 | f2 = self.freq2 | |||
|
149 | f3 = self.freq3 | |||
|
150 | post_data = {"f0":f0, "f1":f1, "f2":f2, "f3":f3} | |||
|
151 | route = "http://" + str(ip) + ":" + str(port) + "/frequencies/" | |||
|
152 | ||||
|
153 | try: | |||
|
154 | r = requests.post(route, post_data) | |||
|
155 | except: | |||
|
156 | self.message = "Could not write CGS parameters" | |||
|
157 | return None | |||
|
158 | ||||
|
159 | text = r.text | |||
|
160 | text = text.split(',') | |||
|
161 | ||||
|
162 | if len(text)>1: | |||
|
163 | title = text[0] | |||
|
164 | status = text[1] | |||
|
165 | if title == "okay": | |||
|
166 | self.message = status | |||
|
167 | return 3 | |||
|
168 | else: | |||
|
169 | self.message = title + ", " + status | |||
|
170 | return 1 | |||
|
171 | ||||
|
172 | return 1 | |||
|
173 | ||||
|
174 | ||||
68 | class Meta: |
|
175 | class Meta: | |
69 | db_table = 'cgs_configurations' |
|
176 | db_table = 'cgs_configurations' |
@@ -1,14 +1,14 | |||||
1 | from django.conf.urls import url |
|
1 | from django.conf.urls import url | |
2 |
|
2 | |||
3 | urlpatterns = ( |
|
3 | urlpatterns = ( | |
4 | url(r'^(?P<id_conf>-?\d+)/$', 'apps.cgs.views.cgs_conf', name='url_cgs_conf'), |
|
4 | url(r'^(?P<id_conf>-?\d+)/$', 'apps.cgs.views.cgs_conf', name='url_cgs_conf'), | |
5 | url(r'^(?P<id_conf>-?\d+)/edit/$', 'apps.cgs.views.cgs_conf_edit', name='url_edit_cgs_conf'), |
|
5 | url(r'^(?P<id_conf>-?\d+)/edit/$', 'apps.cgs.views.cgs_conf_edit', name='url_edit_cgs_conf'), | |
6 | #url(r'^(?P<id_conf>-?\d+)/(?P<message>-?\d+)/$', 'apps.cgs.views.cgs_conf', name='url_cgs_conf'), |
|
6 | #url(r'^(?P<id_conf>-?\d+)/(?P<message>-?\d+)/$', 'apps.cgs.views.cgs_conf', name='url_cgs_conf'), | |
7 | url(r'^(?P<id_conf>-?\d+)/edit/$', 'apps.cgs.views.cgs_conf_edit', name='url_edit_cgs_conf'), |
|
7 | # url(r'^(?P<id_conf>-?\d+)/edit/$', 'apps.cgs.views.cgs_conf_edit', name='url_edit_cgs_conf'), | |
8 | url(r'^(?P<id_conf>-?\d+)/write/$', 'apps.cgs.views.cgs_conf_write', name='url_write_cgs_conf'), |
|
8 | # url(r'^(?P<id_conf>-?\d+)/write/$', 'apps.cgs.views.cgs_conf_write', name='url_write_cgs_conf'), | |
9 | url(r'^(?P<id_conf>-?\d+)/read/$', 'apps.cgs.views.cgs_conf_read', name='url_read_cgs_conf'), |
|
9 | # url(r'^(?P<id_conf>-?\d+)/read/$', 'apps.cgs.views.cgs_conf_read', name='url_read_cgs_conf'), | |
10 | url(r'^(?P<id_conf>-?\d+)/import/$', 'apps.cgs.views.cgs_conf_import', name='url_import_cgs_conf'), |
|
10 | # url(r'^(?P<id_conf>-?\d+)/import/$', 'apps.cgs.views.cgs_conf_import', name='url_import_cgs_conf'), | |
11 | url(r'^(?P<id_conf>-?\d+)/export/$', 'apps.cgs.views.cgs_conf_export', name='url_export_cgs_conf'), |
|
11 | # url(r'^(?P<id_conf>-?\d+)/export/$', 'apps.cgs.views.cgs_conf_export', name='url_export_cgs_conf'), | |
12 | #url(r'^(?P<id_conf>-?\d+)/export/$', 'apps.main.views.dev_conf_export', name='url_export_cgs_conf'), |
|
12 | #url(r'^(?P<id_conf>-?\d+)/export/$', 'apps.main.views.dev_conf_export', name='url_export_cgs_conf'), | |
13 | ) |
|
13 | ) | |
14 |
|
14 |
@@ -1,322 +1,326 | |||||
1 | from django.shortcuts import redirect, render, get_object_or_404 |
|
1 | from django.shortcuts import redirect, render, get_object_or_404 | |
2 | from django.contrib import messages |
|
2 | from django.contrib import messages | |
3 | from django.http import HttpResponse |
|
3 | from django.http import HttpResponse | |
4 |
|
4 | |||
5 | from apps.main.models import Experiment, Configuration |
|
5 | from apps.main.models import Experiment, Configuration | |
6 | from .models import CGSConfiguration |
|
6 | from .models import CGSConfiguration | |
7 |
|
7 | |||
8 | from .forms import CGSConfigurationForm, UploadFileForm |
|
8 | from .forms import CGSConfigurationForm, UploadFileForm | |
9 | from apps.main.views import sidebar |
|
9 | from apps.main.views import sidebar | |
10 |
|
10 | |||
11 | import requests |
|
11 | import requests | |
12 | import json |
|
12 | import json | |
13 | #from __builtin__ import None |
|
13 | #from __builtin__ import None | |
14 | # Create your views here. |
|
14 | # Create your views here. | |
15 |
|
15 | |||
16 | def cgs_conf(request, id_conf): |
|
16 | def cgs_conf(request, id_conf): | |
17 |
|
17 | |||
18 | conf = get_object_or_404(CGSConfiguration, pk=id_conf) |
|
18 | conf = get_object_or_404(CGSConfiguration, pk=id_conf) | |
19 |
|
19 | |||
20 | ip=conf.device.ip_address |
|
20 | ip=conf.device.ip_address | |
21 | port=conf.device.port_address |
|
21 | port=conf.device.port_address | |
22 |
|
22 | |||
23 | kwargs = {} |
|
23 | kwargs = {} | |
24 |
|
24 | |||
25 | if request.method=='GET': |
|
25 | kwargs['status'] = conf.device.get_status_display() | |
|
26 | ||||
|
27 | #if request.method=='GET': | |||
26 | #r: response = icon, status |
|
28 | #r: response = icon, status | |
27 | try: |
|
29 | # try: | |
28 | route = "http://" + str(ip) + ":" + str(port) + "/status/ad9548" |
|
30 | # route = "http://" + str(ip) + ":" + str(port) + "/status/ad9548" | |
29 | r = requests.get(route) |
|
31 | # r = requests.get(route) | |
30 | response = str(r.text) |
|
32 | # response = str(r.text) | |
31 | response = response.split(";") |
|
33 | # response = response.split(";") | |
32 | icon = response[0] |
|
34 | # icon = response[0] | |
33 | status = response[-1] |
|
35 | # status = response[-1] | |
34 | #print r.text |
|
36 | #print r.text | |
35 | #"icon" could be: "alert" or "okay" |
|
37 | #"icon" could be: "alert" or "okay" | |
36 | # Si hay alerta pero esta conectado |
|
38 | # Si hay alerta pero esta conectado | |
37 | if "alert" in icon: |
|
39 | # if "alert" in icon: | |
38 | if "Starting Up" in status: #No Esta conectado |
|
40 | # if "Starting Up" in status: #No Esta conectado | |
39 | kwargs['connected'] = False |
|
41 | # kwargs['connected'] = False | |
40 | else: |
|
42 | # else: | |
41 | kwargs['connected'] = True |
|
43 | # kwargs['connected'] = True | |
42 | elif "okay" in icon: |
|
44 | # elif "okay" in icon: | |
43 | kwargs['connected'] = True |
|
45 | # kwargs['connected'] = True | |
44 | else: |
|
46 | # else: | |
45 | kwargs['connected'] = False |
|
47 | # kwargs['connected'] = False | |
46 |
|
48 | |||
47 | except: |
|
49 | # except: | |
48 | kwargs['connected'] = False |
|
50 | # kwargs['connected'] = False | |
49 | status = "The Device is not connected." |
|
51 | # status = "The Device is not connected." | |
50 |
|
52 | |||
51 | if not kwargs['connected']: |
|
53 | #if not kwargs['connected']: | |
52 | messages.error(request, message=status) |
|
54 | # messages.error(request, message=status) | |
53 |
|
55 | |||
54 | kwargs['dev_conf'] = conf |
|
56 | kwargs['dev_conf'] = conf | |
55 | kwargs['dev_conf_keys'] = ['experiment', 'device', |
|
57 | kwargs['dev_conf_keys'] = ['experiment', 'device', | |
56 | 'freq0', 'freq1', |
|
58 | 'freq0', 'freq1', | |
57 | 'freq2', 'freq3'] |
|
59 | 'freq2', 'freq3'] | |
58 |
|
60 | |||
59 | kwargs['title'] = 'CGS Configuration' |
|
61 | kwargs['title'] = 'CGS Configuration' | |
60 | kwargs['suptitle'] = 'Details' |
|
62 | kwargs['suptitle'] = 'Details' | |
61 |
|
63 | |||
62 | kwargs['button'] = 'Edit Configuration' |
|
64 | kwargs['button'] = 'Edit Configuration' | |
63 |
|
65 | |||
|
66 | kwargs['no_play'] = True | |||
|
67 | ||||
64 | ###### SIDEBAR ###### |
|
68 | ###### SIDEBAR ###### | |
65 | kwargs.update(sidebar(conf)) |
|
69 | kwargs.update(sidebar(conf)) | |
66 |
|
70 | |||
67 | return render(request, 'cgs_conf.html', kwargs) |
|
71 | return render(request, 'cgs_conf.html', kwargs) | |
68 |
|
72 | |||
69 | def cgs_conf_edit(request, id_conf): |
|
73 | def cgs_conf_edit(request, id_conf): | |
70 |
|
74 | |||
71 | conf = get_object_or_404(CGSConfiguration, pk=id_conf) |
|
75 | conf = get_object_or_404(CGSConfiguration, pk=id_conf) | |
72 |
|
76 | |||
73 | if request.method=='GET': |
|
77 | if request.method=='GET': | |
74 | form = CGSConfigurationForm(instance=conf) |
|
78 | form = CGSConfigurationForm(instance=conf) | |
75 |
|
79 | |||
76 | if request.method=='POST': |
|
80 | if request.method=='POST': | |
77 | form = CGSConfigurationForm(request.POST, instance=conf) |
|
81 | form = CGSConfigurationForm(request.POST, instance=conf) | |
78 |
|
82 | |||
79 | if form.is_valid(): |
|
83 | if form.is_valid(): | |
80 | if conf.freq0 == None: conf.freq0 = 0 |
|
84 | if conf.freq0 == None: conf.freq0 = 0 | |
81 | if conf.freq1 == None: conf.freq1 = 0 |
|
85 | if conf.freq1 == None: conf.freq1 = 0 | |
82 | if conf.freq2 == None: conf.freq2 = 0 |
|
86 | if conf.freq2 == None: conf.freq2 = 0 | |
83 | if conf.freq3 == None: conf.freq3 = 0 |
|
87 | if conf.freq3 == None: conf.freq3 = 0 | |
84 |
|
88 | |||
85 | conf = form.save(commit=False) |
|
89 | conf = form.save(commit=False) | |
86 |
|
90 | |||
87 | if conf.verify_frequencies(): |
|
91 | if conf.verify_frequencies(): | |
88 | conf.save() |
|
92 | conf.save() | |
89 | return redirect('url_cgs_conf', id_conf=conf.id) |
|
93 | return redirect('url_cgs_conf', id_conf=conf.id) | |
90 |
|
94 | |||
91 | ##ERRORS |
|
95 | ##ERRORS | |
92 |
|
96 | |||
93 | kwargs = {} |
|
97 | kwargs = {} | |
94 | kwargs['id_dev'] = conf.id |
|
98 | kwargs['id_dev'] = conf.id | |
95 | kwargs['form'] = form |
|
99 | kwargs['form'] = form | |
96 | kwargs['title'] = 'Device Configuration' |
|
100 | kwargs['title'] = 'Device Configuration' | |
97 | kwargs['suptitle'] = 'Edit' |
|
101 | kwargs['suptitle'] = 'Edit' | |
98 | kwargs['button'] = 'Save' |
|
102 | kwargs['button'] = 'Save' | |
99 |
|
103 | |||
100 |
|
104 | |||
101 | ###### SIDEBAR ###### |
|
105 | ###### SIDEBAR ###### | |
102 | kwargs.update(sidebar(conf)) |
|
106 | kwargs.update(sidebar(conf)) | |
103 |
|
107 | |||
104 | return render(request, 'cgs_conf_edit.html', kwargs) |
|
108 | return render(request, 'cgs_conf_edit.html', kwargs) | |
105 |
|
109 | # | ||
106 | def cgs_conf_write(request, id_conf): |
|
110 | # def cgs_conf_write(request, id_conf): | |
107 |
|
111 | # | ||
108 | conf = get_object_or_404(CGSConfiguration, pk=id_conf) |
|
112 | # conf = get_object_or_404(CGSConfiguration, pk=id_conf) | |
109 | ip=conf.device.ip_address |
|
113 | # ip=conf.device.ip_address | |
110 | port=conf.device.port_address |
|
114 | # port=conf.device.port_address | |
111 |
|
115 | # | ||
112 | #Frequencies from form |
|
116 | # #Frequencies from form | |
113 | f0 = conf.freq0 |
|
117 | # f0 = conf.freq0 | |
114 | f1 = conf.freq1 |
|
118 | # f1 = conf.freq1 | |
115 | f2 = conf.freq2 |
|
119 | # f2 = conf.freq2 | |
116 | f3 = conf.freq3 |
|
120 | # f3 = conf.freq3 | |
117 |
|
121 | # | ||
118 | try: |
|
122 | # try: | |
119 | post_data = {"f0":f0, "f1":f1, "f2":f2, "f3":f3} |
|
123 | # post_data = {"f0":f0, "f1":f1, "f2":f2, "f3":f3} | |
120 | route = "http://" + str(ip) + ":" + str(port) + "/frequencies/" |
|
124 | # route = "http://" + str(ip) + ":" + str(port) + "/frequencies/" | |
121 | r = requests.post(route, post_data) |
|
125 | # r = requests.post(route, post_data) | |
122 | text = r.text |
|
126 | # text = r.text | |
123 | text = text.split(',') |
|
127 | # text = text.split(',') | |
124 |
|
128 | # | ||
125 | try: |
|
129 | # try: | |
126 | if len(text)>1: |
|
130 | # if len(text)>1: | |
127 | title = text[0] |
|
131 | # title = text[0] | |
128 | status = text[1] |
|
132 | # status = text[1] | |
129 | status_ok = r.status_code |
|
133 | # status_ok = r.status_code | |
130 | if title == "okay": |
|
134 | # if title == "okay": | |
131 | messages.success(request, status) |
|
135 | # messages.success(request, status) | |
132 | else: |
|
136 | # else: | |
133 | messages.error(request, status) |
|
137 | # messages.error(request, status) | |
134 |
|
138 | # | ||
135 | else: |
|
139 | # else: | |
136 | title = text[0] |
|
140 | # title = text[0] | |
137 | messages.error(request, title) |
|
141 | # messages.error(request, title) | |
138 |
|
142 | # | ||
139 | except: |
|
143 | # except: | |
140 | messages.error(request, "An hardware error was found.") |
|
144 | # messages.error(request, "An hardware error was found.") | |
141 |
|
145 | # | ||
142 | except: |
|
146 | # except: | |
143 | messages.error(request, "Could not write parameters.") |
|
147 | # messages.error(request, "Could not write parameters.") | |
144 |
|
148 | # | ||
145 |
|
149 | # | ||
146 |
|
150 | # | ||
147 |
|
151 | # | ||
148 | return redirect('url_cgs_conf', id_conf=conf.id) |
|
152 | # return redirect('url_cgs_conf', id_conf=conf.id) | |
149 |
|
153 | # | ||
150 | def cgs_conf_read(request, id_conf): |
|
154 | # def cgs_conf_read(request, id_conf): | |
151 |
|
155 | # | ||
152 | conf = get_object_or_404(CGSConfiguration, pk=id_conf) |
|
156 | # conf = get_object_or_404(CGSConfiguration, pk=id_conf) | |
153 |
|
157 | # | ||
154 | ip=conf.device.ip_address |
|
158 | # ip=conf.device.ip_address | |
155 | port=conf.device.port_address |
|
159 | # port=conf.device.port_address | |
156 |
|
160 | # | ||
157 | if request.method=='POST': |
|
161 | # if request.method=='POST': | |
158 | form = CGSConfigurationForm(request.POST, instance=conf) |
|
162 | # form = CGSConfigurationForm(request.POST, instance=conf) | |
159 |
|
163 | # | ||
160 | if form.is_valid(): |
|
164 | # if form.is_valid(): | |
161 | cgs_model = form.save(commit=False) |
|
165 | # cgs_model = form.save(commit=False) | |
162 |
|
166 | # | ||
163 | if cgs_model.verify_frequencies(): |
|
167 | # if cgs_model.verify_frequencies(): | |
164 |
|
168 | # | ||
165 | cgs_model.save() |
|
169 | # cgs_model.save() | |
166 | return redirect('url_cgs_conf', id_conf=conf.id) |
|
170 | # return redirect('url_cgs_conf', id_conf=conf.id) | |
167 |
|
171 | # | ||
168 | messages.error(request, "Parameters could not be saved. Invalid parameters") |
|
172 | # messages.error(request, "Parameters could not be saved. Invalid parameters") | |
169 |
|
173 | # | ||
170 | data = {} |
|
174 | # data = {} | |
171 |
|
175 | # | ||
172 |
|
176 | # | ||
173 | if request.method=='GET': |
|
177 | # if request.method=='GET': | |
174 | #r: response = icon, status |
|
178 | # #r: response = icon, status | |
175 | route = "http://" + str(ip) + ":" + str(port) + "/status/ad9548" |
|
179 | # route = "http://" + str(ip) + ":" + str(port) + "/status/ad9548" | |
176 | try: |
|
180 | # try: | |
177 | r = requests.get(route) |
|
181 | # r = requests.get(route) | |
178 | response = str(r.text) |
|
182 | # response = str(r.text) | |
179 | response = response.split(";") |
|
183 | # response = response.split(";") | |
180 | icon = response[0] |
|
184 | # icon = response[0] | |
181 | status = response[-1] |
|
185 | # status = response[-1] | |
182 | print r.text |
|
186 | # print r.text | |
183 | #"icon" could be: "alert" or "okay" |
|
187 | # #"icon" could be: "alert" or "okay" | |
184 | if "okay" in icon: |
|
188 | # if "okay" in icon: | |
185 | messages.success(request, status) |
|
189 | # messages.success(request, status) | |
186 | else: |
|
190 | # else: | |
187 | messages.error(request, status) |
|
191 | # messages.error(request, status) | |
188 | #Get Frequencies |
|
192 | # #Get Frequencies | |
189 | route = "http://" + str(ip) + ":" + str(port) + "/frequencies/" |
|
193 | # route = "http://" + str(ip) + ":" + str(port) + "/frequencies/" | |
190 | #frequencies = requests.get('http://10.10.10.175:8080/frequencies/') |
|
194 | # #frequencies = requests.get('http://10.10.10.175:8080/frequencies/') | |
191 | frequencies = requests.get(route) |
|
195 | # frequencies = requests.get(route) | |
192 | frequencies = frequencies.json() |
|
196 | # frequencies = frequencies.json() | |
193 | frequencies = frequencies.get("Frecuencias") |
|
197 | # frequencies = frequencies.get("Frecuencias") | |
194 | f0 = frequencies.get("f0") |
|
198 | # f0 = frequencies.get("f0") | |
195 | f1 = frequencies.get("f1") |
|
199 | # f1 = frequencies.get("f1") | |
196 | f2 = frequencies.get("f2") |
|
200 | # f2 = frequencies.get("f2") | |
197 | f3 = frequencies.get("f3") |
|
201 | # f3 = frequencies.get("f3") | |
198 | print f0,f1,f2,f3 |
|
202 | # print f0,f1,f2,f3 | |
199 |
|
203 | # | ||
200 |
|
204 | # | ||
201 | if not response: |
|
205 | # if not response: | |
202 | messages.error(request, "Could not read parameters from Device") |
|
206 | # messages.error(request, "Could not read parameters from Device") | |
203 | return redirect('url_cgs_conf', id_conf=conf.id) |
|
207 | # return redirect('url_cgs_conf', id_conf=conf.id) | |
204 |
|
208 | # | ||
205 | data = {'experiment' : conf.experiment.id, |
|
209 | # data = {'experiment' : conf.experiment.id, | |
206 | 'device' : conf.device.id, |
|
210 | # 'device' : conf.device.id, | |
207 | 'freq0' : f0, |
|
211 | # 'freq0' : f0, | |
208 | 'freq1' : f1, |
|
212 | # 'freq1' : f1, | |
209 | 'freq2' : f2, |
|
213 | # 'freq2' : f2, | |
210 | 'freq3' : f3, |
|
214 | # 'freq3' : f3, | |
211 | } |
|
215 | # } | |
212 | except: |
|
216 | # except: | |
213 | messages.error(request, "Could not read parameters from Device") |
|
217 | # messages.error(request, "Could not read parameters from Device") | |
214 | data = {'experiment' : conf.experiment.id, |
|
218 | # data = {'experiment' : conf.experiment.id, | |
215 | 'device' : conf.device.id, |
|
219 | # 'device' : conf.device.id, | |
216 | 'freq0' : None, |
|
220 | # 'freq0' : None, | |
217 | 'freq1' : None, |
|
221 | # 'freq1' : None, | |
218 | 'freq2' : None, |
|
222 | # 'freq2' : None, | |
219 | 'freq3' : None, |
|
223 | # 'freq3' : None, | |
220 | } |
|
224 | # } | |
221 | return redirect('url_cgs_conf', id_conf=conf.id) |
|
225 | # return redirect('url_cgs_conf', id_conf=conf.id) | |
222 |
|
226 | # | ||
223 | form = CGSConfigurationForm(initial = data) |
|
227 | # form = CGSConfigurationForm(initial = data) | |
224 |
|
228 | # | ||
225 | kwargs = {} |
|
229 | # kwargs = {} | |
226 | kwargs['id_dev'] = conf.id |
|
230 | # kwargs['id_dev'] = conf.id | |
227 | kwargs['form'] = form |
|
231 | # kwargs['form'] = form | |
228 | kwargs['title'] = 'Device Configuration' |
|
232 | # kwargs['title'] = 'Device Configuration' | |
229 | kwargs['suptitle'] = 'Parameters read from device' |
|
233 | # kwargs['suptitle'] = 'Parameters read from device' | |
230 | kwargs['button'] = 'Save' |
|
234 | # kwargs['button'] = 'Save' | |
231 |
|
235 | # | ||
232 | ###### SIDEBAR ###### |
|
236 | # ###### SIDEBAR ###### | |
233 | kwargs.update(sidebar(conf)) |
|
237 | # kwargs.update(sidebar(conf)) | |
234 |
|
238 | # | ||
235 | return render(request, 'cgs_conf_edit.html', kwargs) |
|
239 | # return render(request, 'cgs_conf_edit.html', kwargs) | |
236 |
|
240 | # | ||
237 | def cgs_conf_import(request, id_conf): |
|
241 | # def cgs_conf_import(request, id_conf): | |
238 |
|
242 | # | ||
239 | conf = get_object_or_404(CGSConfiguration, pk=id_conf) |
|
243 | # conf = get_object_or_404(CGSConfiguration, pk=id_conf) | |
240 |
|
244 | # | ||
241 | if request.method == 'POST': |
|
245 | # if request.method == 'POST': | |
242 | file_form = UploadFileForm(request.POST, request.FILES) |
|
246 | # file_form = UploadFileForm(request.POST, request.FILES) | |
243 |
|
247 | # | ||
244 | if file_form.is_valid(): |
|
248 | # if file_form.is_valid(): | |
245 |
|
249 | # | ||
246 | try: |
|
250 | # try: | |
247 | if conf.update_from_file(request.FILES['file']): |
|
251 | # if conf.update_from_file(request.FILES['file']): | |
248 |
|
252 | # | ||
249 | try: |
|
253 | # try: | |
250 | conf.full_clean() |
|
254 | # conf.full_clean() | |
251 | except ValidationError as e: |
|
255 | # except ValidationError as e: | |
252 | messages.error(request, e) |
|
256 | # messages.error(request, e) | |
253 | else: |
|
257 | # else: | |
254 | conf.save() |
|
258 | # conf.save() | |
255 |
|
259 | # | ||
256 | messages.success(request, "Parameters imported from file: '%s'." %request.FILES['file'].name) |
|
260 | # messages.success(request, "Parameters imported from file: '%s'." %request.FILES['file'].name) | |
257 | #messages.warning(request,"") |
|
261 | # #messages.warning(request,"") | |
258 | return redirect('url_cgs_conf', id_conf=conf.id) |
|
262 | # return redirect('url_cgs_conf', id_conf=conf.id) | |
259 | except: |
|
263 | # except: | |
260 | messages.error(request, "No JSON object could be decoded.") |
|
264 | # messages.error(request, "No JSON object could be decoded.") | |
261 |
|
265 | # | ||
262 | messages.error(request, "Could not import parameters from file") |
|
266 | # messages.error(request, "Could not import parameters from file") | |
263 |
|
267 | # | ||
264 | else: |
|
268 | # else: | |
265 | file_form = UploadFileForm(initial={'title': '.json'}) |
|
269 | # file_form = UploadFileForm(initial={'title': '.json'}) | |
266 |
|
270 | # | ||
267 |
|
271 | # | ||
268 | kwargs = {} |
|
272 | # kwargs = {} | |
269 | kwargs['id_dev'] = conf.id |
|
273 | # kwargs['id_dev'] = conf.id | |
270 | kwargs['title'] = 'Device Configuration' |
|
274 | # kwargs['title'] = 'Device Configuration' | |
271 | kwargs['form'] = file_form |
|
275 | # kwargs['form'] = file_form | |
272 | kwargs['suptitle'] = 'Importing file' |
|
276 | # kwargs['suptitle'] = 'Importing file' | |
273 | kwargs['button'] = 'Import' |
|
277 | # kwargs['button'] = 'Import' | |
274 |
|
278 | # | ||
275 | kwargs.update(sidebar(conf)) |
|
279 | # kwargs.update(sidebar(conf)) | |
276 |
|
280 | # | ||
277 | return render(request, 'cgs_conf_import.html', kwargs) |
|
281 | # return render(request, 'cgs_conf_import.html', kwargs) | |
278 |
|
282 | # | ||
279 | def handle_uploaded_file(f): |
|
283 | # def handle_uploaded_file(f): | |
280 |
|
284 | # | ||
281 | data = {'freq0' : 62500000, |
|
285 | # data = {'freq0' : 62500000, | |
282 | 'freq1' : 62500000, |
|
286 | # 'freq1' : 62500000, | |
283 | 'freq2' : 62500000, |
|
287 | # 'freq2' : 62500000, | |
284 | 'freq3' : 62500000, |
|
288 | # 'freq3' : 62500000, | |
285 | } |
|
289 | # } | |
286 |
|
290 | # | ||
287 | return data |
|
291 | # return data | |
288 |
|
292 | # | ||
289 | def cgs_conf_export(request, id_conf): |
|
293 | # def cgs_conf_export(request, id_conf): | |
290 |
|
294 | # | ||
291 | conf = get_object_or_404(CGSConfiguration, pk=id_conf) |
|
295 | # conf = get_object_or_404(CGSConfiguration, pk=id_conf) | |
292 | ip=conf.device.ip_address |
|
296 | # ip=conf.device.ip_address | |
293 | port=conf.device.port_address |
|
297 | # port=conf.device.port_address | |
294 |
|
298 | # | ||
295 | if request.method=='GET': |
|
299 | # #if request.method=='GET': | |
296 | data = {"Frequencies": [ |
|
300 | # # data = {"Frequencies": [ | |
297 | ["freq0", conf.freq0], |
|
301 | # # ["freq0", conf.freq0], | |
298 | ["freq1", conf.freq1], |
|
302 | # # ["freq1", conf.freq1], | |
299 | ["freq2", conf.freq2], |
|
303 | # # ["freq2", conf.freq2], | |
300 | ["freq3", conf.freq3] |
|
304 | # # ["freq3", conf.freq3] | |
301 | ]} |
|
305 | # # ]} | |
302 | json_data = json.dumps(data) |
|
306 | # # json_data = json.dumps(data) | |
303 | conf.parameters = json_data |
|
307 | # # conf.parameters = json_data | |
304 | response = HttpResponse(conf.parameters, content_type="application/json") |
|
308 | # # response = HttpResponse(conf.parameters, content_type="application/json") | |
305 | response['Content-Disposition'] = 'attachment; filename="data.json"' |
|
309 | # # response['Content-Disposition'] = 'attachment; filename="data.json"' | |
306 |
|
310 | # | ||
307 | return response |
|
311 | # # return response | |
308 |
|
312 | # | ||
309 | kwargs = {} |
|
313 | # kwargs = {} | |
310 | kwargs['dev_conf'] = conf |
|
314 | # kwargs['dev_conf'] = conf | |
311 | kwargs['dev_conf_keys'] = ['experiment', 'device', |
|
315 | # kwargs['dev_conf_keys'] = ['experiment', 'device', | |
312 | 'freq0', 'freq1', |
|
316 | # 'freq0', 'freq1', | |
313 | 'freq2', 'freq3'] |
|
317 | # 'freq2', 'freq3'] | |
314 |
|
318 | # | ||
315 | kwargs['title'] = 'CGS Configuration' |
|
319 | # kwargs['title'] = 'CGS Configuration' | |
316 | kwargs['suptitle'] = 'Details' |
|
320 | # kwargs['suptitle'] = 'Details' | |
317 |
|
321 | # | ||
318 | kwargs['button'] = 'Edit Configuration' |
|
322 | # kwargs['button'] = 'Edit Configuration' | |
319 |
|
323 | # | ||
320 | ###### SIDEBAR ###### |
|
324 | # ###### SIDEBAR ###### | |
321 | kwargs.update(sidebar(conf)) |
|
325 | # kwargs.update(sidebar(conf)) | |
322 | return render(request, 'cgs_conf.html', kwargs) No newline at end of file |
|
326 | # return render(request, 'cgs_conf.html', kwargs) No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now