@@ -1,818 +1,818 | |||
|
1 | 1 | |
|
2 | 2 | import os |
|
3 | 3 | import json |
|
4 | 4 | import requests |
|
5 | 5 | import time |
|
6 | 6 | from datetime import datetime |
|
7 | 7 | |
|
8 | 8 | try: |
|
9 | 9 | from polymorphic.models import PolymorphicModel |
|
10 | 10 | except: |
|
11 | 11 | from polymorphic import PolymorphicModel |
|
12 | 12 | |
|
13 | 13 | from django.template.base import kwarg_re |
|
14 | 14 | from django.db import models |
|
15 | 15 | from django.urls import reverse |
|
16 | 16 | from django.core.validators import MinValueValidator, MaxValueValidator |
|
17 | 17 | from django.shortcuts import get_object_or_404 |
|
18 | 18 | from django.contrib.auth.models import User |
|
19 | 19 | from django.db.models.signals import post_save |
|
20 | 20 | from django.dispatch import receiver |
|
21 | 21 | |
|
22 | 22 | from apps.main.utils import Params |
|
23 | 23 | from apps.rc.utils import RCFile |
|
24 | 24 | from apps.jars.utils import RacpFile |
|
25 | 25 | from devices.dds import api as dds_api |
|
26 | 26 | from devices.dds import data as dds_data |
|
27 | 27 | |
|
28 | 28 | |
|
29 | 29 | DEV_PORTS = { |
|
30 | 30 | 'rc' : 2000, |
|
31 | 31 | 'dds' : 2000, |
|
32 | 32 | 'jars' : 2000, |
|
33 | 33 | 'usrp' : 2000, |
|
34 | 34 | 'cgs' : 8080, |
|
35 | 35 | 'abs' : 8080, |
|
36 | 36 | 'dds_rest': 80 |
|
37 | 37 | } |
|
38 | 38 | |
|
39 | 39 | RADAR_STATES = ( |
|
40 | 40 | (0, 'No connected'), |
|
41 | 41 | (1, 'Connected'), |
|
42 | 42 | (2, 'Configured'), |
|
43 | 43 | (3, 'Running'), |
|
44 | 44 | (4, 'Scheduled'), |
|
45 | 45 | ) |
|
46 | 46 | |
|
47 | 47 | EXPERIMENT_TYPE = ( |
|
48 | 48 | (0, 'RAW_DATA'), |
|
49 | 49 | (1, 'PDATA'), |
|
50 | 50 | ) |
|
51 | 51 | |
|
52 | 52 | DECODE_TYPE = ( |
|
53 | 53 | (0, 'None'), |
|
54 | 54 | (1, 'TimeDomain'), |
|
55 | 55 | (2, 'FreqDomain'), |
|
56 | 56 | (3, 'InvFreqDomain'), |
|
57 | 57 | ) |
|
58 | 58 | |
|
59 | 59 | DEV_STATES = ( |
|
60 | 60 | (0, 'No connected'), |
|
61 | 61 | (1, 'Connected'), |
|
62 | 62 | (2, 'Configured'), |
|
63 | 63 | (3, 'Running'), |
|
64 | 64 | (4, 'Unknown'), |
|
65 | 65 | ) |
|
66 | 66 | |
|
67 | 67 | DEV_TYPES = ( |
|
68 | 68 | ('', 'Select a device type'), |
|
69 | 69 | ('rc', 'Radar Controller'), |
|
70 | 70 | ('dds', 'Direct Digital Synthesizer'), |
|
71 | 71 | ('jars', 'Jicamarca Radar Acquisition System'), |
|
72 | 72 | ('usrp', 'Universal Software Radio Peripheral'), |
|
73 | 73 | ('cgs', 'Clock Generator System'), |
|
74 | 74 | ('abs', 'Automatic Beam Switching'), |
|
75 | 75 | ('dds_rest', 'Direct Digital Synthesizer_REST'), |
|
76 | 76 | ) |
|
77 | 77 | |
|
78 | 78 | EXP_STATES = ( |
|
79 | 79 | (0,'Error'), #RED |
|
80 | 80 | (1,'Cancelled'), #YELLOW |
|
81 | 81 | (2,'Running'), #GREEN |
|
82 | 82 | (3,'Scheduled'), #BLUE |
|
83 | 83 | (4,'Unknown'), #WHITE |
|
84 | 84 | ) |
|
85 | 85 | |
|
86 | 86 | CONF_TYPES = ( |
|
87 | 87 | (0, 'Active'), |
|
88 | 88 | (1, 'Historical'), |
|
89 | 89 | ) |
|
90 | 90 | |
|
91 | 91 | class Profile(models.Model): |
|
92 | 92 | user = models.OneToOneField(User, on_delete=models.CASCADE) |
|
93 | 93 | theme = models.CharField(max_length=30, default='spacelab') |
|
94 | 94 | |
|
95 | 95 | |
|
96 | 96 | @receiver(post_save, sender=User) |
|
97 | 97 | def create_user_profile(sender, instance, created, **kwargs): |
|
98 | 98 | if created: |
|
99 | 99 | Profile.objects.create(user=instance) |
|
100 | 100 | |
|
101 | 101 | @receiver(post_save, sender=User) |
|
102 | 102 | def save_user_profile(sender, instance, **kwargs): |
|
103 | 103 | instance.profile.save() |
|
104 | 104 | |
|
105 | 105 | |
|
106 | 106 | class Location(models.Model): |
|
107 | 107 | |
|
108 | 108 | name = models.CharField(max_length = 30) |
|
109 | 109 | description = models.TextField(blank=True, null=True) |
|
110 | 110 | |
|
111 | 111 | class Meta: |
|
112 | 112 | db_table = 'db_location' |
|
113 | 113 | |
|
114 | 114 | def __str__(self): |
|
115 | 115 | return u'%s' % self.name |
|
116 | 116 | |
|
117 | 117 | def get_absolute_url(self): |
|
118 | 118 | return reverse('url_location', args=[str(self.id)]) |
|
119 | 119 | |
|
120 | 120 | |
|
121 | 121 | class DeviceType(models.Model): |
|
122 | 122 | |
|
123 | 123 | name = models.CharField(max_length = 10, choices = DEV_TYPES, default = 'dds_rest') |
|
124 | 124 | sequence = models.PositiveSmallIntegerField(default=55) |
|
125 | 125 | description = models.TextField(blank=True, null=True) |
|
126 | 126 | |
|
127 | 127 | class Meta: |
|
128 | 128 | db_table = 'db_device_types' |
|
129 | 129 | |
|
130 | 130 | def __str__(self): |
|
131 | 131 | return u'%s' % self.get_name_display() |
|
132 | 132 | |
|
133 | 133 | class Device(models.Model): |
|
134 | 134 | |
|
135 | 135 | device_type = models.ForeignKey('DeviceType', on_delete=models.CASCADE) |
|
136 | 136 | location = models.ForeignKey('Location', on_delete=models.CASCADE) |
|
137 | 137 | ip_address = models.GenericIPAddressField(protocol='IPv4', default='0.0.0.0') |
|
138 | 138 | port_address = models.PositiveSmallIntegerField(default=2000) |
|
139 | 139 | description = models.TextField(blank=True, null=True) |
|
140 | 140 | status = models.PositiveSmallIntegerField(default=4, choices=DEV_STATES) |
|
141 | 141 | conf_active = models.PositiveIntegerField(default=0, verbose_name='Current configuration') |
|
142 | 142 | |
|
143 | 143 | class Meta: |
|
144 | 144 | db_table = 'db_devices' |
|
145 | 145 | |
|
146 | 146 | def __str__(self): |
|
147 | 147 | ret = u'{} [{}]'.format(self.device_type.name.upper(), self.location.name) |
|
148 | 148 | |
|
149 | 149 | return ret |
|
150 | 150 | |
|
151 | 151 | @property |
|
152 | 152 | def name(self): |
|
153 | 153 | return str(self) |
|
154 | 154 | |
|
155 | 155 | def get_status(self): |
|
156 | 156 | return self.status |
|
157 | 157 | |
|
158 | 158 | @property |
|
159 | 159 | def status_color(self): |
|
160 | 160 | color = 'muted' |
|
161 | 161 | if self.status == 0: |
|
162 | 162 | color = "danger" |
|
163 | 163 | elif self.status == 1: |
|
164 | 164 | color = "warning" |
|
165 | 165 | elif self.status == 2: |
|
166 | 166 | color = "info" |
|
167 | 167 | elif self.status == 3: |
|
168 | 168 | color = "success" |
|
169 | 169 | |
|
170 | 170 | return color |
|
171 | 171 | |
|
172 | 172 | def url(self, path=None): |
|
173 | 173 | |
|
174 | 174 | if path: |
|
175 | 175 | return 'http://{}:{}/{}/'.format(self.ip_address, self.port_address, path) |
|
176 | 176 | else: |
|
177 | 177 | return 'http://{}:{}/'.format(self.ip_address, self.port_address) |
|
178 | 178 | |
|
179 | 179 | def get_absolute_url(self): |
|
180 | 180 | return reverse('url_device', args=[str(self.id)]) |
|
181 | 181 | |
|
182 | 182 | def get_absolute_url_edit(self): |
|
183 | 183 | return reverse('url_edit_device', args=[str(self.id)]) |
|
184 | 184 | |
|
185 | 185 | def get_absolute_url_delete(self): |
|
186 | 186 | return reverse('url_delete_device', args=[str(self.id)]) |
|
187 | 187 | |
|
188 | 188 | def change_ip(self, ip_address, mask, gateway, dns, **kwargs): |
|
189 | 189 | |
|
190 | 190 | if self.device_type.name=='dds': |
|
191 | 191 | try: |
|
192 | 192 | answer = dds_api.change_ip(ip = self.ip_address, |
|
193 | 193 | port = self.port_address, |
|
194 | 194 | new_ip = ip_address, |
|
195 | 195 | mask = mask, |
|
196 | 196 | gateway = gateway) |
|
197 | 197 | if answer[0]=='1': |
|
198 | 198 | self.message = '25|DDS - {}'.format(answer) |
|
199 | 199 | self.ip_address = ip_address |
|
200 | 200 | self.save() |
|
201 | 201 | else: |
|
202 | 202 | self.message = '30|DDS - {}'.format(answer) |
|
203 | 203 | return False |
|
204 | 204 | except Exception as e: |
|
205 | 205 | self.message = '40|{}'.format(str(e)) |
|
206 | 206 | return False |
|
207 | 207 | |
|
208 | 208 | elif self.device_type.name=='rc': |
|
209 | 209 | headers = {'content-type': "application/json", |
|
210 | 210 | 'cache-control': "no-cache"} |
|
211 | 211 | |
|
212 | 212 | ip = [int(x) for x in ip_address.split('.')] |
|
213 | 213 | dns = [int(x) for x in dns.split('.')] |
|
214 | 214 | gateway = [int(x) for x in gateway.split('.')] |
|
215 | 215 | subnet = [int(x) for x in mask.split('.')] |
|
216 | 216 | |
|
217 | 217 | payload = { |
|
218 | 218 | "ip": ip, |
|
219 | 219 | "dns": dns, |
|
220 | 220 | "gateway": gateway, |
|
221 | 221 | "subnet": subnet |
|
222 | 222 | } |
|
223 | 223 | |
|
224 | 224 | req = requests.post(self.url('changeip'), data=json.dumps(payload), headers=headers) |
|
225 | 225 | try: |
|
226 | 226 | answer = req.json() |
|
227 | 227 | if answer['changeip']=='ok': |
|
228 | 228 | self.message = '25|IP succesfully changed' |
|
229 | 229 | self.ip_address = ip_address |
|
230 | 230 | self.save() |
|
231 | 231 | else: |
|
232 | 232 | self.message = '30|An error ocuur when changing IP' |
|
233 | 233 | except Exception as e: |
|
234 | 234 | self.message = '40|{}'.format(str(e)) |
|
235 | 235 | else: |
|
236 | 236 | self.message = 'Not implemented' |
|
237 | 237 | return False |
|
238 | 238 | |
|
239 | 239 | return True |
|
240 | 240 | |
|
241 | 241 | |
|
242 | 242 | class Campaign(models.Model): |
|
243 | 243 | |
|
244 | 244 | template = models.BooleanField(default=False) |
|
245 | 245 | name = models.CharField(max_length=60, unique=True) |
|
246 | 246 | start_date = models.DateTimeField(blank=True, null=True) |
|
247 | 247 | end_date = models.DateTimeField(blank=True, null=True) |
|
248 | 248 | tags = models.CharField(max_length=40, blank=True, null=True) |
|
249 | 249 | description = models.TextField(blank=True, null=True) |
|
250 | 250 | experiments = models.ManyToManyField('Experiment', blank=True) |
|
251 | 251 | author = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE) |
|
252 | 252 | |
|
253 | 253 | class Meta: |
|
254 | 254 | db_table = 'db_campaigns' |
|
255 | 255 | ordering = ('name',) |
|
256 | 256 | |
|
257 | 257 | def __str__(self): |
|
258 | 258 | if self.template: |
|
259 | 259 | return u'{} (template)'.format(self.name) |
|
260 | 260 | else: |
|
261 | 261 | return u'{}'.format(self.name) |
|
262 | 262 | |
|
263 | 263 | def jsonify(self): |
|
264 | 264 | |
|
265 | 265 | data = {} |
|
266 | 266 | |
|
267 | 267 | ignored = ('template') |
|
268 | 268 | |
|
269 | 269 | for field in self._meta.fields: |
|
270 | 270 | if field.name in ignored: |
|
271 | 271 | continue |
|
272 | 272 | data[field.name] = field.value_from_object(self) |
|
273 | 273 | |
|
274 | 274 | data['start_date'] = data['start_date'].strftime('%Y-%m-%d') |
|
275 | 275 | data['end_date'] = data['end_date'].strftime('%Y-%m-%d') |
|
276 | 276 | |
|
277 | 277 | return data |
|
278 | 278 | |
|
279 | 279 | def parms_to_dict(self): |
|
280 | 280 | |
|
281 | 281 | params = Params({}) |
|
282 | 282 | params.add(self.jsonify(), 'campaigns') |
|
283 | 283 | |
|
284 | 284 | for exp in Experiment.objects.filter(campaign = self): |
|
285 | 285 | params.add(exp.jsonify(), 'experiments') |
|
286 | 286 | configurations = Configuration.objects.filter(experiment=exp, type=0) |
|
287 | 287 | |
|
288 | 288 | for conf in configurations: |
|
289 | 289 | params.add(conf.jsonify(), 'configurations') |
|
290 | 290 | if conf.device.device_type.name=='rc': |
|
291 | 291 | for line in conf.get_lines(): |
|
292 | 292 | params.add(line.jsonify(), 'lines') |
|
293 | 293 | |
|
294 | 294 | return params.data |
|
295 | 295 | |
|
296 | 296 | def dict_to_parms(self, parms, CONF_MODELS): |
|
297 | 297 | |
|
298 | 298 | experiments = Experiment.objects.filter(campaign = self) |
|
299 | 299 | |
|
300 | 300 | if experiments: |
|
301 | 301 | for experiment in experiments: |
|
302 | 302 | experiment.delete() |
|
303 | 303 | |
|
304 | 304 | for id_exp in parms['experiments']['allIds']: |
|
305 | 305 | exp_parms = parms['experiments']['byId'][id_exp] |
|
306 | 306 | dum = (datetime.now() - datetime(1970, 1, 1)).total_seconds() |
|
307 | 307 | exp = Experiment(name='{}'.format(dum)) |
|
308 | 308 | exp.save() |
|
309 | 309 | exp.dict_to_parms(parms, CONF_MODELS, id_exp=id_exp) |
|
310 | 310 | self.experiments.add(exp) |
|
311 | 311 | |
|
312 | 312 | camp_parms = parms['campaigns']['byId'][parms['campaigns']['allIds'][0]] |
|
313 | 313 | |
|
314 | 314 | self.name = '{}-{}'.format(camp_parms['name'], datetime.now().strftime('%y%m%d')) |
|
315 | 315 | self.start_date = camp_parms['start_date'] |
|
316 | 316 | self.end_date = camp_parms['end_date'] |
|
317 | 317 | self.tags = camp_parms['tags'] |
|
318 | 318 | self.save() |
|
319 | 319 | |
|
320 | 320 | return self |
|
321 | 321 | |
|
322 | 322 | def get_experiments_by_radar(self, radar=None): |
|
323 | 323 | |
|
324 | 324 | ret = [] |
|
325 | 325 | if radar: |
|
326 | 326 | locations = Location.objects.filter(pk=radar) |
|
327 | 327 | else: |
|
328 | 328 | locations = set([e.location for e in self.experiments.all()]) |
|
329 | 329 | |
|
330 | 330 | for loc in locations: |
|
331 | 331 | dum = {} |
|
332 | 332 | dum['name'] = loc.name |
|
333 | 333 | dum['id'] = loc.pk |
|
334 | 334 | dum['experiments'] = [e for e in self.experiments.all() if e.location==loc] |
|
335 | 335 | ret.append(dum) |
|
336 | 336 | |
|
337 | 337 | return ret |
|
338 | 338 | |
|
339 | 339 | def get_absolute_url(self): |
|
340 | 340 | return reverse('url_campaign', args=[str(self.id)]) |
|
341 | 341 | |
|
342 | 342 | def get_absolute_url_edit(self): |
|
343 | 343 | return reverse('url_edit_campaign', args=[str(self.id)]) |
|
344 | 344 | |
|
345 | 345 | def get_absolute_url_delete(self): |
|
346 | 346 | return reverse('url_delete_campaign', args=[str(self.id)]) |
|
347 | 347 | |
|
348 | 348 | def get_absolute_url_export(self): |
|
349 | 349 | return reverse('url_export_campaign', args=[str(self.id)]) |
|
350 | 350 | |
|
351 | 351 | def get_absolute_url_import(self): |
|
352 | 352 | return reverse('url_import_campaign', args=[str(self.id)]) |
|
353 | 353 | |
|
354 | 354 | |
|
355 | 355 | class RunningExperiment(models.Model): |
|
356 | 356 | radar = models.OneToOneField('Location', on_delete=models.CASCADE) |
|
357 | 357 | running_experiment = models.ManyToManyField('Experiment', blank = True) |
|
358 | 358 | status = models.PositiveSmallIntegerField(default=0, choices=RADAR_STATES) |
|
359 | 359 | |
|
360 | 360 | |
|
361 | 361 | class Experiment(models.Model): |
|
362 | 362 | |
|
363 | 363 | template = models.BooleanField(default=False) |
|
364 | 364 | name = models.CharField(max_length=40, default='', unique=True) |
|
365 | 365 | location = models.ForeignKey('Location', null=True, blank=True, on_delete=models.CASCADE) |
|
366 | 366 | freq = models.FloatField(verbose_name='Operating Freq. (MHz)', validators=[MinValueValidator(1), MaxValueValidator(10000)], default=49.9200) |
|
367 | 367 | start_time = models.TimeField(default='00:00:00') |
|
368 | 368 | end_time = models.TimeField(default='23:59:59') |
|
369 | 369 | task = models.CharField(max_length=36, default='', blank=True, null=True) |
|
370 | 370 | status = models.PositiveSmallIntegerField(default=4, choices=EXP_STATES) |
|
371 | 371 | author = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE) |
|
372 | 372 | hash = models.CharField(default='', max_length=64, null=True, blank=True) |
|
373 | 373 | |
|
374 | 374 | class Meta: |
|
375 | 375 | db_table = 'db_experiments' |
|
376 | 376 | ordering = ('template', 'name') |
|
377 | 377 | |
|
378 | 378 | def __str__(self): |
|
379 | 379 | if self.template: |
|
380 | 380 | return u'%s (template)' % (self.name) |
|
381 | 381 | else: |
|
382 | 382 | return u'%s' % (self.name) |
|
383 | 383 | |
|
384 | 384 | def jsonify(self): |
|
385 | 385 | |
|
386 | 386 | data = {} |
|
387 | 387 | |
|
388 | 388 | ignored = ('template') |
|
389 | 389 | |
|
390 | 390 | for field in self._meta.fields: |
|
391 | 391 | if field.name in ignored: |
|
392 | 392 | continue |
|
393 | 393 | data[field.name] = field.value_from_object(self) |
|
394 | 394 | |
|
395 | 395 | data['start_time'] = data['start_time'].strftime('%H:%M:%S') |
|
396 | 396 | data['end_time'] = data['end_time'].strftime('%H:%M:%S') |
|
397 | 397 | data['location'] = self.location.name |
|
398 | 398 | data['configurations'] = ['{}'.format(conf.pk) for |
|
399 | 399 | conf in Configuration.objects.filter(experiment=self, type=0)] |
|
400 | 400 | |
|
401 | 401 | return data |
|
402 | 402 | |
|
403 | 403 | @property |
|
404 | 404 | def radar_system(self): |
|
405 | 405 | return self.location |
|
406 | 406 | |
|
407 | 407 | def clone(self, **kwargs): |
|
408 | 408 | |
|
409 | 409 | confs = Configuration.objects.filter(experiment=self, type=0) |
|
410 | 410 | self.pk = None |
|
411 | self.name = '{}_{:%y%m%d}'.format(self.name, datetime.now()) | |
|
411 | self.name = '{}_{:%y%m%d%H%M%S}'.format(self.name, datetime.now()) | |
|
412 | 412 | for attr, value in kwargs.items(): |
|
413 | 413 | setattr(self, attr, value) |
|
414 | 414 | |
|
415 | 415 | self.save() |
|
416 | 416 | |
|
417 | 417 | for conf in confs: |
|
418 | 418 | conf.clone(experiment=self, template=False) |
|
419 | 419 | |
|
420 | 420 | return self |
|
421 | 421 | |
|
422 | 422 | def start(self): |
|
423 | 423 | ''' |
|
424 | 424 | Configure and start experiments's devices |
|
425 | 425 | ABS-CGS-DDS-RC-JARS |
|
426 | 426 | ''' |
|
427 | 427 | |
|
428 | 428 | confs = [] |
|
429 | 429 | allconfs = Configuration.objects.filter(experiment=self, type = 0).order_by('-device__device_type__sequence') |
|
430 | 430 | rc_mix = [conf for conf in allconfs if conf.device.device_type.name=='rc' and conf.mix] |
|
431 | 431 | if rc_mix: |
|
432 | 432 | for conf in allconfs: |
|
433 | 433 | if conf.device.device_type.name == 'rc' and not conf.mix: |
|
434 | 434 | continue |
|
435 | 435 | confs.append(conf) |
|
436 | 436 | else: |
|
437 | 437 | confs = allconfs |
|
438 | 438 | |
|
439 | 439 | print("confs: ",confs) |
|
440 | 440 | #try: |
|
441 | 441 | for conf in confs: |
|
442 | 442 | print("conf->",conf) |
|
443 | 443 | conf.stop_device() |
|
444 | 444 | conf.write_device() |
|
445 | 445 | conf.device.conf_active = conf.pk |
|
446 | 446 | conf.device.save() |
|
447 | 447 | conf.start_device() |
|
448 | 448 | time.sleep(1) |
|
449 | 449 | #except: |
|
450 | 450 | #return 0 |
|
451 | 451 | return 2 |
|
452 | 452 | |
|
453 | 453 | |
|
454 | 454 | def stop(self): |
|
455 | 455 | ''' |
|
456 | 456 | Stop experiments's devices |
|
457 | 457 | DDS-JARS-RC-CGS-ABS |
|
458 | 458 | ''' |
|
459 | 459 | |
|
460 | 460 | confs = Configuration.objects.filter(experiment=self, type = 0).order_by('device__device_type__sequence') |
|
461 | 461 | confs = confs.exclude(device__device_type__name='cgs') |
|
462 | 462 | try: |
|
463 | 463 | for conf in confs: |
|
464 | 464 | conf.stop_device() |
|
465 | 465 | except: |
|
466 | 466 | return 0 |
|
467 | 467 | return 1 |
|
468 | 468 | |
|
469 | 469 | def get_status(self): |
|
470 | 470 | |
|
471 | 471 | if self.status == 3: |
|
472 | 472 | return |
|
473 | 473 | |
|
474 | 474 | confs = Configuration.objects.filter(experiment=self, type=0) |
|
475 | 475 | |
|
476 | 476 | for conf in confs: |
|
477 | 477 | conf.status_device() |
|
478 | 478 | |
|
479 | 479 | total = confs.aggregate(models.Sum('device__status'))['device__status__sum'] |
|
480 | 480 | |
|
481 | 481 | if total==2*confs.count(): |
|
482 | 482 | status = 1 |
|
483 | 483 | elif total == 3*confs.count(): |
|
484 | 484 | status = 2 |
|
485 | 485 | else: |
|
486 | 486 | status = 0 |
|
487 | 487 | |
|
488 | 488 | self.status = status |
|
489 | 489 | self.save() |
|
490 | 490 | |
|
491 | 491 | def status_color(self): |
|
492 | 492 | color = 'muted' |
|
493 | 493 | if self.status == 0: |
|
494 | 494 | color = "danger" |
|
495 | 495 | elif self.status == 1: |
|
496 | 496 | color = "warning" |
|
497 | 497 | elif self.status == 2: |
|
498 | 498 | color = "success" |
|
499 | 499 | elif self.status == 3: |
|
500 | 500 | color = "info" |
|
501 | 501 | |
|
502 | 502 | return color |
|
503 | 503 | |
|
504 | 504 | def parms_to_dict(self): |
|
505 | 505 | |
|
506 | 506 | params = Params({}) |
|
507 | 507 | params.add(self.jsonify(), 'experiments') |
|
508 | 508 | |
|
509 | 509 | configurations = Configuration.objects.filter(experiment=self, type=0) |
|
510 | 510 | |
|
511 | 511 | for conf in configurations: |
|
512 | 512 | params.add(conf.jsonify(), 'configurations') |
|
513 | 513 | if conf.device.device_type.name=='rc': |
|
514 | 514 | for line in conf.get_lines(): |
|
515 | 515 | params.add(line.jsonify(), 'lines') |
|
516 | 516 | |
|
517 | 517 | return params.data |
|
518 | 518 | |
|
519 | 519 | def dict_to_parms(self, parms, CONF_MODELS, id_exp=None): |
|
520 | 520 | |
|
521 | 521 | configurations = Configuration.objects.filter(experiment=self) |
|
522 | 522 | |
|
523 | 523 | if id_exp is not None: |
|
524 | 524 | exp_parms = parms['experiments']['byId'][id_exp] |
|
525 | 525 | else: |
|
526 | 526 | exp_parms = parms['experiments']['byId'][parms['experiments']['allIds'][0]] |
|
527 | 527 | |
|
528 | 528 | if configurations: |
|
529 | 529 | for configuration in configurations: |
|
530 | 530 | configuration.delete() |
|
531 | 531 | |
|
532 | 532 | for id_conf in exp_parms['configurations']: |
|
533 | 533 | conf_parms = parms['configurations']['byId'][id_conf] |
|
534 | 534 | device = Device.objects.filter(device_type__name=conf_parms['device_type'])[0] |
|
535 | 535 | model = CONF_MODELS[conf_parms['device_type']] |
|
536 | 536 | conf = model( |
|
537 | 537 | experiment = self, |
|
538 | 538 | device = device, |
|
539 | 539 | ) |
|
540 | 540 | conf.dict_to_parms(parms, id=id_conf) |
|
541 | 541 | |
|
542 | 542 | |
|
543 | 543 | location, created = Location.objects.get_or_create(name=exp_parms['location']) |
|
544 | 544 | self.name = '{}-{}'.format(exp_parms['name'], datetime.now().strftime('%y%m%d')) |
|
545 | 545 | self.location = location |
|
546 | 546 | self.start_time = exp_parms['start_time'] |
|
547 | 547 | self.end_time = exp_parms['end_time'] |
|
548 | 548 | self.save() |
|
549 | 549 | |
|
550 | 550 | return self |
|
551 | 551 | |
|
552 | 552 | def get_absolute_url(self): |
|
553 | 553 | return reverse('url_experiment', args=[str(self.id)]) |
|
554 | 554 | |
|
555 | 555 | def get_absolute_url_edit(self): |
|
556 | 556 | return reverse('url_edit_experiment', args=[str(self.id)]) |
|
557 | 557 | |
|
558 | 558 | def get_absolute_url_delete(self): |
|
559 | 559 | return reverse('url_delete_experiment', args=[str(self.id)]) |
|
560 | 560 | |
|
561 | 561 | def get_absolute_url_import(self): |
|
562 | 562 | return reverse('url_import_experiment', args=[str(self.id)]) |
|
563 | 563 | |
|
564 | 564 | def get_absolute_url_export(self): |
|
565 | 565 | return reverse('url_export_experiment', args=[str(self.id)]) |
|
566 | 566 | |
|
567 | 567 | def get_absolute_url_start(self): |
|
568 | 568 | return reverse('url_start_experiment', args=[str(self.id)]) |
|
569 | 569 | |
|
570 | 570 | def get_absolute_url_stop(self): |
|
571 | 571 | return reverse('url_stop_experiment', args=[str(self.id)]) |
|
572 | 572 | |
|
573 | 573 | |
|
574 | 574 | class Configuration(PolymorphicModel): |
|
575 | 575 | |
|
576 | 576 | template = models.BooleanField(default=False) |
|
577 | 577 | # name = models.CharField(verbose_name="Configuration Name", max_length=40, default='') |
|
578 | 578 | device = models.ForeignKey('Device', verbose_name='Device', null=True, on_delete=models.CASCADE) |
|
579 | 579 | label = models.CharField(verbose_name="Label", max_length=40, default='', blank=True, null=True) |
|
580 | 580 | experiment = models.ForeignKey('Experiment', verbose_name='Experiment', null=True, blank=True, on_delete=models.CASCADE) |
|
581 | 581 | type = models.PositiveSmallIntegerField(default=0, choices=CONF_TYPES) |
|
582 | 582 | created_date = models.DateTimeField(auto_now_add=True) |
|
583 | 583 | programmed_date = models.DateTimeField(auto_now=True) |
|
584 | 584 | parameters = models.TextField(default='{}') |
|
585 | 585 | author = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE) |
|
586 | 586 | hash = models.CharField(default='', max_length=64, null=True, blank=True) |
|
587 | 587 | message = "" |
|
588 | 588 | |
|
589 | 589 | class Meta: |
|
590 | 590 | db_table = 'db_configurations' |
|
591 | 591 | ordering = ('device__device_type__name',) |
|
592 | 592 | |
|
593 | 593 | def __str__(self): |
|
594 | 594 | |
|
595 | 595 | ret = u'{} '.format(self.device.device_type.name.upper()) |
|
596 | 596 | |
|
597 | 597 | if 'mix' in [f.name for f in self._meta.get_fields()]: |
|
598 | 598 | if self.mix: |
|
599 | 599 | ret = '{} MIX '.format(self.device.device_type.name.upper()) |
|
600 | 600 | |
|
601 | 601 | if 'label' in [f.name for f in self._meta.get_fields()]: |
|
602 | 602 | ret += '{}'.format(self.label) |
|
603 | 603 | |
|
604 | 604 | if self.template: |
|
605 | 605 | ret += ' (template)' |
|
606 | 606 | |
|
607 | 607 | return ret |
|
608 | 608 | |
|
609 | 609 | @property |
|
610 | 610 | def name(self): |
|
611 | 611 | |
|
612 | 612 | return str(self) |
|
613 | 613 | |
|
614 | 614 | def jsonify(self): |
|
615 | 615 | |
|
616 | 616 | data = {} |
|
617 | 617 | |
|
618 | 618 | ignored = ('type', 'polymorphic_ctype', 'configuration_ptr', |
|
619 | 619 | 'created_date', 'programmed_date', 'template', 'device', |
|
620 | 620 | 'experiment') |
|
621 | 621 | |
|
622 | 622 | for field in self._meta.fields: |
|
623 | 623 | if field.name in ignored: |
|
624 | 624 | continue |
|
625 | 625 | data[field.name] = field.value_from_object(self) |
|
626 | 626 | |
|
627 | 627 | data['device_type'] = self.device.device_type.name |
|
628 | 628 | |
|
629 | 629 | if self.device.device_type.name == 'rc': |
|
630 | 630 | data['lines'] = ['{}'.format(line.pk) for line in self.get_lines()] |
|
631 | 631 | data['delays'] = self.get_delays() |
|
632 | 632 | data['pulses'] = self.get_pulses() |
|
633 | 633 | |
|
634 | 634 | elif self.device.device_type.name == 'jars': |
|
635 | 635 | data['decode_type'] = DECODE_TYPE[self.decode_data][1] |
|
636 | 636 | |
|
637 | 637 | elif self.device.device_type.name == 'dds': |
|
638 | 638 | data['frequencyA_Mhz'] = float(data['frequencyA_Mhz']) |
|
639 | 639 | data['frequencyB_Mhz'] = float(data['frequencyB_Mhz']) |
|
640 | 640 | data['phaseA'] = dds_data.phase_to_binary(data['phaseA_degrees']) |
|
641 | 641 | data['phaseB'] = dds_data.phase_to_binary(data['phaseB_degrees']) |
|
642 | 642 | |
|
643 | 643 | elif self.device.device_type.name == 'dds_rest': |
|
644 | 644 | data['frequencyA_Mhz'] = float(data['frequencyA_Mhz']) |
|
645 | 645 | data['frequencyB_Mhz'] = float(data['frequencyB_Mhz']) |
|
646 | 646 | data['phaseA'] = dds_data.phase_to_binary(data['phaseA_degrees']) |
|
647 | 647 | data['phaseB'] = dds_data.phase_to_binary(data['phaseB_degrees']) |
|
648 | 648 | data['delta_frequency_Mhz'] = float(data['delta_frequency_Mhz'] or 0.00) |
|
649 | 649 | data['update_clock_Mhz'] = float(data['update_clock_Mhz'] or 0.00) |
|
650 | 650 | data['ramp_rate_clock_Mhz'] = float(data['ramp_rate_clock_Mhz'] or 0.0) |
|
651 | 651 | return data |
|
652 | 652 | |
|
653 | 653 | def clone(self, **kwargs): |
|
654 | 654 | |
|
655 | 655 | self.pk = None |
|
656 | 656 | self.id = None |
|
657 | 657 | for attr, value in kwargs.items(): |
|
658 | 658 | setattr(self, attr, value) |
|
659 | 659 | |
|
660 | 660 | self.save() |
|
661 | 661 | |
|
662 | 662 | return self |
|
663 | 663 | |
|
664 | 664 | def parms_to_dict(self): |
|
665 | 665 | |
|
666 | 666 | params = Params({}) |
|
667 | 667 | params.add(self.jsonify(), 'configurations') |
|
668 | 668 | |
|
669 | 669 | if self.device.device_type.name=='rc': |
|
670 | 670 | for line in self.get_lines(): |
|
671 | 671 | params.add(line.jsonify(), 'lines') |
|
672 | 672 | |
|
673 | 673 | return params.data |
|
674 | 674 | |
|
675 | 675 | def parms_to_text(self): |
|
676 | 676 | |
|
677 | 677 | raise NotImplementedError("This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper()) |
|
678 | 678 | |
|
679 | 679 | |
|
680 | 680 | def parms_to_binary(self): |
|
681 | 681 | |
|
682 | 682 | raise NotImplementedError("This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper()) |
|
683 | 683 | |
|
684 | 684 | |
|
685 | 685 | def dict_to_parms(self, parameters, id=None): |
|
686 | 686 | |
|
687 | 687 | params = Params(parameters) |
|
688 | 688 | |
|
689 | 689 | if id: |
|
690 | 690 | data = params.get_conf(id_conf=id) |
|
691 | 691 | else: |
|
692 | 692 | data = params.get_conf(dtype=self.device.device_type.name) |
|
693 | 693 | |
|
694 | 694 | if data['device_type']=='rc': |
|
695 | 695 | self.clean_lines() |
|
696 | 696 | lines = data.pop('lines', None) |
|
697 | 697 | for line_id in lines: |
|
698 | 698 | pass |
|
699 | 699 | |
|
700 | 700 | for key, value in data.items(): |
|
701 | 701 | if key not in ('id', 'device_type'): |
|
702 | 702 | setattr(self, key, value) |
|
703 | 703 | |
|
704 | 704 | self.save() |
|
705 | 705 | |
|
706 | 706 | |
|
707 | 707 | def export_to_file(self, format="json"): |
|
708 | 708 | |
|
709 | 709 | content_type = '' |
|
710 | 710 | |
|
711 | 711 | if format == 'racp': |
|
712 | 712 | content_type = 'text/plain' |
|
713 | 713 | filename = '%s_%s.%s' %(self.device.device_type.name, self.name, 'racp') |
|
714 | 714 | content = self.parms_to_text(file_format = 'racp') |
|
715 | 715 | |
|
716 | 716 | if format == 'text': |
|
717 | 717 | content_type = 'text/plain' |
|
718 | 718 | filename = '%s_%s.%s' %(self.device.device_type.name, self.name, self.device.device_type.name) |
|
719 | 719 | content = self.parms_to_text() |
|
720 | 720 | |
|
721 | 721 | if format == 'binary': |
|
722 | 722 | content_type = 'application/octet-stream' |
|
723 | 723 | filename = '%s_%s.bin' %(self.device.device_type.name, self.name) |
|
724 | 724 | content = self.parms_to_binary() |
|
725 | 725 | |
|
726 | 726 | if not content_type: |
|
727 | 727 | content_type = 'application/json' |
|
728 | 728 | filename = '%s_%s.json' %(self.device.device_type.name, self.name) |
|
729 | 729 | content = json.dumps(self.parms_to_dict(), indent=2) |
|
730 | 730 | |
|
731 | 731 | fields = {'content_type':content_type, |
|
732 | 732 | 'filename':filename, |
|
733 | 733 | 'content':content |
|
734 | 734 | } |
|
735 | 735 | |
|
736 | 736 | return fields |
|
737 | 737 | |
|
738 | 738 | def import_from_file(self, fp): |
|
739 | 739 | |
|
740 | 740 | parms = {} |
|
741 | 741 | |
|
742 | 742 | path, ext = os.path.splitext(fp.name) |
|
743 | 743 | |
|
744 | 744 | if ext == '.json': |
|
745 | 745 | parms = json.load(fp) |
|
746 | 746 | |
|
747 | 747 | if ext == '.dds': |
|
748 | 748 | lines = fp.readlines() |
|
749 | 749 | parms = dds_data.text_to_dict(lines) |
|
750 | 750 | |
|
751 | 751 | if ext == '.racp': |
|
752 | 752 | if self.device.device_type.name == 'jars': |
|
753 | 753 | parms = RacpFile(fp).to_dict() |
|
754 | 754 | parms['filter_parms'] = json.loads(self.filter_parms) |
|
755 | 755 | return parms |
|
756 | 756 | parms = RCFile(fp).to_dict() |
|
757 | 757 | |
|
758 | 758 | return parms |
|
759 | 759 | |
|
760 | 760 | def status_device(self): |
|
761 | 761 | |
|
762 | 762 | self.message = 'Function not implemented' |
|
763 | 763 | return False |
|
764 | 764 | |
|
765 | 765 | |
|
766 | 766 | def stop_device(self): |
|
767 | 767 | |
|
768 | 768 | self.message = 'Function not implemented' |
|
769 | 769 | return False |
|
770 | 770 | |
|
771 | 771 | |
|
772 | 772 | def start_device(self): |
|
773 | 773 | |
|
774 | 774 | self.message = 'Function not implemented' |
|
775 | 775 | return False |
|
776 | 776 | |
|
777 | 777 | |
|
778 | 778 | def write_device(self, parms): |
|
779 | 779 | |
|
780 | 780 | self.message = 'Function not implemented' |
|
781 | 781 | return False |
|
782 | 782 | |
|
783 | 783 | |
|
784 | 784 | def read_device(self): |
|
785 | 785 | |
|
786 | 786 | self.message = 'Function not implemented' |
|
787 | 787 | return False |
|
788 | 788 | |
|
789 | 789 | |
|
790 | 790 | def get_absolute_url(self): |
|
791 | 791 | return reverse('url_%s_conf' % self.device.device_type.name, args=[str(self.id)]) |
|
792 | 792 | |
|
793 | 793 | def get_absolute_url_edit(self): |
|
794 | 794 | return reverse('url_edit_%s_conf' % self.device.device_type.name, args=[str(self.id)]) |
|
795 | 795 | |
|
796 | 796 | def get_absolute_url_delete(self): |
|
797 | 797 | return reverse('url_delete_dev_conf', args=[str(self.id)]) |
|
798 | 798 | |
|
799 | 799 | def get_absolute_url_import(self): |
|
800 | 800 | return reverse('url_import_dev_conf', args=[str(self.id)]) |
|
801 | 801 | |
|
802 | 802 | def get_absolute_url_export(self): |
|
803 | 803 | return reverse('url_export_dev_conf', args=[str(self.id)]) |
|
804 | 804 | |
|
805 | 805 | def get_absolute_url_write(self): |
|
806 | 806 | return reverse('url_write_dev_conf', args=[str(self.id)]) |
|
807 | 807 | |
|
808 | 808 | def get_absolute_url_read(self): |
|
809 | 809 | return reverse('url_read_dev_conf', args=[str(self.id)]) |
|
810 | 810 | |
|
811 | 811 | def get_absolute_url_start(self): |
|
812 | 812 | return reverse('url_start_dev_conf', args=[str(self.id)]) |
|
813 | 813 | |
|
814 | 814 | def get_absolute_url_stop(self): |
|
815 | 815 | return reverse('url_stop_dev_conf', args=[str(self.id)]) |
|
816 | 816 | |
|
817 | 817 | def get_absolute_url_status(self): |
|
818 | 818 | return reverse('url_status_dev_conf', args=[str(self.id)]) |
@@ -1,2029 +1,2029 | |||
|
1 | 1 | import ast |
|
2 | 2 | import json |
|
3 | 3 | import hashlib |
|
4 | 4 | from datetime import datetime, timedelta |
|
5 | 5 | |
|
6 | 6 | from django.shortcuts import render, redirect, get_object_or_404, HttpResponse |
|
7 | 7 | from django.utils.safestring import mark_safe |
|
8 | 8 | from django.http import HttpResponseRedirect |
|
9 | 9 | from django.urls import reverse |
|
10 | 10 | from django.db.models import Q |
|
11 | 11 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
|
12 | 12 | from django.contrib import messages |
|
13 | 13 | from django.http.request import QueryDict |
|
14 | 14 | from django.contrib.auth.decorators import login_required, user_passes_test |
|
15 | 15 | |
|
16 | 16 | from django.utils.timezone import is_aware |
|
17 | 17 | |
|
18 | 18 | try: |
|
19 | 19 | from urllib.parse import urlencode |
|
20 | 20 | except ImportError: |
|
21 | 21 | from urllib import urlencode |
|
22 | 22 | |
|
23 | 23 | from .forms import CampaignForm, ExperimentForm, DeviceForm, ConfigurationForm, LocationForm, UploadFileForm, DownloadFileForm, OperationForm, NewForm |
|
24 | 24 | from .forms import OperationSearchForm, FilterForm, ChangeIpForm |
|
25 | 25 | |
|
26 | 26 | from apps.rc.forms import RCConfigurationForm, RCLineCode, RCMixConfigurationForm |
|
27 | 27 | from apps.dds.forms import DDSConfigurationForm |
|
28 | 28 | from apps.jars.forms import JARSConfigurationForm |
|
29 | 29 | from apps.cgs.forms import CGSConfigurationForm |
|
30 | 30 | from apps.abs.forms import ABSConfigurationForm |
|
31 | 31 | from apps.usrp.forms import USRPConfigurationForm |
|
32 | 32 | from apps.dds_rest.forms import DDSRestConfigurationForm |
|
33 | 33 | from .utils import Params |
|
34 | 34 | |
|
35 | 35 | from .models import Campaign, Experiment, Device, Configuration, Location, RunningExperiment, DEV_STATES |
|
36 | 36 | from apps.cgs.models import CGSConfiguration |
|
37 | 37 | from apps.jars.models import JARSConfiguration, EXPERIMENT_TYPE |
|
38 | 38 | from apps.usrp.models import USRPConfiguration |
|
39 | 39 | from apps.abs.models import ABSConfiguration |
|
40 | 40 | from apps.rc.models import RCConfiguration, RCLine, RCLineType, RCClock |
|
41 | 41 | from apps.dds.models import DDSConfiguration |
|
42 | 42 | from apps.dds_rest.models import DDSRestConfiguration |
|
43 | 43 | |
|
44 | 44 | #from .tasks import task_start |
|
45 | 45 | from radarsys.celery import app |
|
46 | 46 | |
|
47 | 47 | #comentario test |
|
48 | 48 | CONF_FORMS = { |
|
49 | 49 | 'rc': RCConfigurationForm, |
|
50 | 50 | 'dds': DDSConfigurationForm, |
|
51 | 51 | 'dds_rest': DDSRestConfigurationForm, |
|
52 | 52 | 'jars': JARSConfigurationForm, |
|
53 | 53 | 'cgs': CGSConfigurationForm, |
|
54 | 54 | 'abs': ABSConfigurationForm, |
|
55 | 55 | 'usrp': USRPConfigurationForm, |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | CONF_MODELS = { |
|
59 | 59 | 'rc': RCConfiguration, |
|
60 | 60 | 'dds': DDSConfiguration, |
|
61 | 61 | 'dds_rest': DDSRestConfiguration, |
|
62 | 62 | 'jars': JARSConfiguration, |
|
63 | 63 | 'cgs': CGSConfiguration, |
|
64 | 64 | 'abs': ABSConfiguration, |
|
65 | 65 | 'usrp': USRPConfiguration, |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | MIX_MODES = { |
|
69 | 69 | '0': 'P', |
|
70 | 70 | '1': 'S', |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | MIX_OPERATIONS = { |
|
74 | 74 | '0': 'OR', |
|
75 | 75 | '1': 'XOR', |
|
76 | 76 | '2': 'AND', |
|
77 | 77 | '3': 'NAND', |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | |
|
81 | 81 | def is_developer(user): |
|
82 | 82 | |
|
83 | 83 | groups = [str(g.name) for g in user.groups.all()] |
|
84 | 84 | return 'Developer' in groups or user.is_staff |
|
85 | 85 | |
|
86 | 86 | |
|
87 | 87 | def is_operator(user): |
|
88 | 88 | |
|
89 | 89 | groups = [str(g.name) for g in user.groups.all()] |
|
90 | 90 | return 'Operator' in groups or user.is_staff |
|
91 | 91 | |
|
92 | 92 | |
|
93 | 93 | def has_been_modified(model): |
|
94 | 94 | |
|
95 | 95 | prev_hash = model.hash |
|
96 | 96 | new_hash = hashlib.sha256(str(model.parms_to_dict).encode()).hexdigest() |
|
97 | 97 | if prev_hash != new_hash: |
|
98 | 98 | model.hash = new_hash |
|
99 | 99 | model.save() |
|
100 | 100 | return True |
|
101 | 101 | return False |
|
102 | 102 | |
|
103 | 103 | |
|
104 | 104 | def index(request): |
|
105 | 105 | kwargs = {'no_sidebar': True} |
|
106 | 106 | |
|
107 | 107 | return render(request, 'index.html', kwargs) |
|
108 | 108 | |
|
109 | 109 | |
|
110 | 110 | def locations(request): |
|
111 | 111 | |
|
112 | 112 | page = request.GET.get('page') |
|
113 | 113 | order = ('name',) |
|
114 | 114 | |
|
115 | 115 | kwargs = get_paginator(Location, page, order) |
|
116 | 116 | |
|
117 | 117 | kwargs['keys'] = ['name', 'description'] |
|
118 | 118 | kwargs['title'] = 'Radar System' |
|
119 | 119 | kwargs['suptitle'] = 'List' |
|
120 | 120 | kwargs['no_sidebar'] = True |
|
121 | 121 | |
|
122 | 122 | return render(request, 'base_list.html', kwargs) |
|
123 | 123 | |
|
124 | 124 | |
|
125 | 125 | def location(request, id_loc): |
|
126 | 126 | |
|
127 | 127 | location = get_object_or_404(Location, pk=id_loc) |
|
128 | 128 | |
|
129 | 129 | kwargs = {} |
|
130 | 130 | kwargs['location'] = location |
|
131 | 131 | kwargs['location_keys'] = ['name', 'description'] |
|
132 | 132 | |
|
133 | 133 | kwargs['title'] = 'Location' |
|
134 | 134 | kwargs['suptitle'] = 'Details' |
|
135 | 135 | |
|
136 | 136 | return render(request, 'location.html', kwargs) |
|
137 | 137 | |
|
138 | 138 | |
|
139 | 139 | @login_required |
|
140 | 140 | def location_new(request): |
|
141 | 141 | |
|
142 | 142 | if request.method == 'GET': |
|
143 | 143 | form = LocationForm() |
|
144 | 144 | |
|
145 | 145 | if request.method == 'POST': |
|
146 | 146 | form = LocationForm(request.POST) |
|
147 | 147 | |
|
148 | 148 | if form.is_valid(): |
|
149 | 149 | form.save() |
|
150 | 150 | return redirect('url_locations') |
|
151 | 151 | |
|
152 | 152 | kwargs = {} |
|
153 | 153 | kwargs['form'] = form |
|
154 | 154 | kwargs['title'] = 'Radar System' |
|
155 | 155 | kwargs['suptitle'] = 'New' |
|
156 | 156 | kwargs['button'] = 'Create' |
|
157 | 157 | |
|
158 | 158 | return render(request, 'base_edit.html', kwargs) |
|
159 | 159 | |
|
160 | 160 | |
|
161 | 161 | @login_required |
|
162 | 162 | def location_edit(request, id_loc): |
|
163 | 163 | |
|
164 | 164 | location = get_object_or_404(Location, pk=id_loc) |
|
165 | 165 | |
|
166 | 166 | if request.method == 'GET': |
|
167 | 167 | form = LocationForm(instance=location) |
|
168 | 168 | |
|
169 | 169 | if request.method == 'POST': |
|
170 | 170 | form = LocationForm(request.POST, instance=location) |
|
171 | 171 | |
|
172 | 172 | if form.is_valid(): |
|
173 | 173 | form.save() |
|
174 | 174 | return redirect('url_locations') |
|
175 | 175 | |
|
176 | 176 | kwargs = {} |
|
177 | 177 | kwargs['form'] = form |
|
178 | 178 | kwargs['title'] = 'Location' |
|
179 | 179 | kwargs['suptitle'] = 'Edit' |
|
180 | 180 | kwargs['button'] = 'Update' |
|
181 | 181 | |
|
182 | 182 | return render(request, 'base_edit.html', kwargs) |
|
183 | 183 | |
|
184 | 184 | |
|
185 | 185 | @login_required |
|
186 | 186 | def location_delete(request, id_loc): |
|
187 | 187 | |
|
188 | 188 | location = get_object_or_404(Location, pk=id_loc) |
|
189 | 189 | |
|
190 | 190 | if request.method == 'POST': |
|
191 | 191 | |
|
192 | 192 | if is_developer(request.user): |
|
193 | 193 | location.delete() |
|
194 | 194 | return redirect('url_locations') |
|
195 | 195 | |
|
196 | 196 | messages.error(request, 'Not enough permission to delete this object') |
|
197 | 197 | return redirect(location.get_absolute_url()) |
|
198 | 198 | |
|
199 | 199 | kwargs = { |
|
200 | 200 | 'title': 'Delete', |
|
201 | 201 | 'suptitle': 'Location', |
|
202 | 202 | 'object': location, |
|
203 | 203 | 'delete': True |
|
204 | 204 | } |
|
205 | 205 | |
|
206 | 206 | return render(request, 'confirm.html', kwargs) |
|
207 | 207 | |
|
208 | 208 | |
|
209 | 209 | def devices(request): |
|
210 | 210 | |
|
211 | 211 | page = request.GET.get('page') |
|
212 | 212 | order = ('location', 'device_type') |
|
213 | 213 | |
|
214 | 214 | filters = request.GET.copy() |
|
215 | 215 | kwargs = get_paginator(Device, page, order, filters) |
|
216 | 216 | form = FilterForm(initial=request.GET, extra_fields=['tags']) |
|
217 | 217 | |
|
218 | 218 | kwargs['keys'] = ['device_type', 'location', |
|
219 | 219 | 'ip_address', 'port_address', 'actions'] |
|
220 | 220 | kwargs['title'] = 'Device' |
|
221 | 221 | kwargs['suptitle'] = 'List' |
|
222 | 222 | kwargs['no_sidebar'] = True |
|
223 | 223 | kwargs['form'] = form |
|
224 | 224 | kwargs['add_url'] = reverse('url_add_device') |
|
225 | 225 | filters.pop('page', None) |
|
226 | 226 | kwargs['q'] = urlencode(filters) |
|
227 | 227 | kwargs['menu_devices'] = 'active' |
|
228 | 228 | return render(request, 'base_list.html', kwargs) |
|
229 | 229 | |
|
230 | 230 | |
|
231 | 231 | def device(request, id_dev): |
|
232 | 232 | |
|
233 | 233 | device = get_object_or_404(Device, pk=id_dev) |
|
234 | 234 | |
|
235 | 235 | kwargs = {} |
|
236 | 236 | kwargs['device'] = device |
|
237 | 237 | kwargs['device_keys'] = ['device_type', |
|
238 | 238 | 'ip_address', 'port_address', 'description'] |
|
239 | 239 | |
|
240 | 240 | kwargs['title'] = 'Device' |
|
241 | 241 | kwargs['suptitle'] = 'Details' |
|
242 | 242 | kwargs['menu_devices'] = 'active' |
|
243 | 243 | |
|
244 | 244 | return render(request, 'device.html', kwargs) |
|
245 | 245 | |
|
246 | 246 | |
|
247 | 247 | @login_required |
|
248 | 248 | def device_new(request): |
|
249 | 249 | |
|
250 | 250 | if request.method == 'GET': |
|
251 | 251 | form = DeviceForm() |
|
252 | 252 | |
|
253 | 253 | if request.method == 'POST': |
|
254 | 254 | form = DeviceForm(request.POST) |
|
255 | 255 | |
|
256 | 256 | if form.is_valid(): |
|
257 | 257 | form.save() |
|
258 | 258 | return redirect('url_devices') |
|
259 | 259 | |
|
260 | 260 | kwargs = {} |
|
261 | 261 | kwargs['form'] = form |
|
262 | 262 | kwargs['title'] = 'Device' |
|
263 | 263 | kwargs['suptitle'] = 'New_2' |
|
264 | 264 | kwargs['button'] = 'Create' |
|
265 | 265 | kwargs['menu_devices'] = 'active' |
|
266 | 266 | |
|
267 | 267 | return render(request, 'base_edit.html', kwargs) |
|
268 | 268 | |
|
269 | 269 | |
|
270 | 270 | @login_required |
|
271 | 271 | def device_edit(request, id_dev): |
|
272 | 272 | |
|
273 | 273 | device = get_object_or_404(Device, pk=id_dev) |
|
274 | 274 | |
|
275 | 275 | if request.method == 'GET': |
|
276 | 276 | form = DeviceForm(instance=device) |
|
277 | 277 | |
|
278 | 278 | if request.method == 'POST': |
|
279 | 279 | form = DeviceForm(request.POST, instance=device) |
|
280 | 280 | |
|
281 | 281 | if form.is_valid(): |
|
282 | 282 | form.save() |
|
283 | 283 | return redirect(device.get_absolute_url()) |
|
284 | 284 | |
|
285 | 285 | kwargs = {} |
|
286 | 286 | kwargs['form'] = form |
|
287 | 287 | kwargs['title'] = 'Device' |
|
288 | 288 | kwargs['suptitle'] = 'Edit' |
|
289 | 289 | kwargs['button'] = 'Update' |
|
290 | 290 | kwargs['menu_devices'] = 'active' |
|
291 | 291 | |
|
292 | 292 | return render(request, 'base_edit.html', kwargs) |
|
293 | 293 | |
|
294 | 294 | |
|
295 | 295 | @login_required |
|
296 | 296 | def device_delete(request, id_dev): |
|
297 | 297 | |
|
298 | 298 | device = get_object_or_404(Device, pk=id_dev) |
|
299 | 299 | |
|
300 | 300 | if request.method == 'POST': |
|
301 | 301 | |
|
302 | 302 | if is_developer(request.user): |
|
303 | 303 | device.delete() |
|
304 | 304 | return redirect('url_devices') |
|
305 | 305 | |
|
306 | 306 | messages.error(request, 'Not enough permission to delete this object') |
|
307 | 307 | return redirect(device.get_absolute_url()) |
|
308 | 308 | |
|
309 | 309 | kwargs = { |
|
310 | 310 | 'title': 'Delete', |
|
311 | 311 | 'suptitle': 'Device', |
|
312 | 312 | 'object': device, |
|
313 | 313 | 'delete': True |
|
314 | 314 | } |
|
315 | 315 | kwargs['menu_devices'] = 'active' |
|
316 | 316 | |
|
317 | 317 | return render(request, 'confirm.html', kwargs) |
|
318 | 318 | |
|
319 | 319 | |
|
320 | 320 | @login_required |
|
321 | 321 | def device_change_ip(request, id_dev): |
|
322 | 322 | |
|
323 | 323 | device = get_object_or_404(Device, pk=id_dev) |
|
324 | 324 | |
|
325 | 325 | if request.method == 'POST': |
|
326 | 326 | |
|
327 | 327 | if is_developer(request.user): |
|
328 | 328 | device.change_ip(**request.POST.dict()) |
|
329 | 329 | |
|
330 | 330 | print(device.ip_address, device.message) |
|
331 | 331 | |
|
332 | 332 | level, message = device.message.split('|') |
|
333 | 333 | messages.add_message(request, level, message) |
|
334 | 334 | else: |
|
335 | 335 | messages.error( |
|
336 | 336 | request, 'Not enough permission to delete this object') |
|
337 | 337 | return redirect(device.get_absolute_url()) |
|
338 | 338 | |
|
339 | 339 | kwargs = { |
|
340 | 340 | 'title': 'Device', |
|
341 | 341 | 'suptitle': 'Change IP', |
|
342 | 342 | 'object': device, |
|
343 | 343 | 'previous': device.get_absolute_url(), |
|
344 | 344 | 'form': ChangeIpForm(initial={'ip_address': device.ip_address}), |
|
345 | 345 | 'message': ' ', |
|
346 | 346 | } |
|
347 | 347 | kwargs['menu_devices'] = 'active' |
|
348 | 348 | |
|
349 | 349 | return render(request, 'confirm.html', kwargs) |
|
350 | 350 | |
|
351 | 351 | |
|
352 | 352 | def campaigns(request): |
|
353 | 353 | |
|
354 | 354 | page = request.GET.get('page') |
|
355 | 355 | order = ('-start_date',) |
|
356 | 356 | filters = request.GET.copy() |
|
357 | 357 | |
|
358 | 358 | kwargs = get_paginator(Campaign, page, order, filters) |
|
359 | 359 | |
|
360 | 360 | form = FilterForm(initial=request.GET, extra_fields=[ |
|
361 | 361 | 'range_date', 'tags', 'template']) |
|
362 | 362 | kwargs['keys'] = ['name', 'start_date', 'end_date', 'actions'] |
|
363 | 363 | kwargs['title'] = 'Campaign' |
|
364 | 364 | kwargs['suptitle'] = 'List' |
|
365 | 365 | kwargs['no_sidebar'] = True |
|
366 | 366 | kwargs['form'] = form |
|
367 | 367 | kwargs['add_url'] = reverse('url_add_campaign') |
|
368 | 368 | filters.pop('page', None) |
|
369 | 369 | kwargs['q'] = urlencode(filters) |
|
370 | 370 | kwargs['menu_campaigns'] = 'active' |
|
371 | 371 | |
|
372 | 372 | return render(request, 'base_list.html', kwargs) |
|
373 | 373 | |
|
374 | 374 | |
|
375 | 375 | def campaign(request, id_camp): |
|
376 | 376 | |
|
377 | 377 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
378 | 378 | experiments = Experiment.objects.filter(campaign=campaign) |
|
379 | 379 | |
|
380 | 380 | form = CampaignForm(instance=campaign) |
|
381 | 381 | |
|
382 | 382 | kwargs = {} |
|
383 | 383 | kwargs['campaign'] = campaign |
|
384 | 384 | kwargs['campaign_keys'] = ['template', 'name', |
|
385 | 385 | 'start_date', 'end_date', 'tags', 'description'] |
|
386 | 386 | |
|
387 | 387 | kwargs['experiments'] = experiments |
|
388 | 388 | kwargs['experiment_keys'] = [ |
|
389 | 389 | 'name', 'radar_system', 'start_time', 'end_time'] |
|
390 | 390 | |
|
391 | 391 | kwargs['title'] = 'Campaign' |
|
392 | 392 | kwargs['suptitle'] = 'Details' |
|
393 | 393 | |
|
394 | 394 | kwargs['form'] = form |
|
395 | 395 | kwargs['button'] = 'Add Experiment' |
|
396 | 396 | kwargs['menu_campaigns'] = 'active' |
|
397 | 397 | |
|
398 | 398 | return render(request, 'campaign.html', kwargs) |
|
399 | 399 | |
|
400 | 400 | |
|
401 | 401 | @login_required |
|
402 | 402 | def campaign_new(request): |
|
403 | 403 | |
|
404 | 404 | kwargs = {} |
|
405 | 405 | |
|
406 | 406 | if request.method == 'GET': |
|
407 | 407 | |
|
408 | 408 | if 'template' in request.GET: |
|
409 | 409 | if request.GET['template'] == '0': |
|
410 | 410 | form = NewForm(initial={'create_from': 2}, |
|
411 | 411 | template_choices=Campaign.objects.filter(template=True).values_list('id', 'name')) |
|
412 | 412 | else: |
|
413 | 413 | kwargs['button'] = 'Create' |
|
414 | 414 | kwargs['experiments'] = Configuration.objects.filter( |
|
415 | 415 | experiment=request.GET['template']) |
|
416 | 416 | kwargs['experiment_keys'] = ['name', 'start_time', 'end_time'] |
|
417 | 417 | camp = Campaign.objects.get(pk=request.GET['template']) |
|
418 | 418 | form = CampaignForm(instance=camp, |
|
419 | initial={'name': '{}_{:%Y%m%d}'.format(camp.name, datetime.now()), | |
|
419 | initial={'name': '{}_{:%Y%m%d%H%M%S}'.format(camp.name, datetime.now()), | |
|
420 | 420 | 'template': False}) |
|
421 | 421 | elif 'blank' in request.GET: |
|
422 | 422 | kwargs['button'] = 'Create' |
|
423 | 423 | form = CampaignForm() |
|
424 | 424 | else: |
|
425 | 425 | form = NewForm() |
|
426 | 426 | |
|
427 | 427 | if request.method == 'POST': |
|
428 | 428 | kwargs['button'] = 'Create' |
|
429 | 429 | post = request.POST.copy() |
|
430 | 430 | experiments = [] |
|
431 | 431 | |
|
432 | 432 | for id_exp in post.getlist('experiments'): |
|
433 | 433 | exp = Experiment.objects.get(pk=id_exp) |
|
434 | 434 | new_exp = exp.clone(template=False) |
|
435 | 435 | experiments.append(new_exp) |
|
436 | 436 | |
|
437 | 437 | post.setlist('experiments', []) |
|
438 | 438 | |
|
439 | 439 | form = CampaignForm(post) |
|
440 | 440 | |
|
441 | 441 | if form.is_valid(): |
|
442 | 442 | campaign = form.save(commit=False) |
|
443 | 443 | campaign.author = request.user |
|
444 | 444 | campaign.save() |
|
445 | 445 | for exp in experiments: |
|
446 | 446 | campaign.experiments.add(exp) |
|
447 | 447 | |
|
448 | 448 | return redirect('url_campaign', id_camp=campaign.id) |
|
449 | 449 | |
|
450 | 450 | kwargs['form'] = form |
|
451 | 451 | kwargs['title'] = 'Campaign' |
|
452 | 452 | kwargs['suptitle'] = 'New' |
|
453 | 453 | kwargs['menu_campaigns'] = 'active' |
|
454 | 454 | |
|
455 | 455 | return render(request, 'campaign_edit.html', kwargs) |
|
456 | 456 | |
|
457 | 457 | |
|
458 | 458 | @login_required |
|
459 | 459 | def campaign_edit(request, id_camp): |
|
460 | 460 | |
|
461 | 461 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
462 | 462 | |
|
463 | 463 | if request.method == 'GET': |
|
464 | 464 | form = CampaignForm(instance=campaign) |
|
465 | 465 | |
|
466 | 466 | if request.method == 'POST': |
|
467 | 467 | exps = campaign.experiments.all().values_list('pk', flat=True) |
|
468 | 468 | post = request.POST.copy() |
|
469 | 469 | new_exps = post.getlist('experiments') |
|
470 | 470 | post.setlist('experiments', []) |
|
471 | 471 | form = CampaignForm(post, instance=campaign) |
|
472 | 472 | |
|
473 | 473 | if form.is_valid(): |
|
474 | 474 | camp = form.save() |
|
475 | 475 | for id_exp in new_exps: |
|
476 | 476 | if int(id_exp) in exps: |
|
477 | 477 | exps.pop(id_exp) |
|
478 | 478 | else: |
|
479 | 479 | exp = Experiment.objects.get(pk=id_exp) |
|
480 | 480 | if exp.template: |
|
481 | 481 | camp.experiments.add(exp.clone(template=False)) |
|
482 | 482 | else: |
|
483 | 483 | camp.experiments.add(exp) |
|
484 | 484 | |
|
485 | 485 | for id_exp in exps: |
|
486 | 486 | camp.experiments.remove(Experiment.objects.get(pk=id_exp)) |
|
487 | 487 | |
|
488 | 488 | return redirect('url_campaign', id_camp=id_camp) |
|
489 | 489 | |
|
490 | 490 | kwargs = {} |
|
491 | 491 | kwargs['form'] = form |
|
492 | 492 | kwargs['title'] = 'Campaign' |
|
493 | 493 | kwargs['suptitle'] = 'Edit' |
|
494 | 494 | kwargs['button'] = 'Update' |
|
495 | 495 | kwargs['menu_campaigns'] = 'active' |
|
496 | 496 | |
|
497 | 497 | return render(request, 'campaign_edit.html', kwargs) |
|
498 | 498 | |
|
499 | 499 | |
|
500 | 500 | @login_required |
|
501 | 501 | def campaign_delete(request, id_camp): |
|
502 | 502 | |
|
503 | 503 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
504 | 504 | |
|
505 | 505 | if request.method == 'POST': |
|
506 | 506 | if is_developer(request.user): |
|
507 | 507 | |
|
508 | 508 | for exp in campaign.experiments.all(): |
|
509 | 509 | for conf in Configuration.objects.filter(experiment=exp): |
|
510 | 510 | conf.delete() |
|
511 | 511 | exp.delete() |
|
512 | 512 | campaign.delete() |
|
513 | 513 | |
|
514 | 514 | return redirect('url_campaigns') |
|
515 | 515 | |
|
516 | 516 | messages.error(request, 'Not enough permission to delete this object') |
|
517 | 517 | return redirect(campaign.get_absolute_url()) |
|
518 | 518 | |
|
519 | 519 | kwargs = { |
|
520 | 520 | 'title': 'Delete', |
|
521 | 521 | 'suptitle': 'Campaign', |
|
522 | 522 | 'object': campaign, |
|
523 | 523 | 'delete': True |
|
524 | 524 | } |
|
525 | 525 | kwargs['menu_campaigns'] = 'active' |
|
526 | 526 | |
|
527 | 527 | return render(request, 'confirm.html', kwargs) |
|
528 | 528 | |
|
529 | 529 | |
|
530 | 530 | @login_required |
|
531 | 531 | def campaign_export(request, id_camp): |
|
532 | 532 | |
|
533 | 533 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
534 | 534 | content = campaign.parms_to_dict() |
|
535 | 535 | content_type = 'application/json' |
|
536 | 536 | filename = '%s_%s.json' % (campaign.name, campaign.id) |
|
537 | 537 | |
|
538 | 538 | response = HttpResponse(content_type=content_type) |
|
539 | 539 | response['Content-Disposition'] = 'attachment; filename="%s"' % filename |
|
540 | 540 | response.write(json.dumps(content, indent=2)) |
|
541 | 541 | |
|
542 | 542 | return response |
|
543 | 543 | |
|
544 | 544 | |
|
545 | 545 | @login_required |
|
546 | 546 | def campaign_import(request, id_camp): |
|
547 | 547 | |
|
548 | 548 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
549 | 549 | |
|
550 | 550 | if request.method == 'GET': |
|
551 | 551 | file_form = UploadFileForm() |
|
552 | 552 | |
|
553 | 553 | if request.method == 'POST': |
|
554 | 554 | file_form = UploadFileForm(request.POST, request.FILES) |
|
555 | 555 | |
|
556 | 556 | if file_form.is_valid(): |
|
557 | 557 | new_camp = campaign.dict_to_parms( |
|
558 | 558 | json.load(request.FILES['file']), CONF_MODELS) |
|
559 | 559 | messages.success( |
|
560 | 560 | request, "Parameters imported from: '%s'." % request.FILES['file'].name) |
|
561 | 561 | return redirect(new_camp.get_absolute_url_edit()) |
|
562 | 562 | |
|
563 | 563 | messages.error(request, "Could not import parameters from file") |
|
564 | 564 | |
|
565 | 565 | kwargs = {} |
|
566 | 566 | kwargs['title'] = 'Campaign' |
|
567 | 567 | kwargs['form'] = file_form |
|
568 | 568 | kwargs['suptitle'] = 'Importing file' |
|
569 | 569 | kwargs['button'] = 'Import' |
|
570 | 570 | kwargs['menu_campaigns'] = 'active' |
|
571 | 571 | |
|
572 | 572 | return render(request, 'campaign_import.html', kwargs) |
|
573 | 573 | |
|
574 | 574 | |
|
575 | 575 | def experiments(request): |
|
576 | 576 | |
|
577 | 577 | page = request.GET.get('page') |
|
578 | 578 | order = ('location',) |
|
579 | 579 | filters = request.GET.copy() |
|
580 | 580 | |
|
581 | 581 | if 'my experiments' in filters: |
|
582 | 582 | filters.pop('my experiments', None) |
|
583 | 583 | filters['mine'] = request.user.id |
|
584 | 584 | |
|
585 | 585 | kwargs = get_paginator(Experiment, page, order, filters) |
|
586 | 586 | |
|
587 | 587 | fields = ['tags', 'template'] |
|
588 | 588 | if request.user.is_authenticated: |
|
589 | 589 | fields.append('my experiments') |
|
590 | 590 | |
|
591 | 591 | form = FilterForm(initial=request.GET, extra_fields=fields) |
|
592 | 592 | |
|
593 | 593 | kwargs['keys'] = ['name', 'radar_system', |
|
594 | 594 | 'start_time', 'end_time', 'actions'] |
|
595 | 595 | kwargs['title'] = 'Experiment' |
|
596 | 596 | kwargs['suptitle'] = 'List' |
|
597 | 597 | kwargs['no_sidebar'] = True |
|
598 | 598 | kwargs['form'] = form |
|
599 | 599 | kwargs['add_url'] = reverse('url_add_experiment') |
|
600 | 600 | filters = request.GET.copy() |
|
601 | 601 | filters.pop('page', None) |
|
602 | 602 | kwargs['q'] = urlencode(filters) |
|
603 | 603 | kwargs['menu_experiments'] = 'active' |
|
604 | 604 | |
|
605 | 605 | return render(request, 'base_list.html', kwargs) |
|
606 | 606 | |
|
607 | 607 | |
|
608 | 608 | def experiment(request, id_exp): |
|
609 | 609 | |
|
610 | 610 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
611 | 611 | |
|
612 | 612 | configurations = Configuration.objects.filter( |
|
613 | 613 | experiment=experiment, type=0) |
|
614 | 614 | |
|
615 | 615 | kwargs = {} |
|
616 | 616 | |
|
617 | 617 | kwargs['experiment_keys'] = ['template', 'radar_system', |
|
618 | 618 | 'name', 'freq', 'start_time', 'end_time'] |
|
619 | 619 | kwargs['experiment'] = experiment |
|
620 | 620 | kwargs['configuration_keys'] = ['name', 'device__ip_address', |
|
621 | 621 | 'device__port_address', 'device__status'] |
|
622 | 622 | kwargs['configurations'] = configurations |
|
623 | 623 | kwargs['title'] = 'Experiment' |
|
624 | 624 | kwargs['suptitle'] = 'Details' |
|
625 | 625 | kwargs['button'] = 'Add Configuration' |
|
626 | 626 | kwargs['menu_experiments'] = 'active' |
|
627 | 627 | |
|
628 | 628 | ###### SIDEBAR ###### |
|
629 | 629 | kwargs.update(sidebar(experiment=experiment)) |
|
630 | 630 | |
|
631 | 631 | return render(request, 'experiment.html', kwargs) |
|
632 | 632 | |
|
633 | 633 | |
|
634 | 634 | @login_required |
|
635 | 635 | def experiment_new(request, id_camp=None): |
|
636 | 636 | |
|
637 | 637 | if not is_developer(request.user): |
|
638 | 638 | messages.error( |
|
639 | 639 | request, 'Developer required, to create new Experiments') |
|
640 | 640 | return redirect('index') |
|
641 | 641 | kwargs = {} |
|
642 | 642 | |
|
643 | 643 | if request.method == 'GET': |
|
644 | 644 | if 'template' in request.GET: |
|
645 | 645 | if request.GET['template'] == '0': |
|
646 | 646 | form = NewForm(initial={'create_from': 2}, |
|
647 | 647 | template_choices=Experiment.objects.filter(template=True).values_list('id', 'name')) |
|
648 | 648 | else: |
|
649 | 649 | kwargs['button'] = 'Create' |
|
650 | 650 | kwargs['configurations'] = Configuration.objects.filter( |
|
651 | 651 | experiment=request.GET['template']) |
|
652 | 652 | kwargs['configuration_keys'] = ['name', 'device__name', |
|
653 | 653 | 'device__ip_address', 'device__port_address'] |
|
654 | 654 | exp = Experiment.objects.get(pk=request.GET['template']) |
|
655 | 655 | form = ExperimentForm(instance=exp, |
|
656 | 656 | initial={'name': '{}_{:%y%m%d}'.format(exp.name, datetime.now()), |
|
657 | 657 | 'template': False}) |
|
658 | 658 | elif 'blank' in request.GET: |
|
659 | 659 | kwargs['button'] = 'Create' |
|
660 | 660 | form = ExperimentForm() |
|
661 | 661 | else: |
|
662 | 662 | form = NewForm() |
|
663 | 663 | |
|
664 | 664 | if request.method == 'POST': |
|
665 | 665 | form = ExperimentForm(request.POST) |
|
666 | 666 | if form.is_valid(): |
|
667 | 667 | experiment = form.save(commit=False) |
|
668 | 668 | experiment.author = request.user |
|
669 | 669 | experiment.save() |
|
670 | 670 | |
|
671 | 671 | if 'template' in request.GET: |
|
672 | 672 | configurations = Configuration.objects.filter( |
|
673 | 673 | experiment=request.GET['template'], type=0) |
|
674 | 674 | for conf in configurations: |
|
675 | 675 | conf.clone(experiment=experiment, template=False) |
|
676 | 676 | |
|
677 | 677 | return redirect('url_experiment', id_exp=experiment.id) |
|
678 | 678 | |
|
679 | 679 | kwargs['form'] = form |
|
680 | 680 | kwargs['title'] = 'Experiment' |
|
681 | 681 | kwargs['suptitle'] = 'New' |
|
682 | 682 | kwargs['menu_experiments'] = 'active' |
|
683 | 683 | |
|
684 | 684 | return render(request, 'experiment_edit.html', kwargs) |
|
685 | 685 | |
|
686 | 686 | |
|
687 | 687 | @login_required |
|
688 | 688 | def experiment_edit(request, id_exp): |
|
689 | 689 | |
|
690 | 690 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
691 | 691 | |
|
692 | 692 | if request.method == 'GET': |
|
693 | 693 | form = ExperimentForm(instance=experiment) |
|
694 | 694 | |
|
695 | 695 | if request.method == 'POST': |
|
696 | 696 | form = ExperimentForm(request.POST, instance=experiment) |
|
697 | 697 | |
|
698 | 698 | if form.is_valid(): |
|
699 | 699 | experiment = form.save() |
|
700 | 700 | return redirect('url_experiment', id_exp=experiment.id) |
|
701 | 701 | |
|
702 | 702 | kwargs = {} |
|
703 | 703 | kwargs['form'] = form |
|
704 | 704 | kwargs['title'] = 'Experiment' |
|
705 | 705 | kwargs['suptitle'] = 'Edit' |
|
706 | 706 | kwargs['button'] = 'Update' |
|
707 | 707 | kwargs['menu_experiments'] = 'active' |
|
708 | 708 | |
|
709 | 709 | return render(request, 'experiment_edit.html', kwargs) |
|
710 | 710 | |
|
711 | 711 | |
|
712 | 712 | @login_required |
|
713 | 713 | def experiment_delete(request, id_exp): |
|
714 | 714 | |
|
715 | 715 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
716 | 716 | |
|
717 | 717 | if request.method == 'POST': |
|
718 | 718 | if is_developer(request.user): |
|
719 | 719 | for conf in Configuration.objects.filter(experiment=experiment): |
|
720 | 720 | conf.delete() |
|
721 | 721 | experiment.delete() |
|
722 | 722 | return redirect('url_experiments') |
|
723 | 723 | |
|
724 | 724 | messages.error(request, 'Not enough permission to delete this object') |
|
725 | 725 | return redirect(experiment.get_absolute_url()) |
|
726 | 726 | |
|
727 | 727 | kwargs = { |
|
728 | 728 | 'title': 'Delete', |
|
729 | 729 | 'suptitle': 'Experiment', |
|
730 | 730 | 'object': experiment, |
|
731 | 731 | 'delete': True |
|
732 | 732 | } |
|
733 | 733 | |
|
734 | 734 | return render(request, 'confirm.html', kwargs) |
|
735 | 735 | |
|
736 | 736 | |
|
737 | 737 | @login_required |
|
738 | 738 | def experiment_export(request, id_exp): |
|
739 | 739 | |
|
740 | 740 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
741 | 741 | content = experiment.parms_to_dict() |
|
742 | 742 | content_type = 'application/json' |
|
743 | 743 | filename = '%s_%s.json' % (experiment.name, experiment.id) |
|
744 | 744 | |
|
745 | 745 | response = HttpResponse(content_type=content_type) |
|
746 | 746 | response['Content-Disposition'] = 'attachment; filename="%s"' % filename |
|
747 | 747 | response.write(json.dumps(content, indent=2)) |
|
748 | 748 | |
|
749 | 749 | return response |
|
750 | 750 | |
|
751 | 751 | |
|
752 | 752 | @login_required |
|
753 | 753 | def experiment_import(request, id_exp): |
|
754 | 754 | |
|
755 | 755 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
756 | 756 | configurations = Configuration.objects.filter(experiment=experiment) |
|
757 | 757 | |
|
758 | 758 | if request.method == 'GET': |
|
759 | 759 | file_form = UploadFileForm() |
|
760 | 760 | |
|
761 | 761 | if request.method == 'POST': |
|
762 | 762 | file_form = UploadFileForm(request.POST, request.FILES) |
|
763 | 763 | |
|
764 | 764 | if file_form.is_valid(): |
|
765 | 765 | new_exp = experiment.dict_to_parms( |
|
766 | 766 | json.load(request.FILES['file']), CONF_MODELS) |
|
767 | 767 | messages.success( |
|
768 | 768 | request, "Parameters imported from: '%s'." % request.FILES['file'].name) |
|
769 | 769 | return redirect(new_exp.get_absolute_url_edit()) |
|
770 | 770 | |
|
771 | 771 | messages.error(request, "Could not import parameters from file") |
|
772 | 772 | |
|
773 | 773 | kwargs = {} |
|
774 | 774 | kwargs['title'] = 'Experiment' |
|
775 | 775 | kwargs['form'] = file_form |
|
776 | 776 | kwargs['suptitle'] = 'Importing file' |
|
777 | 777 | kwargs['button'] = 'Import' |
|
778 | 778 | kwargs['menu_experiments'] = 'active' |
|
779 | 779 | |
|
780 | 780 | kwargs.update(sidebar(experiment=experiment)) |
|
781 | 781 | |
|
782 | 782 | return render(request, 'experiment_import.html', kwargs) |
|
783 | 783 | |
|
784 | 784 | |
|
785 | 785 | @login_required |
|
786 | 786 | def experiment_start(request, id_exp): |
|
787 | 787 | |
|
788 | 788 | exp = get_object_or_404(Experiment, pk=id_exp) |
|
789 | 789 | |
|
790 | 790 | if exp.status == 2: |
|
791 | 791 | messages.warning(request, 'Experiment {} already runnnig'.format(exp)) |
|
792 | 792 | else: |
|
793 | 793 | exp.status = exp.start() |
|
794 | 794 | if exp.status == 0: |
|
795 | 795 | messages.error(request, 'Experiment {} not start'.format(exp)) |
|
796 | 796 | if exp.status == 2: |
|
797 | 797 | messages.success(request, 'Experiment {} started'.format(exp)) |
|
798 | 798 | |
|
799 | 799 | exp.save() |
|
800 | 800 | |
|
801 | 801 | return redirect(exp.get_absolute_url()) |
|
802 | 802 | |
|
803 | 803 | |
|
804 | 804 | @login_required |
|
805 | 805 | def experiment_stop(request, id_exp): |
|
806 | 806 | |
|
807 | 807 | exp = get_object_or_404(Experiment, pk=id_exp) |
|
808 | 808 | |
|
809 | 809 | if exp.status == 2: |
|
810 | 810 | exp.status = exp.stop() |
|
811 | 811 | exp.save() |
|
812 | 812 | messages.success(request, 'Experiment {} stopped'.format(exp)) |
|
813 | 813 | else: |
|
814 | 814 | messages.error(request, 'Experiment {} not running'.format(exp)) |
|
815 | 815 | |
|
816 | 816 | return redirect(exp.get_absolute_url()) |
|
817 | 817 | |
|
818 | 818 | |
|
819 | 819 | def experiment_status(request, id_exp): |
|
820 | 820 | |
|
821 | 821 | exp = get_object_or_404(Experiment, pk=id_exp) |
|
822 | 822 | |
|
823 | 823 | exp.get_status() |
|
824 | 824 | |
|
825 | 825 | return redirect(exp.get_absolute_url()) |
|
826 | 826 | |
|
827 | 827 | |
|
828 | 828 | @login_required |
|
829 | 829 | def experiment_mix(request, id_exp): |
|
830 | 830 | |
|
831 | 831 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
832 | 832 | rc_confs = [conf for conf in RCConfiguration.objects.filter( |
|
833 | 833 | experiment=id_exp, |
|
834 | 834 | type=0, |
|
835 | 835 | mix=False)] |
|
836 | 836 | |
|
837 | 837 | if len(rc_confs) < 2: |
|
838 | 838 | messages.warning( |
|
839 | 839 | request, 'You need at least two RC Configurations to make a mix') |
|
840 | 840 | return redirect(experiment.get_absolute_url()) |
|
841 | 841 | |
|
842 | 842 | mix_confs = RCConfiguration.objects.filter(experiment=id_exp, mix=True, type=0) |
|
843 | 843 | |
|
844 | 844 | if mix_confs: |
|
845 | 845 | mix = mix_confs[0] |
|
846 | 846 | else: |
|
847 | 847 | mix = RCConfiguration(experiment=experiment, |
|
848 | 848 | device=rc_confs[0].device, |
|
849 | 849 | ipp=rc_confs[0].ipp, |
|
850 | 850 | clock_in=rc_confs[0].clock_in, |
|
851 | 851 | clock_divider=rc_confs[0].clock_divider, |
|
852 | 852 | mix=True, |
|
853 | 853 | parameters='') |
|
854 | 854 | mix.save() |
|
855 | 855 | |
|
856 | 856 | line_type = RCLineType.objects.get(name='mix') |
|
857 | 857 | print("VIew obteniendo len getlines") |
|
858 | 858 | print(len(rc_confs[0].get_lines())) |
|
859 | 859 | for i in range(len(rc_confs[0].get_lines())): |
|
860 | 860 | line = RCLine(rc_configuration=mix, line_type=line_type, channel=i) |
|
861 | 861 | line.save() |
|
862 | 862 | |
|
863 | 863 | initial = {'name': mix.name, |
|
864 | 864 | 'result': parse_mix_result(mix.parameters), |
|
865 | 865 | 'delay': 0, |
|
866 | 866 | 'mask': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] |
|
867 | 867 | } |
|
868 | 868 | |
|
869 | 869 | if request.method == 'GET': |
|
870 | 870 | form = RCMixConfigurationForm(confs=rc_confs, initial=initial) |
|
871 | 871 | |
|
872 | 872 | if request.method == 'POST': |
|
873 | 873 | result = mix.parameters |
|
874 | 874 | |
|
875 | 875 | if '{}|'.format(request.POST['experiment']) in result: |
|
876 | 876 | messages.error(request, 'Configuration already added') |
|
877 | 877 | else: |
|
878 | 878 | if 'operation' in request.POST: |
|
879 | 879 | operation = MIX_OPERATIONS[request.POST['operation']] |
|
880 | 880 | else: |
|
881 | 881 | operation = ' ' |
|
882 | 882 | |
|
883 | 883 | mode = MIX_MODES[request.POST['mode']] |
|
884 | 884 | |
|
885 | 885 | if result: |
|
886 | 886 | result = '{}-{}|{}|{}|{}|{}'.format(mix.parameters, |
|
887 | 887 | request.POST['experiment'], |
|
888 | 888 | mode, |
|
889 | 889 | operation, |
|
890 | 890 | float( |
|
891 | 891 | request.POST['delay']), |
|
892 | 892 | parse_mask( |
|
893 | 893 | request.POST.getlist('mask')) |
|
894 | 894 | ) |
|
895 | 895 | else: |
|
896 | 896 | result = '{}|{}|{}|{}|{}'.format(request.POST['experiment'], |
|
897 | 897 | mode, |
|
898 | 898 | operation, |
|
899 | 899 | float(request.POST['delay']), |
|
900 | 900 | parse_mask( |
|
901 | 901 | request.POST.getlist('mask')) |
|
902 | 902 | ) |
|
903 | 903 | |
|
904 | 904 | mix.parameters = result |
|
905 | 905 | mix.save() |
|
906 | 906 | mix.update_pulses() |
|
907 | 907 | |
|
908 | 908 | initial['result'] = parse_mix_result(result) |
|
909 | 909 | initial['name'] = mix.name |
|
910 | 910 | |
|
911 | 911 | form = RCMixConfigurationForm(initial=initial, confs=rc_confs) |
|
912 | 912 | |
|
913 | 913 | kwargs = { |
|
914 | 914 | 'title': 'Experiment', |
|
915 | 915 | 'suptitle': 'Mix Configurations', |
|
916 | 916 | 'form': form, |
|
917 | 917 | 'extra_button': 'Delete', |
|
918 | 918 | 'button': 'Add', |
|
919 | 919 | 'cancel': 'Back', |
|
920 | 920 | 'previous': experiment.get_absolute_url(), |
|
921 | 921 | 'id_exp': id_exp, |
|
922 | 922 | |
|
923 | 923 | } |
|
924 | 924 | kwargs['menu_experiments'] = 'active' |
|
925 | 925 | |
|
926 | 926 | return render(request, 'experiment_mix.html', kwargs) |
|
927 | 927 | |
|
928 | 928 | |
|
929 | 929 | @login_required |
|
930 | 930 | def experiment_mix_delete(request, id_exp): |
|
931 | 931 | |
|
932 | 932 | conf = RCConfiguration.objects.get(experiment=id_exp, mix=True, type=0) |
|
933 | 933 | values = conf.parameters.split('-') |
|
934 | 934 | conf.parameters = '-'.join(values[:-1]) |
|
935 | 935 | conf.save() |
|
936 | 936 | |
|
937 | 937 | return redirect('url_mix_experiment', id_exp=id_exp) |
|
938 | 938 | |
|
939 | 939 | |
|
940 | 940 | def experiment_summary(request, id_exp): |
|
941 | 941 | |
|
942 | 942 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
943 | 943 | configurations = Configuration.objects.filter( |
|
944 | 944 | experiment=experiment, type=0) |
|
945 | 945 | |
|
946 | 946 | kwargs = {} |
|
947 | 947 | kwargs['experiment_keys'] = ['radar_system', |
|
948 | 948 | 'name', 'freq', 'start_time', 'end_time'] |
|
949 | 949 | kwargs['experiment'] = experiment |
|
950 | 950 | kwargs['configurations'] = [] |
|
951 | 951 | kwargs['title'] = 'Experiment Summary' |
|
952 | 952 | kwargs['suptitle'] = 'Details' |
|
953 | 953 | kwargs['button'] = 'Verify Parameters' |
|
954 | 954 | |
|
955 | 955 | c_vel = 3.0*(10**8) # m/s |
|
956 | 956 | ope_freq = experiment.freq*(10**6) # 1/s |
|
957 | 957 | radar_lambda = c_vel/ope_freq # m |
|
958 | 958 | kwargs['radar_lambda'] = radar_lambda |
|
959 | 959 | |
|
960 | 960 | ipp = None |
|
961 | 961 | nsa = 1 |
|
962 | 962 | code_id = 0 |
|
963 | 963 | tx_line = {} |
|
964 | 964 | |
|
965 | 965 | for configuration in configurations.filter(device__device_type__name = 'rc'): |
|
966 | 966 | |
|
967 | 967 | if configuration.mix: |
|
968 | 968 | continue |
|
969 | 969 | conf = {'conf': configuration} |
|
970 | 970 | conf['keys'] = [] |
|
971 | 971 | conf['NTxs'] = configuration.ntx |
|
972 | 972 | conf['keys'].append('NTxs') |
|
973 | 973 | ipp = configuration.ipp |
|
974 | 974 | conf['IPP'] = ipp |
|
975 | 975 | conf['keys'].append('IPP') |
|
976 | 976 | lines = configuration.get_lines(line_type__name='tx') |
|
977 | 977 | |
|
978 | 978 | for tx_line in lines: |
|
979 | 979 | tx_params = json.loads(tx_line.params) |
|
980 | 980 | conf[tx_line.get_name()] = '{} Km'.format(tx_params['pulse_width']) |
|
981 | 981 | conf['keys'].append(tx_line.get_name()) |
|
982 | 982 | delays = tx_params['delays'] |
|
983 | 983 | if delays not in ('', '0'): |
|
984 | 984 | n = len(delays.split(',')) |
|
985 | 985 | taus = '{} Taus: {}'.format(n, delays) |
|
986 | 986 | else: |
|
987 | 987 | taus = '-' |
|
988 | 988 | conf['Taus ({})'.format(tx_line.get_name())] = taus |
|
989 | 989 | conf['keys'].append('Taus ({})'.format(tx_line.get_name())) |
|
990 | 990 | for code_line in configuration.get_lines(line_type__name='codes'): |
|
991 | 991 | code_params = json.loads(code_line.params) |
|
992 | 992 | code_id = code_params['code'] |
|
993 | 993 | if tx_line.pk == int(code_params['TX_ref']): |
|
994 | 994 | conf['Code ({})'.format(tx_line.get_name())] = '{}:{}'.format(RCLineCode.objects.get(pk=code_params['code']), |
|
995 | 995 | '-'.join(code_params['codes'])) |
|
996 | 996 | conf['keys'].append('Code ({})'.format(tx_line.get_name())) |
|
997 | 997 | |
|
998 | 998 | for windows_line in configuration.get_lines(line_type__name='windows'): |
|
999 | 999 | win_params = json.loads(windows_line.params) |
|
1000 | 1000 | if tx_line.pk == int(win_params['TX_ref']): |
|
1001 | 1001 | windows = '' |
|
1002 | 1002 | nsa = win_params['params'][0]['number_of_samples'] |
|
1003 | 1003 | for i, params in enumerate(win_params['params']): |
|
1004 | 1004 | windows += 'W{}: Ho={first_height} km DH={resolution} km NSA={number_of_samples}<br>'.format( |
|
1005 | 1005 | i, **params) |
|
1006 | 1006 | conf['Window'] = mark_safe(windows) |
|
1007 | 1007 | conf['keys'].append('Window') |
|
1008 | 1008 | |
|
1009 | 1009 | kwargs['configurations'].append(conf) |
|
1010 | 1010 | |
|
1011 | 1011 | for configuration in configurations.filter(device__device_type__name = 'jars'): |
|
1012 | 1012 | |
|
1013 | 1013 | conf = {'conf': configuration} |
|
1014 | 1014 | conf['keys'] = [] |
|
1015 | 1015 | conf['Type of Data'] = EXPERIMENT_TYPE[configuration.exp_type][1] |
|
1016 | 1016 | conf['keys'].append('Type of Data') |
|
1017 | 1017 | channels_number = configuration.channels_number |
|
1018 | 1018 | exp_type = configuration.exp_type |
|
1019 | 1019 | fftpoints = configuration.fftpoints |
|
1020 | 1020 | filter_parms = json.loads(configuration.filter_parms) |
|
1021 | 1021 | spectral_number = configuration.spectral_number |
|
1022 | 1022 | acq_profiles = configuration.acq_profiles |
|
1023 | 1023 | cohe_integr = configuration.cohe_integr |
|
1024 | 1024 | profiles_block = configuration.profiles_block |
|
1025 | 1025 | |
|
1026 | 1026 | conf['Num of Profiles'] = acq_profiles |
|
1027 | 1027 | conf['keys'].append('Num of Profiles') |
|
1028 | 1028 | |
|
1029 | 1029 | conf['Prof per Block'] = profiles_block |
|
1030 | 1030 | conf['keys'].append('Prof per Block') |
|
1031 | 1031 | |
|
1032 | 1032 | conf['Blocks per File'] = configuration.raw_data_blocks |
|
1033 | 1033 | conf['keys'].append('Blocks per File') |
|
1034 | 1034 | |
|
1035 | 1035 | if exp_type == 0: # Short |
|
1036 | 1036 | bytes_ = 2 |
|
1037 | 1037 | b = nsa*2*bytes_*channels_number |
|
1038 | 1038 | else: # Float |
|
1039 | 1039 | bytes_ = 4 |
|
1040 | 1040 | channels = channels_number + spectral_number |
|
1041 | 1041 | b = nsa*2*bytes_*fftpoints*channels |
|
1042 | 1042 | |
|
1043 | 1043 | codes_num = 7 |
|
1044 | 1044 | if code_id == 2: |
|
1045 | 1045 | codes_num = 7 |
|
1046 | 1046 | elif code_id == 12: |
|
1047 | 1047 | codes_num = 15 |
|
1048 | 1048 | |
|
1049 | 1049 | #Jars filter values: |
|
1050 | 1050 | |
|
1051 | 1051 | clock = float(filter_parms['clock']) |
|
1052 | 1052 | filter_2 = int(filter_parms['cic_2']) |
|
1053 | 1053 | filter_5 = int(filter_parms['cic_5']) |
|
1054 | 1054 | filter_fir = int(filter_parms['fir']) |
|
1055 | 1055 | Fs_MHz = clock/(filter_2*filter_5*filter_fir) |
|
1056 | 1056 | |
|
1057 | 1057 | #Jars values: |
|
1058 | 1058 | if ipp is not None: |
|
1059 | 1059 | IPP_units = ipp/0.15*Fs_MHz |
|
1060 | 1060 | IPP_us = IPP_units / Fs_MHz |
|
1061 | 1061 | IPP_s = IPP_units / (Fs_MHz * (10**6)) |
|
1062 | 1062 | Ts = 1/(Fs_MHz*(10**6)) |
|
1063 | 1063 | |
|
1064 | 1064 | Va = radar_lambda/(4*Ts*cohe_integr) |
|
1065 | 1065 | rate_bh = ((nsa-codes_num)*channels_number*2 * |
|
1066 | 1066 | bytes_/IPP_us)*(36*(10**8)/cohe_integr) |
|
1067 | 1067 | rate_gh = rate_bh/(1024*1024*1024) |
|
1068 | 1068 | |
|
1069 | 1069 | conf['Time per Block'] = IPP_s * profiles_block * cohe_integr |
|
1070 | 1070 | conf['keys'].append('Time per Block') |
|
1071 | 1071 | conf['Acq time'] = IPP_s * acq_profiles |
|
1072 | 1072 | conf['keys'].append('Acq time') |
|
1073 | 1073 | conf['Data rate'] = str(rate_gh)+" (GB/h)" |
|
1074 | 1074 | conf['keys'].append('Data rate') |
|
1075 | 1075 | conf['Va (m/s)'] = Va |
|
1076 | 1076 | conf['keys'].append('Va (m/s)') |
|
1077 | 1077 | conf['Vrange (m/s)'] = 3/(2*IPP_s*cohe_integr) |
|
1078 | 1078 | conf['keys'].append('Vrange (m/s)') |
|
1079 | 1079 | |
|
1080 | 1080 | kwargs['configurations'].append(conf) |
|
1081 | 1081 | kwargs['menu_experiments'] = 'active' |
|
1082 | 1082 | |
|
1083 | 1083 | ###### SIDEBAR ###### |
|
1084 | 1084 | kwargs.update(sidebar(experiment=experiment)) |
|
1085 | 1085 | |
|
1086 | 1086 | return render(request, 'experiment_summary.html', kwargs) |
|
1087 | 1087 | |
|
1088 | 1088 | |
|
1089 | 1089 | @login_required |
|
1090 | 1090 | def experiment_verify(request, id_exp): |
|
1091 | 1091 | |
|
1092 | 1092 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
1093 | 1093 | experiment_data = experiment.parms_to_dict() |
|
1094 | 1094 | configurations = Configuration.objects.filter( |
|
1095 | 1095 | experiment=experiment, type=0) |
|
1096 | 1096 | |
|
1097 | 1097 | kwargs = {} |
|
1098 | 1098 | |
|
1099 | 1099 | kwargs['experiment_keys'] = ['template', |
|
1100 | 1100 | 'radar_system', 'name', 'start_time', 'end_time'] |
|
1101 | 1101 | kwargs['experiment'] = experiment |
|
1102 | 1102 | |
|
1103 | 1103 | kwargs['configuration_keys'] = ['name', 'device__ip_address', |
|
1104 | 1104 | 'device__port_address', 'device__status'] |
|
1105 | 1105 | kwargs['configurations'] = configurations |
|
1106 | 1106 | kwargs['experiment_data'] = experiment_data |
|
1107 | 1107 | |
|
1108 | 1108 | kwargs['title'] = 'Verify Experiment' |
|
1109 | 1109 | kwargs['suptitle'] = 'Parameters' |
|
1110 | 1110 | |
|
1111 | 1111 | kwargs['button'] = 'Update' |
|
1112 | 1112 | |
|
1113 | 1113 | jars_conf = False |
|
1114 | 1114 | rc_conf = False |
|
1115 | 1115 | dds_conf = False |
|
1116 | 1116 | |
|
1117 | 1117 | for configuration in configurations: |
|
1118 | 1118 | #-------------------- JARS -----------------------: |
|
1119 | 1119 | if configuration.device.device_type.name == 'jars': |
|
1120 | 1120 | jars_conf = True |
|
1121 | 1121 | jars = configuration |
|
1122 | 1122 | kwargs['jars_conf'] = jars_conf |
|
1123 | 1123 | filter_parms = json.loads(jars.filter_parms) |
|
1124 | 1124 | kwargs['filter_parms'] = filter_parms |
|
1125 | 1125 | #--Sampling Frequency |
|
1126 | 1126 | clock = filter_parms['clock'] |
|
1127 | 1127 | filter_2 = filter_parms['cic_2'] |
|
1128 | 1128 | filter_5 = filter_parms['cic_5'] |
|
1129 | 1129 | filter_fir = filter_parms['fir'] |
|
1130 | 1130 | samp_freq_jars = clock/filter_2/filter_5/filter_fir |
|
1131 | 1131 | |
|
1132 | 1132 | kwargs['samp_freq_jars'] = samp_freq_jars |
|
1133 | 1133 | kwargs['jars'] = configuration |
|
1134 | 1134 | |
|
1135 | 1135 | #--------------------- RC ----------------------: |
|
1136 | 1136 | if configuration.device.device_type.name == 'rc' and not configuration.mix: |
|
1137 | 1137 | rc_conf = True |
|
1138 | 1138 | rc = configuration |
|
1139 | 1139 | |
|
1140 | 1140 | rc_parms = configuration.parms_to_dict() |
|
1141 | 1141 | |
|
1142 | 1142 | win_lines = rc.get_lines(line_type__name='windows') |
|
1143 | 1143 | if win_lines: |
|
1144 | 1144 | dh = json.loads(win_lines[0].params)['params'][0]['resolution'] |
|
1145 | 1145 | #--Sampling Frequency |
|
1146 | 1146 | samp_freq_rc = 0.15/dh |
|
1147 | 1147 | kwargs['samp_freq_rc'] = samp_freq_rc |
|
1148 | 1148 | |
|
1149 | 1149 | kwargs['rc_conf'] = rc_conf |
|
1150 | 1150 | kwargs['rc'] = configuration |
|
1151 | 1151 | |
|
1152 | 1152 | #-------------------- DDS ----------------------: |
|
1153 | 1153 | if configuration.device.device_type.name == 'dds': |
|
1154 | 1154 | dds_conf = True |
|
1155 | 1155 | dds = configuration |
|
1156 | 1156 | dds_parms = configuration.parms_to_dict() |
|
1157 | 1157 | |
|
1158 | 1158 | kwargs['dds_conf'] = dds_conf |
|
1159 | 1159 | kwargs['dds'] = configuration |
|
1160 | 1160 | |
|
1161 | 1161 | #------------Validation------------: |
|
1162 | 1162 | #Clock |
|
1163 | 1163 | if dds_conf and rc_conf and jars_conf: |
|
1164 | 1164 | if float(filter_parms['clock']) != float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']) and float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']) != float(dds_parms['configurations']['byId'][str(dds.pk)]['clock']): |
|
1165 | 1165 | messages.warning(request, "Devices don't have the same clock.") |
|
1166 | 1166 | elif rc_conf and jars_conf: |
|
1167 | 1167 | if float(filter_parms['clock']) != float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']): |
|
1168 | 1168 | messages.warning(request, "Devices don't have the same clock.") |
|
1169 | 1169 | elif rc_conf and dds_conf: |
|
1170 | 1170 | if float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']) != float(dds_parms['configurations']['byId'][str(dds.pk)]['clock']): |
|
1171 | 1171 | messages.warning(request, "Devices don't have the same clock.") |
|
1172 | 1172 | if float(samp_freq_rc) != float(dds_parms['configurations']['byId'][str(dds.pk)]['frequencyA']): |
|
1173 | 1173 | messages.warning( |
|
1174 | 1174 | request, "Devices don't have the same Frequency A.") |
|
1175 | 1175 | |
|
1176 | 1176 | #------------POST METHOD------------: |
|
1177 | 1177 | if request.method == 'POST': |
|
1178 | 1178 | if request.POST['suggest_clock']: |
|
1179 | 1179 | try: |
|
1180 | 1180 | suggest_clock = float(request.POST['suggest_clock']) |
|
1181 | 1181 | except: |
|
1182 | 1182 | messages.warning(request, "Invalid value in CLOCK IN.") |
|
1183 | 1183 | return redirect('url_verify_experiment', id_exp=experiment.id) |
|
1184 | 1184 | else: |
|
1185 | 1185 | suggest_clock = "" |
|
1186 | 1186 | if suggest_clock: |
|
1187 | 1187 | if rc_conf: |
|
1188 | 1188 | rc.clock_in = suggest_clock |
|
1189 | 1189 | rc.save() |
|
1190 | 1190 | if jars_conf: |
|
1191 | 1191 | filter_parms = jars.filter_parms |
|
1192 | 1192 | filter_parms = ast.literal_eval(filter_parms) |
|
1193 | 1193 | filter_parms['clock'] = suggest_clock |
|
1194 | 1194 | jars.filter_parms = json.dumps(filter_parms) |
|
1195 | 1195 | jars.save() |
|
1196 | 1196 | kwargs['filter_parms'] = filter_parms |
|
1197 | 1197 | if dds_conf: |
|
1198 | 1198 | dds.clock = suggest_clock |
|
1199 | 1199 | dds.save() |
|
1200 | 1200 | |
|
1201 | 1201 | if request.POST['suggest_frequencyA']: |
|
1202 | 1202 | try: |
|
1203 | 1203 | suggest_frequencyA = float(request.POST['suggest_frequencyA']) |
|
1204 | 1204 | except: |
|
1205 | 1205 | messages.warning(request, "Invalid value in FREQUENCY A.") |
|
1206 | 1206 | return redirect('url_verify_experiment', id_exp=experiment.id) |
|
1207 | 1207 | else: |
|
1208 | 1208 | suggest_frequencyA = "" |
|
1209 | 1209 | if suggest_frequencyA: |
|
1210 | 1210 | if jars_conf: |
|
1211 | 1211 | filter_parms = jars.filter_parms |
|
1212 | 1212 | filter_parms = ast.literal_eval(filter_parms) |
|
1213 | 1213 | filter_parms['fch'] = suggest_frequencyA |
|
1214 | 1214 | jars.filter_parms = json.dumps(filter_parms) |
|
1215 | 1215 | jars.save() |
|
1216 | 1216 | kwargs['filter_parms'] = filter_parms |
|
1217 | 1217 | if dds_conf: |
|
1218 | 1218 | dds.frequencyA_Mhz = request.POST['suggest_frequencyA'] |
|
1219 | 1219 | dds.save() |
|
1220 | 1220 | |
|
1221 | 1221 | kwargs['menu_experiments'] = 'active' |
|
1222 | 1222 | kwargs.update(sidebar(experiment=experiment)) |
|
1223 | 1223 | return render(request, 'experiment_verify.html', kwargs) |
|
1224 | 1224 | |
|
1225 | 1225 | |
|
1226 | 1226 | def parse_mix_result(s): |
|
1227 | 1227 | |
|
1228 | 1228 | values = s.split('-') |
|
1229 | 1229 | html = 'EXP MOD OPE DELAY MASK\r\n' |
|
1230 | 1230 | |
|
1231 | 1231 | if not values or values[0] in ('', ' '): |
|
1232 | 1232 | return mark_safe(html) |
|
1233 | 1233 | |
|
1234 | 1234 | for i, value in enumerate(values): |
|
1235 | 1235 | if not value: |
|
1236 | 1236 | continue |
|
1237 | 1237 | pk, mode, operation, delay, mask = value.split('|') |
|
1238 | 1238 | conf = RCConfiguration.objects.get(pk=pk) |
|
1239 | 1239 | if i == 0: |
|
1240 | 1240 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( |
|
1241 | 1241 | conf.name, |
|
1242 | 1242 | mode, |
|
1243 | 1243 | ' ', |
|
1244 | 1244 | delay, |
|
1245 | 1245 | mask) |
|
1246 | 1246 | else: |
|
1247 | 1247 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( |
|
1248 | 1248 | conf.name, |
|
1249 | 1249 | mode, |
|
1250 | 1250 | operation, |
|
1251 | 1251 | delay, |
|
1252 | 1252 | mask) |
|
1253 | 1253 | |
|
1254 | 1254 | return mark_safe(html) |
|
1255 | 1255 | |
|
1256 | 1256 | |
|
1257 | 1257 | def parse_mask(l): |
|
1258 | 1258 | |
|
1259 | 1259 | values = [] |
|
1260 | 1260 | |
|
1261 | 1261 | for x in range(16): |
|
1262 | 1262 | if '{}'.format(x) in l: |
|
1263 | 1263 | values.append(1) |
|
1264 | 1264 | else: |
|
1265 | 1265 | values.append(0) |
|
1266 | 1266 | |
|
1267 | 1267 | values.reverse() |
|
1268 | 1268 | |
|
1269 | 1269 | return int(''.join([str(x) for x in values]), 2) |
|
1270 | 1270 | |
|
1271 | 1271 | |
|
1272 | 1272 | def dev_confs(request): |
|
1273 | 1273 | |
|
1274 | 1274 | page = request.GET.get('page') |
|
1275 | 1275 | order = ('-programmed_date', ) |
|
1276 | 1276 | filters = request.GET.copy() |
|
1277 | 1277 | if 'my configurations' in filters: |
|
1278 | 1278 | filters.pop('my configurations', None) |
|
1279 | 1279 | filters['mine'] = request.user.id |
|
1280 | 1280 | kwargs = get_paginator(Configuration, page, order, filters) |
|
1281 | 1281 | fields = ['tags', 'template', 'historical'] |
|
1282 | 1282 | if request.user.is_authenticated: |
|
1283 | 1283 | fields.append('my configurations') |
|
1284 | 1284 | form = FilterForm(initial=request.GET, extra_fields=fields) |
|
1285 | 1285 | kwargs['keys'] = ['name', 'experiment', |
|
1286 | 1286 | 'type', 'programmed_date', 'actions'] |
|
1287 | 1287 | kwargs['title'] = 'Configuration' |
|
1288 | 1288 | kwargs['suptitle'] = 'List' |
|
1289 | 1289 | kwargs['no_sidebar'] = True |
|
1290 | 1290 | kwargs['form'] = form |
|
1291 | 1291 | kwargs['add_url'] = reverse('url_add_dev_conf', args=[0]) |
|
1292 | 1292 | filters = request.GET.copy() |
|
1293 | 1293 | filters.pop('page', None) |
|
1294 | 1294 | kwargs['q'] = urlencode(filters) |
|
1295 | 1295 | kwargs['menu_configurations'] = 'active' |
|
1296 | 1296 | |
|
1297 | 1297 | return render(request, 'base_list.html', kwargs) |
|
1298 | 1298 | |
|
1299 | 1299 | |
|
1300 | 1300 | def dev_conf(request, id_conf): |
|
1301 | 1301 | |
|
1302 | 1302 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1303 | 1303 | |
|
1304 | 1304 | return redirect(conf.get_absolute_url()) |
|
1305 | 1305 | |
|
1306 | 1306 | |
|
1307 | 1307 | @login_required |
|
1308 | 1308 | def dev_conf_new(request, id_exp=0, id_dev=0): |
|
1309 | 1309 | |
|
1310 | 1310 | if not is_developer(request.user): |
|
1311 | 1311 | messages.error( |
|
1312 | 1312 | request, 'Developer required, to create new configurations') |
|
1313 | 1313 | return redirect('index') |
|
1314 | 1314 | |
|
1315 | 1315 | initial = {} |
|
1316 | 1316 | kwargs = {} |
|
1317 | 1317 | |
|
1318 | 1318 | if id_exp != 0: |
|
1319 | 1319 | initial['experiment'] = id_exp |
|
1320 | 1320 | |
|
1321 | 1321 | if id_dev != 0: |
|
1322 | 1322 | initial['device'] = id_dev |
|
1323 | 1323 | |
|
1324 | 1324 | if request.method == 'GET': |
|
1325 | 1325 | |
|
1326 | 1326 | if id_dev: |
|
1327 | 1327 | kwargs['button'] = 'Create' |
|
1328 | 1328 | device = Device.objects.get(pk=id_dev) |
|
1329 | 1329 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
1330 | 1330 | initial['name'] = request.GET['name'] |
|
1331 | 1331 | form = DevConfForm(initial=initial) |
|
1332 | 1332 | else: |
|
1333 | 1333 | if 'template' in request.GET: |
|
1334 | 1334 | if request.GET['template'] == '0': |
|
1335 | 1335 | choices = [(conf.pk, '{}'.format(conf)) |
|
1336 | 1336 | for conf in Configuration.objects.filter(template=True)] |
|
1337 | 1337 | form = NewForm(initial={'create_from': 2}, |
|
1338 | 1338 | template_choices=choices) |
|
1339 | 1339 | else: |
|
1340 | 1340 | kwargs['button'] = 'Create' |
|
1341 | 1341 | conf = Configuration.objects.get( |
|
1342 | 1342 | pk=request.GET['template']) |
|
1343 | 1343 | id_dev = conf.device.pk |
|
1344 | 1344 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1345 | 1345 | form = DevConfForm(instance=conf, |
|
1346 | 1346 | initial={'name': '{}_{:%y%m%d}'.format(conf.name, datetime.now()), |
|
1347 | 1347 | 'template': False, |
|
1348 | 1348 | 'experiment': id_exp}) |
|
1349 | 1349 | elif 'blank' in request.GET: |
|
1350 | 1350 | kwargs['button'] = 'Create' |
|
1351 | 1351 | form = ConfigurationForm(initial=initial) |
|
1352 | 1352 | else: |
|
1353 | 1353 | form = NewForm() |
|
1354 | 1354 | |
|
1355 | 1355 | if request.method == 'POST': |
|
1356 | 1356 | |
|
1357 | 1357 | device = Device.objects.get(pk=request.POST['device']) |
|
1358 | 1358 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
1359 | 1359 | |
|
1360 | 1360 | form = DevConfForm(request.POST) |
|
1361 | 1361 | kwargs['button'] = 'Create' |
|
1362 | 1362 | if form.is_valid(): |
|
1363 | 1363 | conf = form.save(commit=False) |
|
1364 | 1364 | conf.author = request.user |
|
1365 | 1365 | conf.save() |
|
1366 | 1366 | |
|
1367 | 1367 | if 'template' in request.GET and conf.device.device_type.name == 'rc': |
|
1368 | 1368 | lines = RCLine.objects.filter( |
|
1369 | 1369 | rc_configuration=request.GET['template']) |
|
1370 | 1370 | for line in lines: |
|
1371 | 1371 | line.clone(rc_configuration=conf) |
|
1372 | 1372 | |
|
1373 | 1373 | new_lines = conf.get_lines() |
|
1374 | 1374 | for line in new_lines: |
|
1375 | 1375 | line_params = json.loads(line.params) |
|
1376 | 1376 | if 'TX_ref' in line_params: |
|
1377 | 1377 | ref_line = RCLine.objects.get(pk=line_params['TX_ref']) |
|
1378 | 1378 | line_params['TX_ref'] = ['{}'.format( |
|
1379 | 1379 | l.pk) for l in new_lines if l.get_name() == ref_line.get_name()][0] |
|
1380 | 1380 | line.params = json.dumps(line_params) |
|
1381 | 1381 | line.save() |
|
1382 | 1382 | elif conf.device.device_type.name == 'rc': |
|
1383 | 1383 | clk = RCClock(rc_configuration=conf) |
|
1384 | 1384 | clk.save() |
|
1385 | 1385 | |
|
1386 | 1386 | return redirect('url_dev_conf', id_conf=conf.pk) |
|
1387 | 1387 | |
|
1388 | 1388 | kwargs['id_exp'] = id_exp |
|
1389 | 1389 | kwargs['form'] = form |
|
1390 | 1390 | kwargs['title'] = 'Configuration' |
|
1391 | 1391 | kwargs['suptitle'] = 'New' |
|
1392 | 1392 | kwargs['menu_configurations'] = 'active' |
|
1393 | 1393 | |
|
1394 | 1394 | if id_dev != 0: |
|
1395 | 1395 | device = Device.objects.get(pk=id_dev) |
|
1396 | 1396 | kwargs['device'] = device.device_type.name |
|
1397 | 1397 | print(id_exp) |
|
1398 | 1398 | return render(request, 'dev_conf_edit.html', kwargs) |
|
1399 | 1399 | |
|
1400 | 1400 | |
|
1401 | 1401 | @login_required |
|
1402 | 1402 | def dev_conf_edit(request, id_conf): |
|
1403 | 1403 | |
|
1404 | 1404 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1405 | 1405 | |
|
1406 | 1406 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1407 | 1407 | |
|
1408 | 1408 | if request.method == 'GET': |
|
1409 | 1409 | form = DevConfForm(instance=conf) |
|
1410 | 1410 | |
|
1411 | 1411 | if request.method == 'POST': |
|
1412 | 1412 | form = DevConfForm(request.POST, instance=conf) |
|
1413 | 1413 | |
|
1414 | 1414 | if form.is_valid(): |
|
1415 | 1415 | form.save() |
|
1416 | 1416 | return redirect('url_dev_conf', id_conf=id_conf) |
|
1417 | 1417 | |
|
1418 | 1418 | kwargs = {} |
|
1419 | 1419 | kwargs['form'] = form |
|
1420 | 1420 | kwargs['title'] = 'Device Configuration' |
|
1421 | 1421 | kwargs['suptitle'] = 'Edit' |
|
1422 | 1422 | kwargs['button'] = 'Update' |
|
1423 | 1423 | kwargs['menu_configurations'] = 'active' |
|
1424 | 1424 | |
|
1425 | 1425 | ###### SIDEBAR ###### |
|
1426 | 1426 | kwargs.update(sidebar(conf=conf)) |
|
1427 | 1427 | |
|
1428 | 1428 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1429 | 1429 | |
|
1430 | 1430 | |
|
1431 | 1431 | @login_required |
|
1432 | 1432 | def dev_conf_start(request, id_conf): |
|
1433 | 1433 | |
|
1434 | 1434 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1435 | 1435 | |
|
1436 | 1436 | if conf.start_device(): |
|
1437 | 1437 | messages.success(request, conf.message) |
|
1438 | 1438 | else: |
|
1439 | 1439 | messages.error(request, conf.message) |
|
1440 | 1440 | |
|
1441 | 1441 | #conf.status_device() |
|
1442 | 1442 | |
|
1443 | 1443 | return redirect(conf.get_absolute_url()) |
|
1444 | 1444 | |
|
1445 | 1445 | |
|
1446 | 1446 | @login_required |
|
1447 | 1447 | def dev_conf_stop(request, id_conf): |
|
1448 | 1448 | |
|
1449 | 1449 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1450 | 1450 | |
|
1451 | 1451 | if conf.stop_device(): |
|
1452 | 1452 | messages.success(request, conf.message) |
|
1453 | 1453 | else: |
|
1454 | 1454 | messages.error(request, conf.message) |
|
1455 | 1455 | |
|
1456 | 1456 | #conf.status_device() |
|
1457 | 1457 | |
|
1458 | 1458 | return redirect(conf.get_absolute_url()) |
|
1459 | 1459 | |
|
1460 | 1460 | |
|
1461 | 1461 | @login_required |
|
1462 | 1462 | def dev_conf_status(request, id_conf): |
|
1463 | 1463 | |
|
1464 | 1464 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1465 | 1465 | |
|
1466 | 1466 | conf_active = Configuration.objects.filter(pk=conf.device.conf_active).first() |
|
1467 | 1467 | if conf_active!=conf: |
|
1468 | 1468 | url = '#' if conf_active is None else conf_active.get_absolute_url() |
|
1469 | 1469 | label = 'None' if conf_active is None else conf_active.label |
|
1470 | 1470 | messages.warning( |
|
1471 | 1471 | request, |
|
1472 | 1472 | mark_safe('The current configuration has not been written to device, the active configuration is <a href="{}">{}</a>'.format( |
|
1473 | 1473 | url, |
|
1474 | 1474 | label |
|
1475 | 1475 | )) |
|
1476 | 1476 | ) |
|
1477 | 1477 | |
|
1478 | 1478 | return redirect(conf.get_absolute_url()) |
|
1479 | 1479 | |
|
1480 | 1480 | if conf.status_device(): |
|
1481 | 1481 | messages.success(request, conf.message) |
|
1482 | 1482 | else: |
|
1483 | 1483 | messages.error(request, conf.message) |
|
1484 | 1484 | |
|
1485 | 1485 | return redirect(conf.get_absolute_url()) |
|
1486 | 1486 | |
|
1487 | 1487 | |
|
1488 | 1488 | @login_required |
|
1489 | 1489 | def dev_conf_reset(request, id_conf): |
|
1490 | 1490 | |
|
1491 | 1491 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1492 | 1492 | |
|
1493 | 1493 | if conf.reset_device(): |
|
1494 | 1494 | messages.success(request, conf.message) |
|
1495 | 1495 | else: |
|
1496 | 1496 | messages.error(request, conf.message) |
|
1497 | 1497 | |
|
1498 | 1498 | return redirect(conf.get_absolute_url()) |
|
1499 | 1499 | |
|
1500 | 1500 | |
|
1501 | 1501 | @login_required |
|
1502 | 1502 | def dev_conf_write(request, id_conf): |
|
1503 | 1503 | |
|
1504 | 1504 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1505 | 1505 | |
|
1506 | 1506 | if request.method == 'POST': |
|
1507 | 1507 | if conf.write_device(): |
|
1508 | 1508 | conf.device.conf_active = conf.pk |
|
1509 | 1509 | conf.device.save() |
|
1510 | 1510 | messages.success(request, conf.message) |
|
1511 | 1511 | if has_been_modified(conf): |
|
1512 | 1512 | conf.clone(type=1, template=False) |
|
1513 | 1513 | else: |
|
1514 | 1514 | messages.error(request, conf.message) |
|
1515 | 1515 | |
|
1516 | 1516 | return redirect(get_object_or_404(Configuration, pk=id_conf).get_absolute_url()) |
|
1517 | 1517 | |
|
1518 | 1518 | kwargs = { |
|
1519 | 1519 | 'title': 'Write Configuration', |
|
1520 | 1520 | 'suptitle': conf.label, |
|
1521 | 1521 | 'message': 'Are you sure yo want to write this {} configuration?'.format(conf.device), |
|
1522 | 1522 | 'delete': False |
|
1523 | 1523 | } |
|
1524 | 1524 | kwargs['menu_configurations'] = 'active' |
|
1525 | 1525 | |
|
1526 | 1526 | return render(request, 'confirm.html', kwargs) |
|
1527 | 1527 | |
|
1528 | 1528 | |
|
1529 | 1529 | @login_required |
|
1530 | 1530 | def dev_conf_read(request, id_conf): |
|
1531 | 1531 | |
|
1532 | 1532 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1533 | 1533 | |
|
1534 | 1534 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1535 | 1535 | |
|
1536 | 1536 | if request.method == 'GET': |
|
1537 | 1537 | |
|
1538 | 1538 | parms = conf.read_device() |
|
1539 | 1539 | #conf.status_device() |
|
1540 | 1540 | |
|
1541 | 1541 | if not parms: |
|
1542 | 1542 | messages.error(request, conf.message) |
|
1543 | 1543 | return redirect(conf.get_absolute_url()) |
|
1544 | 1544 | |
|
1545 | 1545 | form = DevConfForm(initial=parms, instance=conf) |
|
1546 | 1546 | |
|
1547 | 1547 | if request.method == 'POST': |
|
1548 | 1548 | form = DevConfForm(request.POST, instance=conf) |
|
1549 | 1549 | |
|
1550 | 1550 | if form.is_valid(): |
|
1551 | 1551 | form.save() |
|
1552 | 1552 | return redirect(conf.get_absolute_url()) |
|
1553 | 1553 | |
|
1554 | 1554 | messages.error(request, "Parameters could not be saved") |
|
1555 | 1555 | |
|
1556 | 1556 | kwargs = {} |
|
1557 | 1557 | kwargs['id_dev'] = conf.id |
|
1558 | 1558 | kwargs['form'] = form |
|
1559 | 1559 | kwargs['title'] = 'Device Configuration' |
|
1560 | 1560 | kwargs['suptitle'] = 'Parameters read from device' |
|
1561 | 1561 | kwargs['button'] = 'Save' |
|
1562 | 1562 | |
|
1563 | 1563 | ###### SIDEBAR ###### |
|
1564 | 1564 | kwargs.update(sidebar(conf=conf)) |
|
1565 | 1565 | |
|
1566 | 1566 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1567 | 1567 | |
|
1568 | 1568 | |
|
1569 | 1569 | @login_required |
|
1570 | 1570 | def dev_conf_import(request, id_conf): |
|
1571 | 1571 | |
|
1572 | 1572 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1573 | 1573 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1574 | 1574 | |
|
1575 | 1575 | if request.method == 'GET': |
|
1576 | 1576 | file_form = UploadFileForm() |
|
1577 | 1577 | |
|
1578 | 1578 | if request.method == 'POST': |
|
1579 | 1579 | file_form = UploadFileForm(request.POST, request.FILES) |
|
1580 | 1580 | |
|
1581 | 1581 | if file_form.is_valid(): |
|
1582 | 1582 | |
|
1583 | 1583 | data = conf.import_from_file(request.FILES['file']) |
|
1584 | 1584 | parms = Params(data=data).get_conf( |
|
1585 | 1585 | dtype=conf.device.device_type.name) |
|
1586 | 1586 | |
|
1587 | 1587 | if parms: |
|
1588 | 1588 | |
|
1589 | 1589 | form = DevConfForm(initial=parms, instance=conf) |
|
1590 | 1590 | |
|
1591 | 1591 | kwargs = {} |
|
1592 | 1592 | kwargs['id_dev'] = conf.id |
|
1593 | 1593 | kwargs['form'] = form |
|
1594 | 1594 | kwargs['title'] = 'Device Configuration' |
|
1595 | 1595 | kwargs['suptitle'] = 'Parameters imported' |
|
1596 | 1596 | kwargs['button'] = 'Save' |
|
1597 | 1597 | kwargs['action'] = conf.get_absolute_url_edit() |
|
1598 | 1598 | kwargs['previous'] = conf.get_absolute_url() |
|
1599 | 1599 | |
|
1600 | 1600 | ###### SIDEBAR ###### |
|
1601 | 1601 | kwargs.update(sidebar(conf=conf)) |
|
1602 | 1602 | |
|
1603 | 1603 | messages.success( |
|
1604 | 1604 | request, "Parameters imported from: '%s'." % request.FILES['file'].name) |
|
1605 | 1605 | |
|
1606 | 1606 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1607 | 1607 | |
|
1608 | 1608 | messages.error(request, "Could not import parameters from file") |
|
1609 | 1609 | |
|
1610 | 1610 | kwargs = {} |
|
1611 | 1611 | kwargs['id_dev'] = conf.id |
|
1612 | 1612 | kwargs['title'] = 'Device Configuration' |
|
1613 | 1613 | kwargs['form'] = file_form |
|
1614 | 1614 | kwargs['suptitle'] = 'Importing file' |
|
1615 | 1615 | kwargs['button'] = 'Import' |
|
1616 | 1616 | kwargs['menu_configurations'] = 'active' |
|
1617 | 1617 | |
|
1618 | 1618 | kwargs.update(sidebar(conf=conf)) |
|
1619 | 1619 | |
|
1620 | 1620 | return render(request, 'dev_conf_import.html', kwargs) |
|
1621 | 1621 | |
|
1622 | 1622 | |
|
1623 | 1623 | @login_required |
|
1624 | 1624 | def dev_conf_export(request, id_conf): |
|
1625 | 1625 | |
|
1626 | 1626 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1627 | 1627 | |
|
1628 | 1628 | if request.method == 'GET': |
|
1629 | 1629 | file_form = DownloadFileForm(conf.device.device_type.name) |
|
1630 | 1630 | |
|
1631 | 1631 | if request.method == 'POST': |
|
1632 | 1632 | file_form = DownloadFileForm( |
|
1633 | 1633 | conf.device.device_type.name, request.POST) |
|
1634 | 1634 | |
|
1635 | 1635 | if file_form.is_valid(): |
|
1636 | 1636 | fields = conf.export_to_file( |
|
1637 | 1637 | format=file_form.cleaned_data['format']) |
|
1638 | 1638 | if not fields['content']: |
|
1639 | 1639 | messages.error(request, conf.message) |
|
1640 | 1640 | return redirect(conf.get_absolute_url_export()) |
|
1641 | 1641 | response = HttpResponse(content_type=fields['content_type']) |
|
1642 | 1642 | response['Content-Disposition'] = 'attachment; filename="%s"' % fields['filename'] |
|
1643 | 1643 | response.write(fields['content']) |
|
1644 | 1644 | |
|
1645 | 1645 | return response |
|
1646 | 1646 | |
|
1647 | 1647 | messages.error(request, "Could not export parameters") |
|
1648 | 1648 | |
|
1649 | 1649 | kwargs = {} |
|
1650 | 1650 | kwargs['id_dev'] = conf.id |
|
1651 | 1651 | kwargs['title'] = 'Device Configuration' |
|
1652 | 1652 | kwargs['form'] = file_form |
|
1653 | 1653 | kwargs['suptitle'] = 'Exporting file' |
|
1654 | 1654 | kwargs['button'] = 'Export' |
|
1655 | 1655 | kwargs['menu_configurations'] = 'active' |
|
1656 | 1656 | |
|
1657 | 1657 | return render(request, 'dev_conf_export.html', kwargs) |
|
1658 | 1658 | |
|
1659 | 1659 | |
|
1660 | 1660 | @login_required |
|
1661 | 1661 | def dev_conf_delete(request, id_conf): |
|
1662 | 1662 | |
|
1663 | 1663 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1664 | 1664 | |
|
1665 | 1665 | if request.method == 'POST': |
|
1666 | 1666 | if is_developer(request.user): |
|
1667 | 1667 | conf.delete() |
|
1668 | 1668 | return redirect('url_dev_confs') |
|
1669 | 1669 | |
|
1670 | 1670 | messages.error(request, 'Not enough permission to delete this object') |
|
1671 | 1671 | return redirect(conf.get_absolute_url()) |
|
1672 | 1672 | |
|
1673 | 1673 | kwargs = { |
|
1674 | 1674 | 'title': 'Delete', |
|
1675 | 1675 | 'suptitle': 'Configuration', |
|
1676 | 1676 | 'object': conf, |
|
1677 | 1677 | 'delete': True |
|
1678 | 1678 | } |
|
1679 | 1679 | kwargs['menu_configurations'] = 'active' |
|
1680 | 1680 | |
|
1681 | 1681 | return render(request, 'confirm.html', kwargs) |
|
1682 | 1682 | |
|
1683 | 1683 | |
|
1684 | 1684 | def sidebar(**kwargs): |
|
1685 | 1685 | |
|
1686 | 1686 | side_data = {} |
|
1687 | 1687 | |
|
1688 | 1688 | conf = kwargs.get('conf', None) |
|
1689 | 1689 | experiment = kwargs.get('experiment', None) |
|
1690 | 1690 | |
|
1691 | 1691 | if not experiment: |
|
1692 | 1692 | experiment = conf.experiment |
|
1693 | 1693 | |
|
1694 | 1694 | if experiment: |
|
1695 | 1695 | side_data['experiment'] = experiment |
|
1696 | 1696 | campaign = experiment.campaign_set.all() |
|
1697 | 1697 | if campaign: |
|
1698 | 1698 | side_data['campaign'] = campaign[0] |
|
1699 | 1699 | experiments = campaign[0].experiments.all().order_by('name') |
|
1700 | 1700 | else: |
|
1701 | 1701 | experiments = [experiment] |
|
1702 | 1702 | configurations = experiment.configuration_set.filter(type=0) |
|
1703 | 1703 | side_data['side_experiments'] = experiments |
|
1704 | 1704 | side_data['side_configurations'] = configurations.order_by( |
|
1705 | 1705 | 'device__device_type__name') |
|
1706 | 1706 | |
|
1707 | 1707 | return side_data |
|
1708 | 1708 | |
|
1709 | 1709 | def get_paginator(model, page, order, filters={}, n=8): |
|
1710 | 1710 | kwargs = {} |
|
1711 | 1711 | query = Q() |
|
1712 | 1712 | if isinstance(filters, QueryDict): |
|
1713 | 1713 | filters = filters.dict() |
|
1714 | 1714 | copy_filters=filters.copy() |
|
1715 | 1715 | [filters.pop(key) for key in copy_filters.keys() if copy_filters[key] in (' ', ' ')] |
|
1716 | 1716 | filters.pop('page', None) |
|
1717 | 1717 | |
|
1718 | 1718 | fields = [f.name for f in model._meta.get_fields()] |
|
1719 | 1719 | |
|
1720 | 1720 | if 'template' in copy_filters: |
|
1721 | 1721 | filters['template'] = True |
|
1722 | 1722 | if 'historical' in copy_filters: |
|
1723 | 1723 | filters.pop('historical') |
|
1724 | 1724 | filters['type'] = 1 |
|
1725 | 1725 | elif 'type' in fields: |
|
1726 | 1726 | filters['type'] = 0 |
|
1727 | 1727 | if 'start_date' in copy_filters: |
|
1728 | 1728 | filters['start_date__gte'] =filters.pop('start_date') |
|
1729 | 1729 | if 'end_date' in copy_filters: |
|
1730 | 1730 | filters['start_date__lte'] =filters.pop('end_date') |
|
1731 | 1731 | if 'tags' in copy_filters: |
|
1732 | 1732 | tags =filters.pop('tags') |
|
1733 | 1733 | if 'tags' in fields: |
|
1734 | 1734 | query = query | Q(tags__icontains=tags) |
|
1735 | 1735 | if 'label' in fields: |
|
1736 | 1736 | query = query | Q(label__icontains=tags) |
|
1737 | 1737 | if 'location' in fields: |
|
1738 | 1738 | query = query | Q(location__name__icontains=tags) |
|
1739 | 1739 | if 'device' in fields: |
|
1740 | 1740 | query = query | Q(device__device_type__name__icontains=tags) |
|
1741 | 1741 | query = query | Q(device__location__name__icontains=tags) |
|
1742 | 1742 | if 'device_type' in fields: |
|
1743 | 1743 | query = query | Q(device_type__name__icontains=tags) |
|
1744 | 1744 | |
|
1745 | 1745 | if 'mine' in copy_filters: |
|
1746 | 1746 | filters['author_id'] =filters['mine'] |
|
1747 | 1747 | filters.pop('mine') |
|
1748 | 1748 | object_list = model.objects.filter(query, **filters).order_by(*order) |
|
1749 | 1749 | paginator = Paginator(object_list, n) |
|
1750 | 1750 | |
|
1751 | 1751 | try: |
|
1752 | 1752 | objects = paginator.page(page) |
|
1753 | 1753 | except PageNotAnInteger: |
|
1754 | 1754 | objects = paginator.page(1) |
|
1755 | 1755 | except EmptyPage: |
|
1756 | 1756 | objects = paginator.page(paginator.num_pages) |
|
1757 | 1757 | |
|
1758 | 1758 | kwargs['objects'] = objects |
|
1759 | 1759 | kwargs['offset'] = (int(page)-1)*n if page else 0 |
|
1760 | 1760 | |
|
1761 | 1761 | return kwargs |
|
1762 | 1762 | |
|
1763 | 1763 | # def get_paginator(model, page, order, filters={}, n=8): |
|
1764 | 1764 | # kwargs = {} |
|
1765 | 1765 | # query = Q() |
|
1766 | 1766 | # if isinstance(filters, QueryDict): |
|
1767 | 1767 | # filters = filters.dict() |
|
1768 | 1768 | # [filters.pop(key) for key in filters.keys() if filters[key] in ('', ' ')] |
|
1769 | 1769 | # filters.pop('page', None) |
|
1770 | 1770 | |
|
1771 | 1771 | # fields = [f.name for f in model._meta.get_fields()] |
|
1772 | 1772 | |
|
1773 | 1773 | # if 'template' in filters: |
|
1774 | 1774 | # filters['template'] = True |
|
1775 | 1775 | # if 'historical' in filters: |
|
1776 | 1776 | # filters.pop('historical') |
|
1777 | 1777 | # filters['type'] = 1 |
|
1778 | 1778 | # elif 'type' in fields: |
|
1779 | 1779 | # filters['type'] = 0 |
|
1780 | 1780 | # if 'start_date' in filters: |
|
1781 | 1781 | # filters['start_date__gte'] = filters.pop('start_date') |
|
1782 | 1782 | # if 'end_date' in filters: |
|
1783 | 1783 | # filters['start_date__lte'] = filters.pop('end_date') |
|
1784 | 1784 | # if 'tags' in filters: |
|
1785 | 1785 | # tags = filters.pop('tags') |
|
1786 | 1786 | # if 'tags' in fields: |
|
1787 | 1787 | # query = query | Q(tags__icontains=tags) |
|
1788 | 1788 | # if 'label' in fields: |
|
1789 | 1789 | # query = query | Q(label__icontains=tags) |
|
1790 | 1790 | # if 'location' in fields: |
|
1791 | 1791 | # query = query | Q(location__name__icontains=tags) |
|
1792 | 1792 | # if 'device' in fields: |
|
1793 | 1793 | # query = query | Q(device__device_type__name__icontains=tags) |
|
1794 | 1794 | # query = query | Q(device__location__name__icontains=tags) |
|
1795 | 1795 | # if 'device_type' in fields: |
|
1796 | 1796 | # query = query | Q(device_type__name__icontains=tags) |
|
1797 | 1797 | |
|
1798 | 1798 | # if 'mine' in filters: |
|
1799 | 1799 | # filters['author_id'] = filters['mine'] |
|
1800 | 1800 | # filters.pop('mine') |
|
1801 | 1801 | # object_list = model.objects.filter(query, **filters).order_by(*order) |
|
1802 | 1802 | # paginator = Paginator(object_list, n) |
|
1803 | 1803 | |
|
1804 | 1804 | # try: |
|
1805 | 1805 | # objects = paginator.page(page) |
|
1806 | 1806 | # except PageNotAnInteger: |
|
1807 | 1807 | # objects = paginator.page(1) |
|
1808 | 1808 | # except EmptyPage: |
|
1809 | 1809 | # objects = paginator.page(paginator.num_pages) |
|
1810 | 1810 | |
|
1811 | 1811 | # kwargs['objects'] = objects |
|
1812 | 1812 | # kwargs['offset'] = (int(page)-1)*n if page else 0 |
|
1813 | 1813 | |
|
1814 | 1814 | # return kwargs |
|
1815 | 1815 | |
|
1816 | 1816 | |
|
1817 | 1817 | def operation(request, id_camp=None): |
|
1818 | 1818 | |
|
1819 | 1819 | kwargs = {} |
|
1820 | 1820 | kwargs['title'] = 'Radars Operation' |
|
1821 | 1821 | kwargs['no_sidebar'] = True |
|
1822 | 1822 | kwargs['menu_operation'] = 'active' |
|
1823 | 1823 | campaigns = Campaign.objects.filter(start_date__lte=datetime.now(), |
|
1824 | 1824 | end_date__gte=datetime.now()).order_by('-start_date') |
|
1825 | 1825 | |
|
1826 | 1826 | if id_camp: |
|
1827 | 1827 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
1828 | 1828 | form = OperationForm( |
|
1829 | 1829 | initial={'campaign': campaign.id}, campaigns=campaigns) |
|
1830 | 1830 | kwargs['campaign'] = campaign |
|
1831 | 1831 | else: |
|
1832 | 1832 | # form = OperationForm(campaigns=campaigns) |
|
1833 | 1833 | kwargs['campaigns'] = campaigns |
|
1834 | 1834 | return render(request, 'operation.html', kwargs) |
|
1835 | 1835 | |
|
1836 | 1836 | #---Experiment |
|
1837 | 1837 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] |
|
1838 | 1838 | kwargs['experiment_keys'] = keys[1:] |
|
1839 | 1839 | kwargs['experiments'] = experiments |
|
1840 | 1840 | #---Radar |
|
1841 | 1841 | kwargs['locations'] = campaign.get_experiments_by_radar() |
|
1842 | 1842 | kwargs['form'] = form |
|
1843 | 1843 | |
|
1844 | 1844 | return render(request, 'operation.html', kwargs) |
|
1845 | 1845 | |
|
1846 | 1846 | |
|
1847 | 1847 | @login_required |
|
1848 | 1848 | def radar_start(request, id_camp, id_radar): |
|
1849 | 1849 | |
|
1850 | 1850 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
1851 | 1851 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] |
|
1852 | 1852 | now = datetime.now() |
|
1853 | 1853 | |
|
1854 | 1854 | for exp in experiments: |
|
1855 | 1855 | #app.control.revoke(exp.task) |
|
1856 | 1856 | print("----------------------") |
|
1857 | 1857 | print("status:->", exp.status) |
|
1858 | 1858 | start = datetime.combine(datetime.now().date(), exp.start_time) |
|
1859 | 1859 | end = datetime.combine(datetime.now().date(), exp.end_time) |
|
1860 | 1860 | print("start exp: ",exp.start_time) |
|
1861 | 1861 | print("end exp: ",exp.end_time) |
|
1862 | 1862 | |
|
1863 | 1863 | print("start comb: ",start) |
|
1864 | 1864 | print("end comb: ",end) |
|
1865 | 1865 | print(is_aware(start)) |
|
1866 | 1866 | print("start camp",campaign.start_date) |
|
1867 | 1867 | print("end camp",campaign.end_date) |
|
1868 | 1868 | print(is_aware(campaign.start_date)) |
|
1869 | 1869 | if end < start: |
|
1870 | 1870 | end += timedelta(1) |
|
1871 | 1871 | |
|
1872 | 1872 | if exp.status == 2: |
|
1873 | 1873 | messages.warning( |
|
1874 | 1874 | request, 'Experiment {} already running'.format(exp)) |
|
1875 | 1875 | #continue |
|
1876 | 1876 | |
|
1877 | 1877 | if exp.status == 3: |
|
1878 | 1878 | messages.warning( |
|
1879 | 1879 | request, 'Experiment {} already programmed'.format(exp)) |
|
1880 | 1880 | #continue |
|
1881 | 1881 | |
|
1882 | 1882 | if exp.status == 1: |
|
1883 | 1883 | messages.warning( |
|
1884 | 1884 | request, 'Experiment {} stopped'.format(exp)) |
|
1885 | 1885 | #continue |
|
1886 | 1886 | |
|
1887 | 1887 | if start > campaign.end_date: |
|
1888 | 1888 | messages.warning( |
|
1889 | 1889 | request, 'Experiment {} out of date'.format(exp)) |
|
1890 | 1890 | |
|
1891 | 1891 | #app.control.revoke(exp.task) |
|
1892 | 1892 | print("Llego luego del revoke") |
|
1893 | 1893 | if now >= start and now <= end: |
|
1894 | 1894 | |
|
1895 | 1895 | print("Caso now > start and < end -- (1)") |
|
1896 | 1896 | |
|
1897 | 1897 | # ------------------------------------------- |
|
1898 | 1898 | |
|
1899 | 1899 | # task = task_start.delay(exp.id) |
|
1900 | 1900 | # exp.task = task.id |
|
1901 | 1901 | # exp.status = task.get() |
|
1902 | 1902 | # ------------------------------------------- |
|
1903 | 1903 | |
|
1904 | 1904 | #exp.status = task.wait() |
|
1905 | 1905 | |
|
1906 | 1906 | if exp.status == 0: |
|
1907 | 1907 | messages.error(request, 'Experiment {} not start'.format(exp)) |
|
1908 | 1908 | if exp.status == 2: |
|
1909 | 1909 | messages.success(request, 'Experiment {} started'.format(exp)) |
|
1910 | 1910 | elif now < start: |
|
1911 | 1911 | print("Caso now <= start -- (2)",exp.pk) |
|
1912 | 1912 | #task = task_start.apply_async((exp.pk, ), eta=start)#start+timedelta(hours=5)) |
|
1913 | 1913 | # task = task_start.apply_async((exp.pk, ), eta=start+timedelta(hours=5))#) |
|
1914 | 1914 | # exp.task = task.id |
|
1915 | 1915 | # exp.status = 3 |
|
1916 | 1916 | messages.success(request, 'Experiment {} programmed to start at {}'.format(exp, start)) |
|
1917 | 1917 | else: |
|
1918 | 1918 | print("Caso now > end -- (3)") |
|
1919 | 1919 | exp.status = 4 |
|
1920 | 1920 | messages.warning( |
|
1921 | 1921 | request, 'Experiment {} out of date'.format(exp)) |
|
1922 | 1922 | |
|
1923 | 1923 | exp.save() |
|
1924 | 1924 | |
|
1925 | 1925 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1926 | 1926 | |
|
1927 | 1927 | |
|
1928 | 1928 | @login_required |
|
1929 | 1929 | def radar_stop(request, id_camp, id_radar): |
|
1930 | 1930 | |
|
1931 | 1931 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
1932 | 1932 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] |
|
1933 | 1933 | print("Ingreso en stop radar_stop") |
|
1934 | 1934 | #for exp in experiments: |
|
1935 | 1935 | |
|
1936 | 1936 | # if exp.task: |
|
1937 | 1937 | # print("Ingreso antes de revoke stop") |
|
1938 | 1938 | # app.control.revoke(exp.task) |
|
1939 | 1939 | |
|
1940 | 1940 | |
|
1941 | 1941 | # if exp.status == 2: #status 2 es started |
|
1942 | 1942 | # print("llama a exp.stop") |
|
1943 | 1943 | # exp.stop() |
|
1944 | 1944 | # messages.warning(request, 'Experiment {} stopped'.format(exp)) |
|
1945 | 1945 | # exp.status = 1 |
|
1946 | 1946 | # exp.save() |
|
1947 | 1947 | |
|
1948 | 1948 | #return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1949 | 1949 | |
|
1950 | 1950 | |
|
1951 | 1951 | @login_required |
|
1952 | 1952 | def radar_refresh(request, id_camp, id_radar): |
|
1953 | 1953 | |
|
1954 | 1954 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
1955 | 1955 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] |
|
1956 | 1956 | |
|
1957 | 1957 | i = app.control.inspect() |
|
1958 | 1958 | print("inspect",i) |
|
1959 | 1959 | print(i.scheduled()) |
|
1960 | 1960 | print(i.scheduled().values()) |
|
1961 | 1961 | scheduled = list(i.scheduled().values())[0] |
|
1962 | 1962 | revoked = list(i.revoked().values())[0] |
|
1963 | 1963 | |
|
1964 | 1964 | # for exp in experiments: |
|
1965 | 1965 | # if exp.task in revoked: |
|
1966 | 1966 | # exp.status = 1 |
|
1967 | 1967 | # elif exp.task in [t['request']['id'] for t in scheduled if 'task_stop' in t['request']['name']]: |
|
1968 | 1968 | # exp.status = 2 |
|
1969 | 1969 | # elif exp.task in [t['request']['id'] for t in scheduled if 'task_start' in t['request']['name']]: |
|
1970 | 1970 | # exp.status = 3 |
|
1971 | 1971 | # else: |
|
1972 | 1972 | # exp.status = 4 |
|
1973 | 1973 | # exp.save() |
|
1974 | 1974 | # return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1975 | 1975 | |
|
1976 | 1976 | #@login_required |
|
1977 | 1977 | # def revoke_tasks(request, id_camp): |
|
1978 | 1978 | |
|
1979 | 1979 | # i = app.control.inspect() |
|
1980 | 1980 | # scheduled = list(i.scheduled().values())[0] |
|
1981 | 1981 | # revoked = list(i.revoked().values())[0] |
|
1982 | 1982 | |
|
1983 | 1983 | # for t in scheduled: |
|
1984 | 1984 | # if t['request']['id'] in revoked: |
|
1985 | 1985 | # continue |
|
1986 | 1986 | # app.control.revoke(t['request']['id']) |
|
1987 | 1987 | # exp = Experiment.objects.get(pk=eval(str(t['request']['args']))[0]) |
|
1988 | 1988 | # eta = t['eta'] |
|
1989 | 1989 | # #task = t['request']['name'].split('.')[-1] |
|
1990 | 1990 | # messages.warning(request, 'Scheduled {} at {} for experiment {} revoked'.format(task, eta, exp.name)) |
|
1991 | 1991 | |
|
1992 | 1992 | # return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1993 | 1993 | |
|
1994 | 1994 | # @login_required |
|
1995 | 1995 | # def show_tasks(request, id_camp): |
|
1996 | 1996 | |
|
1997 | 1997 | # i = app.control.inspect() |
|
1998 | 1998 | # scheduled = list(i.scheduled().values())[0] |
|
1999 | 1999 | # revoked = list(i.revoked().values())[0] |
|
2000 | 2000 | |
|
2001 | 2001 | # for t in scheduled: |
|
2002 | 2002 | # if t['request']['id'] in revoked: |
|
2003 | 2003 | # continue |
|
2004 | 2004 | # exp = Experiment.objects.get(pk=eval(str(t['request']['args']))[0]) |
|
2005 | 2005 | # eta = t['eta'] |
|
2006 | 2006 | # #task = t['request']['name'].split('.')[-1] |
|
2007 | 2007 | # #messages.success(request, 'Task {} scheduled at {} for experiment {}'.format(task, eta, exp.name)) |
|
2008 | 2008 | |
|
2009 | 2009 | # return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
2010 | 2010 | |
|
2011 | 2011 | def real_time(request): |
|
2012 | 2012 | |
|
2013 | 2013 | graphic_path = "/home/fiorella/Pictures/catwbeanie.jpg" |
|
2014 | 2014 | |
|
2015 | 2015 | kwargs = {} |
|
2016 | 2016 | kwargs['title'] = 'CLAIRE' |
|
2017 | 2017 | kwargs['suptitle'] = 'Real Time' |
|
2018 | 2018 | kwargs['no_sidebar'] = True |
|
2019 | 2019 | kwargs['graphic_path'] = graphic_path |
|
2020 | 2020 | kwargs['graphic1_path'] = 'http://www.bluemaize.net/im/girls-accessories/shark-beanie-11.jpg' |
|
2021 | 2021 | |
|
2022 | 2022 | return render(request, 'real_time.html', kwargs) |
|
2023 | 2023 | |
|
2024 | 2024 | def theme(request, theme): |
|
2025 | 2025 | |
|
2026 | 2026 | user = request.user |
|
2027 | 2027 | user.profile.theme = theme |
|
2028 | 2028 | user.save() |
|
2029 | 2029 | return redirect('index') |
General Comments 0
You need to be logged in to leave comments.
Login now