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