@@ -1,283 +1,305 | |||
|
1 | 1 | import json |
|
2 | 2 | import requests |
|
3 | 3 | |
|
4 | 4 | from django.db import models |
|
5 | 5 | from django.core.validators import MinValueValidator, MaxValueValidator |
|
6 | 6 | from django.core.urlresolvers import reverse |
|
7 | 7 | |
|
8 | 8 | from apps.main.models import Configuration |
|
9 | 9 | from apps.main.utils import Params |
|
10 | ||
|
10 | 11 | # Create your models here. |
|
11 | 12 | |
|
12 | 13 | EXPERIMENT_TYPE = ( |
|
13 | 14 | (0, 'RAW_DATA'), |
|
14 | 15 | (1, 'PDATA'), |
|
15 | 16 | ) |
|
16 | 17 | |
|
17 | 18 | DATA_TYPE = ( |
|
18 | 19 | (0, 'SHORT'), |
|
19 | 20 | (1, 'FLOAT'), |
|
20 | 21 | ) |
|
21 | 22 | |
|
22 | 23 | DECODE_TYPE = ( |
|
23 | 24 | (0, 'None'), |
|
24 | 25 | (1, 'TimeDomain'), |
|
25 | 26 | (2, 'FreqDomain'), |
|
26 | 27 | (3, 'InvFreqDomain'), |
|
27 | 28 | ) |
|
28 | 29 | |
|
29 | 30 | class JARSfilter(models.Model): |
|
30 | 31 | |
|
31 | 32 | JARS_NBITS = 32 |
|
32 | 33 | |
|
33 | 34 | name = models.CharField(max_length=60, unique=True, default='') |
|
34 | 35 | clock = models.FloatField(verbose_name='Clock In (MHz)',validators=[MinValueValidator(5), MaxValueValidator(75)], null=True, default=60) |
|
35 | 36 | mult = models.PositiveIntegerField(verbose_name='Multiplier',validators=[MinValueValidator(1), MaxValueValidator(20)], default=5) |
|
36 | 37 | fch = models.FloatField(verbose_name='Frequency (MHz)', validators=[MaxValueValidator(150)], null=True, default=49.9200) |
|
37 | 38 | fch_decimal = models.BigIntegerField(verbose_name='Frequency (Decimal)',validators=[MinValueValidator(-9223372036854775808), MaxValueValidator(2**JARS_NBITS-1)], null=True, default=721554505) |
|
38 | 39 | filter_2 = models.PositiveIntegerField(verbose_name='Filter 2',validators=[MinValueValidator(2), MaxValueValidator(100)], default = 10) |
|
39 | 40 | filter_5 = models.PositiveIntegerField(verbose_name='Filter 5',validators=[MinValueValidator(1), MaxValueValidator(100)], default = 1) |
|
40 | 41 | filter_fir = models.PositiveIntegerField(verbose_name='FIR Filter',validators=[MinValueValidator(1), MaxValueValidator(100)], default = 6) |
|
41 | 42 | |
|
42 | 43 | class Meta: |
|
43 | 44 | db_table = 'jars_filters' |
|
44 | 45 | |
|
45 | 46 | def __unicode__(self): |
|
46 | 47 | return u'%s' % (self.name) |
|
47 | 48 | |
|
48 | 49 | def parms_to_dict(self): |
|
49 | 50 | |
|
50 | 51 | parameters = {} |
|
51 | 52 | |
|
52 | 53 | #parameters['name'] = self.name |
|
53 | 54 | parameters['clock'] = float(self.clock) |
|
54 | 55 | parameters['mult'] = int(self.mult) |
|
55 | 56 | parameters['fch'] = float(self.fch) |
|
56 | 57 | parameters['fch_decimal'] = int(self.fch) |
|
57 | 58 | parameters['filter_fir'] = int(self.filter_fir) |
|
58 | 59 | parameters['filter_2'] = int(self.filter_2) |
|
59 | 60 | parameters['filter_5'] = int(self.filter_5) |
|
60 | 61 | |
|
61 | 62 | return parameters |
|
62 | 63 | |
|
63 | 64 | def dict_to_parms(self, parameters): |
|
64 | 65 | |
|
65 | 66 | #self.name = parameters['name'] |
|
66 | 67 | self.clock = parameters['clock'] |
|
67 | 68 | self.mult = parameters['mult'] |
|
68 | 69 | self.fch = parameters['fch'] |
|
69 | 70 | self.fch_decimal = parameters['fch_decimal'] |
|
70 | 71 | self.filter_fir = parameters['filter_fir'] |
|
71 | 72 | self.filter_2 = parameters['filter_2'] |
|
72 | 73 | self.filter_5 = parameters['filter_5'] |
|
73 | 74 | |
|
74 | 75 | |
|
75 | 76 | class JARSConfiguration(Configuration): |
|
76 | 77 | |
|
77 | 78 | ADC_RESOLUTION = 8 |
|
78 | 79 | PCI_DIO_BUSWIDTH = 32 |
|
79 | 80 | HEADER_VERSION = 1103 |
|
80 | 81 | BEGIN_ON_START = True |
|
81 | 82 | REFRESH_RATE = 1 |
|
82 | 83 | |
|
83 | 84 | exp_type = models.PositiveIntegerField(verbose_name='Experiment Type', choices=EXPERIMENT_TYPE, default=0) |
|
84 | 85 | cards_number = models.PositiveIntegerField(verbose_name='Number of Cards', validators=[MinValueValidator(1), MaxValueValidator(4)], default = 1) |
|
85 | 86 | channels_number = models.PositiveIntegerField(verbose_name='Number of Channels', validators=[MinValueValidator(1), MaxValueValidator(8)], default = 5) |
|
86 | 87 | channels = models.CharField(verbose_name='Channels', max_length=15, default = '1,2,3,4,5') |
|
87 | 88 | data_type = models.PositiveIntegerField(verbose_name='Data Type', choices=DATA_TYPE, default=0) |
|
88 | 89 | raw_data_blocks = models.PositiveIntegerField(verbose_name='Raw Data Blocks', validators=[MaxValueValidator(5000)], default=60) |
|
89 | 90 | profiles_block = models.PositiveIntegerField(verbose_name='Profiles Per Block', default=400) |
|
90 | 91 | acq_profiles = models.PositiveIntegerField(verbose_name='Acquired Profiles', default=400) |
|
91 | 92 | ftp_interval = models.PositiveIntegerField(verbose_name='FTP Interval', default=60) |
|
92 | 93 | fftpoints = models.PositiveIntegerField(verbose_name='FFT Points',default=16) |
|
93 | 94 | cohe_integr_str = models.PositiveIntegerField(verbose_name='Coh. Int. Stride',validators=[MinValueValidator(1)], default=30) |
|
94 | 95 | cohe_integr = models.PositiveIntegerField(verbose_name='Coherent Integrations',validators=[MinValueValidator(1)], default=30) |
|
95 | 96 | incohe_integr = models.PositiveIntegerField(verbose_name='Incoherent Integrations',validators=[MinValueValidator(1)], default=30) |
|
96 | 97 | decode_data = models.PositiveIntegerField(verbose_name='Decode Data', choices=DECODE_TYPE, default=0) |
|
97 | 98 | post_coh_int = models.BooleanField(verbose_name='Post Coherent Integration', default=False) |
|
98 | 99 | spectral_number = models.PositiveIntegerField(verbose_name='# Spectral Combinations',validators=[MinValueValidator(1)], default=1) |
|
99 | 100 | spectral = models.CharField(verbose_name='Combinations', max_length=5000, default = '[0, 0],') |
|
100 | 101 | create_directory = models.BooleanField(verbose_name='Create Directory Per Day', default=True) |
|
101 | 102 | include_expname = models.BooleanField(verbose_name='Experiment Name in Directory', default=False) |
|
102 | 103 | #view_raw_data = models.BooleanField(verbose_name='View Raw Data', default=True) |
|
103 | 104 | save_ch_dc = models.BooleanField(verbose_name='Save Channels DC', default=True) |
|
104 | 105 | save_data = models.BooleanField(verbose_name='Save Data', default=True) |
|
105 |
filter_parms = models.CharField(max_length=10000, default='{"clock": 60, "mult": 5, "fch": 49.92, "fch_decimal": 721554506, "filter_fir": 2, "filter_2": 12, "filter_5": 25} |
|
|
106 | filter_parms = models.CharField(max_length=10000, default='{"clock": 60, "mult": 5, "fch": 49.92, "fch_decimal": 721554506, "filter_fir": 2, "filter_2": 12, "filter_5": 25}') | |
|
106 | 107 | |
|
107 | 108 | class Meta: |
|
108 | 109 | db_table = 'jars_configurations' |
|
109 | 110 | |
|
110 | 111 | def filter_resolution(self): |
|
111 | 112 | filter_parms = eval(self.filter_parms) |
|
112 | 113 | if filter_parms.__class__.__name__=='str': |
|
113 | 114 | filter_parms = eval(filter_parms) |
|
114 | 115 | |
|
115 | filter_clock = filter_parms['clock'] | |
|
116 | filter_clock = float(filter_parms['clock']) | |
|
116 | 117 | filter_2 = filter_parms['filter_2'] |
|
117 | 118 | filter_5 = filter_parms['filter_5'] |
|
118 | 119 | filter_fir = filter_parms['filter_fir'] |
|
119 | 120 | |
|
120 | 121 | resolution = round((filter_clock/(filter_2*filter_5*filter_fir)),2) |
|
121 | 122 | return resolution |
|
122 | 123 | |
|
123 | 124 | def dict_to_parms(self, params, id=None): |
|
124 | 125 | |
|
125 | 126 | if id is not None: |
|
126 | 127 | data = Params(params).get_conf(id_conf=id) |
|
127 | 128 | else: |
|
128 | 129 | data = Params(params).get_conf(dtype='jars') |
|
129 | 130 | |
|
130 | 131 | self.name = data['name'] |
|
131 | 132 | self.exp_type = data['exp_type'] |
|
132 | 133 | #if data['exp_type'] in (1, '1') : |
|
133 | 134 | self.incohe_integr = data['incohe_integr'] |
|
134 | 135 | self.spectral_number = data['spectral_number'] |
|
135 | 136 | self.spectral = data['spectral'] |
|
136 | 137 | #self.pd_directory = data['pd_directory'] |
|
137 | 138 | self.cards_number = data['cards_number'] |
|
138 | 139 | self.channels_number = data['channels_number'] |
|
139 | 140 | self.channels = data['channels'] |
|
140 | 141 | #self.rd_directory = data['rd_directory'] |
|
141 | 142 | #self.raw_data_blocks = data['raw_data_blocks'] |
|
142 | 143 | self.data_type = data['data_type'] |
|
143 | 144 | self.cohe_integr_str = data['cohe_integr_str'] |
|
144 | 145 | self.acq_profiles = data['acq_profiles'] |
|
145 | 146 | self.profiles_block = data['profiles_block'] |
|
146 | 147 | self.ftp_interval = data['ftp_interval'] |
|
147 | 148 | self.fftpoints = data['fftpoints'] |
|
148 | 149 | self.cohe_integr = data['cohe_integr'] |
|
149 | 150 | self.filter_parms = json.dumps(data['filter_parms']) |
|
150 | 151 | self.create_directory = data['create_directory'] |
|
151 | 152 | self.include_expname = data['include_expname'] |
|
152 | 153 | #self.acq_link = data['acq_link'] |
|
153 | 154 | #self.view_raw_data = data['view_raw_data'] |
|
154 | 155 | self.save_ch_dc = data['save_ch_dc'] |
|
155 | 156 | self.save_data = data['save_data'] |
|
156 | 157 | self.save() |
|
157 | 158 | |
|
158 | 159 | def request(self, cmd, method='get', **kwargs): |
|
159 | 160 | |
|
160 | 161 | req = getattr(requests, method)(self.device.url(cmd), **kwargs) |
|
161 | 162 | payload = req.json() |
|
162 | 163 | return payload |
|
163 | 164 | |
|
164 | 165 | def status_device(self): |
|
165 | 166 | |
|
166 | 167 | try: |
|
167 | 168 | payload = self.request('status', |
|
168 | 169 | params={'name': self.experiment.name}) |
|
169 | 170 | self.device.status = payload['status'] |
|
170 | 171 | self.device.save() |
|
171 | 172 | self.message = payload['message'] |
|
172 | 173 | except Exception as e: |
|
173 | 174 | self.device.status = 0 |
|
174 | 175 | self.message = str(e) |
|
175 | 176 | self.device.save() |
|
176 | 177 | return False |
|
177 | 178 | |
|
178 | 179 | return True |
|
179 | 180 | |
|
180 | 181 | def stop_device(self): |
|
181 | 182 | |
|
182 | 183 | try: |
|
183 | 184 | payload = self.request('stop', 'post') |
|
184 | 185 | self.device.status = payload['status'] |
|
185 | 186 | self.device.save() |
|
186 | 187 | self.message = payload['message'] |
|
187 | 188 | except Exception as e: |
|
188 | 189 | self.device.status = 0 |
|
189 | 190 | self.message = str(e) |
|
190 | 191 | self.device.save() |
|
191 | 192 | return False |
|
192 | 193 | |
|
193 | 194 | return True |
|
194 | 195 | |
|
195 | 196 | def read_device(self): |
|
196 | 197 | |
|
197 | 198 | try: |
|
198 | 199 | payload = self.request('read', params={'name': self.experiment.name}) |
|
199 | 200 | self.message = 'Configuration loaded' |
|
200 | 201 | except: |
|
201 | 202 | self.device.status = 0 |
|
202 | 203 | self.device.save() |
|
203 | 204 | self.message = 'Could not read JARS configuration.' |
|
204 | 205 | return False |
|
205 | 206 | |
|
206 | 207 | return payload |
|
207 | 208 | |
|
208 | 209 | def write_device(self): |
|
209 | 210 | |
|
210 | 211 | if self.device.status == 3: |
|
211 | 212 | self.message = 'Could not configure device. Software Acquisition is running' |
|
212 | 213 | return False |
|
213 | 214 | |
|
214 | 215 | data = self.experiment.parms_to_dict() |
|
215 | 216 | |
|
216 | 217 | for key in data['configurations']['allIds']: |
|
217 | 218 | if data['configurations']['byId'][key]['device_type'] in ('dds', 'cgs'): |
|
218 | 219 | data['configurations']['allIds'].remove(key) |
|
219 | 220 | data['configurations']['byId'].pop(key) |
|
220 | 221 | elif data['configurations']['byId'][key]['device_type'] == 'jars': |
|
221 | 222 | data['configurations']['byId'][key] = self.parms_to_dict()['configurations']['byId'][str(self.pk)] |
|
222 | 223 | elif data['configurations']['byId'][key]['device_type'] == 'rc': |
|
223 | 224 | data['configurations']['byId'][key]['pulses'] = '' |
|
224 | 225 | data['configurations']['byId'][key]['delays'] = '' |
|
225 | 226 | rc_ids = [pk for pk in data['configurations']['allIds'] if data['configurations']['byId'][pk]['device_type']=='rc'] |
|
226 | 227 | mix_ids = [pk for pk in rc_ids if data['configurations']['byId'][pk]['mix']] |
|
227 | 228 | if mix_ids: |
|
228 | 229 | params = data['configurations']['byId'][mix_ids[0]]['parameters'] |
|
229 | 230 | rc = data['configurations']['byId'][params.split('-')[0].split('|')[0]] |
|
230 | 231 | rc['mix'] = True |
|
231 | 232 | data['configurations']['byId'][rc['id']] = rc |
|
232 | 233 | elif len(rc_ids)==0: |
|
233 | 234 | self.message = 'Missing RC configuration' |
|
234 | 235 | return False |
|
235 | 236 | |
|
236 | 237 | json_data = json.dumps(data) |
|
237 | 238 | print json_data |
|
238 | 239 | try: |
|
239 | 240 | payload = self.request('write', 'post', json=json_data) |
|
240 | 241 | self.device.status = payload['status'] |
|
241 | 242 | self.message = payload['message'] |
|
242 | 243 | self.device.save() |
|
243 | 244 | if self.device.status == 1: |
|
244 | 245 | return False |
|
245 | 246 | |
|
246 | 247 | except Exception as e: |
|
247 | 248 | self.device.status = 0 |
|
248 | 249 | self.message = str(e) |
|
249 | 250 | self.device.save() |
|
250 | 251 | return False |
|
251 | 252 | |
|
252 | 253 | return True |
|
253 | 254 | |
|
254 | 255 | def start_device(self): |
|
255 | 256 | |
|
256 | 257 | try: |
|
257 | 258 | payload = self.request('start', 'post', |
|
258 | 259 | json={'name': self.experiment.name}) |
|
259 | 260 | self.device.status = payload['status'] |
|
260 | 261 | self.message = payload['message'] |
|
261 | 262 | self.device.save() |
|
262 | 263 | if self.device.status == 1: |
|
263 | 264 | return False |
|
264 | 265 | |
|
265 | 266 | except Exception as e: |
|
266 | 267 | self.device.status = 0 |
|
267 | 268 | self.message = str(e) |
|
268 | 269 | self.device.save() |
|
269 | 270 | return False |
|
270 | 271 | |
|
271 | 272 | return True |
|
272 | 273 | |
|
274 | ||
|
275 | def get_log(self): | |
|
276 | ||
|
277 | payload = None | |
|
278 | ||
|
279 | try: | |
|
280 | payload = requests.get(self.device.url('get_log'), params={'name':self.experiment.name}) | |
|
281 | except: | |
|
282 | self.device.status = 0 | |
|
283 | self.device.save() | |
|
284 | self.message = 'Jars API is not running.' | |
|
285 | return False | |
|
286 | ||
|
287 | self.message = 'Jars API is running' | |
|
288 | ||
|
289 | return payload | |
|
290 | ||
|
291 | ||
|
273 | 292 | def update_from_file(self, filename): |
|
274 | 293 | |
|
275 | 294 | f = JARSFile(filename) |
|
276 | 295 | self.dict_to_parms(f.data) |
|
277 | 296 | self.save() |
|
278 | 297 | |
|
279 | 298 | def get_absolute_url_import(self): |
|
280 | 299 | return reverse('url_import_jars_conf', args=[str(self.id)]) |
|
281 | 300 | |
|
282 | 301 | def get_absolute_url_read(self): |
|
283 | 302 | return reverse('url_read_jars_conf', args=[str(self.id)]) |
|
303 | ||
|
304 | def get_absolute_url_log(self): | |
|
305 | return reverse('url_get_jars_log', args=[str(self.id)]) |
@@ -1,16 +1,19 | |||
|
1 | 1 | {% extends "dev_conf.html" %} |
|
2 | 2 | {% load static %} |
|
3 | 3 | {% load bootstrap3 %} |
|
4 | 4 | {% load main_tags %} |
|
5 | 5 | |
|
6 | {% block extra-menu-actions %} | |
|
7 | <li><a href="{{ dev_conf.get_absolute_url_log }}"><span class="glyphicon glyphicon-save-file" aria-hidden="true"></span> Get Log File</a></li> | |
|
8 | {% endblock %} | |
|
6 | 9 | |
|
7 | 10 | {% block extra-content %} |
|
8 | 11 | |
|
9 | 12 | <div class="clearfix"></div> |
|
10 | 13 | <h2>JARS filter: {{resolution}}</h2> |
|
11 | 14 | <hr> |
|
12 | 15 | <div class="panel-group" id="div_lines" role="tablist" aria-multiselectable="true"> |
|
13 | 16 | {% include "jars_filter.html" %} |
|
14 | 17 | </div> |
|
15 | 18 | |
|
16 | 19 | {% endblock extra-content%} |
@@ -1,12 +1,13 | |||
|
1 | 1 | from django.conf.urls import url |
|
2 | 2 | |
|
3 | 3 | from apps.jars import views |
|
4 | 4 | |
|
5 | 5 | urlpatterns = ( |
|
6 | 6 | url(r'^(?P<id_conf>-?\d+)/$', views.jars_conf, name='url_jars_conf'), |
|
7 | 7 | url(r'^(?P<id_conf>-?\d+)/edit/$', views.jars_conf_edit, name='url_edit_jars_conf'), |
|
8 | 8 | url(r'^(?P<conf_id>-?\d+)/change_filter/$', views.change_filter, name='url_change_jars_filter'), |
|
9 | 9 | url(r'^(?P<conf_id>-?\d+)/change_filter/(?P<filter_id>-?\d+)/$', views.change_filter, name='url_change_jars_filter'), |
|
10 | 10 | url(r'^(?P<conf_id>-?\d+)/import/$', views.import_file, name='url_import_jars_conf'), |
|
11 | 11 | url(r'^(?P<conf_id>-?\d+)/read/$', views.read_conf, name='url_read_jars_conf'), |
|
12 | url(r'^(?P<conf_id>-?\d+)/get_log/$', views.get_log, name='url_get_jars_log'), | |
|
12 | 13 | ) |
@@ -1,209 +1,235 | |||
|
1 | 1 | from django.shortcuts import render_to_response |
|
2 | 2 | from django.template import RequestContext |
|
3 | 3 | from django.shortcuts import redirect, render, get_object_or_404 |
|
4 | 4 | from django.contrib import messages |
|
5 | from django.http import HttpResponse | |
|
5 | 6 | |
|
6 | 7 | from apps.main.models import Device |
|
7 | 8 | from apps.main.views import sidebar |
|
8 | 9 | |
|
9 | 10 | from .models import JARSConfiguration, JARSfilter |
|
10 | 11 | from .forms import JARSConfigurationForm, JARSfilterForm, JARSImportForm |
|
11 | 12 | |
|
12 | 13 | import json |
|
13 | 14 | # Create your views here. |
|
14 | 15 | |
|
15 | 16 | def jars_conf(request, id_conf): |
|
16 | 17 | |
|
17 | 18 | conf = get_object_or_404(JARSConfiguration, pk=id_conf) |
|
18 | 19 | |
|
19 | 20 | filter_parms = eval(conf.filter_parms) |
|
20 | 21 | if filter_parms.__class__.__name__=='str': |
|
21 | 22 | filter_parms = eval(filter_parms) |
|
22 | 23 | |
|
23 | 24 | kwargs = {} |
|
24 | 25 | kwargs['filter'] = filter_parms |
|
25 | 26 | kwargs['filter_keys'] = ['clock', 'mult', 'fch', 'fch_decimal', |
|
26 | 27 | 'filter_fir', 'filter_2', 'filter_5'] |
|
27 | 28 | |
|
28 | 29 | filter_resolution=conf.filter_resolution() |
|
29 | 30 | kwargs['resolution'] = '{} (MHz)'.format(filter_resolution) |
|
30 | 31 | if filter_resolution < 1: |
|
31 | 32 | kwargs['resolution'] = '{} (kHz)'.format(filter_resolution*1000) |
|
32 | 33 | |
|
33 | 34 | kwargs['status'] = conf.device.get_status_display() |
|
34 | 35 | |
|
35 | 36 | |
|
36 | 37 | kwargs['dev_conf'] = conf |
|
37 | 38 | kwargs['dev_conf_keys'] = ['name', |
|
38 | 39 | 'cards_number', 'channels_number', 'channels', |
|
39 | 40 | #'rd_directory', 'pd_directory', |
|
40 | 41 | 'data_type', |
|
41 | 42 | 'acq_profiles', 'profiles_block', 'raw_data_blocks', 'ftp_interval', 'fftpoints', |
|
42 | 43 | 'cohe_integr_str', 'decode_data', |
|
43 | 44 | 'incohe_integr', 'cohe_integr', 'spectral_number', |
|
44 | 45 | 'spectral', 'create_directory', 'include_expname', |
|
45 | 46 | 'save_ch_dc', 'save_data'] |
|
46 | 47 | |
|
47 | 48 | kwargs['title'] = 'JARS Configuration' |
|
48 | 49 | kwargs['suptitle'] = 'Details' |
|
49 | 50 | |
|
50 | 51 | kwargs['button'] = 'Edit Configuration' |
|
51 | 52 | |
|
52 | 53 | #kwargs['no_play'] = True |
|
53 | 54 | |
|
54 | 55 | #kwargs['only_stop'] = True |
|
55 | 56 | |
|
56 | 57 | ###### SIDEBAR ###### |
|
57 | 58 | kwargs.update(sidebar(conf=conf)) |
|
58 | 59 | |
|
59 | 60 | return render(request, 'jars_conf.html', kwargs) |
|
60 | 61 | |
|
61 | 62 | def jars_conf_edit(request, id_conf): |
|
62 | 63 | |
|
63 | 64 | conf = get_object_or_404(JARSConfiguration, pk=id_conf) |
|
64 | 65 | |
|
65 | 66 | filter_parms = eval(conf.filter_parms) |
|
66 | 67 | if filter_parms.__class__.__name__=='str': |
|
67 | 68 | filter_parms = eval(filter_parms) |
|
68 | 69 | |
|
69 | 70 | if request.method=='GET': |
|
70 | 71 | form = JARSConfigurationForm(instance=conf) |
|
71 | 72 | filter_form = JARSfilterForm(initial=filter_parms) |
|
72 | 73 | |
|
73 | 74 | if request.method=='POST': |
|
74 | 75 | form = JARSConfigurationForm(request.POST, instance=conf) |
|
75 | 76 | filter_form = JARSfilterForm(request.POST) |
|
76 | 77 | |
|
77 | 78 | if filter_form.is_valid(): |
|
78 | 79 | jars_filter = filter_form.cleaned_data |
|
79 | 80 | try: |
|
80 | 81 | jars_filter.pop('name') |
|
81 | 82 | except: |
|
82 | 83 | pass |
|
83 | 84 | |
|
84 | 85 | if form.is_valid(): |
|
85 | 86 | conf = form.save(commit=False) |
|
86 | 87 | conf.filter_parms = json.dumps(jars_filter) |
|
87 | 88 | conf.save() |
|
88 | 89 | return redirect('url_jars_conf', id_conf=conf.id) |
|
89 | 90 | |
|
90 | 91 | kwargs = {} |
|
91 | 92 | |
|
92 | 93 | kwargs['id_dev'] = conf.id |
|
93 | 94 | kwargs['form'] = form |
|
94 | 95 | kwargs['filter_form'] = filter_form |
|
95 | 96 | kwargs['title'] = 'Device Configuration' |
|
96 | 97 | kwargs['suptitle'] = 'Edit' |
|
97 | 98 | kwargs['button'] = 'Save' |
|
98 | 99 | |
|
99 | 100 | return render(request, 'jars_conf_edit.html', kwargs) |
|
100 | 101 | |
|
101 | 102 | def import_file(request, conf_id): |
|
102 | 103 | |
|
103 | 104 | conf = get_object_or_404(JARSConfiguration, pk=conf_id) |
|
104 | 105 | if request.method=='POST': |
|
105 | 106 | form = JARSImportForm(request.POST, request.FILES) |
|
106 | 107 | if form.is_valid(): |
|
107 | 108 | #try: |
|
108 | 109 | if True: |
|
109 | 110 | data = conf.import_from_file(request.FILES['file_name']) |
|
110 | 111 | conf.dict_to_parms(data) |
|
111 | 112 | messages.success(request, 'Configuration "%s" loaded succesfully' % request.FILES['file_name']) |
|
112 | 113 | return redirect(conf.get_absolute_url_edit()) |
|
113 | 114 | |
|
114 | 115 | #except Exception as e: |
|
115 | 116 | # messages.error(request, 'Error parsing file: "%s" - %s' % (request.FILES['file_name'], e)) |
|
116 | 117 | |
|
117 | 118 | else: |
|
118 | 119 | messages.warning(request, 'Your current configuration will be replaced') |
|
119 | 120 | form = JARSImportForm() |
|
120 | 121 | |
|
121 | 122 | kwargs = {} |
|
122 | 123 | kwargs['form'] = form |
|
123 | 124 | kwargs['title'] = 'JARS Configuration' |
|
124 | 125 | kwargs['suptitle'] = 'Import file' |
|
125 | 126 | kwargs['button'] = 'Upload' |
|
126 | 127 | kwargs['previous'] = conf.get_absolute_url() |
|
127 | 128 | |
|
128 | 129 | return render(request, 'jars_import.html', kwargs) |
|
129 | 130 | |
|
130 | 131 | def read_conf(request, conf_id): |
|
131 | 132 | |
|
132 | 133 | conf = get_object_or_404(JARSConfiguration, pk=conf_id) |
|
133 | 134 | #filter = get_object_or_404(JARSfilter, pk=filter_id) |
|
134 | 135 | |
|
135 | 136 | if request.method=='GET': |
|
136 | 137 | |
|
137 | 138 | parms = conf.read_device() |
|
138 | 139 | conf.status_device() |
|
139 | 140 | |
|
140 | 141 | if not parms: |
|
141 | 142 | messages.error(request, conf.message) |
|
142 | 143 | return redirect(conf.get_absolute_url()) |
|
143 | 144 | |
|
144 | 145 | form = JARSConfigurationForm(initial=parms, instance=conf) |
|
145 | 146 | |
|
146 | 147 | if request.method=='POST': |
|
147 | 148 | form = JARSConfigurationForm(request.POST, instance=conf) |
|
148 | 149 | |
|
149 | 150 | if form.is_valid(): |
|
150 | 151 | form.save() |
|
151 | 152 | return redirect(conf.get_absolute_url()) |
|
152 | 153 | |
|
153 | 154 | messages.error(request, "Parameters could not be saved") |
|
154 | 155 | |
|
155 | 156 | kwargs = {} |
|
156 | 157 | kwargs['id_dev'] = conf.id |
|
157 | 158 | kwargs['filter_id'] = conf.filter.id |
|
158 | 159 | kwargs['form'] = form |
|
159 | 160 | kwargs['title'] = 'Device Configuration' |
|
160 | 161 | kwargs['suptitle'] = 'Parameters read from device' |
|
161 | 162 | kwargs['button'] = 'Save' |
|
162 | 163 | |
|
163 | 164 | ###### SIDEBAR ###### |
|
164 | 165 | kwargs.update(sidebar(conf=conf)) |
|
165 | 166 | |
|
166 | 167 | return render(request, 'jars_conf_edit.html', kwargs) |
|
167 | 168 | |
|
168 | 169 | |
|
169 | 170 | |
|
170 | 171 | def change_filter(request, conf_id, filter_id=None): |
|
171 | 172 | |
|
172 | 173 | conf = get_object_or_404(JARSConfiguration, pk=conf_id) |
|
173 | 174 | |
|
174 | 175 | if filter_id: |
|
175 | 176 | if filter_id.__class__.__name__ not in ['int', 'float']: |
|
176 | 177 | filter_id = eval(filter_id) |
|
177 | 178 | |
|
178 | 179 | if filter_id == 0: |
|
179 | 180 | return redirect('url_change_jars_filter', conf_id=conf.id) |
|
180 | 181 | |
|
181 | 182 | if request.method=='GET': |
|
182 | 183 | if not filter_id: |
|
183 | 184 | form = JARSfilterForm(initial={'filter_id': 0}) |
|
184 | 185 | else: |
|
185 | 186 | form = JARSfilterForm(initial={'filter_id': filter_id}) |
|
186 | 187 | |
|
187 | 188 | if request.method=='POST': |
|
188 | 189 | form = JARSfilterForm(request.POST) |
|
189 | 190 | if form.is_valid(): |
|
190 | 191 | jars_filter = form.cleaned_data |
|
191 | 192 | try: |
|
192 | 193 | jars_filter.pop('name') |
|
193 | 194 | except: |
|
194 | 195 | pass |
|
195 | 196 | conf.filter_parms = json.dumps(jars_filter) |
|
196 | 197 | conf.save() |
|
197 | 198 | return redirect('url_edit_jars_conf', id_conf=conf.id) |
|
198 | 199 | else: |
|
199 | 200 | messages.error(request, "Select a Filter Template") |
|
200 | 201 | return redirect('url_change_jars_filter', conf_id=conf.id) |
|
201 | 202 | |
|
202 | 203 | kwargs = {} |
|
203 | 204 | kwargs['title'] = 'JARS Configuration' |
|
204 | 205 | kwargs['suptitle'] = 'Change Filter' |
|
205 | 206 | kwargs['form'] = form |
|
206 | 207 | kwargs['button'] = 'Change' |
|
207 | 208 | kwargs['conf_id'] = conf.id |
|
208 | 209 | kwargs['filter_id'] = filter_id |
|
209 | 210 | return render(request, 'change_jars_filter.html', kwargs) |
|
211 | ||
|
212 | ||
|
213 | def get_log(request, conf_id): | |
|
214 | ||
|
215 | conf = get_object_or_404(JARSConfiguration, pk=conf_id) | |
|
216 | response = conf.get_log() | |
|
217 | ||
|
218 | if not response: | |
|
219 | message = conf.message | |
|
220 | messages.error(request, message) | |
|
221 | return redirect('url_jars_conf', id_conf=conf.id) | |
|
222 | ||
|
223 | try: | |
|
224 | message = response.json()['message'] | |
|
225 | messages.error(request, message) | |
|
226 | return redirect('url_jars_conf', id_conf=conf.id) | |
|
227 | except Exception as e: | |
|
228 | message = 'Restarting Report.txt has been downloaded successfully.' | |
|
229 | ||
|
230 | content = response | |
|
231 | filename = 'Log_%s_%s.txt' %(conf.experiment.name, conf.experiment.id) | |
|
232 | response = HttpResponse(content,content_type='text/plain') | |
|
233 | response['Content-Disposition'] = 'attachment; filename="%s"' %filename | |
|
234 | ||
|
235 | return response No newline at end of file |
@@ -1,1710 +1,1707 | |||
|
1 | 1 | import ast |
|
2 | 2 | import json |
|
3 | 3 | from datetime import datetime |
|
4 | 4 | |
|
5 | 5 | from django.shortcuts import render, redirect, get_object_or_404, HttpResponse |
|
6 | 6 | from django.utils.safestring import mark_safe |
|
7 | 7 | from django.http import HttpResponseRedirect |
|
8 | 8 | from django.core.urlresolvers import reverse |
|
9 | 9 | from django.db.models import Q |
|
10 | 10 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
|
11 | 11 | from django.contrib import messages |
|
12 | 12 | from django.http.request import QueryDict |
|
13 | 13 | |
|
14 | 14 | try: |
|
15 | 15 | from urllib.parse import urlencode |
|
16 | 16 | except ImportError: |
|
17 | 17 | from urllib import urlencode |
|
18 | 18 | |
|
19 | 19 | from .forms import CampaignForm, ExperimentForm, DeviceForm, ConfigurationForm, LocationForm, UploadFileForm, DownloadFileForm, OperationForm, NewForm |
|
20 | 20 | from .forms import OperationSearchForm, FilterForm, ChangeIpForm |
|
21 | 21 | |
|
22 | 22 | from .tasks import task_start, task_stop, task_status |
|
23 | 23 | |
|
24 | 24 | from apps.rc.forms import RCConfigurationForm, RCLineCode, RCMixConfigurationForm |
|
25 | 25 | from apps.dds.forms import DDSConfigurationForm |
|
26 | 26 | from apps.jars.forms import JARSConfigurationForm |
|
27 | 27 | from apps.cgs.forms import CGSConfigurationForm |
|
28 | 28 | from apps.abs.forms import ABSConfigurationForm |
|
29 | 29 | from apps.usrp.forms import USRPConfigurationForm |
|
30 | 30 | |
|
31 | 31 | from .models import Campaign, Experiment, Device, Configuration, Location, RunningExperiment, DEV_STATES |
|
32 | 32 | from apps.cgs.models import CGSConfiguration |
|
33 | 33 | from apps.jars.models import JARSConfiguration, EXPERIMENT_TYPE |
|
34 | 34 | from apps.usrp.models import USRPConfiguration |
|
35 | 35 | from apps.abs.models import ABSConfiguration |
|
36 | 36 | from apps.rc.models import RCConfiguration, RCLine, RCLineType |
|
37 | 37 | from apps.dds.models import DDSConfiguration |
|
38 | 38 | |
|
39 | 39 | from django.contrib.auth.decorators import login_required |
|
40 | 40 | from django.contrib.auth.decorators import user_passes_test |
|
41 | 41 | from django.contrib.admin.views.decorators import staff_member_required |
|
42 | 42 | |
|
43 | 43 | CONF_FORMS = { |
|
44 | 44 | 'rc': RCConfigurationForm, |
|
45 | 45 | 'dds': DDSConfigurationForm, |
|
46 | 46 | 'jars': JARSConfigurationForm, |
|
47 | 47 | 'cgs': CGSConfigurationForm, |
|
48 | 48 | 'abs': ABSConfigurationForm, |
|
49 | 49 | 'usrp': USRPConfigurationForm, |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | CONF_MODELS = { |
|
53 | 53 | 'rc': RCConfiguration, |
|
54 | 54 | 'dds': DDSConfiguration, |
|
55 | 55 | 'jars': JARSConfiguration, |
|
56 | 56 | 'cgs': CGSConfiguration, |
|
57 | 57 | 'abs': ABSConfiguration, |
|
58 | 58 | 'usrp': USRPConfiguration, |
|
59 | 59 | } |
|
60 | 60 | |
|
61 | 61 | MIX_MODES = { |
|
62 | 62 | '0': 'P', |
|
63 | 63 | '1': 'S', |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | MIX_OPERATIONS = { |
|
67 | 67 | '0': 'OR', |
|
68 | 68 | '1': 'XOR', |
|
69 | 69 | '2': 'AND', |
|
70 | 70 | '3': 'NAND', |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | def index(request): |
|
74 | 74 | kwargs = {'no_sidebar':True} |
|
75 | 75 | |
|
76 | 76 | return render(request, 'index.html', kwargs) |
|
77 | 77 | |
|
78 | 78 | |
|
79 | 79 | def locations(request): |
|
80 | 80 | |
|
81 | 81 | page = request.GET.get('page') |
|
82 | 82 | order = ('name',) |
|
83 | 83 | |
|
84 | 84 | kwargs = get_paginator(Location, page, order) |
|
85 | 85 | |
|
86 | 86 | kwargs['keys'] = ['name', 'description'] |
|
87 | 87 | kwargs['title'] = 'Radar System' |
|
88 | 88 | kwargs['suptitle'] = 'List' |
|
89 | 89 | kwargs['no_sidebar'] = True |
|
90 | 90 | |
|
91 | 91 | return render(request, 'base_list.html', kwargs) |
|
92 | 92 | |
|
93 | 93 | |
|
94 | 94 | def location(request, id_loc): |
|
95 | 95 | |
|
96 | 96 | location = get_object_or_404(Location, pk=id_loc) |
|
97 | 97 | |
|
98 | 98 | kwargs = {} |
|
99 | 99 | kwargs['location'] = location |
|
100 | 100 | kwargs['location_keys'] = ['name', 'description'] |
|
101 | 101 | |
|
102 | 102 | kwargs['title'] = 'Location' |
|
103 | 103 | kwargs['suptitle'] = 'Details' |
|
104 | 104 | |
|
105 | 105 | return render(request, 'location.html', kwargs) |
|
106 | 106 | |
|
107 | 107 | |
|
108 | 108 | @user_passes_test(lambda u:u.is_staff) |
|
109 | 109 | def location_new(request): |
|
110 | 110 | |
|
111 | 111 | if request.method == 'GET': |
|
112 | 112 | form = LocationForm() |
|
113 | 113 | |
|
114 | 114 | if request.method == 'POST': |
|
115 | 115 | form = LocationForm(request.POST) |
|
116 | 116 | |
|
117 | 117 | if form.is_valid(): |
|
118 | 118 | form.save() |
|
119 | 119 | return redirect('url_locations') |
|
120 | 120 | |
|
121 | 121 | kwargs = {} |
|
122 | 122 | kwargs['form'] = form |
|
123 | 123 | kwargs['title'] = 'Radar System' |
|
124 | 124 | kwargs['suptitle'] = 'New' |
|
125 | 125 | kwargs['button'] = 'Create' |
|
126 | 126 | |
|
127 | 127 | return render(request, 'base_edit.html', kwargs) |
|
128 | 128 | |
|
129 | 129 | |
|
130 | 130 | @user_passes_test(lambda u:u.is_staff) |
|
131 | 131 | def location_edit(request, id_loc): |
|
132 | 132 | |
|
133 | 133 | location = get_object_or_404(Location, pk=id_loc) |
|
134 | 134 | |
|
135 | 135 | if request.method=='GET': |
|
136 | 136 | form = LocationForm(instance=location) |
|
137 | 137 | |
|
138 | 138 | if request.method=='POST': |
|
139 | 139 | form = LocationForm(request.POST, instance=location) |
|
140 | 140 | |
|
141 | 141 | if form.is_valid(): |
|
142 | 142 | form.save() |
|
143 | 143 | return redirect('url_locations') |
|
144 | 144 | |
|
145 | 145 | kwargs = {} |
|
146 | 146 | kwargs['form'] = form |
|
147 | 147 | kwargs['title'] = 'Location' |
|
148 | 148 | kwargs['suptitle'] = 'Edit' |
|
149 | 149 | kwargs['button'] = 'Update' |
|
150 | 150 | |
|
151 | 151 | return render(request, 'base_edit.html', kwargs) |
|
152 | 152 | |
|
153 | 153 | |
|
154 | 154 | @user_passes_test(lambda u:u.is_staff) |
|
155 | 155 | def location_delete(request, id_loc): |
|
156 | 156 | |
|
157 | 157 | location = get_object_or_404(Location, pk=id_loc) |
|
158 | 158 | |
|
159 | 159 | if request.method=='POST': |
|
160 | 160 | |
|
161 | 161 | if request.user.is_staff: |
|
162 | 162 | location.delete() |
|
163 | 163 | return redirect('url_locations') |
|
164 | 164 | |
|
165 | 165 | messages.error(request, 'Not enough permission to delete this object') |
|
166 | 166 | return redirect(location.get_absolute_url()) |
|
167 | 167 | |
|
168 | 168 | kwargs = { |
|
169 | 169 | 'title': 'Delete', |
|
170 | 170 | 'suptitle': 'Location', |
|
171 | 171 | 'object': location, |
|
172 | 172 | 'previous': location.get_absolute_url(), |
|
173 | 173 | 'delete': True |
|
174 | 174 | } |
|
175 | 175 | |
|
176 | 176 | return render(request, 'confirm.html', kwargs) |
|
177 | 177 | |
|
178 | 178 | |
|
179 | 179 | def devices(request): |
|
180 | 180 | |
|
181 | 181 | page = request.GET.get('page') |
|
182 | 182 | order = ('device_type', 'name') |
|
183 | 183 | |
|
184 | 184 | kwargs = get_paginator(Device, page, order) |
|
185 | 185 | kwargs['keys'] = ['name', 'ip_address', 'port_address', 'device_type'] |
|
186 | 186 | kwargs['title'] = 'Device' |
|
187 | 187 | kwargs['suptitle'] = 'List' |
|
188 | 188 | kwargs['no_sidebar'] = True |
|
189 | 189 | |
|
190 | 190 | return render(request, 'base_list.html', kwargs) |
|
191 | 191 | |
|
192 | 192 | |
|
193 | 193 | def device(request, id_dev): |
|
194 | 194 | |
|
195 | 195 | device = get_object_or_404(Device, pk=id_dev) |
|
196 | 196 | |
|
197 | 197 | kwargs = {} |
|
198 | 198 | kwargs['device'] = device |
|
199 | 199 | kwargs['device_keys'] = ['device_type', 'name', 'ip_address', 'port_address', 'description'] |
|
200 | 200 | |
|
201 | 201 | kwargs['title'] = 'Device' |
|
202 | 202 | kwargs['suptitle'] = 'Details' |
|
203 | 203 | |
|
204 | 204 | return render(request, 'device.html', kwargs) |
|
205 | 205 | |
|
206 | 206 | |
|
207 | 207 | @user_passes_test(lambda u:u.is_staff) |
|
208 | 208 | def device_new(request): |
|
209 | 209 | |
|
210 | 210 | if request.method == 'GET': |
|
211 | 211 | form = DeviceForm() |
|
212 | 212 | |
|
213 | 213 | if request.method == 'POST': |
|
214 | 214 | form = DeviceForm(request.POST) |
|
215 | 215 | |
|
216 | 216 | if form.is_valid(): |
|
217 | 217 | form.save() |
|
218 | 218 | return redirect('url_devices') |
|
219 | 219 | |
|
220 | 220 | kwargs = {} |
|
221 | 221 | kwargs['form'] = form |
|
222 | 222 | kwargs['title'] = 'Device' |
|
223 | 223 | kwargs['suptitle'] = 'New' |
|
224 | 224 | kwargs['button'] = 'Create' |
|
225 | 225 | |
|
226 | 226 | return render(request, 'base_edit.html', kwargs) |
|
227 | 227 | |
|
228 | 228 | |
|
229 | 229 | @user_passes_test(lambda u:u.is_staff) |
|
230 | 230 | def device_edit(request, id_dev): |
|
231 | 231 | |
|
232 | 232 | device = get_object_or_404(Device, pk=id_dev) |
|
233 | 233 | |
|
234 | 234 | if request.method=='GET': |
|
235 | 235 | form = DeviceForm(instance=device) |
|
236 | 236 | |
|
237 | 237 | if request.method=='POST': |
|
238 | 238 | form = DeviceForm(request.POST, instance=device) |
|
239 | 239 | |
|
240 | 240 | if form.is_valid(): |
|
241 | 241 | form.save() |
|
242 | 242 | return redirect(device.get_absolute_url()) |
|
243 | 243 | |
|
244 | 244 | kwargs = {} |
|
245 | 245 | kwargs['form'] = form |
|
246 | 246 | kwargs['title'] = 'Device' |
|
247 | 247 | kwargs['suptitle'] = 'Edit' |
|
248 | 248 | kwargs['button'] = 'Update' |
|
249 | 249 | |
|
250 | 250 | return render(request, 'base_edit.html', kwargs) |
|
251 | 251 | |
|
252 | 252 | |
|
253 | 253 | @user_passes_test(lambda u:u.is_staff) |
|
254 | 254 | def device_delete(request, id_dev): |
|
255 | 255 | |
|
256 | 256 | device = get_object_or_404(Device, pk=id_dev) |
|
257 | 257 | |
|
258 | 258 | if request.method=='POST': |
|
259 | 259 | |
|
260 | 260 | if request.user.is_staff: |
|
261 | 261 | device.delete() |
|
262 | 262 | return redirect('url_devices') |
|
263 | 263 | |
|
264 | 264 | messages.error(request, 'Not enough permission to delete this object') |
|
265 | 265 | return redirect(device.get_absolute_url()) |
|
266 | 266 | |
|
267 | 267 | kwargs = { |
|
268 | 268 | 'title': 'Delete', |
|
269 | 269 | 'suptitle': 'Device', |
|
270 | 270 | 'object': device, |
|
271 | 271 | 'previous': device.get_absolute_url(), |
|
272 | 272 | 'delete': True |
|
273 | 273 | } |
|
274 | 274 | |
|
275 | 275 | return render(request, 'confirm.html', kwargs) |
|
276 | 276 | |
|
277 | 277 | |
|
278 | 278 | @user_passes_test(lambda u:u.is_staff) |
|
279 | 279 | def device_change_ip(request, id_dev): |
|
280 | 280 | |
|
281 | 281 | device = get_object_or_404(Device, pk=id_dev) |
|
282 | 282 | |
|
283 | 283 | if request.method=='POST': |
|
284 | 284 | |
|
285 | 285 | if request.user.is_staff: |
|
286 | 286 | device.change_ip(**request.POST.dict()) |
|
287 | 287 | level, message = device.message.split('|') |
|
288 | 288 | messages.add_message(request, level, message) |
|
289 | 289 | else: |
|
290 | 290 | messages.error(request, 'Not enough permission to delete this object') |
|
291 | 291 | return redirect(device.get_absolute_url()) |
|
292 | 292 | |
|
293 | 293 | kwargs = { |
|
294 | 294 | 'title': 'Device', |
|
295 | 295 | 'suptitle': 'Change IP', |
|
296 | 296 | 'object': device, |
|
297 | 297 | 'previous': device.get_absolute_url(), |
|
298 | 298 | 'form': ChangeIpForm(initial={'ip_address':device.ip_address}), |
|
299 | 299 | 'message' : ' ', |
|
300 | 300 | } |
|
301 | 301 | |
|
302 | 302 | return render(request, 'confirm.html', kwargs) |
|
303 | 303 | |
|
304 | 304 | |
|
305 | 305 | def campaigns(request): |
|
306 | 306 | |
|
307 | 307 | page = request.GET.get('page') |
|
308 | 308 | order = ('start_date',) |
|
309 | 309 | filters = request.GET.copy() |
|
310 | 310 | |
|
311 | 311 | kwargs = get_paginator(Campaign, page, order, filters) |
|
312 | 312 | |
|
313 | 313 | form = FilterForm(initial=request.GET, extra_fields=['range_date', 'tags','template']) |
|
314 | 314 | kwargs['keys'] = ['name', 'start_date', 'end_date'] |
|
315 | 315 | kwargs['title'] = 'Campaign' |
|
316 | 316 | kwargs['suptitle'] = 'List' |
|
317 | 317 | kwargs['no_sidebar'] = True |
|
318 | 318 | kwargs['form'] = form |
|
319 | 319 | filters.pop('page', None) |
|
320 | 320 | kwargs['q'] = urlencode(filters) |
|
321 | 321 | |
|
322 | 322 | return render(request, 'base_list.html', kwargs) |
|
323 | 323 | |
|
324 | 324 | |
|
325 | 325 | def campaign(request, id_camp): |
|
326 | 326 | |
|
327 | 327 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
328 | 328 | experiments = Experiment.objects.filter(campaign=campaign) |
|
329 | 329 | |
|
330 | 330 | form = CampaignForm(instance=campaign) |
|
331 | 331 | |
|
332 | 332 | kwargs = {} |
|
333 | 333 | kwargs['campaign'] = campaign |
|
334 | 334 | kwargs['campaign_keys'] = ['template', 'name', 'start_date', 'end_date', 'tags', 'description'] |
|
335 | 335 | |
|
336 | 336 | kwargs['experiments'] = experiments |
|
337 | 337 | kwargs['experiment_keys'] = ['name', 'radar_system', 'start_time', 'end_time'] |
|
338 | 338 | |
|
339 | 339 | kwargs['title'] = 'Campaign' |
|
340 | 340 | kwargs['suptitle'] = 'Details' |
|
341 | 341 | |
|
342 | 342 | kwargs['form'] = form |
|
343 | 343 | kwargs['button'] = 'Add Experiment' |
|
344 | 344 | |
|
345 | 345 | return render(request, 'campaign.html', kwargs) |
|
346 | 346 | |
|
347 | 347 | |
|
348 | 348 | @user_passes_test(lambda u:u.is_staff) |
|
349 | 349 | def campaign_new(request): |
|
350 | 350 | |
|
351 | 351 | kwargs = {} |
|
352 | 352 | |
|
353 | 353 | if request.method == 'GET': |
|
354 | 354 | |
|
355 | 355 | if 'template' in request.GET: |
|
356 | 356 | if request.GET['template']=='0': |
|
357 | 357 | form = NewForm(initial={'create_from':2}, |
|
358 | 358 | template_choices=Campaign.objects.filter(template=True).values_list('id', 'name')) |
|
359 | 359 | else: |
|
360 | 360 | kwargs['button'] = 'Create' |
|
361 | 361 | kwargs['experiments'] = Configuration.objects.filter(experiment=request.GET['template']) |
|
362 | 362 | kwargs['experiment_keys'] = ['name', 'start_time', 'end_time'] |
|
363 | 363 | camp = Campaign.objects.get(pk=request.GET['template']) |
|
364 | 364 | form = CampaignForm(instance=camp, |
|
365 | 365 | initial={'name':'{} [{:%Y/%m/%d}]'.format(camp.name, datetime.now()), |
|
366 | 366 | 'template':False}) |
|
367 | 367 | elif 'blank' in request.GET: |
|
368 | 368 | kwargs['button'] = 'Create' |
|
369 | 369 | form = CampaignForm() |
|
370 | 370 | else: |
|
371 | 371 | form = NewForm() |
|
372 | 372 | |
|
373 | 373 | if request.method == 'POST': |
|
374 | 374 | kwargs['button'] = 'Create' |
|
375 | 375 | post = request.POST.copy() |
|
376 | 376 | experiments = [] |
|
377 | 377 | |
|
378 | 378 | for id_exp in post.getlist('experiments'): |
|
379 | 379 | exp = Experiment.objects.get(pk=id_exp) |
|
380 | 380 | new_exp = exp.clone(template=False) |
|
381 | 381 | experiments.append(new_exp) |
|
382 | 382 | |
|
383 | 383 | post.setlist('experiments', []) |
|
384 | 384 | |
|
385 | 385 | form = CampaignForm(post) |
|
386 | 386 | |
|
387 | 387 | if form.is_valid(): |
|
388 | 388 | campaign = form.save() |
|
389 | 389 | for exp in experiments: |
|
390 | 390 | campaign.experiments.add(exp) |
|
391 | 391 | campaign.save() |
|
392 | 392 | return redirect('url_campaign', id_camp=campaign.id) |
|
393 | 393 | |
|
394 | 394 | kwargs['form'] = form |
|
395 | 395 | kwargs['title'] = 'Campaign' |
|
396 | 396 | kwargs['suptitle'] = 'New' |
|
397 | 397 | |
|
398 | 398 | return render(request, 'campaign_edit.html', kwargs) |
|
399 | 399 | |
|
400 | 400 | |
|
401 | 401 | @user_passes_test(lambda u:u.is_staff) |
|
402 | 402 | def campaign_edit(request, id_camp): |
|
403 | 403 | |
|
404 | 404 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
405 | 405 | |
|
406 | 406 | if request.method=='GET': |
|
407 | 407 | form = CampaignForm(instance=campaign) |
|
408 | 408 | |
|
409 | 409 | if request.method=='POST': |
|
410 | 410 | exps = campaign.experiments.all().values_list('pk', flat=True) |
|
411 | 411 | post = request.POST.copy() |
|
412 | 412 | new_exps = post.getlist('experiments') |
|
413 | 413 | post.setlist('experiments', []) |
|
414 | 414 | form = CampaignForm(post, instance=campaign) |
|
415 | 415 | |
|
416 | 416 | if form.is_valid(): |
|
417 | 417 | camp = form.save() |
|
418 | 418 | for id_exp in new_exps: |
|
419 | 419 | if int(id_exp) in exps: |
|
420 | 420 | exps.pop(id_exp) |
|
421 | 421 | else: |
|
422 | 422 | exp = Experiment.objects.get(pk=id_exp) |
|
423 | 423 | if exp.template: |
|
424 | 424 | camp.experiments.add(exp.clone(template=False)) |
|
425 | 425 | else: |
|
426 | 426 | camp.experiments.add(exp) |
|
427 | 427 | |
|
428 | 428 | for id_exp in exps: |
|
429 | 429 | camp.experiments.remove(Experiment.objects.get(pk=id_exp)) |
|
430 | 430 | |
|
431 | 431 | return redirect('url_campaign', id_camp=id_camp) |
|
432 | 432 | |
|
433 | 433 | kwargs = {} |
|
434 | 434 | kwargs['form'] = form |
|
435 | 435 | kwargs['title'] = 'Campaign' |
|
436 | 436 | kwargs['suptitle'] = 'Edit' |
|
437 | 437 | kwargs['button'] = 'Update' |
|
438 | 438 | |
|
439 | 439 | return render(request, 'campaign_edit.html', kwargs) |
|
440 | 440 | |
|
441 | 441 | |
|
442 | 442 | @user_passes_test(lambda u:u.is_staff) |
|
443 | 443 | def campaign_delete(request, id_camp): |
|
444 | 444 | |
|
445 | 445 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
446 | 446 | |
|
447 | 447 | if request.method=='POST': |
|
448 | 448 | if request.user.is_staff: |
|
449 | 449 | |
|
450 | 450 | for exp in campaign.experiments.all(): |
|
451 | 451 | for conf in Configuration.objects.filter(experiment=exp): |
|
452 | 452 | conf.delete() |
|
453 | 453 | exp.delete() |
|
454 | 454 | campaign.delete() |
|
455 | 455 | |
|
456 | 456 | return redirect('url_campaigns') |
|
457 | 457 | |
|
458 | 458 | messages.error(request, 'Not enough permission to delete this object') |
|
459 | 459 | return redirect(campaign.get_absolute_url()) |
|
460 | 460 | |
|
461 | 461 | kwargs = { |
|
462 | 462 | 'title': 'Delete', |
|
463 | 463 | 'suptitle': 'Campaign', |
|
464 | 464 | 'object': campaign, |
|
465 | 465 | 'previous': campaign.get_absolute_url(), |
|
466 | 466 | 'delete': True |
|
467 | 467 | } |
|
468 | 468 | |
|
469 | 469 | return render(request, 'confirm.html', kwargs) |
|
470 | 470 | |
|
471 | 471 | |
|
472 | 472 | @user_passes_test(lambda u:u.is_staff) |
|
473 | 473 | def campaign_export(request, id_camp): |
|
474 | 474 | |
|
475 | 475 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
476 | 476 | content = campaign.parms_to_dict() |
|
477 | 477 | content_type = 'application/json' |
|
478 | 478 | filename = '%s_%s.json' %(campaign.name, campaign.id) |
|
479 | 479 | |
|
480 | 480 | response = HttpResponse(content_type=content_type) |
|
481 | 481 | response['Content-Disposition'] = 'attachment; filename="%s"' %filename |
|
482 | 482 | response.write(json.dumps(content, indent=2)) |
|
483 | 483 | |
|
484 | 484 | return response |
|
485 | 485 | |
|
486 | 486 | |
|
487 | 487 | @user_passes_test(lambda u:u.is_staff) |
|
488 | 488 | def campaign_import(request, id_camp): |
|
489 | 489 | |
|
490 | 490 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
491 | 491 | |
|
492 | 492 | if request.method == 'GET': |
|
493 | 493 | file_form = UploadFileForm() |
|
494 | 494 | |
|
495 | 495 | if request.method == 'POST': |
|
496 | 496 | file_form = UploadFileForm(request.POST, request.FILES) |
|
497 | 497 | |
|
498 | 498 | if file_form.is_valid(): |
|
499 | 499 | new_camp = campaign.dict_to_parms(json.load(request.FILES['file']), CONF_MODELS) |
|
500 | 500 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
501 | 501 | return redirect(new_camp.get_absolute_url_edit()) |
|
502 | 502 | |
|
503 | 503 | messages.error(request, "Could not import parameters from file") |
|
504 | 504 | |
|
505 | 505 | kwargs = {} |
|
506 | 506 | kwargs['title'] = 'Campaign' |
|
507 | 507 | kwargs['form'] = file_form |
|
508 | 508 | kwargs['suptitle'] = 'Importing file' |
|
509 | 509 | kwargs['button'] = 'Import' |
|
510 | 510 | |
|
511 | 511 | return render(request, 'campaign_import.html', kwargs) |
|
512 | 512 | |
|
513 | 513 | |
|
514 | 514 | def experiments(request): |
|
515 | 515 | |
|
516 | 516 | page = request.GET.get('page') |
|
517 | 517 | order = ('location',) |
|
518 | 518 | filters = request.GET.copy() |
|
519 | 519 | |
|
520 | 520 | kwargs = get_paginator(Experiment, page, order, filters) |
|
521 | 521 | |
|
522 | 522 | form = FilterForm(initial=request.GET, extra_fields=['tags','template']) |
|
523 | 523 | |
|
524 | 524 | kwargs['keys'] = ['name', 'radar_system', 'start_time', 'end_time'] |
|
525 | 525 | kwargs['title'] = 'Experiment' |
|
526 | 526 | kwargs['suptitle'] = 'List' |
|
527 | 527 | kwargs['no_sidebar'] = True |
|
528 | 528 | kwargs['form'] = form |
|
529 | 529 | filters.pop('page', None) |
|
530 | 530 | kwargs['q'] = urlencode(filters) |
|
531 | 531 | |
|
532 | 532 | return render(request, 'base_list.html', kwargs) |
|
533 | 533 | |
|
534 | 534 | |
|
535 | 535 | def experiment(request, id_exp): |
|
536 | 536 | |
|
537 | 537 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
538 | 538 | |
|
539 | 539 | configurations = Configuration.objects.filter(experiment=experiment, type=0) |
|
540 | 540 | |
|
541 | 541 | kwargs = {} |
|
542 | 542 | |
|
543 | 543 | kwargs['experiment_keys'] = ['template', 'radar_system', 'name', 'freq', 'start_time', 'end_time'] |
|
544 | 544 | kwargs['experiment'] = experiment |
|
545 | 545 | |
|
546 | 546 | kwargs['configuration_keys'] = ['name', 'device__ip_address', 'device__port_address', 'device__status'] |
|
547 | 547 | kwargs['configurations'] = configurations |
|
548 | 548 | |
|
549 | 549 | kwargs['title'] = 'Experiment' |
|
550 | 550 | kwargs['suptitle'] = 'Details' |
|
551 | 551 | |
|
552 | 552 | kwargs['button'] = 'Add Configuration' |
|
553 | 553 | |
|
554 | 554 | ###### SIDEBAR ###### |
|
555 | 555 | kwargs.update(sidebar(experiment=experiment)) |
|
556 | 556 | |
|
557 | 557 | return render(request, 'experiment.html', kwargs) |
|
558 | 558 | |
|
559 | 559 | |
|
560 | 560 | @user_passes_test(lambda u:u.is_staff) |
|
561 | 561 | def experiment_new(request, id_camp=None): |
|
562 | 562 | |
|
563 | 563 | kwargs = {} |
|
564 | 564 | |
|
565 | 565 | if request.method == 'GET': |
|
566 | 566 | if 'template' in request.GET: |
|
567 | 567 | if request.GET['template']=='0': |
|
568 | 568 | form = NewForm(initial={'create_from':2}, |
|
569 | 569 | template_choices=Experiment.objects.filter(template=True).values_list('id', 'name')) |
|
570 | 570 | else: |
|
571 | 571 | kwargs['button'] = 'Create' |
|
572 | 572 | kwargs['configurations'] = Configuration.objects.filter(experiment=request.GET['template']) |
|
573 | 573 | kwargs['configuration_keys'] = ['name', 'device__name', 'device__ip_address', 'device__port_address'] |
|
574 | 574 | exp=Experiment.objects.get(pk=request.GET['template']) |
|
575 | 575 | form = ExperimentForm(instance=exp, |
|
576 | 576 | initial={'name': '{}_{:%y%m%d}'.format(exp.name, datetime.now()), |
|
577 | 577 | 'template': False}) |
|
578 | 578 | elif 'blank' in request.GET: |
|
579 | 579 | kwargs['button'] = 'Create' |
|
580 | 580 | form = ExperimentForm() |
|
581 | 581 | else: |
|
582 | 582 | form = NewForm() |
|
583 | 583 | |
|
584 | 584 | if request.method == 'POST': |
|
585 | 585 | form = ExperimentForm(request.POST) |
|
586 | 586 | if form.is_valid(): |
|
587 | 587 | experiment = form.save() |
|
588 | 588 | |
|
589 | 589 | if 'template' in request.GET: |
|
590 | 590 | configurations = Configuration.objects.filter(experiment=request.GET['template'], type=0) |
|
591 | 591 | for conf in configurations: |
|
592 | 592 | conf.clone(experiment=experiment, template=False) |
|
593 | 593 | |
|
594 | 594 | return redirect('url_experiment', id_exp=experiment.id) |
|
595 | 595 | |
|
596 | 596 | kwargs['form'] = form |
|
597 | 597 | kwargs['title'] = 'Experiment' |
|
598 | 598 | kwargs['suptitle'] = 'New' |
|
599 | 599 | |
|
600 | 600 | return render(request, 'experiment_edit.html', kwargs) |
|
601 | 601 | |
|
602 | 602 | |
|
603 | 603 | @user_passes_test(lambda u:u.is_staff) |
|
604 | 604 | def experiment_edit(request, id_exp): |
|
605 | 605 | |
|
606 | 606 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
607 | 607 | |
|
608 | 608 | if request.method == 'GET': |
|
609 | 609 | form = ExperimentForm(instance=experiment) |
|
610 | 610 | |
|
611 | 611 | if request.method=='POST': |
|
612 | 612 | form = ExperimentForm(request.POST, instance=experiment) |
|
613 | 613 | |
|
614 | 614 | if form.is_valid(): |
|
615 | 615 | experiment = form.save() |
|
616 | 616 | return redirect('url_experiment', id_exp=experiment.id) |
|
617 | 617 | |
|
618 | 618 | kwargs = {} |
|
619 | 619 | kwargs['form'] = form |
|
620 | 620 | kwargs['title'] = 'Experiment' |
|
621 | 621 | kwargs['suptitle'] = 'Edit' |
|
622 | 622 | kwargs['button'] = 'Update' |
|
623 | 623 | |
|
624 | 624 | return render(request, 'experiment_edit.html', kwargs) |
|
625 | 625 | |
|
626 | 626 | |
|
627 | 627 | @user_passes_test(lambda u:u.is_staff) |
|
628 | 628 | def experiment_delete(request, id_exp): |
|
629 | 629 | |
|
630 | 630 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
631 | 631 | |
|
632 | 632 | if request.method=='POST': |
|
633 | 633 | if request.user.is_staff: |
|
634 | 634 | for conf in Configuration.objects.filter(experiment=experiment): |
|
635 | 635 | conf.delete() |
|
636 | 636 | experiment.delete() |
|
637 | 637 | return redirect('url_experiments') |
|
638 | 638 | |
|
639 | 639 | messages.error(request, 'Not enough permission to delete this object') |
|
640 | 640 | return redirect(experiment.get_absolute_url()) |
|
641 | 641 | |
|
642 | 642 | kwargs = { |
|
643 | 643 | 'title': 'Delete', |
|
644 | 644 | 'suptitle': 'Experiment', |
|
645 | 645 | 'object': experiment, |
|
646 | 646 | 'previous': experiment.get_absolute_url(), |
|
647 | 647 | 'delete': True |
|
648 | 648 | } |
|
649 | 649 | |
|
650 | 650 | return render(request, 'confirm.html', kwargs) |
|
651 | 651 | |
|
652 | 652 | |
|
653 | 653 | @user_passes_test(lambda u:u.is_staff) |
|
654 | 654 | def experiment_export(request, id_exp): |
|
655 | 655 | |
|
656 | 656 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
657 | 657 | content = experiment.parms_to_dict() |
|
658 | 658 | content_type = 'application/json' |
|
659 | 659 | filename = '%s_%s.json' %(experiment.name, experiment.id) |
|
660 | 660 | |
|
661 | 661 | response = HttpResponse(content_type=content_type) |
|
662 | 662 | response['Content-Disposition'] = 'attachment; filename="%s"' %filename |
|
663 | 663 | response.write(json.dumps(content, indent=2)) |
|
664 | 664 | |
|
665 | 665 | return response |
|
666 | 666 | |
|
667 | 667 | |
|
668 | 668 | @user_passes_test(lambda u:u.is_staff) |
|
669 | 669 | def experiment_import(request, id_exp): |
|
670 | 670 | |
|
671 | 671 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
672 | 672 | configurations = Configuration.objects.filter(experiment=experiment) |
|
673 | 673 | |
|
674 | 674 | if request.method == 'GET': |
|
675 | 675 | file_form = UploadFileForm() |
|
676 | 676 | |
|
677 | 677 | if request.method == 'POST': |
|
678 | 678 | file_form = UploadFileForm(request.POST, request.FILES) |
|
679 | 679 | |
|
680 | 680 | if file_form.is_valid(): |
|
681 | 681 | new_exp = experiment.dict_to_parms(json.load(request.FILES['file']), CONF_MODELS) |
|
682 | 682 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
683 | 683 | return redirect(new_exp.get_absolute_url_edit()) |
|
684 | 684 | |
|
685 | 685 | messages.error(request, "Could not import parameters from file") |
|
686 | 686 | |
|
687 | 687 | kwargs = {} |
|
688 | 688 | kwargs['title'] = 'Experiment' |
|
689 | 689 | kwargs['form'] = file_form |
|
690 | 690 | kwargs['suptitle'] = 'Importing file' |
|
691 | 691 | kwargs['button'] = 'Import' |
|
692 | 692 | |
|
693 | 693 | kwargs.update(sidebar(experiment=experiment)) |
|
694 | 694 | |
|
695 | 695 | return render(request, 'experiment_import.html', kwargs) |
|
696 | 696 | |
|
697 | 697 | |
|
698 | 698 | @user_passes_test(lambda u:u.is_staff) |
|
699 | 699 | def experiment_start(request, id_exp): |
|
700 | 700 | |
|
701 | 701 | exp = get_object_or_404(Experiment, pk=id_exp) |
|
702 | 702 | |
|
703 | 703 | if exp.status == 2: |
|
704 | 704 | messages.warning(request, 'Experiment {} already runnnig'.format(exp)) |
|
705 | 705 | else: |
|
706 | 706 | exp.status = exp.start() |
|
707 | 707 | if exp.status==0: |
|
708 | 708 | messages.error(request, 'Experiment {} not start'.format(exp)) |
|
709 | 709 | if exp.status==2: |
|
710 | 710 | messages.success(request, 'Experiment {} started'.format(exp)) |
|
711 | 711 | |
|
712 | 712 | exp.save() |
|
713 | 713 | |
|
714 | 714 | return redirect(exp.get_absolute_url()) |
|
715 | 715 | |
|
716 | 716 | |
|
717 | 717 | @user_passes_test(lambda u:u.is_staff) |
|
718 | 718 | def experiment_stop(request, id_exp): |
|
719 | 719 | |
|
720 | 720 | exp = get_object_or_404(Experiment, pk=id_exp) |
|
721 | 721 | |
|
722 | 722 | if exp.status == 2: |
|
723 | 723 | exp.status = exp.stop() |
|
724 | 724 | exp.save() |
|
725 | 725 | messages.success(request, 'Experiment {} stopped'.format(exp)) |
|
726 | 726 | else: |
|
727 | 727 | messages.error(request, 'Experiment {} not running'.format(exp)) |
|
728 | 728 | |
|
729 | 729 | return redirect(exp.get_absolute_url()) |
|
730 | 730 | |
|
731 | 731 | |
|
732 | 732 | def experiment_status(request, id_exp): |
|
733 | 733 | |
|
734 | 734 | exp = get_object_or_404(Experiment, pk=id_exp) |
|
735 | 735 | |
|
736 | 736 | exp.get_status() |
|
737 | 737 | |
|
738 | 738 | return redirect(exp.get_absolute_url()) |
|
739 | 739 | |
|
740 | 740 | |
|
741 | 741 | @user_passes_test(lambda u:u.is_staff) |
|
742 | 742 | def experiment_mix(request, id_exp): |
|
743 | 743 | |
|
744 | 744 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
745 | 745 | rc_confs = [conf for conf in RCConfiguration.objects.filter(experiment=id_exp, |
|
746 | 746 | mix=False)] |
|
747 | 747 | |
|
748 | 748 | if len(rc_confs)<2: |
|
749 | 749 | messages.warning(request, 'You need at least two RC Configurations to make a mix') |
|
750 | 750 | return redirect(experiment.get_absolute_url()) |
|
751 | 751 | |
|
752 | 752 | mix_confs = RCConfiguration.objects.filter(experiment=id_exp, mix=True) |
|
753 | 753 | |
|
754 | 754 | if mix_confs: |
|
755 | 755 | mix = mix_confs[0] |
|
756 | 756 | else: |
|
757 | 757 | mix = RCConfiguration(experiment=experiment, |
|
758 | 758 | device=rc_confs[0].device, |
|
759 | 759 | ipp=rc_confs[0].ipp, |
|
760 | 760 | clock_in=rc_confs[0].clock_in, |
|
761 | 761 | clock_divider=rc_confs[0].clock_divider, |
|
762 | 762 | mix=True, |
|
763 | 763 | parameters='') |
|
764 | 764 | mix.save() |
|
765 | 765 | |
|
766 | 766 | line_type = RCLineType.objects.get(name='mix') |
|
767 | 767 | for i in range(len(rc_confs[0].get_lines())): |
|
768 | 768 | line = RCLine(rc_configuration=mix, line_type=line_type, channel=i) |
|
769 | 769 | line.save() |
|
770 | 770 | |
|
771 | 771 | initial = {'name': mix.name, |
|
772 | 772 | 'result': parse_mix_result(mix.parameters), |
|
773 | 773 | 'delay': 0, |
|
774 | 774 | 'mask': [0,1,2,3,4,5,6,7] |
|
775 | 775 | } |
|
776 | 776 | |
|
777 | 777 | if request.method=='GET': |
|
778 | 778 | form = RCMixConfigurationForm(confs=rc_confs, initial=initial) |
|
779 | 779 | |
|
780 | 780 | if request.method=='POST': |
|
781 | 781 | result = mix.parameters |
|
782 | 782 | |
|
783 | 783 | if '{}|'.format(request.POST['experiment']) in result: |
|
784 | 784 | messages.error(request, 'Configuration already added') |
|
785 | 785 | else: |
|
786 | 786 | if 'operation' in request.POST: |
|
787 | 787 | operation = MIX_OPERATIONS[request.POST['operation']] |
|
788 | 788 | else: |
|
789 | 789 | operation = ' ' |
|
790 | 790 | |
|
791 | 791 | mode = MIX_MODES[request.POST['mode']] |
|
792 | 792 | |
|
793 | 793 | if result: |
|
794 | 794 | result = '{}-{}|{}|{}|{}|{}'.format(mix.parameters, |
|
795 | 795 | request.POST['experiment'], |
|
796 | 796 | mode, |
|
797 | 797 | operation, |
|
798 | 798 | float(request.POST['delay']), |
|
799 | 799 | parse_mask(request.POST.getlist('mask')) |
|
800 | 800 | ) |
|
801 | 801 | else: |
|
802 | 802 | result = '{}|{}|{}|{}|{}'.format(request.POST['experiment'], |
|
803 | 803 | mode, |
|
804 | 804 | operation, |
|
805 | 805 | float(request.POST['delay']), |
|
806 | 806 | parse_mask(request.POST.getlist('mask')) |
|
807 | 807 | ) |
|
808 | 808 | |
|
809 | 809 | mix.parameters = result |
|
810 | 810 | mix.name = request.POST['name'] |
|
811 | 811 | mix.save() |
|
812 | 812 | mix.update_pulses() |
|
813 | 813 | |
|
814 | 814 | initial['result'] = parse_mix_result(result) |
|
815 | 815 | initial['name'] = mix.name |
|
816 | 816 | |
|
817 | 817 | form = RCMixConfigurationForm(initial=initial, confs=rc_confs) |
|
818 | 818 | |
|
819 | 819 | |
|
820 | 820 | kwargs = { |
|
821 | 821 | 'title': 'Experiment', |
|
822 | 822 | 'suptitle': 'Mix Configurations', |
|
823 | 823 | 'form' : form, |
|
824 | 824 | 'extra_button': 'Delete', |
|
825 | 825 | 'button': 'Add', |
|
826 | 826 | 'cancel': 'Back', |
|
827 | 827 | 'previous': experiment.get_absolute_url(), |
|
828 | 828 | 'id_exp':id_exp, |
|
829 | 829 | |
|
830 | 830 | } |
|
831 | 831 | |
|
832 | 832 | return render(request, 'experiment_mix.html', kwargs) |
|
833 | 833 | |
|
834 | 834 | |
|
835 | 835 | @user_passes_test(lambda u:u.is_staff) |
|
836 | 836 | def experiment_mix_delete(request, id_exp): |
|
837 | 837 | |
|
838 | 838 | conf = RCConfiguration.objects.get(experiment=id_exp, mix=True) |
|
839 | 839 | values = conf.parameters.split('-') |
|
840 | 840 | conf.parameters = '-'.join(values[:-1]) |
|
841 | 841 | conf.save() |
|
842 | 842 | |
|
843 | 843 | return redirect('url_mix_experiment', id_exp=id_exp) |
|
844 | 844 | |
|
845 | 845 | |
|
846 | 846 | def experiment_summary(request, id_exp): |
|
847 | 847 | |
|
848 | 848 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
849 | 849 | configurations = Configuration.objects.filter(experiment=experiment, type=0) |
|
850 | 850 | |
|
851 | 851 | kwargs = {} |
|
852 | 852 | |
|
853 | 853 | kwargs['experiment_keys'] = ['radar_system', 'name', 'freq', 'start_time', 'end_time'] |
|
854 | 854 | kwargs['experiment'] = experiment |
|
855 | 855 | |
|
856 | 856 | kwargs['configurations'] = [] |
|
857 | 857 | |
|
858 | 858 | kwargs['title'] = 'Experiment Summary' |
|
859 | 859 | kwargs['suptitle'] = 'Details' |
|
860 | 860 | |
|
861 | 861 | kwargs['button'] = 'Verify Parameters' |
|
862 | 862 | |
|
863 | 863 | c_vel = 3.0*(10**8) #m/s |
|
864 | 864 | ope_freq = experiment.freq*(10**6) #1/s |
|
865 | 865 | radar_lambda = c_vel/ope_freq #m |
|
866 | 866 | kwargs['radar_lambda'] = radar_lambda |
|
867 | 867 | |
|
868 | 868 | jars_conf = False |
|
869 | 869 | rc_conf = False |
|
870 | 870 | code_id = 0 |
|
871 | 871 | tx_line = {} |
|
872 | 872 | |
|
873 | 873 | for configuration in configurations: |
|
874 | 874 | |
|
875 | 875 | #--------------------- RC ----------------------: |
|
876 | 876 | if configuration.device.device_type.name == 'rc': |
|
877 | 877 | if configuration.mix: |
|
878 | 878 | continue |
|
879 | 879 | rc_conf = True |
|
880 | 880 | ipp = configuration.ipp |
|
881 | 881 | lines = configuration.get_lines(line_type__name='tx') |
|
882 | 882 | configuration.tx_lines = [] |
|
883 | 883 | |
|
884 | 884 | for tx_line in lines: |
|
885 | 885 | if tx_line.get_name()[-1] == 'A': |
|
886 | 886 | txa_line = tx_line |
|
887 | 887 | txa_params = json.loads(txa_line.params) |
|
888 | 888 | line = {'name':tx_line.get_name()} |
|
889 | 889 | tx_params = json.loads(tx_line.params) |
|
890 | 890 | line['width'] = tx_params['pulse_width'] |
|
891 | 891 | if line['width'] in (0, '0'): |
|
892 | 892 | continue |
|
893 | 893 | delays = tx_params['delays'] |
|
894 | 894 | |
|
895 | 895 | if delays not in ('', '0'): |
|
896 | 896 | n = len(delays.split(',')) |
|
897 | 897 | line['taus'] = '{} Taus: {}'.format(n, delays) |
|
898 | 898 | else: |
|
899 | 899 | line['taus'] = '-' |
|
900 | 900 | |
|
901 | 901 | for code_line in configuration.get_lines(line_type__name='codes'): |
|
902 | 902 | code_params = json.loads(code_line.params) |
|
903 | 903 | code_id = code_params['code'] |
|
904 | 904 | if tx_line.pk==int(code_params['TX_ref']): |
|
905 | 905 | line['codes'] = '{}:{}'.format(RCLineCode.objects.get(pk=code_params['code']), |
|
906 | 906 | '-'.join(code_params['codes'])) |
|
907 | 907 | |
|
908 | 908 | for windows_line in configuration.get_lines(line_type__name='windows'): |
|
909 | 909 | win_params = json.loads(windows_line.params) |
|
910 | 910 | if tx_line.pk==int(win_params['TX_ref']): |
|
911 | 911 | windows = '' |
|
912 | 912 | nsa = win_params['params'][0]['number_of_samples'] |
|
913 | 913 | for i, params in enumerate(win_params['params']): |
|
914 | 914 | windows += 'W{}: Ho={first_height} km DH={resolution} km NSA={number_of_samples}<br>'.format(i, **params) |
|
915 | 915 | line['windows'] = mark_safe(windows) |
|
916 | 916 | |
|
917 | 917 | configuration.tx_lines.append(line) |
|
918 | 918 | |
|
919 | 919 | if txa_line: |
|
920 | 920 | kwargs['duty_cycle'] = float(txa_params['pulse_width'])/ipp*100 |
|
921 | 921 | |
|
922 | 922 | #-------------------- JARS -----------------------: |
|
923 | 923 | if configuration.device.device_type.name == 'jars': |
|
924 | 924 | jars_conf = True |
|
925 | 925 | kwargs['exp_type'] = EXPERIMENT_TYPE[configuration.exp_type][1] |
|
926 | 926 | channels_number = configuration.channels_number |
|
927 | 927 | exp_type = configuration.exp_type |
|
928 | 928 | fftpoints = configuration.fftpoints |
|
929 | 929 | filter_parms = configuration.filter_parms |
|
930 | 930 | filter_parms = ast.literal_eval(filter_parms) |
|
931 | 931 | spectral_number = configuration.spectral_number |
|
932 | 932 | acq_profiles = configuration.acq_profiles |
|
933 | 933 | cohe_integr = configuration.cohe_integr |
|
934 | 934 | profiles_block = configuration.profiles_block |
|
935 | 935 | |
|
936 | 936 | if filter_parms.__class__.__name__=='str': |
|
937 | 937 | filter_parms = eval(filter_parms) |
|
938 | 938 | |
|
939 | 939 | kwargs['configurations'].append(configuration) |
|
940 | 940 | |
|
941 | 941 | |
|
942 | 942 | #------ RC & JARS ------: |
|
943 | 943 | if rc_conf and jars_conf: |
|
944 | 944 | #RC filter values: |
|
945 | 945 | |
|
946 | 946 | if exp_type == 0: #Short |
|
947 | 947 | bytes_ = 2 |
|
948 | 948 | b = nsa*2*bytes_*channels_number |
|
949 | 949 | else: #Float |
|
950 | 950 | bytes_ = 4 |
|
951 | 951 | channels = channels_number + spectral_number |
|
952 | 952 | b = nsa*2*bytes_*fftpoints*channels |
|
953 | 953 | |
|
954 | 954 | codes_num = 7 |
|
955 | 955 | if code_id == 2: |
|
956 | 956 | codes_num = 7 |
|
957 | 957 | elif code_id == 12: |
|
958 | 958 | codes_num = 15 |
|
959 | 959 | |
|
960 | 960 | #Jars filter values: |
|
961 | 961 | try: |
|
962 | 962 | clock = eval(filter_parms['clock']) |
|
963 | 963 | filter_2 = eval(filter_parms['filter_2']) |
|
964 | 964 | filter_5 = eval(filter_parms['filter_5']) |
|
965 | 965 | filter_fir = eval(filter_parms['filter_fir']) |
|
966 | 966 | except: |
|
967 | 967 | clock = float(filter_parms['clock']) |
|
968 | 968 | filter_2 = int(filter_parms['filter_2']) |
|
969 | 969 | filter_5 = int(filter_parms['filter_5']) |
|
970 | 970 | filter_fir = int(filter_parms['filter_fir']) |
|
971 | 971 | Fs_MHz = clock/(filter_2*filter_5*filter_fir) |
|
972 | 972 | |
|
973 | 973 | #Jars values: |
|
974 | 974 | IPP_units = ipp/0.15*Fs_MHz |
|
975 | 975 | IPP_us = IPP_units / Fs_MHz |
|
976 | 976 | IPP_s = IPP_units / (Fs_MHz * (10**6)) |
|
977 | 977 | Ts = 1/(Fs_MHz*(10**6)) |
|
978 | 978 | |
|
979 | 979 | #Values |
|
980 | 980 | Va = radar_lambda/(4*Ts*cohe_integr) |
|
981 | 981 | rate_bh = ((nsa-codes_num)*channels_number*2*bytes_/IPP_us)*(36*(10**8)/cohe_integr) |
|
982 | 982 | rate_gh = rate_bh/(1024*1024*1024) |
|
983 | 983 | kwargs['rate_bh'] = str(rate_bh) + " (Bytes/h)" |
|
984 | 984 | kwargs['rate_gh'] = str(rate_gh)+" (GBytes/h)" |
|
985 | 985 | kwargs['va'] = Va |
|
986 | 986 | kwargs['time_per_block'] = IPP_s * profiles_block * cohe_integr |
|
987 | 987 | kwargs['acqtime'] = IPP_s * acq_profiles |
|
988 | 988 | kwargs['vrange'] = 3/(2*IPP_s*cohe_integr) |
|
989 | 989 | |
|
990 | 990 | else: |
|
991 | 991 | kwargs['rate_bh'] = '' |
|
992 | 992 | kwargs['rate_gh'] = '' |
|
993 | 993 | kwargs['va'] = '' |
|
994 | 994 | kwargs['time_per_block'] = '' |
|
995 | 995 | kwargs['acqtime'] = '' |
|
996 | 996 | kwargs['vrange'] = '' |
|
997 | 997 | |
|
998 | 998 | ###### SIDEBAR ###### |
|
999 | 999 | kwargs.update(sidebar(experiment=experiment)) |
|
1000 | 1000 | |
|
1001 | 1001 | return render(request, 'experiment_summary.html', kwargs) |
|
1002 | 1002 | |
|
1003 | 1003 | |
|
1004 | 1004 | @user_passes_test(lambda u:u.is_staff) |
|
1005 | 1005 | def experiment_verify(request, id_exp): |
|
1006 | 1006 | |
|
1007 | 1007 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
1008 | 1008 | experiment_data = experiment.parms_to_dict() |
|
1009 | 1009 | configurations = Configuration.objects.filter(experiment=experiment, type=0) |
|
1010 | 1010 | |
|
1011 | 1011 | kwargs = {} |
|
1012 | 1012 | |
|
1013 | 1013 | kwargs['experiment_keys'] = ['template', 'radar_system', 'name', 'start_time', 'end_time'] |
|
1014 | 1014 | kwargs['experiment'] = experiment |
|
1015 | 1015 | |
|
1016 | 1016 | kwargs['configuration_keys'] = ['name', 'device__ip_address', 'device__port_address', 'device__status'] |
|
1017 | 1017 | kwargs['configurations'] = configurations |
|
1018 | 1018 | kwargs['experiment_data'] = experiment_data |
|
1019 | 1019 | |
|
1020 | 1020 | kwargs['title'] = 'Verify Experiment' |
|
1021 | 1021 | kwargs['suptitle'] = 'Parameters' |
|
1022 | 1022 | |
|
1023 | 1023 | kwargs['button'] = 'Update' |
|
1024 | 1024 | |
|
1025 | 1025 | jars_conf = False |
|
1026 | 1026 | rc_conf = False |
|
1027 | 1027 | dds_conf = False |
|
1028 | 1028 | |
|
1029 | 1029 | for configuration in configurations: |
|
1030 | 1030 | #-------------------- JARS -----------------------: |
|
1031 | 1031 | if configuration.device.device_type.name == 'jars': |
|
1032 | 1032 | jars_conf = True |
|
1033 | 1033 | jars = configuration |
|
1034 | 1034 | kwargs['jars_conf'] = jars_conf |
|
1035 | 1035 | filter_parms = jars.filter_parms |
|
1036 | 1036 | filter_parms = ast.literal_eval(filter_parms) |
|
1037 | 1037 | kwargs['filter_parms'] = filter_parms |
|
1038 | 1038 | #--Sampling Frequency |
|
1039 | 1039 | clock = eval(filter_parms['clock']) |
|
1040 | 1040 | filter_2 = eval(filter_parms['filter_2']) |
|
1041 | 1041 | filter_5 = eval(filter_parms['filter_5']) |
|
1042 | 1042 | filter_fir = eval(filter_parms['filter_fir']) |
|
1043 | 1043 | samp_freq_jars = clock/filter_2/filter_5/filter_fir |
|
1044 | 1044 | |
|
1045 | 1045 | kwargs['samp_freq_jars'] = samp_freq_jars |
|
1046 | 1046 | kwargs['jars'] = configuration |
|
1047 | 1047 | |
|
1048 | 1048 | #--------------------- RC ----------------------: |
|
1049 | 1049 | if configuration.device.device_type.name == 'rc' and not configuration.mix: |
|
1050 | 1050 | rc_conf = True |
|
1051 | 1051 | rc = configuration |
|
1052 | 1052 | |
|
1053 | 1053 | rc_parms = configuration.parms_to_dict() |
|
1054 | 1054 | |
|
1055 | 1055 | win_lines = rc.get_lines(line_type__name='windows') |
|
1056 | 1056 | if win_lines: |
|
1057 | 1057 | dh = json.loads(win_lines[0].params)['params'][0]['resolution'] |
|
1058 | 1058 | #--Sampling Frequency |
|
1059 | 1059 | samp_freq_rc = 0.15/dh |
|
1060 | 1060 | kwargs['samp_freq_rc'] = samp_freq_rc |
|
1061 | 1061 | |
|
1062 | 1062 | kwargs['rc_conf'] = rc_conf |
|
1063 | 1063 | kwargs['rc'] = configuration |
|
1064 | 1064 | |
|
1065 | 1065 | #-------------------- DDS ----------------------: |
|
1066 | 1066 | if configuration.device.device_type.name == 'dds': |
|
1067 | 1067 | dds_conf = True |
|
1068 | 1068 | dds = configuration |
|
1069 | 1069 | dds_parms = configuration.parms_to_dict() |
|
1070 | 1070 | |
|
1071 | 1071 | kwargs['dds_conf'] = dds_conf |
|
1072 | 1072 | kwargs['dds'] = configuration |
|
1073 | 1073 | |
|
1074 | 1074 | |
|
1075 | 1075 | #------------Validation------------: |
|
1076 | 1076 | #Clock |
|
1077 | 1077 | if dds_conf and rc_conf and jars_conf: |
|
1078 | 1078 | 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']): |
|
1079 | 1079 | messages.warning(request, "Devices don't have the same clock.") |
|
1080 | 1080 | elif rc_conf and jars_conf: |
|
1081 | 1081 | if float(filter_parms['clock']) != float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']): |
|
1082 | 1082 | messages.warning(request, "Devices don't have the same clock.") |
|
1083 | 1083 | elif rc_conf and dds_conf: |
|
1084 | 1084 | if float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']) != float(dds_parms['configurations']['byId'][str(dds.pk)]['clock']): |
|
1085 | 1085 | messages.warning(request, "Devices don't have the same clock.") |
|
1086 | 1086 | if float(samp_freq_rc) != float(dds_parms['configurations']['byId'][str(dds.pk)]['frequencyA']): |
|
1087 | 1087 | messages.warning(request, "Devices don't have the same Frequency A.") |
|
1088 | 1088 | |
|
1089 | 1089 | |
|
1090 | 1090 | #------------POST METHOD------------: |
|
1091 | 1091 | if request.method == 'POST': |
|
1092 | 1092 | if request.POST['suggest_clock']: |
|
1093 | 1093 | try: |
|
1094 | 1094 | suggest_clock = float(request.POST['suggest_clock']) |
|
1095 | 1095 | except: |
|
1096 | 1096 | messages.warning(request, "Invalid value in CLOCK IN.") |
|
1097 | 1097 | return redirect('url_verify_experiment', id_exp=experiment.id) |
|
1098 | 1098 | else: |
|
1099 | 1099 | suggest_clock = "" |
|
1100 | 1100 | if suggest_clock: |
|
1101 | 1101 | if rc_conf: |
|
1102 | 1102 | rc.clock_in = suggest_clock |
|
1103 | 1103 | rc.save() |
|
1104 | 1104 | if jars_conf: |
|
1105 | 1105 | filter_parms = jars.filter_parms |
|
1106 | 1106 | filter_parms = ast.literal_eval(filter_parms) |
|
1107 | 1107 | filter_parms['clock'] = suggest_clock |
|
1108 | 1108 | jars.filter_parms = json.dumps(filter_parms) |
|
1109 | 1109 | jars.save() |
|
1110 | 1110 | kwargs['filter_parms'] = filter_parms |
|
1111 | 1111 | if dds_conf: |
|
1112 | 1112 | dds.clock = suggest_clock |
|
1113 | 1113 | dds.save() |
|
1114 | 1114 | |
|
1115 | 1115 | if request.POST['suggest_frequencyA']: |
|
1116 | 1116 | try: |
|
1117 | 1117 | suggest_frequencyA = float(request.POST['suggest_frequencyA']) |
|
1118 | 1118 | except: |
|
1119 | 1119 | messages.warning(request, "Invalid value in FREQUENCY A.") |
|
1120 | 1120 | return redirect('url_verify_experiment', id_exp=experiment.id) |
|
1121 | 1121 | else: |
|
1122 | 1122 | suggest_frequencyA = "" |
|
1123 | 1123 | if suggest_frequencyA: |
|
1124 | 1124 | if jars_conf: |
|
1125 | 1125 | filter_parms = jars.filter_parms |
|
1126 | 1126 | filter_parms = ast.literal_eval(filter_parms) |
|
1127 | 1127 | filter_parms['fch'] = suggest_frequencyA |
|
1128 | 1128 | jars.filter_parms = json.dumps(filter_parms) |
|
1129 | 1129 | jars.save() |
|
1130 | 1130 | kwargs['filter_parms'] = filter_parms |
|
1131 | 1131 | if dds_conf: |
|
1132 | 1132 | dds.frequencyA_Mhz = request.POST['suggest_frequencyA'] |
|
1133 | 1133 | dds.save() |
|
1134 | 1134 | |
|
1135 | 1135 | ###### SIDEBAR ###### |
|
1136 | 1136 | kwargs.update(sidebar(experiment=experiment)) |
|
1137 | 1137 | |
|
1138 | 1138 | |
|
1139 | 1139 | |
|
1140 | 1140 | |
|
1141 | 1141 | |
|
1142 | 1142 | return render(request, 'experiment_verify.html', kwargs) |
|
1143 | 1143 | |
|
1144 | 1144 | |
|
1145 | 1145 | #@user_passes_test(lambda u:u.is_staff) |
|
1146 | 1146 | def parse_mix_result(s): |
|
1147 | 1147 | |
|
1148 | 1148 | values = s.split('-') |
|
1149 | 1149 | html = 'EXP MOD OPE DELAY MASK\r\n' |
|
1150 | 1150 | |
|
1151 | 1151 | if not values or values[0] in ('', ' '): |
|
1152 | 1152 | return mark_safe(html) |
|
1153 | 1153 | |
|
1154 | 1154 | for i, value in enumerate(values): |
|
1155 | 1155 | if not value: |
|
1156 | 1156 | continue |
|
1157 | 1157 | pk, mode, operation, delay, mask = value.split('|') |
|
1158 | 1158 | conf = RCConfiguration.objects.get(pk=pk) |
|
1159 | 1159 | if i==0: |
|
1160 | 1160 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( |
|
1161 | 1161 | conf.name, |
|
1162 | 1162 | mode, |
|
1163 | 1163 | ' ', |
|
1164 | 1164 | delay, |
|
1165 | 1165 | mask) |
|
1166 | 1166 | else: |
|
1167 | 1167 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( |
|
1168 | 1168 | conf.name, |
|
1169 | 1169 | mode, |
|
1170 | 1170 | operation, |
|
1171 | 1171 | delay, |
|
1172 | 1172 | mask) |
|
1173 | 1173 | |
|
1174 | 1174 | return mark_safe(html) |
|
1175 | 1175 | |
|
1176 | 1176 | def parse_mask(l): |
|
1177 | 1177 | |
|
1178 | 1178 | values = [] |
|
1179 | 1179 | |
|
1180 | 1180 | for x in range(8): |
|
1181 | 1181 | if '{}'.format(x) in l: |
|
1182 | 1182 | values.append(1) |
|
1183 | 1183 | else: |
|
1184 | 1184 | values.append(0) |
|
1185 | 1185 | |
|
1186 | 1186 | values.reverse() |
|
1187 | 1187 | |
|
1188 | 1188 | return int(''.join([str(x) for x in values]), 2) |
|
1189 | 1189 | |
|
1190 | 1190 | |
|
1191 | 1191 | def dev_confs(request): |
|
1192 | 1192 | |
|
1193 | 1193 | |
|
1194 | 1194 | page = request.GET.get('page') |
|
1195 | 1195 | order = ('type', 'device__device_type', 'experiment') |
|
1196 | 1196 | filters = request.GET.copy() |
|
1197 | 1197 | |
|
1198 | 1198 | kwargs = get_paginator(Configuration, page, order, filters) |
|
1199 | 1199 | |
|
1200 | 1200 | form = FilterForm(initial=request.GET, extra_fields=['tags','template']) |
|
1201 | 1201 | kwargs['keys'] = ['name', 'experiment', 'type', 'programmed_date'] |
|
1202 | 1202 | kwargs['title'] = 'Configuration' |
|
1203 | 1203 | kwargs['suptitle'] = 'List' |
|
1204 | 1204 | kwargs['no_sidebar'] = True |
|
1205 | 1205 | kwargs['form'] = form |
|
1206 | 1206 | filters.pop('page', None) |
|
1207 | 1207 | kwargs['q'] = urlencode(filters) |
|
1208 | 1208 | |
|
1209 | 1209 | return render(request, 'base_list.html', kwargs) |
|
1210 | 1210 | |
|
1211 | 1211 | |
|
1212 | 1212 | def dev_conf(request, id_conf): |
|
1213 | 1213 | |
|
1214 | 1214 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1215 | 1215 | |
|
1216 | 1216 | return redirect(conf.get_absolute_url()) |
|
1217 | 1217 | |
|
1218 | 1218 | |
|
1219 | 1219 | @user_passes_test(lambda u:u.is_staff) |
|
1220 | 1220 | def dev_conf_new(request, id_exp=0, id_dev=0): |
|
1221 | 1221 | |
|
1222 | 1222 | initial = {} |
|
1223 | 1223 | kwargs = {} |
|
1224 | 1224 | |
|
1225 | 1225 | if id_exp!=0: |
|
1226 | 1226 | initial['experiment'] = id_exp |
|
1227 | 1227 | |
|
1228 | 1228 | if id_dev!=0: |
|
1229 | 1229 | initial['device'] = id_dev |
|
1230 | 1230 | |
|
1231 | 1231 | if request.method == 'GET': |
|
1232 | 1232 | |
|
1233 | 1233 | if id_dev: |
|
1234 | 1234 | kwargs['button'] = 'Create' |
|
1235 | 1235 | device = Device.objects.get(pk=id_dev) |
|
1236 | 1236 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
1237 | 1237 | initial['name'] = request.GET['name'] |
|
1238 | 1238 | form = DevConfForm(initial=initial) |
|
1239 | 1239 | else: |
|
1240 | 1240 | if 'template' in request.GET: |
|
1241 | 1241 | if request.GET['template']=='0': |
|
1242 | 1242 | choices = [(conf.pk, '{}'.format(conf)) for conf in Configuration.objects.filter(template=True)] |
|
1243 | 1243 | form = NewForm(initial={'create_from':2}, |
|
1244 | 1244 | template_choices=choices) |
|
1245 | 1245 | else: |
|
1246 | 1246 | kwargs['button'] = 'Create' |
|
1247 | 1247 | conf = Configuration.objects.get(pk=request.GET['template']) |
|
1248 | 1248 | id_dev = conf.device.pk |
|
1249 | 1249 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1250 | 1250 | form = DevConfForm(instance=conf, |
|
1251 | 1251 | initial={'name': '{}_{:%y%m%d}'.format(conf.name, datetime.now()), |
|
1252 | 1252 | 'template': False, |
|
1253 | 1253 | 'experiment':id_exp}) |
|
1254 | 1254 | elif 'blank' in request.GET: |
|
1255 | 1255 | kwargs['button'] = 'Create' |
|
1256 | 1256 | form = ConfigurationForm(initial=initial) |
|
1257 | 1257 | else: |
|
1258 | 1258 | form = NewForm() |
|
1259 | 1259 | |
|
1260 | 1260 | if request.method == 'POST': |
|
1261 | 1261 | |
|
1262 | 1262 | device = Device.objects.get(pk=request.POST['device']) |
|
1263 | 1263 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
1264 | 1264 | |
|
1265 | 1265 | form = DevConfForm(request.POST) |
|
1266 | 1266 | kwargs['button'] = 'Create' |
|
1267 | 1267 | if form.is_valid(): |
|
1268 | 1268 | conf = form.save() |
|
1269 | 1269 | |
|
1270 | 1270 | if 'template' in request.GET and conf.device.device_type.name=='rc': |
|
1271 | 1271 | lines = RCLine.objects.filter(rc_configuration=request.GET['template']) |
|
1272 | 1272 | for line in lines: |
|
1273 | 1273 | line.clone(rc_configuration=conf) |
|
1274 | 1274 | |
|
1275 | 1275 | new_lines = conf.get_lines() |
|
1276 | 1276 | for line in new_lines: |
|
1277 | 1277 | line_params = json.loads(line.params) |
|
1278 | 1278 | if 'TX_ref' in line_params: |
|
1279 | 1279 | ref_line = RCLine.objects.get(pk=line_params['TX_ref']) |
|
1280 | 1280 | line_params['TX_ref'] = ['{}'.format(l.pk) for l in new_lines if l.get_name()==ref_line.get_name()][0] |
|
1281 | 1281 | line.params = json.dumps(line_params) |
|
1282 | 1282 | line.save() |
|
1283 | 1283 | |
|
1284 | if conf.device.device_type.name=='jars': | |
|
1285 | conf.add_parms_to_filter() | |
|
1286 | ||
|
1287 | 1284 | return redirect('url_dev_conf', id_conf=conf.pk) |
|
1288 | 1285 | |
|
1289 | 1286 | kwargs['id_exp'] = id_exp |
|
1290 | 1287 | kwargs['form'] = form |
|
1291 | 1288 | kwargs['title'] = 'Configuration' |
|
1292 | 1289 | kwargs['suptitle'] = 'New' |
|
1293 | 1290 | |
|
1294 | 1291 | |
|
1295 | 1292 | if id_dev != 0: |
|
1296 | 1293 | device = Device.objects.get(pk=id_dev) |
|
1297 | 1294 | kwargs['device'] = device.device_type.name |
|
1298 | 1295 | |
|
1299 | 1296 | return render(request, 'dev_conf_edit.html', kwargs) |
|
1300 | 1297 | |
|
1301 | 1298 | |
|
1302 | 1299 | @user_passes_test(lambda u:u.is_staff) |
|
1303 | 1300 | def dev_conf_edit(request, id_conf): |
|
1304 | 1301 | |
|
1305 | 1302 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1306 | 1303 | |
|
1307 | 1304 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1308 | 1305 | |
|
1309 | 1306 | if request.method=='GET': |
|
1310 | 1307 | form = DevConfForm(instance=conf) |
|
1311 | 1308 | |
|
1312 | 1309 | if request.method=='POST': |
|
1313 | 1310 | form = DevConfForm(request.POST, instance=conf) |
|
1314 | 1311 | |
|
1315 | 1312 | if form.is_valid(): |
|
1316 | 1313 | form.save() |
|
1317 | 1314 | return redirect('url_dev_conf', id_conf=id_conf) |
|
1318 | 1315 | |
|
1319 | 1316 | kwargs = {} |
|
1320 | 1317 | kwargs['form'] = form |
|
1321 | 1318 | kwargs['title'] = 'Device Configuration' |
|
1322 | 1319 | kwargs['suptitle'] = 'Edit' |
|
1323 | 1320 | kwargs['button'] = 'Update' |
|
1324 | 1321 | |
|
1325 | 1322 | ###### SIDEBAR ###### |
|
1326 | 1323 | kwargs.update(sidebar(conf=conf)) |
|
1327 | 1324 | |
|
1328 | 1325 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1329 | 1326 | |
|
1330 | 1327 | |
|
1331 | 1328 | @user_passes_test(lambda u:u.is_staff) |
|
1332 | 1329 | def dev_conf_start(request, id_conf): |
|
1333 | 1330 | |
|
1334 | 1331 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1335 | 1332 | |
|
1336 | 1333 | if conf.start_device(): |
|
1337 | 1334 | messages.success(request, conf.message) |
|
1338 | 1335 | else: |
|
1339 | 1336 | messages.error(request, conf.message) |
|
1340 | 1337 | |
|
1341 | 1338 | #conf.status_device() |
|
1342 | 1339 | |
|
1343 | 1340 | return redirect(conf.get_absolute_url()) |
|
1344 | 1341 | |
|
1345 | 1342 | |
|
1346 | 1343 | @user_passes_test(lambda u:u.is_staff) |
|
1347 | 1344 | def dev_conf_stop(request, id_conf): |
|
1348 | 1345 | |
|
1349 | 1346 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1350 | 1347 | |
|
1351 | 1348 | if conf.stop_device(): |
|
1352 | 1349 | messages.success(request, conf.message) |
|
1353 | 1350 | else: |
|
1354 | 1351 | messages.error(request, conf.message) |
|
1355 | 1352 | |
|
1356 | 1353 | #conf.status_device() |
|
1357 | 1354 | |
|
1358 | 1355 | return redirect(conf.get_absolute_url()) |
|
1359 | 1356 | |
|
1360 | 1357 | |
|
1361 | 1358 | def dev_conf_status(request, id_conf): |
|
1362 | 1359 | |
|
1363 | 1360 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1364 | 1361 | |
|
1365 | 1362 | if conf.status_device(): |
|
1366 | 1363 | messages.success(request, conf.message) |
|
1367 | 1364 | else: |
|
1368 | 1365 | messages.error(request, conf.message) |
|
1369 | 1366 | |
|
1370 | 1367 | return redirect(conf.get_absolute_url()) |
|
1371 | 1368 | |
|
1372 | 1369 | |
|
1373 | 1370 | @user_passes_test(lambda u:u.is_staff) |
|
1374 | 1371 | def dev_conf_reset(request, id_conf): |
|
1375 | 1372 | |
|
1376 | 1373 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1377 | 1374 | |
|
1378 | 1375 | if conf.reset_device(): |
|
1379 | 1376 | messages.success(request, conf.message) |
|
1380 | 1377 | else: |
|
1381 | 1378 | messages.error(request, conf.message) |
|
1382 | 1379 | |
|
1383 | 1380 | return redirect(conf.get_absolute_url()) |
|
1384 | 1381 | |
|
1385 | 1382 | |
|
1386 | 1383 | @user_passes_test(lambda u:u.is_staff) |
|
1387 | 1384 | def dev_conf_write(request, id_conf): |
|
1388 | 1385 | |
|
1389 | 1386 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1390 | 1387 | |
|
1391 | 1388 | if conf.write_device(): |
|
1392 | 1389 | messages.success(request, conf.message) |
|
1393 | 1390 | conf.clone(type=1, template=False) |
|
1394 | 1391 | else: |
|
1395 | 1392 | messages.error(request, conf.message) |
|
1396 | 1393 | |
|
1397 | 1394 | return redirect(conf.get_absolute_url()) |
|
1398 | 1395 | |
|
1399 | 1396 | |
|
1400 | 1397 | @user_passes_test(lambda u:u.is_staff) |
|
1401 | 1398 | def dev_conf_read(request, id_conf): |
|
1402 | 1399 | |
|
1403 | 1400 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1404 | 1401 | |
|
1405 | 1402 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1406 | 1403 | |
|
1407 | 1404 | if request.method=='GET': |
|
1408 | 1405 | |
|
1409 | 1406 | parms = conf.read_device() |
|
1410 | 1407 | #conf.status_device() |
|
1411 | 1408 | |
|
1412 | 1409 | if not parms: |
|
1413 | 1410 | messages.error(request, conf.message) |
|
1414 | 1411 | return redirect(conf.get_absolute_url()) |
|
1415 | 1412 | |
|
1416 | 1413 | form = DevConfForm(initial=parms, instance=conf) |
|
1417 | 1414 | |
|
1418 | 1415 | if request.method=='POST': |
|
1419 | 1416 | form = DevConfForm(request.POST, instance=conf) |
|
1420 | 1417 | |
|
1421 | 1418 | if form.is_valid(): |
|
1422 | 1419 | form.save() |
|
1423 | 1420 | return redirect(conf.get_absolute_url()) |
|
1424 | 1421 | |
|
1425 | 1422 | messages.error(request, "Parameters could not be saved") |
|
1426 | 1423 | |
|
1427 | 1424 | kwargs = {} |
|
1428 | 1425 | kwargs['id_dev'] = conf.id |
|
1429 | 1426 | kwargs['form'] = form |
|
1430 | 1427 | kwargs['title'] = 'Device Configuration' |
|
1431 | 1428 | kwargs['suptitle'] = 'Parameters read from device' |
|
1432 | 1429 | kwargs['button'] = 'Save' |
|
1433 | 1430 | |
|
1434 | 1431 | ###### SIDEBAR ###### |
|
1435 | 1432 | kwargs.update(sidebar(conf=conf)) |
|
1436 | 1433 | |
|
1437 | 1434 | return render(request, '%s_conf_edit.html' %conf.device.device_type.name, kwargs) |
|
1438 | 1435 | |
|
1439 | 1436 | |
|
1440 | 1437 | @user_passes_test(lambda u:u.is_staff) |
|
1441 | 1438 | def dev_conf_import(request, id_conf): |
|
1442 | 1439 | |
|
1443 | 1440 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1444 | 1441 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1445 | 1442 | |
|
1446 | 1443 | if request.method == 'GET': |
|
1447 | 1444 | file_form = UploadFileForm() |
|
1448 | 1445 | |
|
1449 | 1446 | if request.method == 'POST': |
|
1450 | 1447 | file_form = UploadFileForm(request.POST, request.FILES) |
|
1451 | 1448 | |
|
1452 | 1449 | if file_form.is_valid(): |
|
1453 | 1450 | |
|
1454 | 1451 | data = conf.import_from_file(request.FILES['file']) |
|
1455 | 1452 | parms = Params(data=data).get_conf(dtype=conf.device.device_type.name) |
|
1456 | 1453 | |
|
1457 | 1454 | if parms: |
|
1458 | 1455 | |
|
1459 | 1456 | form = DevConfForm(initial=parms, instance=conf) |
|
1460 | 1457 | |
|
1461 | 1458 | kwargs = {} |
|
1462 | 1459 | kwargs['id_dev'] = conf.id |
|
1463 | 1460 | kwargs['form'] = form |
|
1464 | 1461 | kwargs['title'] = 'Device Configuration' |
|
1465 | 1462 | kwargs['suptitle'] = 'Parameters imported' |
|
1466 | 1463 | kwargs['button'] = 'Save' |
|
1467 | 1464 | kwargs['action'] = conf.get_absolute_url_edit() |
|
1468 | 1465 | kwargs['previous'] = conf.get_absolute_url() |
|
1469 | 1466 | |
|
1470 | 1467 | ###### SIDEBAR ###### |
|
1471 | 1468 | kwargs.update(sidebar(conf=conf)) |
|
1472 | 1469 | |
|
1473 | 1470 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
1474 | 1471 | |
|
1475 | 1472 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1476 | 1473 | |
|
1477 | 1474 | messages.error(request, "Could not import parameters from file") |
|
1478 | 1475 | |
|
1479 | 1476 | kwargs = {} |
|
1480 | 1477 | kwargs['id_dev'] = conf.id |
|
1481 | 1478 | kwargs['title'] = 'Device Configuration' |
|
1482 | 1479 | kwargs['form'] = file_form |
|
1483 | 1480 | kwargs['suptitle'] = 'Importing file' |
|
1484 | 1481 | kwargs['button'] = 'Import' |
|
1485 | 1482 | |
|
1486 | 1483 | kwargs.update(sidebar(conf=conf)) |
|
1487 | 1484 | |
|
1488 | 1485 | return render(request, 'dev_conf_import.html', kwargs) |
|
1489 | 1486 | |
|
1490 | 1487 | |
|
1491 | 1488 | @user_passes_test(lambda u:u.is_staff) |
|
1492 | 1489 | def dev_conf_export(request, id_conf): |
|
1493 | 1490 | |
|
1494 | 1491 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1495 | 1492 | |
|
1496 | 1493 | if request.method == 'GET': |
|
1497 | 1494 | file_form = DownloadFileForm(conf.device.device_type.name) |
|
1498 | 1495 | |
|
1499 | 1496 | if request.method == 'POST': |
|
1500 | 1497 | file_form = DownloadFileForm(conf.device.device_type.name, request.POST) |
|
1501 | 1498 | |
|
1502 | 1499 | if file_form.is_valid(): |
|
1503 | 1500 | fields = conf.export_to_file(format = file_form.cleaned_data['format']) |
|
1504 | 1501 | |
|
1505 | 1502 | response = HttpResponse(content_type=fields['content_type']) |
|
1506 | 1503 | response['Content-Disposition'] = 'attachment; filename="%s"' %fields['filename'] |
|
1507 | 1504 | response.write(fields['content']) |
|
1508 | 1505 | |
|
1509 | 1506 | return response |
|
1510 | 1507 | |
|
1511 | 1508 | messages.error(request, "Could not export parameters") |
|
1512 | 1509 | |
|
1513 | 1510 | kwargs = {} |
|
1514 | 1511 | kwargs['id_dev'] = conf.id |
|
1515 | 1512 | kwargs['title'] = 'Device Configuration' |
|
1516 | 1513 | kwargs['form'] = file_form |
|
1517 | 1514 | kwargs['suptitle'] = 'Exporting file' |
|
1518 | 1515 | kwargs['button'] = 'Export' |
|
1519 | 1516 | |
|
1520 | 1517 | return render(request, 'dev_conf_export.html', kwargs) |
|
1521 | 1518 | |
|
1522 | 1519 | |
|
1523 | 1520 | @user_passes_test(lambda u:u.is_staff) |
|
1524 | 1521 | def dev_conf_delete(request, id_conf): |
|
1525 | 1522 | |
|
1526 | 1523 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1527 | 1524 | |
|
1528 | 1525 | if request.method=='POST': |
|
1529 | 1526 | if request.user.is_staff: |
|
1530 | 1527 | conf.delete() |
|
1531 | 1528 | return redirect('url_dev_confs') |
|
1532 | 1529 | |
|
1533 | 1530 | messages.error(request, 'Not enough permission to delete this object') |
|
1534 | 1531 | return redirect(conf.get_absolute_url()) |
|
1535 | 1532 | |
|
1536 | 1533 | kwargs = { |
|
1537 | 1534 | 'title': 'Delete', |
|
1538 | 1535 | 'suptitle': 'Experiment', |
|
1539 | 1536 | 'object': conf, |
|
1540 | 1537 | 'previous': conf.get_absolute_url(), |
|
1541 | 1538 | 'delete': True |
|
1542 | 1539 | } |
|
1543 | 1540 | |
|
1544 | 1541 | return render(request, 'confirm.html', kwargs) |
|
1545 | 1542 | |
|
1546 | 1543 | |
|
1547 | 1544 | def sidebar(**kwargs): |
|
1548 | 1545 | |
|
1549 | 1546 | side_data = {} |
|
1550 | 1547 | |
|
1551 | 1548 | conf = kwargs.get('conf', None) |
|
1552 | 1549 | experiment = kwargs.get('experiment', None) |
|
1553 | 1550 | |
|
1554 | 1551 | if not experiment: |
|
1555 | 1552 | experiment = conf.experiment |
|
1556 | 1553 | |
|
1557 | 1554 | if experiment: |
|
1558 | 1555 | side_data['experiment'] = experiment |
|
1559 | 1556 | campaign = experiment.campaign_set.all() |
|
1560 | 1557 | if campaign: |
|
1561 | 1558 | side_data['campaign'] = campaign[0] |
|
1562 | 1559 | experiments = campaign[0].experiments.all() |
|
1563 | 1560 | else: |
|
1564 | 1561 | experiments = [experiment] |
|
1565 | 1562 | configurations = experiment.configuration_set.filter(type=0) |
|
1566 | 1563 | side_data['side_experiments'] = experiments |
|
1567 | 1564 | side_data['side_configurations'] = configurations |
|
1568 | 1565 | |
|
1569 | 1566 | return side_data |
|
1570 | 1567 | |
|
1571 | 1568 | def get_paginator(model, page, order, filters={}, n=10): |
|
1572 | 1569 | |
|
1573 | 1570 | kwargs = {} |
|
1574 | 1571 | query = Q() |
|
1575 | 1572 | if isinstance(filters, QueryDict): |
|
1576 | 1573 | filters = filters.dict() |
|
1577 | 1574 | [filters.pop(key) for key in filters.keys() if filters[key] in ('', ' ')] |
|
1578 | 1575 | filters.pop('page', None) |
|
1579 | 1576 | |
|
1580 | 1577 | if 'template' in filters: |
|
1581 | 1578 | filters['template'] = True |
|
1582 | 1579 | if 'start_date' in filters: |
|
1583 | 1580 | filters['start_date__gte'] = filters.pop('start_date') |
|
1584 | 1581 | if 'end_date' in filters: |
|
1585 | 1582 | filters['start_date__lte'] = filters.pop('end_date') |
|
1586 | 1583 | if 'tags' in filters: |
|
1587 | 1584 | tags = filters.pop('tags') |
|
1588 | 1585 | fields = [f.name for f in model._meta.get_fields()] |
|
1589 | 1586 | |
|
1590 | 1587 | if 'tags' in fields: |
|
1591 | 1588 | query = query | Q(tags__icontains=tags) |
|
1592 | 1589 | if 'name' in fields: |
|
1593 | 1590 | query = query | Q(name__icontains=tags) |
|
1594 | 1591 | if 'location' in fields: |
|
1595 | 1592 | query = query | Q(location__name__icontains=tags) |
|
1596 | 1593 | if 'device' in fields: |
|
1597 | 1594 | query = query | Q(device__device_type__name__icontains=tags) |
|
1598 | 1595 | |
|
1599 | 1596 | object_list = model.objects.filter(query, **filters).order_by(*order) |
|
1600 | 1597 | paginator = Paginator(object_list, n) |
|
1601 | 1598 | |
|
1602 | 1599 | try: |
|
1603 | 1600 | objects = paginator.page(page) |
|
1604 | 1601 | except PageNotAnInteger: |
|
1605 | 1602 | objects = paginator.page(1) |
|
1606 | 1603 | except EmptyPage: |
|
1607 | 1604 | objects = paginator.page(paginator.num_pages) |
|
1608 | 1605 | |
|
1609 | 1606 | kwargs['objects'] = objects |
|
1610 | 1607 | kwargs['offset'] = (int(page)-1)*n if page else 0 |
|
1611 | 1608 | |
|
1612 | 1609 | return kwargs |
|
1613 | 1610 | |
|
1614 | 1611 | def operation(request, id_camp=None): |
|
1615 | 1612 | |
|
1616 | 1613 | kwargs = {} |
|
1617 | 1614 | kwargs['title'] = 'Radars Operation' |
|
1618 | 1615 | kwargs['no_sidebar'] = True |
|
1619 | 1616 | campaigns = Campaign.objects.filter(start_date__lte=datetime.now(), |
|
1620 | 1617 | end_date__gte=datetime.now()).order_by('-start_date') |
|
1621 | 1618 | |
|
1622 | 1619 | |
|
1623 | 1620 | if id_camp: |
|
1624 | 1621 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1625 | 1622 | form = OperationForm(initial={'campaign': campaign.id}, campaigns=campaigns) |
|
1626 | 1623 | kwargs['campaign'] = campaign |
|
1627 | 1624 | else: |
|
1628 | 1625 | form = OperationForm(campaigns=campaigns) |
|
1629 | 1626 | kwargs['form'] = form |
|
1630 | 1627 | return render(request, 'operation.html', kwargs) |
|
1631 | 1628 | |
|
1632 | 1629 | #---Experiment |
|
1633 | 1630 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] |
|
1634 | 1631 | kwargs['experiment_keys'] = keys[1:] |
|
1635 | 1632 | kwargs['experiments'] = experiments |
|
1636 | 1633 | #---Radar |
|
1637 | 1634 | kwargs['locations'] = campaign.get_experiments_by_radar() |
|
1638 | 1635 | kwargs['form'] = form |
|
1639 | 1636 | |
|
1640 | 1637 | return render(request, 'operation.html', kwargs) |
|
1641 | 1638 | |
|
1642 | 1639 | |
|
1643 | 1640 | @login_required |
|
1644 | 1641 | def radar_start(request, id_camp, id_radar): |
|
1645 | 1642 | |
|
1646 | 1643 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1647 | 1644 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] |
|
1648 | 1645 | now = datetime.utcnow() |
|
1649 | 1646 | |
|
1650 | 1647 | for exp in experiments: |
|
1651 | 1648 | date = datetime.combine(datetime.now().date(), exp.start_time) |
|
1652 | 1649 | |
|
1653 | 1650 | if exp.status == 2: |
|
1654 | 1651 | messages.warning(request, 'Experiment {} already running'.format(exp)) |
|
1655 | 1652 | continue |
|
1656 | 1653 | |
|
1657 | 1654 | if exp.status == 3: |
|
1658 | 1655 | messages.warning(request, 'Experiment {} already programmed'.format(exp)) |
|
1659 | 1656 | continue |
|
1660 | 1657 | |
|
1661 | 1658 | if date>campaign.end_date or date<campaign.start_date: |
|
1662 | 1659 | messages.warning(request, 'Experiment {} out of date'.format(exp)) |
|
1663 | 1660 | continue |
|
1664 | 1661 | |
|
1665 | 1662 | if now>=date: |
|
1666 | 1663 | task = task_start.delay(exp.pk) |
|
1667 | 1664 | exp.status = task.wait() |
|
1668 | 1665 | if exp.status==0: |
|
1669 | 1666 | messages.error(request, 'Experiment {} not start'.format(exp)) |
|
1670 | 1667 | if exp.status==2: |
|
1671 | 1668 | messages.success(request, 'Experiment {} started'.format(exp)) |
|
1672 | 1669 | else: |
|
1673 | 1670 | task = task_start.apply_async((exp.pk,), eta=date) |
|
1674 | 1671 | exp.status = 3 |
|
1675 | 1672 | messages.success(request, 'Experiment {} programmed to start at {}'.format(exp, date)) |
|
1676 | 1673 | |
|
1677 | 1674 | exp.save() |
|
1678 | 1675 | |
|
1679 | 1676 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1680 | 1677 | |
|
1681 | 1678 | |
|
1682 | 1679 | @login_required |
|
1683 | 1680 | def radar_stop(request, id_camp, id_radar): |
|
1684 | 1681 | |
|
1685 | 1682 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1686 | 1683 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] |
|
1687 | 1684 | |
|
1688 | 1685 | for exp in experiments: |
|
1689 | 1686 | |
|
1690 | 1687 | if exp.status == 2: |
|
1691 | 1688 | task = task_stop.delay(exp.pk) |
|
1692 | 1689 | exp.status = task.wait() |
|
1693 | 1690 | messages.warning(request, 'Experiment {} stopped'.format(exp)) |
|
1694 | 1691 | exp.save() |
|
1695 | 1692 | else: |
|
1696 | 1693 | messages.error(request, 'Experiment {} not running'.format(exp)) |
|
1697 | 1694 | |
|
1698 | 1695 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1699 | 1696 | |
|
1700 | 1697 | |
|
1701 | 1698 | @login_required |
|
1702 | 1699 | def radar_refresh(request, id_camp, id_radar): |
|
1703 | 1700 | |
|
1704 | 1701 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1705 | 1702 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] |
|
1706 | 1703 | |
|
1707 | 1704 | for exp in experiments: |
|
1708 | 1705 | exp.get_status() |
|
1709 | 1706 | |
|
1710 | 1707 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
General Comments 0
You need to be logged in to leave comments.
Login now