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