@@ -1,602 +1,602 | |||||
1 | from django.shortcuts import render, redirect, get_object_or_404, HttpResponse |
|
1 | from django.shortcuts import render, redirect, get_object_or_404, HttpResponse | |
2 | from datetime import datetime |
|
2 | from datetime import datetime | |
3 |
|
3 | |||
4 | from django.db import models |
|
4 | from django.db import models | |
5 | from polymorphic import PolymorphicModel |
|
5 | from polymorphic import PolymorphicModel | |
6 |
|
6 | |||
7 | from django.core.urlresolvers import reverse |
|
7 | from django.core.urlresolvers import reverse | |
8 |
|
8 | |||
9 |
|
9 | |||
10 | CONF_STATES = ( |
|
10 | CONF_STATES = ( | |
11 | (0, 'Disconnected'), |
|
11 | (0, 'Disconnected'), | |
12 | (1, 'Connected'), |
|
12 | (1, 'Connected'), | |
13 | (2, 'Running'), |
|
13 | (2, 'Running'), | |
14 | ) |
|
14 | ) | |
15 |
|
15 | |||
16 | EXP_STATES = ( |
|
16 | EXP_STATES = ( | |
17 | (0,'Error'), #RED |
|
17 | (0,'Error'), #RED | |
18 | (1,'Configured'), #BLUE |
|
18 | (1,'Configured'), #BLUE | |
19 | (2,'Running'), #GREEN |
|
19 | (2,'Running'), #GREEN | |
20 | (3,'Waiting'), #YELLOW |
|
20 | (3,'Waiting'), #YELLOW | |
21 | (4,'Not Configured'), #WHITE |
|
21 | (4,'Not Configured'), #WHITE | |
22 | ) |
|
22 | ) | |
23 |
|
23 | |||
24 | CONF_TYPES = ( |
|
24 | CONF_TYPES = ( | |
25 | (0, 'Active'), |
|
25 | (0, 'Active'), | |
26 | (1, 'Historical'), |
|
26 | (1, 'Historical'), | |
27 | ) |
|
27 | ) | |
28 |
|
28 | |||
29 | DEV_STATES = ( |
|
29 | DEV_STATES = ( | |
30 | (0, 'No connected'), |
|
30 | (0, 'No connected'), | |
31 | (1, 'Connected'), |
|
31 | (1, 'Connected'), | |
32 | (2, 'Configured'), |
|
32 | (2, 'Configured'), | |
33 | (3, 'Running'), |
|
33 | (3, 'Running'), | |
34 | ) |
|
34 | ) | |
35 |
|
35 | |||
36 | DEV_TYPES = ( |
|
36 | DEV_TYPES = ( | |
37 | ('', 'Select a device type'), |
|
37 | ('', 'Select a device type'), | |
38 | ('rc', 'Radar Controller'), |
|
38 | ('rc', 'Radar Controller'), | |
39 | ('rc_mix', 'Radar Controller (Mix)'), |
|
39 | ('rc_mix', 'Radar Controller (Mix)'), | |
40 | ('dds', 'Direct Digital Synthesizer'), |
|
40 | ('dds', 'Direct Digital Synthesizer'), | |
41 | ('jars', 'Jicamarca Radar Acquisition System'), |
|
41 | ('jars', 'Jicamarca Radar Acquisition System'), | |
42 | ('usrp', 'Universal Software Radio Peripheral'), |
|
42 | ('usrp', 'Universal Software Radio Peripheral'), | |
43 | ('cgs', 'Clock Generator System'), |
|
43 | ('cgs', 'Clock Generator System'), | |
44 | ('abs', 'Automatic Beam Switching'), |
|
44 | ('abs', 'Automatic Beam Switching'), | |
45 | ) |
|
45 | ) | |
46 |
|
46 | |||
47 | DEV_PORTS = { |
|
47 | DEV_PORTS = { | |
48 | 'rc' : 2000, |
|
48 | 'rc' : 2000, | |
49 | 'rc_mix': 2000, |
|
49 | 'rc_mix': 2000, | |
50 | 'dds' : 2000, |
|
50 | 'dds' : 2000, | |
51 | 'jars' : 2000, |
|
51 | 'jars' : 2000, | |
52 | 'usrp' : 2000, |
|
52 | 'usrp' : 2000, | |
53 | 'cgs' : 8080, |
|
53 | 'cgs' : 8080, | |
54 | 'abs' : 8080 |
|
54 | 'abs' : 8080 | |
55 | } |
|
55 | } | |
56 |
|
56 | |||
57 | RADAR_STATES = ( |
|
57 | RADAR_STATES = ( | |
58 | (0, 'No connected'), |
|
58 | (0, 'No connected'), | |
59 | (1, 'Connected'), |
|
59 | (1, 'Connected'), | |
60 | (2, 'Configured'), |
|
60 | (2, 'Configured'), | |
61 | (3, 'Running'), |
|
61 | (3, 'Running'), | |
62 | (4, 'Scheduled'), |
|
62 | (4, 'Scheduled'), | |
63 | ) |
|
63 | ) | |
64 | # Create your models here. |
|
64 | # Create your models here. | |
65 |
|
65 | |||
66 | class Location(models.Model): |
|
66 | class Location(models.Model): | |
67 |
|
67 | |||
68 | name = models.CharField(max_length = 30) |
|
68 | name = models.CharField(max_length = 30) | |
69 | description = models.TextField(blank=True, null=True) |
|
69 | description = models.TextField(blank=True, null=True) | |
70 |
|
70 | |||
71 | class Meta: |
|
71 | class Meta: | |
72 | db_table = 'db_location' |
|
72 | db_table = 'db_location' | |
73 |
|
73 | |||
74 | def __unicode__(self): |
|
74 | def __unicode__(self): | |
75 | return u'%s' % self.name |
|
75 | return u'%s' % self.name | |
76 |
|
76 | |||
77 | def get_absolute_url(self): |
|
77 | def get_absolute_url(self): | |
78 | return reverse('url_location', args=[str(self.id)]) |
|
78 | return reverse('url_location', args=[str(self.id)]) | |
79 |
|
79 | |||
80 |
|
80 | |||
81 | class DeviceType(models.Model): |
|
81 | class DeviceType(models.Model): | |
82 |
|
82 | |||
83 | name = models.CharField(max_length = 10, choices = DEV_TYPES, default = 'rc') |
|
83 | name = models.CharField(max_length = 10, choices = DEV_TYPES, default = 'rc') | |
84 | description = models.TextField(blank=True, null=True) |
|
84 | description = models.TextField(blank=True, null=True) | |
85 |
|
85 | |||
86 | class Meta: |
|
86 | class Meta: | |
87 | db_table = 'db_device_types' |
|
87 | db_table = 'db_device_types' | |
88 |
|
88 | |||
89 | def __unicode__(self): |
|
89 | def __unicode__(self): | |
90 | return u'%s' % self.get_name_display() |
|
90 | return u'%s' % self.get_name_display() | |
91 |
|
91 | |||
92 | class Device(models.Model): |
|
92 | class Device(models.Model): | |
93 |
|
93 | |||
94 | device_type = models.ForeignKey(DeviceType, on_delete=models.CASCADE) |
|
94 | device_type = models.ForeignKey(DeviceType, on_delete=models.CASCADE) | |
95 | location = models.ForeignKey(Location, on_delete=models.CASCADE) |
|
95 | location = models.ForeignKey(Location, on_delete=models.CASCADE) | |
96 |
|
96 | |||
97 | name = models.CharField(max_length=40, default='') |
|
97 | name = models.CharField(max_length=40, default='') | |
98 | ip_address = models.GenericIPAddressField(protocol='IPv4', default='0.0.0.0') |
|
98 | ip_address = models.GenericIPAddressField(protocol='IPv4', default='0.0.0.0') | |
99 | port_address = models.PositiveSmallIntegerField(default=2000) |
|
99 | port_address = models.PositiveSmallIntegerField(default=2000) | |
100 | description = models.TextField(blank=True, null=True) |
|
100 | description = models.TextField(blank=True, null=True) | |
101 | status = models.PositiveSmallIntegerField(default=0, choices=DEV_STATES) |
|
101 | status = models.PositiveSmallIntegerField(default=0, choices=DEV_STATES) | |
102 |
|
102 | |||
103 | class Meta: |
|
103 | class Meta: | |
104 | db_table = 'db_devices' |
|
104 | db_table = 'db_devices' | |
105 |
|
105 | |||
106 | def __unicode__(self): |
|
106 | def __unicode__(self): | |
107 | return u'[{}]: {}'.format(self.device_type.name.upper(), |
|
107 | return u'[{}]: {}'.format(self.device_type.name.upper(), | |
108 | self.name) |
|
108 | self.name) | |
109 |
|
109 | |||
110 | def get_status(self): |
|
110 | def get_status(self): | |
111 | return self.status |
|
111 | return self.status | |
112 |
|
112 | |||
113 | @property |
|
113 | @property | |
114 | def status_color(self): |
|
114 | def status_color(self): | |
115 | color = 'muted' |
|
115 | color = 'muted' | |
116 | if self.status == 0: |
|
116 | if self.status == 0: | |
117 | color = "danger" |
|
117 | color = "danger" | |
118 | elif self.status == 1: |
|
118 | elif self.status == 1: | |
119 | color = "warning" |
|
119 | color = "warning" | |
120 | elif self.status == 2: |
|
120 | elif self.status == 2: | |
121 | color = "info" |
|
121 | color = "info" | |
122 | elif self.status == 3: |
|
122 | elif self.status == 3: | |
123 | color = "success" |
|
123 | color = "success" | |
124 |
|
124 | |||
125 | return color |
|
125 | return color | |
126 |
|
126 | |||
127 | def get_absolute_url(self): |
|
127 | def get_absolute_url(self): | |
128 | return reverse('url_device', args=[str(self.id)]) |
|
128 | return reverse('url_device', args=[str(self.id)]) | |
129 |
|
129 | |||
130 |
|
130 | |||
131 | class Campaign(models.Model): |
|
131 | class Campaign(models.Model): | |
132 |
|
132 | |||
133 | template = models.BooleanField(default=False) |
|
133 | template = models.BooleanField(default=False) | |
134 | name = models.CharField(max_length=60, unique=True) |
|
134 | name = models.CharField(max_length=60, unique=True) | |
135 | start_date = models.DateTimeField(blank=True, null=True) |
|
135 | start_date = models.DateTimeField(blank=True, null=True) | |
136 | end_date = models.DateTimeField(blank=True, null=True) |
|
136 | end_date = models.DateTimeField(blank=True, null=True) | |
137 | tags = models.CharField(max_length=40) |
|
137 | tags = models.CharField(max_length=40) | |
138 | description = models.TextField(blank=True, null=True) |
|
138 | description = models.TextField(blank=True, null=True) | |
139 | experiments = models.ManyToManyField('Experiment', blank=True) |
|
139 | experiments = models.ManyToManyField('Experiment', blank=True) | |
140 |
|
140 | |||
141 | class Meta: |
|
141 | class Meta: | |
142 | db_table = 'db_campaigns' |
|
142 | db_table = 'db_campaigns' | |
143 | ordering = ('name',) |
|
143 | ordering = ('name',) | |
144 |
|
144 | |||
145 | def __unicode__(self): |
|
145 | def __unicode__(self): | |
146 | if self.template: |
|
146 | if self.template: | |
147 | return u'{} (template)'.format(self.name) |
|
147 | return u'{} (template)'.format(self.name) | |
148 | else: |
|
148 | else: | |
149 | return u'{}'.format(self.name) |
|
149 | return u'{}'.format(self.name) | |
150 |
|
150 | |||
151 |
|
151 | |||
152 | def parms_to_dict(self): |
|
152 | def parms_to_dict(self): | |
153 |
|
153 | |||
154 | import json |
|
154 | import json | |
155 |
|
155 | |||
156 | parameters = {} |
|
156 | parameters = {} | |
157 | exp_parameters = {} |
|
157 | exp_parameters = {} | |
158 | experiments = Experiment.objects.filter(campaign = self) |
|
158 | experiments = Experiment.objects.filter(campaign = self) | |
159 |
|
159 | |||
160 | i=1 |
|
160 | i=1 | |
161 | for experiment in experiments: |
|
161 | for experiment in experiments: | |
162 | exp_parameters['experiment-'+str(i)] = json.loads(experiment.parms_to_dict()) |
|
162 | exp_parameters['experiment-'+str(i)] = json.loads(experiment.parms_to_dict()) | |
163 | i += 1 |
|
163 | i += 1 | |
164 |
|
164 | |||
165 |
|
165 | |||
166 | parameters['experiments'] = exp_parameters |
|
166 | parameters['experiments'] = exp_parameters | |
167 | parameters['end_date'] = self.end_date.strftime("%Y-%m-%d") |
|
167 | parameters['end_date'] = self.end_date.strftime("%Y-%m-%d") | |
168 | parameters['start_date'] = self.start_date.strftime("%Y-%m-%d") |
|
168 | parameters['start_date'] = self.start_date.strftime("%Y-%m-%d") | |
169 | parameters['campaign'] = self.__unicode__() |
|
169 | parameters['campaign'] = self.__unicode__() | |
170 | parameters['tags'] =self.tags |
|
170 | parameters['tags'] =self.tags | |
171 |
|
171 | |||
172 | parameters = json.dumps(parameters, indent=2, sort_keys=False) |
|
172 | parameters = json.dumps(parameters, indent=2, sort_keys=False) | |
173 |
|
173 | |||
174 | return parameters |
|
174 | return parameters | |
175 |
|
175 | |||
176 | def import_from_file(self, fp): |
|
176 | def import_from_file(self, fp): | |
177 |
|
177 | |||
178 | import os, json |
|
178 | import os, json | |
179 |
|
179 | |||
180 | parms = {} |
|
180 | parms = {} | |
181 |
|
181 | |||
182 | path, ext = os.path.splitext(fp.name) |
|
182 | path, ext = os.path.splitext(fp.name) | |
183 |
|
183 | |||
184 | if ext == '.json': |
|
184 | if ext == '.json': | |
185 | parms = json.load(fp) |
|
185 | parms = json.load(fp) | |
186 |
|
186 | |||
187 | return parms |
|
187 | return parms | |
188 |
|
188 | |||
189 | def dict_to_parms(self, parms, CONF_MODELS): |
|
189 | def dict_to_parms(self, parms, CONF_MODELS): | |
190 |
|
190 | |||
191 | experiments = Experiment.objects.filter(campaign = self) |
|
191 | experiments = Experiment.objects.filter(campaign = self) | |
192 | configurations = Configuration.objects.filter(experiment = experiments) |
|
192 | configurations = Configuration.objects.filter(experiment = experiments) | |
193 |
|
193 | |||
194 | if configurations: |
|
194 | if configurations: | |
195 | for configuration in configurations: |
|
195 | for configuration in configurations: | |
196 | configuration.delete() |
|
196 | configuration.delete() | |
197 |
|
197 | |||
198 | if experiments: |
|
198 | if experiments: | |
199 | for experiment in experiments: |
|
199 | for experiment in experiments: | |
200 | experiment.delete() |
|
200 | experiment.delete() | |
201 |
|
201 | |||
202 | for parms_exp in parms['experiments']: |
|
202 | for parms_exp in parms['experiments']: | |
203 | location = Location.objects.get(name = parms['experiments'][parms_exp]['radar']) |
|
203 | location = Location.objects.get(name = parms['experiments'][parms_exp]['radar']) | |
204 | new_exp = Experiment( |
|
204 | new_exp = Experiment( | |
205 | name = parms['experiments'][parms_exp]['experiment'], |
|
205 | name = parms['experiments'][parms_exp]['experiment'], | |
206 | location = location, |
|
206 | location = location, | |
207 | start_time = parms['experiments'][parms_exp]['start_time'], |
|
207 | start_time = parms['experiments'][parms_exp]['start_time'], | |
208 | end_time = parms['experiments'][parms_exp]['end_time'], |
|
208 | end_time = parms['experiments'][parms_exp]['end_time'], | |
209 | ) |
|
209 | ) | |
210 | new_exp.save() |
|
210 | new_exp.save() | |
211 | new_exp.dict_to_parms(parms['experiments'][parms_exp],CONF_MODELS) |
|
211 | new_exp.dict_to_parms(parms['experiments'][parms_exp],CONF_MODELS) | |
212 | new_exp.save() |
|
212 | new_exp.save() | |
213 |
|
213 | |||
214 | self.name = parms['campaign'] |
|
214 | self.name = parms['campaign'] | |
215 | self.start_date = parms['start_date'] |
|
215 | self.start_date = parms['start_date'] | |
216 | self.end_date = parms['end_date'] |
|
216 | self.end_date = parms['end_date'] | |
217 | self.tags = parms['tags'] |
|
217 | self.tags = parms['tags'] | |
218 | self.experiments.add(new_exp) |
|
218 | self.experiments.add(new_exp) | |
219 | self.save() |
|
219 | self.save() | |
220 |
|
220 | |||
221 | return self |
|
221 | return self | |
222 |
|
222 | |||
223 | def get_absolute_url(self): |
|
223 | def get_absolute_url(self): | |
224 | return reverse('url_campaign', args=[str(self.id)]) |
|
224 | return reverse('url_campaign', args=[str(self.id)]) | |
225 |
|
225 | |||
226 | def get_absolute_url_edit(self): |
|
226 | def get_absolute_url_edit(self): | |
227 | return reverse('url_edit_campaign', args=[str(self.id)]) |
|
227 | return reverse('url_edit_campaign', args=[str(self.id)]) | |
228 |
|
228 | |||
229 | def get_absolute_url_export(self): |
|
229 | def get_absolute_url_export(self): | |
230 | return reverse('url_export_campaign', args=[str(self.id)]) |
|
230 | return reverse('url_export_campaign', args=[str(self.id)]) | |
231 |
|
231 | |||
232 | def get_absolute_url_import(self): |
|
232 | def get_absolute_url_import(self): | |
233 | return reverse('url_import_campaign', args=[str(self.id)]) |
|
233 | return reverse('url_import_campaign', args=[str(self.id)]) | |
234 |
|
234 | |||
235 |
|
235 | |||
236 |
|
236 | |||
237 | class RunningExperiment(models.Model): |
|
237 | class RunningExperiment(models.Model): | |
238 | radar = models.OneToOneField('Location', on_delete=models.CASCADE) |
|
238 | radar = models.OneToOneField('Location', on_delete=models.CASCADE) | |
239 | running_experiment = models.ManyToManyField('Experiment', blank = True) |
|
239 | running_experiment = models.ManyToManyField('Experiment', blank = True) | |
240 | status = models.PositiveSmallIntegerField(default=0, choices=RADAR_STATES) |
|
240 | status = models.PositiveSmallIntegerField(default=0, choices=RADAR_STATES) | |
241 |
|
241 | |||
242 |
|
242 | |||
243 | class Experiment(models.Model): |
|
243 | class Experiment(models.Model): | |
244 |
|
244 | |||
245 | template = models.BooleanField(default=False) |
|
245 | template = models.BooleanField(default=False) | |
246 | name = models.CharField(max_length=40, default='', unique=True) |
|
246 | name = models.CharField(max_length=40, default='', unique=True) | |
247 | location = models.ForeignKey('Location', null=True, blank=True, on_delete=models.CASCADE) |
|
247 | location = models.ForeignKey('Location', null=True, blank=True, on_delete=models.CASCADE) | |
248 | start_time = models.TimeField(default='00:00:00') |
|
248 | start_time = models.TimeField(default='00:00:00') | |
249 | end_time = models.TimeField(default='23:59:59') |
|
249 | end_time = models.TimeField(default='23:59:59') | |
250 | status = models.PositiveSmallIntegerField(default=0, choices=EXP_STATES) |
|
250 | status = models.PositiveSmallIntegerField(default=0, choices=EXP_STATES) | |
251 |
|
251 | |||
252 | class Meta: |
|
252 | class Meta: | |
253 | db_table = 'db_experiments' |
|
253 | db_table = 'db_experiments' | |
254 | ordering = ('template', 'name') |
|
254 | ordering = ('template', 'name') | |
255 |
|
255 | |||
256 | def __unicode__(self): |
|
256 | def __unicode__(self): | |
257 | if self.template: |
|
257 | if self.template: | |
258 | return u'%s (template)' % (self.name) |
|
258 | return u'%s (template)' % (self.name) | |
259 | else: |
|
259 | else: | |
260 | return u'%s' % (self.name) |
|
260 | return u'%s' % (self.name) | |
261 |
|
261 | |||
262 | @property |
|
262 | @property | |
263 | def radar_system(self): |
|
263 | def radar_system(self): | |
264 | return self.location |
|
264 | return self.location | |
265 |
|
265 | |||
266 | def clone(self, **kwargs): |
|
266 | def clone(self, **kwargs): | |
267 |
|
267 | |||
268 | confs = Configuration.objects.filter(experiment=self, type=0) |
|
268 | confs = Configuration.objects.filter(experiment=self, type=0) | |
269 | self.pk = None |
|
269 | self.pk = None | |
270 | self.name = '{} [{:%Y/%m/%d}]'.format(self.name, datetime.now()) |
|
270 | self.name = '{} [{:%Y/%m/%d}]'.format(self.name, datetime.now()) | |
271 | for attr, value in kwargs.items(): |
|
271 | for attr, value in kwargs.items(): | |
272 | setattr(self, attr, value) |
|
272 | setattr(self, attr, value) | |
273 |
|
273 | |||
274 | self.save() |
|
274 | self.save() | |
275 |
|
275 | |||
276 | for conf in confs: |
|
276 | for conf in confs: | |
277 | conf.clone(experiment=self, template=False) |
|
277 | conf.clone(experiment=self, template=False) | |
278 |
|
278 | |||
279 | return self |
|
279 | return self | |
280 |
|
280 | |||
281 | def get_status(self): |
|
281 | def get_status(self): | |
282 | configurations = Configuration.objects.filter(experiment=self) |
|
282 | configurations = Configuration.objects.filter(experiment=self) | |
283 | exp_status=[] |
|
283 | exp_status=[] | |
284 | for conf in configurations: |
|
284 | for conf in configurations: | |
285 | print conf.status_device() |
|
285 | print conf.status_device() | |
286 | exp_status.append(conf.status_device()) |
|
286 | exp_status.append(conf.status_device()) | |
287 |
|
287 | |||
288 | if not exp_status: #No Configuration |
|
288 | if not exp_status: #No Configuration | |
289 | self.status = 4 |
|
289 | self.status = 4 | |
290 | self.save() |
|
290 | self.save() | |
291 | return |
|
291 | return | |
292 |
|
292 | |||
293 | total = 1 |
|
293 | total = 1 | |
294 | for e_s in exp_status: |
|
294 | for e_s in exp_status: | |
295 | total = total*e_s |
|
295 | total = total*e_s | |
296 |
|
296 | |||
297 | if total == 0: #Error |
|
297 | if total == 0: #Error | |
298 | status = 0 |
|
298 | status = 0 | |
299 | elif total == (3**len(exp_status)): #Running |
|
299 | elif total == (3**len(exp_status)): #Running | |
300 | status = 2 |
|
300 | status = 2 | |
301 | else: |
|
301 | else: | |
302 | status = 1 #Configurated |
|
302 | status = 1 #Configurated | |
303 |
|
303 | |||
304 | self.status = status |
|
304 | self.status = status | |
305 | self.save() |
|
305 | self.save() | |
306 |
|
306 | |||
307 | def status_color(self): |
|
307 | def status_color(self): | |
308 | color = 'muted' |
|
308 | color = 'muted' | |
309 | if self.status == 0: |
|
309 | if self.status == 0: | |
310 | color = "danger" |
|
310 | color = "danger" | |
311 | elif self.status == 1: |
|
311 | elif self.status == 1: | |
312 | color = "info" |
|
312 | color = "info" | |
313 | elif self.status == 2: |
|
313 | elif self.status == 2: | |
314 | color = "success" |
|
314 | color = "success" | |
315 | elif self.status == 3: |
|
315 | elif self.status == 3: | |
316 | color = "warning" |
|
316 | color = "warning" | |
317 |
|
317 | |||
318 | return color |
|
318 | return color | |
319 |
|
319 | |||
320 | def get_absolute_url(self): |
|
320 | def get_absolute_url(self): | |
321 | return reverse('url_experiment', args=[str(self.id)]) |
|
321 | return reverse('url_experiment', args=[str(self.id)]) | |
322 |
|
322 | |||
323 | def parms_to_dict(self): |
|
323 | def parms_to_dict(self): | |
324 |
|
324 | |||
325 | import json |
|
325 | import json | |
326 |
|
326 | |||
327 | configurations = Configuration.objects.filter(experiment=self) |
|
327 | configurations = Configuration.objects.filter(experiment=self) | |
328 | conf_parameters = {} |
|
328 | conf_parameters = {} | |
329 | parameters={} |
|
329 | parameters={} | |
330 |
|
330 | |||
331 | for configuration in configurations: |
|
331 | for configuration in configurations: | |
332 | if 'cgs' in configuration.device.device_type.name: |
|
332 | if 'cgs' in configuration.device.device_type.name: | |
333 | conf_parameters['cgs'] = configuration.parms_to_dict() |
|
333 | conf_parameters['cgs'] = configuration.parms_to_dict() | |
334 | if 'dds' in configuration.device.device_type.name: |
|
334 | if 'dds' in configuration.device.device_type.name: | |
335 | conf_parameters['dds'] = configuration.parms_to_dict() |
|
335 | conf_parameters['dds'] = configuration.parms_to_dict() | |
336 | if 'rc' in configuration.device.device_type.name: |
|
336 | if 'rc' in configuration.device.device_type.name: | |
337 | conf_parameters['rc'] = configuration.parms_to_dict() |
|
337 | conf_parameters['rc'] = configuration.parms_to_dict() | |
338 | if 'jars' in configuration.device.device_type.name: |
|
338 | if 'jars' in configuration.device.device_type.name: | |
339 | conf_parameters['jars'] = configuration.parms_to_dict() |
|
339 | conf_parameters['jars'] = configuration.parms_to_dict() | |
340 | if 'usrp' in configuration.device.device_type.name: |
|
340 | if 'usrp' in configuration.device.device_type.name: | |
341 | conf_parameters['usrp'] = configuration.parms_to_dict() |
|
341 | conf_parameters['usrp'] = configuration.parms_to_dict() | |
342 | if 'abs' in configuration.device.device_type.name: |
|
342 | if 'abs' in configuration.device.device_type.name: | |
343 | conf_parameters['abs'] = configuration.parms_to_dict() |
|
343 | conf_parameters['abs'] = configuration.parms_to_dict() | |
344 |
|
344 | |||
345 | parameters['configurations'] = conf_parameters |
|
345 | parameters['configurations'] = conf_parameters | |
346 | parameters['end_time'] = self.end_time.strftime("%H:%M:%S") |
|
346 | parameters['end_time'] = self.end_time.strftime("%H:%M:%S") | |
347 | parameters['start_time'] = self.start_time.strftime("%H:%M:%S") |
|
347 | parameters['start_time'] = self.start_time.strftime("%H:%M:%S") | |
348 | parameters['radar'] = self.radar.name |
|
348 | parameters['radar'] = self.radar_system.name | |
349 | parameters['experiment'] = self.name |
|
349 | parameters['experiment'] = self.name | |
350 | parameters = json.dumps(parameters, indent=2) |
|
350 | parameters = json.dumps(parameters, indent=2) | |
351 |
|
351 | |||
352 | return parameters |
|
352 | return parameters | |
353 |
|
353 | |||
354 | def import_from_file(self, fp): |
|
354 | def import_from_file(self, fp): | |
355 |
|
355 | |||
356 | import os, json |
|
356 | import os, json | |
357 |
|
357 | |||
358 | parms = {} |
|
358 | parms = {} | |
359 |
|
359 | |||
360 | path, ext = os.path.splitext(fp.name) |
|
360 | path, ext = os.path.splitext(fp.name) | |
361 |
|
361 | |||
362 | if ext == '.json': |
|
362 | if ext == '.json': | |
363 | parms = json.load(fp) |
|
363 | parms = json.load(fp) | |
364 |
|
364 | |||
365 | return parms |
|
365 | return parms | |
366 |
|
366 | |||
367 | def dict_to_parms(self, parms, CONF_MODELS): |
|
367 | def dict_to_parms(self, parms, CONF_MODELS): | |
368 |
|
368 | |||
369 | configurations = Configuration.objects.filter(experiment=self) |
|
369 | configurations = Configuration.objects.filter(experiment=self) | |
370 |
|
370 | |||
371 | if configurations: |
|
371 | if configurations: | |
372 | for configuration in configurations: |
|
372 | for configuration in configurations: | |
373 | configuration.delete() |
|
373 | configuration.delete() | |
374 |
|
374 | |||
375 | for conf_type in parms['configurations']: |
|
375 | for conf_type in parms['configurations']: | |
376 | #--For ABS Device: |
|
376 | #--For ABS Device: | |
377 | #--For USRP Device: |
|
377 | #--For USRP Device: | |
378 | #--For JARS Device: |
|
378 | #--For JARS Device: | |
379 | #--For RC Device: |
|
379 | #--For RC Device: | |
380 | if conf_type == 'rc': |
|
380 | if conf_type == 'rc': | |
381 | device = get_object_or_404(Device, pk=parms['configurations']['rc']['device_id']) |
|
381 | device = get_object_or_404(Device, pk=parms['configurations']['rc']['device_id']) | |
382 | DevConfModel = CONF_MODELS[conf_type] |
|
382 | DevConfModel = CONF_MODELS[conf_type] | |
383 | confrc_form = DevConfModel( |
|
383 | confrc_form = DevConfModel( | |
384 | experiment = self, |
|
384 | experiment = self, | |
385 | name = 'RC', |
|
385 | name = 'RC', | |
386 | device=device, |
|
386 | device=device, | |
387 | ) |
|
387 | ) | |
388 | confrc_form.dict_to_parms(parms['configurations']['rc']) |
|
388 | confrc_form.dict_to_parms(parms['configurations']['rc']) | |
389 | confrc_form.save() |
|
389 | confrc_form.save() | |
390 | #--For DDS Device: |
|
390 | #--For DDS Device: | |
391 | if conf_type == 'dds': |
|
391 | if conf_type == 'dds': | |
392 | device = get_object_or_404(Device, pk=parms['configurations']['dds']['device_id']) |
|
392 | device = get_object_or_404(Device, pk=parms['configurations']['dds']['device_id']) | |
393 | DevConfModel = CONF_MODELS[conf_type] |
|
393 | DevConfModel = CONF_MODELS[conf_type] | |
394 | confdds_form = DevConfModel( |
|
394 | confdds_form = DevConfModel( | |
395 | experiment = self, |
|
395 | experiment = self, | |
396 | name = 'DDS', |
|
396 | name = 'DDS', | |
397 | device=device, |
|
397 | device=device, | |
398 | ) |
|
398 | ) | |
399 | confdds_form.dict_to_parms(parms['configurations']['dds']) |
|
399 | confdds_form.dict_to_parms(parms['configurations']['dds']) | |
400 | confdds_form.save() |
|
400 | confdds_form.save() | |
401 | #--For CGS Device: |
|
401 | #--For CGS Device: | |
402 | if conf_type == 'cgs': |
|
402 | if conf_type == 'cgs': | |
403 | device = get_object_or_404(Device, pk=parms['configurations']['cgs']['device_id']) |
|
403 | device = get_object_or_404(Device, pk=parms['configurations']['cgs']['device_id']) | |
404 | DevConfModel = CONF_MODELS[conf_type] |
|
404 | DevConfModel = CONF_MODELS[conf_type] | |
405 | confcgs_form = DevConfModel( |
|
405 | confcgs_form = DevConfModel( | |
406 | experiment = self, |
|
406 | experiment = self, | |
407 | name = 'CGS', |
|
407 | name = 'CGS', | |
408 | device=device, |
|
408 | device=device, | |
409 | ) |
|
409 | ) | |
410 | confcgs_form.dict_to_parms(parms['configurations']['cgs']) |
|
410 | confcgs_form.dict_to_parms(parms['configurations']['cgs']) | |
411 | confcgs_form.save() |
|
411 | confcgs_form.save() | |
412 |
|
412 | |||
413 | location = Location.objects.get(name = parms['radar']) |
|
413 | location = Location.objects.get(name = parms['radar']) | |
414 | self.name = parms['experiment'] |
|
414 | self.name = parms['experiment'] | |
415 | self.location = location |
|
415 | self.location = location | |
416 | self.start_time = parms['start_time'] |
|
416 | self.start_time = parms['start_time'] | |
417 | self.end_time = parms['end_time'] |
|
417 | self.end_time = parms['end_time'] | |
418 | self.save() |
|
418 | self.save() | |
419 |
|
419 | |||
420 | return self |
|
420 | return self | |
421 |
|
421 | |||
422 | def get_absolute_url_edit(self): |
|
422 | def get_absolute_url_edit(self): | |
423 | return reverse('url_edit_experiment', args=[str(self.id)]) |
|
423 | return reverse('url_edit_experiment', args=[str(self.id)]) | |
424 |
|
424 | |||
425 | def get_absolute_url_import(self): |
|
425 | def get_absolute_url_import(self): | |
426 | return reverse('url_import_experiment', args=[str(self.id)]) |
|
426 | return reverse('url_import_experiment', args=[str(self.id)]) | |
427 |
|
427 | |||
428 | def get_absolute_url_export(self): |
|
428 | def get_absolute_url_export(self): | |
429 | return reverse('url_export_experiment', args=[str(self.id)]) |
|
429 | return reverse('url_export_experiment', args=[str(self.id)]) | |
430 |
|
430 | |||
431 |
|
431 | |||
432 | class Configuration(PolymorphicModel): |
|
432 | class Configuration(PolymorphicModel): | |
433 |
|
433 | |||
434 | template = models.BooleanField(default=False) |
|
434 | template = models.BooleanField(default=False) | |
435 |
|
435 | |||
436 | name = models.CharField(verbose_name="Configuration Name", max_length=40, default='') |
|
436 | name = models.CharField(verbose_name="Configuration Name", max_length=40, default='') | |
437 |
|
437 | |||
438 | experiment = models.ForeignKey('Experiment', verbose_name='Experiment', null=True, blank=True, on_delete=models.CASCADE) |
|
438 | experiment = models.ForeignKey('Experiment', verbose_name='Experiment', null=True, blank=True, on_delete=models.CASCADE) | |
439 | device = models.ForeignKey('Device', verbose_name='Device', null=True, on_delete=models.CASCADE) |
|
439 | device = models.ForeignKey('Device', verbose_name='Device', null=True, on_delete=models.CASCADE) | |
440 |
|
440 | |||
441 | type = models.PositiveSmallIntegerField(default=0, choices=CONF_TYPES) |
|
441 | type = models.PositiveSmallIntegerField(default=0, choices=CONF_TYPES) | |
442 |
|
442 | |||
443 | created_date = models.DateTimeField(auto_now_add=True) |
|
443 | created_date = models.DateTimeField(auto_now_add=True) | |
444 | programmed_date = models.DateTimeField(auto_now=True) |
|
444 | programmed_date = models.DateTimeField(auto_now=True) | |
445 |
|
445 | |||
446 | parameters = models.TextField(default='{}') |
|
446 | parameters = models.TextField(default='{}') | |
447 |
|
447 | |||
448 | message = "" |
|
448 | message = "" | |
449 |
|
449 | |||
450 | class Meta: |
|
450 | class Meta: | |
451 | db_table = 'db_configurations' |
|
451 | db_table = 'db_configurations' | |
452 |
|
452 | |||
453 | def __unicode__(self): |
|
453 | def __unicode__(self): | |
454 |
|
454 | |||
455 | device = '{}:'.format(self.device.device_type.name.upper()) |
|
455 | device = '{}:'.format(self.device.device_type.name.upper()) | |
456 |
|
456 | |||
457 | if 'mix' in self._meta.get_all_field_names(): |
|
457 | if 'mix' in self._meta.get_all_field_names(): | |
458 | if self.mix: |
|
458 | if self.mix: | |
459 | device = '{} MIXED:'.format(self.device.device_type.name.upper()) |
|
459 | device = '{} MIXED:'.format(self.device.device_type.name.upper()) | |
460 |
|
460 | |||
461 | if self.template: |
|
461 | if self.template: | |
462 | return u'{} {} (template)'.format(device, self.name) |
|
462 | return u'{} {} (template)'.format(device, self.name) | |
463 | else: |
|
463 | else: | |
464 | return u'{} {}'.format(device, self.name) |
|
464 | return u'{} {}'.format(device, self.name) | |
465 |
|
465 | |||
466 | def clone(self, **kwargs): |
|
466 | def clone(self, **kwargs): | |
467 |
|
467 | |||
468 | self.pk = None |
|
468 | self.pk = None | |
469 | self.id = None |
|
469 | self.id = None | |
470 | for attr, value in kwargs.items(): |
|
470 | for attr, value in kwargs.items(): | |
471 | setattr(self, attr, value) |
|
471 | setattr(self, attr, value) | |
472 |
|
472 | |||
473 | self.save() |
|
473 | self.save() | |
474 |
|
474 | |||
475 | return self |
|
475 | return self | |
476 |
|
476 | |||
477 | def parms_to_dict(self): |
|
477 | def parms_to_dict(self): | |
478 |
|
478 | |||
479 | parameters = {} |
|
479 | parameters = {} | |
480 |
|
480 | |||
481 | for key in self.__dict__.keys(): |
|
481 | for key in self.__dict__.keys(): | |
482 | parameters[key] = getattr(self, key) |
|
482 | parameters[key] = getattr(self, key) | |
483 |
|
483 | |||
484 | return parameters |
|
484 | return parameters | |
485 |
|
485 | |||
486 | def parms_to_text(self): |
|
486 | def parms_to_text(self): | |
487 |
|
487 | |||
488 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
488 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() | |
489 |
|
489 | |||
490 | return '' |
|
490 | return '' | |
491 |
|
491 | |||
492 | def parms_to_binary(self): |
|
492 | def parms_to_binary(self): | |
493 |
|
493 | |||
494 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
494 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() | |
495 |
|
495 | |||
496 | return '' |
|
496 | return '' | |
497 |
|
497 | |||
498 | def dict_to_parms(self, parameters): |
|
498 | def dict_to_parms(self, parameters): | |
499 |
|
499 | |||
500 | if type(parameters) != type({}): |
|
500 | if type(parameters) != type({}): | |
501 | return |
|
501 | return | |
502 |
|
502 | |||
503 | for key in parameters.keys(): |
|
503 | for key in parameters.keys(): | |
504 | setattr(self, key, parameters[key]) |
|
504 | setattr(self, key, parameters[key]) | |
505 |
|
505 | |||
506 | def export_to_file(self, format="json"): |
|
506 | def export_to_file(self, format="json"): | |
507 |
|
507 | |||
508 | import json |
|
508 | import json | |
509 |
|
509 | |||
510 | content_type = '' |
|
510 | content_type = '' | |
511 |
|
511 | |||
512 | if format == 'text': |
|
512 | if format == 'text': | |
513 | content_type = 'text/plain' |
|
513 | content_type = 'text/plain' | |
514 | filename = '%s_%s.%s' %(self.device.device_type.name, self.name, self.device.device_type.name) |
|
514 | filename = '%s_%s.%s' %(self.device.device_type.name, self.name, self.device.device_type.name) | |
515 | content = self.parms_to_text() |
|
515 | content = self.parms_to_text() | |
516 |
|
516 | |||
517 | if format == 'binary': |
|
517 | if format == 'binary': | |
518 | content_type = 'application/octet-stream' |
|
518 | content_type = 'application/octet-stream' | |
519 | filename = '%s_%s.bin' %(self.device.device_type.name, self.name) |
|
519 | filename = '%s_%s.bin' %(self.device.device_type.name, self.name) | |
520 | content = self.parms_to_binary() |
|
520 | content = self.parms_to_binary() | |
521 |
|
521 | |||
522 | if not content_type: |
|
522 | if not content_type: | |
523 | content_type = 'application/json' |
|
523 | content_type = 'application/json' | |
524 | filename = '%s_%s.json' %(self.device.device_type.name, self.name) |
|
524 | filename = '%s_%s.json' %(self.device.device_type.name, self.name) | |
525 | content = json.dumps(self.parms_to_dict(), indent=2) |
|
525 | content = json.dumps(self.parms_to_dict(), indent=2) | |
526 |
|
526 | |||
527 | fields = {'content_type':content_type, |
|
527 | fields = {'content_type':content_type, | |
528 | 'filename':filename, |
|
528 | 'filename':filename, | |
529 | 'content':content |
|
529 | 'content':content | |
530 | } |
|
530 | } | |
531 |
|
531 | |||
532 | return fields |
|
532 | return fields | |
533 |
|
533 | |||
534 | def import_from_file(self, fp): |
|
534 | def import_from_file(self, fp): | |
535 |
|
535 | |||
536 | import os, json |
|
536 | import os, json | |
537 |
|
537 | |||
538 | parms = {} |
|
538 | parms = {} | |
539 |
|
539 | |||
540 | path, ext = os.path.splitext(fp.name) |
|
540 | path, ext = os.path.splitext(fp.name) | |
541 |
|
541 | |||
542 | if ext == '.json': |
|
542 | if ext == '.json': | |
543 | parms = json.load(fp) |
|
543 | parms = json.load(fp) | |
544 |
|
544 | |||
545 | return parms |
|
545 | return parms | |
546 |
|
546 | |||
547 | def status_device(self): |
|
547 | def status_device(self): | |
548 |
|
548 | |||
549 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
549 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() | |
550 |
|
550 | |||
551 | return None |
|
551 | return None | |
552 |
|
552 | |||
553 | def stop_device(self): |
|
553 | def stop_device(self): | |
554 |
|
554 | |||
555 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
555 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() | |
556 |
|
556 | |||
557 | return None |
|
557 | return None | |
558 |
|
558 | |||
559 | def start_device(self): |
|
559 | def start_device(self): | |
560 |
|
560 | |||
561 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
561 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() | |
562 |
|
562 | |||
563 | return None |
|
563 | return None | |
564 |
|
564 | |||
565 | def write_device(self, parms): |
|
565 | def write_device(self, parms): | |
566 |
|
566 | |||
567 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
567 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() | |
568 |
|
568 | |||
569 | return None |
|
569 | return None | |
570 |
|
570 | |||
571 | def read_device(self): |
|
571 | def read_device(self): | |
572 |
|
572 | |||
573 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() |
|
573 | raise NotImplementedError, "This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper() | |
574 |
|
574 | |||
575 | return None |
|
575 | return None | |
576 |
|
576 | |||
577 | def get_absolute_url(self): |
|
577 | def get_absolute_url(self): | |
578 | return reverse('url_%s_conf' % self.device.device_type.name, args=[str(self.id)]) |
|
578 | return reverse('url_%s_conf' % self.device.device_type.name, args=[str(self.id)]) | |
579 |
|
579 | |||
580 | def get_absolute_url_edit(self): |
|
580 | def get_absolute_url_edit(self): | |
581 | return reverse('url_edit_%s_conf' % self.device.device_type.name, args=[str(self.id)]) |
|
581 | return reverse('url_edit_%s_conf' % self.device.device_type.name, args=[str(self.id)]) | |
582 |
|
582 | |||
583 | def get_absolute_url_import(self): |
|
583 | def get_absolute_url_import(self): | |
584 | return reverse('url_import_dev_conf', args=[str(self.id)]) |
|
584 | return reverse('url_import_dev_conf', args=[str(self.id)]) | |
585 |
|
585 | |||
586 | def get_absolute_url_export(self): |
|
586 | def get_absolute_url_export(self): | |
587 | return reverse('url_export_dev_conf', args=[str(self.id)]) |
|
587 | return reverse('url_export_dev_conf', args=[str(self.id)]) | |
588 |
|
588 | |||
589 | def get_absolute_url_write(self): |
|
589 | def get_absolute_url_write(self): | |
590 | return reverse('url_write_dev_conf', args=[str(self.id)]) |
|
590 | return reverse('url_write_dev_conf', args=[str(self.id)]) | |
591 |
|
591 | |||
592 | def get_absolute_url_read(self): |
|
592 | def get_absolute_url_read(self): | |
593 | return reverse('url_read_dev_conf', args=[str(self.id)]) |
|
593 | return reverse('url_read_dev_conf', args=[str(self.id)]) | |
594 |
|
594 | |||
595 | def get_absolute_url_start(self): |
|
595 | def get_absolute_url_start(self): | |
596 | return reverse('url_start_dev_conf', args=[str(self.id)]) |
|
596 | return reverse('url_start_dev_conf', args=[str(self.id)]) | |
597 |
|
597 | |||
598 | def get_absolute_url_stop(self): |
|
598 | def get_absolute_url_stop(self): | |
599 | return reverse('url_stop_dev_conf', args=[str(self.id)]) |
|
599 | return reverse('url_stop_dev_conf', args=[str(self.id)]) | |
600 |
|
600 | |||
601 | def get_absolute_url_status(self): |
|
601 | def get_absolute_url_status(self): | |
602 | return reverse('url_status_dev_conf', args=[str(self.id)]) No newline at end of file |
|
602 | return reverse('url_status_dev_conf', args=[str(self.id)]) |
General Comments 0
You need to be logged in to leave comments.
Login now