@@ -1,116 +1,116 | |||
|
1 | 1 | import os |
|
2 | 2 | |
|
3 | 3 | from django import forms |
|
4 | 4 | from apps.main.models import Device, Experiment |
|
5 | 5 | from .models import JARSConfiguration, JARSfilter |
|
6 | 6 | from .widgets import SpectralWidget |
|
7 | 7 | from apps.main.forms import add_empty_choice |
|
8 | 8 | |
|
9 | 9 | def create_choices_from_model(model, filter_id=None): |
|
10 | 10 | |
|
11 | 11 | #instance = globals()[model] |
|
12 | 12 | choices = model.objects.all().values_list('pk', 'name') |
|
13 | 13 | choices = add_empty_choice(choices) |
|
14 | 14 | return choices |
|
15 | 15 | |
|
16 | 16 | class JARSConfigurationForm(forms.ModelForm): |
|
17 | 17 | def __init__(self, *args, **kwargs): |
|
18 | 18 | super(JARSConfigurationForm, self).__init__(*args, **kwargs) |
|
19 | 19 | instance = getattr(self, 'instance', None) |
|
20 | 20 | |
|
21 | 21 | if instance and instance.pk: |
|
22 | 22 | devices = Device.objects.filter(device_type__name='jars') |
|
23 | 23 | |
|
24 | 24 | if instance.experiment: |
|
25 | 25 | experiments = Experiment.objects.filter(pk=instance.experiment.id) |
|
26 | 26 | self.fields['experiment'].widget.choices = [(experiment.id, experiment) for experiment in experiments] |
|
27 | 27 | |
|
28 | 28 | self.fields['device'].widget.choices = [(device.id, device) for device in devices] |
|
29 | 29 | #self.fields['spectral'].widget = SpectralWidget() |
|
30 | 30 | self.fields['spectral_number'].widget.attrs['readonly'] = True |
|
31 | 31 | self.fields['spectral'].widget = SpectralWidget() |
|
32 | 32 | |
|
33 | 33 | #-------------JARS Configuration needs an Experiment----------------- |
|
34 | 34 | def clean(self): |
|
35 | 35 | cleaned_data = super(JARSConfigurationForm, self).clean() |
|
36 | 36 | experiment = cleaned_data.get('experiment') |
|
37 | 37 | if experiment == None: |
|
38 | 38 | msg = "Error: Jars Configuration needs an Experiment" |
|
39 | 39 | self.add_error('experiment', msg) |
|
40 | 40 | |
|
41 | 41 | class Meta: |
|
42 | 42 | model = JARSConfiguration |
|
43 | 43 | exclude = ('type', 'parameters', 'status', 'filter_parms') |
|
44 | 44 | |
|
45 | 45 | |
|
46 | 46 | class JARSfilterForm(forms.ModelForm): |
|
47 | 47 | def __init__(self, *args, **kwargs): |
|
48 | 48 | super(JARSfilterForm, self).__init__(*args, **kwargs) |
|
49 | 49 | instance = getattr(self, 'instance', None) |
|
50 | 50 | |
|
51 | 51 | self.fields['fch_decimal'].widget.attrs['readonly'] = True |
|
52 | 52 | |
|
53 | 53 | if 'initial' in kwargs: |
|
54 | 54 | if 'filter_id' not in kwargs['initial']: |
|
55 | 55 | self.fields.pop('name') |
|
56 | 56 | else: |
|
57 | 57 | self.fields['name'] = forms.ChoiceField(choices=create_choices_from_model(JARSfilter)) |
|
58 | 58 | filter_id = kwargs['initial']['filter_id'] |
|
59 | 59 | |
|
60 | 60 | if filter_id == 0: |
|
61 | 61 | for value in self.fields: |
|
62 | 62 | if value != 'name': |
|
63 | 63 | self.fields.pop(value) |
|
64 | 64 | self.fields['name'].label = "Filter Template Name" |
|
65 | 65 | else: |
|
66 | 66 | self.fields['name'] = forms.ChoiceField(choices=create_choices_from_model(JARSfilter, kwargs['initial']['filter_id'])) |
|
67 | 67 | jars_filter = JARSfilter.objects.get(pk=kwargs['initial']['filter_id']) |
|
68 | 68 | labels = [f.name for f in jars_filter._meta.get_fields()] |
|
69 | 69 | |
|
70 |
for label in ['id' |
|
|
70 | for label in ['id']: | |
|
71 | 71 | labels.remove(label) |
|
72 | 72 | for label in labels: |
|
73 | 73 | self.fields['name'].initial = kwargs['initial']['filter_id'] |
|
74 | 74 | self.fields[label].initial = getattr(jars_filter,label) |
|
75 | 75 | self.fields['name'].label = "Filter Template Name" |
|
76 | 76 | |
|
77 | 77 | class Meta: |
|
78 | 78 | model = JARSfilter |
|
79 | 79 | exclude = ('type', 'parameters', 'status') |
|
80 | 80 | |
|
81 | 81 | |
|
82 | 82 | class ExtFileField(forms.FileField): |
|
83 | 83 | """ |
|
84 | 84 | Same as forms.FileField, but you can specify a file extension whitelist. |
|
85 | 85 | |
|
86 | 86 | >>> from django.core.files.uploadedfile import SimpleUploadedFile |
|
87 | 87 | >>> |
|
88 | 88 | >>> t = ExtFileField(ext_whitelist=(".pdf", ".txt")) |
|
89 | 89 | >>> |
|
90 | 90 | >>> t.clean(SimpleUploadedFile('filename.pdf', 'Some File Content')) |
|
91 | 91 | >>> t.clean(SimpleUploadedFile('filename.txt', 'Some File Content')) |
|
92 | 92 | >>> |
|
93 | 93 | >>> t.clean(SimpleUploadedFile('filename.exe', 'Some File Content')) |
|
94 | 94 | Traceback (most recent call last): |
|
95 | 95 | ... |
|
96 | 96 | ValidationError: [u'Not allowed filetype!'] |
|
97 | 97 | """ |
|
98 | 98 | def __init__(self, *args, **kwargs): |
|
99 | 99 | extensions = kwargs.pop("extensions") |
|
100 | 100 | self.extensions = [i.lower() for i in extensions] |
|
101 | 101 | |
|
102 | 102 | super(ExtFileField, self).__init__(*args, **kwargs) |
|
103 | 103 | |
|
104 | 104 | def clean(self, *args, **kwargs): |
|
105 | 105 | data = super(ExtFileField, self).clean(*args, **kwargs) |
|
106 | 106 | filename = data.name |
|
107 | 107 | ext = os.path.splitext(filename)[1] |
|
108 | 108 | ext = ext.lower() |
|
109 | 109 | if ext not in self.extensions: |
|
110 | 110 | raise forms.ValidationError('Not allowed file type: %s' % ext) |
|
111 | 111 | |
|
112 | 112 | |
|
113 | 113 | class JARSImportForm(forms.Form): |
|
114 | 114 | |
|
115 | 115 | #file_name = ExtFileField(extensions=['.racp', '.json', '.dat']) |
|
116 | 116 | file_name = ExtFileField(extensions=['.json']) |
@@ -1,288 +1,283 | |||
|
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 | # Create your models here. |
|
11 | 11 | |
|
12 | 12 | EXPERIMENT_TYPE = ( |
|
13 | 13 | (0, 'RAW_DATA'), |
|
14 | 14 | (1, 'PDATA'), |
|
15 | 15 | ) |
|
16 | 16 | |
|
17 | 17 | DATA_TYPE = ( |
|
18 | 18 | (0, 'SHORT'), |
|
19 | 19 | (1, 'FLOAT'), |
|
20 | 20 | ) |
|
21 | 21 | |
|
22 | 22 | DECODE_TYPE = ( |
|
23 | 23 | (0, 'None'), |
|
24 | 24 | (1, 'TimeDomain'), |
|
25 | 25 | (2, 'FreqDomain'), |
|
26 | 26 | (3, 'InvFreqDomain'), |
|
27 | 27 | ) |
|
28 | 28 | |
|
29 | 29 | class JARSfilter(models.Model): |
|
30 | 30 | |
|
31 | 31 | JARS_NBITS = 32 |
|
32 | 32 | |
|
33 | 33 | name = models.CharField(max_length=60, unique=True, default='') |
|
34 | 34 | clock = models.FloatField(verbose_name='Clock In (MHz)',validators=[MinValueValidator(5), MaxValueValidator(75)], null=True, default=60) |
|
35 | 35 | mult = models.PositiveIntegerField(verbose_name='Multiplier',validators=[MinValueValidator(1), MaxValueValidator(20)], default=5) |
|
36 | 36 | fch = models.FloatField(verbose_name='Frequency (MHz)', validators=[MaxValueValidator(150)], null=True, default=49.9200) |
|
37 | 37 | fch_decimal = models.BigIntegerField(verbose_name='Frequency (Decimal)',validators=[MinValueValidator(-9223372036854775808), MaxValueValidator(2**JARS_NBITS-1)], null=True, default=721554505) |
|
38 | 38 | filter_2 = models.PositiveIntegerField(verbose_name='Filter 2',validators=[MinValueValidator(2), MaxValueValidator(100)], default = 10) |
|
39 | 39 | filter_5 = models.PositiveIntegerField(verbose_name='Filter 5',validators=[MinValueValidator(1), MaxValueValidator(100)], default = 1) |
|
40 | 40 | filter_fir = models.PositiveIntegerField(verbose_name='FIR Filter',validators=[MinValueValidator(1), MaxValueValidator(100)], default = 6) |
|
41 | 41 | |
|
42 | 42 | class Meta: |
|
43 | 43 | db_table = 'jars_filters' |
|
44 | 44 | |
|
45 | 45 | def __unicode__(self): |
|
46 | 46 | return u'%s' % (self.name) |
|
47 | 47 | |
|
48 | 48 | def parms_to_dict(self): |
|
49 | 49 | |
|
50 | 50 | parameters = {} |
|
51 | 51 | |
|
52 | 52 | #parameters['name'] = self.name |
|
53 | 53 | parameters['clock'] = float(self.clock) |
|
54 | 54 | parameters['mult'] = int(self.mult) |
|
55 | 55 | parameters['fch'] = float(self.fch) |
|
56 | 56 | parameters['fch_decimal'] = int(self.fch) |
|
57 | 57 | parameters['filter_fir'] = int(self.filter_fir) |
|
58 | 58 | parameters['filter_2'] = int(self.filter_2) |
|
59 | 59 | parameters['filter_5'] = int(self.filter_5) |
|
60 | 60 | |
|
61 | 61 | return parameters |
|
62 | 62 | |
|
63 | 63 | def dict_to_parms(self, parameters): |
|
64 | 64 | |
|
65 | 65 | #self.name = parameters['name'] |
|
66 | 66 | self.clock = parameters['clock'] |
|
67 | 67 | self.mult = parameters['mult'] |
|
68 | 68 | self.fch = parameters['fch'] |
|
69 | 69 | self.fch_decimal = parameters['fch_decimal'] |
|
70 | 70 | self.filter_fir = parameters['filter_fir'] |
|
71 | 71 | self.filter_2 = parameters['filter_2'] |
|
72 | 72 | self.filter_5 = parameters['filter_5'] |
|
73 | 73 | |
|
74 | 74 | |
|
75 | 75 | class JARSConfiguration(Configuration): |
|
76 | 76 | |
|
77 | 77 | ADC_RESOLUTION = 8 |
|
78 | 78 | PCI_DIO_BUSWIDTH = 32 |
|
79 | 79 | HEADER_VERSION = 1103 |
|
80 | 80 | BEGIN_ON_START = True |
|
81 | 81 | REFRESH_RATE = 1 |
|
82 | 82 | |
|
83 | 83 | exp_type = models.PositiveIntegerField(verbose_name='Experiment Type', choices=EXPERIMENT_TYPE, default=0) |
|
84 | 84 | cards_number = models.PositiveIntegerField(verbose_name='Number of Cards', validators=[MinValueValidator(1), MaxValueValidator(4)], default = 1) |
|
85 | 85 | channels_number = models.PositiveIntegerField(verbose_name='Number of Channels', validators=[MinValueValidator(1), MaxValueValidator(8)], default = 5) |
|
86 | 86 | channels = models.CharField(verbose_name='Channels', max_length=15, default = '1,2,3,4,5') |
|
87 | 87 | data_type = models.PositiveIntegerField(verbose_name='Data Type', choices=DATA_TYPE, default=0) |
|
88 | 88 | raw_data_blocks = models.PositiveIntegerField(verbose_name='Raw Data Blocks', validators=[MaxValueValidator(5000)], default=60) |
|
89 | 89 | profiles_block = models.PositiveIntegerField(verbose_name='Profiles Per Block', default=400) |
|
90 | 90 | acq_profiles = models.PositiveIntegerField(verbose_name='Acquired Profiles', default=400) |
|
91 | 91 | ftp_interval = models.PositiveIntegerField(verbose_name='FTP Interval', default=60) |
|
92 | 92 | fftpoints = models.PositiveIntegerField(verbose_name='FFT Points',default=16) |
|
93 | 93 | cohe_integr_str = models.PositiveIntegerField(verbose_name='Coh. Int. Stride',validators=[MinValueValidator(1)], default=30) |
|
94 | 94 | cohe_integr = models.PositiveIntegerField(verbose_name='Coherent Integrations',validators=[MinValueValidator(1)], default=30) |
|
95 | 95 | incohe_integr = models.PositiveIntegerField(verbose_name='Incoherent Integrations',validators=[MinValueValidator(1)], default=30) |
|
96 | 96 | decode_data = models.PositiveIntegerField(verbose_name='Decode Data', choices=DECODE_TYPE, default=0) |
|
97 | 97 | post_coh_int = models.BooleanField(verbose_name='Post Coherent Integration', default=False) |
|
98 | filter = models.ForeignKey(JARSfilter, on_delete=models.CASCADE, null=True) | |
|
99 | 98 | spectral_number = models.PositiveIntegerField(verbose_name='# Spectral Combinations',validators=[MinValueValidator(1)], default=1) |
|
100 | 99 | spectral = models.CharField(verbose_name='Combinations', max_length=5000, default = '[0, 0],') |
|
101 | 100 | create_directory = models.BooleanField(verbose_name='Create Directory Per Day', default=True) |
|
102 | 101 | include_expname = models.BooleanField(verbose_name='Experiment Name in Directory', default=False) |
|
103 | 102 | #view_raw_data = models.BooleanField(verbose_name='View Raw Data', default=True) |
|
104 | 103 | save_ch_dc = models.BooleanField(verbose_name='Save Channels DC', default=True) |
|
105 | 104 | save_data = models.BooleanField(verbose_name='Save Data', default=True) |
|
106 |
filter_parms = models.CharField(max_length=10000, default='{ |
|
|
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}, "model": "jars.jarsfilter", "pk": 1}') | |
|
107 | 106 | |
|
108 | 107 | class Meta: |
|
109 | 108 | db_table = 'jars_configurations' |
|
110 | 109 | |
|
111 | def add_parms_to_filter(self): | |
|
112 | self.filter_parms = self.filter.parms_to_dict() | |
|
113 | self.save() | |
|
114 | ||
|
115 | 110 | def filter_resolution(self): |
|
116 | 111 | filter_parms = eval(self.filter_parms) |
|
117 | 112 | if filter_parms.__class__.__name__=='str': |
|
118 | 113 | filter_parms = eval(filter_parms) |
|
119 | 114 | |
|
120 | 115 | filter_clock = filter_parms['clock'] |
|
121 | 116 | filter_2 = filter_parms['filter_2'] |
|
122 | 117 | filter_5 = filter_parms['filter_5'] |
|
123 | 118 | filter_fir = filter_parms['filter_fir'] |
|
124 | 119 | |
|
125 | 120 | resolution = round((filter_clock/(filter_2*filter_5*filter_fir)),2) |
|
126 | 121 | return resolution |
|
127 | 122 | |
|
128 | 123 | def dict_to_parms(self, params, id=None): |
|
129 | 124 | |
|
130 | 125 | if id is not None: |
|
131 | 126 | data = Params(params).get_conf(id_conf=id) |
|
132 | 127 | else: |
|
133 | 128 | data = Params(params).get_conf(dtype='jars') |
|
134 | 129 | |
|
135 | 130 | self.name = data['name'] |
|
136 | 131 | self.exp_type = data['exp_type'] |
|
137 | 132 | #if data['exp_type'] in (1, '1') : |
|
138 | 133 | self.incohe_integr = data['incohe_integr'] |
|
139 | 134 | self.spectral_number = data['spectral_number'] |
|
140 | 135 | self.spectral = data['spectral'] |
|
141 | 136 | #self.pd_directory = data['pd_directory'] |
|
142 | 137 | self.cards_number = data['cards_number'] |
|
143 | 138 | self.channels_number = data['channels_number'] |
|
144 | 139 | self.channels = data['channels'] |
|
145 | 140 | #self.rd_directory = data['rd_directory'] |
|
146 | 141 | #self.raw_data_blocks = data['raw_data_blocks'] |
|
147 | 142 | self.data_type = data['data_type'] |
|
148 | 143 | self.cohe_integr_str = data['cohe_integr_str'] |
|
149 | 144 | self.acq_profiles = data['acq_profiles'] |
|
150 | 145 | self.profiles_block = data['profiles_block'] |
|
151 | 146 | self.ftp_interval = data['ftp_interval'] |
|
152 | 147 | self.fftpoints = data['fftpoints'] |
|
153 | 148 | self.cohe_integr = data['cohe_integr'] |
|
154 | 149 | self.filter_parms = json.dumps(data['filter_parms']) |
|
155 | 150 | self.create_directory = data['create_directory'] |
|
156 | 151 | self.include_expname = data['include_expname'] |
|
157 | 152 | #self.acq_link = data['acq_link'] |
|
158 | 153 | #self.view_raw_data = data['view_raw_data'] |
|
159 | 154 | self.save_ch_dc = data['save_ch_dc'] |
|
160 | 155 | self.save_data = data['save_data'] |
|
161 | 156 | self.save() |
|
162 | 157 | |
|
163 | 158 | def request(self, cmd, method='get', **kwargs): |
|
164 | 159 | |
|
165 | 160 | req = getattr(requests, method)(self.device.url(cmd), **kwargs) |
|
166 | 161 | payload = req.json() |
|
167 | 162 | return payload |
|
168 | 163 | |
|
169 | 164 | def status_device(self): |
|
170 | 165 | |
|
171 | 166 | try: |
|
172 | 167 | payload = self.request('status', |
|
173 | 168 | params={'name': self.experiment.name}) |
|
174 | 169 | self.device.status = payload['status'] |
|
175 | 170 | self.device.save() |
|
176 | 171 | self.message = payload['message'] |
|
177 | 172 | except Exception as e: |
|
178 | 173 | self.device.status = 0 |
|
179 | 174 | self.message = str(e) |
|
180 | 175 | self.device.save() |
|
181 | 176 | return False |
|
182 | 177 | |
|
183 | 178 | return True |
|
184 | 179 | |
|
185 | 180 | def stop_device(self): |
|
186 | 181 | |
|
187 | 182 | try: |
|
188 | 183 | payload = self.request('stop', 'post') |
|
189 | 184 | self.device.status = payload['status'] |
|
190 | 185 | self.device.save() |
|
191 | 186 | self.message = payload['message'] |
|
192 | 187 | except Exception as e: |
|
193 | 188 | self.device.status = 0 |
|
194 | 189 | self.message = str(e) |
|
195 | 190 | self.device.save() |
|
196 | 191 | return False |
|
197 | 192 | |
|
198 | 193 | return True |
|
199 | 194 | |
|
200 | 195 | def read_device(self): |
|
201 | 196 | |
|
202 | 197 | try: |
|
203 | 198 | payload = self.request('read', params={'name': self.experiment.name}) |
|
204 | 199 | self.message = 'Configuration loaded' |
|
205 | 200 | except: |
|
206 | 201 | self.device.status = 0 |
|
207 | 202 | self.device.save() |
|
208 | 203 | self.message = 'Could not read JARS configuration.' |
|
209 | 204 | return False |
|
210 | 205 | |
|
211 | 206 | return payload |
|
212 | 207 | |
|
213 | 208 | def write_device(self): |
|
214 | 209 | |
|
215 | 210 | if self.device.status == 3: |
|
216 | 211 | self.message = 'Could not configure device. Software Acquisition is running' |
|
217 | 212 | return False |
|
218 | 213 | |
|
219 | 214 | data = self.experiment.parms_to_dict() |
|
220 | 215 | |
|
221 | 216 | for key in data['configurations']['allIds']: |
|
222 | 217 | if data['configurations']['byId'][key]['device_type'] in ('dds', 'cgs'): |
|
223 | 218 | data['configurations']['allIds'].remove(key) |
|
224 | 219 | data['configurations']['byId'].pop(key) |
|
225 | 220 | elif data['configurations']['byId'][key]['device_type'] == 'jars': |
|
226 | 221 | data['configurations']['byId'][key] = self.parms_to_dict()['configurations']['byId'][str(self.pk)] |
|
227 | 222 | elif data['configurations']['byId'][key]['device_type'] == 'rc': |
|
228 | 223 | data['configurations']['byId'][key]['pulses'] = '' |
|
229 | 224 | data['configurations']['byId'][key]['delays'] = '' |
|
230 | 225 | rc_ids = [pk for pk in data['configurations']['allIds'] if data['configurations']['byId'][pk]['device_type']=='rc'] |
|
231 | 226 | mix_ids = [pk for pk in rc_ids if data['configurations']['byId'][pk]['mix']] |
|
232 | 227 | if mix_ids: |
|
233 | 228 | params = data['configurations']['byId'][mix_ids[0]]['parameters'] |
|
234 | 229 | rc = data['configurations']['byId'][params.split('-')[0].split('|')[0]] |
|
235 | 230 | rc['mix'] = True |
|
236 | 231 | data['configurations']['byId'][rc['id']] = rc |
|
237 | 232 | elif len(rc_ids)==0: |
|
238 | 233 | self.message = 'Missing RC configuration' |
|
239 | 234 | return False |
|
240 | 235 | |
|
241 | 236 | json_data = json.dumps(data) |
|
242 | 237 | print json_data |
|
243 | 238 | try: |
|
244 | 239 | payload = self.request('write', 'post', json=json_data) |
|
245 | 240 | self.device.status = payload['status'] |
|
246 | 241 | self.message = payload['message'] |
|
247 | 242 | self.device.save() |
|
248 | 243 | if self.device.status == 1: |
|
249 | 244 | return False |
|
250 | 245 | |
|
251 | 246 | except Exception as e: |
|
252 | 247 | self.device.status = 0 |
|
253 | 248 | self.message = str(e) |
|
254 | 249 | self.device.save() |
|
255 | 250 | return False |
|
256 | 251 | |
|
257 | 252 | return True |
|
258 | 253 | |
|
259 | 254 | def start_device(self): |
|
260 | 255 | |
|
261 | 256 | try: |
|
262 | 257 | payload = self.request('start', 'post', |
|
263 | 258 | json={'name': self.experiment.name}) |
|
264 | 259 | self.device.status = payload['status'] |
|
265 | 260 | self.message = payload['message'] |
|
266 | 261 | self.device.save() |
|
267 | 262 | if self.device.status == 1: |
|
268 | 263 | return False |
|
269 | 264 | |
|
270 | 265 | except Exception as e: |
|
271 | 266 | self.device.status = 0 |
|
272 | 267 | self.message = str(e) |
|
273 | 268 | self.device.save() |
|
274 | 269 | return False |
|
275 | 270 | |
|
276 | 271 | return True |
|
277 | 272 | |
|
278 | 273 | def update_from_file(self, filename): |
|
279 | 274 | |
|
280 | 275 | f = JARSFile(filename) |
|
281 | 276 | self.dict_to_parms(f.data) |
|
282 | 277 | self.save() |
|
283 | 278 | |
|
284 | 279 | def get_absolute_url_import(self): |
|
285 | 280 | return reverse('url_import_jars_conf', args=[str(self.id)]) |
|
286 | 281 | |
|
287 | 282 | def get_absolute_url_read(self): |
|
288 | 283 | return reverse('url_read_jars_conf', args=[str(self.id)]) |
@@ -1,209 +1,209 | |||
|
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 | 5 | |
|
6 | 6 | from apps.main.models import Device |
|
7 | 7 | from apps.main.views import sidebar |
|
8 | 8 | |
|
9 | 9 | from .models import JARSConfiguration, JARSfilter |
|
10 | 10 | from .forms import JARSConfigurationForm, JARSfilterForm, JARSImportForm |
|
11 | 11 | |
|
12 | 12 | import json |
|
13 | 13 | # Create your views here. |
|
14 | 14 | |
|
15 | 15 | def jars_conf(request, id_conf): |
|
16 | 16 | |
|
17 | 17 | conf = get_object_or_404(JARSConfiguration, pk=id_conf) |
|
18 | 18 | |
|
19 | 19 | filter_parms = eval(conf.filter_parms) |
|
20 | 20 | if filter_parms.__class__.__name__=='str': |
|
21 | 21 | filter_parms = eval(filter_parms) |
|
22 | 22 | |
|
23 | 23 | kwargs = {} |
|
24 | 24 | kwargs['filter'] = filter_parms |
|
25 | 25 | kwargs['filter_keys'] = ['clock', 'mult', 'fch', 'fch_decimal', |
|
26 | 26 | 'filter_fir', 'filter_2', 'filter_5'] |
|
27 | 27 | |
|
28 | 28 | filter_resolution=conf.filter_resolution() |
|
29 | 29 | kwargs['resolution'] = '{} (MHz)'.format(filter_resolution) |
|
30 | 30 | if filter_resolution < 1: |
|
31 | 31 | kwargs['resolution'] = '{} (kHz)'.format(filter_resolution*1000) |
|
32 | 32 | |
|
33 | 33 | kwargs['status'] = conf.device.get_status_display() |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | kwargs['dev_conf'] = conf |
|
37 | 37 | kwargs['dev_conf_keys'] = ['name', |
|
38 | 38 | 'cards_number', 'channels_number', 'channels', |
|
39 | 39 | #'rd_directory', 'pd_directory', |
|
40 | 40 | 'data_type', |
|
41 | 41 | 'acq_profiles', 'profiles_block', 'raw_data_blocks', 'ftp_interval', 'fftpoints', |
|
42 | 42 | 'cohe_integr_str', 'decode_data', |
|
43 | 43 | 'incohe_integr', 'cohe_integr', 'spectral_number', |
|
44 | 44 | 'spectral', 'create_directory', 'include_expname', |
|
45 | 45 | 'save_ch_dc', 'save_data'] |
|
46 | 46 | |
|
47 | 47 | kwargs['title'] = 'JARS Configuration' |
|
48 | 48 | kwargs['suptitle'] = 'Details' |
|
49 | 49 | |
|
50 | 50 | kwargs['button'] = 'Edit Configuration' |
|
51 | 51 | |
|
52 | 52 | #kwargs['no_play'] = True |
|
53 | 53 | |
|
54 | 54 | #kwargs['only_stop'] = True |
|
55 | 55 | |
|
56 | 56 | ###### SIDEBAR ###### |
|
57 | 57 | kwargs.update(sidebar(conf=conf)) |
|
58 | 58 | |
|
59 | 59 | return render(request, 'jars_conf.html', kwargs) |
|
60 | 60 | |
|
61 | 61 | def jars_conf_edit(request, id_conf): |
|
62 | 62 | |
|
63 | 63 | conf = get_object_or_404(JARSConfiguration, pk=id_conf) |
|
64 | 64 | |
|
65 | 65 | filter_parms = eval(conf.filter_parms) |
|
66 | 66 | if filter_parms.__class__.__name__=='str': |
|
67 | 67 | filter_parms = eval(filter_parms) |
|
68 | 68 | |
|
69 | 69 | if request.method=='GET': |
|
70 | 70 | form = JARSConfigurationForm(instance=conf) |
|
71 | 71 | filter_form = JARSfilterForm(initial=filter_parms) |
|
72 | 72 | |
|
73 | 73 | if request.method=='POST': |
|
74 | 74 | form = JARSConfigurationForm(request.POST, instance=conf) |
|
75 | 75 | filter_form = JARSfilterForm(request.POST) |
|
76 | 76 | |
|
77 | 77 | if filter_form.is_valid(): |
|
78 | 78 | jars_filter = filter_form.cleaned_data |
|
79 | 79 | try: |
|
80 | 80 | jars_filter.pop('name') |
|
81 | 81 | except: |
|
82 | 82 | pass |
|
83 | 83 | |
|
84 | 84 | if form.is_valid(): |
|
85 | 85 | conf = form.save(commit=False) |
|
86 | 86 | conf.filter_parms = json.dumps(jars_filter) |
|
87 | 87 | conf.save() |
|
88 | 88 | return redirect('url_jars_conf', id_conf=conf.id) |
|
89 | 89 | |
|
90 | 90 | kwargs = {} |
|
91 | 91 | |
|
92 | 92 | kwargs['id_dev'] = conf.id |
|
93 | 93 | kwargs['form'] = form |
|
94 | 94 | kwargs['filter_form'] = filter_form |
|
95 | 95 | kwargs['title'] = 'Device Configuration' |
|
96 | 96 | kwargs['suptitle'] = 'Edit' |
|
97 | 97 | kwargs['button'] = 'Save' |
|
98 | 98 | |
|
99 | 99 | return render(request, 'jars_conf_edit.html', kwargs) |
|
100 | 100 | |
|
101 | 101 | def import_file(request, conf_id): |
|
102 | 102 | |
|
103 | 103 | conf = get_object_or_404(JARSConfiguration, pk=conf_id) |
|
104 | 104 | if request.method=='POST': |
|
105 | 105 | form = JARSImportForm(request.POST, request.FILES) |
|
106 | 106 | if form.is_valid(): |
|
107 | 107 | #try: |
|
108 | 108 | if True: |
|
109 | 109 | data = conf.import_from_file(request.FILES['file_name']) |
|
110 | 110 | conf.dict_to_parms(data) |
|
111 | 111 | messages.success(request, 'Configuration "%s" loaded succesfully' % request.FILES['file_name']) |
|
112 | 112 | return redirect(conf.get_absolute_url_edit()) |
|
113 | 113 | |
|
114 | 114 | #except Exception as e: |
|
115 | 115 | # messages.error(request, 'Error parsing file: "%s" - %s' % (request.FILES['file_name'], e)) |
|
116 | 116 | |
|
117 | 117 | else: |
|
118 | 118 | messages.warning(request, 'Your current configuration will be replaced') |
|
119 | 119 | form = JARSImportForm() |
|
120 | 120 | |
|
121 | 121 | kwargs = {} |
|
122 | 122 | kwargs['form'] = form |
|
123 | 123 | kwargs['title'] = 'JARS Configuration' |
|
124 | 124 | kwargs['suptitle'] = 'Import file' |
|
125 | 125 | kwargs['button'] = 'Upload' |
|
126 | 126 | kwargs['previous'] = conf.get_absolute_url() |
|
127 | 127 | |
|
128 | 128 | return render(request, 'jars_import.html', kwargs) |
|
129 | 129 | |
|
130 | 130 | def read_conf(request, conf_id): |
|
131 | 131 | |
|
132 | 132 | conf = get_object_or_404(JARSConfiguration, pk=conf_id) |
|
133 | 133 | #filter = get_object_or_404(JARSfilter, pk=filter_id) |
|
134 | 134 | |
|
135 | 135 | if request.method=='GET': |
|
136 | 136 | |
|
137 | 137 | parms = conf.read_device() |
|
138 | 138 | conf.status_device() |
|
139 | 139 | |
|
140 | 140 | if not parms: |
|
141 | 141 | messages.error(request, conf.message) |
|
142 | 142 | return redirect(conf.get_absolute_url()) |
|
143 | 143 | |
|
144 | 144 | form = JARSConfigurationForm(initial=parms, instance=conf) |
|
145 | 145 | |
|
146 | 146 | if request.method=='POST': |
|
147 | 147 | form = JARSConfigurationForm(request.POST, instance=conf) |
|
148 | 148 | |
|
149 | 149 | if form.is_valid(): |
|
150 | 150 | form.save() |
|
151 | 151 | return redirect(conf.get_absolute_url()) |
|
152 | 152 | |
|
153 | 153 | messages.error(request, "Parameters could not be saved") |
|
154 | 154 | |
|
155 | 155 | kwargs = {} |
|
156 | 156 | kwargs['id_dev'] = conf.id |
|
157 | 157 | kwargs['filter_id'] = conf.filter.id |
|
158 | 158 | kwargs['form'] = form |
|
159 | 159 | kwargs['title'] = 'Device Configuration' |
|
160 | 160 | kwargs['suptitle'] = 'Parameters read from device' |
|
161 | 161 | kwargs['button'] = 'Save' |
|
162 | 162 | |
|
163 | 163 | ###### SIDEBAR ###### |
|
164 | 164 | kwargs.update(sidebar(conf=conf)) |
|
165 | 165 | |
|
166 | 166 | return render(request, 'jars_conf_edit.html', kwargs) |
|
167 | 167 | |
|
168 | 168 | |
|
169 | 169 | |
|
170 | 170 | def change_filter(request, conf_id, filter_id=None): |
|
171 | 171 | |
|
172 | 172 | conf = get_object_or_404(JARSConfiguration, pk=conf_id) |
|
173 | 173 | |
|
174 | 174 | if filter_id: |
|
175 | 175 | if filter_id.__class__.__name__ not in ['int', 'float']: |
|
176 | 176 | filter_id = eval(filter_id) |
|
177 | 177 | |
|
178 | 178 | if filter_id == 0: |
|
179 | 179 | return redirect('url_change_jars_filter', conf_id=conf.id) |
|
180 | 180 | |
|
181 | 181 | if request.method=='GET': |
|
182 | 182 | if not filter_id: |
|
183 |
form = JARSfilterForm(initial={' |
|
|
183 | form = JARSfilterForm(initial={'filter_id': 0}) | |
|
184 | 184 | else: |
|
185 |
form = JARSfilterForm(initial={' |
|
|
185 | form = JARSfilterForm(initial={'filter_id': filter_id}) | |
|
186 | 186 | |
|
187 | 187 | if request.method=='POST': |
|
188 | 188 | form = JARSfilterForm(request.POST) |
|
189 | 189 | if form.is_valid(): |
|
190 | 190 | jars_filter = form.cleaned_data |
|
191 | 191 | try: |
|
192 | 192 | jars_filter.pop('name') |
|
193 | 193 | except: |
|
194 | 194 | pass |
|
195 | 195 | conf.filter_parms = json.dumps(jars_filter) |
|
196 | 196 | conf.save() |
|
197 | 197 | return redirect('url_edit_jars_conf', id_conf=conf.id) |
|
198 | 198 | else: |
|
199 | 199 | messages.error(request, "Select a Filter Template") |
|
200 | 200 | return redirect('url_change_jars_filter', conf_id=conf.id) |
|
201 | 201 | |
|
202 | 202 | kwargs = {} |
|
203 | 203 | kwargs['title'] = 'JARS Configuration' |
|
204 | 204 | kwargs['suptitle'] = 'Change Filter' |
|
205 | 205 | kwargs['form'] = form |
|
206 | 206 | kwargs['button'] = 'Change' |
|
207 | 207 | kwargs['conf_id'] = conf.id |
|
208 | 208 | kwargs['filter_id'] = filter_id |
|
209 | 209 | return render(request, 'change_jars_filter.html', kwargs) |
General Comments 0
You need to be logged in to leave comments.
Login now