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