##// END OF EJS Templates
ATRAD actualizando títulos
Renato Huallpa -
r397:e40954e05ac0
parent child
Show More
@@ -1,35 +1,36
1
1
2 REDIS_HOST=radarsys-redis
2 REDIS_HOST=radarsys-redis
3 REDIS_PORT=6300
3 REDIS_PORT=6300
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_HOST=postgres
6 #POSTGRES_HOST=postgres
7 POSTGRES_HOST=localhost
7 POSTGRES_HOST=localhost
8 POSTGRES_USER=docker
8 POSTGRES_USER=docker
9 POSTGRES_PASSWORD=docker
9 POSTGRES_PASSWORD=docker
10 POSTGRES_DB=radarsys
10 POSTGRES_DB=radarsys
11
11
12 # DB_NAME=radarsys
12 # DB_NAME=radarsys
13 # DB_USER=docker
13 # DB_USER=docker
14 # DB_PASSWORD=docker
14 # DB_PASSWORD=docker
15 PGDATA=/var/lib/postgresql/data
15 PGDATA=/var/lib/postgresql/data
16 LC_ALL=C.UTF-8
16 LC_ALL=C.UTF-8
17 TZ=America/Lima
17 TZ=America/Lima
18 DOCKER_DATA=/data/dockers/radarsys/
18 DOCKER_DATA=/data/dockers/radarsys/
19 LOCAL_IP=192.168.1.128
19 LOCAL_IP=192.168.1.128
20
20
21 MQTT_SERVER=10.10.10.200
21 MQTT_SERVER=10.10.10.200
22 # MQTT_SERVER = 192.168.100.5
22 # MQTT_SERVER = 192.168.100.5
23 MQTT_PORT = 1883
23 MQTT_PORT = 1883
24 MQTT_KEEPALIVE = 3660
24 MQTT_KEEPALIVE = 3660
25
25
26 MQTT_USER_ATRAD=''
26 MQTT_USER_ATRAD=''
27 MQTT_PASSWORD_ATRAD = ''
27 MQTT_PASSWORD_ATRAD = ''
28 MQTT_TOPIC_ATRAD_RECIEVE = "atrad/test4"
28 MQTT_TOPIC_ATRAD_RECIEVE = "atrad/test4"
29 MQTT_TOPIC_ATRAD_CONTROL = "atrad/test2"
29
30
30 MQTT_USER = abs
31 MQTT_USER = abs
31 MQTT_PASSWORD = abs
32 MQTT_PASSWORD = abs
32
33
33 TOPIC_ABS=abs/beams
34 TOPIC_ABS=abs/beams
34 TOPIC_ABS_ACK=abs/beams_ack
35 TOPIC_ABS_ACK=abs/beams_ack
35 TOPIC_ABS_CHANGE=abs/change_beam
36 TOPIC_ABS_CHANGE=abs/change_beam
@@ -1,1190 +1,1164
1 from django.db import models
1 from django.db import models
2 from apps.main.models import Configuration , User
2 from apps.main.models import Configuration , User
3 from django.urls import reverse
3 from django.urls import reverse
4 from celery.execute import send_task
4 from celery.execute import send_task
5 from datetime import datetime
5 from datetime import datetime
6 import ast
6 import ast
7 import socket
7 import socket
8 import json
8 import json
9 import requests
9 import requests
10 import struct
10 import struct
11 import os, sys, time
11 import os, sys, time
12
12
13 from .mqtt import client as mqtt_client
13 from .mqtt import client as mqtt_client
14 from radarsys.socketconfig import sio as sio
14 from radarsys.socketconfig import sio as sio
15 import json
15 import json
16
16
17 antenna_default = json.dumps({
17 antenna_default = json.dumps({
18 "antenna_up": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
18 "antenna_up": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
19 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
19 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
20 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
20 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
21 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
21 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
22 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
22 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
23 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
23 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
24 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
24 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
25 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0]
25 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0]
26 ]
26 ]
27 ,
27 ,
28 "antenna_down": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
28 "antenna_down": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
29 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
29 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
30 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
30 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
31 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
31 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
32 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
32 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
33 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
33 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
34 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
34 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
35 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0]],
35 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0]],
36 })
36 })
37
37
38
38
39 tx_default = json.dumps({
39 tx_default = json.dumps({
40 "up": [[1,1,1,1,0,0,0,0],
40 "up": [[1,1,1,1,0,0,0,0],
41 [1,1,1,1,0,0,0,0],
41 [1,1,1,1,0,0,0,0],
42 [1,1,1,1,0,0,0,0],
42 [1,1,1,1,0,0,0,0],
43 [1,1,1,1,0,0,0,0],
43 [1,1,1,1,0,0,0,0],
44 [0,0,0,0,1,1,1,1],
44 [0,0,0,0,1,1,1,1],
45 [0,0,0,0,1,1,1,1],
45 [0,0,0,0,1,1,1,1],
46 [0,0,0,0,1,1,1,1],
46 [0,0,0,0,1,1,1,1],
47 [0,0,0,0,1,1,1,1]],
47 [0,0,0,0,1,1,1,1]],
48
48
49 "down": [[1,1,1,1,0,0,0,0],
49 "down": [[1,1,1,1,0,0,0,0],
50 [1,1,1,1,0,0,0,0],
50 [1,1,1,1,0,0,0,0],
51 [1,1,1,1,0,0,0,0],
51 [1,1,1,1,0,0,0,0],
52 [1,1,1,1,0,0,0,0],
52 [1,1,1,1,0,0,0,0],
53 [0,0,0,0,1,1,1,1],
53 [0,0,0,0,1,1,1,1],
54 [0,0,0,0,1,1,1,1],
54 [0,0,0,0,1,1,1,1],
55 [0,0,0,0,1,1,1,1],
55 [0,0,0,0,1,1,1,1],
56 [0,0,0,0,1,1,1,1]],
56 [0,0,0,0,1,1,1,1]],
57 })
57 })
58
58
59 rx_default = json.dumps({
59 rx_default = json.dumps({
60 "up": [[1,1,1,1,0,0,0,0],
60 "up": [[1,1,1,1,0,0,0,0],
61 [1,1,1,1,0,0,0,0],
61 [1,1,1,1,0,0,0,0],
62 [1,1,1,1,0,0,0,0],
62 [1,1,1,1,0,0,0,0],
63 [1,1,1,1,0,0,0,0],
63 [1,1,1,1,0,0,0,0],
64 [0,0,0,0,1,1,1,1],
64 [0,0,0,0,1,1,1,1],
65 [0,0,0,0,1,1,1,1],
65 [0,0,0,0,1,1,1,1],
66 [0,0,0,0,1,1,1,1],
66 [0,0,0,0,1,1,1,1],
67 [0,0,0,0,1,1,1,1]],
67 [0,0,0,0,1,1,1,1]],
68
68
69 "down": [[1,1,1,1,0,0,0,0],
69 "down": [[1,1,1,1,0,0,0,0],
70 [1,1,1,1,0,0,0,0],
70 [1,1,1,1,0,0,0,0],
71 [1,1,1,1,0,0,0,0],
71 [1,1,1,1,0,0,0,0],
72 [1,1,1,1,0,0,0,0],
72 [1,1,1,1,0,0,0,0],
73 [0,0,0,0,1,1,1,1],
73 [0,0,0,0,1,1,1,1],
74 [0,0,0,0,1,1,1,1],
74 [0,0,0,0,1,1,1,1],
75 [0,0,0,0,1,1,1,1],
75 [0,0,0,0,1,1,1,1],
76 [0,0,0,0,1,1,1,1]],
76 [0,0,0,0,1,1,1,1]],
77 })
77 })
78
78
79 status_default = '0000000000000000000000000000000000000000000000000000000000000000'
79 status_default = '0000000000000000000000000000000000000000000000000000000000000000'
80 default_messages = {}
80 default_messages = {}
81
81
82 for i in range(1,65):
82 for i in range(1,65):
83 default_messages[str(i)] = "Module "+str(i)
83 default_messages[str(i)] = "Module "+str(i)
84
84
85
85
86 ues_default = json.dumps({
86 ues_default = json.dumps({
87 "up": [0.533333,0.00000,1.06667,0.00000],
87 "up": [0.533333,0.00000,1.06667,0.00000],
88 "down": [0.533333,0.00000,1.06667,0.00000]
88 "down": [0.533333,0.00000,1.06667,0.00000]
89 })
89 })
90
90
91 onlyrx_default = json.dumps({
91 onlyrx_default = json.dumps({
92 "up": False,
92 "up": False,
93 "down": False
93 "down": False
94 })
94 })
95
95
96 def up_convertion(cadena):
96 def up_convertion(cadena):
97 valores = []
97 valores = []
98 for c in cadena:
98 for c in cadena:
99 if c == 1.0: valores=valores+['000']
99 if c == 1.0: valores=valores+['000']
100 if c == 2.0: valores=valores+['001']
100 if c == 2.0: valores=valores+['001']
101 if c == 3.0: valores=valores+['010']
101 if c == 3.0: valores=valores+['010']
102 if c == 0.0: valores=valores+['011']
102 if c == 0.0: valores=valores+['011']
103 if c == 0.5: valores=valores+['100']
103 if c == 0.5: valores=valores+['100']
104 if c == 1.5: valores=valores+['101']
104 if c == 1.5: valores=valores+['101']
105 if c == 2.5: valores=valores+['110']
105 if c == 2.5: valores=valores+['110']
106 if c == 3.5: valores=valores+['111']
106 if c == 3.5: valores=valores+['111']
107
107
108 return valores
108 return valores
109
109
110 def up_conv_bits(value):
110 def up_conv_bits(value):
111
111
112 if value == 1.0: bits="000"
112 if value == 1.0: bits="000"
113 if value == 2.0: bits="001"
113 if value == 2.0: bits="001"
114 if value == 3.0: bits="010"
114 if value == 3.0: bits="010"
115 if value == 0.0: bits="011"
115 if value == 0.0: bits="011"
116 if value == 0.5: bits="100"
116 if value == 0.5: bits="100"
117 if value == 1.5: bits="101"
117 if value == 1.5: bits="101"
118 if value == 2.5: bits="110"
118 if value == 2.5: bits="110"
119 if value == 3.5: bits="111"
119 if value == 3.5: bits="111"
120
120
121 return bits
121 return bits
122
122
123 def down_convertion(cadena):
123 def down_convertion(cadena):
124 valores = []
124 valores = []
125 for c in cadena:
125 for c in cadena:
126 if c == 1.0: valores=valores+['000']
126 if c == 1.0: valores=valores+['000']
127 if c == 2.0: valores=valores+['001']
127 if c == 2.0: valores=valores+['001']
128 if c == 3.0: valores=valores+['010']
128 if c == 3.0: valores=valores+['010']
129 if c == 0.0: valores=valores+['011']
129 if c == 0.0: valores=valores+['011']
130 if c == 0.5: valores=valores+['100']
130 if c == 0.5: valores=valores+['100']
131 if c == 1.5: valores=valores+['101']
131 if c == 1.5: valores=valores+['101']
132 if c == 2.5: valores=valores+['110']
132 if c == 2.5: valores=valores+['110']
133 if c == 3.5: valores=valores+['111']
133 if c == 3.5: valores=valores+['111']
134
134
135 return valores
135 return valores
136
136
137 def down_conv_bits(value):
137 def down_conv_bits(value):
138
138
139 if value == 1.0: bits="000"
139 if value == 1.0: bits="000"
140 if value == 2.0: bits="001"
140 if value == 2.0: bits="001"
141 if value == 3.0: bits="010"
141 if value == 3.0: bits="010"
142 if value == 0.0: bits="011"
142 if value == 0.0: bits="011"
143 if value == 0.5: bits="100"
143 if value == 0.5: bits="100"
144 if value == 1.5: bits="101"
144 if value == 1.5: bits="101"
145 if value == 2.5: bits="110"
145 if value == 2.5: bits="110"
146 if value == 3.5: bits="111"
146 if value == 3.5: bits="111"
147
147
148 return bits
148 return bits
149
149
150 def up_conv_value(bits):
150 def up_conv_value(bits):
151
151
152 if bits == "000": value=1.0
152 if bits == "000": value=1.0
153 if bits == "001": value=2.0
153 if bits == "001": value=2.0
154 if bits == "010": value=3.0
154 if bits == "010": value=3.0
155 if bits == "011": value=0.0
155 if bits == "011": value=0.0
156 if bits == "100": value=0.5
156 if bits == "100": value=0.5
157 if bits == "101": value=1.5
157 if bits == "101": value=1.5
158 if bits == "110": value=2.5
158 if bits == "110": value=2.5
159 if bits == "111": value=3.5
159 if bits == "111": value=3.5
160
160
161 return value
161 return value
162
162
163 def down_conv_value(bits):
163 def down_conv_value(bits):
164
164
165 if bits == "000": value=1.0
165 if bits == "000": value=1.0
166 if bits == "001": value=2.0
166 if bits == "001": value=2.0
167 if bits == "010": value=3.0
167 if bits == "010": value=3.0
168 if bits == "011": value=0.0
168 if bits == "011": value=0.0
169 if bits == "100": value=0.5
169 if bits == "100": value=0.5
170 if bits == "101": value=1.5
170 if bits == "101": value=1.5
171 if bits == "110": value=2.5
171 if bits == "110": value=2.5
172 if bits == "111": value=3.5
172 if bits == "111": value=3.5
173
173
174 return value
174 return value
175
175
176 def ip2position(module_number):
176 def ip2position(module_number):
177 j=0
177 j=0
178 i=0
178 i=0
179 for x in range(0,module_number-1):
179 for x in range(0,module_number-1):
180 j=j+1
180 j=j+1
181 if j==8:
181 if j==8:
182 i=i+1
182 i=i+1
183 j=0
183 j=0
184
184
185 pos = [i,j]
185 pos = [i,j]
186 return pos
186 return pos
187
187
188
188
189 def fromBinary2Char(binary_string):
189 def fromBinary2Char(binary_string):
190 number = int(binary_string, 2)
190 number = int(binary_string, 2)
191 #Plus 33 to avoid more than 1 characters values such as: '\x01'-'\x1f'
191 #Plus 33 to avoid more than 1 characters values such as: '\x01'-'\x1f'
192 number = number + 33
192 number = number + 33
193 char = chr(number)
193 char = chr(number)
194 return char
194 return char
195
195
196 def fromChar2Binary(char):
196 def fromChar2Binary(char):
197 number = ord(char) - 33
197 number = ord(char) - 33
198 #Minus 33 to get the real value
198 #Minus 33 to get the real value
199 bits = bin(number)[2:]
199 bits = bin(number)[2:]
200 #To ensure we have a string with 6bits
200 #To ensure we have a string with 6bits
201 if len(bits) < 6:
201 if len(bits) < 6:
202 bits = bits.zfill(6)
202 bits = bits.zfill(6)
203 return bits
203 return bits
204
204
205 OPERATION_MODES = (
205 OPERATION_MODES = (
206 (0, 'Manual'),
206 (0, 'Manual'),
207 (1, 'Automatic'),
207 (1, 'Automatic'),
208 )
208 )
209
209
210 class ABSConfiguration(Configuration):
210 class ABSConfiguration(Configuration):
211 active_beam = models.PositiveSmallIntegerField(verbose_name='Active Beam', default=0)
211 active_beam = models.PositiveSmallIntegerField(verbose_name='Active Beam', default=0)
212 module_status = models.CharField(verbose_name='Module Status', max_length=10000, default=status_default)
212 module_status = models.CharField(verbose_name='Module Status', max_length=10000, default=status_default)
213 operation_mode = models.PositiveSmallIntegerField(verbose_name='Operation Mode', choices=OPERATION_MODES, default = 0)
213 operation_mode = models.PositiveSmallIntegerField(verbose_name='Operation Mode', choices=OPERATION_MODES, default = 0)
214 operation_value = models.FloatField(verbose_name='Periodic (seconds)', default="10", null=True, blank=True)
214 operation_value = models.FloatField(verbose_name='Periodic (seconds)', default="10", null=True, blank=True)
215 module_messages = models.CharField(verbose_name='Modules Messages', max_length=10000, default=json.dumps(default_messages))
215 module_messages = models.CharField(verbose_name='Modules Messages', max_length=10000, default=json.dumps(default_messages))
216
216
217 class Meta:
217 class Meta:
218 db_table = 'abs_configurations'
218 db_table = 'abs_configurations'
219
219
220 def get_absolute_url_plot(self):
220 def get_absolute_url_plot(self):
221 return reverse('url_plot_abs_patterns', args=[str(self.id)])
221 return reverse('url_plot_abs_patterns', args=[str(self.id)])
222
222
223
223
224 def parms_to_dict(self):
224 def parms_to_dict(self):
225
225
226 parameters = {}
226 parameters = {}
227
227
228 parameters['device_id'] = self.device.id
228 parameters['device_id'] = self.device.id
229 parameters['label'] = self.label
229 parameters['label'] = self.label
230 parameters['device_type'] = self.device.device_type.name
230 parameters['device_type'] = self.device.device_type.name
231 parameters['beams'] = {}
231 parameters['beams'] = {}
232
232
233 beams = ABSBeam.objects.filter(abs_conf=self)
233 beams = ABSBeam.objects.filter(abs_conf=self)
234 b=1
234 b=1
235 for beam in beams:
235 for beam in beams:
236 #absbeam = ABSBeam.objects.get(pk=beams[beam])
236 #absbeam = ABSBeam.objects.get(pk=beams[beam])
237 parameters['beams']['beam'+str(b)] = beam.parms_to_dict()#absbeam.parms_to_dict()
237 parameters['beams']['beam'+str(b)] = beam.parms_to_dict()#absbeam.parms_to_dict()
238 b+=1
238 b+=1
239
239
240 return parameters
240 return parameters
241
241
242
242
243 def dict_to_parms(self, parameters):
243 def dict_to_parms(self, parameters):
244
244
245 self.label = parameters['label']
245 self.label = parameters['label']
246
246
247 absbeams = ABSBeam.objects.filter(abs_conf=self)
247 absbeams = ABSBeam.objects.filter(abs_conf=self)
248 beams = parameters['beams']
248 beams = parameters['beams']
249
249
250 if absbeams:
250 if absbeams:
251 beams_number = len(beams)
251 beams_number = len(beams)
252 absbeams_number = len(absbeams)
252 absbeams_number = len(absbeams)
253 if beams_number==absbeams_number:
253 if beams_number==absbeams_number:
254 i = 1
254 i = 1
255 for absbeam in absbeams:
255 for absbeam in absbeams:
256 absbeam.dict_to_parms(beams['beam'+str(i)])
256 absbeam.dict_to_parms(beams['beam'+str(i)])
257 i = i+1
257 i = i+1
258 elif beams_number > absbeams_number:
258 elif beams_number > absbeams_number:
259 i = 1
259 i = 1
260 for absbeam in absbeams:
260 for absbeam in absbeams:
261 absbeam.dict_to_parms(beams['beam'+str(i)])
261 absbeam.dict_to_parms(beams['beam'+str(i)])
262 i=i+1
262 i=i+1
263 for x in range(i,beams_number+1):
263 for x in range(i,beams_number+1):
264 new_beam = ABSBeam(
264 new_beam = ABSBeam(
265 name =beams['beam'+str(i)]['name'],
265 name =beams['beam'+str(i)]['name'],
266 antenna =json.dumps(beams['beam'+str(i)]['antenna']),
266 antenna =json.dumps(beams['beam'+str(i)]['antenna']),
267 abs_conf = self,
267 abs_conf = self,
268 tx =json.dumps(beams['beam'+str(i)]['tx']),
268 tx =json.dumps(beams['beam'+str(i)]['tx']),
269 rx =json.dumps(beams['beam'+str(i)]['rx']),
269 rx =json.dumps(beams['beam'+str(i)]['rx']),
270 ues =json.dumps(beams['beam'+str(i)]['ues']),
270 ues =json.dumps(beams['beam'+str(i)]['ues']),
271 only_rx =json.dumps(beams['beam'+str(i)]['only_rx'])
271 only_rx =json.dumps(beams['beam'+str(i)]['only_rx'])
272 )
272 )
273 new_beam.save()
273 new_beam.save()
274 i=i+1
274 i=i+1
275 else: #beams_number < absbeams_number:
275 else: #beams_number < absbeams_number:
276 i = 1
276 i = 1
277 for absbeam in absbeams:
277 for absbeam in absbeams:
278 if i <= beams_number:
278 if i <= beams_number:
279 absbeam.dict_to_parms(beams['beam'+str(i)])
279 absbeam.dict_to_parms(beams['beam'+str(i)])
280 i=i+1
280 i=i+1
281 else:
281 else:
282 absbeam.delete()
282 absbeam.delete()
283 else:
283 else:
284 for beam in beams:
284 for beam in beams:
285 new_beam = ABSBeam(
285 new_beam = ABSBeam(
286 name =beams[beam]['name'],
286 name =beams[beam]['name'],
287 antenna =json.dumps(beams[beam]['antenna']),
287 antenna =json.dumps(beams[beam]['antenna']),
288 abs_conf = self,
288 abs_conf = self,
289 tx =json.dumps(beams[beam]['tx']),
289 tx =json.dumps(beams[beam]['tx']),
290 rx =json.dumps(beams[beam]['rx']),
290 rx =json.dumps(beams[beam]['rx']),
291 ues =json.dumps(beams[beam]['ues']),
291 ues =json.dumps(beams[beam]['ues']),
292 only_rx =json.dumps(beams[beam]['only_rx'])
292 only_rx =json.dumps(beams[beam]['only_rx'])
293 )
293 )
294 new_beam.save()
294 new_beam.save()
295
295
296
296
297
297
298 def update_from_file(self, parameters):
298 def update_from_file(self, parameters):
299
299
300 self.dict_to_parms(parameters)
300 self.dict_to_parms(parameters)
301 self.save()
301 self.save()
302
302
303
303
304 def get_beams(self, **kwargs):
304 def get_beams(self, **kwargs):
305 '''
305 '''
306 This function returns ABS Configuration beams
306 This function returns ABS Configuration beams
307 '''
307 '''
308 return ABSBeam.objects.filter(abs_conf=self.pk, **kwargs)
308 return ABSBeam.objects.filter(abs_conf=self.pk, **kwargs)
309
309
310 def clone(self, **kwargs):
310 def clone(self, **kwargs):
311
311
312 beams = self.get_beams()
312 beams = self.get_beams()
313 self.pk = None
313 self.pk = None
314 self.id = None
314 self.id = None
315 for attr, value in kwargs.items():
315 for attr, value in kwargs.items():
316 setattr(self, attr, value)
316 setattr(self, attr, value)
317 self.save()
317 self.save()
318
318
319 for beam in beams:
319 for beam in beams:
320 beam.clone(abs_conf=self)
320 beam.clone(abs_conf=self)
321
321
322 #-----For Active Beam-----
322 #-----For Active Beam-----
323 new_beams = ABSBeam.objects.filter(abs_conf=self)
323 new_beams = ABSBeam.objects.filter(abs_conf=self)
324 self.active_beam = new_beams[0].id
324 self.active_beam = new_beams[0].id
325 self.save()
325 self.save()
326 #-----For Active Beam-----
326 #-----For Active Beam-----
327 #-----For Device Status---
327 #-----For Device Status---
328 self.device.status = 3
328 self.device.status = 3
329 self.device.save()
329 self.device.save()
330 #-----For Device Status---
330 #-----For Device Status---
331
331
332 return self
332 return self
333
333
334
334
335 def start_device(self):
335 def start_device(self):
336
336
337 if self.device.status == 3:
337 if self.device.status == 3:
338
338
339 try:
339 try:
340 #self.write_device()
340 #self.write_device()
341 send_task('task_change_beam', [self.id],)
341 send_task('task_change_beam', [self.id],)
342 # print("*************************RUNNING ABS**************************",flush=True)
343 self.message = 'ABS running'
342 self.message = 'ABS running'
344
343
345 except Exception as e:
344 except Exception as e:
346 self.message = str(e)
345 self.message = str(e)
347 return False
346 return False
348
347
349 return True
348 return True
350
349
351 else:
350 else:
352 self.message = 'Please, select Write ABS Device first.'
351 self.message = 'Please, select Write ABS Device first.'
353 return False
352 return False
354
353
355
354
356 def stop_device(self):
355 def stop_device(self):
357 self.device.status = 2
356 self.device.status = 2
358 self.active_beam = 0
357 self.active_beam = 0
359 self.device.save()
358 self.device.save()
360 self.message = 'ABS has been stopped.'
359 self.message = 'ABS has been stopped.'
361 self.save()
360 self.save()
362
361
363 return True
362 return True
364
363
365 def stop_device_mqtt(self):
364 def stop_device_mqtt(self):
366
365
367 self.device.status = 2
366 self.device.status = 2
368 self.active_beam = 0
367 self.active_beam = 0
369 # conf_active = None
368 # conf_active = None
370 # conf_active.save()
369 # conf_active.save()
371 self.device.save()
370 self.device.save()
372 self.message = 'ABS has been stopped.'
371 self.message = 'ABS has been stopped.'
373 self.save()
372 self.save()
374
373
375 mqtt_client.publish(os.environ.get('TOPIC_ABS', 'abs/beams'),"STOP")
374 mqtt_client.publish(os.environ.get('TOPIC_ABS', 'abs/beams'),"STOP")
376
375
377 return True
376 return True
378
377
379 def write_device(self):
378 def write_device(self):
380
379
381 """
380 """
382 This function sends the beams list to every abs module.
381 This function sends the beams list to every abs module.
383 It needs 'module_conf' function
382 It needs 'module_conf' function
384 """
383 """
385
384
386 beams = ABSBeam.objects.filter(abs_conf=self)
385 beams = ABSBeam.objects.filter(abs_conf=self)
387 nbeams = len(beams)
386 nbeams = len(beams)
388
387
389 # Se manda a cero RC para poder realizar cambio de beam
388 # Se manda a cero RC para poder realizar cambio de beam
390 if self.experiment is None:
389 if self.experiment is None:
391 confs = []
390 confs = []
392 else:
391 else:
393 confs = Configuration.objects.filter(experiment = self.experiment).filter(type=0)
392 confs = Configuration.objects.filter(experiment = self.experiment).filter(type=0)
394 confdds = ''
393 confdds = ''
395 confjars = ''
394 confjars = ''
396 confrc = ''
395 confrc = ''
397 #TO STOP DEVICES: DDS-JARS-RC
396 #TO STOP DEVICES: DDS-JARS-RC
398 for i in range(0,len(confs)):
397 for i in range(0,len(confs)):
399 if i==0:
398 if i==0:
400 for conf in confs:
399 for conf in confs:
401 if conf.device.device_type.name == 'dds':
400 if conf.device.device_type.name == 'dds':
402 confdds = conf
401 confdds = conf
403 confdds.stop_device()
402 confdds.stop_device()
404 break
403 break
405 if i==1:
404 if i==1:
406 for conf in confs:
405 for conf in confs:
407 if conf.device.device_type.name == 'jars':
406 if conf.device.device_type.name == 'jars':
408 confjars = conf
407 confjars = conf
409 confjars.stop_device()
408 confjars.stop_device()
410 break
409 break
411 if i==2:
410 if i==2:
412 for conf in confs:
411 for conf in confs:
413 if conf.device.device_type.name == 'rc':
412 if conf.device.device_type.name == 'rc':
414 confrc = conf
413 confrc = conf
415 confrc.stop_device()
414 confrc.stop_device()
416 break
415 break
417
416
418 '''
417 '''
419 if self.connected_modules() == 0 :
418 if self.connected_modules() == 0 :
420 print("No encuentra modulos")
419 print("No encuentra modulos")
421 self.message = "No ABS Module detected."
420 self.message = "No ABS Module detected."
422 return False
421 return False
423 '''
422 '''
424 #-------------Write each abs module-----------
423 #-------------Write each abs module-----------
425
424
426 if beams:
425 if beams:
427 block_id = 0
426 block_id = 0
428 message = 'SNDF{:03d}{:02d}{:02d}'.format(nbeams, nbeams, block_id)
427 message = 'SNDF{:03d}{:02d}{:02d}'.format(nbeams, nbeams, block_id)
429 for i, status in enumerate(self.module_status):
428 for i, status in enumerate(self.module_status):
430 message += ''.join([fromBinary2Char(beam.module_6bits(i)) for beam in beams])
429 message += ''.join([fromBinary2Char(beam.module_6bits(i)) for beam in beams])
431 status = ['0'] * 64
430 status = ['0'] * 64
432 n = 0
431 n = 0
433 print("Estoy en write_device normal",flush=True)
434 sock = self.send_multicast(message)
432 sock = self.send_multicast(message)
435
433
436 while True:
434 while True:
437 #for i in range(32):
435 #for i in range(32):
438 try:
436 try:
439 data, address = sock.recvfrom(1024)
437 data, address = sock.recvfrom(1024)
440 print (address, data)
438 print (address, data)
441 data = data.decode("utf-8")
439 data = data.decode("utf-8")
442 if data == '1':
440 if data == '1':
443 status[int(address[0][10:])-1] = '3'
441 status[int(address[0][10:])-1] = '3'
444 #print (int(address[0][10:])-1)
442 #print (int(address[0][10:])-1)
445 elif data == '0':
443 elif data == '0':
446 status[int(address[0][10:])-1] = '1'
444 status[int(address[0][10:])-1] = '1'
447 except socket.timeout:
445 except socket.timeout:
448 print('Timeout')
446 print('Timeout')
449 break
447 break
450 except Exception as e:
448 except Exception as e:
451 print ('Error {}'.format(e))
449 print ('Error {}'.format(e))
452 n += 1
450 n += 1
453 sock.close()
451 sock.close()
454 else:
452 else:
455 self.message = "ABS Configuration does not have beams"
453 self.message = "ABS Configuration does not have beams"
456 # print('No beams')
454 # print('No beams')
457 #Start DDS-RC-JARS
455 #Start DDS-RC-JARS
458 if confdds:
456 if confdds:
459 confdds.start_device()
457 confdds.start_device()
460 if confrc:
458 if confrc:
461 #print confrc
459 #print confrc
462 confrc.start_device()
460 confrc.start_device()
463 if confjars:
461 if confjars:
464 confjars.start_device()
462 confjars.start_device()
465 return False
463 return False
466
464
467 if n == 64:
465 if n == 64:
468 self.message = "Could not write ABS Modules"
466 self.message = "Could not write ABS Modules"
469 self.device.status = 0
467 self.device.status = 0
470 self.module_status = ''.join(status)
468 self.module_status = ''.join(status)
471 self.save()
469 self.save()
472 # print('Could not write ABS')
470 # print('Could not write ABS')
473 #Start DDS-RC-JARS
471 #Start DDS-RC-JARS
474 if confdds:
472 if confdds:
475 confdds.start_device()
473 confdds.start_device()
476 if confrc:
474 if confrc:
477 #print confrc
475 #print confrc
478 confrc.start_device()
476 confrc.start_device()
479 if confjars:
477 if confjars:
480 confjars.start_device()
478 confjars.start_device()
481 return False
479 return False
482 else:
480 else:
483 self.message = "ABS Beams List have been sent to ABS Modules"
481 self.message = "ABS Beams List have been sent to ABS Modules"
484 print('ABS beams list sent')
482 print('ABS beams list sent')
485 self.active_beam = beams[0].pk
483 self.active_beam = beams[0].pk
486
484
487 #Start DDS-RC-JARS
485 #Start DDS-RC-JARS
488 if confdds:
486 if confdds:
489 confdds.start_device()
487 confdds.start_device()
490 if confrc:
488 if confrc:
491 #print confrc
489 #print confrc
492 confrc.start_device()
490 confrc.start_device()
493 if confjars:
491 if confjars:
494 confjars.start_device()
492 confjars.start_device()
495
493
496 # print('Inicia intento de salvar device.status')
494 # print('Inicia intento de salvar device.status')
497 self.device.status = 3
495 self.device.status = 3
498 self.module_status = ''.join(status)
496 self.module_status = ''.join(status)
499 #print(status)
497 #print(status)
500 self.save()
498 self.save()
501 # print('Estatus salvado')
499 # print('Estatus salvado')
502 conf_active, __ = ABSActive.objects.get_or_create(pk=1)
500 conf_active, __ = ABSActive.objects.get_or_create(pk=1)
503 conf_active.conf = self
501 conf_active.conf = self
504 conf_active.save()
502 conf_active.save()
505 return True
503 return True
506
504
507
505
508 # DEV_STATES = (
506 # DEV_STATES = (
509 # (0, 'No connected'),
507 # (0, 'No connected'),
510 # (1, 'Connected'),
508 # (1, 'Connected'),
511 # (2, 'Configured'),
509 # (2, 'Configured'),
512 # (3, 'Running'),
510 # (3, 'Running'),
513 # (4, 'Unknown'),
511 # (4, 'Unknown'),
514 # (5, 'Busy')
512 # (5, 'Busy')
515 # )
513 # )
516
514
517 def write_device_mqtt(self):
515 def write_device_mqtt(self):
518
516
519 if self.experiment is None:
517 if self.experiment is None:
520 confs = []
518 confs = []
521 else:
519 else:
522 confs = Configuration.objects.filter(experiment = self.experiment).filter(type=0)
520 confs = Configuration.objects.filter(experiment = self.experiment).filter(type=0)
523 confdds = ''
521 confdds = ''
524 confjars = ''
522 confjars = ''
525 confrc = ''
523 confrc = ''
526 #TO STOP DEVICES: DDS-JARS-RC
524 #TO STOP DEVICES: DDS-JARS-RC
527 for i in range(0,len(confs)):
525 for i in range(0,len(confs)):
528 if i==0:
526 if i==0:
529 for conf in confs:
527 for conf in confs:
530 if conf.device.device_type.name == 'dds':
528 if conf.device.device_type.name == 'dds':
531 confdds = conf
529 confdds = conf
532 confdds.stop_device()
530 confdds.stop_device()
533 break
531 break
534 if i==1:
532 if i==1:
535 for conf in confs:
533 for conf in confs:
536 if conf.device.device_type.name == 'jars':
534 if conf.device.device_type.name == 'jars':
537 confjars = conf
535 confjars = conf
538 confjars.stop_device()
536 confjars.stop_device()
539 break
537 break
540 if i==2:
538 if i==2:
541 for conf in confs:
539 for conf in confs:
542 if conf.device.device_type.name == 'rc':
540 if conf.device.device_type.name == 'rc':
543 confrc = conf
541 confrc = conf
544 confrc.stop_device()
542 confrc.stop_device()
545 break
543 break
546
544
547 apuntes_up_down=''
545 apuntes_up_down=''
548 beams = ABSBeam.objects.filter(abs_conf=self)
546 beams = ABSBeam.objects.filter(abs_conf=self)
549
547
550 inicializacion="{\"beams\":["
548 inicializacion="{\"beams\":["
551 finalizacion="]}"
549 finalizacion="]}"
552
550
553 for beam in beams:
551 for beam in beams:
554 beam.antenna=beam.antenna[1:]
552 beam.antenna=beam.antenna[1:]
555 info="{\"id\":"+str(beam.id)+","+beam.antenna + ","
553 info="{\"id\":"+str(beam.id)+","+beam.antenna + ","
556 apuntes_up_down=apuntes_up_down+info
554 apuntes_up_down=apuntes_up_down+info
557
555
558 apuntes_up_down=apuntes_up_down[:len(apuntes_up_down)-1]
556 apuntes_up_down=apuntes_up_down[:len(apuntes_up_down)-1]
559 apuntes_up_down=inicializacion+ apuntes_up_down+finalizacion
557 apuntes_up_down=inicializacion+ apuntes_up_down+finalizacion
560 mqtt_client.publish(os.environ.get('TOPIC_ABS', 'abs/beams'),apuntes_up_down)
558 mqtt_client.publish(os.environ.get('TOPIC_ABS', 'abs/beams'),apuntes_up_down)
561
559
562 self.active_beam = beams[0].pk
560 self.active_beam = beams[0].pk
563
561
564 #Start DDS-RC-JARS
562 #Start DDS-RC-JARS
565 if confdds:
563 if confdds:
566 confdds.start_device()
564 confdds.start_device()
567 if confrc:
565 if confrc:
568 #print confrc
566 #print confrc
569 confrc.start_device()
567 confrc.start_device()
570 if confjars:
568 if confjars:
571 confjars.start_device()
569 confjars.start_device()
572
570
573 self.device.status = 3
571 self.device.status = 3
574 self.save()
572 self.save()
575
573
576 conf_active, __ = ABSActive.objects.get_or_create(pk=1)
574 conf_active, __ = ABSActive.objects.get_or_create(pk=1)
577 conf_active.conf = self
575 conf_active.conf = self
578 conf_active.save()
576 conf_active.save()
579 return True
577 return True
580
578
581 def read_module(self, module):
579 def read_module(self, module):
582
580
583 """
581 """
584 Read out-bits (up-down) of 1 abs module NOT for Configuration
582 Read out-bits (up-down) of 1 abs module NOT for Configuration
585 """
583 """
586
584
587 ip_address = self.device.ip_address
585 ip_address = self.device.ip_address
588 ip_address = ip_address.split('.')
586 ip_address = ip_address.split('.')
589 module_seq = (ip_address[0],ip_address[1],ip_address[2])
587 module_seq = (ip_address[0],ip_address[1],ip_address[2])
590 dot = '.'
588 dot = '.'
591 module_ip = dot.join(module_seq)+'.'+str(module)
589 module_ip = dot.join(module_seq)+'.'+str(module)
592 module_port = self.device.port_address
590 module_port = self.device.port_address
593 read_route = 'http://'+module_ip+':'+str(module_port)+'/read'
591 read_route = 'http://'+module_ip+':'+str(module_port)+'/read'
594
592
595 module_status = json.loads(self.module_status)
593 module_status = json.loads(self.module_status)
596 print(read_route)
594 print(read_route)
597
595
598 module_bits = ''
596 module_bits = ''
599
597
600 try:
598 try:
601 r_read = requests.get(read_route, timeout=0.5)
599 r_read = requests.get(read_route, timeout=0.5)
602 answer = r_read.json()
600 answer = r_read.json()
603 module_bits = answer['allbits']
601 module_bits = answer['allbits']
604 except:
602 except:
605 return {}
603 return {}
606
604
607 return module_bits
605 return module_bits
608
606
609 def read_device(self):
607 def read_device(self):
610
608
611 parms = {}
609 parms = {}
612 # Reads active modules.
610 # Reads active modules.
613 module_status = json.loads(self.module_status)
611 module_status = json.loads(self.module_status)
614 total = 0
612 total = 0
615 for status in module_status:
613 for status in module_status:
616 if module_status[status] != 0:
614 if module_status[status] != 0:
617 module_bits = self.read_module(int(status))
615 module_bits = self.read_module(int(status))
618 bits={}
616 bits={}
619 if module_bits:
617 if module_bits:
620 bits = (str(module_bits['um2']) + str(module_bits['um1']) + str(module_bits['um0']) +
618 bits = (str(module_bits['um2']) + str(module_bits['um1']) + str(module_bits['um0']) +
621 str(module_bits['dm2']) + str(module_bits['dm1']) + str(module_bits['dm0']) )
619 str(module_bits['dm2']) + str(module_bits['dm1']) + str(module_bits['dm0']) )
622 parms[str(status)] = bits
620 parms[str(status)] = bits
623
621
624 total +=1
622 total +=1
625
623
626 if total==0:
624 if total==0:
627 self.message = "No ABS Module detected. Please select 'Status'."
625 self.message = "No ABS Module detected. Please select 'Status'."
628 return False
626 return False
629
627
630
628
631
629
632 self.message = "ABS Modules have been read"
630 self.message = "ABS Modules have been read"
633 #monitoreo_tx = JROABSClnt_01CeCnMod000000MNTR10
631 #monitoreo_tx = JROABSClnt_01CeCnMod000000MNTR10
634 return parms
632 return parms
635
633
636
634
637 def connected_modules(self):
635 def connected_modules(self):
638 """
636 """
639 This function returns the number of connected abs-modules without updating.
637 This function returns the number of connected abs-modules without updating.
640 """
638 """
641 num = 0
639 num = 0
642 print(self.module_status)
640 print(self.module_status)
643 for i, status in enumerate(self.module_status):
641 for i, status in enumerate(self.module_status):
644 if status != '0':
642 if status != '0':
645 num += 1
643 num += 1
646 #print('status {}:{}'.format(i+1, status))
644 #print('status {}:{}'.format(i+1, status))
647 return num
645 return num
648
646
649 def send_multicast(self, message):
647 def send_multicast(self, message):
650 #print("Send multicast")
648 #print("Send multicast")
651 multicast_group = ('224.3.29.71', 10000)
649 multicast_group = ('224.3.29.71', 10000)
652 # Create the datagram socket
650 # Create the datagram socket
653 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
651 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
654 sock.settimeout(1)
652 sock.settimeout(1)
655 local_ip = os.environ.get('LOCAL_IP', '0.0.0.0')
653 local_ip = os.environ.get('LOCAL_IP', '0.0.0.0')
656 local_ip = '0.0.0.0'
654 local_ip = '0.0.0.0'
657 print("He llegado a IP local")
655 print("He llegado a IP local")
658
656
659 sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, socket.inet_aton(local_ip))
657 sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, socket.inet_aton(local_ip))
660 sock.sendto(message.encode(), multicast_group)
658 sock.sendto(message.encode(), multicast_group)
661 print('Sending ' + message)
659 print('Sending ' + message)
662 return sock
660 return sock
663
661
664 def status_device(self):
662 def status_device(self):
665 """
663 """
666 This function returns the status of all abs-modules as one.
664 This function returns the status of all abs-modules as one.
667 If at least one module is connected, its answer is "1"
665 If at least one module is connected, its answer is "1"
668 """
666 """
669 print ('Status device')
667 print ('Status device')
670 print (self.active_beam)
668 print (self.active_beam)
671 beams = ABSBeam.objects.filter(abs_conf=self)
669 beams = ABSBeam.objects.filter(abs_conf=self)
672 #print beams[self.active_beam-1].module_6bits(0)
670 #print beams[self.active_beam-1].module_6bits(0)
673 active = ABSActive.objects.get(pk=1)
671 active = ABSActive.objects.get(pk=1)
674 if active.conf != self:
672 if active.conf != self:
675 self.message = 'La configuracion actual es la del siguiente enlace %s.' % active.conf.get_absolute_url()
673 self.message = 'La configuracion actual es la del siguiente enlace %s.' % active.conf.get_absolute_url()
676 self.message += "\n"
674 self.message += "\n"
677 self.message += 'Se debe realizar un write en esta configuracion para luego obtener un status valido.'
675 self.message += 'Se debe realizar un write en esta configuracion para luego obtener un status valido.'
678
676
679 return False
677 return False
680
678
681 sock = self.send_multicast('MNTR')
679 sock = self.send_multicast('MNTR')
682 print("Estoy en status_deice",flush=True)
680 # print("Estoy en status_deice",flush=True)
683
681
684 n = 0
682 n = 0
685 status = ['0'] * 64
683 status = ['0'] * 64
686
684
687 while True:
685 while True:
688 #for i in range(32):
686 #for i in range(32):
689 #if True:
687 #if True:
690 try:
688 try:
691 print("Recibiendo")
689 print("Recibiendo")
692 address = None
690 address = None
693 data, address = sock.recvfrom(2)
691 data, address = sock.recvfrom(2)
694 print (address, data)
692 print (address, data)
695 print("!!!!")
693 print("!!!!")
696 data = data.decode()
694 data = data.decode()
697 aux_mon = "1"
695 aux_mon = "1"
698 aux_expected = aux_mon
696 aux_expected = aux_mon
699 if(len(data)==2):
697 if(len(data)==2):
700 print ("data[1]: ")
698 print ("data[1]: ")
701 print (data[1])
699 print (data[1])
702 aux_mon = fromChar2Binary(data[1])
700 aux_mon = fromChar2Binary(data[1])
703 print (aux_mon)
701 print (aux_mon)
704 aux_i = (str(address[0]).split('.'))[3]
702 aux_i = (str(address[0]).split('.'))[3]
705 print (aux_i)
703 print (aux_i)
706 print ('Active beam')
704 print ('Active beam')
707 beam_active = ABSBeam.objects.get(pk=self.active_beam)
705 beam_active = ABSBeam.objects.get(pk=self.active_beam)
708 print (beam_active)
706 print (beam_active)
709 aux_expected = beam_active.module_6bits(int(aux_i)-1)
707 aux_expected = beam_active.module_6bits(int(aux_i)-1)
710 print (aux_expected)
708 print (aux_expected)
711
709
712 print ("data[0]: ")
710 print ("data[0]: ")
713 print (data[0])
711 print (data[0])
714
712
715 if data[0] == '1':
713 if data[0] == '1':
716 status[int(address[0][10:])-1] = '3'
714 status[int(address[0][10:])-1] = '3'
717 if aux_mon == aux_expected:
715 if aux_mon == aux_expected:
718 print ('Es igual')
716 print ('Es igual')
719 else:
717 else:
720 print ('Es diferente')
718 print ('Es diferente')
721 status[int(address[0][10:])-1] = '2'
719 status[int(address[0][10:])-1] = '2'
722
720
723 elif data[0] == '0':
721 elif data[0] == '0':
724 status[int(address[0][10:])-1] = '1'
722 status[int(address[0][10:])-1] = '1'
725 n += 1
723 n += 1
726 print('Module: {} connected'.format(address))
724 print('Module: {} connected'.format(address))
727 except socket.timeout:
725 except socket.timeout:
728 print('Timeout')
726 print('Timeout')
729 break
727 break
730 except:
728 except:
731 print('Module: {} error'.format(address))
729 print('Module: {} error'.format(address))
732 pass
730 pass
733
731
734 sock.close()
732 sock.close()
735
733
736 if n > 0:
734 if n > 0:
737 self.message = 'ABS modules Status have been updated.'
735 self.message = 'ABS modules Status have been updated.'
738 self.device.status = 1
736 self.device.status = 1
739 else:
737 else:
740 self.device.status = 0
738 self.device.status = 0
741 self.message = 'No ABS module is connected.'
739 self.message = 'No ABS module is connected.'
742 self.module_status = ''.join(status)
740 self.module_status = ''.join(status)
743 self.save()
741 self.save()
744
742
745 return self.device.status
743 return self.device.status
746
744
747
745
748 def send_beam(self, beam_pos):
746 def send_beam(self, beam_pos):
749 """
747 """
750 This function connects to a multicast group and sends the beam number
748 This function connects to a multicast group and sends the beam number
751 to all abs modules.
749 to all abs modules.
752 """
750 """
753 print ('Send beam')
751 print ('Send beam')
754 print (self.active_beam)
752 print (self.active_beam)
755 beams = ABSBeam.objects.filter(abs_conf=self)
753 beams = ABSBeam.objects.filter(abs_conf=self)
756 #print beams[self.active_beam-1].module_6bits(0)
754 #print beams[self.active_beam-1].module_6bits(0)
757 active = ABSActive.objects.get(pk=1)
755 active = ABSActive.objects.get(pk=1)
758 if active.conf != self:
756 if active.conf != self:
759 self.message = 'La configuracion actual es la del siguiente enlace %s.' % active.conf.get_absolute_url()
757 self.message = 'La configuracion actual es la del siguiente enlace %s.' % active.conf.get_absolute_url()
760 self.message += "\n"
758 self.message += "\n"
761 self.message += 'Se debe realizar un write en esta configuracion para luego obtener un status valido.'
759 self.message += 'Se debe realizar un write en esta configuracion para luego obtener un status valido.'
762
760
763 return False
761 return False
764
762
765 # Se manda a cero RC para poder realizar cambio de beam
763 # Se manda a cero RC para poder realizar cambio de beam
766 if self.experiment is None:
764 if self.experiment is None:
767 confs = []
765 confs = []
768 else:
766 else:
769 confs = Configuration.objects.filter(experiment = self.experiment).filter(type=0)
767 confs = Configuration.objects.filter(experiment = self.experiment).filter(type=0)
770 confdds = ''
768 confdds = ''
771 confjars = ''
769 confjars = ''
772 confrc = ''
770 confrc = ''
773 #TO STOP DEVICES: DDS-JARS-RC
771 #TO STOP DEVICES: DDS-JARS-RC
774 for i in range(0,len(confs)):
772 for i in range(0,len(confs)):
775 if i==0:
773 if i==0:
776 for conf in confs:
774 for conf in confs:
777 if conf.device.device_type.name == 'dds':
775 if conf.device.device_type.name == 'dds':
778 confdds = conf
776 confdds = conf
779 confdds.stop_device()
777 confdds.stop_device()
780 break
778 break
781 if i==1:
779 if i==1:
782 for conf in confs:
780 for conf in confs:
783 if conf.device.device_type.name == 'jars':
781 if conf.device.device_type.name == 'jars':
784 confjars = conf
782 confjars = conf
785 confjars.stop_device()
783 confjars.stop_device()
786 break
784 break
787 if i==2:
785 if i==2:
788 for conf in confs:
786 for conf in confs:
789 if conf.device.device_type.name == 'rc':
787 if conf.device.device_type.name == 'rc':
790 confrc = conf
788 confrc = conf
791 confrc.stop_device()
789 confrc.stop_device()
792 break
790 break
793 if beam_pos > 0:
791 if beam_pos > 0:
794 beam_pos = beam_pos - 1
792 beam_pos = beam_pos - 1
795 else:
793 else:
796 beam_pos = 0
794 beam_pos = 0
797
795
798 #El indice del apunte debe ser menor que el numero total de apuntes
796 #El indice del apunte debe ser menor que el numero total de apuntes
799 #El servidor tcp en el embebido comienza a contar desde 0
797 #El servidor tcp en el embebido comienza a contar desde 0
800 status = ['0'] * 64
798 status = ['0'] * 64
801 message = 'CHGB{}'.format(beam_pos)
799 message = 'CHGB{}'.format(beam_pos)
802 sock = self.send_multicast(message)
800 sock = self.send_multicast(message)
803 print("Estoy en send_beam ",flush=True)
801 print("Estoy en send_beam ",flush=True)
804 while True:
802 while True:
805 #for i in range(32):
803 #for i in range(32):
806 try:
804 try:
807 data, address = sock.recvfrom(1024)
805 data, address = sock.recvfrom(1024)
808 print (address, data)
806 print (address, data)
809 data = data.decode()
807 data = data.decode()
810 if data == '1':
808 if data == '1':
811 status[int(address[0][10:])-1] = '3'
809 status[int(address[0][10:])-1] = '3'
812 elif data == '0':
810 elif data == '0':
813 status[int(address[0][10:])-1] = '1'
811 status[int(address[0][10:])-1] = '1'
814 except socket.timeout:
812 except socket.timeout:
815 print('Timeout')
813 print('Timeout')
816 break
814 break
817 except Exception as e:
815 except Exception as e:
818 print ('Error {}'.format(e))
816 print ('Error {}'.format(e))
819 pass
817 pass
820
818
821 sock.close()
819 sock.close()
822
820
823 #Start DDS-RC-JARS
821 #Start DDS-RC-JARS
824 if confdds:
822 if confdds:
825 confdds.start_device()
823 confdds.start_device()
826 if confrc:
824 if confrc:
827 #print confrc
825 #print confrc
828 confrc.start_device()
826 confrc.start_device()
829 if confjars:
827 if confjars:
830 confjars.start_device()
828 confjars.start_device()
831
829
832 self.message = "ABS Beam has been changed"
830 self.message = "ABS Beam has been changed"
833 self.module_status = ''.join(status)
831 self.module_status = ''.join(status)
834 self.save()
832 self.save()
835 return True
833 return True
836
834
837 def change_beam_mqtt(self, beam_pos):
835 def change_beam_mqtt(self, beam_pos):
838 """
836 """
839 This function connects send through mqtt the order of change_beam (id of beam).
837 This function connects send through mqtt the order of change_beam (id of beam).
840 """
838 """
841 print ('Change beam MQTT')
839 print ('Change beam MQTT')
842 print (self.active_beam)
840 print (self.active_beam)
843 beams = ABSBeam.objects.filter(abs_conf=self)
841 beams = ABSBeam.objects.filter(abs_conf=self)
844 #print beams[self.active_beam-1].module_6bits(0)
842 #print beams[self.active_beam-1].module_6bits(0)
845 active = ABSActive.objects.get(pk=1)
843 active = ABSActive.objects.get(pk=1)
846 if active.conf != self:
844 if active.conf != self:
847 self.message = 'La configuracion actual es la del siguiente enlace %s.' % active.conf.get_absolute_mqtt_url()
845 self.message = 'La configuracion actual es la del siguiente enlace %s.' % active.conf.get_absolute_mqtt_url()
848 self.message += "\n"
846 self.message += "\n"
849 self.message += 'Se debe realizar un write en esta configuracion para luego obtener un status valido.'
847 self.message += 'Se debe realizar un write en esta configuracion para luego obtener un status valido.'
850
848
851 return False
849 return False
852
850
853 # Se manda a cero RC para poder realizar cambio de beam
851 # Se manda a cero RC para poder realizar cambio de beam
854 if self.experiment is None:
852 if self.experiment is None:
855 confs = []
853 confs = []
856 else:
854 else:
857 confs = Configuration.objects.filter(experiment = self.experiment).filter(type=0)
855 confs = Configuration.objects.filter(experiment = self.experiment).filter(type=0)
858 confdds = ''
856 confdds = ''
859 confjars = ''
857 confjars = ''
860 confrc = ''
858 confrc = ''
861 #TO STOP DEVICES: DDS-JARS-RC
859 #TO STOP DEVICES: DDS-JARS-RC
862 for i in range(0,len(confs)):
860 for i in range(0,len(confs)):
863 if i==0:
861 if i==0:
864 for conf in confs:
862 for conf in confs:
865 if conf.device.device_type.name == 'dds':
863 if conf.device.device_type.name == 'dds':
866 confdds = conf
864 confdds = conf
867 confdds.stop_device()
865 confdds.stop_device()
868 break
866 break
869 if i==1:
867 if i==1:
870 for conf in confs:
868 for conf in confs:
871 if conf.device.device_type.name == 'jars':
869 if conf.device.device_type.name == 'jars':
872 confjars = conf
870 confjars = conf
873 confjars.stop_device()
871 confjars.stop_device()
874 break
872 break
875 if i==2:
873 if i==2:
876 for conf in confs:
874 for conf in confs:
877 if conf.device.device_type.name == 'rc':
875 if conf.device.device_type.name == 'rc':
878 confrc = conf
876 confrc = conf
879 confrc.stop_device()
877 confrc.stop_device()
880 break
878 break
881 if beam_pos > 0:
879 if beam_pos > 0:
882 beam_pos = beam_pos - 1
880 beam_pos = beam_pos - 1
883 else:
881 else:
884 beam_pos = 0
882 beam_pos = 0
885
883
886 #El indice del apunte debe ser menor que el numero total de apuntes
887 #El servidor tcp en el embebido comienza a contar desde 0
888 # status = ['0'] * 64
889 # message = 'CHGB{}'.format(beam_pos)
890 # sock = self.send_multicast(message)
891 # while True:
892 # #for i in range(32):
893 # try:
894 # data, address = sock.recvfrom(1024)
895 # print (address, data)
896 # data = data.decode()
897 # if data == '1':
898 # status[int(address[0][10:])-1] = '3'
899 # elif data == '0':
900 # status[int(address[0][10:])-1] = '1'
901 # except socket.timeout:
902 # print('Timeout')
903 # break
904 # except Exception as e:
905 # print ('Error {}'.format(e))
906 # pass
907
908 # sock.close()
909 mqtt_client.publish('abs/change_beam',str(beam_pos))
884 mqtt_client.publish('abs/change_beam',str(beam_pos))
910
885
911
886
912 #Start DDS-RC-JARS
887 #Start DDS-RC-JARS
913 if confdds:
888 if confdds:
914 confdds.start_device()
889 confdds.start_device()
915 if confrc:
890 if confrc:
916 #print confrc
917 confrc.start_device()
891 confrc.start_device()
918 if confjars:
892 if confjars:
919 confjars.start_device()
893 confjars.start_device()
920
894
921 self.message = "ABS Beam has been changed"
895 self.message = "ABS Beam has been changed"
922 self.save()
896 self.save()
923 return True
897 return True
924
898
925 def get_absolute_url_import(self):
899 def get_absolute_url_import(self):
926 return reverse('url_import_abs_conf', args=[str(self.id)])
900 return reverse('url_import_abs_conf', args=[str(self.id)])
927
901
928 class ABSActive(models.Model):
902 class ABSActive(models.Model):
929 conf = models.ForeignKey(ABSConfiguration, null=True, verbose_name='ABS Configuration', on_delete=models.CASCADE)
903 conf = models.ForeignKey(ABSConfiguration, null=True, verbose_name='ABS Configuration', on_delete=models.CASCADE)
930
904
931 class Meta:
905 class Meta:
932 db_table = 'abs_absactive'
906 db_table = 'abs_absactive'
933
907
934 class ABSBeam(models.Model):
908 class ABSBeam(models.Model):
935
909
936 name = models.CharField(max_length=60, default='Beam')
910 name = models.CharField(max_length=60, default='Beam')
937 antenna = models.CharField(verbose_name='Antenna', max_length=1000, default=antenna_default)
911 antenna = models.CharField(verbose_name='Antenna', max_length=1000, default=antenna_default)
938 abs_conf = models.ForeignKey('ABSConfiguration', null=True,
912 abs_conf = models.ForeignKey('ABSConfiguration', null=True,
939 verbose_name='ABS Configuration', on_delete=models.CASCADE)
913 verbose_name='ABS Configuration', on_delete=models.CASCADE)
940 tx = models.CharField(verbose_name='Tx', max_length=1000, default=tx_default)
914 tx = models.CharField(verbose_name='Tx', max_length=1000, default=tx_default)
941 rx = models.CharField(verbose_name='Rx', max_length=1000, default=rx_default)
915 rx = models.CharField(verbose_name='Rx', max_length=1000, default=rx_default)
942 s_time = models.TimeField(verbose_name='Star Time', default='00:00:00')
916 s_time = models.TimeField(verbose_name='Star Time', default='00:00:00')
943 e_time = models.TimeField(verbose_name='End Time', default='23:59:59')
917 e_time = models.TimeField(verbose_name='End Time', default='23:59:59')
944 ues = models.CharField(verbose_name='Ues', max_length=100, default=ues_default)
918 ues = models.CharField(verbose_name='Ues', max_length=100, default=ues_default)
945 only_rx = models.CharField(verbose_name='Only RX', max_length=40, default=onlyrx_default)
919 only_rx = models.CharField(verbose_name='Only RX', max_length=40, default=onlyrx_default)
946
920
947 class Meta:
921 class Meta:
948 db_table = 'abs_beams'
922 db_table = 'abs_beams'
949
923
950 def __unicode__(self):
924 def __unicode__(self):
951 return u'%s' % (self.name)
925 return u'%s' % (self.name)
952
926
953 def parms_to_dict(self):
927 def parms_to_dict(self):
954
928
955 parameters = {}
929 parameters = {}
956 parameters['name'] = self.name
930 parameters['name'] = self.name
957 parameters['antenna'] = ast.literal_eval(self.antenna)
931 parameters['antenna'] = ast.literal_eval(self.antenna)
958 parameters['abs_conf'] = self.abs_conf.name
932 parameters['abs_conf'] = self.abs_conf.name
959 parameters['tx'] = ast.literal_eval(self.tx)
933 parameters['tx'] = ast.literal_eval(self.tx)
960 parameters['rx'] = ast.literal_eval(self.rx)
934 parameters['rx'] = ast.literal_eval(self.rx)
961 parameters['s_time'] = self.s_time.strftime("%H:%M:%S")
935 parameters['s_time'] = self.s_time.strftime("%H:%M:%S")
962 parameters['e_time'] = self.e_time.strftime("%H:%M:%S")
936 parameters['e_time'] = self.e_time.strftime("%H:%M:%S")
963 parameters['ues'] = ast.literal_eval(self.ues)
937 parameters['ues'] = ast.literal_eval(self.ues)
964 parameters['only_rx'] = json.loads(self.only_rx)
938 parameters['only_rx'] = json.loads(self.only_rx)
965
939
966 return parameters
940 return parameters
967
941
968 def dict_to_parms(self, parameters):
942 def dict_to_parms(self, parameters):
969
943
970 self.name = parameters['name']
944 self.name = parameters['name']
971 self.antenna = json.dumps(parameters['antenna'])
945 self.antenna = json.dumps(parameters['antenna'])
972 #self.abs_conf = parameters['abs_conf']
946 #self.abs_conf = parameters['abs_conf']
973 self.tx = json.dumps(parameters['tx'])
947 self.tx = json.dumps(parameters['tx'])
974 self.rx = json.dumps(parameters['rx'])
948 self.rx = json.dumps(parameters['rx'])
975 #self.s_time = parameters['s_time']
949 #self.s_time = parameters['s_time']
976 #self.e_time = parameters['e_time']
950 #self.e_time = parameters['e_time']
977 self.ues = json.dumps(parameters['ues'])
951 self.ues = json.dumps(parameters['ues'])
978 self.only_rx = json.dumps(parameters['only_rx'])
952 self.only_rx = json.dumps(parameters['only_rx'])
979 self.save()
953 self.save()
980
954
981
955
982 def clone(self, **kwargs):
956 def clone(self, **kwargs):
983
957
984 self.pk = None
958 self.pk = None
985 self.id = None
959 self.id = None
986 for attr, value in kwargs.items():
960 for attr, value in kwargs.items():
987 setattr(self, attr, value)
961 setattr(self, attr, value)
988
962
989 self.save()
963 self.save()
990
964
991 return self
965 return self
992
966
993
967
994 def module_6bits(self, module):
968 def module_6bits(self, module):
995 """
969 """
996 This function reads antenna pattern and choose 6bits (upbits-downbits) for one abs module
970 This function reads antenna pattern and choose 6bits (upbits-downbits) for one abs module
997 """
971 """
998 module += 1
972 module += 1
999 if module > 64:
973 if module > 64:
1000 beam_bits = ""
974 beam_bits = ""
1001 return beam_bits
975 return beam_bits
1002
976
1003 data = ast.literal_eval(self.antenna)
977 data = ast.literal_eval(self.antenna)
1004 up_data = data['antenna_up']
978 up_data = data['antenna_up']
1005 down_data = data['antenna_down']
979 down_data = data['antenna_down']
1006
980
1007 pos = ip2position(module)
981 pos = ip2position(module)
1008 up_value = up_data[pos[0]][pos[1]]
982 up_value = up_data[pos[0]][pos[1]]
1009 down_value = down_data[pos[0]][pos[1]]
983 down_value = down_data[pos[0]][pos[1]]
1010
984
1011 up_bits = up_conv_bits(up_value)
985 up_bits = up_conv_bits(up_value)
1012 down_bits = down_conv_bits(down_value)
986 down_bits = down_conv_bits(down_value)
1013 beam_bits = up_bits+down_bits
987 beam_bits = up_bits+down_bits
1014
988
1015 return beam_bits
989 return beam_bits
1016
990
1017
991
1018 @property
992 @property
1019 def get_upvalues(self):
993 def get_upvalues(self):
1020 """
994 """
1021 This function reads antenna pattern and show the up-value of one abs module
995 This function reads antenna pattern and show the up-value of one abs module
1022 """
996 """
1023
997
1024 data = ast.literal_eval(self.antenna)
998 data = ast.literal_eval(self.antenna)
1025 up_data = data['antenna_up']
999 up_data = data['antenna_up']
1026
1000
1027 up_values = []
1001 up_values = []
1028 for data in up_data:
1002 for data in up_data:
1029 for i in range(0,8):
1003 for i in range(0,8):
1030 up_values.append(data[i])
1004 up_values.append(data[i])
1031
1005
1032 return up_values
1006 return up_values
1033
1007
1034 @property
1008 @property
1035 def antenna_upvalues(self):
1009 def antenna_upvalues(self):
1036 """
1010 """
1037 This function reads antenna pattern and show the up - values of one abs beam
1011 This function reads antenna pattern and show the up - values of one abs beam
1038 in a particular order
1012 in a particular order
1039 """
1013 """
1040 data = ast.literal_eval(self.antenna)
1014 data = ast.literal_eval(self.antenna)
1041 up_data = data['antenna_up']
1015 up_data = data['antenna_up']
1042
1016
1043 return up_data
1017 return up_data
1044
1018
1045 @property
1019 @property
1046 def antenna_downvalues(self):
1020 def antenna_downvalues(self):
1047 """
1021 """
1048 This function reads antenna pattern and show the down - values of one abs beam
1022 This function reads antenna pattern and show the down - values of one abs beam
1049 in a particular order
1023 in a particular order
1050 """
1024 """
1051 data = ast.literal_eval(self.antenna)
1025 data = ast.literal_eval(self.antenna)
1052 down_data = data['antenna_down']
1026 down_data = data['antenna_down']
1053
1027
1054 return down_data
1028 return down_data
1055
1029
1056 @property
1030 @property
1057 def get_downvalues(self):
1031 def get_downvalues(self):
1058 """
1032 """
1059 This function reads antenna pattern and show the down-value of one abs module
1033 This function reads antenna pattern and show the down-value of one abs module
1060 """
1034 """
1061
1035
1062 data = ast.literal_eval(self.antenna)
1036 data = ast.literal_eval(self.antenna)
1063 down_data = data['antenna_down']
1037 down_data = data['antenna_down']
1064
1038
1065 down_values = []
1039 down_values = []
1066 for data in down_data:
1040 for data in down_data:
1067 for i in range(0,8):
1041 for i in range(0,8):
1068 down_values.append(data[i])
1042 down_values.append(data[i])
1069
1043
1070 return down_values
1044 return down_values
1071
1045
1072 @property
1046 @property
1073 def get_up_ues(self):
1047 def get_up_ues(self):
1074 """
1048 """
1075 This function shows the up-ues-value of one beam
1049 This function shows the up-ues-value of one beam
1076 """
1050 """
1077 data = ast.literal_eval(self.ues)
1051 data = ast.literal_eval(self.ues)
1078 up_ues = data['up']
1052 up_ues = data['up']
1079
1053
1080 return up_ues
1054 return up_ues
1081
1055
1082 @property
1056 @property
1083 def get_down_ues(self):
1057 def get_down_ues(self):
1084 """
1058 """
1085 This function shows the down-ues-value of one beam
1059 This function shows the down-ues-value of one beam
1086 """
1060 """
1087 data = ast.literal_eval(self.ues)
1061 data = ast.literal_eval(self.ues)
1088 down_ues = data['down']
1062 down_ues = data['down']
1089
1063
1090 return down_ues
1064 return down_ues
1091
1065
1092 @property
1066 @property
1093 def get_up_onlyrx(self):
1067 def get_up_onlyrx(self):
1094 """
1068 """
1095 This function shows the up-onlyrx-value of one beam
1069 This function shows the up-onlyrx-value of one beam
1096 """
1070 """
1097 data = json.loads(self.only_rx)
1071 data = json.loads(self.only_rx)
1098 up_onlyrx = data['up']
1072 up_onlyrx = data['up']
1099
1073
1100 return up_onlyrx
1074 return up_onlyrx
1101
1075
1102 @property
1076 @property
1103 def get_down_onlyrx(self):
1077 def get_down_onlyrx(self):
1104 """
1078 """
1105 This function shows the down-onlyrx-value of one beam
1079 This function shows the down-onlyrx-value of one beam
1106 """
1080 """
1107 data = json.loads(self.only_rx)
1081 data = json.loads(self.only_rx)
1108 down_onlyrx = data['down']
1082 down_onlyrx = data['down']
1109
1083
1110 return down_onlyrx
1084 return down_onlyrx
1111
1085
1112 @property
1086 @property
1113 def get_tx(self):
1087 def get_tx(self):
1114 """
1088 """
1115 This function shows the tx-values of one beam
1089 This function shows the tx-values of one beam
1116 """
1090 """
1117 data = json.loads(self.tx)
1091 data = json.loads(self.tx)
1118
1092
1119 return data
1093 return data
1120
1094
1121 @property
1095 @property
1122 def get_uptx(self):
1096 def get_uptx(self):
1123 """
1097 """
1124 This function shows the up-tx-values of one beam
1098 This function shows the up-tx-values of one beam
1125 """
1099 """
1126 data = json.loads(self.tx)
1100 data = json.loads(self.tx)
1127 up_data = data['up']
1101 up_data = data['up']
1128
1102
1129 up_values = []
1103 up_values = []
1130 for data in up_data:
1104 for data in up_data:
1131 for i in range(0,8):
1105 for i in range(0,8):
1132 up_values.append(data[i])
1106 up_values.append(data[i])
1133
1107
1134 return up_values
1108 return up_values
1135
1109
1136 @property
1110 @property
1137 def get_downtx(self):
1111 def get_downtx(self):
1138 """
1112 """
1139 This function shows the down-tx-values of one beam
1113 This function shows the down-tx-values of one beam
1140 """
1114 """
1141 data = json.loads(self.tx)
1115 data = json.loads(self.tx)
1142 down_data = data['down']
1116 down_data = data['down']
1143
1117
1144 down_values = []
1118 down_values = []
1145 for data in down_data:
1119 for data in down_data:
1146 for i in range(0,8):
1120 for i in range(0,8):
1147 down_values.append(data[i])
1121 down_values.append(data[i])
1148
1122
1149 return down_values
1123 return down_values
1150
1124
1151
1125
1152
1126
1153 @property
1127 @property
1154 def get_rx(self):
1128 def get_rx(self):
1155 """
1129 """
1156 This function shows the rx-values of one beam
1130 This function shows the rx-values of one beam
1157 """
1131 """
1158 data = json.loads(self.rx)
1132 data = json.loads(self.rx)
1159
1133
1160 return data
1134 return data
1161
1135
1162 @property
1136 @property
1163 def get_uprx(self):
1137 def get_uprx(self):
1164 """
1138 """
1165 This function shows the up-rx-values of one beam
1139 This function shows the up-rx-values of one beam
1166 """
1140 """
1167 data = json.loads(self.rx)
1141 data = json.loads(self.rx)
1168 up_data = data['up']
1142 up_data = data['up']
1169
1143
1170 up_values = []
1144 up_values = []
1171 for data in up_data:
1145 for data in up_data:
1172 for i in range(0,8):
1146 for i in range(0,8):
1173 up_values.append(data[i])
1147 up_values.append(data[i])
1174
1148
1175 return up_values
1149 return up_values
1176
1150
1177 @property
1151 @property
1178 def get_downrx(self):
1152 def get_downrx(self):
1179 """
1153 """
1180 This function shows the down-rx-values of one beam
1154 This function shows the down-rx-values of one beam
1181 """
1155 """
1182 data = json.loads(self.rx)
1156 data = json.loads(self.rx)
1183 down_data = data['down']
1157 down_data = data['down']
1184
1158
1185 down_values = []
1159 down_values = []
1186 for data in down_data:
1160 for data in down_data:
1187 for i in range(0,8):
1161 for i in range(0,8):
1188 down_values.append(data[i])
1162 down_values.append(data[i])
1189
1163
1190 return down_values
1164 return down_values
@@ -1,574 +1,574
1 from django.shortcuts import render
1 from django.shortcuts import render
2 from django.template import RequestContext
2 from django.template import RequestContext
3 from django.shortcuts import redirect, render, get_object_or_404
3 from django.shortcuts import redirect, render, get_object_or_404
4 from django.contrib import messages
4 from django.contrib import messages
5 from django.conf import settings
5 from django.conf import settings
6 from django.http import HttpResponse
6 from django.http import HttpResponse
7 from django.urls import reverse
7 from django.urls import reverse
8 from django.views.decorators.csrf import csrf_exempt
8 from django.views.decorators.csrf import csrf_exempt
9 from django.utils.safestring import mark_safe
9 from django.utils.safestring import mark_safe
10
10
11 from datetime import datetime
11 from datetime import datetime
12 from time import sleep
12 from time import sleep
13 import os
13 import os
14 import io
14 import io
15
15
16 from apps.main.models import Device, Configuration, Experiment
16 from apps.main.models import Device, Configuration, Experiment
17 from apps.main.views import sidebar
17 from apps.main.views import sidebar
18
18
19 from .models import ABSConfiguration, ABSBeam
19 from .models import ABSConfiguration, ABSBeam
20 from .forms import ABSConfigurationForm, ABSBeamEditForm, ABSBeamAddForm, ABSImportForm
20 from .forms import ABSConfigurationForm, ABSBeamEditForm, ABSBeamAddForm, ABSImportForm
21
21
22 from .utils.overJroShow import overJroShow
22 from .utils.overJroShow import overJroShow
23 #from .utils.OverJRO import OverJRO
23 #from .utils.OverJRO import OverJRO
24 #Create your views here.
24 #Create your views here.
25 import json, ast
25 import json, ast
26
26
27 from .mqtt import client as mqtt_client
27 from .mqtt import client as mqtt_client
28 from radarsys.socketconfig import sio as sio
28 from radarsys.socketconfig import sio as sio
29
29
30 def get_values_from_form(form_data):
30 def get_values_from_form(form_data):
31
31
32 sublistup = []
32 sublistup = []
33 sublistdown = []
33 sublistdown = []
34 subtxlistup = []
34 subtxlistup = []
35 subtxlistdown = []
35 subtxlistdown = []
36 subrxlistup = []
36 subrxlistup = []
37 subrxlistdown = []
37 subrxlistdown = []
38
38
39 up_values_list = []
39 up_values_list = []
40 down_values_list = []
40 down_values_list = []
41 up_txvalues_list = []
41 up_txvalues_list = []
42 down_txvalues_list = []
42 down_txvalues_list = []
43 up_rxvalues_list = []
43 up_rxvalues_list = []
44 down_rxvalues_list = []
44 down_rxvalues_list = []
45
45
46 values_list = {}
46 values_list = {}
47 cont = 1
47 cont = 1
48
48
49 for i in range(1,65):
49 for i in range(1,65):
50 x = float(form_data['abs_up'+str(i)])
50 x = float(form_data['abs_up'+str(i)])
51 y = float(form_data['abs_down'+str(i)])
51 y = float(form_data['abs_down'+str(i)])
52 sublistup.append(x)
52 sublistup.append(x)
53 sublistdown.append(y)
53 sublistdown.append(y)
54
54
55 if str(i) in form_data.getlist('uptx_checks'):
55 if str(i) in form_data.getlist('uptx_checks'):
56 subtxlistup.append(1)
56 subtxlistup.append(1)
57 else:
57 else:
58 subtxlistup.append(0)
58 subtxlistup.append(0)
59 if str(i) in form_data.getlist('downtx_checks'):
59 if str(i) in form_data.getlist('downtx_checks'):
60 subtxlistdown.append(1)
60 subtxlistdown.append(1)
61 else:
61 else:
62 subtxlistdown.append(0)
62 subtxlistdown.append(0)
63
63
64 if str(i) in form_data.getlist('uprx_checks'):
64 if str(i) in form_data.getlist('uprx_checks'):
65 subrxlistup.append(1)
65 subrxlistup.append(1)
66 else:
66 else:
67 subrxlistup.append(0)
67 subrxlistup.append(0)
68 if str(i) in form_data.getlist('downrx_checks'):
68 if str(i) in form_data.getlist('downrx_checks'):
69 subrxlistdown.append(1)
69 subrxlistdown.append(1)
70 else:
70 else:
71 subrxlistdown.append(0)
71 subrxlistdown.append(0)
72
72
73 cont = cont+1
73 cont = cont+1
74
74
75 if cont == 9:
75 if cont == 9:
76 up_values_list.append(sublistup)
76 up_values_list.append(sublistup)
77 down_values_list.append(sublistdown)
77 down_values_list.append(sublistdown)
78 sublistup = []
78 sublistup = []
79 sublistdown = []
79 sublistdown = []
80
80
81 up_txvalues_list.append(subtxlistup)
81 up_txvalues_list.append(subtxlistup)
82 down_txvalues_list.append(subtxlistdown)
82 down_txvalues_list.append(subtxlistdown)
83 subtxlistup = []
83 subtxlistup = []
84 subtxlistdown = []
84 subtxlistdown = []
85 up_rxvalues_list.append(subrxlistup)
85 up_rxvalues_list.append(subrxlistup)
86 down_rxvalues_list.append(subrxlistdown)
86 down_rxvalues_list.append(subrxlistdown)
87 subrxlistup = []
87 subrxlistup = []
88 subrxlistdown = []
88 subrxlistdown = []
89 cont = 1
89 cont = 1
90
90
91
91
92 list_uesup = []
92 list_uesup = []
93 list_uesdown = []
93 list_uesdown = []
94 for i in range(1,5):
94 for i in range(1,5):
95 if form_data['ues_up'+str(i)] == '':
95 if form_data['ues_up'+str(i)] == '':
96 list_uesup.append(0.0)
96 list_uesup.append(0.0)
97 else:
97 else:
98 list_uesup.append(float(form_data['ues_up'+str(i)]))
98 list_uesup.append(float(form_data['ues_up'+str(i)]))
99
99
100 if form_data['ues_down'+str(i)] == '':
100 if form_data['ues_down'+str(i)] == '':
101 list_uesdown.append(0.0)
101 list_uesdown.append(0.0)
102 else:
102 else:
103 list_uesdown.append(float(form_data['ues_down'+str(i)]))
103 list_uesdown.append(float(form_data['ues_down'+str(i)]))
104
104
105 onlyrx_list = form_data.getlist('onlyrx')
105 onlyrx_list = form_data.getlist('onlyrx')
106 only_rx = {}
106 only_rx = {}
107 if '1' in onlyrx_list:
107 if '1' in onlyrx_list:
108 only_rx['up'] = True
108 only_rx['up'] = True
109 else:
109 else:
110 only_rx['up'] = False
110 only_rx['up'] = False
111 if '2' in onlyrx_list:
111 if '2' in onlyrx_list:
112 only_rx['down'] = True
112 only_rx['down'] = True
113 else:
113 else:
114 only_rx['down'] = False
114 only_rx['down'] = False
115
115
116 antenna = {'antenna_up': up_values_list, 'antenna_down': down_values_list}
116 antenna = {'antenna_up': up_values_list, 'antenna_down': down_values_list}
117 tx = {'up': up_txvalues_list, 'down': down_txvalues_list}
117 tx = {'up': up_txvalues_list, 'down': down_txvalues_list}
118 rx = {'up': up_rxvalues_list, 'down': down_rxvalues_list}
118 rx = {'up': up_rxvalues_list, 'down': down_rxvalues_list}
119 ues = {'up': list_uesup, 'down': list_uesdown}
119 ues = {'up': list_uesup, 'down': list_uesdown}
120 name = str(form_data['beam_name'])
120 name = str(form_data['beam_name'])
121
121
122 beam_data = {'name': name, 'antenna': antenna, 'tx': tx, 'rx': rx, 'ues': ues, 'only_rx': only_rx}
122 beam_data = {'name': name, 'antenna': antenna, 'tx': tx, 'rx': rx, 'ues': ues, 'only_rx': only_rx}
123
123
124 return beam_data
124 return beam_data
125
125
126
126
127 def abs_conf(request, id_conf):
127 def abs_conf(request, id_conf):
128
128
129 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
129 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
130 beams = ABSBeam.objects.filter(abs_conf=conf)
130 beams = ABSBeam.objects.filter(abs_conf=conf)
131 #------------Colors for Active Beam:-------------
131 #------------Colors for Active Beam:-------------
132 all_status = {}
132 all_status = {}
133 module_messages = json.loads(conf.module_messages)
133 module_messages = json.loads(conf.module_messages)
134
134
135 color_status = {}
135 color_status = {}
136 for i, status in enumerate(conf.module_status):
136 for i, status in enumerate(conf.module_status):
137 if status == '3': #Running background-color: #00cc00;
137 if status == '3': #Running background-color: #00cc00;
138 all_status['{}'.format(i+1)] = 2
138 all_status['{}'.format(i+1)] = 2
139 color_status['{}'.format(i+1)] = 'class=text-success'#'bgcolor=#00cc00'
139 color_status['{}'.format(i+1)] = 'class=text-success'#'bgcolor=#00cc00'
140 elif status == '2':
140 elif status == '2':
141 all_status['{}'.format(i+1)] = 1
141 all_status['{}'.format(i+1)] = 1
142 color_status['{}'.format(i+1)] = 'class=text-info'
142 color_status['{}'.format(i+1)] = 'class=text-info'
143 elif status == '1': #Connected background-color: #ee902c;
143 elif status == '1': #Connected background-color: #ee902c;
144 all_status['{}'.format(i+1)] = 1
144 all_status['{}'.format(i+1)] = 1
145 color_status['{}'.format(i+1)] = 'class=text-warning'#'bgcolor=#ee902c'
145 color_status['{}'.format(i+1)] = 'class=text-warning'#'bgcolor=#ee902c'
146 else: #Disconnected background-color: #ff0000;
146 else: #Disconnected background-color: #ff0000;
147 all_status['{}'.format(i+1)] = 0
147 all_status['{}'.format(i+1)] = 0
148 color_status['{}'.format(i+1)] = 'class=text-danger'#'bgcolor=#FF0000'
148 color_status['{}'.format(i+1)] = 'class=text-danger'#'bgcolor=#FF0000'
149 #------------------------------------------------
149 #------------------------------------------------
150
150
151 kwargs = {}
151 kwargs = {}
152 kwargs['connected_modules'] = str(conf.connected_modules())+'/64'
152 kwargs['connected_modules'] = str(conf.connected_modules())+'/64'
153 kwargs['dev_conf'] = conf
153 kwargs['dev_conf'] = conf
154
154
155 if conf.operation_mode == 0:
155 if conf.operation_mode == 0:
156 kwargs['dev_conf_keys'] = ['label', 'operation_mode']
156 kwargs['dev_conf_keys'] = ['label', 'operation_mode']
157 else:
157 else:
158 kwargs['dev_conf_keys'] = ['label', 'operation_mode', 'operation_value']
158 kwargs['dev_conf_keys'] = ['label', 'operation_mode', 'operation_value']
159
159
160 kwargs['title'] = 'ABS Configuration'
160 kwargs['title'] = 'ABS Configuration'
161 kwargs['suptitle'] = 'Details'
161 kwargs['suptitle'] = 'Details'
162 kwargs['button'] = 'Edit Configuration'
162 kwargs['button'] = 'Edit Configuration'
163
163
164 if conf.active_beam != 0:
164 if conf.active_beam != 0:
165 kwargs['active_beam'] = int(conf.active_beam)
165 kwargs['active_beam'] = int(conf.active_beam)
166
166
167 kwargs['beams'] = beams
167 kwargs['beams'] = beams
168 kwargs['modules_status'] = all_status
168 kwargs['modules_status'] = all_status
169 kwargs['color_status'] = color_status
169 kwargs['color_status'] = color_status
170 kwargs['module_messages'] = module_messages
170 kwargs['module_messages'] = module_messages
171 ###### SIDEBAR ######
171 ###### SIDEBAR ######
172 kwargs.update(sidebar(conf=conf))
172 kwargs.update(sidebar(conf=conf))
173
173
174 return render(request, 'abs_conf.html', kwargs)
174 return render(request, 'abs_conf.html', kwargs)
175
175
176 def abs_conf_mqtt(request, id_conf):
176 def abs_conf_mqtt(request, id_conf):
177 print("Estoy en abs_conf_mqtt",flush=True)
177 # print("Estoy en abs_conf_mqtt",flush=True)
178 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
178 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
179 beams = ABSBeam.objects.filter(abs_conf=conf)
179 beams = ABSBeam.objects.filter(abs_conf=conf)
180 #------------Colors for Active Beam:-------------
180 #------------Colors for Active Beam:-------------
181 all_status = {}
181 all_status = {}
182 module_messages = json.loads(conf.module_messages)
182 module_messages = json.loads(conf.module_messages)
183
183
184 kwargs = {}
184 kwargs = {}
185 kwargs['connected_modules'] = str(conf.connected_modules())+'/64'
185 kwargs['connected_modules'] = str(conf.connected_modules())+'/64'
186 kwargs['dev_conf'] = conf
186 kwargs['dev_conf'] = conf
187
187
188 if conf.operation_mode == 0:
188 if conf.operation_mode == 0:
189 kwargs['dev_conf_keys'] = ['label', 'operation_mode']
189 kwargs['dev_conf_keys'] = ['label', 'operation_mode']
190 else:
190 else:
191 kwargs['dev_conf_keys'] = ['label', 'operation_mode', 'operation_value']
191 kwargs['dev_conf_keys'] = ['label', 'operation_mode', 'operation_value']
192
192
193 kwargs['title'] = 'ABS Configuration'
193 kwargs['title'] = 'ABS Configuration'
194 kwargs['suptitle'] = 'Details'
194 kwargs['suptitle'] = 'Details'
195 kwargs['button'] = 'Edit Configuration'
195 kwargs['button'] = 'Edit Configuration'
196
196
197 if conf.active_beam != 0:
197 if conf.active_beam != 0:
198 kwargs['active_beam'] = int(conf.active_beam)
198 kwargs['active_beam'] = int(conf.active_beam)
199
199
200 kwargs['mqtt']=True
200 kwargs['mqtt']=True
201 kwargs['beams'] = beams
201 kwargs['beams'] = beams
202 kwargs['modules_status'] = all_status
202 kwargs['modules_status'] = all_status
203 kwargs['module_messages'] = module_messages
203 kwargs['module_messages'] = module_messages
204 ###### SIDEBAR ######
204 ###### SIDEBAR ######
205 kwargs.update(sidebar(conf=conf))
205 kwargs.update(sidebar(conf=conf))
206 print("conf.active_beam: {}",format(conf.active_beam))
206 print("conf.active_beam: {}",format(conf.active_beam))
207
207
208 # print(conf.beam.pk)
208 # print(conf.beam.pk)
209
209
210 return render(request, 'abs_conf_mqtt.html', kwargs)
210 return render(request, 'abs_conf_mqtt.html', kwargs)
211 # // console.log(beam.pk);
211 # // console.log(beam.pk);
212 # // console.log(active_beam);
212 # // console.log(active_beam);
213 # def abs_conf_mqtt(request, id_conf):
213 # def abs_conf_mqtt(request, id_conf):
214 # # socket.on('beams_ack',function(data){
214 # # socket.on('beams_ack',function(data){
215 # ack=data;
215 # ack=data;
216 # console.log("ack")
216 # console.log("ack")
217 # console.log(ack)
217 # console.log(ack)
218 # })
218 # })
219
219
220
220
221 def abs_conf_edit(request, id_conf):
221 def abs_conf_edit(request, id_conf):
222
222
223 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
223 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
224
224
225 beams = ABSBeam.objects.filter(abs_conf=conf)
225 beams = ABSBeam.objects.filter(abs_conf=conf)
226
226
227 if request.method=='GET':
227 if request.method=='GET':
228 form = ABSConfigurationForm(instance=conf)
228 form = ABSConfigurationForm(instance=conf)
229
229
230 if request.method=='POST':
230 if request.method=='POST':
231 form = ABSConfigurationForm(request.POST, instance=conf)
231 form = ABSConfigurationForm(request.POST, instance=conf)
232
232
233 if form.is_valid():
233 if form.is_valid():
234 conf = form.save(commit=False)
234 conf = form.save(commit=False)
235 conf.save()
235 conf.save()
236 return redirect('url_abs_conf_mqtt', id_conf=conf.id)
236 return redirect('url_abs_conf_mqtt', id_conf=conf.id)
237
237
238 ###### SIDEBAR ######
238 ###### SIDEBAR ######
239 kwargs = {}
239 kwargs = {}
240
240
241 kwargs['dev_conf'] = conf
241 kwargs['dev_conf'] = conf
242 #kwargs['id_dev'] = conf.id
242 #kwargs['id_dev'] = conf.id
243 kwargs['id_conf'] = conf.id
243 kwargs['id_conf'] = conf.id
244 kwargs['form'] = form
244 kwargs['form'] = form
245 kwargs['abs_beams'] = beams
245 kwargs['abs_beams'] = beams
246 kwargs['title'] = 'Device Configuration'
246 kwargs['title'] = 'Device Configuration'
247 kwargs['suptitle'] = 'Edit'
247 kwargs['suptitle'] = 'Edit'
248 kwargs['button'] = 'Save'
248 kwargs['button'] = 'Save'
249 kwargs['mqtt']=True
249 kwargs['mqtt']=True
250
250
251 kwargs['edit'] = True
251 kwargs['edit'] = True
252
252
253 return render(request, 'abs_conf_edit.html', kwargs)
253 return render(request, 'abs_conf_edit.html', kwargs)
254
254
255 @csrf_exempt
255 @csrf_exempt
256 def abs_conf_alert(request):
256 def abs_conf_alert(request):
257
257
258 if request.method == 'POST':
258 if request.method == 'POST':
259 print (request.POST)
259 print (request.POST)
260 return HttpResponse(json.dumps({'result':1}), content_type='application/json')
260 return HttpResponse(json.dumps({'result':1}), content_type='application/json')
261 else:
261 else:
262 return redirect('index')
262 return redirect('index')
263
263
264
264
265 def import_file(request, id_conf):
265 def import_file(request, id_conf):
266
266
267 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
267 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
268 if request.method=='POST':
268 if request.method=='POST':
269 form = ABSImportForm(request.POST, request.FILES)
269 form = ABSImportForm(request.POST, request.FILES)
270 if form.is_valid():
270 if form.is_valid():
271 try:
271 try:
272 parms = conf.import_from_file(request.FILES['file_name'])
272 parms = conf.import_from_file(request.FILES['file_name'])
273
273
274 if parms:
274 if parms:
275 conf.update_from_file(parms)
275 conf.update_from_file(parms)
276 messages.success(request, 'Configuration "%s" loaded succesfully' % request.FILES['file_name'])
276 messages.success(request, 'Configuration "%s" loaded succesfully' % request.FILES['file_name'])
277 return redirect(conf.get_absolute_url_edit())
277 return redirect(conf.get_absolute_url_edit())
278
278
279 except Exception as e:
279 except Exception as e:
280 messages.error(request, 'Error parsing file: "%s" - %s' % (request.FILES['file_name'], e))
280 messages.error(request, 'Error parsing file: "%s" - %s' % (request.FILES['file_name'], e))
281
281
282 else:
282 else:
283 messages.warning(request, 'Your current configuration will be replaced')
283 messages.warning(request, 'Your current configuration will be replaced')
284 form = ABSImportForm()
284 form = ABSImportForm()
285
285
286 kwargs = {}
286 kwargs = {}
287 kwargs['form'] = form
287 kwargs['form'] = form
288 kwargs['title'] = 'ABS Configuration'
288 kwargs['title'] = 'ABS Configuration'
289 kwargs['suptitle'] = 'Import file'
289 kwargs['suptitle'] = 'Import file'
290 kwargs['button'] = 'Upload'
290 kwargs['button'] = 'Upload'
291 kwargs['previous'] = conf.get_absolute_url()
291 kwargs['previous'] = conf.get_absolute_url()
292
292
293 return render(request, 'abs_import.html', kwargs)
293 return render(request, 'abs_import.html', kwargs)
294
294
295
295
296 def send_beam(request, id_conf, id_beam):
296 def send_beam(request, id_conf, id_beam):
297
297
298 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
298 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
299
299
300 abs = Configuration.objects.filter(pk=conf.device.conf_active).first()
300 abs = Configuration.objects.filter(pk=conf.device.conf_active).first()
301 if abs!=conf:
301 if abs!=conf:
302 url = '#' if abs is None else abs.get_absolute_url()
302 url = '#' if abs is None else abs.get_absolute_url()
303 label = 'None' if abs is None else abs.label
303 label = 'None' if abs is None else abs.label
304 messages.warning(
304 messages.warning(
305 request,
305 request,
306 mark_safe('The current configuration has not been written in the modules, the active configuration is <a href="{}">{}</a>'.format(
306 mark_safe('The current configuration has not been written in the modules, the active configuration is <a href="{}">{}</a>'.format(
307 url,
307 url,
308 label
308 label
309 ))
309 ))
310 )
310 )
311 return redirect(conf.get_absolute_url())
311 return redirect(conf.get_absolute_url())
312
312
313 beam = get_object_or_404(ABSBeam, pk=id_beam)
313 beam = get_object_or_404(ABSBeam, pk=id_beam)
314
314
315 if request.method == 'POST':
315 if request.method == 'POST':
316
316
317 beams_list = ABSBeam.objects.filter(abs_conf=conf)
317 beams_list = ABSBeam.objects.filter(abs_conf=conf)
318 conf.active_beam = id_beam
318 conf.active_beam = id_beam
319
319
320 i = 0
320 i = 0
321 for b in beams_list:
321 for b in beams_list:
322 if b.id == int(id_beam):
322 if b.id == int(id_beam):
323 break
323 break
324 else:
324 else:
325 i += 1
325 i += 1
326 beam_pos = i + 1 #Estandarizar
326 beam_pos = i + 1 #Estandarizar
327 print('{} Position {}'.format(beam.name,str(beam_pos)))
327 print('{} Position {}'.format(beam.name,str(beam_pos)))
328 conf.send_beam(beam_pos)
328 conf.send_beam(beam_pos)
329
329
330 return redirect('url_abs_conf', conf.id)
330 return redirect('url_abs_conf', conf.id)
331
331
332 kwargs = {
332 kwargs = {
333 'title': 'ABS',
333 'title': 'ABS',
334 'suptitle': conf.label,
334 'suptitle': conf.label,
335 'message': 'Are you sure you want to change ABS Beam through SEND BEAM to: {}?'.format(beam.name),
335 'message': 'Are you sure you want to change ABS Beam through SEND BEAM to: {}?'.format(beam.name),
336 'delete': False
336 'delete': False
337 }
337 }
338 kwargs['menu_configurations'] = 'active'
338 kwargs['menu_configurations'] = 'active'
339
339
340 return render(request, 'confirm.html', kwargs)
340 return render(request, 'confirm.html', kwargs)
341
341
342 def change_beam_mqtt(request, id_conf, id_beam):
342 def change_beam_mqtt(request, id_conf, id_beam):
343
343
344 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
344 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
345 print("conf: {}".format(conf),flush=True)
345 # print("conf: {}".format(conf),flush=True)
346
346
347 abs = Configuration.objects.filter(pk=conf.device.conf_active).first()
347 abs = Configuration.objects.filter(pk=conf.device.conf_active).first()
348 print("abs: {}".format(abs),flush=True)
348 # print("abs: {}".format(abs),flush=True)
349 if abs!=conf:
349 if abs!=conf:
350 url = '#' if abs is None else abs.get_absolute_url()
350 url = '#' if abs is None else abs.get_absolute_url()
351 label = 'None' if abs is None else abs.label
351 label = 'None' if abs is None else abs.label
352 messages.warning(
352 messages.warning(
353 request,
353 request,
354 mark_safe('The current configuration has not been written in the modules, the active configuration is <a href="{}">{}</a>'.format(
354 mark_safe('The current configuration has not been written in the modules, the active configuration is <a href="{}">{}</a>'.format(
355 url,
355 url,
356 label
356 label
357 ))
357 ))
358 )
358 )
359 return redirect(conf.get_absolute_mqtt_url())
359 return redirect(conf.get_absolute_mqtt_url())
360
360
361 beams = ABSBeam.objects.filter(abs_conf=conf)
361 beams = ABSBeam.objects.filter(abs_conf=conf)
362 beam = get_object_or_404(ABSBeam, pk=id_beam)
362 beam = get_object_or_404(ABSBeam, pk=id_beam)
363
363
364 if request.method == 'POST':
364 if request.method == 'POST':
365
365
366 beams_list = ABSBeam.objects.filter(abs_conf=conf)
366 beams_list = ABSBeam.objects.filter(abs_conf=conf)
367 conf.active_beam = id_beam
367 conf.active_beam = id_beam
368
368
369 i = 0
369 i = 0
370 for b in beams_list:
370 for b in beams_list:
371 if b.id == int(id_beam):
371 if b.id == int(id_beam):
372 break
372 break
373 else:
373 else:
374 i += 1
374 i += 1
375 beam_pos = i + 1 #Estandarizar
375 beam_pos = i + 1 #Estandarizar
376
376
377 print('{} Position {}'.format(beam.name,str(beam_pos)),flush=True)
377 print('{} Position {}'.format(beam.name,str(beam_pos)),flush=True)
378 conf.change_beam_mqtt(beam_pos)
378 conf.change_beam_mqtt(beam_pos)
379
379
380 # return redirect('url_abs_conf', conf.id)
380 # return redirect('url_abs_conf', conf.id)
381 return redirect('url_abs_conf_mqtt', conf.id)
381 return redirect('url_abs_conf_mqtt', conf.id)
382 # return render(request, 'abs_conf_mqtt.html',kwargs)
382 # return render(request, 'abs_conf_mqtt.html',kwargs)
383
383
384 kwargs = {
384 kwargs = {
385 'title': 'ABS',
385 'title': 'ABS',
386 'mqtt':True,
386 'mqtt':True,
387 'suptitle': conf.label,
387 'suptitle': conf.label,
388 'message': 'Are you sure you want to change ABS Beam through MQTT to: {}?'.format(beam.name),
388 'message': 'Are you sure you want to change ABS Beam through MQTT to: {}?'.format(beam.name),
389 'delete': False
389 'delete': False
390 }
390 }
391 kwargs['menu_configurations'] = 'active'
391 kwargs['menu_configurations'] = 'active'
392
392
393 return render(request, 'confirm.html', kwargs)
393 return render(request, 'confirm.html', kwargs)
394
394
395 def add_beam(request, id_conf):
395 def add_beam(request, id_conf):
396
396
397 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
397 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
398 confs = Configuration.objects.all()
398 confs = Configuration.objects.all()
399
399
400 if request.method=='GET':
400 if request.method=='GET':
401 form = ABSBeamAddForm()
401 form = ABSBeamAddForm()
402
402
403 if request.method=='POST':
403 if request.method=='POST':
404 form = ABSBeamAddForm(request.POST)
404 form = ABSBeamAddForm(request.POST)
405
405
406 beam_data = get_values_from_form(request.POST)
406 beam_data = get_values_from_form(request.POST)
407
407
408 new_beam = ABSBeam(
408 new_beam = ABSBeam(
409 name = beam_data['name'],
409 name = beam_data['name'],
410 antenna = json.dumps(beam_data['antenna']),
410 antenna = json.dumps(beam_data['antenna']),
411 abs_conf = conf,
411 abs_conf = conf,
412 tx = json.dumps(beam_data['tx']),
412 tx = json.dumps(beam_data['tx']),
413 rx = json.dumps(beam_data['rx']),
413 rx = json.dumps(beam_data['rx']),
414 ues = json.dumps(beam_data['ues']),
414 ues = json.dumps(beam_data['ues']),
415 only_rx = json.dumps(beam_data['only_rx'])
415 only_rx = json.dumps(beam_data['only_rx'])
416 )
416 )
417 new_beam.save()
417 new_beam.save()
418 messages.success(request, 'Beam: "%s" has been added.' % new_beam.name)
418 messages.success(request, 'Beam: "%s" has been added.' % new_beam.name)
419
419
420 return redirect('url_edit_abs_conf', conf.id)
420 return redirect('url_edit_abs_conf', conf.id)
421
421
422 ###### SIDEBAR ######
422 ###### SIDEBAR ######
423 kwargs = {}
423 kwargs = {}
424
424
425 #kwargs['dev_conf'] = conf.device
425 #kwargs['dev_conf'] = conf.device
426 #kwargs['id_dev'] = conf.device
426 #kwargs['id_dev'] = conf.device
427 #kwargs['previous'] = conf.get_absolute_url_edit()
427 #kwargs['previous'] = conf.get_absolute_url_edit()
428 kwargs['id_conf'] = conf.id
428 kwargs['id_conf'] = conf.id
429 kwargs['form'] = form
429 kwargs['form'] = form
430 kwargs['title'] = 'ABS Beams'
430 kwargs['title'] = 'ABS Beams'
431 kwargs['suptitle'] = 'Add Beam'
431 kwargs['suptitle'] = 'Add Beam'
432 kwargs['button'] = 'Add'
432 kwargs['button'] = 'Add'
433 kwargs['no_sidebar'] = True
433 kwargs['no_sidebar'] = True
434 kwargs['edit'] = True
434 kwargs['edit'] = True
435
435
436 return render(request, 'abs_add_beam.html', kwargs)
436 return render(request, 'abs_add_beam.html', kwargs)
437
437
438
438
439 def edit_beam(request, id_conf, id_beam):
439 def edit_beam(request, id_conf, id_beam):
440
440
441 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
441 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
442 beam = get_object_or_404(ABSBeam, pk=id_beam)
442 beam = get_object_or_404(ABSBeam, pk=id_beam)
443
443
444 if request.method=='GET':
444 if request.method=='GET':
445 form = ABSBeamEditForm(initial={'beam': beam})
445 form = ABSBeamEditForm(initial={'beam': beam})
446
446
447 if request.method=='POST':
447 if request.method=='POST':
448 form = ABSBeamEditForm(request.POST)
448 form = ABSBeamEditForm(request.POST)
449
449
450 beam_data = get_values_from_form(request.POST)
450 beam_data = get_values_from_form(request.POST)
451
451
452 beam.dict_to_parms(beam_data)
452 beam.dict_to_parms(beam_data)
453 beam.save()
453 beam.save()
454
454
455 messages.success(request, 'Beam: "%s" has been updated.' % beam.name)
455 messages.success(request, 'Beam: "%s" has been updated.' % beam.name)
456
456
457 return redirect('url_edit_abs_conf', conf.id)
457 return redirect('url_edit_abs_conf', conf.id)
458
458
459 ###### SIDEBAR ######
459 ###### SIDEBAR ######
460 kwargs = {}
460 kwargs = {}
461
461
462 kwargs['id_conf'] = conf.id
462 kwargs['id_conf'] = conf.id
463 kwargs['form'] = form
463 kwargs['form'] = form
464 kwargs['title'] = 'ABS Beams'
464 kwargs['title'] = 'ABS Beams'
465 kwargs['suptitle'] = 'Edit Beam'
465 kwargs['suptitle'] = 'Edit Beam'
466 kwargs['button'] = 'Save'
466 kwargs['button'] = 'Save'
467 kwargs['no_sidebar'] = True
467 kwargs['no_sidebar'] = True
468
468
469 #kwargs['previous'] = conf.get_absolute_url_edit()
469 #kwargs['previous'] = conf.get_absolute_url_edit()
470 kwargs['edit'] = True
470 kwargs['edit'] = True
471
471
472 return render(request, 'abs_edit_beam.html', kwargs)
472 return render(request, 'abs_edit_beam.html', kwargs)
473
473
474
474
475
475
476 def remove_beam(request, id_conf, id_beam):
476 def remove_beam(request, id_conf, id_beam):
477
477
478 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
478 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
479 beam = get_object_or_404(ABSBeam, pk=id_beam)
479 beam = get_object_or_404(ABSBeam, pk=id_beam)
480
480
481 if request.method=='POST':
481 if request.method=='POST':
482 if beam:
482 if beam:
483 try:
483 try:
484 beam.delete()
484 beam.delete()
485 messages.success(request, 'Beam: "%s" has been deleted.' % beam)
485 messages.success(request, 'Beam: "%s" has been deleted.' % beam)
486 except:
486 except:
487 messages.error(request, 'Unable to delete beam: "%s".' % beam)
487 messages.error(request, 'Unable to delete beam: "%s".' % beam)
488
488
489 return redirect('url_edit_abs_conf', conf.id)
489 return redirect('url_edit_abs_conf', conf.id)
490
490
491 ###### SIDEBAR ######
491 ###### SIDEBAR ######
492 kwargs = {}
492 kwargs = {}
493
493
494 kwargs['object'] = beam
494 kwargs['object'] = beam
495 kwargs['delete'] = True
495 kwargs['delete'] = True
496 kwargs['title'] = 'Delete'
496 kwargs['title'] = 'Delete'
497 kwargs['suptitle'] = 'Beam'
497 kwargs['suptitle'] = 'Beam'
498 kwargs['previous'] = conf.get_absolute_url_edit()
498 kwargs['previous'] = conf.get_absolute_url_edit()
499 return render(request, 'confirm.html', kwargs)
499 return render(request, 'confirm.html', kwargs)
500
500
501
501
502
502
503 def plot_patterns(request, id_conf, id_beam=None):
503 def plot_patterns(request, id_conf, id_beam=None):
504
504
505 kwargs = {}
505 kwargs = {}
506 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
506 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
507 beams = ABSBeam.objects.filter(abs_conf=conf)
507 beams = ABSBeam.objects.filter(abs_conf=conf)
508
508
509 if id_beam:
509 if id_beam:
510 beam = get_object_or_404(ABSBeam, pk=id_beam)
510 beam = get_object_or_404(ABSBeam, pk=id_beam)
511 kwargs['beam'] = beam
511 kwargs['beam'] = beam
512
512
513 ###### SIDEBAR ######
513 ###### SIDEBAR ######
514
514
515 kwargs['dev_conf'] = conf.device
515 kwargs['dev_conf'] = conf.device
516 kwargs['id_dev'] = conf.device
516 kwargs['id_dev'] = conf.device
517 kwargs['id_conf'] = conf.id
517 kwargs['id_conf'] = conf.id
518 kwargs['abs_beams'] = beams
518 kwargs['abs_beams'] = beams
519 kwargs['title'] = 'ABS Patterns'
519 kwargs['title'] = 'ABS Patterns'
520 kwargs['suptitle'] = conf.name
520 kwargs['suptitle'] = conf.name
521 kwargs['no_sidebar'] = True
521 kwargs['no_sidebar'] = True
522
522
523 return render(request, 'abs_patterns.html', kwargs)
523 return render(request, 'abs_patterns.html', kwargs)
524
524
525
525
526 def plot_pattern(request, id_conf, id_beam, antenna):
526 def plot_pattern(request, id_conf, id_beam, antenna):
527
527
528 if antenna=='down':
528 if antenna=='down':
529 sleep(3)
529 sleep(3)
530
530
531 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
531 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
532 beam = get_object_or_404(ABSBeam, pk=id_beam)
532 beam = get_object_or_404(ABSBeam, pk=id_beam)
533 just_rx = 1 if json.loads(beam.only_rx)[antenna] else 0
533 just_rx = 1 if json.loads(beam.only_rx)[antenna] else 0
534 phases = json.loads(beam.antenna)['antenna_{}'.format(antenna)]
534 phases = json.loads(beam.antenna)['antenna_{}'.format(antenna)]
535 gain_tx = json.loads(beam.tx)[antenna]
535 gain_tx = json.loads(beam.tx)[antenna]
536 gain_rx = json.loads(beam.rx)[antenna]
536 gain_rx = json.loads(beam.rx)[antenna]
537 ues = json.loads(beam.ues)[antenna]
537 ues = json.loads(beam.ues)[antenna]
538 newOverJro = overJroShow(beam.name)
538 newOverJro = overJroShow(beam.name)
539 fig = newOverJro.plotPattern2(datetime.today(), phases, gain_tx, gain_rx, ues, just_rx)
539 fig = newOverJro.plotPattern2(datetime.today(), phases, gain_tx, gain_rx, ues, just_rx)
540 buf = io.BytesIO()
540 buf = io.BytesIO()
541 fig.savefig(buf, format='png')
541 fig.savefig(buf, format='png')
542 response = HttpResponse(buf.getvalue(), content_type='image/png')
542 response = HttpResponse(buf.getvalue(), content_type='image/png')
543 return response
543 return response
544
544
545 import os
545 import os
546 from django.http import HttpResponse
546 from django.http import HttpResponse
547
547
548 @sio.on('connection-bind')
548 @sio.on('connection-bind')
549 def abs_connection_bind(sid, data):
549 def abs_connection_bind(sid, data):
550 print("sid:",sid,"data",data)
550 print("sid:",sid,"data",data)
551
551
552 @sio.on('disconnect')
552 @sio.on('disconnect')
553 def abs_test_disconnect(sid):
553 def abs_test_disconnect(sid):
554 print("Disconnected")
554 print("Disconnected")
555
555
556 @sio.event
556 @sio.event
557 def abs_send_beam_up(sid, message):
557 def abs_send_beam_up(sid, message):
558 mqtt_client.publish('abs/beams_up', message['data'])
558 mqtt_client.publish('abs/beams_up', message['data'])
559
559
560 @sio.event
560 @sio.event
561 def abs_send_beam_down(sid, message):
561 def abs_send_beam_down(sid, message):
562 mqtt_client.publish('abs/beams_down', message['data'])
562 mqtt_client.publish('abs/beams_down', message['data'])
563
563
564 # @sio.event
564 # @sio.event
565 # def change_beam(sid,message):
565 # def change_beam(sid,message):
566 # data=str(message['data'])
566 # data=str(message['data'])
567 # data=data[16]
567 # data=data[16]
568 # mqtt_client.publish('abs/change_beam',data)
568 # mqtt_client.publish('abs/change_beam',data)
569
569
570
570
571
571
572
572
573
573
574
574
@@ -1,284 +1,289
1 from django.db import models
1 from django.db import models
2 from apps.main.models import Configuration
2 from apps.main.models import Configuration
3 from apps.main.utils import Params
3 from apps.main.utils import Params
4 from django.core.validators import MinValueValidator, MaxValueValidator
4 from django.core.validators import MinValueValidator, MaxValueValidator
5
5
6 from .files import read_json_file
6 from .files import read_json_file
7 import requests
7 import requests
8
8
9 class ATRADData(models.Model):
9 class ATRADData(models.Model):
10 datetime = models.DateTimeField()
10 datetime = models.DateTimeField()
11
11
12 nstx = models.SmallIntegerField()
12 nstx = models.SmallIntegerField()
13
13
14 status_1 = models.SmallIntegerField()
14 status_1 = models.SmallIntegerField()
15 temp_cll_1 = models.SmallIntegerField()
15 temp_cll_1 = models.SmallIntegerField()
16 nboards_1 = models.SmallIntegerField()
16 nboards_1 = models.SmallIntegerField()
17 tempdvr_1 = models.SmallIntegerField()
17 tempdvr_1 = models.SmallIntegerField()
18 potincdvr_1 = models.SmallIntegerField()
18 potincdvr_1 = models.SmallIntegerField()
19 potretdvr_1 = models.SmallIntegerField()
19 potretdvr_1 = models.SmallIntegerField()
20
20
21 temp1_1 = models.SmallIntegerField()
21 temp1_1 = models.SmallIntegerField()
22 potinc1_1 = models.SmallIntegerField()
22 potinc1_1 = models.SmallIntegerField()
23 potret1_1 = models.SmallIntegerField()
23 potret1_1 = models.SmallIntegerField()
24 temp2_1 = models.SmallIntegerField()
24 temp2_1 = models.SmallIntegerField()
25 potinc2_1 = models.SmallIntegerField()
25 potinc2_1 = models.SmallIntegerField()
26 potret2_1 = models.SmallIntegerField()
26 potret2_1 = models.SmallIntegerField()
27 temp3_1 = models.SmallIntegerField()
27 temp3_1 = models.SmallIntegerField()
28 potinc3_1 = models.SmallIntegerField()
28 potinc3_1 = models.SmallIntegerField()
29 potret3_1 = models.SmallIntegerField()
29 potret3_1 = models.SmallIntegerField()
30 temp4_1 = models.SmallIntegerField()
30 temp4_1 = models.SmallIntegerField()
31 potinc4_1 = models.SmallIntegerField()
31 potinc4_1 = models.SmallIntegerField()
32 potret4_1 = models.SmallIntegerField()
32 potret4_1 = models.SmallIntegerField()
33 temp5_1 = models.SmallIntegerField()
33 temp5_1 = models.SmallIntegerField()
34 potinc5_1 = models.SmallIntegerField()
34 potinc5_1 = models.SmallIntegerField()
35 potret5_1 = models.SmallIntegerField()
35 potret5_1 = models.SmallIntegerField()
36 temp6_1 = models.SmallIntegerField()
36 temp6_1 = models.SmallIntegerField()
37 potinc6_1 = models.SmallIntegerField()
37 potinc6_1 = models.SmallIntegerField()
38 potret6_1 = models.SmallIntegerField()
38 potret6_1 = models.SmallIntegerField()
39
39
40 status_2 = models.SmallIntegerField()
40 status_2 = models.SmallIntegerField()
41 temp_cll_2 = models.SmallIntegerField()
41 temp_cll_2 = models.SmallIntegerField()
42 nboards_2 = models.SmallIntegerField()
42 nboards_2 = models.SmallIntegerField()
43 tempdvr_2 = models.SmallIntegerField()
43 tempdvr_2 = models.SmallIntegerField()
44 potincdvr_2 = models.SmallIntegerField()
44 potincdvr_2 = models.SmallIntegerField()
45 potretdvr_2 = models.SmallIntegerField()
45 potretdvr_2 = models.SmallIntegerField()
46
46
47 temp1_2 = models.SmallIntegerField()
47 temp1_2 = models.SmallIntegerField()
48 potinc1_2 = models.SmallIntegerField()
48 potinc1_2 = models.SmallIntegerField()
49 potret1_2 = models.SmallIntegerField()
49 potret1_2 = models.SmallIntegerField()
50 temp2_2 = models.SmallIntegerField()
50 temp2_2 = models.SmallIntegerField()
51 potinc2_2 = models.SmallIntegerField()
51 potinc2_2 = models.SmallIntegerField()
52 potret2_2 = models.SmallIntegerField()
52 potret2_2 = models.SmallIntegerField()
53 temp3_2 = models.SmallIntegerField()
53 temp3_2 = models.SmallIntegerField()
54 potinc3_2 = models.SmallIntegerField()
54 potinc3_2 = models.SmallIntegerField()
55 potret3_2 = models.SmallIntegerField()
55 potret3_2 = models.SmallIntegerField()
56 temp4_2 = models.SmallIntegerField()
56 temp4_2 = models.SmallIntegerField()
57 potinc4_2 = models.SmallIntegerField()
57 potinc4_2 = models.SmallIntegerField()
58 potret4_2 = models.SmallIntegerField()
58 potret4_2 = models.SmallIntegerField()
59 temp5_2 = models.SmallIntegerField()
59 temp5_2 = models.SmallIntegerField()
60 potinc5_2 = models.SmallIntegerField()
60 potinc5_2 = models.SmallIntegerField()
61 potret5_2 = models.SmallIntegerField()
61 potret5_2 = models.SmallIntegerField()
62 temp6_2 = models.SmallIntegerField()
62 temp6_2 = models.SmallIntegerField()
63 potinc6_2 = models.SmallIntegerField()
63 potinc6_2 = models.SmallIntegerField()
64 potret6_2 = models.SmallIntegerField()
64 potret6_2 = models.SmallIntegerField()
65
65
66 status_3 = models.SmallIntegerField()
66 status_3 = models.SmallIntegerField()
67 temp_cll_3 = models.SmallIntegerField()
67 temp_cll_3 = models.SmallIntegerField()
68 nboards_3 = models.SmallIntegerField()
68 nboards_3 = models.SmallIntegerField()
69 tempdvr_3 = models.SmallIntegerField()
69 tempdvr_3 = models.SmallIntegerField()
70 potincdvr_3 = models.SmallIntegerField()
70 potincdvr_3 = models.SmallIntegerField()
71 potretdvr_3 = models.SmallIntegerField()
71 potretdvr_3 = models.SmallIntegerField()
72
72
73 temp1_3 = models.SmallIntegerField()
73 temp1_3 = models.SmallIntegerField()
74 potinc1_3 = models.SmallIntegerField()
74 potinc1_3 = models.SmallIntegerField()
75 potret1_3 = models.SmallIntegerField()
75 potret1_3 = models.SmallIntegerField()
76 temp2_3 = models.SmallIntegerField()
76 temp2_3 = models.SmallIntegerField()
77 potinc2_3 = models.SmallIntegerField()
77 potinc2_3 = models.SmallIntegerField()
78 potret2_3 = models.SmallIntegerField()
78 potret2_3 = models.SmallIntegerField()
79 temp3_3 = models.SmallIntegerField()
79 temp3_3 = models.SmallIntegerField()
80 potinc3_3 = models.SmallIntegerField()
80 potinc3_3 = models.SmallIntegerField()
81 potret3_3 = models.SmallIntegerField()
81 potret3_3 = models.SmallIntegerField()
82 temp4_3 = models.SmallIntegerField()
82 temp4_3 = models.SmallIntegerField()
83 potinc4_3 = models.SmallIntegerField()
83 potinc4_3 = models.SmallIntegerField()
84 potret4_3 = models.SmallIntegerField()
84 potret4_3 = models.SmallIntegerField()
85 temp5_3 = models.SmallIntegerField()
85 temp5_3 = models.SmallIntegerField()
86 potinc5_3 = models.SmallIntegerField()
86 potinc5_3 = models.SmallIntegerField()
87 potret5_3 = models.SmallIntegerField()
87 potret5_3 = models.SmallIntegerField()
88 temp6_3 = models.SmallIntegerField()
88 temp6_3 = models.SmallIntegerField()
89 potinc6_3 = models.SmallIntegerField()
89 potinc6_3 = models.SmallIntegerField()
90 potret6_3 = models.SmallIntegerField()
90 potret6_3 = models.SmallIntegerField()
91
91
92 status_4 = models.SmallIntegerField()
92 status_4 = models.SmallIntegerField()
93 temp_cll_4 = models.SmallIntegerField()
93 temp_cll_4 = models.SmallIntegerField()
94 nboards_4 = models.SmallIntegerField()
94 nboards_4 = models.SmallIntegerField()
95 tempdvr_4 = models.SmallIntegerField()
95 tempdvr_4 = models.SmallIntegerField()
96 potincdvr_4 = models.SmallIntegerField()
96 potincdvr_4 = models.SmallIntegerField()
97 potretdvr_4 = models.SmallIntegerField()
97 potretdvr_4 = models.SmallIntegerField()
98
98
99 temp1_4 = models.SmallIntegerField()
99 temp1_4 = models.SmallIntegerField()
100 potinc1_4 = models.SmallIntegerField()
100 potinc1_4 = models.SmallIntegerField()
101 potret1_4 = models.SmallIntegerField()
101 potret1_4 = models.SmallIntegerField()
102 temp2_4 = models.SmallIntegerField()
102 temp2_4 = models.SmallIntegerField()
103 potinc2_4 = models.SmallIntegerField()
103 potinc2_4 = models.SmallIntegerField()
104 potret2_4 = models.SmallIntegerField()
104 potret2_4 = models.SmallIntegerField()
105 temp3_4 = models.SmallIntegerField()
105 temp3_4 = models.SmallIntegerField()
106 potinc3_4 = models.SmallIntegerField()
106 potinc3_4 = models.SmallIntegerField()
107 potret3_4 = models.SmallIntegerField()
107 potret3_4 = models.SmallIntegerField()
108 temp4_4 = models.SmallIntegerField()
108 temp4_4 = models.SmallIntegerField()
109 potinc4_4 = models.SmallIntegerField()
109 potinc4_4 = models.SmallIntegerField()
110 potret4_4 = models.SmallIntegerField()
110 potret4_4 = models.SmallIntegerField()
111 temp5_4 = models.SmallIntegerField()
111 temp5_4 = models.SmallIntegerField()
112 potinc5_4 = models.SmallIntegerField()
112 potinc5_4 = models.SmallIntegerField()
113 potret5_4 = models.SmallIntegerField()
113 potret5_4 = models.SmallIntegerField()
114 temp6_4 = models.SmallIntegerField()
114 temp6_4 = models.SmallIntegerField()
115 potinc6_4 = models.SmallIntegerField()
115 potinc6_4 = models.SmallIntegerField()
116 potret6_4 = models.SmallIntegerField()
116 potret6_4 = models.SmallIntegerField()
117
117
118 combiner1 = models.SmallIntegerField()
119 combiner2 = models.SmallIntegerField()
120 combiner3 = models.SmallIntegerField()
121 combiner4 = models.SmallIntegerField()
122
118 class Meta:
123 class Meta:
119 db_table = 'atrad_datas'
124 db_table = 'atrad_datas'
120
125
121 def __unicode__(self):
126 def __unicode__(self):
122 return u'%s' % (self.name)
127 return u'%s' % (self.name)
123
128
124 class ATRADConfiguration(Configuration):
129 class ATRADConfiguration(Configuration):
125
130
126 topic = models.PositiveIntegerField(verbose_name='Topic',validators=[MaxValueValidator(10)], default = 0)
131 topic = models.PositiveIntegerField(verbose_name='Topic',validators=[MaxValueValidator(10)], default = 0)
127
132
128 def status_device(self):
133 def status_device(self):
129
134
130 ip=self.device.ip_address
135 ip=self.device.ip_address
131 port=self.device.port_address
136 port=self.device.port_address
132
137
133 route = "http://" + str(ip) + ":" + str(port) + "/status/"
138 route = "http://" + str(ip) + ":" + str(port) + "/status/"
134 try:
139 try:
135 r = requests.get(route, timeout=0.7)
140 r = requests.get(route, timeout=0.7)
136 except Exception as e:
141 except Exception as e:
137 self.device.status = 0
142 self.device.status = 0
138 self.device.save()
143 self.device.save()
139 self.message = 'Could not read TX status: ' + str(e)
144 self.message = 'Could not read TX status: ' + str(e)
140 return False
145 return False
141
146
142 response = r.json()
147 response = r.json()
143 self.device.status = response['status']
148 self.device.status = response['status']
144 self.message = response['message']
149 self.message = response['message']
145 self.device.save()
150 self.device.save()
146
151
147 if response['components_status']==0:
152 if response['components_status']==0:
148 return False
153 return False
149
154
150 return True
155 return True
151
156
152
157
153 def start_device(self):
158 def start_device(self):
154
159
155 ip=self.device.ip_address
160 ip=self.device.ip_address
156 port=self.device.port_address
161 port=self.device.port_address
157
162
158 #---Device must be configured
163 #---Device must be configured
159 if not self.device.status == 2:
164 if not self.device.status == 2:
160 self.message = 'TX Device must be configured.'
165 self.message = 'TX Device must be configured.'
161 return False
166 return False
162 #---Frequencies from form
167 #---Frequencies from form
163 post_data = self.parms_to_dict()
168 post_data = self.parms_to_dict()
164 route = "http://" + str(ip) + ":" + str(port) + "/write/"
169 route = "http://" + str(ip) + ":" + str(port) + "/write/"
165
170
166 try:
171 try:
167 r = requests.post(route, post_data, timeout=0.7)
172 r = requests.post(route, post_data, timeout=0.7)
168 except Exception as e:
173 except Exception as e:
169 self.message = "Could not start TX device. "+str(e)
174 self.message = "Could not start TX device. "+str(e)
170 return False
175 return False
171
176
172 response = r.json()
177 response = r.json()
173 if response['status']==1:
178 if response['status']==1:
174 self.device.status = 1
179 self.device.status = 1
175 self.device.save()
180 self.device.save()
176 self.message = response['message']
181 self.message = response['message']
177 return False
182 return False
178
183
179 self.device.status = response['status']
184 self.device.status = response['status']
180 self.device.save()
185 self.device.save()
181 self.message = response['message']
186 self.message = response['message']
182
187
183 return True
188 return True
184
189
185
190
186 def stop_device(self):
191 def stop_device(self):
187
192
188 ip=self.device.ip_address
193 ip=self.device.ip_address
189 port=self.device.port_address
194 port=self.device.port_address
190
195
191 if self.device.status == 2: #Configured
196 if self.device.status == 2: #Configured
192 self.message = 'TX device is already stopped.'
197 self.message = 'TX device is already stopped.'
193 return False
198 return False
194
199
195 post_data = {"topic":0}
200 post_data = {"topic":0}
196 route = "http://" + str(ip) + ":" + str(port) + "/write/"
201 route = "http://" + str(ip) + ":" + str(port) + "/write/"
197
202
198 try:
203 try:
199 r = requests.post(route, post_data, timeout=0.7)
204 r = requests.post(route, post_data, timeout=0.7)
200 except Exception as e:
205 except Exception as e:
201 self.message = "Could not write TX parameters. "+str(e)
206 self.message = "Could not write TX parameters. "+str(e)
202 self.device.status = 0
207 self.device.status = 0
203 self.device.save()
208 self.device.save()
204 return False
209 return False
205
210
206 response = r.json()
211 response = r.json()
207 status = response['status']
212 status = response['status']
208 if status == 1:
213 if status == 1:
209 self.device.status = status
214 self.device.status = status
210 self.device.save()
215 self.device.save()
211 self.message = 'Could not stop TX device.'
216 self.message = 'Could not stop TX device.'
212 return False
217 return False
213
218
214 self.message = 'TX device has been stopped successfully.'
219 self.message = 'TX device has been stopped successfully.'
215 self.device.status = 2
220 self.device.status = 2
216 self.device.save()
221 self.device.save()
217
222
218 return True
223 return True
219
224
220
225
221 def read_device(self):
226 def read_device(self):
222
227
223 ip=self.device.ip_address
228 ip=self.device.ip_address
224 port=self.device.port_address
229 port=self.device.port_address
225
230
226 route = "http://" + str(ip) + ":" + str(port) + "/read/"
231 route = "http://" + str(ip) + ":" + str(port) + "/read/"
227 try:
232 try:
228 frequencies = requests.get(route,timeout=0.7)
233 frequencies = requests.get(route,timeout=0.7)
229 except:
234 except:
230 self.message = "Could not read TX parameters from this device"
235 self.message = "Could not read TX parameters from this device"
231 return None
236 return None
232
237
233 frequencies = frequencies.json()
238 frequencies = frequencies.json()
234 if frequencies:
239 if frequencies:
235 frequencies = frequencies.get("Frequencies")
240 frequencies = frequencies.get("Frequencies")
236 topic = frequencies.get("topic")
241 topic = frequencies.get("topic")
237
242
238 parms = {'topic': topic}
243 parms = {'topic': topic}
239
244
240 self.message = "TX parameters have been successfully read"
245 self.message = "TX parameters have been successfully read"
241 return parms
246 return parms
242 else:
247 else:
243 self.message = "Error reading TX parameters"
248 self.message = "Error reading TX parameters"
244 return None
249 return None
245
250
246
251
247 def write_device(self):
252 def write_device(self):
248
253
249 ip=self.device.ip_address
254 ip=self.device.ip_address
250 port=self.device.port_address
255 port=self.device.port_address
251
256
252 #---Frequencies from form
257 #---Frequencies from form
253 parms = self.parms_to_dict()['configurations']
258 parms = self.parms_to_dict()['configurations']
254 for parm in parms['allIds']:
259 for parm in parms['allIds']:
255 byid = parm
260 byid = parm
256 frequencies = parms['byId'][byid]
261 frequencies = parms['byId'][byid]
257 post_data = {}
262 post_data = {}
258 for data in frequencies:
263 for data in frequencies:
259 if data in ['topic']:
264 if data in ['topic']:
260 post_data[data] = frequencies[data]
265 post_data[data] = frequencies[data]
261
266
262 route = "http://" + str(ip) + ":" + str(port) + "/write/"
267 route = "http://" + str(ip) + ":" + str(port) + "/write/"
263 print (post_data)
268 print (post_data)
264 try:
269 try:
265 r = requests.post(route, post_data, timeout=0.7)
270 r = requests.post(route, post_data, timeout=0.7)
266 except:
271 except:
267 self.message = "Could not write TX parameters"
272 self.message = "Could not write TX parameters"
268 self.device.status = 0
273 self.device.status = 0
269 self.device.save()
274 self.device.save()
270 return False
275 return False
271
276
272 response = r.json()
277 response = r.json()
273 self.message = response['message']
278 self.message = response['message']
274 self.device.status = response['status']
279 self.device.status = response['status']
275 self.device.save()
280 self.device.save()
276
281
277 if self.device.status==1:
282 if self.device.status==1:
278 return False
283 return False
279
284
280 return True
285 return True
281
286
282
287
283 class Meta:
288 class Meta:
284 db_table = 'atrad_configurations' No newline at end of file
289 db_table = 'atrad_configurations'
@@ -1,127 +1,129
1 import paho.mqtt.client as mqtt
1 import paho.mqtt.client as mqtt
2 from radarsys import settings
2 from radarsys import settings
3 from radarsys.socketconfig import sio as sio
3 from radarsys.socketconfig import sio as sio
4 import numpy as np
4 import numpy as np
5 import psycopg2
5 import psycopg2
6 import os
6 import os
7
7
8 def insert(time,data):
8 def insert(time,data):
9 sql = """INSERT INTO atrad_datas(
9 sql = """INSERT INTO atrad_datas(
10 datetime,nstx,status_1,temp_cll_1,nboards_1,tempdvr_1,potincdvr_1,potretdvr_1,
10 datetime,nstx,status_1,temp_cll_1,nboards_1,tempdvr_1,potincdvr_1,potretdvr_1,
11 temp1_1,potinc1_1,potret1_1,temp2_1,potinc2_1,potret2_1,temp3_1,potinc3_1,potret3_1,
11 temp1_1,potinc1_1,potret1_1,temp2_1,potinc2_1,potret2_1,temp3_1,potinc3_1,potret3_1,
12 temp4_1,potinc4_1,potret4_1,temp5_1,potinc5_1,potret5_1,temp6_1,potinc6_1,potret6_1,
12 temp4_1,potinc4_1,potret4_1,temp5_1,potinc5_1,potret5_1,temp6_1,potinc6_1,potret6_1,
13 status_2,temp_cll_2,nboards_2,tempdvr_2,potincdvr_2,potretdvr_2,
13 status_2,temp_cll_2,nboards_2,tempdvr_2,potincdvr_2,potretdvr_2,
14 temp1_2,potinc1_2,potret1_2,temp2_2,potinc2_2,potret2_2,temp3_2,potinc3_2,potret3_2,
14 temp1_2,potinc1_2,potret1_2,temp2_2,potinc2_2,potret2_2,temp3_2,potinc3_2,potret3_2,
15 temp4_2,potinc4_2,potret4_2,temp5_2,potinc5_2,potret5_2,temp6_2,potinc6_2,potret6_2,
15 temp4_2,potinc4_2,potret4_2,temp5_2,potinc5_2,potret5_2,temp6_2,potinc6_2,potret6_2,
16 status_3,temp_cll_3,nboards_3,tempdvr_3,potincdvr_3,potretdvr_3,
16 status_3,temp_cll_3,nboards_3,tempdvr_3,potincdvr_3,potretdvr_3,
17 temp1_3,potinc1_3,potret1_3,temp2_3,potinc2_3,potret2_3,temp3_3,potinc3_3,potret3_3,
17 temp1_3,potinc1_3,potret1_3,temp2_3,potinc2_3,potret2_3,temp3_3,potinc3_3,potret3_3,
18 temp4_3,potinc4_3,potret4_3,temp5_3,potinc5_3,potret5_3,temp6_3,potinc6_3,potret6_3,
18 temp4_3,potinc4_3,potret4_3,temp5_3,potinc5_3,potret5_3,temp6_3,potinc6_3,potret6_3,
19 status_4,temp_cll_4,nboards_4,tempdvr_4,potincdvr_4,potretdvr_4,
19 status_4,temp_cll_4,nboards_4,tempdvr_4,potincdvr_4,potretdvr_4,
20 temp1_4,potinc1_4,potret1_4,temp2_4,potinc2_4,potret2_4,temp3_4,potinc3_4,potret3_4,
20 temp1_4,potinc1_4,potret1_4,temp2_4,potinc2_4,potret2_4,temp3_4,potinc3_4,potret3_4,
21 temp4_4,potinc4_4,potret4_4,temp5_4,potinc5_4,potret5_4,temp6_4,potinc6_4,potret6_4)
21 temp4_4,potinc4_4,potret4_4,temp5_4,potinc5_4,potret5_4,temp6_4,potinc6_4,potret6_4,
22 combiner1,combiner2,combiner3,combiner4)
22 VALUES(%s,%s,%s,%s,%s,%s,%s,%s,
23 VALUES(%s,%s,%s,%s,%s,%s,%s,%s,
23 %s,%s,%s,%s,%s,%s,%s,%s,%s,
24 %s,%s,%s,%s,%s,%s,%s,%s,%s,
24 %s,%s,%s,%s,%s,%s,%s,%s,%s,
25 %s,%s,%s,%s,%s,%s,%s,%s,%s,
25 %s,%s,%s,%s,%s,%s,
26 %s,%s,%s,%s,%s,%s,
26 %s,%s,%s,%s,%s,%s,%s,%s,%s,
27 %s,%s,%s,%s,%s,%s,%s,%s,%s,
27 %s,%s,%s,%s,%s,%s,%s,%s,%s,
28 %s,%s,%s,%s,%s,%s,%s,%s,%s,
28 %s,%s,%s,%s,%s,%s,
29 %s,%s,%s,%s,%s,%s,
29 %s,%s,%s,%s,%s,%s,%s,%s,%s,
30 %s,%s,%s,%s,%s,%s,%s,%s,%s,
30 %s,%s,%s,%s,%s,%s,%s,%s,%s,
31 %s,%s,%s,%s,%s,%s,%s,%s,%s,
31 %s,%s,%s,%s,%s,%s,
32 %s,%s,%s,%s,%s,%s,
32 %s,%s,%s,%s,%s,%s,%s,%s,%s,
33 %s,%s,%s,%s,%s,%s,%s,%s,%s,
33 %s,%s,%s,%s,%s,%s,%s,%s,%s);"""
34 %s,%s,%s,%s,%s,%s,%s,%s,%s,
35 %s,%s,%s,%s);"""
34 try:
36 try:
35 # connect to the PostgreSQL database
37 # connect to the PostgreSQL database
36 conn = psycopg2.connect(database="radarsys", user='docker', password='docker', host='radarsys-postgres', port= '5432')
38 conn = psycopg2.connect(database="radarsys", user='docker', password='docker', host='radarsys-postgres', port= '5432')
37 # create a new cursor
39 # create a new cursor
38 cur = conn.cursor()
40 cur = conn.cursor()
39 # execute the INSERT statement
41 # execute the INSERT statement
40 #data_tuple = [tuple(i[:]) for i in a]
42 #data_tuple = [tuple(i[:]) for i in a]
41 values = (time,) + tuple(data[0][:25])+tuple(data[1][1:25])+tuple(data[2][1:25])+tuple(data[3][1:25])
43 values = (time,) + tuple(data[0][:25])+tuple(data[1][1:25])+tuple(data[2][1:25])+tuple(data[3][1:25]) + tuple(data[0][29:31]) +tuple(data[2][27:29])
42 cur.execute(sql, values)
44 cur.execute(sql, values)
43
45
44 # get the generated id back
46 # get the generated id back
45 #vendor_id = cur.fetchone()[0]
47 #vendor_id = cur.fetchone()[0]
46
48
47 # commit the changes to the database
49 # commit the changes to the database
48 conn.commit()
50 conn.commit()
49 # close communication with the database
51 # close communication with the database
50 cur.close()
52 cur.close()
51 except (Exception, psycopg2.DatabaseError) as error:
53 except (Exception, psycopg2.DatabaseError) as error:
52 print(error)
54 print(error)
53 finally:
55 finally:
54 if conn is not None:
56 if conn is not None:
55 conn.close()
57 conn.close()
56
58
57 def maxTemperature(trs):
59 def maxTemperature(trs):
58 temps = GetTemperatures(trs)
60 temps = GetTemperatures(trs)
59 maxT_STX = [max(i) for i in temps]
61 maxT_STX = [max(i) for i in temps]
60 maxT = max(maxT_STX)
62 maxT = max(maxT_STX)
61 STXnum = maxT_STX.index(maxT)
63 STXnum = maxT_STX.index(maxT)
62 STXloc = temps[STXnum].index(maxT)
64 STXloc = temps[STXnum].index(maxT)
63 maxT_loc = 'Amp ' + str(STXnum+1)
65 maxT_loc = 'Amp ' + str(STXnum+1)
64
66
65 if STXloc == 0:
67 if STXloc == 0:
66 maxT_loc = maxT_loc + " Controller"
68 maxT_loc = maxT_loc + " Controller"
67 elif STXloc == 1:
69 elif STXloc == 1:
68 maxT_loc = maxT_loc + " Driver"
70 maxT_loc = maxT_loc + " Driver"
69 elif STXloc<8:
71 elif STXloc<8:
70 maxT_loc = maxT_loc + " PA " + str(STXloc-1)
72 maxT_loc = maxT_loc + " PA " + str(STXloc-1)
71 else:
73 else:
72 maxT_loc = maxT_loc + " Combiners"
74 maxT_loc = maxT_loc + " Combiners"
73
75
74 return maxT,maxT_loc,temps
76 return maxT,maxT_loc,temps
75
77
76 def dataConvert(msg):
78 def dataConvert(msg):
77 msgStr = str(msg.payload)
79 msgStr = str(msg.payload)
78 msgClean = [i for i in msgStr[21:-1].split("*")]
80 msgClean = [i for i in msgStr[21:-1].split("*")]
79 dataSTX = [[],[],[],[]]
81 dataSTX = [[],[],[],[]]
80 for trs,i in zip(msgClean,[0,1,2,3]) :
82 for trs,i in zip(msgClean,[0,1,2,3]) :
81 dataSTX[i]= [int(i) for i in trs[1:-1].split(",")]
83 dataSTX[i]= [int(i) for i in trs[1:-1].split(",")]
82 # Data to database
84 # Data to database
83 insert(msgStr[2:21],dataSTX)
85 insert(msgStr[2:21],dataSTX)
84 # Data to send by socket
86 # Data to send by socket
85 id_STX = dataSTX[0][0] // 4
87 id_STX = dataSTX[0][0] // 4
86 status = ''.join([msgClean[i][3] for i in [0,1,2,3]])
88 status = ''.join([msgClean[i][3] for i in [0,1,2,3]])
87 powers = [dataSTX[0][34],dataSTX[0][36],dataSTX[2][32],dataSTX[2][34],0,0,0,0]
89 powers = [dataSTX[0][34],dataSTX[0][36],dataSTX[2][32],dataSTX[2][34],0,0,0,0]
88 # alerta
90 # alerta
89 for i in range(4):
91 for i in range(4):
90 if powers[i] < 10000 and status == '1111':
92 if powers[i] < 10000 and status == '1111':
91 power[4+i] = 1
93 power[4+i] = 1
92
94
93 tmax,index,tempData = maxTemperature(dataSTX)
95 tmax,index,tempData = maxTemperature(dataSTX)
94 #Json to send
96 #Json to send
95 data = {'time':msgStr[2:21],'num':id_STX,'pow':powers,'tmax':[str(tmax),index],'status':status}
97 data = {'time':msgStr[2:21],'num':id_STX,'pow':powers,'tmax':[str(tmax),index],'status':status}
96 data_temp = {'time':msgStr[2:21],'temp':tempData}
98 data_temp = {'time':msgStr[2:21],'temp':tempData}
97 return data, data_temp
99 return data, data_temp
98
100
99 def GetTemperatures(data):
101 def GetTemperatures(data):
100 np_data = [np.array(i) for i in data]
102 np_data = [np.array(i) for i in data]
101 temps = [i[i<40] for i in np_data]
103 temps = [i[i<40] for i in np_data]
102 return [i[i>15].tolist() for i in temps]
104 return [i[i>15].tolist() for i in temps]
103
105
104 def on_connect(mqtt_client, userdata, flags, rc):
106 def on_connect(mqtt_client, userdata, flags, rc):
105 if rc == 0:
107 if rc == 0:
106 print('Connected successfullyasdss')
108 # print('Connected successfullyasdss')
107 mqtt_client.subscribe("atrad/test4")
109 mqtt_client.subscribe(os.environ.get('MQTT_TOPIC_ATRAD_RECIEVE', 'atrad/test4'))
108 print("Exito")
110 # print("Exito")
109 else:
111 else:
110 print('Bad connection. Code:', rc)
112 print('Bad connection. Code:', rc)
111
113
112 def on_message(mqtt_client, userdata, msg):
114 def on_message(mqtt_client, userdata, msg):
113 print('Received message on topic: {} with payload: {}'.format(msg.topic,msg.payload), flush=True)
115 print('Received message on topic: {} with payload: {}'.format(msg.topic,msg.payload), flush=True)
114 mainData, tempData = dataConvert(msg)
116 mainData, tempData = dataConvert(msg)
115 sio.emit('test',data = mainData)
117 sio.emit('test',data = mainData)
116 #socket for temperature details
118 #socket for temperature details
117 sio.emit('temptx'+str(mainData['num'] + 1),data = tempData)
119 sio.emit('temptx'+str(mainData['num'] + 1),data = tempData)
118
120
119 client = mqtt.Client()
121 client = mqtt.Client()
120 client.on_connect = on_connect
122 client.on_connect = on_connect
121 client.on_message = on_message
123 client.on_message = on_message
122 client.username_pw_set( '','')
124 client.username_pw_set( '','')
123 client.connect(
125 client.connect(
124 host=os.environ.get('MQTT_SERVER', '10.10.10.200'),
126 host=os.environ.get('MQTT_SERVER', '10.10.10.200'),
125 port=int(settings.os.environ.get('MQTT_PORT', 1883)),
127 port=int(settings.os.environ.get('MQTT_PORT', 1883)),
126 keepalive=int(os.environ.get('MQTT_KEEPALIVE', 60))
128 keepalive=int(os.environ.get('MQTT_KEEPALIVE', 60))
127 ) No newline at end of file
129 )
@@ -1,283 +1,283
1 {% extends "dev_conf.html" %}
1 {% extends "dev_conf.html" %}
2 {% load static %}
2 {% load static %}
3 {% block extra-head %}
3 {% block extra-head %}
4 <style>
4 <style>
5 .dot {
5 .dot {
6 height: 25px;
6 height: 25px;
7 width: 25px;
7 width: 25px;
8 background-color: #b0b3af;
8 background-color: #b0b3af;
9 border-radius: 50%;
9 border-radius: 50%;
10 display: inline-block;
10 display: inline-block;
11 }
11 }
12 .row{
12 .row{
13 margin-top: 3px;
13 margin-top: 3px;
14 margin-bottom: 3px;
14 margin-bottom: 3px;
15 }
15 }
16 .th {
16 .th {
17 vertical-align: middle;
17 vertical-align: middle;
18 }
18 }
19 </style>
19 </style>
20 <link href="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.6.1/css/bootstrap4-toggle.min.css" rel="stylesheet">
20 <link href="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.6.1/css/bootstrap4-toggle.min.css" rel="stylesheet">
21 {% endblock %}
21 {% endblock %}
22
22
23 {% block content-graph %}
23 {% block content-graph %}
24 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
24 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
25 <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/4.4.1/socket.io.min.js"></script>
25 <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/4.4.1/socket.io.min.js"></script>
26
26
27 <div class="container-fluid">
27 <div class="container-fluid">
28 <h1 class="py-4 text-center" >ATRAD Monitoring</h1>
28 <h1 class="py-4 text-center" >ATRAD Monitoring</h1>
29
29
30 <!-- First row : Control-->
30 <!-- First row : Control-->
31 <div class="row">
31 <div class="row">
32 <div class="card border-light">
32 <div class="card border-light">
33 <div class="card-body">
33 <div class="card-body">
34 <h3 class="card-title">Control</h3>
34 <h3 class="card-title">Control</h3>
35 <table class="table table-borderless">
35 <table class="table table-borderless">
36 <tbody>
36 <tbody>
37 <tr>
37 <tr>
38 <th class="align-middle">Tx1</th>
38 <th class="align-middle">Tx1</th>
39 <td>
39 <td>
40 <button type="button" class="btn btn-outline-success" id="ONBtn1"> ON </button>
40 <button type="button" class="btn btn-outline-success" id="ONBtn1"> ON </button>
41 <button type="button" class="btn btn-outline-danger" id="OFFBtn1"> OFF </button>
41 <button type="button" class="btn btn-outline-danger" id="OFFBtn1"> OFF </button>
42 </td>
42 </td>
43 </tr>
43 </tr>
44 <tr>
44 <tr>
45 <th class="align-middle">Tx2</th>
45 <th class="align-middle">Tx2</th>
46 <td>
46 <td>
47 <button type="button" class="btn btn-outline-success" id="ONBtn2"> ON </button>
47 <button type="button" class="btn btn-outline-success" id="ONBtn2"> ON </button>
48 <button type="button" class="btn btn-outline-danger" id="OFFBtn2"> OFF </button>
48 <button type="button" class="btn btn-outline-danger" id="OFFBtn2"> OFF </button>
49 </td>
49 </td>
50 </tr>
50 </tr>
51 </tbody>
51 </tbody>
52 </table>
52 </table>
53 </div>
53 </div>
54 </div>
54 </div>
55 </div>
55 </div>
56
56
57 <!-- Second row : Status and alerts -->
57 <!-- Second row : Status and alerts -->
58 <div class="row">
58 <div class="row">
59 <div class="card-deck" style="width: 90%;">
59 <div class="card-deck" style="width: 100%;">
60 <div class="card border-light">
60 <div class="card border-light">
61 <div class="card-body">
61 <div class="card-body">
62 <h4 class="card-title">Status</h4>
62 <h4 class="card-title">Status</h4>
63 <table class="table table-borderless">
63 <table class="table table-borderless">
64 <tbody>
64 <tbody>
65 <tr>
65 <tr>
66 <th scope="row">Tx1</th>
66 <th scope="row">Tx1</th>
67 <td><span id="status1" class="dot"></span></td>
67 <td><span id="status1" class="dot"></span></td>
68 <td><p id="status-text1" class="font-weight-bold">No data</p></td>
68 <td><p id="status-text1" class="font-weight-bold">No data</p></td>
69 </tr>
69 </tr>
70 <tr>
70 <tr>
71 <th scope="row">Tx2</th>
71 <th scope="row">Tx2</th>
72 <td><span id="status2" class="dot"></span></td>
72 <td><span id="status2" class="dot"></span></td>
73 <td><p id="status-text2" class="font-weight-bold">No data</p></td>
73 <td><p id="status-text2" class="font-weight-bold">No data</p></td>
74 </tr>
74 </tr>
75 </tbody>
75 </tbody>
76 </table>
76 </table>
77 </div>
77 </div>
78 </div>
78 </div>
79 <div class="card border-light">
79 <div class="card border-light">
80 <div class="card-body">
80 <div class="card-body">
81 <h4 class="card-title">High Temperature Alerts</h4>
81 <h4 class="card-title">High Temperature Alerts</h4>
82 <table class="table table-borderless">
82 <table class="table table-borderless">
83 <tbody>
83 <tbody>
84 <tr>
84 <tr>
85 <th scope="row"><p id="alerttemp-time1" class="font-weight-bold"></p></th>
85 <th scope="row"><p id="alerttemp-time1" class="font-weight-bold"></p></th>
86 <td><p id="alerttemp-1" class="font-weight-bold"></p></td>
86 <td><p id="alerttemp-1" class="font-weight-bold"></p></td>
87 <td><p id="alerttemp-loc1" class="font-weight-bold"></p></td>
87 <td><p id="alerttemp-loc1" class="font-weight-bold"></p></td>
88 </tr>
88 </tr>
89 <tr>
89 <tr>
90 <th scope="row"><p id="alerttemp-time2" class="font-weight-bold"></p></th>
90 <th scope="row"><p id="alerttemp-time2" class="font-weight-bold"></p></th>
91 <td><p id="alerttemp-2" class="font-weight-bold"></p></td>
91 <td><p id="alerttemp-2" class="font-weight-bold"></p></td>
92 <td><p id="alerttemp-loc2" class="font-weight-bold"></p></td>
92 <td><p id="alerttemp-loc2" class="font-weight-bold"></p></td>
93 </tr>
93 </tr>
94 </tbody>
94 </tbody>
95 </table>
95 </table>
96 </div>
96 </div>
97 </div>
97 </div>
98 <div class="card border-light">
98 <div class="card border-light">
99 <div class="card-body">
99 <div class="card-body">
100 <h4 class="card-title">Power Alerts</h4>
100 <h4 class="card-title">Power Alerts</h4>
101 <table class="table table-borderless">
101 <table class="table table-borderless">
102 <tbody>
102 <tbody>
103 <tr>
103 <tr>
104 <th scope="row"><p id="alertpot-time1" class="font-weight-bold"></p></th>
104 <th scope="row"><p id="alertpot-time1" class="font-weight-bold"></p></th>
105 <td><p id="alertpot-1" class="font-weight-bold"></p></td>
105 <td><p id="alertpot-1" class="font-weight-bold"></p></td>
106 <td><p id="alertpot-loc1" class="font-weight-bold"></p></td>
106 <td><p id="alertpot-loc1" class="font-weight-bold"></p></td>
107 </tr>
107 </tr>
108 <tr>
108 <tr>
109 <th scope="row"><p id="alertpot-time2" class="font-weight-bold"></p></th>
109 <th scope="row"><p id="alertpot-time2" class="font-weight-bold"></p></th>
110 <td><p id="alertpot-2" class="font-weight-bold"></p></td>
110 <td><p id="alertpot-2" class="font-weight-bold"></p></td>
111 <td><p id="alertpot-loc2" class="font-weight-bold"></p></td>
111 <td><p id="alertpot-loc2" class="font-weight-bold"></p></td>
112 </tr>
112 </tr>
113 </tbody>
113 </tbody>
114 </table>
114 </table>
115 </div>
115 </div>
116 </div>
116 </div>
117 </div>
117 </div>
118 </div>
118 </div>
119
119
120 <!-- Third row : power graph-->
120 <!-- Third row : power graph-->
121 <div class="row ">
121 <div class="row ">
122 <div class="card border-light">
122 <div class="card border-light">
123 <div class="card-body">
123 <div class="card-body">
124 <h3 class="card-title">Power graph</h3>
124 <h3 class="card-title">Power graph</h3>
125 <div class="row justify-content-md-center">
125 <div class="row justify-content-md-center">
126 <div class="col-md-9">
126 <div class="col-md-9">
127 <div id="plot-pot"></div>
127 <div id="plot-pot"></div>
128 </div>
128 </div>
129 <div class="col-md-3 text-end">
129 <div class="col-md-3 text-end">
130 <div class="card-body">
130 <div class="card-body">
131 <table class="table" style="max-width:170px;">
131 <table class="table" style="max-width:170px;">
132 <tbody>
132 <tbody>
133 <tr>
133 <tr>
134 <th scope="col"># Tx</th>
134 <th scope="col"># Tx</th>
135 <th scope="col">Power (kW)</th>
135 <th scope="col">Power (kW)</th>
136 </tr>
136 </tr>
137 <tr>
137 <tr>
138 <th scope="row">1</th>
138 <th scope="row">1</th>
139 <td><p id="pot1"></p></td>
139 <td><p id="pot1"></p></td>
140 </tr>
140 </tr>
141 <tr>
141 <tr>
142 <th scope="row">2</th>
142 <th scope="row">2</th>
143 <td><p id="pot2"></p></td>
143 <td><p id="pot2"></p></td>
144 </tr>
144 </tr>
145 </tbody>
145 </tbody>
146 </table>
146 </table>
147 </div>
147 </div>
148 <div class="card-body">
148 <div class="card-body">
149 <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#Pot-1">T1</button>
149 <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#Pot-1">T1</button>
150 <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#Pot-2">T2</button>
150 <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#Pot-2">T2</button>
151 </div>
151 </div>
152 </div>
152 </div>
153 </div>
153 </div>
154 </div>
154 </div>
155 </div>
155 </div>
156 </div>
156 </div>
157 <!--Temperature Graph-->
157 <!--Temperature Graph-->
158 <div class="row ">
158 <div class="row ">
159 <div class="card border-light">
159 <div class="card border-light">
160 <div class="card-body">
160 <div class="card-body">
161 <h3 class="card-title">Temperature graph</h3>
161 <h3 class="card-title">Temperature graph</h3>
162 <div class="row justify-content-md-center">
162 <div class="row justify-content-md-center">
163 <div class="col-md-9">
163 <div class="col-md-9">
164 <div id="plot-temp"></div>
164 <div id="plot-temp"></div>
165 </div>
165 </div>
166 <div class="col-md-3 text-end">
166 <div class="col-md-3 text-end">
167 <div class="card-body">
167 <div class="card-body">
168 <table class="table table-hover" style="max-width:170px;">
168 <table class="table table-hover" style="max-width:170px;">
169 <tbody>
169 <tbody>
170 <tr>
170 <tr>
171 <th scope="col"># Tx</th>
171 <th scope="col"># Tx</th>
172 <th scope="col">Temperature (°C)</th>
172 <th scope="col">Temperature (°C)</th>
173 </tr>
173 </tr>
174 <tr class="clickable-row" data-href="{% url 'url_tx_atrad' dev_conf.id 'tx1' %}">
174 <tr class="clickable-row" data-href="{% url 'url_tx_atrad' dev_conf.id 'tx1' %}">
175 <th scope="row">1</th>
175 <th scope="row">1</th>
176 <td><p id="temp1"></p></td>
176 <td><p id="temp1"></p></td>
177 </tr>
177 </tr>
178 <tr class="clickable-row" data-href="{% url 'url_tx_atrad' dev_conf.id 'tx2' %}">
178 <tr class="clickable-row" data-href="{% url 'url_tx_atrad' dev_conf.id 'tx2' %}">
179 <th scope="row">2</th>
179 <th scope="row">2</th>
180 <td><p id="temp2"></p></td>
180 <td><p id="temp2"></p></td>
181 </tr>
181 </tr>
182 </tbody>
182 </tbody>
183 </table>
183 </table>
184 </div>
184 </div>
185 </div>
185 </div>
186 </div>
186 </div>
187 </div>
187 </div>
188 </div>
188 </div>
189 </div>
189 </div>
190 </div>
190 </div>
191
191
192 <!--Modales-->
192 <!--Modales-->
193 <div class="modal fade" id="Pot-1" role="dialog">
193 <div class="modal fade" id="Pot-1" role="dialog">
194 <div class="modal-dialog modal-lg" style="max-width:50%;min-width:750px">
194 <div class="modal-dialog modal-lg" style="max-width:50%;min-width:928px">
195 <div class="modal-content">
195 <div class="modal-content">
196 <div class="modal-header">
196 <div class="modal-header">
197 <h4 class="modal-title">Potencia incidente - Transmisor 1</h4>
197 <h4 class="modal-title">Incident Power - Transmitter 1 </h4>
198 <button type="button" class="close" data-dismiss="modal">&times;</button>
198 <button type="button" class="close" data-dismiss="modal">&times;</button>
199 </div>
199 </div>
200 <div class="modal-body">
200 <div class="modal-body">
201 <div class="row">
201 <div class="row">
202 <div class="col">
202 <div class="col">
203 <div id="plot-pot-t1"></div>
203 <div id="plot-pot-t1"></div>
204 </div>
204 </div>
205 <div class="col">
205 <div class="col">
206 <table class="table" style="max-width: 150px;">
206 <table class="table" style="max-width: 150px;">
207 <tbody>
207 <tbody>
208 <tr>
208 <tr>
209 <th scope="col"># Amp</th>
209 <th scope="col"># Amp</th>
210 <th scope="col">Potencia (kW)</th>
210 <th scope="col">Power (kW)</th>
211 </tr>
211 </tr>
212 <tr>
212 <tr>
213 <th scope="row">P1</th>
213 <th scope="row">P1</th>
214 <td><p id="pot1-1"></p></td>
214 <td><p id="pot1-1"></p></td>
215 </tr>
215 </tr>
216 <tr>
216 <tr>
217 <th scope="row">P2</th>
217 <th scope="row">P2</th>
218 <td><p id="pot1-2"></p></td>
218 <td><p id="pot1-2"></p></td>
219 </tr>
219 </tr>
220 <tr>
220 <tr>
221 <th scope="row">P3</th>
221 <th scope="row">P3</th>
222 <td><p id="pot1-3"></p></td>
222 <td><p id="pot1-3"></p></td>
223 </tr>
223 </tr>
224 <tr>
224 <tr>
225 <th scope="row">P4</th>
225 <th scope="row">P4</th>
226 <td><p id="pot1-4"></p></td>
226 <td><p id="pot1-4"></p></td>
227 </tr>
227 </tr>
228 </tbody>
228 </tbody>
229 </table>
229 </table>
230 </div>
230 </div>
231 </div>
231 </div>
232 </div>
232 </div>
233 </div>
233 </div>
234 </div>
234 </div>
235 </div>
235 </div>
236
236
237 <div class="modal fade" id="Pot-2" role="dialog">
237 <div class="modal fade" id="Pot-2" role="dialog">
238 <div class="modal-dialog modal-lg" style="max-width:50%;min-width:750px">
238 <div class="modal-dialog modal-lg" style="max-width:50%;min-width:928px">
239 <div class="modal-content">
239 <div class="modal-content">
240 <div class="modal-header">
240 <div class="modal-header">
241 <h4 class="modal-title">Potencia incidente - Transmisor 2</h4>
241 <h4 class="modal-title">Incident Power - Transmitter 2</h4>
242 <button type="button" class="close" data-dismiss="modal">&times;</button>
242 <button type="button" class="close" data-dismiss="modal">&times;</button>
243 </div>
243 </div>
244 <div class="modal-body">
244 <div class="modal-body">
245 <div class="row">
245 <div class="row">
246 <div class="col">
246 <div class="col">
247 <div id="plot-pot-t2"></div>
247 <div id="plot-pot-t2"></div>
248 </div>
248 </div>
249 <div class="col">
249 <div class="col">
250 <table class="table" style="max-width: 150px;">
250 <table class="table" style="max-width: 150px;">
251 <tbody>
251 <tbody>
252 <tr>
252 <tr>
253 <th scope="col"># Amp</th>
253 <th scope="col"># Amp</th>
254 <th scope="col">Potencia (kW)</th>
254 <th scope="col">Power (kW)</th>
255 </tr>
255 </tr>
256 <tr>
256 <tr>
257 <th scope="row">P1</th>
257 <th scope="row">P1</th>
258 <td><p id="pot2-1"></p></td>
258 <td><p id="pot2-1"></p></td>
259 </tr>
259 </tr>
260 <tr>
260 <tr>
261 <th scope="row">P2</th>
261 <th scope="row">P2</th>
262 <td><p id="pot2-2"></p></td>
262 <td><p id="pot2-2"></p></td>
263 </tr>
263 </tr>
264 <tr>
264 <tr>
265 <th scope="row">P3</th>
265 <th scope="row">P3</th>
266 <td><p id="pot2-3"></p></td>
266 <td><p id="pot2-3"></p></td>
267 </tr>
267 </tr>
268 <tr>
268 <tr>
269 <th scope="row">P4</th>
269 <th scope="row">P4</th>
270 <td><p id="pot2-4"></p></td>
270 <td><p id="pot2-4"></p></td>
271 </tr>
271 </tr>
272 </tbody>
272 </tbody>
273 </table>
273 </table>
274 </div>
274 </div>
275 </div>
275 </div>
276 </div>
276 </div>
277 </div>
277 </div>
278 </div>
278 </div>
279 </div>
279 </div>
280
280
281 <script src="{% static 'js/atrad-conf.js' %}"></script>
281 <script src="{% static 'js/atrad-mainpage.js' %}"></script>
282
282
283 {% endblock %} No newline at end of file
283 {% endblock %}
@@ -1,168 +1,174
1 {% extends "base.html" %}
1 {% extends "base.html" %}
2 {% load static %}
2 {% load static %}
3 {% block title %}ATRAD Tx{{id_tx}} Temperatures{% endblock %}
3 {% block title %}ATRAD Tx{{id_tx}} Temperatures{% endblock %}
4 {% block content-title %}{{title}}{% endblock %}
4 {% block content-title %}{{title}}{% endblock %}
5 {% block content-suptitle %}Tx {{id_tx}}{% endblock %}
5 {% block content-suptitle %}Tx {{id_tx}}{% endblock %}
6
6
7 {% block extra-head %}
7 {% block extra-head %}
8 <style>
8 <style>
9 .panel-body {
9 .panel-body {
10 background:#ffffff;}
10 background:#ffffff;}
11 .panel-footer {
11 .panel-footer {
12 background:#ffffff;}
12 background:#ffffff;}
13 .card-body {
13 .card-body {
14 padding-top: 1px;
14 padding-top: 1px;
15 padding-bottom: 1px;
15 padding-bottom: 1px;
16 }
16 }
17 </style>
17 </style>
18 {% endblock %}
18 {% endblock %}
19
19
20 {% block content-graph %}
20 {% block content-graph %}
21 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
21 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
22 <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/4.4.1/socket.io.min.js"></script>
22 <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/4.4.1/socket.io.min.js"></script>
23
23
24 <div class="container-fluid">
24 <div class="container-fluid">
25 <!-- Temperatura generales -->
25 <!-- Temperatura generales -->
26 <div class="row ">
26 <div class="row ">
27 <div class="panel panel-default">
27 <div class="panel panel-default">
28 <div class="panel-heading">
28 <div class="panel-heading">
29 <h3 class="panel-title">Amplifiers</h3>
29 <h3 class="panel-title">Amplifiers Temperature (°C)</h3>
30 </div>
30 </div>
31 <div class="panel-body">
31 <div class="panel-body">
32 <div class="card border-0">
32 <div class="card border-0">
33 <div class="row">
33 <div class="row">
34 <div class="card-body">
34 <div class="card-body">
35 <h5 class="card-title">Amplifier 1</h5>
35 <h5 class="card-title">Amplifier 1</h5>
36 <div id="plot-temp-1"></div>
36 <div id="plot-temp-1"></div>
37 </div>
37 </div>
38 <div class="card-body">
38 <div class="card-body">
39 <h5 class="card-title">Amplifier 2</h5>
39 <h5 class="card-title">Amplifier 2</h5>
40 <div id="plot-temp-2"></div>
40 <div id="plot-temp-2"></div>
41 </div>
41 </div>
42 <div class="card-body">
42 <div class="card-body">
43 <h5 class="card-title">Amplifier 3</h5>
43 <h5 class="card-title">Amplifier 3</h5>
44 <div id="plot-temp-3"></div>
44 <div id="plot-temp-3"></div>
45 </div>
45 </div>
46 <div class="card-body">
46 <div class="card-body">
47 <h5 class="card-title">Amplifier 4</h5>
47 <h5 class="card-title">Amplifier 4</h5>
48 <div id="plot-temp-4"></div>
48 <div id="plot-temp-4"></div>
49 </div>
49 </div>
50 </div>
50 </div>
51 </div>
51 </div>
52 </div>
52 </div>
53 </div>
53 </div>
54 </div>
54 </div>
55 <div class="row">
55 <div class="row">
56 <div class="panel panel-default">
56 <div class="panel panel-default">
57 <div class="panel-heading">
57 <div class="panel-heading">
58 <h3 class="panel-title">Controllers and combiners</h3>
58 <h3 class="panel-title">Combiners Temperature °C</h3>
59 </div>
59 </div>
60 <div class="panel-body">
60 <div class="panel-body">
61 <div id="plot-temp-0"></div>
61 <div id="plot-temp-0"></div>
62 </div>
62 </div>
63 </div>
63 </div>
64 </div>
64 </div>
65 </div>
65 </div>
66
66
67 <script>
67 <script>
68 $(document).ready(function() {
68 $(document).ready(function() {
69 var socket = io.connect('http://' + document.domain + ':' + location.port);
69 var socket = io.connect('http://' + document.domain + ':' + location.port);
70 socket.on('connect', function(data) {
70 socket.on('connect', function(data) {
71 console.log('Connecting... OK');
71 console.log('Connecting... OK');
72 var basedata = JSON.parse('{{data|safe}}')
72 var basedata = JSON.parse('{{data|safe}}')
73 console.log(basedata);
73 console.log(basedata);
74 makePlot("plot-temp-0",4,["PC1","PC2","PC3","PC4"],[10,40]);
74 makePlot("plot-temp-0",4,["PC1","PC2","PC3","PC4"],[10,40],basedata);
75 makePlot2("plot-temp-1",6,["PA1","PA2","PA3","PA4","PA5","PA6"],[10,40],basedata)
75 makePlot2("plot-temp-1",6,["PA1","PA2","PA3","PA4","PA5","PA6"],[10,40],basedata)
76 makePlot2("plot-temp-2",6,["PA1","PA2","PA3","PA4","PA5","PA6"],[10,40],basedata)
76 makePlot2("plot-temp-2",6,["PA1","PA2","PA3","PA4","PA5","PA6"],[10,40],basedata)
77 makePlot2("plot-temp-3",6,["PA1","PA2","PA3","PA4","PA5","PA6"],[10,40],basedata)
77 makePlot2("plot-temp-3",6,["PA1","PA2","PA3","PA4","PA5","PA6"],[10,40],basedata)
78 makePlot2("plot-temp-4",6,["PA1","PA2","PA3","PA4","PA5","PA6"],[10,40],basedata)
78 makePlot2("plot-temp-4",6,["PA1","PA2","PA3","PA4","PA5","PA6"],[10,40],basedata)
79 })
79 })
80 socket.on("temptx"+"{{id_tx}}", function(data) {
80 socket.on("temptx"+"{{id_tx}}", function(data) {
81 var controllers = [data.temp[0][1],data.temp[1][1],data.temp[2][1],data.temp[3][1]]
81 var combiners = [data.temp[0][8],data.temp[0][9],data.temp[2][8],data.temp[2][9]]
82 streamPlot2("plot-temp-0",data.time,controllers,4);
82 streamPlot2("plot-temp-0",data.time,controllers,4);
83 streamPlot2("plot-temp-1",data.time,data.temp[0].slice(2,8),6);
83 streamPlot2("plot-temp-1",data.time,data.temp[0].slice(2,8),6);
84 streamPlot2("plot-temp-2",data.time,data.temp[1].slice(2,8),6);
84 streamPlot2("plot-temp-2",data.time,data.temp[1].slice(2,8),6);
85 streamPlot2("plot-temp-3",data.time,data.temp[2].slice(2,8),6);
85 streamPlot2("plot-temp-3",data.time,data.temp[2].slice(2,8),6);
86 streamPlot2("plot-temp-4",data.time,data.temp[3].slice(2,8),6);
86 streamPlot2("plot-temp-4",data.time,data.temp[3].slice(2,8),6);
87 })
87 })
88 });
88 });
89
89
90 function makePlot(div, n=1, names=["", ""],ranges){
90 function makePlot(div, n=1, names=["", ""],ranges,data){
91 var plotDiv = document.getElementById(div);
91 var plotDiv = document.getElementById(div);
92 var traces = [];
92 var traces = [];
93 var keys = ['combiner1','combiner2','combiner3','combiner4']
93 for (let i = 0; i < n; i++) {
94 for (let i = 0; i < n; i++) {
94 traces.push({x: [], y: [],mode: 'lines', name: names[i]});
95 var x = [];
96 var y = [];
97 for(let j = 0;j < data.length; j++){
98 x[j] = data[j].datetime;
99 y[j] = data[j][keys[i]];
100 }
101 traces.push({x:x, y:y,mode: 'lines', name: names[i]});
95 }
102 }
96 traces.push({x: [], y: [],mode: 'lines',line: {color:'rgb(219, 64, 82)',dash: 'dot',width: 2},name:"nominal",showlegend: false});
97 var yrange = ranges;
103 var yrange = ranges;
98 var layout = {
104 var layout = {
99 width: 505,
105 width: 505,
100 height: 300,
106 height: 300,
101 font: {size: 12},
107 font: {size: 12},
102 margin: { t: 10, b:50, r:15 ,l:15},
108 margin: { t: 10, b:50, r:15 ,l:15},
103 xaxis: {
109 xaxis: {
104 type: 'date'
110 type: 'date'
105 },
111 },
106 yaxis: {
112 yaxis: {
107 range: yrange,
113 range: yrange,
108 },
114 },
109 legend: {"orientation": "h"},
115 legend: {"orientation": "h"},
110 };
116 };
111 var config = {responsive: true}
117 var config = {responsive: true}
112 Plotly.newPlot(plotDiv, traces, layout,config);
118 Plotly.newPlot(plotDiv, traces, layout,config);
113 };
119 };
114
120
115 function makePlot2(div, n=1, names=["", ""],ranges,data){
121 function makePlot2(div, n=1, names=["", ""],ranges,data,keys){
116 var plotDiv = document.getElementById(div);
122 var plotDiv = document.getElementById(div);
117 var traces = [];
123 var traces = [];
118 var keys = ['temp1','temp2','temp3','temp4','temp5','temp6']
124 var keys = ['temp1','temp2','temp3','temp4','temp5','temp6']
119
125
120 for (let i = 0; i < n; i++) {
126 for (let i = 0; i < n; i++) {
121 var x = [];
127 var x = [];
122 var y = [];
128 var y = [];
123 for(let j = 0;j < data.length; j++){
129 for(let j = 0;j < data.length; j++){
124 x[j] = data[j].datetime;
130 x[j] = data[j].datetime;
125 y[j] = data[j][keys[i]+'_'+div.charAt(10)];
131 y[j] = data[j][keys[i]+'_'+div.charAt(10)];
126 }
132 }
127 traces.push({x:x, y:y,mode: 'lines', name: names[i]});
133 traces.push({x:x, y:y,mode: 'lines', name: names[i]});
128 }
134 }
129 var yrange = ranges;
135 var yrange = ranges;
130 var layout = {
136 var layout = {
131 width: 505,
137 width: 505,
132 height: 400,
138 height: 300,
133 font: {size: 12},
139 font: {size: 12},
134 margin: { t: 10, b:50, r:15 ,l:15},
140 margin: { t: 10, b:50, r:15 ,l:15},
135 xaxis: {
141 xaxis: {
136 type: 'date'
142 type: 'date'
137 },
143 },
138 yaxis: {
144 yaxis: {
139 range: yrange,
145 range: yrange,
140 },
146 },
141 legend: {"orientation": "h"},
147 legend: {"orientation": "h"},
142 };
148 };
143 var config = {responsive: true}
149 var config = {responsive: true}
144 Plotly.newPlot(plotDiv, traces, layout,config);
150 Plotly.newPlot(plotDiv, traces, layout,config);
145 };
151 };
146
152
147 function streamPlot2(div,x,y,cant){
153 function streamPlot2(div,x,y,cant){
148 var plotDiv = document.getElementById(div);
154 var plotDiv = document.getElementById(div);
149 if (plotDiv.data[0].x.length > 8){
155 if (plotDiv.data[0].x.length > 8){
150 for(let i=0;i<cant;i++){
156 for(let i=0;i<cant;i++){
151 plotDiv.data[i].x = plotDiv.data[i].x.slice(-11)
157 plotDiv.data[i].x = plotDiv.data[i].x.slice(-11)
152 plotDiv.data[i].y = plotDiv.data[i].y.slice(-11)
158 plotDiv.data[i].y = plotDiv.data[i].y.slice(-11)
153 }
159 }
154 }
160 }
155 var tm = [];
161 var tm = [];
156 var values = [];
162 var values = [];
157 var index = []
163 var index = []
158 for(let i=0;i<cant;i++){
164 for(let i=0;i<cant;i++){
159 values[i]=[y[i]];
165 values[i]=[y[i]];
160 tm[i] = [x];
166 tm[i] = [x];
161 index[i] = i;
167 index[i] = i;
162 }
168 }
163 var data_update = {x: tm, y: values}
169 var data_update = {x: tm, y: values}
164 Plotly.extendTraces(plotDiv, data_update,index)
170 Plotly.extendTraces(plotDiv, data_update,index)
165 };
171 };
166
172
167 </script>
173 </script>
168 {% endblock %} No newline at end of file
174 {% endblock %}
@@ -1,125 +1,133
1 from django.shortcuts import redirect, render, get_object_or_404
1 from django.shortcuts import redirect, render, get_object_or_404
2 from django.contrib import messages
2 from django.contrib import messages
3 from django.http import HttpResponse
3 from django.http import HttpResponse
4 from django.template.loader import get_template
4 from django.template.loader import get_template
5
5
6 from apps.main.models import Experiment
6 from apps.main.models import Experiment
7 from .models import ATRADConfiguration, ATRADData
7 from .models import ATRADConfiguration, ATRADData
8
8
9 from .forms import ATRADConfigurationForm, UploadFileForm
9 from .forms import ATRADConfigurationForm, UploadFileForm
10 from apps.main.views import sidebar
10 from apps.main.views import sidebar
11
11
12 import requests
12 import requests
13 import json
13 import json
14
14
15 import os
15 import os
16 from django.http import JsonResponse
16 from django.http import JsonResponse
17 from .mqtt import client as mqtt_client
17 from .mqtt import client as mqtt_client
18 from radarsys.socketconfig import sio as sio
18 from radarsys.socketconfig import sio as sio
19 from datetime import timedelta
19 from datetime import timedelta
20 from datetime import datetime
20
21
21 def atrad_conf(request, id_conf):
22 def atrad_conf(request, id_conf):
22
23
23 conf = get_object_or_404(ATRADConfiguration, pk=id_conf)
24 conf = get_object_or_404(ATRADConfiguration, pk=id_conf)
24
25
25 ip=conf.device.ip_address
26 ip=conf.device.ip_address
26 port=conf.device.port_address
27 port=conf.device.port_address
27
28
28 kwargs = {}
29 kwargs = {}
29
30
30 kwargs['status'] = conf.device.get_status_display()
31 kwargs['status'] = conf.device.get_status_display()
31
32
32 kwargs['dev_conf'] = conf
33 kwargs['dev_conf'] = conf
33 kwargs['dev_conf_keys'] = ['label',
34 kwargs['dev_conf_keys'] = ['label',
34 'topic']
35 'topic']
35
36
36 kwargs['title'] = 'ATRAD Configuration'
37 kwargs['title'] = 'ATRAD Configuration'
37 kwargs['suptitle'] = 'Details'
38 kwargs['suptitle'] = 'Details'
38
39
39 kwargs['button'] = 'Edit Configuration'
40 kwargs['button'] = 'Edit Configuration'
40
41
41 #kwargs['no_play'] = True
42 #kwargs['no_play'] = True
42 ###### SIDEBAR ######
43 ###### SIDEBAR ######
43 kwargs.update(sidebar(conf=conf))
44 kwargs.update(sidebar(conf=conf))
44
45
45 return render(request, 'atrad_conf.html', kwargs)
46 return render(request, 'atrad_conf.html', kwargs)
46
47
47 def atrad_tx(request, id_conf, id_tx):
48 def atrad_tx(request, id_conf, id_tx):
48 kwargs = {}
49 kwargs = {}
49 kwargs['id_tx'] = id_tx[-1]
50 kwargs['id_tx'] = id_tx[-1]
50 kwargs['title'] = 'Temperature Details'
51 kwargs['title'] = 'Temperature Details'
51 kwargs['button'] = 'Edit Configuration'
52 kwargs['button'] = 'Edit Configuration'
52 time = ATRADData.objects.last().datetime
53 try:
54 time = ATRADData.objects.last().datetime
55 except:
56 time = datetime.now()
57
53 id_stx = (int(id_tx[-1])-1)*4+1
58 id_stx = (int(id_tx[-1])-1)*4+1
54 mydata = ATRADData.objects.filter(datetime__gte = (time-timedelta(hours=1)),nstx = id_stx).values('datetime','temp1_1','temp2_1','temp3_1','temp4_1','temp5_1','temp6_1',
59 mydata = ATRADData.objects.filter(datetime__gte = (time-timedelta(hours=1)),nstx = id_stx).values('datetime',
55 'temp1_2','temp2_2','temp3_2','temp4_2','temp5_2','temp6_2','temp1_3','temp2_3','temp3_3','temp4_3','temp5_3','temp6_3',
60 'temp1_1','temp2_1','temp3_1','temp4_1','temp5_1','temp6_1',
56 'temp1_4','temp2_4','temp3_4','temp4_4','temp5_4','temp6_4')
61 'temp1_2','temp2_2','temp3_2','temp4_2','temp5_2','temp6_2',
62 'temp1_3','temp2_3','temp3_3','temp4_3','temp5_3','temp6_3',
63 'temp1_4','temp2_4','temp3_4','temp4_4','temp5_4','temp6_4',
64 'combiner1','combiner2','combiner3','combiner4')
57 kwargs['data'] = json.dumps(list(mydata),default=str)
65 kwargs['data'] = json.dumps(list(mydata),default=str)
58 return render(request, 'atrad_tx.html', kwargs)
66 return render(request, 'atrad_tx.html', kwargs)
59
67
60 def QuerytoStr(data):
68 def QuerytoStr(data):
61 time = data[0]
69 time = data[0]
62 strdata = str(time)
70 strdata = str(time)
63 return strdata
71 return strdata
64
72
65 def atrad_conf_edit(request, id_conf):
73 def atrad_conf_edit(request, id_conf):
66
74
67 conf = get_object_or_404(ATRADConfiguration, pk=id_conf)
75 conf = get_object_or_404(ATRADConfiguration, pk=id_conf)
68
76
69 if request.method=='GET':
77 if request.method=='GET':
70 form = ATRADConfigurationForm(instance=conf)
78 form = ATRADConfigurationForm(instance=conf)
71
79
72 if request.method=='POST':
80 if request.method=='POST':
73 form = ATRADConfigurationForm(request.POST, instance=conf)
81 form = ATRADConfigurationForm(request.POST, instance=conf)
74
82
75 if form.is_valid():
83 if form.is_valid():
76 if conf.topic == None: conf.topic = 0
84 if conf.topic == None: conf.topic = 0
77
85
78 conf = form.save(commit=False)
86 conf = form.save(commit=False)
79
87
80 if conf.verify_frequencies():
88 if conf.verify_frequencies():
81 conf.save()
89 conf.save()
82 return redirect('url_atrad_conf', id_conf=conf.id)
90 return redirect('url_atrad_conf', id_conf=conf.id)
83
91
84 kwargs = {}
92 kwargs = {}
85 kwargs['id_dev'] = conf.id
93 kwargs['id_dev'] = conf.id
86 kwargs['form'] = form
94 kwargs['form'] = form
87 kwargs['title'] = 'Device Configuration'
95 kwargs['title'] = 'Device Configuration'
88 kwargs['suptitle'] = 'Edit'
96 kwargs['suptitle'] = 'Edit'
89 kwargs['button'] = 'Save'
97 kwargs['button'] = 'Save'
90
98
91 return render(request, 'atrad_conf_edit.html', kwargs)
99 return render(request, 'atrad_conf_edit.html', kwargs)
92
100
93 def publish_message(request):
101 def publish_message(request):
94 rc, mid = mqtt_client.publish('test/data2',1)
102 rc, mid = mqtt_client.publish('test/data2',1)
95 return JsonResponse({'code1': 'HIKA', 'code2': 'LUCAS'})
103 return JsonResponse({'code1': 'HIKA', 'code2': 'LUCAS'})
96
104
97 def monitor(request):
105 def monitor(request):
98 kwargs = {'no_sidebar': True}
106 kwargs = {'no_sidebar': True}
99 return render(request, 'monitor.html', kwargs)
107 return render(request, 'monitor.html', kwargs)
100
108
101 def atrad_prueba(request):
109 def atrad_prueba(request):
102 keys = ['id','temp1','temp2','temp3','temp4','temp5','temp6']
110 keys = ['id','temp1','temp2','temp3','temp4','temp5','temp6']
103 time = ATRADData.objects.last().datetime
111 time = ATRADData.objects.last().datetime
104 mydata = ATRADData.objects.filter(datetime__gte = (time-timedelta(hours=1))).values('id','temp1_1','temp2_1','temp3_1','temp4_1','temp5_1','temp6_1',
112 mydata = ATRADData.objects.filter(datetime__gte = (time-timedelta(hours=1))).values('id','temp1_1','temp2_1','temp3_1','temp4_1','temp5_1','temp6_1',
105 'temp1_2','temp2_2','temp3_2','temp4_2','temp5_2','temp6_2','temp1_3','temp2_3','temp3_3','temp4_3','temp5_3','temp6_3',
113 'temp1_2','temp2_2','temp3_2','temp4_2','temp5_2','temp6_2','temp1_3','temp2_3','temp3_3','temp4_3','temp5_3','temp6_3',
106 'temp1_4','temp2_4','temp3_4','temp4_4','temp5_4','temp6_4')
114 'temp1_4','temp2_4','temp3_4','temp4_4','temp5_4','temp6_4')
107 template = get_template('prueba.html')
115 template = get_template('prueba.html')
108 context = {
116 context = {
109 'last' : time,
117 'last' : time,
110 'temps': mydata,
118 'temps': mydata,
111 'keys' : keys,
119 'keys' : keys,
112 }
120 }
113 return HttpResponse(template.render(context, request))
121 return HttpResponse(template.render(context, request))
114
122
115 @sio.on('connection-bind')
123 @sio.on('connection-bind')
116 def atrad_connection_bind(sid, data):
124 def atrad_connection_bind(sid, data):
117 print("sid:",sid,"data",data)
125 print("sid:",sid,"data",data)
118
126
119 @sio.on('disconnect')
127 @sio.on('disconnect')
120 def atrad_disconnect(sid):
128 def atrad_disconnect(sid):
121 print("Disconnected")
129 print("Disconnected")
122
130
123 @sio.event
131 @sio.event
124 def atrad_control_event(sid,message):
132 def atrad_control_event(sid,message):
125 mqtt_client.publish('test/data2', json.dumps(message)) No newline at end of file
133 mqtt_client.publish(os.environ.get('MQTT_TOPIC_ATRAD_CONTROL', 'atrad/test2'), json.dumps(message)) No newline at end of file
@@ -1,409 +1,409
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.urls import reverse
6 from django.urls 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 FILTER = '{"id":1, "clock": 60, "multiplier": 5, "frequency": 49.92, "f_decimal": 721554506, "fir": 2, "cic_2": 12, "cic_5": 25}'
31 FILTER = '{"id":1, "clock": 60, "multiplier": 5, "frequency": 49.92, "f_decimal": 721554506, "fir": 2, "cic_2": 12, "cic_5": 25}'
32
32
33 class JARSFilter(models.Model):
33 class JARSFilter(models.Model):
34
34
35 JARS_NBITS = 32
35 JARS_NBITS = 32
36
36
37 name = models.CharField(verbose_name='Name', max_length=60, unique=True, default='')
37 name = models.CharField(verbose_name='Name', max_length=60, unique=True, default='')
38 clock = models.FloatField(verbose_name='Clock In (MHz)', validators=[
38 clock = models.FloatField(verbose_name='Clock In (MHz)', validators=[
39 MinValueValidator(5), MaxValueValidator(75)], null=True, default=60)
39 MinValueValidator(5), MaxValueValidator(75)], null=True, default=60)
40 multiplier = models.PositiveIntegerField(verbose_name='Multiplier', validators=[
40 multiplier = models.PositiveIntegerField(verbose_name='Multiplier', validators=[
41 MinValueValidator(1), MaxValueValidator(20)], default=5)
41 MinValueValidator(1), MaxValueValidator(20)], default=5)
42 frequency = models.FloatField(verbose_name='Frequency (MHz)', validators=[
42 frequency = models.FloatField(verbose_name='Frequency (MHz)', validators=[
43 MaxValueValidator(150)], null=True, default=49.9200)
43 MaxValueValidator(150)], null=True, default=49.9200)
44 f_decimal = models.BigIntegerField(verbose_name='Frequency (Decimal)', validators=[
44 f_decimal = models.BigIntegerField(verbose_name='Frequency (Decimal)', validators=[
45 MinValueValidator(-9223372036854775808), MaxValueValidator(2**JARS_NBITS-1)], null=True, default=721554505)
45 MinValueValidator(-9223372036854775808), MaxValueValidator(2**JARS_NBITS-1)], null=True, default=721554505)
46 cic_2 = models.PositiveIntegerField(verbose_name='CIC2', validators=[
46 cic_2 = models.PositiveIntegerField(verbose_name='CIC2', validators=[
47 MinValueValidator(2), MaxValueValidator(100)], default=10)
47 MinValueValidator(2), MaxValueValidator(100)], default=10)
48 scale_cic_2 = models.PositiveIntegerField(verbose_name='Scale CIC2', validators=[
48 scale_cic_2 = models.PositiveIntegerField(verbose_name='Scale CIC2', validators=[
49 MinValueValidator(0), MaxValueValidator(6)], default=1)
49 MinValueValidator(0), MaxValueValidator(6)], default=1)
50 cic_5 = models.PositiveIntegerField(verbose_name='CIC5', validators=[
50 cic_5 = models.PositiveIntegerField(verbose_name='CIC5', validators=[
51 MinValueValidator(1), MaxValueValidator(100)], default=1)
51 MinValueValidator(1), MaxValueValidator(100)], default=1)
52 scale_cic_5 = models.PositiveIntegerField(verbose_name='Scale CIC5', validators=[
52 scale_cic_5 = models.PositiveIntegerField(verbose_name='Scale CIC5', validators=[
53 MinValueValidator(0), MaxValueValidator(20)], default=5)
53 MinValueValidator(0), MaxValueValidator(20)], default=5)
54 fir = models.PositiveIntegerField(verbose_name='FIR', validators=[
54 fir = models.PositiveIntegerField(verbose_name='FIR', validators=[
55 MinValueValidator(1), MaxValueValidator(100)], default=6)
55 MinValueValidator(1), MaxValueValidator(100)], default=6)
56 scale_fir = models.PositiveIntegerField(verbose_name='Scale FIR', validators=[
56 scale_fir = models.PositiveIntegerField(verbose_name='Scale FIR', validators=[
57 MinValueValidator(0), MaxValueValidator(7)], default=3)
57 MinValueValidator(0), MaxValueValidator(7)], default=3)
58 number_taps = models.PositiveIntegerField(verbose_name='Number of taps', validators=[
58 number_taps = models.PositiveIntegerField(verbose_name='Number of taps', validators=[
59 MinValueValidator(1), MaxValueValidator(256)], default=4)
59 MinValueValidator(1), MaxValueValidator(256)], default=4)
60 taps = models.CharField(verbose_name='Taps', max_length=1600, default='0')
60 taps = models.CharField(verbose_name='Taps', max_length=1600, default='0')
61
61
62 class Meta:
62 class Meta:
63 db_table = 'jars_filters'
63 db_table = 'jars_filters'
64
64
65 def __unicode__(self):
65 def __unicode__(self):
66 return u'%s' % (self.name)
66 return u'%s' % (self.name)
67
67
68 def jsonify(self):
68 def jsonify(self):
69
69
70 data = {}
70 data = {}
71 ignored = ()
71 ignored = ()
72
72
73 for field in self._meta.fields:
73 for field in self._meta.fields:
74 if field.name in ignored:
74 if field.name in ignored:
75 continue
75 continue
76 data[field.name] = field.value_from_object(self)
76 data[field.name] = field.value_from_object(self)
77
77
78 return data
78 return data
79
79
80 def parms_to_dict(self):
80 def parms_to_dict(self):
81
81
82 parameters = {}
82 parameters = {}
83
83
84 parameters['name'] = self.name
84 parameters['name'] = self.name
85 parameters['clock'] = float(self.clock)
85 parameters['clock'] = float(self.clock)
86 parameters['multiplier'] = int(self.multiplier)
86 parameters['multiplier'] = int(self.multiplier)
87 parameters['frequency'] = float(self.frequency)
87 parameters['frequency'] = float(self.frequency)
88 parameters['f_decimal'] = int(self.frequency)
88 parameters['f_decimal'] = int(self.frequency)
89 parameters['fir'] = int(self.fir)
89 parameters['fir'] = int(self.fir)
90 parameters['cic_2'] = int(self.cic_2)
90 parameters['cic_2'] = int(self.cic_2)
91 parameters['cic_5'] = int(self.cic_5)
91 parameters['cic_5'] = int(self.cic_5)
92
92
93 return parameters
93 return parameters
94
94
95 def dict_to_parms(self, parameters):
95 def dict_to_parms(self, parameters):
96
96
97 self.name = parameters['name']
97 self.name = parameters['name']
98 self.clock = parameters['clock']
98 self.clock = parameters['clock']
99 self.multiplier = parameters['multiplier']
99 self.multiplier = parameters['multiplier']
100 self.frequency = parameters['frequency']
100 self.frequency = parameters['frequency']
101 self.f_decimal = parameters['f_decimal']
101 self.f_decimal = parameters['f_decimal']
102 self.fir = parameters['fir']
102 self.fir = parameters['fir']
103 self.cic_2 = parameters['cic_2']
103 self.cic_2 = parameters['cic_2']
104 self.cic_5 = parameters['cic_5']
104 self.cic_5 = parameters['cic_5']
105
105
106 def dict_to_parms_new(self, parameters):
106 def dict_to_parms_new(self, parameters):
107
107
108 self.name = parameters['name']
108 self.name = parameters['name']
109 self.clock = parameters['clock']
109 self.clock = parameters['clock']
110 self.multiplier = parameters['multiplier']
110 self.multiplier = parameters['multiplier']
111 self.frequency = parameters['frequency']
111 self.frequency = parameters['frequency']
112 self.f_decimal = parameters['f_decimal']
112 self.f_decimal = parameters['f_decimal']
113 self.fir = parameters['fir']
113 self.fir = parameters['fir']
114 self.cic_2 = parameters['cic_2']
114 self.cic_2 = parameters['cic_2']
115 self.cic_5 = parameters['cic_5']
115 self.cic_5 = parameters['cic_5']
116 self.scale_fir = parameters['scale_fir']
116 self.scale_fir = parameters['scale_fir']
117 self.scale_cic_2 = parameters['scale_cic_2']
117 self.scale_cic_2 = parameters['scale_cic_2']
118 self.scale_cic_5 = parameters['scale_cic_5']
118 self.scale_cic_5 = parameters['scale_cic_5']
119 self.number_taps = parameters['number_taps']
119 self.number_taps = parameters['number_taps']
120 self.taps = parameters['taps']
120 self.taps = parameters['taps']
121
121
122 class JARSConfiguration(Configuration):
122 class JARSConfiguration(Configuration):
123
123
124 ADC_RESOLUTION = 8
124 ADC_RESOLUTION = 8
125 PCI_DIO_BUSWIDTH = 32
125 PCI_DIO_BUSWIDTH = 32
126 HEADER_VERSION = 1103
126 HEADER_VERSION = 1103
127 BEGIN_ON_START = True
127 BEGIN_ON_START = True
128 REFRESH_RATE = 1
128 REFRESH_RATE = 1
129
129
130 exp_type = models.PositiveIntegerField(
130 exp_type = models.PositiveIntegerField(
131 verbose_name='Experiment Type', choices=EXPERIMENT_TYPE, default=0)
131 verbose_name='Experiment Type', choices=EXPERIMENT_TYPE, default=0)
132 cards_number = models.PositiveIntegerField(verbose_name='Number of Cards', validators=[
132 cards_number = models.PositiveIntegerField(verbose_name='Number of Cards', validators=[
133 MinValueValidator(1), MaxValueValidator(4)], default=1)
133 MinValueValidator(1), MaxValueValidator(4)], default=1)
134 channels_number = models.PositiveIntegerField(verbose_name='Number of Channels', validators=[
134 channels_number = models.PositiveIntegerField(verbose_name='Number of Channels', validators=[
135 MinValueValidator(1), MaxValueValidator(8)], default=5)
135 MinValueValidator(1), MaxValueValidator(8)], default=5)
136 channels = models.CharField(
136 channels = models.CharField(
137 verbose_name='Channels', max_length=15, default='1,2,3,4,5')
137 verbose_name='Channels', max_length=15, default='1,2,3,4,5')
138 data_type = models.PositiveIntegerField(
138 data_type = models.PositiveIntegerField(
139 verbose_name='Data Type', choices=DATA_TYPE, default=0)
139 verbose_name='Data Type', choices=DATA_TYPE, default=0)
140 raw_data_blocks = models.PositiveIntegerField(
140 raw_data_blocks = models.PositiveIntegerField(
141 verbose_name='Raw Data Blocks', validators=[MaxValueValidator(5000)], default=60)
141 verbose_name='Raw Data Blocks', validators=[MaxValueValidator(5000)], default=60)
142 profiles_block = models.PositiveIntegerField(
142 profiles_block = models.PositiveIntegerField(
143 verbose_name='Profiles Per Block', default=400)
143 verbose_name='Profiles Per Block', default=400)
144 acq_profiles = models.PositiveIntegerField(
144 acq_profiles = models.PositiveIntegerField(
145 verbose_name='Acquired Profiles', default=400)
145 verbose_name='Acquired Profiles', default=400)
146 ftp_interval = models.PositiveIntegerField(
146 ftp_interval = models.PositiveIntegerField(
147 verbose_name='FTP Interval', default=60)
147 verbose_name='FTP Interval', default=60)
148 fftpoints = models.PositiveIntegerField(
148 fftpoints = models.PositiveIntegerField(
149 verbose_name='FFT Points', default=16)
149 verbose_name='FFT Points', default=16)
150 cohe_integr_str = models.PositiveIntegerField(
150 cohe_integr_str = models.PositiveIntegerField(
151 verbose_name='Coh. Int. Stride', validators=[MinValueValidator(1)], default=30)
151 verbose_name='Coh. Int. Stride', validators=[MinValueValidator(1)], default=30)
152 cohe_integr = models.PositiveIntegerField(
152 cohe_integr = models.PositiveIntegerField(
153 verbose_name='Coherent Integrations', validators=[MinValueValidator(1)], default=30)
153 verbose_name='Coherent Integrations', validators=[MinValueValidator(1)], default=30)
154 incohe_integr = models.PositiveIntegerField(
154 incohe_integr = models.PositiveIntegerField(
155 verbose_name='Incoherent Integrations', validators=[MinValueValidator(1)], default=30)
155 verbose_name='Incoherent Integrations', validators=[MinValueValidator(1)], default=30)
156 decode_data = models.PositiveIntegerField(
156 decode_data = models.PositiveIntegerField(
157 verbose_name='Decode Data', choices=DECODE_TYPE, default=0)
157 verbose_name='Decode Data', choices=DECODE_TYPE, default=0)
158 post_coh_int = models.BooleanField(
158 post_coh_int = models.BooleanField(
159 verbose_name='Post Coherent Integration', default=False)
159 verbose_name='Post Coherent Integration', default=False)
160 spectral_number = models.PositiveIntegerField(
160 spectral_number = models.PositiveIntegerField(
161 verbose_name='# Spectral Combinations', validators=[MinValueValidator(1)], default=1)
161 verbose_name='# Spectral Combinations', validators=[MinValueValidator(1)], default=1)
162 spectral = models.CharField(
162 spectral = models.CharField(
163 verbose_name='Combinations', max_length=5000, default='[0, 0],')
163 verbose_name='Combinations', max_length=5000, default='[0, 0],')
164 create_directory = models.BooleanField(
164 create_directory = models.BooleanField(
165 verbose_name='Create Directory Per Day', default=True)
165 verbose_name='Create Directory Per Day', default=True)
166 include_expname = models.BooleanField(
166 include_expname = models.BooleanField(
167 verbose_name='Experiment Name in Directory', default=False)
167 verbose_name='Experiment Name in Directory', default=False)
168 #view_raw_data = models.BooleanField(verbose_name='View Raw Data', default=True)
168 #view_raw_data = models.BooleanField(verbose_name='View Raw Data', default=True)
169 save_ch_dc = models.BooleanField(
169 save_ch_dc = models.BooleanField(
170 verbose_name='Save Channels DC', default=True)
170 verbose_name='Save Channels DC', default=True)
171 save_data = models.BooleanField(verbose_name='Save Data', default=True)
171 save_data = models.BooleanField(verbose_name='Save Data', default=True)
172 filter_parms = models.CharField(
172 filter_parms = models.CharField(
173 max_length=10000, default=FILTER)
173 max_length=10000, default=FILTER)
174 filter = models.ForeignKey(
174 filter = models.ForeignKey(
175 'JARSFilter', verbose_name='Filter', null=True, blank=True, on_delete=models.CASCADE)
175 'JARSFilter', verbose_name='Filter', null=True, blank=True, on_delete=models.CASCADE)
176
176
177 class Meta:
177 class Meta:
178 db_table = 'jars_configurations'
178 db_table = 'jars_configurations'
179
179
180 def filter_resolution(self):
180 def filter_resolution(self):
181 filter_parms = json.loads(self.filter_parms)
181 filter_parms = json.loads(self.filter_parms)
182 clock = float(filter_parms['clock'])
182 clock = float(filter_parms['clock'])
183 cic_2 = filter_parms['cic_2']
183 cic_2 = filter_parms['cic_2']
184 cic_5 = filter_parms['cic_5']
184 cic_5 = filter_parms['cic_5']
185 fir = filter_parms['fir']
185 fir = filter_parms['fir']
186 resolution = round((clock/(cic_2*cic_5*fir)), 2)
186 resolution = round((clock/(cic_2*cic_5*fir)), 2)
187 return resolution
187 return resolution
188
188
189 def dict_to_parms(self, params, id=None):
189 def dict_to_parms(self, params, id=None):
190
190
191 if id is not None:
191 if id is not None:
192 data = Params(params).get_conf(id_conf=id)
192 data = Params(params).get_conf(id_conf=id)
193 else:
193 else:
194 data = Params(params).get_conf(dtype='jars')
194 data = Params(params).get_conf(dtype='jars')
195 data['filter_parms'] = params['filter_parms']
195 data['filter_parms'] = params['filter_parms']
196
196
197 # self.name = data['name']
197 # self.name = data['name']
198 self.exp_type = data['exp_type']
198 self.exp_type = data['exp_type']
199 #----PDATA----
199 #----PDATA----
200 if self.exp_type == 1:
200 if self.exp_type == 1:
201 self.incohe_integr = data['incohe_integr']
201 self.incohe_integr = data['incohe_integr']
202 self.spectral_number = data['spectral_number']
202 self.spectral_number = data['spectral_number']
203 self.spectral = data['spectral']
203 self.spectral = data['spectral']
204 self.fftpoints = data['fftpoints']
204 self.fftpoints = data['fftpoints']
205 self.save_ch_dc = data['save_ch_dc']
205 self.save_ch_dc = data['save_ch_dc']
206 else:
206 else:
207 self.raw_data_blocks = data['raw_data_blocks']
207 self.raw_data_blocks = data['raw_data_blocks']
208 #----PDATA----
208 #----PDATA----
209 self.cards_number = data['cards_number']
209 self.cards_number = data['cards_number']
210 self.channels_number = data['channels_number']
210 self.channels_number = data['channels_number']
211 self.channels = data['channels']
211 self.channels = data['channels']
212 self.data_type = data['data_type']
212 self.data_type = data['data_type']
213 self.profiles_block = data['profiles_block']
213 self.profiles_block = data['profiles_block']
214 self.acq_profiles = data['acq_profiles']
214 self.acq_profiles = data['acq_profiles']
215 self.ftp_interval = data['ftp_interval']
215 self.ftp_interval = data['ftp_interval']
216 self.cohe_integr_str = data['cohe_integr_str']
216 self.cohe_integr_str = data['cohe_integr_str']
217 self.cohe_integr = data['cohe_integr']
217 self.cohe_integr = data['cohe_integr']
218 #----DECODE----
218 #----DECODE----
219 self.decode_data = data['decode_data']
219 self.decode_data = data['decode_data']
220 self.post_coh_int = data['post_coh_int']
220 self.post_coh_int = data['post_coh_int']
221 #----DECODE----
221 #----DECODE----
222 self.create_directory = data['create_directory']
222 self.create_directory = data['create_directory']
223 self.include_expname = data['include_expname']
223 self.include_expname = data['include_expname']
224 self.save_data = data['save_data']
224 self.save_data = data['save_data']
225 self.filter_parms = json.dumps(data['filter_parms'])
225 self.filter_parms = json.dumps(data['filter_parms'])
226
226
227 self.save()
227 self.save()
228
228
229 def parms_to_text(self, file_format='jars'):
229 def parms_to_text(self, file_format='jars'):
230
230
231 data = self.experiment.parms_to_dict()
231 data = self.experiment.parms_to_dict()
232
232
233 for key in data['configurations']['allIds']:
233 for key in data['configurations']['allIds']:
234 if data['configurations']['byId'][key]['device_type'] in ('dds', 'cgs'):
234 if data['configurations']['byId'][key]['device_type'] in ('dds', 'cgs'):
235 data['configurations']['allIds'].remove(key)
235 data['configurations']['allIds'].remove(key)
236 data['configurations']['byId'].pop(key)
236 data['configurations']['byId'].pop(key)
237 elif data['configurations']['byId'][key]['device_type'] == 'jars':
237 elif data['configurations']['byId'][key]['device_type'] == 'jars':
238 data['configurations']['byId'][key] = self.parms_to_dict(
238 data['configurations']['byId'][key] = self.parms_to_dict(
239 )['configurations']['byId'][str(self.pk)]
239 )['configurations']['byId'][str(self.pk)]
240 elif data['configurations']['byId'][key]['device_type'] == 'rc':
240 elif data['configurations']['byId'][key]['device_type'] == 'rc':
241 data['configurations']['byId'][key]['pulses'] = ''
241 data['configurations']['byId'][key]['pulses'] = ''
242 data['configurations']['byId'][key]['delays'] = ''
242 data['configurations']['byId'][key]['delays'] = ''
243 rc_ids = [pk for pk in data['configurations']['allIds']
243 rc_ids = [pk for pk in data['configurations']['allIds']
244 if data['configurations']['byId'][pk]['device_type'] == 'rc']
244 if data['configurations']['byId'][pk]['device_type'] == 'rc']
245 mix_ids = [pk for pk in rc_ids if data['configurations']
245 mix_ids = [pk for pk in rc_ids if data['configurations']
246 ['byId'][pk]['mix']]
246 ['byId'][pk]['mix']]
247
247
248 if mix_ids:
248 if mix_ids:
249 params = data['configurations']['byId'][mix_ids[0]]['parameters']
249 params = data['configurations']['byId'][mix_ids[0]]['parameters']
250 rc = data['configurations']['byId'][params.split(
250 rc = data['configurations']['byId'][params.split(
251 '-')[0].split('|')[0]]
251 '-')[0].split('|')[0]]
252 rc['mix'] = True
252 rc['mix'] = True
253 data['configurations']['byId'][rc['id']] = rc
253 data['configurations']['byId'][rc['id']] = rc
254 elif len(rc_ids) == 0:
254 elif len(rc_ids) == 0:
255 self.message = 'File needs RC configuration'
255 self.message = 'File needs RC configuration'
256 return ''
256 return ''
257
257
258 json_data = json.dumps(data)
258 json_data = json.dumps(data)
259 racp_file, filter_file = create_jarsfiles(json_data)
259 racp_file, filter_file = create_jarsfiles(json_data)
260 if file_format == 'racp':
260 if file_format == 'racp':
261 return racp_file
261 return racp_file
262
262
263 return filter_file
263 return filter_file
264
264
265 def request(self, cmd, method='get', **kwargs):
265 def request(self, cmd, method='get', **kwargs):
266
266
267 req = getattr(requests, method)(self.device.url(cmd), **kwargs)
267 req = getattr(requests, method)(self.device.url(cmd), **kwargs)
268 payload = req.json()
268 payload = req.json()
269 return payload
269 return payload
270
270
271 def status_device(self):
271 def status_device(self):
272
272
273 try:
273 try:
274 payload = self.request('status',
274 payload = self.request('status',
275 params={'name': self.experiment.name})
275 params={'name': self.experiment.name})
276 self.device.status = payload['status']
276 self.device.status = payload['status']
277 self.device.save()
277 self.device.save()
278 self.message = payload['message']
278 self.message = payload['message']
279 except Exception as e:
279 except Exception as e:
280 self.device.status = 0
280 self.device.status = 0
281 self.message = str(e)
281 self.message = str(e)
282 self.device.save()
282 self.device.save()
283 return False
283 return False
284
284
285 return True
285 return True
286
286
287 def stop_device(self):
287 def stop_device(self):
288
288
289 # Se crea el modo ocupado para una vez inicia el STOP
289 # Se crea el modo ocupado para una vez inicia el STOP
290 self.device.status = 5
290 self.device.status = 5
291 self.device.save()
291 self.device.save()
292 # Por si se demora deteniendo, que su estado sea busy
292 # Por si se demora deteniendo, que su estado sea busy
293
293
294 try:
294 try:
295 payload = self.request('stop', 'post')
295 payload = self.request('stop', 'post')
296 self.device.status = payload['status']
296 self.device.status = payload['status']
297 self.device.save()
297 self.device.save()
298 self.message = payload['message']
298 self.message = payload['message']
299 except Exception as e:
299 except Exception as e:
300 self.device.status = 0
300 self.device.status = 0
301 self.message = str(e)
301 self.message = str(e)
302 self.device.save()
302 self.device.save()
303 return False
303 return False
304
304
305 return True
305 return True
306
306
307 def read_device(self):
307 def read_device(self):
308
308
309 try:
309 try:
310 payload = self.request(
310 payload = self.request(
311 'read', params={'name': self.experiment.name})
311 'read', params={'name': self.experiment.name})
312 self.message = 'Configuration loaded'
312 self.message = 'Configuration loaded'
313 except:
313 except:
314 self.device.status = 0
314 self.device.status = 0
315 self.device.save()
315 self.device.save()
316 self.message = 'Could not read JARS configuration.'
316 self.message = 'Could not read JARS configuration.'
317 return False
317 return False
318
318
319 return payload
319 return payload
320
320
321 def write_device(self):
321 def write_device(self):
322
322
323 if self.device.status == 3:
323 if self.device.status == 3:
324 self.message = 'Could not configure device. Software Acquisition is running'
324 self.message = 'Could not configure device. Software Acquisition is running'
325 print('Could not configure device. Software Acquisition is running')
325 print('Could not configure device. Software Acquisition is running')
326 return False
326 return False
327
327
328 data = self.experiment.parms_to_dict()
328 data = self.experiment.parms_to_dict()
329 #print(data)
329 #print(data)
330 for key in data['configurations']['allIds']:
330 for key in data['configurations']['allIds']:
331 if data['configurations']['byId'][key]['device_type'] in ('dds', 'cgs'):
331 if data['configurations']['byId'][key]['device_type'] in ('dds', 'cgs'):
332 data['configurations']['allIds'].remove(key)
332 data['configurations']['allIds'].remove(key)
333 data['configurations']['byId'].pop(key)
333 data['configurations']['byId'].pop(key)
334 elif data['configurations']['byId'][key]['device_type'] == 'rc':
334 elif data['configurations']['byId'][key]['device_type'] == 'rc':
335 data['configurations']['byId'][key]['pulses'] = ''
335 data['configurations']['byId'][key]['pulses'] = ''
336 data['configurations']['byId'][key]['delays'] = ''
336 data['configurations']['byId'][key]['delays'] = ''
337 rc_ids = [pk for pk in data['configurations']['allIds']
337 rc_ids = [pk for pk in data['configurations']['allIds']
338 if data['configurations']['byId'][pk]['device_type'] == 'rc']
338 if data['configurations']['byId'][pk]['device_type'] == 'rc']
339 if len(rc_ids) == 0:
339 if len(rc_ids) == 0:
340 self.message = 'Missing RC configuration'
340 self.message = 'Missing RC configuration'
341 return False
341 return False
342
342
343 json_data = json.dumps(data)
343 json_data = json.dumps(data)
344 print("STATUS:", self.device.status,flush=True)
344 # print("STATUS:", self.device.status,flush=True)
345
345
346 try:
346 try:
347 payload = self.request('write', 'post', json=json_data)
347 payload = self.request('write', 'post', json=json_data)
348 self.device.status = payload['status']
348 self.device.status = payload['status']
349 self.message = payload['message']
349 self.message = payload['message']
350 self.device.save()
350 self.device.save()
351 if self.device.status == 1:
351 if self.device.status == 1:
352 return False
352 return False
353
353
354 except Exception as e:
354 except Exception as e:
355 self.device.status = 0
355 self.device.status = 0
356 self.message = str(e)
356 self.message = str(e)
357 self.device.save()
357 self.device.save()
358 return False
358 return False
359 return True
359 return True
360
360
361 def start_device(self):
361 def start_device(self):
362
362
363 try:
363 try:
364 payload = self.request('start', 'post',
364 payload = self.request('start', 'post',
365 json={'name': self.experiment.name})
365 json={'name': self.experiment.name})
366 self.device.status = payload['status']
366 self.device.status = payload['status']
367 self.message = payload['message']
367 self.message = payload['message']
368 self.device.save()
368 self.device.save()
369 if self.device.status == 1:
369 if self.device.status == 1:
370 return False
370 return False
371
371
372 except Exception as e:
372 except Exception as e:
373 self.device.status = 0
373 self.device.status = 0
374 self.message = str(e)
374 self.message = str(e)
375 self.device.save()
375 self.device.save()
376 return False
376 return False
377 return True
377 return True
378
378
379 def get_log(self):
379 def get_log(self):
380
380
381 payload = None
381 payload = None
382
382
383 try:
383 try:
384 payload = requests.get(self.device.url('get_log'), params={
384 payload = requests.get(self.device.url('get_log'), params={
385 'name': self.experiment.name})
385 'name': self.experiment.name})
386 except:
386 except:
387 self.device.status = 0
387 self.device.status = 0
388 self.device.save()
388 self.device.save()
389 self.message = 'Jars API is not running.'
389 self.message = 'Jars API is not running.'
390 return False
390 return False
391
391
392 self.message = 'Jars API is running'
392 self.message = 'Jars API is running'
393
393
394 return payload
394 return payload
395
395
396 def update_from_file(self, filename):
396 def update_from_file(self, filename):
397
397
398 f = JARSFile(filename)
398 f = JARSFile(filename)
399 self.dict_to_parms(f.data)
399 self.dict_to_parms(f.data)
400 self.save()
400 self.save()
401
401
402 def get_absolute_url_import(self):
402 def get_absolute_url_import(self):
403 return reverse('url_import_jars_conf', args=[str(self.id)])
403 return reverse('url_import_jars_conf', args=[str(self.id)])
404
404
405 def get_absolute_url_read(self):
405 def get_absolute_url_read(self):
406 return reverse('url_read_jars_conf', args=[str(self.id)])
406 return reverse('url_read_jars_conf', args=[str(self.id)])
407
407
408 def get_absolute_url_log(self):
408 def get_absolute_url_log(self):
409 return reverse('url_get_jars_log', args=[str(self.id)])
409 return reverse('url_get_jars_log', args=[str(self.id)])
1 NO CONTENT: file renamed from apps/main/management/commands/hello.py to apps/main/management/commands/scheduler.py
NO CONTENT: file renamed from apps/main/management/commands/hello.py to apps/main/management/commands/scheduler.py
@@ -1,856 +1,853
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.urls import reverse
15 from django.urls 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 from django.contrib.auth.models import User
18 from django.contrib.auth.models import User
19 from django.db.models.signals import post_save
19 from django.db.models.signals import post_save
20 from django.dispatch import receiver
20 from django.dispatch import receiver
21
21
22 from apps.main.utils import Params
22 from apps.main.utils import Params
23 from apps.rc.utils import RCFile
23 from apps.rc.utils import RCFile
24 from apps.jars.utils import RacpFile
24 from apps.jars.utils import RacpFile
25 from devices.dds import api as dds_api
25 from devices.dds import api as dds_api
26 from devices.dds import data as dds_data
26 from devices.dds import data as dds_data
27
27
28
28
29 DEV_PORTS = {
29 DEV_PORTS = {
30 'rc' : 2000,
30 'rc' : 2000,
31 'dds' : 2000,
31 'dds' : 2000,
32 'jars' : 2000,
32 'jars' : 2000,
33 'usrp' : 2000,
33 'usrp' : 2000,
34 'cgs' : 8080,
34 'cgs' : 8080,
35 'abs' : 8080,
35 'abs' : 8080,
36 'dds_rest': 80
36 'dds_rest': 80
37 }
37 }
38
38
39 RADAR_STATES = (
39 RADAR_STATES = (
40 (0, 'No connected'),
40 (0, 'No connected'),
41 (1, 'Connected'),
41 (1, 'Connected'),
42 (2, 'Configured'),
42 (2, 'Configured'),
43 (3, 'Running'),
43 (3, 'Running'),
44 (4, 'Scheduled'),
44 (4, 'Scheduled'),
45 )
45 )
46
46
47 EXPERIMENT_TYPE = (
47 EXPERIMENT_TYPE = (
48 (0, 'RAW_DATA'),
48 (0, 'RAW_DATA'),
49 (1, 'PDATA'),
49 (1, 'PDATA'),
50 )
50 )
51
51
52 DECODE_TYPE = (
52 DECODE_TYPE = (
53 (0, 'None'),
53 (0, 'None'),
54 (1, 'TimeDomain'),
54 (1, 'TimeDomain'),
55 (2, 'FreqDomain'),
55 (2, 'FreqDomain'),
56 (3, 'InvFreqDomain'),
56 (3, 'InvFreqDomain'),
57 )
57 )
58
58
59 DEV_STATES = (
59 DEV_STATES = (
60 (0, 'No connected'),
60 (0, 'No connected'),
61 (1, 'Connected'),
61 (1, 'Connected'),
62 (2, 'Configured'),
62 (2, 'Configured'),
63 (3, 'Running'),
63 (3, 'Running'),
64 (4, 'Unknown'),
64 (4, 'Unknown'),
65 (5, 'Busy')
65 (5, 'Busy')
66 )
66 )
67
67
68 DEV_TYPES = (
68 DEV_TYPES = (
69 ('', 'Select a device type'),
69 ('', 'Select a device type'),
70 ('rc', 'Radar Controller'),
70 ('rc', 'Radar Controller'),
71 ('dds', 'Direct Digital Synthesizer'),
71 ('dds', 'Direct Digital Synthesizer'),
72 ('jars', 'Jicamarca Radar Acquisition System'),
72 ('jars', 'Jicamarca Radar Acquisition System'),
73 ('usrp', 'Universal Software Radio Peripheral'),
73 ('usrp', 'Universal Software Radio Peripheral'),
74 ('cgs', 'Clock Generator System'),
74 ('cgs', 'Clock Generator System'),
75 ('abs', 'Automatic Beam Switching'),
75 ('abs', 'Automatic Beam Switching'),
76 ('dds_rest', 'Direct Digital Synthesizer_REST'),
76 ('dds_rest', 'Direct Digital Synthesizer_REST'),
77 ('atrad', 'Transmitter ATRAD'),
77 ('atrad', 'Transmitter ATRAD'),
78 )
78 )
79
79
80 EXP_STATES = (
80 EXP_STATES = (
81 (0,'Error'), #RED
81 (0,'Error'), #RED
82 (1,'Cancelled'), #YELLOW
82 (1,'Cancelled'), #YELLOW
83 (2,'Running'), #GREEN
83 (2,'Running'), #GREEN
84 (3,'Scheduled'), #BLUE
84 (3,'Scheduled'), #BLUE
85 (4,'Unknown'), #WHITE
85 (4,'Unknown'), #WHITE
86 (5,'Busy'),
86 (5,'Busy'),
87 )
87 )
88
88
89 CONF_TYPES = (
89 CONF_TYPES = (
90 (0, 'Active'),
90 (0, 'Active'),
91 (1, 'Historical'),
91 (1, 'Historical'),
92 )
92 )
93
93
94 class Profile(models.Model):
94 class Profile(models.Model):
95 user = models.OneToOneField(User, on_delete=models.CASCADE)
95 user = models.OneToOneField(User, on_delete=models.CASCADE)
96 theme = models.CharField(max_length=30, default='spacelab')
96 theme = models.CharField(max_length=30, default='spacelab')
97
97
98
98
99 @receiver(post_save, sender=User)
99 @receiver(post_save, sender=User)
100 def create_user_profile(sender, instance, created, **kwargs):
100 def create_user_profile(sender, instance, created, **kwargs):
101 if created:
101 if created:
102 Profile.objects.create(user=instance)
102 Profile.objects.create(user=instance)
103
103
104 @receiver(post_save, sender=User)
104 @receiver(post_save, sender=User)
105 def save_user_profile(sender, instance, **kwargs):
105 def save_user_profile(sender, instance, **kwargs):
106 instance.profile.save()
106 instance.profile.save()
107
107
108
108
109 class Location(models.Model):
109 class Location(models.Model):
110
110
111 name = models.CharField(max_length = 30)
111 name = models.CharField(max_length = 30)
112 description = models.TextField(blank=True, null=True)
112 description = models.TextField(blank=True, null=True)
113
113
114 class Meta:
114 class Meta:
115 db_table = 'db_location'
115 db_table = 'db_location'
116
116
117 def __str__(self):
117 def __str__(self):
118 return u'%s' % self.name
118 return u'%s' % self.name
119
119
120 def get_absolute_url(self):
120 def get_absolute_url(self):
121 return reverse('url_location', args=[str(self.id)])
121 return reverse('url_location', args=[str(self.id)])
122
122
123
123
124 class DeviceType(models.Model):
124 class DeviceType(models.Model):
125
125
126 name = models.CharField(max_length = 10, choices = DEV_TYPES, default = 'dds_rest')
126 name = models.CharField(max_length = 10, choices = DEV_TYPES, default = 'dds_rest')
127 sequence = models.PositiveSmallIntegerField(default=55)
127 sequence = models.PositiveSmallIntegerField(default=55)
128 description = models.TextField(blank=True, null=True)
128 description = models.TextField(blank=True, null=True)
129
129
130 class Meta:
130 class Meta:
131 db_table = 'db_device_types'
131 db_table = 'db_device_types'
132
132
133 def __str__(self):
133 def __str__(self):
134 return u'%s' % self.get_name_display()
134 return u'%s' % self.get_name_display()
135
135
136 class Device(models.Model):
136 class Device(models.Model):
137
137
138 device_type = models.ForeignKey('DeviceType', on_delete=models.CASCADE)
138 device_type = models.ForeignKey('DeviceType', on_delete=models.CASCADE)
139 location = models.ForeignKey('Location', on_delete=models.CASCADE)
139 location = models.ForeignKey('Location', on_delete=models.CASCADE)
140 ip_address = models.GenericIPAddressField(protocol='IPv4', default='0.0.0.0')
140 ip_address = models.GenericIPAddressField(protocol='IPv4', default='0.0.0.0')
141 port_address = models.PositiveSmallIntegerField(default=2000)
141 port_address = models.PositiveSmallIntegerField(default=2000)
142 description = models.TextField(blank=True, null=True)
142 description = models.TextField(blank=True, null=True)
143 status = models.PositiveSmallIntegerField(default=4, choices=DEV_STATES)
143 status = models.PositiveSmallIntegerField(default=4, choices=DEV_STATES)
144 conf_active = models.PositiveIntegerField(default=0, verbose_name='Current configuration')
144 conf_active = models.PositiveIntegerField(default=0, verbose_name='Current configuration')
145
145
146 class Meta:
146 class Meta:
147 db_table = 'db_devices'
147 db_table = 'db_devices'
148
148
149 def __str__(self):
149 def __str__(self):
150 ret = u'{} [{}]'.format(self.device_type.name.upper(), self.location.name)
150 ret = u'{} [{}]'.format(self.device_type.name.upper(), self.location.name)
151
151
152 return ret
152 return ret
153
153
154 @property
154 @property
155 def name(self):
155 def name(self):
156 return str(self)
156 return str(self)
157
157
158 def get_status(self):
158 def get_status(self):
159 return self.status
159 return self.status
160
160
161 @property
161 @property
162 def status_color(self):
162 def status_color(self):
163 color = 'muted'
163 color = 'muted'
164 if self.status == 0:
164 if self.status == 0:
165 color = "danger"
165 color = "danger"
166 elif self.status == 1:
166 elif self.status == 1:
167 color = "warning"
167 color = "warning"
168 elif self.status == 2:
168 elif self.status == 2:
169 color = "info"
169 color = "info"
170 elif self.status == 3:
170 elif self.status == 3:
171 color = "success"
171 color = "success"
172
172
173 return color
173 return color
174
174
175 def url(self, path=None):
175 def url(self, path=None):
176
176
177 if path:
177 if path:
178 return 'http://{}:{}/{}/'.format(self.ip_address, self.port_address, path)
178 return 'http://{}:{}/{}/'.format(self.ip_address, self.port_address, path)
179 else:
179 else:
180 return 'http://{}:{}/'.format(self.ip_address, self.port_address)
180 return 'http://{}:{}/'.format(self.ip_address, self.port_address)
181
181
182 def get_absolute_url(self):
182 def get_absolute_url(self):
183 return reverse('url_device', args=[str(self.id)])
183 return reverse('url_device', args=[str(self.id)])
184
184
185 def get_absolute_url_edit(self):
185 def get_absolute_url_edit(self):
186 return reverse('url_edit_device', args=[str(self.id)])
186 return reverse('url_edit_device', args=[str(self.id)])
187
187
188 def get_absolute_url_delete(self):
188 def get_absolute_url_delete(self):
189 return reverse('url_delete_device', args=[str(self.id)])
189 return reverse('url_delete_device', args=[str(self.id)])
190
190
191 def change_ip(self, ip_address, mask, gateway, dns, **kwargs):
191 def change_ip(self, ip_address, mask, gateway, dns, **kwargs):
192
192
193 if self.device_type.name=='dds':
193 if self.device_type.name=='dds':
194 try:
194 try:
195 answer = dds_api.change_ip(ip = self.ip_address,
195 answer = dds_api.change_ip(ip = self.ip_address,
196 port = self.port_address,
196 port = self.port_address,
197 new_ip = ip_address,
197 new_ip = ip_address,
198 mask = mask,
198 mask = mask,
199 gateway = gateway)
199 gateway = gateway)
200 if answer[0]=='1':
200 if answer[0]=='1':
201 self.message = '25|DDS - {}'.format(answer)
201 self.message = '25|DDS - {}'.format(answer)
202 self.ip_address = ip_address
202 self.ip_address = ip_address
203 self.save()
203 self.save()
204 else:
204 else:
205 self.message = '30|DDS - {}'.format(answer)
205 self.message = '30|DDS - {}'.format(answer)
206 return False
206 return False
207 except Exception as e:
207 except Exception as e:
208 self.message = '40|{}'.format(str(e))
208 self.message = '40|{}'.format(str(e))
209 return False
209 return False
210
210
211 elif self.device_type.name=='rc':
211 elif self.device_type.name=='rc':
212 headers = {'content-type': "application/json",
212 headers = {'content-type': "application/json",
213 'cache-control': "no-cache"}
213 'cache-control': "no-cache"}
214
214
215 ip = [int(x) for x in ip_address.split('.')]
215 ip = [int(x) for x in ip_address.split('.')]
216 dns = [int(x) for x in dns.split('.')]
216 dns = [int(x) for x in dns.split('.')]
217 gateway = [int(x) for x in gateway.split('.')]
217 gateway = [int(x) for x in gateway.split('.')]
218 subnet = [int(x) for x in mask.split('.')]
218 subnet = [int(x) for x in mask.split('.')]
219
219
220 payload = {
220 payload = {
221 "ip": ip,
221 "ip": ip,
222 "dns": dns,
222 "dns": dns,
223 "gateway": gateway,
223 "gateway": gateway,
224 "subnet": subnet
224 "subnet": subnet
225 }
225 }
226
226
227 req = requests.post(self.url('changeip'), data=json.dumps(payload), headers=headers)
227 req = requests.post(self.url('changeip'), data=json.dumps(payload), headers=headers)
228 try:
228 try:
229 answer = req.json()
229 answer = req.json()
230 if answer['changeip']=='ok':
230 if answer['changeip']=='ok':
231 self.message = '25|IP succesfully changed'
231 self.message = '25|IP succesfully changed'
232 self.ip_address = ip_address
232 self.ip_address = ip_address
233 self.save()
233 self.save()
234 else:
234 else:
235 self.message = '30|An error ocuur when changing IP'
235 self.message = '30|An error ocuur when changing IP'
236 except Exception as e:
236 except Exception as e:
237 self.message = '40|{}'.format(str(e))
237 self.message = '40|{}'.format(str(e))
238 else:
238 else:
239 self.message = 'Not implemented'
239 self.message = 'Not implemented'
240 return False
240 return False
241
241
242 return True
242 return True
243
243
244
244
245 class Campaign(models.Model):
245 class Campaign(models.Model):
246
246
247 template = models.BooleanField(default=False)
247 template = models.BooleanField(default=False)
248 name = models.CharField(max_length=60, unique=True)
248 name = models.CharField(max_length=60, unique=True)
249 start_date = models.DateTimeField(blank=True, null=True)
249 start_date = models.DateTimeField(blank=True, null=True)
250 end_date = models.DateTimeField(blank=True, null=True)
250 end_date = models.DateTimeField(blank=True, null=True)
251 tags = models.CharField(max_length=40, blank=True, null=True)
251 tags = models.CharField(max_length=40, blank=True, null=True)
252 description = models.TextField(blank=True, null=True)
252 description = models.TextField(blank=True, null=True)
253 experiments = models.ManyToManyField('Experiment', blank=True)
253 experiments = models.ManyToManyField('Experiment', blank=True)
254 author = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE)
254 author = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE)
255
255
256 class Meta:
256 class Meta:
257 db_table = 'db_campaigns'
257 db_table = 'db_campaigns'
258 ordering = ('name',)
258 ordering = ('name',)
259
259
260 def __str__(self):
260 def __str__(self):
261 if self.template:
261 if self.template:
262 return u'{} (template)'.format(self.name)
262 return u'{} (template)'.format(self.name)
263 else:
263 else:
264 return u'{}'.format(self.name)
264 return u'{}'.format(self.name)
265
265
266 def jsonify(self):
266 def jsonify(self):
267
267
268 data = {}
268 data = {}
269
269
270 ignored = ('template')
270 ignored = ('template')
271
271
272 for field in self._meta.fields:
272 for field in self._meta.fields:
273 if field.name in ignored:
273 if field.name in ignored:
274 continue
274 continue
275 data[field.name] = field.value_from_object(self)
275 data[field.name] = field.value_from_object(self)
276
276
277 data['start_date'] = data['start_date'].strftime('%Y-%m-%d')
277 data['start_date'] = data['start_date'].strftime('%Y-%m-%d')
278 data['end_date'] = data['end_date'].strftime('%Y-%m-%d')
278 data['end_date'] = data['end_date'].strftime('%Y-%m-%d')
279
279
280 return data
280 return data
281
281
282 def parms_to_dict(self):
282 def parms_to_dict(self):
283
283
284 params = Params({})
284 params = Params({})
285 params.add(self.jsonify(), 'campaigns')
285 params.add(self.jsonify(), 'campaigns')
286
286
287 for exp in Experiment.objects.filter(campaign = self):
287 for exp in Experiment.objects.filter(campaign = self):
288 params.add(exp.jsonify(), 'experiments')
288 params.add(exp.jsonify(), 'experiments')
289 configurations = Configuration.objects.filter(experiment=exp, type=0)
289 configurations = Configuration.objects.filter(experiment=exp, type=0)
290
290
291 for conf in configurations:
291 for conf in configurations:
292 params.add(conf.jsonify(), 'configurations')
292 params.add(conf.jsonify(), 'configurations')
293 if conf.device.device_type.name=='rc':
293 if conf.device.device_type.name=='rc':
294 for line in conf.get_lines():
294 for line in conf.get_lines():
295 params.add(line.jsonify(), 'lines')
295 params.add(line.jsonify(), 'lines')
296
296
297 return params.data
297 return params.data
298
298
299 def dict_to_parms(self, parms, CONF_MODELS):
299 def dict_to_parms(self, parms, CONF_MODELS):
300
300
301 experiments = Experiment.objects.filter(campaign = self)
301 experiments = Experiment.objects.filter(campaign = self)
302
302
303 if experiments:
303 if experiments:
304 for experiment in experiments:
304 for experiment in experiments:
305 experiment.delete()
305 experiment.delete()
306
306
307 for id_exp in parms['experiments']['allIds']:
307 for id_exp in parms['experiments']['allIds']:
308 exp_parms = parms['experiments']['byId'][id_exp]
308 exp_parms = parms['experiments']['byId'][id_exp]
309 dum = (datetime.now() - datetime(1970, 1, 1)).total_seconds()
309 dum = (datetime.now() - datetime(1970, 1, 1)).total_seconds()
310 exp = Experiment(name='{}'.format(dum))
310 exp = Experiment(name='{}'.format(dum))
311 exp.save()
311 exp.save()
312 exp.dict_to_parms(parms, CONF_MODELS, id_exp=id_exp)
312 exp.dict_to_parms(parms, CONF_MODELS, id_exp=id_exp)
313 self.experiments.add(exp)
313 self.experiments.add(exp)
314
314
315 camp_parms = parms['campaigns']['byId'][parms['campaigns']['allIds'][0]]
315 camp_parms = parms['campaigns']['byId'][parms['campaigns']['allIds'][0]]
316
316
317 self.name = '{}-{}'.format(camp_parms['name'], datetime.now().strftime('%y%m%d'))
317 self.name = '{}-{}'.format(camp_parms['name'], datetime.now().strftime('%y%m%d'))
318 self.start_date = camp_parms['start_date']
318 self.start_date = camp_parms['start_date']
319 self.end_date = camp_parms['end_date']
319 self.end_date = camp_parms['end_date']
320 self.tags = camp_parms['tags']
320 self.tags = camp_parms['tags']
321 self.save()
321 self.save()
322
322
323 return self
323 return self
324
324
325 def get_experiments_by_radar(self, radar=None):
325 def get_experiments_by_radar(self, radar=None):
326
326
327 ret = []
327 ret = []
328 if radar:
328 if radar:
329 locations = Location.objects.filter(pk=radar)
329 locations = Location.objects.filter(pk=radar)
330 else:
330 else:
331 locations = set([e.location for e in self.experiments.all()])
331 locations = set([e.location for e in self.experiments.all()])
332
332
333 for loc in locations:
333 for loc in locations:
334 dum = {}
334 dum = {}
335 dum['name'] = loc.name
335 dum['name'] = loc.name
336 dum['id'] = loc.pk
336 dum['id'] = loc.pk
337 dum['experiments'] = [e for e in self.experiments.all() if e.location==loc]
337 dum['experiments'] = [e for e in self.experiments.all() if e.location==loc]
338 ret.append(dum)
338 ret.append(dum)
339 return ret
339 return ret
340
340
341 def get_absolute_url(self):
341 def get_absolute_url(self):
342 return reverse('url_campaign', args=[str(self.id)])
342 return reverse('url_campaign', args=[str(self.id)])
343
343
344 def get_absolute_url_edit(self):
344 def get_absolute_url_edit(self):
345 return reverse('url_edit_campaign', args=[str(self.id)])
345 return reverse('url_edit_campaign', args=[str(self.id)])
346
346
347 def get_absolute_url_delete(self):
347 def get_absolute_url_delete(self):
348 return reverse('url_delete_campaign', args=[str(self.id)])
348 return reverse('url_delete_campaign', args=[str(self.id)])
349
349
350 def get_absolute_url_export(self):
350 def get_absolute_url_export(self):
351 return reverse('url_export_campaign', args=[str(self.id)])
351 return reverse('url_export_campaign', args=[str(self.id)])
352
352
353 def get_absolute_url_import(self):
353 def get_absolute_url_import(self):
354 return reverse('url_import_campaign', args=[str(self.id)])
354 return reverse('url_import_campaign', args=[str(self.id)])
355
355
356
356
357 class RunningExperiment(models.Model):
357 class RunningExperiment(models.Model):
358 radar = models.OneToOneField('Location', on_delete=models.CASCADE)
358 radar = models.OneToOneField('Location', on_delete=models.CASCADE)
359 running_experiment = models.ManyToManyField('Experiment', blank = True)
359 running_experiment = models.ManyToManyField('Experiment', blank = True)
360 status = models.PositiveSmallIntegerField(default=0, choices=RADAR_STATES)
360 status = models.PositiveSmallIntegerField(default=0, choices=RADAR_STATES)
361
361
362
362
363 class Experiment(models.Model):
363 class Experiment(models.Model):
364
364
365 template = models.BooleanField(default=False)
365 template = models.BooleanField(default=False)
366 name = models.CharField(max_length=40, default='', unique=True)
366 name = models.CharField(max_length=40, default='', unique=True)
367 location = models.ForeignKey('Location', null=True, blank=True, on_delete=models.CASCADE)
367 location = models.ForeignKey('Location', null=True, blank=True, on_delete=models.CASCADE)
368 freq = models.FloatField(verbose_name='Operating Freq. (MHz)', validators=[MinValueValidator(1), MaxValueValidator(10000)], default=49.9200)
368 freq = models.FloatField(verbose_name='Operating Freq. (MHz)', validators=[MinValueValidator(1), MaxValueValidator(10000)], default=49.9200)
369 start_time = models.TimeField(default='00:00:00')
369 start_time = models.TimeField(default='00:00:00')
370 end_time = models.TimeField(default='23:59:59')
370 end_time = models.TimeField(default='23:59:59')
371 task = models.CharField(max_length=36, default='', blank=True, null=True)
371 task = models.CharField(max_length=36, default='', blank=True, null=True)
372 status = models.PositiveSmallIntegerField(default=4, choices=EXP_STATES)
372 status = models.PositiveSmallIntegerField(default=4, choices=EXP_STATES)
373 author = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE)
373 author = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE)
374 hash = models.CharField(default='', max_length=64, null=True, blank=True)
374 hash = models.CharField(default='', max_length=64, null=True, blank=True)
375
375
376 class Meta:
376 class Meta:
377 db_table = 'db_experiments'
377 db_table = 'db_experiments'
378 ordering = ('template', 'name')
378 ordering = ('template', 'name')
379
379
380 def __str__(self):
380 def __str__(self):
381 if self.template:
381 if self.template:
382 return u'%s (template)' % (self.name)
382 return u'%s (template)' % (self.name)
383 else:
383 else:
384 return u'%s' % (self.name)
384 return u'%s' % (self.name)
385
385
386 def jsonify(self):
386 def jsonify(self):
387
387
388 data = {}
388 data = {}
389
389
390 ignored = ('template')
390 ignored = ('template')
391
391
392 for field in self._meta.fields:
392 for field in self._meta.fields:
393 if field.name in ignored:
393 if field.name in ignored:
394 continue
394 continue
395 data[field.name] = field.value_from_object(self)
395 data[field.name] = field.value_from_object(self)
396
396
397 data['start_time'] = data['start_time'].strftime('%H:%M:%S')
397 data['start_time'] = data['start_time'].strftime('%H:%M:%S')
398 data['end_time'] = data['end_time'].strftime('%H:%M:%S')
398 data['end_time'] = data['end_time'].strftime('%H:%M:%S')
399 data['location'] = self.location.name
399 data['location'] = self.location.name
400 data['configurations'] = ['{}'.format(conf.pk) for
400 data['configurations'] = ['{}'.format(conf.pk) for
401 conf in Configuration.objects.filter(experiment=self, type=0)]
401 conf in Configuration.objects.filter(experiment=self, type=0)]
402
402
403 return data
403 return data
404
404
405 @property
405 @property
406 def radar_system(self):
406 def radar_system(self):
407 return self.location
407 return self.location
408
408
409 def clone(self, **kwargs):
409 def clone(self, **kwargs):
410
410
411 confs = Configuration.objects.filter(experiment=self, type=0)
411 confs = Configuration.objects.filter(experiment=self, type=0)
412 self.pk = None
412 self.pk = None
413 self.name = '{}_{:%y%m%d%H%M%S}'.format(self.name, datetime.now())
413 self.name = '{}_{:%y%m%d%H%M%S}'.format(self.name, datetime.now())
414 for attr, value in kwargs.items():
414 for attr, value in kwargs.items():
415 setattr(self, attr, value)
415 setattr(self, attr, value)
416
416
417 self.save()
417 self.save()
418
418
419 for conf in confs:
419 for conf in confs:
420 conf.clone(experiment=self, template=False)
420 conf.clone(experiment=self, template=False)
421
421
422 return self
422 return self
423
423
424 def start(self):
424 def start(self):
425 '''
425 '''
426 Configure and start experiments's devices
426 Configure and start experiments's devices
427 ABS-CGS-DDS-RC-JARS
427 ABS-CGS-DDS-RC-JARS
428 '''
428 '''
429 self.status=5 #Busy
429 self.status=5 #Busy
430 self.save()
430 self.save()
431 print("Realizando operación. \nEXPERIMENT STATUS : {}".format(self.status))
431 print("Realizando operación. \nEXPERIMENT STATUS : {}".format(self.status))
432
432
433 confs = []
433 confs = []
434 allconfs = Configuration.objects.filter(experiment=self, type = 0).order_by('-device__device_type__sequence')
434 allconfs = Configuration.objects.filter(experiment=self, type = 0).order_by('-device__device_type__sequence')
435 rc_mix = [conf for conf in allconfs if conf.device.device_type.name=='rc' and conf.mix]
435 rc_mix = [conf for conf in allconfs if conf.device.device_type.name=='rc' and conf.mix]
436 if rc_mix:
436 if rc_mix:
437 for conf in allconfs:
437 for conf in allconfs:
438 if conf.device.device_type.name == 'rc' and not conf.mix:
438 if conf.device.device_type.name == 'rc' and not conf.mix:
439 continue
439 continue
440 confs.append(conf)
440 confs.append(conf)
441 else:
441 else:
442 confs = allconfs
442 confs = allconfs
443
443
444
444
445 for conf in confs:
445 for conf in confs:
446 print("--------------",flush=True)
446 print("--------------",flush=True)
447 print("STATUS: {}".format(conf.device.status))
447 print("STATUS: {}".format(conf.device.status))
448 print("Stop ",conf.name,flush=True)
448 print("Stop ",conf.name,flush=True)
449 if conf.stop_device() ==False:
449 if conf.stop_device() ==False:
450 print("Falló Stop ",conf.name)
450 print("Falló Stop ",conf.name)
451 print("Cancelando Campaña...",flush=True)
451 print("Cancelando Campaña...",flush=True)
452 return 0
452 return 0
453 print("Write ",conf.name,flush=True)
453 print("Write ",conf.name,flush=True)
454 if conf.write_device() ==False:
454 if conf.write_device() ==False:
455 print("Falló Write ",conf.name)
455 print("Falló Write ",conf.name)
456 print("Cancelando Campaña...",flush=True)
456 print("Cancelando Campaña...",flush=True)
457 return 0
457 return 0
458 print("Save",conf.name,flush=True)
458 print("Save",conf.name,flush=True)
459 conf.device.conf_active = conf.pk
459 conf.device.conf_active = conf.pk
460 conf.device.save()
460 conf.device.save()
461 print("Start",conf.name,flush=True)
461 print("Start",conf.name,flush=True)
462 if conf.start_device()==False:
462 if conf.start_device()==False:
463 print("Falló Start ",conf.name)
463 print("Falló Start ",conf.name)
464 print("Cancelando Campaña...",flush=True)
464 print("Cancelando Campaña...",flush=True)
465 return 0
465 return 0
466 print("--- CONFIGURACIÓN EXITOSA ---",flush=True)
466 print("--- CONFIGURACIÓN EXITOSA ---",flush=True)
467 time.sleep(1)
467 time.sleep(1)
468
468
469 return 2
469 return 2
470
470
471
471
472 def stop(self):
472 def stop(self):
473 '''
473 '''
474 Stop experiments's devices
474 Stop experiments's devices
475 DDS-JARS-RC-CGS-ABS
475 DDS-JARS-RC-CGS-ABS
476 '''
476 '''
477
477
478 confs = Configuration.objects.filter(experiment=self, type = 0).order_by('device__device_type__sequence')
478 confs = Configuration.objects.filter(experiment=self, type = 0).order_by('device__device_type__sequence')
479 confs = confs.exclude(device__device_type__name='cgs')
479 confs = confs.exclude(device__device_type__name='cgs')
480 try:
480 try:
481 for conf in confs:
481 for conf in confs:
482 conf.stop_device()
482 conf.stop_device()
483 except:
483 except:
484 return 0
484 return 0
485 return 1
485 return 1
486
486
487 def get_status(self):
487 def get_status(self):
488
488
489 if self.status == 3:
489 if self.status == 3:
490 return
490 return
491
491
492 confs = Configuration.objects.filter(experiment=self, type=0)
492 confs = Configuration.objects.filter(experiment=self, type=0)
493
493
494 for conf in confs:
494 for conf in confs:
495 conf.status_device()
495 conf.status_device()
496
496
497 total = confs.aggregate(models.Sum('device__status'))['device__status__sum']
497 total = confs.aggregate(models.Sum('device__status'))['device__status__sum']
498
498
499 if total==2*confs.count():
499 if total==2*confs.count():
500 status = 1
500 status = 1
501 elif total == 3*confs.count():
501 elif total == 3*confs.count():
502 status = 2
502 status = 2
503 else:
503 else:
504 status = 0
504 status = 0
505
505
506 self.status = status
506 self.status = status
507 self.save()
507 self.save()
508
508
509 def status_color(self):
509 def status_color(self):
510 color = 'muted'
510 color = 'muted'
511 if self.status == 0:
511 if self.status == 0:
512 color = "danger"
512 color = "danger"
513 elif self.status == 1:
513 elif self.status == 1:
514 color = "warning"
514 color = "warning"
515 elif self.status == 2:
515 elif self.status == 2:
516 color = "success"
516 color = "success"
517 elif self.status == 3:
517 elif self.status == 3:
518 color = "info"
518 color = "info"
519
519
520 return color
520 return color
521
521
522 def parms_to_dict(self):
522 def parms_to_dict(self):
523
523
524 params = Params({})
524 params = Params({})
525 params.add(self.jsonify(), 'experiments')
525 params.add(self.jsonify(), 'experiments')
526
526
527 configurations = Configuration.objects.filter(experiment=self, type=0)
527 configurations = Configuration.objects.filter(experiment=self, type=0)
528
528
529 for conf in configurations:
529 for conf in configurations:
530 params.add(conf.jsonify(), 'configurations')
530 params.add(conf.jsonify(), 'configurations')
531 if conf.device.device_type.name=='rc':
531 if conf.device.device_type.name=='rc':
532 for line in conf.get_lines():
532 for line in conf.get_lines():
533 params.add(line.jsonify(), 'lines')
533 params.add(line.jsonify(), 'lines')
534
534
535 return params.data
535 return params.data
536
536
537 def dict_to_parms(self, parms, CONF_MODELS, id_exp=None):
537 def dict_to_parms(self, parms, CONF_MODELS, id_exp=None):
538
538
539 configurations = Configuration.objects.filter(experiment=self)
539 configurations = Configuration.objects.filter(experiment=self)
540
540
541 if id_exp is not None:
541 if id_exp is not None:
542 exp_parms = parms['experiments']['byId'][id_exp]
542 exp_parms = parms['experiments']['byId'][id_exp]
543 else:
543 else:
544 exp_parms = parms['experiments']['byId'][parms['experiments']['allIds'][0]]
544 exp_parms = parms['experiments']['byId'][parms['experiments']['allIds'][0]]
545
545
546 if configurations:
546 if configurations:
547 for configuration in configurations:
547 for configuration in configurations:
548 configuration.delete()
548 configuration.delete()
549
549
550 for id_conf in exp_parms['configurations']:
550 for id_conf in exp_parms['configurations']:
551 conf_parms = parms['configurations']['byId'][id_conf]
551 conf_parms = parms['configurations']['byId'][id_conf]
552 device = Device.objects.filter(device_type__name=conf_parms['device_type'])[0]
552 device = Device.objects.filter(device_type__name=conf_parms['device_type'])[0]
553 model = CONF_MODELS[conf_parms['device_type']]
553 model = CONF_MODELS[conf_parms['device_type']]
554 conf = model(
554 conf = model(
555 experiment = self,
555 experiment = self,
556 device = device,
556 device = device,
557 )
557 )
558 conf.dict_to_parms(parms, id=id_conf)
558 conf.dict_to_parms(parms, id=id_conf)
559
559
560
560
561 location, created = Location.objects.get_or_create(name=exp_parms['location'])
561 location, created = Location.objects.get_or_create(name=exp_parms['location'])
562 self.name = '{}-{}'.format(exp_parms['name'], datetime.now().strftime('%y%m%d'))
562 self.name = '{}-{}'.format(exp_parms['name'], datetime.now().strftime('%y%m%d'))
563 self.location = location
563 self.location = location
564 self.start_time = exp_parms['start_time']
564 self.start_time = exp_parms['start_time']
565 self.end_time = exp_parms['end_time']
565 self.end_time = exp_parms['end_time']
566 self.save()
566 self.save()
567
567
568 return self
568 return self
569
569
570 def get_absolute_url(self):
570 def get_absolute_url(self):
571 return reverse('url_experiment', args=[str(self.id)])
571 return reverse('url_experiment', args=[str(self.id)])
572
572
573 def get_absolute_url_edit(self):
573 def get_absolute_url_edit(self):
574 return reverse('url_edit_experiment', args=[str(self.id)])
574 return reverse('url_edit_experiment', args=[str(self.id)])
575
575
576 def get_absolute_url_delete(self):
576 def get_absolute_url_delete(self):
577 return reverse('url_delete_experiment', args=[str(self.id)])
577 return reverse('url_delete_experiment', args=[str(self.id)])
578
578
579 def get_absolute_url_import(self):
579 def get_absolute_url_import(self):
580 return reverse('url_import_experiment', args=[str(self.id)])
580 return reverse('url_import_experiment', args=[str(self.id)])
581
581
582 def get_absolute_url_export(self):
582 def get_absolute_url_export(self):
583 return reverse('url_export_experiment', args=[str(self.id)])
583 return reverse('url_export_experiment', args=[str(self.id)])
584
584
585 def get_absolute_url_start(self):
585 def get_absolute_url_start(self):
586 return reverse('url_start_experiment', args=[str(self.id)])
586 return reverse('url_start_experiment', args=[str(self.id)])
587
587
588 def get_absolute_url_stop(self):
588 def get_absolute_url_stop(self):
589 return reverse('url_stop_experiment', args=[str(self.id)])
589 return reverse('url_stop_experiment', args=[str(self.id)])
590
590
591
591
592 class Configuration(PolymorphicModel):
592 class Configuration(PolymorphicModel):
593
593
594 template = models.BooleanField(default=False)
594 template = models.BooleanField(default=False)
595 # name = models.CharField(verbose_name="Configuration Name", max_length=40, default='')
595 # name = models.CharField(verbose_name="Configuration Name", max_length=40, default='')
596 device = models.ForeignKey('Device', verbose_name='Device', null=True, on_delete=models.CASCADE)
596 device = models.ForeignKey('Device', verbose_name='Device', null=True, on_delete=models.CASCADE)
597 label = models.CharField(verbose_name="Label", max_length=40, default='', blank=True, null=True)
597 label = models.CharField(verbose_name="Label", max_length=40, default='', blank=True, null=True)
598 experiment = models.ForeignKey('Experiment', verbose_name='Experiment', null=True, blank=True, on_delete=models.CASCADE)
598 experiment = models.ForeignKey('Experiment', verbose_name='Experiment', null=True, blank=True, on_delete=models.CASCADE)
599 type = models.PositiveSmallIntegerField(default=0, choices=CONF_TYPES)
599 type = models.PositiveSmallIntegerField(default=0, choices=CONF_TYPES)
600 created_date = models.DateTimeField(auto_now_add=True)
600 created_date = models.DateTimeField(auto_now_add=True)
601 programmed_date = models.DateTimeField(auto_now=True)
601 programmed_date = models.DateTimeField(auto_now=True)
602 parameters = models.TextField(default='{}')
602 parameters = models.TextField(default='{}')
603 author = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE)
603 author = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE)
604 hash = models.CharField(default='', max_length=64, null=True, blank=True)
604 hash = models.CharField(default='', max_length=64, null=True, blank=True)
605 message = ""
605 message = ""
606
606
607 class Meta:
607 class Meta:
608 db_table = 'db_configurations'
608 db_table = 'db_configurations'
609 ordering = ('device__device_type__name',)
609 ordering = ('device__device_type__name',)
610
610
611 def __str__(self):
611 def __str__(self):
612
612
613 ret = u'{} '.format(self.device.device_type.name.upper())
613 ret = u'{} '.format(self.device.device_type.name.upper())
614
614
615 if 'mix' in [f.name for f in self._meta.get_fields()]:
615 if 'mix' in [f.name for f in self._meta.get_fields()]:
616 if self.mix:
616 if self.mix:
617 ret = '{} MIX '.format(self.device.device_type.name.upper())
617 ret = '{} MIX '.format(self.device.device_type.name.upper())
618
618
619 if 'label' in [f.name for f in self._meta.get_fields()]:
619 if 'label' in [f.name for f in self._meta.get_fields()]:
620 ret += '{}'.format(self.label)
620 ret += '{}'.format(self.label)
621
621
622 if self.template:
622 if self.template:
623 ret += ' (template)'
623 ret += ' (template)'
624
624
625 return ret
625 return ret
626
626
627 @property
627 @property
628 def name(self):
628 def name(self):
629
629
630 return str(self)
630 return str(self)
631
631
632 def jsonify(self):
632 def jsonify(self):
633
633
634 data = {}
634 data = {}
635
635
636 ignored = ('type', 'polymorphic_ctype', 'configuration_ptr',
636 ignored = ('type', 'polymorphic_ctype', 'configuration_ptr',
637 'created_date', 'programmed_date', 'template', 'device',
637 'created_date', 'programmed_date', 'template', 'device',
638 'experiment')
638 'experiment')
639
639
640 for field in self._meta.fields:
640 for field in self._meta.fields:
641 if field.name in ignored:
641 if field.name in ignored:
642 continue
642 continue
643 data[field.name] = field.value_from_object(self)
643 data[field.name] = field.value_from_object(self)
644
644
645 data['device_type'] = self.device.device_type.name
645 data['device_type'] = self.device.device_type.name
646
646
647 if self.device.device_type.name == 'rc':
647 if self.device.device_type.name == 'rc':
648 data['lines'] = ['{}'.format(line.pk) for line in self.get_lines()]
648 data['lines'] = ['{}'.format(line.pk) for line in self.get_lines()]
649 data['delays'] = self.get_delays()
649 data['delays'] = self.get_delays()
650 data['pulses'] = self.get_pulses()
650 data['pulses'] = self.get_pulses()
651
651
652 elif self.device.device_type.name == 'jars':
652 elif self.device.device_type.name == 'jars':
653 data['decode_type'] = DECODE_TYPE[self.decode_data][1]
653 data['decode_type'] = DECODE_TYPE[self.decode_data][1]
654
654
655 elif self.device.device_type.name == 'dds':
655 elif self.device.device_type.name == 'dds':
656 data['frequencyA_Mhz'] = float(data['frequencyA_Mhz'])
656 data['frequencyA_Mhz'] = float(data['frequencyA_Mhz'])
657 data['frequencyB_Mhz'] = float(data['frequencyB_Mhz'])
657 data['frequencyB_Mhz'] = float(data['frequencyB_Mhz'])
658 data['phaseA'] = dds_data.phase_to_binary(data['phaseA_degrees'])
658 data['phaseA'] = dds_data.phase_to_binary(data['phaseA_degrees'])
659 data['phaseB'] = dds_data.phase_to_binary(data['phaseB_degrees'])
659 data['phaseB'] = dds_data.phase_to_binary(data['phaseB_degrees'])
660
660
661 elif self.device.device_type.name == 'dds_rest':
661 elif self.device.device_type.name == 'dds_rest':
662 data['frequencyA_Mhz'] = float(data['frequencyA_Mhz'])
662 data['frequencyA_Mhz'] = float(data['frequencyA_Mhz'])
663 data['frequencyB_Mhz'] = float(data['frequencyB_Mhz'])
663 data['frequencyB_Mhz'] = float(data['frequencyB_Mhz'])
664 data['phaseA'] = dds_data.phase_to_binary(data['phaseA_degrees'])
664 data['phaseA'] = dds_data.phase_to_binary(data['phaseA_degrees'])
665 data['phaseB'] = dds_data.phase_to_binary(data['phaseB_degrees'])
665 data['phaseB'] = dds_data.phase_to_binary(data['phaseB_degrees'])
666 data['delta_frequency_Mhz'] = float(data['delta_frequency_Mhz'] or 0.00)
666 data['delta_frequency_Mhz'] = float(data['delta_frequency_Mhz'] or 0.00)
667 data['update_clock_Mhz'] = float(data['update_clock_Mhz'] or 0.00)
667 data['update_clock_Mhz'] = float(data['update_clock_Mhz'] or 0.00)
668 data['ramp_rate_clock_Mhz'] = float(data['ramp_rate_clock_Mhz'] or 0.0)
668 data['ramp_rate_clock_Mhz'] = float(data['ramp_rate_clock_Mhz'] or 0.0)
669 return data
669 return data
670
670
671 def clone(self, **kwargs):
671 def clone(self, **kwargs):
672
672
673 self.pk = None
673 self.pk = None
674 self.id = None
674 self.id = None
675 for attr, value in kwargs.items():
675 for attr, value in kwargs.items():
676 setattr(self, attr, value)
676 setattr(self, attr, value)
677
677
678 self.save()
678 self.save()
679
679
680 return self
680 return self
681
681
682 def parms_to_dict(self):
682 def parms_to_dict(self):
683
683
684 params = Params({})
684 params = Params({})
685 params.add(self.jsonify(), 'configurations')
685 params.add(self.jsonify(), 'configurations')
686
686
687 if self.device.device_type.name=='rc':
687 if self.device.device_type.name=='rc':
688 for line in self.get_lines():
688 for line in self.get_lines():
689 params.add(line.jsonify(), 'lines')
689 params.add(line.jsonify(), 'lines')
690
690
691 return params.data
691 return params.data
692
692
693 def parms_to_text(self):
693 def parms_to_text(self):
694
694
695 raise NotImplementedError("This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper())
695 raise NotImplementedError("This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper())
696
696
697
697
698 def parms_to_binary(self):
698 def parms_to_binary(self):
699
699
700 raise NotImplementedError("This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper())
700 raise NotImplementedError("This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper())
701
701
702
702
703 def dict_to_parms(self, parameters, id=None):
703 def dict_to_parms(self, parameters, id=None):
704
704
705 params = Params(parameters)
705 params = Params(parameters)
706
706
707 if id:
707 if id:
708 data = params.get_conf(id_conf=id)
708 data = params.get_conf(id_conf=id)
709 else:
709 else:
710 data = params.get_conf(dtype=self.device.device_type.name)
710 data = params.get_conf(dtype=self.device.device_type.name)
711
711
712 if data['device_type']=='rc':
712 if data['device_type']=='rc':
713 self.clean_lines()
713 self.clean_lines()
714 lines = data.pop('lines', None)
714 lines = data.pop('lines', None)
715 for line_id in lines:
715 for line_id in lines:
716 pass
716 pass
717
717
718 for key, value in data.items():
718 for key, value in data.items():
719 if key not in ('id', 'device_type'):
719 if key not in ('id', 'device_type'):
720 setattr(self, key, value)
720 setattr(self, key, value)
721
721
722 self.save()
722 self.save()
723
723
724
724
725 def export_to_file(self, format="json"):
725 def export_to_file(self, format="json"):
726
726
727 content_type = ''
727 content_type = ''
728
728
729 if format == 'racp':
729 if format == 'racp':
730 content_type = 'text/plain'
730 content_type = 'text/plain'
731 filename = '%s_%s.%s' %(self.device.device_type.name, self.name, 'racp')
731 filename = '%s_%s.%s' %(self.device.device_type.name, self.name, 'racp')
732 content = self.parms_to_text(file_format = 'racp')
732 content = self.parms_to_text(file_format = 'racp')
733
733
734 if format == 'text':
734 if format == 'text':
735 content_type = 'text/plain'
735 content_type = 'text/plain'
736 filename = '%s_%s.%s' %(self.device.device_type.name, self.name, self.device.device_type.name)
736 filename = '%s_%s.%s' %(self.device.device_type.name, self.name, self.device.device_type.name)
737 content = self.parms_to_text()
737 content = self.parms_to_text()
738
738
739 if format == 'binary':
739 if format == 'binary':
740 content_type = 'application/octet-stream'
740 content_type = 'application/octet-stream'
741 filename = '%s_%s.bin' %(self.device.device_type.name, self.name)
741 filename = '%s_%s.bin' %(self.device.device_type.name, self.name)
742 content = self.parms_to_binary()
742 content = self.parms_to_binary()
743
743
744 if not content_type:
744 if not content_type:
745 content_type = 'application/json'
745 content_type = 'application/json'
746 filename = '%s_%s.json' %(self.device.device_type.name, self.name)
746 filename = '%s_%s.json' %(self.device.device_type.name, self.name)
747 content = json.dumps(self.parms_to_dict(), indent=2)
747 content = json.dumps(self.parms_to_dict(), indent=2)
748
748
749 fields = {'content_type':content_type,
749 fields = {'content_type':content_type,
750 'filename':filename,
750 'filename':filename,
751 'content':content
751 'content':content
752 }
752 }
753
753
754 return fields
754 return fields
755
755
756 def import_from_file(self, fp):
756 def import_from_file(self, fp):
757
757
758 parms = {}
758 parms = {}
759
759
760 path, ext = os.path.splitext(fp.name)
760 path, ext = os.path.splitext(fp.name)
761
761
762 if ext == '.json':
762 if ext == '.json':
763 parms = json.load(fp)
763 parms = json.load(fp)
764
764
765 if ext == '.dds':
765 if ext == '.dds':
766 lines = fp.readlines()
766 lines = fp.readlines()
767 parms = dds_data.text_to_dict(lines)
767 parms = dds_data.text_to_dict(lines)
768
768
769 if ext == '.racp':
769 if ext == '.racp':
770 if self.device.device_type.name == 'jars':
770 if self.device.device_type.name == 'jars':
771 parms = RacpFile(fp).to_dict()
771 parms = RacpFile(fp).to_dict()
772 parms['filter_parms'] = json.loads(self.filter_parms)
772 parms['filter_parms'] = json.loads(self.filter_parms)
773 return parms
773 return parms
774 parms = RCFile(fp).to_dict()
774 parms = RCFile(fp).to_dict()
775
775
776 return parms
776 return parms
777
777
778 def status_device(self):
778 def status_device(self):
779
779
780 self.message = 'Function not implemented'
780 self.message = 'Function not implemented'
781 return False
781 return False
782
782
783
783
784 def stop_device(self):
784 def stop_device(self):
785
785
786 self.message = 'Function not implemented'
786 self.message = 'Function not implemented'
787 print("BUENAS SEÑALES??? NO LO CREO2",flush=True)
788 return False
787 return False
789
788
790
789
791 def start_device(self):
790 def start_device(self):
792
791
793 self.message = 'Function not implemented'
792 self.message = 'Function not implemented'
794 print("BUENAS SEÑALES??? NO LO CREO",flush=True)
795 return False
793 return False
796
794
797
795
798 def write_device(self, parms):
796 def write_device(self, parms):
799
797
800 self.message = 'Function not implemented'
798 self.message = 'Function not implemented'
801 print("BUENAS SEÑALES??? NO LO CREO3",flush=True)
802 return False
799 return False
803
800
804 def write_device_mqtt(self, parms):
801 def write_device_mqtt(self, parms):
805
802
806 self.message = 'Function not implemented'
803 self.message = 'Function not implemented'
807 return False
804 return False
808
805
809 def read_device(self):
806 def read_device(self):
810
807
811 self.message = 'Function not implemented'
808 self.message = 'Function not implemented'
812 return False
809 return False
813
810
814
811
815 def get_absolute_url(self):
812 def get_absolute_url(self):
816 return reverse('url_%s_conf' % self.device.device_type.name, args=[str(self.id)])
813 return reverse('url_%s_conf' % self.device.device_type.name, args=[str(self.id)])
817
814
818 def get_absolute_mqtt_url(self):
815 def get_absolute_mqtt_url(self):
819 print("----------------- {} ----------------------".format(self.device.device_type.name),flush=True)
816 print("----------------- {} ----------------------".format(self.device.device_type.name),flush=True)
820 if self.device.device_type.name=='abs':
817 if self.device.device_type.name=='abs':
821 return reverse('url_%s_conf_mqtt' % self.device.device_type.name, args=[str(self.id)])
818 return reverse('url_%s_conf_mqtt' % self.device.device_type.name, args=[str(self.id)])
822 else:
819 else:
823 return reverse('url_%s_conf' % self.device.device_type.name, args=[str(self.id)])
820 return reverse('url_%s_conf' % self.device.device_type.name, args=[str(self.id)])
824
821
825 def get_absolute_url_edit(self):
822 def get_absolute_url_edit(self):
826 return reverse('url_edit_%s_conf' % self.device.device_type.name, args=[str(self.id)])
823 return reverse('url_edit_%s_conf' % self.device.device_type.name, args=[str(self.id)])
827
824
828 def get_absolute_url_delete(self):
825 def get_absolute_url_delete(self):
829 return reverse('url_delete_dev_conf', args=[str(self.id)])
826 return reverse('url_delete_dev_conf', args=[str(self.id)])
830
827
831 def get_absolute_url_import(self):
828 def get_absolute_url_import(self):
832 return reverse('url_import_dev_conf', args=[str(self.id)])
829 return reverse('url_import_dev_conf', args=[str(self.id)])
833
830
834 def get_absolute_url_export(self):
831 def get_absolute_url_export(self):
835 return reverse('url_export_dev_conf', args=[str(self.id)])
832 return reverse('url_export_dev_conf', args=[str(self.id)])
836
833
837 def get_absolute_url_write(self):
834 def get_absolute_url_write(self):
838 return reverse('url_write_dev_conf', args=[str(self.id)])
835 return reverse('url_write_dev_conf', args=[str(self.id)])
839
836
840 def get_absolute_url_write_mqtt(self):
837 def get_absolute_url_write_mqtt(self):
841 return reverse('url_write_mqtt_dev_conf', args=[str(self.id)])
838 return reverse('url_write_mqtt_dev_conf', args=[str(self.id)])
842
839
843 def get_absolute_url_read(self):
840 def get_absolute_url_read(self):
844 return reverse('url_read_dev_conf', args=[str(self.id)])
841 return reverse('url_read_dev_conf', args=[str(self.id)])
845
842
846 def get_absolute_url_start(self):
843 def get_absolute_url_start(self):
847 return reverse('url_start_dev_conf', args=[str(self.id)])
844 return reverse('url_start_dev_conf', args=[str(self.id)])
848
845
849 def get_absolute_url_stop(self):
846 def get_absolute_url_stop(self):
850 return reverse('url_stop_dev_conf', args=[str(self.id)])
847 return reverse('url_stop_dev_conf', args=[str(self.id)])
851
848
852 def get_absolute_url_stop_mqtt(self):
849 def get_absolute_url_stop_mqtt(self):
853 return reverse('url_stop_mqtt_dev_conf', args=[str(self.id)])
850 return reverse('url_stop_mqtt_dev_conf', args=[str(self.id)])
854
851
855 def get_absolute_url_status(self):
852 def get_absolute_url_status(self):
856 return reverse('url_status_dev_conf', args=[str(self.id)])
853 return reverse('url_status_dev_conf', args=[str(self.id)])
@@ -1,444 +1,436
1
1
2 import json
2 import json
3
3
4 from django.contrib import messages
4 from django.contrib import messages
5 from django.utils.safestring import mark_safe
5 from django.utils.safestring import mark_safe
6 from django.shortcuts import render, redirect, get_object_or_404, HttpResponse
6 from django.shortcuts import render, redirect, get_object_or_404, HttpResponse
7 from django.contrib.auth.decorators import login_required
7 from django.contrib.auth.decorators import login_required
8
8
9 from apps.main.models import Experiment, Device
9 from apps.main.models import Experiment, Device
10 from apps.main.views import sidebar
10 from apps.main.views import sidebar
11
11
12 from .models import RCConfiguration, RCLine, RCLineType, RCLineCode, RCClock
12 from .models import RCConfiguration, RCLine, RCLineType, RCLineCode, RCClock
13 from .forms import RCConfigurationForm, RCLineForm, RCLineViewForm, RCLineEditForm, RCImportForm, RCLineCodesForm, RCClockForm
13 from .forms import RCConfigurationForm, RCLineForm, RCLineViewForm, RCLineEditForm, RCImportForm, RCLineCodesForm, RCClockForm
14
14
15
15
16 def conf(request, conf_id):
16 def conf(request, conf_id):
17
17
18 conf = get_object_or_404(RCConfiguration, pk=conf_id)
18 conf = get_object_or_404(RCConfiguration, pk=conf_id)
19
19
20 lines = RCLine.objects.filter(rc_configuration=conf).order_by('channel')
20 lines = RCLine.objects.filter(rc_configuration=conf).order_by('channel')
21 clk = RCClock.objects.filter(rc_configuration=conf).first()
21 clk = RCClock.objects.filter(rc_configuration=conf).first()
22 if clk is None:
22 if clk is None:
23 clk = RCClock(rc_configuration=conf)
23 clk = RCClock(rc_configuration=conf)
24 clk.save()
24 clk.save()
25
25
26 for line in lines:
26 for line in lines:
27 params = json.loads(line.params)
27 params = json.loads(line.params)
28 line.form = RCLineViewForm(extra_fields=params, line=line)
28 line.form = RCLineViewForm(extra_fields=params, line=line)
29 if 'params' in params:
29 if 'params' in params:
30 line.subforms = [RCLineViewForm(extra_fields=fields, line=line, subform=True) for fields in params['params']]
30 line.subforms = [RCLineViewForm(extra_fields=fields, line=line, subform=True) for fields in params['params']]
31
31
32 kwargs = {}
32 kwargs = {}
33 kwargs['clock'] = clk
33 kwargs['clock'] = clk
34 kwargs['dev_conf'] = conf
34 kwargs['dev_conf'] = conf
35 kwargs['rc_lines'] = lines
35 kwargs['rc_lines'] = lines
36 kwargs['dev_conf_keys'] = ['ipp_unit', 'ntx', 'clock_divider', 'clock',
36 kwargs['dev_conf_keys'] = ['ipp_unit', 'ntx', 'clock_divider', 'clock',
37 'time_before', 'time_after', 'sync', 'sampling_reference',
37 'time_before', 'time_after', 'sync', 'sampling_reference',
38 'control_tx', 'control_sw']
38 'control_tx', 'control_sw']
39
39
40 kwargs['title'] = 'Configuration'
40 kwargs['title'] = 'Configuration'
41 kwargs['suptitle'] = 'Detail'
41 kwargs['suptitle'] = 'Detail'
42
42
43 kwargs['button'] = 'Edit Configuration'
43 kwargs['button'] = 'Edit Configuration'
44 ###### SIDEBAR ######
44 ###### SIDEBAR ######
45 kwargs.update(sidebar(conf=conf))
45 kwargs.update(sidebar(conf=conf))
46
46
47 return render(request, 'rc_conf.html', kwargs)
47 return render(request, 'rc_conf.html', kwargs)
48
48
49 @login_required
49 @login_required
50 def conf_edit(request, conf_id):
50 def conf_edit(request, conf_id):
51
51
52 conf = get_object_or_404(RCConfiguration, pk=conf_id)
52 conf = get_object_or_404(RCConfiguration, pk=conf_id)
53 clock = RCClock.objects.get(rc_configuration=conf)
53 clock = RCClock.objects.get(rc_configuration=conf)
54 lines = RCLine.objects.filter(rc_configuration=conf).order_by('channel')
54 lines = RCLine.objects.filter(rc_configuration=conf).order_by('channel')
55
55
56 for line in lines:
56 for line in lines:
57 params = json.loads(line.params)
57 params = json.loads(line.params)
58 #print(params)
58 #print(params)
59 line.form = RCLineEditForm(conf=conf, line=line, extra_fields=params)
59 line.form = RCLineEditForm(conf=conf, line=line, extra_fields=params)
60 line.subform = False
60 line.subform = False
61
61
62 if 'params' in params:
62 if 'params' in params:
63 line.subforms = [RCLineEditForm(extra_fields=fields, line=line, subform=i) for i, fields in enumerate(params['params'])]
63 line.subforms = [RCLineEditForm(extra_fields=fields, line=line, subform=i) for i, fields in enumerate(params['params'])]
64 line.subform = True
64 line.subform = True
65 print(params)
65 print(params)
66 print("fin de sub carga de params")
66 print("fin de sub carga de params")
67 #print("fin de carga de params")
67 #print("fin de carga de params")
68 if request.method=='GET':
68 if request.method=='GET':
69 print("GET case")
69 print("GET case")
70 form = RCConfigurationForm(instance=conf)
70 form = RCConfigurationForm(instance=conf)
71 form_clock = RCClockForm(instance=clock)
71 form_clock = RCClockForm(instance=clock)
72
72
73 elif request.method=='POST':
73 elif request.method=='POST':
74 #print("ingreso a post conf edit")
74 #print("ingreso a post conf edit")
75 line_data = {}
75 line_data = {}
76 conf_data = {}
76 conf_data = {}
77 clock_data = {}
77 clock_data = {}
78 extras = []
78 extras = []
79 print("Inicio impresion POST#####")
79 print("Inicio impresion POST#####")
80 print(request.POST.items)
80 print(request.POST.items)
81 print("Fin impresion de POST items#####")
81 print("Fin impresion de POST items#####")
82 #classified post fields
82 #classified post fields
83 for label,value in request.POST.items():
83 for label,value in request.POST.items():
84 if label=='csrfmiddlewaretoken':
84 if label=='csrfmiddlewaretoken':
85 continue
85 continue
86
86
87 if label.count('|')==0:
87 if label.count('|')==0:
88 if label in ('mode', 'multiplier', 'divisor', 'reference', 'frequency'):
88 if label in ('mode', 'multiplier', 'divisor', 'reference', 'frequency'):
89 clock_data[label] = value
89 clock_data[label] = value
90 else:
90 else:
91 conf_data[label] = value
91 conf_data[label] = value
92 continue
92 continue
93
93
94 elif label.split('|')[0]!='-1':
94 elif label.split('|')[0]!='-1':
95 extras.append(label)
95 extras.append(label)
96 continue
96 continue
97
97
98 #print(label)
98 #print(label)
99 x, pk, name = label.split('|')
99 x, pk, name = label.split('|')
100
100
101 if name=='codes':
101 if name=='codes':
102 value = [s for s in value.split('\r\n') if s]
102 value = [s for s in value.split('\r\n') if s]
103
103
104 if pk in line_data:
104 if pk in line_data:
105 line_data[pk][name] = value
105 line_data[pk][name] = value
106 else:
106 else:
107 line_data[pk] = {name:value}
107 line_data[pk] = {name:value}
108 #print(line_data[pk])
108 #print(line_data[pk])
109 #update conf
109 #update conf
110
110
111 form_clock = RCClockForm(clock_data, instance=clock)
111 form_clock = RCClockForm(clock_data, instance=clock)
112 form = RCConfigurationForm(conf_data, instance=conf)
112 form = RCConfigurationForm(conf_data, instance=conf)
113
113
114 #print(request.POST.items())
114 #print(request.POST.items())
115
115
116 if form_clock.is_valid() and form.is_valid():
116 if form_clock.is_valid() and form.is_valid():
117 form_clock.save()
117 form_clock.save()
118 form.save()
118 form.save()
119
119
120 #update lines fields
120 #update lines fields
121 extras.sort()
121 extras.sort()
122 #print("Inicio extras")
122 #print("Inicio extras")
123 #print(extras)
123 #print(extras)
124 #print("Fin extras")
124 #print("Fin extras")
125 for label in extras:
125 for label in extras:
126 x, pk, name = label.split('|')
126 x, pk, name = label.split('|')
127 if pk not in line_data:
127 if pk not in line_data:
128 line_data[pk] = {}
128 line_data[pk] = {}
129 if 'params' not in line_data[pk]:
129 if 'params' not in line_data[pk]:
130 line_data[pk]['params'] = []
130 line_data[pk]['params'] = []
131 if len(line_data[pk]['params'])<int(x)+1:
131 if len(line_data[pk]['params'])<int(x)+1:
132 line_data[pk]['params'].append({})
132 line_data[pk]['params'].append({})
133 line_data[pk]['params'][int(x)][name] = float(request.POST[label])
133 line_data[pk]['params'][int(x)][name] = float(request.POST[label])
134
134
135 for pk, params in line_data.items():
135 for pk, params in line_data.items():
136 line = RCLine.objects.get(pk=pk)
136 line = RCLine.objects.get(pk=pk)
137 if line.line_type.name in ('windows', 'prog_pulses'):
137 if line.line_type.name in ('windows', 'prog_pulses'):
138 if 'params' not in params:
138 if 'params' not in params:
139 params['params'] = []
139 params['params'] = []
140 line.params = json.dumps(params)
140 line.params = json.dumps(params)
141 #print(line.params)
141 #print(line.params)
142 line.save()
142 line.save()
143
143
144
144
145 #update pulses field
145 #update pulses field
146 conf.update_pulses()
146 conf.update_pulses()
147
147
148 messages.success(request, 'RC Configuration successfully updated')
148 messages.success(request, 'RC Configuration successfully updated')
149
149
150 return redirect(conf.get_absolute_url())
150 return redirect(conf.get_absolute_url())
151
151
152 kwargs = {}
152 kwargs = {}
153 kwargs['dev_conf'] = conf
153 kwargs['dev_conf'] = conf
154 kwargs['form'] = form
154 kwargs['form'] = form
155 kwargs['form_clock'] = form_clock
155 kwargs['form_clock'] = form_clock
156 kwargs['rc_lines'] = lines
156 kwargs['rc_lines'] = lines
157 kwargs['edit'] = True
157 kwargs['edit'] = True
158
158
159 kwargs['title'] = 'RC Configuration'
159 kwargs['title'] = 'RC Configuration'
160 kwargs['suptitle'] = 'Edit'
160 kwargs['suptitle'] = 'Edit'
161 kwargs['button'] = 'Update'
161 kwargs['button'] = 'Update'
162
162
163 return render(request, 'rc_conf_edit.html', kwargs)
163 return render(request, 'rc_conf_edit.html', kwargs)
164
164
165
165
166 def add_line(request, conf_id, line_type_id=None, code_id=None):
166 def add_line(request, conf_id, line_type_id=None, code_id=None):
167
167
168 conf = get_object_or_404(RCConfiguration, pk=conf_id)
168 conf = get_object_or_404(RCConfiguration, pk=conf_id)
169
169
170 if request.method=='GET':
170 if request.method=='GET':
171 if line_type_id:
171 if line_type_id:
172 line_type = get_object_or_404(RCLineType, pk=line_type_id)
172 line_type = get_object_or_404(RCLineType, pk=line_type_id)
173
173
174 if code_id:
174 if code_id:
175 form = RCLineForm(initial={'rc_configuration':conf_id, 'line_type': line_type_id, 'code_id': code_id},
175 form = RCLineForm(initial={'rc_configuration':conf_id, 'line_type': line_type_id, 'code_id': code_id},
176 extra_fields=json.loads(line_type.params))
176 extra_fields=json.loads(line_type.params))
177 else:
177 else:
178 form = RCLineForm(initial={'rc_configuration':conf_id, 'line_type': line_type_id},
178 form = RCLineForm(initial={'rc_configuration':conf_id, 'line_type': line_type_id},
179 extra_fields=json.loads(line_type.params))
179 extra_fields=json.loads(line_type.params))
180 else:
180 else:
181 line_type = {'id':0}
181 line_type = {'id':0}
182 form = RCLineForm(initial={'rc_configuration':conf_id})
182 form = RCLineForm(initial={'rc_configuration':conf_id})
183
183
184 if request.method=='POST':
184 if request.method=='POST':
185
185
186 line_type = get_object_or_404(RCLineType, pk=line_type_id)
186 line_type = get_object_or_404(RCLineType, pk=line_type_id)
187 form = RCLineForm(request.POST,
187 form = RCLineForm(request.POST,
188 extra_fields=json.loads(line_type.params))
188 extra_fields=json.loads(line_type.params))
189
189
190 if form.is_valid():
190 if form.is_valid():
191 form.save()
191 form.save()
192 form.instance.update_pulses()
192 form.instance.update_pulses()
193 return redirect('url_edit_rc_conf', conf.id)
193 return redirect('url_edit_rc_conf', conf.id)
194
194
195 kwargs = {}
195 kwargs = {}
196 kwargs['form'] = form
196 kwargs['form'] = form
197 kwargs['title'] = 'RC Configuration'
197 kwargs['title'] = 'RC Configuration'
198 kwargs['suptitle'] = 'Add Line'
198 kwargs['suptitle'] = 'Add Line'
199 kwargs['button'] = 'Add'
199 kwargs['button'] = 'Add'
200 kwargs['previous'] = conf.get_absolute_url_edit()
200 kwargs['previous'] = conf.get_absolute_url_edit()
201 kwargs['dev_conf'] = conf
201 kwargs['dev_conf'] = conf
202 kwargs['line_type'] = line_type
202 kwargs['line_type'] = line_type
203
203
204 return render(request, 'rc_add_line.html', kwargs)
204 return render(request, 'rc_add_line.html', kwargs)
205
205
206 def edit_codes(request, conf_id, line_id, code_id=None):
206 def edit_codes(request, conf_id, line_id, code_id=None):
207
207
208 conf = get_object_or_404(RCConfiguration, pk=conf_id)
208 conf = get_object_or_404(RCConfiguration, pk=conf_id)
209 line = get_object_or_404(RCLine, pk=line_id)
209 line = get_object_or_404(RCLine, pk=line_id)
210 params = json.loads(line.params)
210 params = json.loads(line.params)
211
211
212 if request.method=='GET':
212 if request.method=='GET':
213 if code_id:
213 if code_id:
214 code = get_object_or_404(RCLineCode, pk=code_id)
214 code = get_object_or_404(RCLineCode, pk=code_id)
215 form = RCLineCodesForm(instance=code)
215 form = RCLineCodesForm(instance=code)
216 else:
216 else:
217 initial = {'code': params['code'],
217 initial = {'code': params['code'],
218 'codes': params['codes'] if 'codes' in params else [],
218 'codes': params['codes'] if 'codes' in params else [],
219 'number_of_codes': len(params['codes']) if 'codes' in params else 0,
219 'number_of_codes': len(params['codes']) if 'codes' in params else 0,
220 'bits_per_code': len(params['codes'][0]) if 'codes' in params else 0,
220 'bits_per_code': len(params['codes'][0]) if 'codes' in params else 0,
221 }
221 }
222 form = RCLineCodesForm(initial=initial)
222 form = RCLineCodesForm(initial=initial)
223
223
224 if request.method=='POST':
224 if request.method=='POST':
225 form = RCLineCodesForm(request.POST)
225 form = RCLineCodesForm(request.POST)
226 if form.is_valid():
226 if form.is_valid():
227 params['code'] = request.POST['code']
227 params['code'] = request.POST['code']
228 params['codes'] = [s for s in request.POST['codes'].split('\r\n') if s]
228 params['codes'] = [s for s in request.POST['codes'].split('\r\n') if s]
229 line.params = json.dumps(params)
229 line.params = json.dumps(params)
230 line.save()
230 line.save()
231 messages.success(request, 'Line: "%s" has been updated.' % line)
231 messages.success(request, 'Line: "%s" has been updated.' % line)
232 return redirect('url_edit_rc_conf', conf.id)
232 return redirect('url_edit_rc_conf', conf.id)
233
233
234 kwargs = {}
234 kwargs = {}
235 kwargs['form'] = form
235 kwargs['form'] = form
236 kwargs['title'] = line
236 kwargs['title'] = line
237 kwargs['suptitle'] = 'Edit'
237 kwargs['suptitle'] = 'Edit'
238 kwargs['button'] = 'Update'
238 kwargs['button'] = 'Update'
239 kwargs['dev_conf'] = conf
239 kwargs['dev_conf'] = conf
240 kwargs['previous'] = conf.get_absolute_url_edit()
240 kwargs['previous'] = conf.get_absolute_url_edit()
241 kwargs['line'] = line
241 kwargs['line'] = line
242
242
243 return render(request, 'rc_edit_codes.html', kwargs)
243 return render(request, 'rc_edit_codes.html', kwargs)
244
244
245 def add_subline(request, conf_id, line_id):
245 def add_subline(request, conf_id, line_id):
246
246
247 conf = get_object_or_404(RCConfiguration, pk=conf_id)
247 conf = get_object_or_404(RCConfiguration, pk=conf_id)
248 line = get_object_or_404(RCLine, pk=line_id)
248 line = get_object_or_404(RCLine, pk=line_id)
249
249
250 if request.method == 'POST':
250 if request.method == 'POST':
251 if line:
251 if line:
252 params = json.loads(line.params)
252 params = json.loads(line.params)
253 subparams = json.loads(line.line_type.params)
253 subparams = json.loads(line.line_type.params)
254 if 'params' in subparams:
254 if 'params' in subparams:
255 dum = {}
255 dum = {}
256 for key, value in subparams['params'].items():
256 for key, value in subparams['params'].items():
257 dum[key] = value['value']
257 dum[key] = value['value']
258 params['params'].append(dum)
258 params['params'].append(dum)
259 line.params = json.dumps(params)
259 line.params = json.dumps(params)
260 line.save()
260 line.save()
261 return redirect('url_edit_rc_conf', conf.id)
261 return redirect('url_edit_rc_conf', conf.id)
262
262
263 kwargs = {}
263 kwargs = {}
264
264
265 kwargs['title'] = 'Add new'
265 kwargs['title'] = 'Add new'
266 kwargs['suptitle'] = '%s to %s' % (line.line_type.name, line)
266 kwargs['suptitle'] = '%s to %s' % (line.line_type.name, line)
267
267
268 return render(request, 'confirm.html', kwargs)
268 return render(request, 'confirm.html', kwargs)
269
269
270 def remove_line(request, conf_id, line_id):
270 def remove_line(request, conf_id, line_id):
271
271
272 conf = get_object_or_404(RCConfiguration, pk=conf_id)
272 conf = get_object_or_404(RCConfiguration, pk=conf_id)
273 line = get_object_or_404(RCLine, pk=line_id)
273 line = get_object_or_404(RCLine, pk=line_id)
274
274
275 if request.method == 'POST':
275 if request.method == 'POST':
276 if line:
276 if line:
277 try:
277 try:
278 channel = line.channel
278 channel = line.channel
279 line.delete()
279 line.delete()
280 for ch in range(channel+1, RCLine.objects.filter(rc_configuration=conf).count()+1):
280 for ch in range(channel+1, RCLine.objects.filter(rc_configuration=conf).count()+1):
281 l = RCLine.objects.get(rc_configuration=conf, channel=ch)
281 l = RCLine.objects.get(rc_configuration=conf, channel=ch)
282 l.channel = l.channel-1
282 l.channel = l.channel-1
283 l.save()
283 l.save()
284 messages.success(request, 'Line: "%s" has been deleted.' % line)
284 messages.success(request, 'Line: "%s" has been deleted.' % line)
285 except:
285 except:
286 messages.error(request, 'Unable to delete line: "%s".' % line)
286 messages.error(request, 'Unable to delete line: "%s".' % line)
287
287
288 return redirect('url_edit_rc_conf', conf.id)
288 return redirect('url_edit_rc_conf', conf.id)
289
289
290 kwargs = {}
290 kwargs = {}
291
291
292 kwargs['object'] = line
292 kwargs['object'] = line
293 kwargs['delete'] = True
293 kwargs['delete'] = True
294 kwargs['title'] = 'Delete'
294 kwargs['title'] = 'Delete'
295 kwargs['suptitle'] = 'Line'
295 kwargs['suptitle'] = 'Line'
296 kwargs['previous'] = conf.get_absolute_url_edit()
296 kwargs['previous'] = conf.get_absolute_url_edit()
297 return render(request, 'confirm.html', kwargs)
297 return render(request, 'confirm.html', kwargs)
298
298
299
299
300 def remove_subline(request, conf_id, line_id, subline_id):
300 def remove_subline(request, conf_id, line_id, subline_id):
301
301
302 conf = get_object_or_404(RCConfiguration, pk=conf_id)
302 conf = get_object_or_404(RCConfiguration, pk=conf_id)
303 line = get_object_or_404(RCLine, pk=line_id)
303 line = get_object_or_404(RCLine, pk=line_id)
304
304
305 if request.method == 'POST':
305 if request.method == 'POST':
306 if line:
306 if line:
307 params = json.loads(line.params)
307 params = json.loads(line.params)
308 params['params'].remove(params['params'][int(subline_id)-1])
308 params['params'].remove(params['params'][int(subline_id)-1])
309 line.params = json.dumps(params)
309 line.params = json.dumps(params)
310 line.save()
310 line.save()
311
311
312 return redirect('url_edit_rc_conf', conf.id)
312 return redirect('url_edit_rc_conf', conf.id)
313
313
314 kwargs = {}
314 kwargs = {}
315
315
316 kwargs['object'] = line
316 kwargs['object'] = line
317 kwargs['object_name'] = line.line_type.name
317 kwargs['object_name'] = line.line_type.name
318 kwargs['delete_view'] = True
318 kwargs['delete_view'] = True
319 kwargs['title'] = 'Confirm delete'
319 kwargs['title'] = 'Confirm delete'
320
320
321 return render(request, 'confirm.html', kwargs)
321 return render(request, 'confirm.html', kwargs)
322
322
323
323
324 def update_lines_position(request, conf_id):
324 def update_lines_position(request, conf_id):
325
325
326 conf = get_object_or_404(RCConfiguration, pk=conf_id)
326 conf = get_object_or_404(RCConfiguration, pk=conf_id)
327 print("ingreso a update_lines_position")
327 print("ingreso a update_lines_position")
328 if request.method=='POST':
328 if request.method=='POST':
329 ch = 0
329 ch = 0
330 for item in request.POST['items'].split('&'):
330 for item in request.POST['items'].split('&'):
331 line = RCLine.objects.get(pk=item.split('=')[-1])
331 line = RCLine.objects.get(pk=item.split('=')[-1])
332 line.channel = ch
332 line.channel = ch
333 line.save()
333 line.save()
334 ch += 1
334 ch += 1
335
335
336 lines = RCLine.objects.filter(rc_configuration=conf).order_by('channel')
336 lines = RCLine.objects.filter(rc_configuration=conf).order_by('channel')
337
337
338 for line in lines:
338 for line in lines:
339
339
340 params = json.loads(line.params)
340 params = json.loads(line.params)
341 print(params)
341 print(params)
342 print("Fin de impresion_lines_position")
342 print("Fin de impresion_lines_position")
343 line.form = RCLineEditForm(conf=conf, line=line, extra_fields=params)
343 line.form = RCLineEditForm(conf=conf, line=line, extra_fields=params)
344
344
345 if 'params' in params:
345 if 'params' in params:
346 line.subform = True
346 line.subform = True
347 line.subforms = [RCLineEditForm(extra_fields=fields, line=line, subform=i) for i, fields in enumerate(params['params'])]
347 line.subforms = [RCLineEditForm(extra_fields=fields, line=line, subform=i) for i, fields in enumerate(params['params'])]
348
348
349 html = render(request, 'rc_lines.html', {'dev_conf':conf, 'rc_lines':lines, 'edit':True})
349 html = render(request, 'rc_lines.html', {'dev_conf':conf, 'rc_lines':lines, 'edit':True})
350 #html = render('rc_lines.html', {'dev_conf':conf, 'rc_lines':lines, 'edit':True},request)
350 #html = render('rc_lines.html', {'dev_conf':conf, 'rc_lines':lines, 'edit':True},request)
351 data = {'html': html.content.decode('utf8')}
351 data = {'html': html.content.decode('utf8')}
352
352
353 return HttpResponse(json.dumps(data), content_type="application/json")
353 return HttpResponse(json.dumps(data), content_type="application/json")
354 return redirect('url_edit_rc_conf', conf.id)
354 return redirect('url_edit_rc_conf', conf.id)
355
355
356
356
357 def import_file(request, conf_id):
357 def import_file(request, conf_id):
358 # print("HOLA",flush=True)
359 conf = get_object_or_404(RCConfiguration, pk=conf_id)
358 conf = get_object_or_404(RCConfiguration, pk=conf_id)
360 # print("HOLA2",flush=True)
361 if request.method=='POST':
359 if request.method=='POST':
362 form = RCImportForm(request.POST, request.FILES)
360 form = RCImportForm(request.POST, request.FILES)
363 # print("HOLA3",flush=True)
364 if form.is_valid():
361 if form.is_valid():
365 #print("HOLA4",flush=True)
366 # try:
362 # try:
367 #print("HOLA5",flush=True)
363 data = conf.import_from_file(request.FILES['file_name'])
368 data = conf.import_from_file(request.FILES['file_name'])
369 #print("HOLA6",flush=True)
370 conf.dict_to_parms(data)
364 conf.dict_to_parms(data)
371 #print("1",flush=True)
372 conf.update_pulses()
365 conf.update_pulses()
373 #print("2",flush=True)
374 messages.success(request, 'Configuration "%s" loaded succesfully' % request.FILES['file_name'])
366 messages.success(request, 'Configuration "%s" loaded succesfully' % request.FILES['file_name'])
375 return redirect(conf.get_absolute_url_edit())
367 return redirect(conf.get_absolute_url_edit())
376
368
377 # except Exception as e:
369 # except Exception as e:
378 # messages.error(request, 'Error parsing file: "%s" - %s' % (request.FILES['file_name'], repr(e)))
370 # messages.error(request, 'Error parsing file: "%s" - %s' % (request.FILES['file_name'], repr(e)))
379 else:
371 else:
380 messages.warning(request, 'Your current configuration will be replaced')
372 messages.warning(request, 'Your current configuration will be replaced')
381 form = RCImportForm()
373 form = RCImportForm()
382
374
383 kwargs = {}
375 kwargs = {}
384 kwargs['form'] = form
376 kwargs['form'] = form
385 kwargs['title'] = 'RC Configuration'
377 kwargs['title'] = 'RC Configuration'
386 kwargs['suptitle'] = 'Import file'
378 kwargs['suptitle'] = 'Import file'
387 kwargs['button'] = 'Upload'
379 kwargs['button'] = 'Upload'
388 kwargs['previous'] = conf.get_absolute_url()
380 kwargs['previous'] = conf.get_absolute_url()
389
381
390 return render(request, 'rc_import.html', kwargs)
382 return render(request, 'rc_import.html', kwargs)
391
383
392
384
393 def plot_pulses(request, conf_id):
385 def plot_pulses(request, conf_id):
394
386
395 conf = get_object_or_404(RCConfiguration, pk=conf_id)
387 conf = get_object_or_404(RCConfiguration, pk=conf_id)
396 km = True if 'km' in request.GET else False
388 km = True if 'km' in request.GET else False
397
389
398 script, div = conf.plot_pulses(km=km)
390 script, div = conf.plot_pulses(km=km)
399
391
400 kwargs = {}
392 kwargs = {}
401 kwargs['no_sidebar'] = True
393 kwargs['no_sidebar'] = True
402 kwargs['title'] = 'RC Pulses'
394 kwargs['title'] = 'RC Pulses'
403 kwargs['suptitle'] = conf.name
395 kwargs['suptitle'] = conf.name
404 kwargs['div'] = mark_safe(div)
396 kwargs['div'] = mark_safe(div)
405 kwargs['script'] = mark_safe(script)
397 kwargs['script'] = mark_safe(script)
406 kwargs['units'] = conf.km2unit
398 kwargs['units'] = conf.km2unit
407 kwargs['kms'] = 1/conf.km2unit
399 kwargs['kms'] = 1/conf.km2unit
408
400
409 if km:
401 if km:
410 kwargs['km_selected'] = True
402 kwargs['km_selected'] = True
411
403
412 if 'json' in request.GET:
404 if 'json' in request.GET:
413 return HttpResponse(json.dumps({'div':mark_safe(div), 'script':mark_safe(script)}), content_type="application/json")
405 return HttpResponse(json.dumps({'div':mark_safe(div), 'script':mark_safe(script)}), content_type="application/json")
414 else:
406 else:
415 return render(request, 'rc_pulses.html', kwargs)
407 return render(request, 'rc_pulses.html', kwargs)
416
408
417 def plot_pulses2(request, conf_id):
409 def plot_pulses2(request, conf_id):
418
410
419 conf = get_object_or_404(RCConfiguration, pk=conf_id)
411 conf = get_object_or_404(RCConfiguration, pk=conf_id)
420 km = True if 'km' in request.GET else False
412 km = True if 'km' in request.GET else False
421
413
422 script, div = conf.plot_pulses2(km=km)
414 script, div = conf.plot_pulses2(km=km)
423
415
424 kwargs = {}
416 kwargs = {}
425 kwargs['no_sidebar'] = True
417 kwargs['no_sidebar'] = True
426 kwargs['title'] = 'RC Pulses'
418 kwargs['title'] = 'RC Pulses'
427 kwargs['suptitle'] = conf.name
419 kwargs['suptitle'] = conf.name
428 kwargs['div'] = mark_safe(div)
420 kwargs['div'] = mark_safe(div)
429 kwargs['script'] = mark_safe(script)
421 kwargs['script'] = mark_safe(script)
430 kwargs['units'] = conf.km2unit
422 kwargs['units'] = conf.km2unit
431 kwargs['kms'] = 1/conf.km2unit
423 kwargs['kms'] = 1/conf.km2unit
432
424
433 if km:
425 if km:
434 kwargs['km_selected'] = True
426 kwargs['km_selected'] = True
435
427
436 if 'json' in request.GET:
428 if 'json' in request.GET:
437 return HttpResponse(json.dumps({'div':mark_safe(div), 'script':mark_safe(script)}), content_type="application/json")
429 return HttpResponse(json.dumps({'div':mark_safe(div), 'script':mark_safe(script)}), content_type="application/json")
438 else:
430 else:
439 return render(request, 'rc_pulses.html', kwargs)
431 return render(request, 'rc_pulses.html', kwargs)
440
432
441 def conf_raw(request, conf_id):
433 def conf_raw(request, conf_id):
442 conf = get_object_or_404(RCConfiguration, pk=conf_id)
434 conf = get_object_or_404(RCConfiguration, pk=conf_id)
443 raw = conf.write_device(raw=True)
435 raw = conf.write_device(raw=True)
444 return HttpResponse(raw, content_type='application/json') No newline at end of file
436 return HttpResponse(raw, content_type='application/json')
@@ -1,74 +1,74
1 version: '3'
1 version: '3'
2 services:
2 services:
3 # Django app
3 # Django app
4 radarsys:
4 radarsys:
5 container_name: 'radarsys'
5 container_name: 'radarsys'
6 build: .
6 build: .
7 restart: always
7 restart: always
8 image: radarsys
8 image: radarsys
9 ports:
9 ports:
10 - 8000:8000
10 - 8000:8000
11 env_file: .env
11 env_file: .env
12 links:
12 links:
13 - radarsys-postgres
13 - radarsys-postgres
14 volumes:
14 volumes:
15 - './:/radarsys'
15 - './:/radarsys'
16 - '${DOCKER_DATA}/static:/radarsys/static'
16 - '${DOCKER_DATA}/static:/radarsys/static'
17 labels:
17 labels:
18 ofelia.enabled: "true"
18 ofelia.enabled: "true"
19 # ofelia.job-exec.restart-reception.schedule: "0 1/5 * * * *"
19 # ofelia.job-exec.restart-reception.schedule: "0 1/5 * * * *"
20 ofelia.job-exec.test.schedule: "@every 5s"
20 ofelia.job-exec.test.schedule: "@every 5s"
21 ofelia.job-exec.test.command: "python manage.py hello"
21 ofelia.job-exec.test.command: "python manage.py scheduler"
22 # ofelia.job-exec.restart-pedestal.schedule: "0 2/10 * * * *"
22 # ofelia.job-exec.restart-pedestal.schedule: "0 2/10 * * * *"
23 # ofelia.job-exec.restart-pedestal.command: "python manage.py restart_pedestal"
23 # ofelia.job-exec.restart-pedestal.command: "python manage.py restart_pedestal"
24 # ofelia.job-exec.restart-experiment.schedule: "@midnight"
24 # ofelia.job-exec.restart-experiment.schedule: "@midnight"
25 # ofelia.job-exec.restart-experiment.command: "python manage.py restart_experiment"
25 # ofelia.job-exec.restart-experiment.command: "python manage.py restart_experiment"
26 depends_on:
26 depends_on:
27 - radarsys-postgres
27 - radarsys-postgres
28
28
29 # PostgreSQL
29 # PostgreSQL
30 radarsys-postgres:
30 radarsys-postgres:
31 container_name: 'radarsys-postgres'
31 container_name: 'radarsys-postgres'
32 build: ./postgres/
32 build: ./postgres/
33 volumes:
33 volumes:
34 - ./postgres/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
34 - ./postgres/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
35 - pgdata:/var/lib/postgresql/data
35 - pgdata:/var/lib/postgresql/data
36 ports:
36 ports:
37 - 5432:5432
37 - 5432:5432
38 expose:
38 expose:
39 - 5432
39 - 5432
40 env_file: .env
40 env_file: .env
41
41
42 #Web Server
42 #Web Server
43 radarsys-nginx:
43 radarsys-nginx:
44 container_name: 'radarsys-nginx'
44 container_name: 'radarsys-nginx'
45 restart: always
45 restart: always
46 build: ./nginx/
46 build: ./nginx/
47 ports:
47 ports:
48 - '0.0.0.0:8086:80'
48 - '0.0.0.0:80:80'
49 volumes_from:
49 volumes_from:
50 - radarsys
50 - radarsys
51 links:
51 links:
52 - radarsys:radarsys
52 - radarsys:radarsys
53 depends_on:
53 depends_on:
54 - radarsys
54 - radarsys
55
55
56 #Ofelia Scheduler
56 #Ofelia Scheduler
57 radarsys-job:
57 radarsys-job:
58 container_name: 'radarsys-job'
58 container_name: 'radarsys-job'
59 image: mcuadros/ofelia:v0.3.6
59 image: mcuadros/ofelia:v0.3.6
60 depends_on:
60 depends_on:
61 - radarsys
61 - radarsys
62 # networks:
62 # networks:
63 # - backend_sirm
63 # - backend_sirm
64 command: daemon --docker
64 command: daemon --docker
65 volumes:
65 volumes:
66 - /var/run/docker.sock:/var/run/docker.sock:ro
66 - /var/run/docker.sock:/var/run/docker.sock:ro
67 logging:
67 logging:
68 driver: "json-file"
68 driver: "json-file"
69 options:
69 options:
70 max-size: "12m"
70 max-size: "12m"
71
71
72 volumes:
72 volumes:
73 pgdata:
73 pgdata:
74 driver: local
74 driver: local
@@ -1,157 +1,158
1 """
1 """
2 Django settings for radarsys project.
2 Django settings for radarsys project.
3
3
4 Generated by 'django-admin startproject' using Django 1.8.6.
4 Generated by 'django-admin startproject' using Django 1.8.6.
5
5
6 For more information on this file, see
6 For more information on this file, see
7 https://docs.djangoproject.com/en/1.8/topics/settings/
7 https://docs.djangoproject.com/en/1.8/topics/settings/
8
8
9 For the full list of settings and their values, see
9 For the full list of settings and their values, see
10 https://docs.djangoproject.com/en/1.8/ref/settings/
10 https://docs.djangoproject.com/en/1.8/ref/settings/
11 """
11 """
12
12
13 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
13 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
14 import os
14 import os
15
15
16 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
16 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17
17
18 # Quick-start development settings - unsuitable for production
18 # Quick-start development settings - unsuitable for production
19 # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
19 # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
20
20
21 # SECURITY WARNING: keep the secret key used in production secret!
21 # SECURITY WARNING: keep the secret key used in production secret!
22 SECRET_KEY = 'xshb$k5fc-+j16)cvyffj&9u__0q3$l!hieh#+tbzqg)*f^km0'
22 SECRET_KEY = 'xshb$k5fc-+j16)cvyffj&9u__0q3$l!hieh#+tbzqg)*f^km0'
23
23
24 # SECURITY WARNING: don't run with debug turned on in production!
24 # SECURITY WARNING: don't run with debug turned on in production!
25 DEBUG = True
25 DEBUG = True
26
26
27 ALLOWED_HOSTS = ['*']
27 ALLOWED_HOSTS = ['*']
28
28
29 CSRF_TRUSTED_ORIGINS=[
29 CSRF_TRUSTED_ORIGINS=[
30 "http://*.localhost:8030",
30 "http://*.localhost:8030",
31 "http://localhost:8030",
31 "http://localhost:8030",
32 "http://127.0.0.1:8030",
32 "http://127.0.0.1:8030",
33 "http://*.localhost:8086",
33 "http://*.localhost:8086",
34 "http://0.0.0.0:8086",
34 "http://localhost:8086",
35 "http://localhost:8086",
35 "http://127.0.0.1:8086"
36 "http://127.0.0.1:8086"
36 ]
37 ]
37 #Si se requiere que la aplicación salga de este entorno, para otros usuarios es necesario hacer una API request https://fractalideas.com/blog/making-react-and-django-play-well-together-single-page-app-model/
38 #Si se requiere que la aplicación salga de este entorno, para otros usuarios es necesario hacer una API request https://fractalideas.com/blog/making-react-and-django-play-well-together-single-page-app-model/
38
39
39 # Application definition
40 # Application definition
40
41
41 INSTALLED_APPS = [
42 INSTALLED_APPS = [
42 'django.contrib.admin',
43 'django.contrib.admin',
43 'django.contrib.auth',
44 'django.contrib.auth',
44 'django.contrib.contenttypes',
45 'django.contrib.contenttypes',
45 'django.contrib.sessions',
46 'django.contrib.sessions',
46 'django.contrib.messages',
47 'django.contrib.messages',
47 'django.contrib.staticfiles',
48 'django.contrib.staticfiles',
48 'apps.accounts',
49 'apps.accounts',
49 'apps.main',
50 'apps.main',
50 'apps.misc',
51 'apps.misc',
51 'apps.rc',
52 'apps.rc',
52 'apps.dds',
53 'apps.dds',
53 'apps.jars',
54 'apps.jars',
54 'apps.usrp',
55 'apps.usrp',
55 'apps.abs',
56 'apps.abs',
56 'apps.cgs',
57 'apps.cgs',
57 'apps.dds_rest',
58 'apps.dds_rest',
58 'apps.atrad',
59 'apps.atrad',
59 "django_bootstrap5",
60 "django_bootstrap5",
60 'polymorphic',
61 'polymorphic',
61 'radarsys',
62 'radarsys',
62 ]
63 ]
63
64
64 MIDDLEWARE = [
65 MIDDLEWARE = [
65 'django.middleware.security.SecurityMiddleware',
66 'django.middleware.security.SecurityMiddleware',
66 'django.contrib.sessions.middleware.SessionMiddleware',
67 'django.contrib.sessions.middleware.SessionMiddleware',
67 'django.middleware.common.CommonMiddleware',
68 'django.middleware.common.CommonMiddleware',
68 'django.middleware.csrf.CsrfViewMiddleware',
69 'django.middleware.csrf.CsrfViewMiddleware',
69 'django.contrib.auth.middleware.AuthenticationMiddleware',
70 'django.contrib.auth.middleware.AuthenticationMiddleware',
70 'django.contrib.messages.middleware.MessageMiddleware',
71 'django.contrib.messages.middleware.MessageMiddleware',
71 'django.middleware.clickjacking.XFrameOptionsMiddleware',
72 'django.middleware.clickjacking.XFrameOptionsMiddleware',
72
73
73 ]
74 ]
74
75
75 ROOT_URLCONF = 'radarsys.urls'
76 ROOT_URLCONF = 'radarsys.urls'
76
77
77 TEMPLATES = [
78 TEMPLATES = [
78 {
79 {
79 'BACKEND': 'django.template.backends.django.DjangoTemplates',
80 'BACKEND': 'django.template.backends.django.DjangoTemplates',
80 'DIRS': [os.path.join(BASE_DIR, "templates")],
81 'DIRS': [os.path.join(BASE_DIR, "templates")],
81 'APP_DIRS': True,
82 'APP_DIRS': True,
82 'OPTIONS': {
83 'OPTIONS': {
83 'context_processors': [
84 'context_processors': [
84 'django.template.context_processors.debug',
85 'django.template.context_processors.debug',
85 'django.template.context_processors.request',
86 'django.template.context_processors.request',
86 'django.contrib.auth.context_processors.auth',
87 'django.contrib.auth.context_processors.auth',
87 'django.contrib.messages.context_processors.messages',
88 'django.contrib.messages.context_processors.messages',
88 'apps.main.processors.radarsys_globals',
89 'apps.main.processors.radarsys_globals',
89 ],
90 ],
90 },
91 },
91 },
92 },
92 ]
93 ]
93
94
94 WSGI_APPLICATION = 'radarsys.wsgi.application'
95 WSGI_APPLICATION = 'radarsys.wsgi.application'
95 ASGI_APPLICATION = 'radarsys.asgi.application'
96 ASGI_APPLICATION = 'radarsys.asgi.application'
96
97
97 # Database
98 # Database
98 # https://docs.djangoproject.com/en/1.8/ref/settings/#databases
99 # https://docs.djangoproject.com/en/1.8/ref/settings/#databases
99
100
100 DATABASES = {
101 DATABASES = {
101 # 'default': {
102 # 'default': {
102 # 'ENGINE': 'django.db.backends.sqlite3',
103 # 'ENGINE': 'django.db.backends.sqlite3',
103 # 'NAME': 'radarsys.sqlite',
104 # 'NAME': 'radarsys.sqlite',
104 # }
105 # }
105 'default': {
106 'default': {
106 'ENGINE': 'django.db.backends.postgresql_psycopg2',
107 'ENGINE': 'django.db.backends.postgresql_psycopg2',
107 'NAME': os.environ.get('DB_NAME', 'radarsys'),
108 'NAME': os.environ.get('DB_NAME', 'radarsys'),
108 'USER': os.environ.get('DB_USER', 'docker'),
109 'USER': os.environ.get('DB_USER', 'docker'),
109 'PASSWORD': os.environ.get('DB_PASSWORD', 'docker'),
110 'PASSWORD': os.environ.get('DB_PASSWORD', 'docker'),
110 'HOST': os.environ.get('POSTGRES_PORT_5432_TCP_ADDR', 'localhost'),
111 'HOST': os.environ.get('POSTGRES_PORT_5432_TCP_ADDR', 'localhost'),
111 'PORT': os.environ.get('POSTGRES_PORT_5432_TCP_PORT', '5432'),
112 'PORT': os.environ.get('POSTGRES_PORT_5432_TCP_PORT', '5432'),
112 }
113 }
113 }
114 }
114
115
115 # Internationalization
116 # Internationalization
116 # https://docs.djangoproject.com/en/1.8/topics/i18n/
117 # https://docs.djangoproject.com/en/1.8/topics/i18n/
117
118
118 LANGUAGE_CODE = 'en-us'
119 LANGUAGE_CODE = 'en-us'
119
120
120 USE_TZ = False #True
121 USE_TZ = False #True
121
122
122 TIME_ZONE = os.environ.get('TZ', 'America/Lima')
123 TIME_ZONE = os.environ.get('TZ', 'America/Lima')
123
124
124 USE_I18N = True
125 USE_I18N = True
125
126
126 USE_L10N = True
127 USE_L10N = True
127
128
128 # Static files (CSS, JavaScript, Images)
129 # Static files (CSS, JavaScript, Images)
129 # https://docs.djangoproject.com/en/1.8/howto/static-files/
130 # https://docs.djangoproject.com/en/1.8/howto/static-files/
130
131
131 MEDIA_URL = '/media/'
132 MEDIA_URL = '/media/'
132 MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
133 MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
133
134
134 STATICFILES_DIRS = [
135 STATICFILES_DIRS = [
135 os.path.join(BASE_DIR, 'radarsys/static/')
136 os.path.join(BASE_DIR, 'radarsys/static/')
136 ]
137 ]
137
138
138 STATIC_URL = '/static/'
139 STATIC_URL = '/static/'
139 STATIC_ROOT = os.path.join(BASE_DIR, 'static')
140 STATIC_ROOT = os.path.join(BASE_DIR, 'static')
140
141
141 LOGIN_URL = '/accounts/login'
142 LOGIN_URL = '/accounts/login'
142 LOGOUT_URL = 'logout'
143 LOGOUT_URL = 'logout'
143 LOGIN_REDIRECT_URL = '/admin'
144 LOGIN_REDIRECT_URL = '/admin'
144 LOGOUT_REDIRECT_URL = '/'
145 LOGOUT_REDIRECT_URL = '/'
145
146
146 STATICFILES_FINDERS = (
147 STATICFILES_FINDERS = (
147 'django.contrib.staticfiles.finders.FileSystemFinder',
148 'django.contrib.staticfiles.finders.FileSystemFinder',
148 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
149 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
149 )
150 )
150
151
151
152
152 import django
153 import django
153 from django.utils.encoding import force_str
154 from django.utils.encoding import force_str
154 django.utils.encoding.force_text = force_str
155 django.utils.encoding.force_text = force_str
155
156
156 # choose of auto-created primary keys
157 # choose of auto-created primary keys
157 DEFAULT_AUTO_FIELD='django.db.models.AutoField'
158 DEFAULT_AUTO_FIELD='django.db.models.AutoField'
1 NO CONTENT: file renamed from radarsys/static/js/atrad-conf.js to radarsys/static/js/atrad-mainpage.js
NO CONTENT: file renamed from radarsys/static/js/atrad-conf.js to radarsys/static/js/atrad-mainpage.js
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
1 NO CONTENT: modified file chmod 100644 => 100755
NO CONTENT: modified file chmod 100644 => 100755
General Comments 0
You need to be logged in to leave comments. Login now