@@ -1,33 +1,33 | |||||
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 |
|
|
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=atrad |
|
26 | MQTT_USER_ATRAD=atrad | |
27 | MQTT_PASSWORD_ATRAD = atrad |
|
27 | MQTT_PASSWORD_ATRAD = atrad | |
28 | MQTT_USER = abs |
|
28 | MQTT_USER = abs | |
29 | MQTT_PASSWORD = abs |
|
29 | MQTT_PASSWORD = abs | |
30 | MQTT_CLIENT_ID= abs_id |
|
30 | MQTT_CLIENT_ID= abs_id | |
31 | TOPIC_ABS=abs/beams |
|
31 | TOPIC_ABS=abs/beams | |
32 | TOPIC_ABS_ACK=abs/beams_ack |
|
32 | TOPIC_ABS_ACK=abs/beams_ack | |
33 | TOPIC_ABS_CHANGE=abs/change_beam |
|
33 | TOPIC_ABS_CHANGE=abs/change_beam |
@@ -1,1038 +1,1082 | |||||
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 | self.message = 'ABS running' |
|
342 | self.message = 'ABS running' | |
343 |
|
343 | |||
344 | except Exception as e: |
|
344 | except Exception as e: | |
345 | self.message = str(e) |
|
345 | self.message = str(e) | |
346 | return False |
|
346 | return False | |
347 |
|
347 | |||
348 | return True |
|
348 | return True | |
349 |
|
349 | |||
350 | else: |
|
350 | else: | |
351 | self.message = 'Please, select Write ABS Device first.' |
|
351 | self.message = 'Please, select Write ABS Device first.' | |
352 | return False |
|
352 | return False | |
353 |
|
353 | |||
354 |
|
354 | |||
355 | def stop_device(self): |
|
355 | def stop_device(self): | |
356 |
|
356 | |||
357 | self.device.status = 2 |
|
357 | self.device.status = 2 | |
358 | self.device.save() |
|
358 | self.device.save() | |
359 | self.message = 'ABS has been stopped.' |
|
359 | self.message = 'ABS has been stopped.' | |
360 | self.save() |
|
360 | self.save() | |
361 |
|
361 | |||
362 | return True |
|
362 | return True | |
363 |
|
363 | |||
|
364 | def stop_device_mqtt(self): | |||
|
365 | ||||
|
366 | self.device.status = 2 | |||
|
367 | self.device.save() | |||
|
368 | self.message = 'ABS has been stopped.' | |||
|
369 | self.save() | |||
|
370 | ||||
|
371 | mqtt_client.publish(os.environ.get('TOPIC_ABS', 'abs/beams'),"STOP") | |||
|
372 | ||||
|
373 | return True | |||
364 |
|
374 | |||
365 | def write_device(self): |
|
375 | def write_device(self): | |
366 |
|
376 | |||
367 | """ |
|
377 | """ | |
368 | This function sends the beams list to every abs module. |
|
378 | This function sends the beams list to every abs module. | |
369 | It needs 'module_conf' function |
|
379 | It needs 'module_conf' function | |
370 | """ |
|
380 | """ | |
371 | print("Write 3") |
|
381 | print("Write 3") | |
372 |
|
382 | |||
373 | beams = ABSBeam.objects.filter(abs_conf=self) |
|
383 | beams = ABSBeam.objects.filter(abs_conf=self) | |
374 | nbeams = len(beams) |
|
384 | nbeams = len(beams) | |
375 |
|
385 | |||
376 | # Se manda a cero RC para poder realizar cambio de beam |
|
386 | # Se manda a cero RC para poder realizar cambio de beam | |
377 | if self.experiment is None: |
|
387 | if self.experiment is None: | |
378 | confs = [] |
|
388 | confs = [] | |
379 | else: |
|
389 | else: | |
380 | confs = Configuration.objects.filter(experiment = self.experiment).filter(type=0) |
|
390 | confs = Configuration.objects.filter(experiment = self.experiment).filter(type=0) | |
381 | confdds = '' |
|
391 | confdds = '' | |
382 | confjars = '' |
|
392 | confjars = '' | |
383 | confrc = '' |
|
393 | confrc = '' | |
384 | #TO STOP DEVICES: DDS-JARS-RC |
|
394 | #TO STOP DEVICES: DDS-JARS-RC | |
385 | for i in range(0,len(confs)): |
|
395 | for i in range(0,len(confs)): | |
386 | if i==0: |
|
396 | if i==0: | |
387 | for conf in confs: |
|
397 | for conf in confs: | |
388 | if conf.device.device_type.name == 'dds': |
|
398 | if conf.device.device_type.name == 'dds': | |
389 | confdds = conf |
|
399 | confdds = conf | |
390 | confdds.stop_device() |
|
400 | confdds.stop_device() | |
391 | break |
|
401 | break | |
392 | if i==1: |
|
402 | if i==1: | |
393 | for conf in confs: |
|
403 | for conf in confs: | |
394 | if conf.device.device_type.name == 'jars': |
|
404 | if conf.device.device_type.name == 'jars': | |
395 | confjars = conf |
|
405 | confjars = conf | |
396 | confjars.stop_device() |
|
406 | confjars.stop_device() | |
397 | break |
|
407 | break | |
398 | if i==2: |
|
408 | if i==2: | |
399 | for conf in confs: |
|
409 | for conf in confs: | |
400 | if conf.device.device_type.name == 'rc': |
|
410 | if conf.device.device_type.name == 'rc': | |
401 | confrc = conf |
|
411 | confrc = conf | |
402 | confrc.stop_device() |
|
412 | confrc.stop_device() | |
403 | break |
|
413 | break | |
404 |
|
414 | |||
405 | ''' |
|
415 | ''' | |
406 | if self.connected_modules() == 0 : |
|
416 | if self.connected_modules() == 0 : | |
407 | print("No encuentra modulos") |
|
417 | print("No encuentra modulos") | |
408 | self.message = "No ABS Module detected." |
|
418 | self.message = "No ABS Module detected." | |
409 | return False |
|
419 | return False | |
410 | ''' |
|
420 | ''' | |
411 | #-------------Write each abs module----------- |
|
421 | #-------------Write each abs module----------- | |
412 |
|
422 | |||
413 | if beams: |
|
423 | if beams: | |
414 | block_id = 0 |
|
424 | block_id = 0 | |
415 | message = 'SNDF{:03d}{:02d}{:02d}'.format(nbeams, nbeams, block_id) |
|
425 | message = 'SNDF{:03d}{:02d}{:02d}'.format(nbeams, nbeams, block_id) | |
416 | for i, status in enumerate(self.module_status): |
|
426 | for i, status in enumerate(self.module_status): | |
417 | message += ''.join([fromBinary2Char(beam.module_6bits(i)) for beam in beams]) |
|
427 | message += ''.join([fromBinary2Char(beam.module_6bits(i)) for beam in beams]) | |
418 | status = ['0'] * 64 |
|
428 | status = ['0'] * 64 | |
419 | n = 0 |
|
429 | n = 0 | |
420 |
|
430 | |||
421 | print("Llega una antes entrar a multicast4") |
|
431 | print("Llega una antes entrar a multicast4") | |
422 |
|
432 | |||
423 | sock = self.send_multicast(message) |
|
433 | sock = self.send_multicast(message) | |
424 |
|
434 | |||
425 | while True: |
|
435 | while True: | |
426 | #for i in range(32): |
|
436 | #for i in range(32): | |
427 | try: |
|
437 | try: | |
428 | data, address = sock.recvfrom(1024) |
|
438 | data, address = sock.recvfrom(1024) | |
429 | print (address, data) |
|
439 | print (address, data) | |
430 | data = data.decode("utf-8") |
|
440 | data = data.decode("utf-8") | |
431 | if data == '1': |
|
441 | if data == '1': | |
432 | status[int(address[0][10:])-1] = '3' |
|
442 | status[int(address[0][10:])-1] = '3' | |
433 | #print (int(address[0][10:])-1) |
|
443 | #print (int(address[0][10:])-1) | |
434 | elif data == '0': |
|
444 | elif data == '0': | |
435 | status[int(address[0][10:])-1] = '1' |
|
445 | status[int(address[0][10:])-1] = '1' | |
436 | except socket.timeout: |
|
446 | except socket.timeout: | |
437 | print('Timeout') |
|
447 | print('Timeout') | |
438 | break |
|
448 | break | |
439 | except Exception as e: |
|
449 | except Exception as e: | |
440 | print ('Error {}'.format(e)) |
|
450 | print ('Error {}'.format(e)) | |
441 | n += 1 |
|
451 | n += 1 | |
442 | sock.close() |
|
452 | sock.close() | |
443 | else: |
|
453 | else: | |
444 | self.message = "ABS Configuration does not have beams" |
|
454 | self.message = "ABS Configuration does not have beams" | |
445 | print('No beams') |
|
455 | print('No beams') | |
446 | #Start DDS-RC-JARS |
|
456 | #Start DDS-RC-JARS | |
447 | if confdds: |
|
457 | if confdds: | |
448 | confdds.start_device() |
|
458 | confdds.start_device() | |
449 | if confrc: |
|
459 | if confrc: | |
450 | #print confrc |
|
460 | #print confrc | |
451 | confrc.start_device() |
|
461 | confrc.start_device() | |
452 | if confjars: |
|
462 | if confjars: | |
453 | confjars.start_device() |
|
463 | confjars.start_device() | |
454 | return False |
|
464 | return False | |
455 |
|
465 | |||
456 | if n == 64: |
|
466 | if n == 64: | |
457 | self.message = "Could not write ABS Modules" |
|
467 | self.message = "Could not write ABS Modules" | |
458 | self.device.status = 0 |
|
468 | self.device.status = 0 | |
459 | self.module_status = ''.join(status) |
|
469 | self.module_status = ''.join(status) | |
460 | self.save() |
|
470 | self.save() | |
461 | print('Could not write ABS') |
|
471 | print('Could not write ABS') | |
462 | #Start DDS-RC-JARS |
|
472 | #Start DDS-RC-JARS | |
463 | if confdds: |
|
473 | if confdds: | |
464 | confdds.start_device() |
|
474 | confdds.start_device() | |
465 | if confrc: |
|
475 | if confrc: | |
466 | #print confrc |
|
476 | #print confrc | |
467 | confrc.start_device() |
|
477 | confrc.start_device() | |
468 | if confjars: |
|
478 | if confjars: | |
469 | confjars.start_device() |
|
479 | confjars.start_device() | |
470 | return False |
|
480 | return False | |
471 | else: |
|
481 | else: | |
472 | self.message = "ABS Beams List have been sent to ABS Modules" |
|
482 | self.message = "ABS Beams List have been sent to ABS Modules" | |
473 | print('ABS beams list sent') |
|
483 | print('ABS beams list sent') | |
474 | self.active_beam = beams[0].pk |
|
484 | self.active_beam = beams[0].pk | |
475 |
|
485 | |||
476 | #Start DDS-RC-JARS |
|
486 | #Start DDS-RC-JARS | |
477 | if confdds: |
|
487 | if confdds: | |
478 | confdds.start_device() |
|
488 | confdds.start_device() | |
479 | if confrc: |
|
489 | if confrc: | |
480 | #print confrc |
|
490 | #print confrc | |
481 | confrc.start_device() |
|
491 | confrc.start_device() | |
482 | if confjars: |
|
492 | if confjars: | |
483 | confjars.start_device() |
|
493 | confjars.start_device() | |
484 |
|
494 | |||
485 | print('Inicia intento de salvar device.status') |
|
495 | print('Inicia intento de salvar device.status') | |
486 | self.device.status = 3 |
|
496 | self.device.status = 3 | |
487 | self.module_status = ''.join(status) |
|
497 | self.module_status = ''.join(status) | |
488 | #print(status) |
|
498 | #print(status) | |
489 | self.save() |
|
499 | self.save() | |
490 | print('Estatus salvado') |
|
500 | print('Estatus salvado') | |
491 | conf_active, __ = ABSActive.objects.get_or_create(pk=1) |
|
501 | conf_active, __ = ABSActive.objects.get_or_create(pk=1) | |
492 | conf_active.conf = self |
|
502 | conf_active.conf = self | |
493 | conf_active.save() |
|
503 | conf_active.save() | |
494 | return True |
|
504 | return True | |
495 |
|
505 | |||
496 | def write_device_mqtt(self): |
|
506 | def write_device_mqtt(self): | |
|
507 | ||||
|
508 | if self.experiment is None: | |||
|
509 | confs = [] | |||
|
510 | else: | |||
|
511 | confs = Configuration.objects.filter(experiment = self.experiment).filter(type=0) | |||
|
512 | confdds = '' | |||
|
513 | confjars = '' | |||
|
514 | confrc = '' | |||
|
515 | #TO STOP DEVICES: DDS-JARS-RC | |||
|
516 | for i in range(0,len(confs)): | |||
|
517 | if i==0: | |||
|
518 | for conf in confs: | |||
|
519 | if conf.device.device_type.name == 'dds': | |||
|
520 | confdds = conf | |||
|
521 | confdds.stop_device() | |||
|
522 | break | |||
|
523 | if i==1: | |||
|
524 | for conf in confs: | |||
|
525 | if conf.device.device_type.name == 'jars': | |||
|
526 | confjars = conf | |||
|
527 | confjars.stop_device() | |||
|
528 | break | |||
|
529 | if i==2: | |||
|
530 | for conf in confs: | |||
|
531 | if conf.device.device_type.name == 'rc': | |||
|
532 | confrc = conf | |||
|
533 | confrc.stop_device() | |||
|
534 | break | |||
|
535 | ||||
497 | apuntes_up_down='' |
|
536 | apuntes_up_down='' | |
498 | beams = ABSBeam.objects.filter(abs_conf=self) |
|
537 | beams = ABSBeam.objects.filter(abs_conf=self) | |
499 |
|
538 | |||
500 | inicializacion="{\"beams\":[" |
|
539 | inicializacion="{\"beams\":[" | |
501 | finalizacion="]}" |
|
540 | finalizacion="]}" | |
502 |
|
541 | |||
503 | for beam in beams: |
|
542 | for beam in beams: | |
504 | beam.antenna=beam.antenna[1:] |
|
543 | beam.antenna=beam.antenna[1:] | |
505 | info="{\"id\":"+str(beam.id)+","+beam.antenna + "," |
|
544 | info="{\"id\":"+str(beam.id)+","+beam.antenna + "," | |
506 | apuntes_up_down=apuntes_up_down+info |
|
545 | apuntes_up_down=apuntes_up_down+info | |
507 |
|
546 | |||
508 | apuntes_up_down=apuntes_up_down[:len(apuntes_up_down)-1] |
|
547 | apuntes_up_down=apuntes_up_down[:len(apuntes_up_down)-1] | |
509 |
|
||||
510 | apuntes_up_down=inicializacion+ apuntes_up_down+finalizacion |
|
548 | apuntes_up_down=inicializacion+ apuntes_up_down+finalizacion | |
511 |
|
||||
512 | #print(apuntes_up_down,flush=True) |
|
|||
513 |
|
||||
514 | mqtt_client.publish(os.environ.get('TOPIC_ABS', 'abs/beams'),apuntes_up_down) |
|
549 | mqtt_client.publish(os.environ.get('TOPIC_ABS', 'abs/beams'),apuntes_up_down) | |
515 |
|
550 | |||
|
551 | #Start DDS-RC-JARS | |||
|
552 | if confdds: | |||
|
553 | confdds.start_device() | |||
|
554 | if confrc: | |||
|
555 | #print confrc | |||
|
556 | confrc.start_device() | |||
|
557 | if confjars: | |||
|
558 | confjars.start_device() | |||
|
559 | ||||
516 | return True |
|
560 | return True | |
517 |
|
561 | |||
518 | def read_module(self, module): |
|
562 | def read_module(self, module): | |
519 |
|
563 | |||
520 | """ |
|
564 | """ | |
521 | Read out-bits (up-down) of 1 abs module NOT for Configuration |
|
565 | Read out-bits (up-down) of 1 abs module NOT for Configuration | |
522 | """ |
|
566 | """ | |
523 |
|
567 | |||
524 | ip_address = self.device.ip_address |
|
568 | ip_address = self.device.ip_address | |
525 | ip_address = ip_address.split('.') |
|
569 | ip_address = ip_address.split('.') | |
526 | module_seq = (ip_address[0],ip_address[1],ip_address[2]) |
|
570 | module_seq = (ip_address[0],ip_address[1],ip_address[2]) | |
527 | dot = '.' |
|
571 | dot = '.' | |
528 | module_ip = dot.join(module_seq)+'.'+str(module) |
|
572 | module_ip = dot.join(module_seq)+'.'+str(module) | |
529 | module_port = self.device.port_address |
|
573 | module_port = self.device.port_address | |
530 | read_route = 'http://'+module_ip+':'+str(module_port)+'/read' |
|
574 | read_route = 'http://'+module_ip+':'+str(module_port)+'/read' | |
531 |
|
575 | |||
532 | module_status = json.loads(self.module_status) |
|
576 | module_status = json.loads(self.module_status) | |
533 | print(read_route) |
|
577 | print(read_route) | |
534 |
|
578 | |||
535 | module_bits = '' |
|
579 | module_bits = '' | |
536 |
|
580 | |||
537 | try: |
|
581 | try: | |
538 | r_read = requests.get(read_route, timeout=0.5) |
|
582 | r_read = requests.get(read_route, timeout=0.5) | |
539 | answer = r_read.json() |
|
583 | answer = r_read.json() | |
540 | module_bits = answer['allbits'] |
|
584 | module_bits = answer['allbits'] | |
541 | except: |
|
585 | except: | |
542 | return {} |
|
586 | return {} | |
543 |
|
587 | |||
544 | return module_bits |
|
588 | return module_bits | |
545 |
|
589 | |||
546 | def read_device(self): |
|
590 | def read_device(self): | |
547 |
|
591 | |||
548 | parms = {} |
|
592 | parms = {} | |
549 | # Reads active modules. |
|
593 | # Reads active modules. | |
550 | module_status = json.loads(self.module_status) |
|
594 | module_status = json.loads(self.module_status) | |
551 | total = 0 |
|
595 | total = 0 | |
552 | for status in module_status: |
|
596 | for status in module_status: | |
553 | if module_status[status] != 0: |
|
597 | if module_status[status] != 0: | |
554 | module_bits = self.read_module(int(status)) |
|
598 | module_bits = self.read_module(int(status)) | |
555 | bits={} |
|
599 | bits={} | |
556 | if module_bits: |
|
600 | if module_bits: | |
557 | bits = (str(module_bits['um2']) + str(module_bits['um1']) + str(module_bits['um0']) + |
|
601 | bits = (str(module_bits['um2']) + str(module_bits['um1']) + str(module_bits['um0']) + | |
558 | str(module_bits['dm2']) + str(module_bits['dm1']) + str(module_bits['dm0']) ) |
|
602 | str(module_bits['dm2']) + str(module_bits['dm1']) + str(module_bits['dm0']) ) | |
559 | parms[str(status)] = bits |
|
603 | parms[str(status)] = bits | |
560 |
|
604 | |||
561 | total +=1 |
|
605 | total +=1 | |
562 |
|
606 | |||
563 | if total==0: |
|
607 | if total==0: | |
564 | self.message = "No ABS Module detected. Please select 'Status'." |
|
608 | self.message = "No ABS Module detected. Please select 'Status'." | |
565 | return False |
|
609 | return False | |
566 |
|
610 | |||
567 |
|
611 | |||
568 |
|
612 | |||
569 | self.message = "ABS Modules have been read" |
|
613 | self.message = "ABS Modules have been read" | |
570 | #monitoreo_tx = JROABSClnt_01CeCnMod000000MNTR10 |
|
614 | #monitoreo_tx = JROABSClnt_01CeCnMod000000MNTR10 | |
571 | return parms |
|
615 | return parms | |
572 |
|
616 | |||
573 |
|
617 | |||
574 | def connected_modules(self): |
|
618 | def connected_modules(self): | |
575 | """ |
|
619 | """ | |
576 | This function returns the number of connected abs-modules without updating. |
|
620 | This function returns the number of connected abs-modules without updating. | |
577 | """ |
|
621 | """ | |
578 | num = 0 |
|
622 | num = 0 | |
579 | print(self.module_status) |
|
623 | print(self.module_status) | |
580 | for i, status in enumerate(self.module_status): |
|
624 | for i, status in enumerate(self.module_status): | |
581 | if status != '0': |
|
625 | if status != '0': | |
582 | num += 1 |
|
626 | num += 1 | |
583 | #print('status {}:{}'.format(i+1, status)) |
|
627 | #print('status {}:{}'.format(i+1, status)) | |
584 | return num |
|
628 | return num | |
585 |
|
629 | |||
586 | def send_multicast(self, message): |
|
630 | def send_multicast(self, message): | |
587 | #print("Send multicast") |
|
631 | #print("Send multicast") | |
588 | multicast_group = ('224.3.29.71', 10000) |
|
632 | multicast_group = ('224.3.29.71', 10000) | |
589 | # Create the datagram socket |
|
633 | # Create the datagram socket | |
590 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
|
634 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
591 | sock.settimeout(1) |
|
635 | sock.settimeout(1) | |
592 | local_ip = os.environ.get('LOCAL_IP', '0.0.0.0') |
|
636 | local_ip = os.environ.get('LOCAL_IP', '0.0.0.0') | |
593 | local_ip = '0.0.0.0' |
|
637 | local_ip = '0.0.0.0' | |
594 | print("He llegado a IP local") |
|
638 | print("He llegado a IP local") | |
595 |
|
639 | |||
596 | sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, socket.inet_aton(local_ip)) |
|
640 | sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, socket.inet_aton(local_ip)) | |
597 | sock.sendto(message.encode(), multicast_group) |
|
641 | sock.sendto(message.encode(), multicast_group) | |
598 | print('Sending ' + message) |
|
642 | print('Sending ' + message) | |
599 | return sock |
|
643 | return sock | |
600 |
|
644 | |||
601 | def status_device(self): |
|
645 | def status_device(self): | |
602 | """ |
|
646 | """ | |
603 | This function returns the status of all abs-modules as one. |
|
647 | This function returns the status of all abs-modules as one. | |
604 | If at least one module is connected, its answer is "1" |
|
648 | If at least one module is connected, its answer is "1" | |
605 | """ |
|
649 | """ | |
606 | print ('Status device') |
|
650 | print ('Status device') | |
607 | print (self.active_beam) |
|
651 | print (self.active_beam) | |
608 | beams = ABSBeam.objects.filter(abs_conf=self) |
|
652 | beams = ABSBeam.objects.filter(abs_conf=self) | |
609 | #print beams[self.active_beam-1].module_6bits(0) |
|
653 | #print beams[self.active_beam-1].module_6bits(0) | |
610 | active = ABSActive.objects.get(pk=1) |
|
654 | active = ABSActive.objects.get(pk=1) | |
611 | if active.conf != self: |
|
655 | if active.conf != self: | |
612 | self.message = 'La configuracion actual es la del siguiente enlace %s.' % active.conf.get_absolute_url() |
|
656 | self.message = 'La configuracion actual es la del siguiente enlace %s.' % active.conf.get_absolute_url() | |
613 | self.message += "\n" |
|
657 | self.message += "\n" | |
614 | self.message += 'Se debe realizar un write en esta configuracion para luego obtener un status valido.' |
|
658 | self.message += 'Se debe realizar un write en esta configuracion para luego obtener un status valido.' | |
615 |
|
659 | |||
616 | return False |
|
660 | return False | |
617 |
|
661 | |||
618 | sock = self.send_multicast('MNTR') |
|
662 | sock = self.send_multicast('MNTR') | |
619 |
|
663 | |||
620 | n = 0 |
|
664 | n = 0 | |
621 | status = ['0'] * 64 |
|
665 | status = ['0'] * 64 | |
622 |
|
666 | |||
623 | while True: |
|
667 | while True: | |
624 | #for i in range(32): |
|
668 | #for i in range(32): | |
625 | #if True: |
|
669 | #if True: | |
626 | try: |
|
670 | try: | |
627 | print("Recibiendo") |
|
671 | print("Recibiendo") | |
628 | address = None |
|
672 | address = None | |
629 | data, address = sock.recvfrom(2) |
|
673 | data, address = sock.recvfrom(2) | |
630 | print (address, data) |
|
674 | print (address, data) | |
631 | print("!!!!") |
|
675 | print("!!!!") | |
632 | data = data.decode() |
|
676 | data = data.decode() | |
633 | aux_mon = "1" |
|
677 | aux_mon = "1" | |
634 | aux_expected = aux_mon |
|
678 | aux_expected = aux_mon | |
635 | if(len(data)==2): |
|
679 | if(len(data)==2): | |
636 | print ("data[1]: ") |
|
680 | print ("data[1]: ") | |
637 | print (data[1]) |
|
681 | print (data[1]) | |
638 | aux_mon = fromChar2Binary(data[1]) |
|
682 | aux_mon = fromChar2Binary(data[1]) | |
639 | print (aux_mon) |
|
683 | print (aux_mon) | |
640 | aux_i = (str(address[0]).split('.'))[3] |
|
684 | aux_i = (str(address[0]).split('.'))[3] | |
641 | print (aux_i) |
|
685 | print (aux_i) | |
642 | print ('Active beam') |
|
686 | print ('Active beam') | |
643 | beam_active = ABSBeam.objects.get(pk=self.active_beam) |
|
687 | beam_active = ABSBeam.objects.get(pk=self.active_beam) | |
644 | print (beam_active) |
|
688 | print (beam_active) | |
645 | aux_expected = beam_active.module_6bits(int(aux_i)-1) |
|
689 | aux_expected = beam_active.module_6bits(int(aux_i)-1) | |
646 | print (aux_expected) |
|
690 | print (aux_expected) | |
647 |
|
691 | |||
648 | print ("data[0]: ") |
|
692 | print ("data[0]: ") | |
649 | print (data[0]) |
|
693 | print (data[0]) | |
650 |
|
694 | |||
651 | if data[0] == '1': |
|
695 | if data[0] == '1': | |
652 | status[int(address[0][10:])-1] = '3' |
|
696 | status[int(address[0][10:])-1] = '3' | |
653 | if aux_mon == aux_expected: |
|
697 | if aux_mon == aux_expected: | |
654 | print ('Es igual') |
|
698 | print ('Es igual') | |
655 | else: |
|
699 | else: | |
656 | print ('Es diferente') |
|
700 | print ('Es diferente') | |
657 | status[int(address[0][10:])-1] = '2' |
|
701 | status[int(address[0][10:])-1] = '2' | |
658 |
|
702 | |||
659 | elif data[0] == '0': |
|
703 | elif data[0] == '0': | |
660 | status[int(address[0][10:])-1] = '1' |
|
704 | status[int(address[0][10:])-1] = '1' | |
661 | n += 1 |
|
705 | n += 1 | |
662 | print('Module: {} connected'.format(address)) |
|
706 | print('Module: {} connected'.format(address)) | |
663 | except socket.timeout: |
|
707 | except socket.timeout: | |
664 | print('Timeout') |
|
708 | print('Timeout') | |
665 | break |
|
709 | break | |
666 | except: |
|
710 | except: | |
667 | print('Module: {} error'.format(address)) |
|
711 | print('Module: {} error'.format(address)) | |
668 | pass |
|
712 | pass | |
669 |
|
713 | |||
670 | sock.close() |
|
714 | sock.close() | |
671 |
|
715 | |||
672 | if n > 0: |
|
716 | if n > 0: | |
673 | self.message = 'ABS modules Status have been updated.' |
|
717 | self.message = 'ABS modules Status have been updated.' | |
674 | self.device.status = 1 |
|
718 | self.device.status = 1 | |
675 | else: |
|
719 | else: | |
676 | self.device.status = 0 |
|
720 | self.device.status = 0 | |
677 | self.message = 'No ABS module is connected.' |
|
721 | self.message = 'No ABS module is connected.' | |
678 | self.module_status = ''.join(status) |
|
722 | self.module_status = ''.join(status) | |
679 | self.save() |
|
723 | self.save() | |
680 |
|
724 | |||
681 | return self.device.status |
|
725 | return self.device.status | |
682 |
|
726 | |||
683 |
|
727 | |||
684 | def send_beam(self, beam_pos): |
|
728 | def send_beam(self, beam_pos): | |
685 | """ |
|
729 | """ | |
686 | This function connects to a multicast group and sends the beam number |
|
730 | This function connects to a multicast group and sends the beam number | |
687 | to all abs modules. |
|
731 | to all abs modules. | |
688 | """ |
|
732 | """ | |
689 | print ('Send beam') |
|
733 | print ('Send beam') | |
690 | print (self.active_beam) |
|
734 | print (self.active_beam) | |
691 | beams = ABSBeam.objects.filter(abs_conf=self) |
|
735 | beams = ABSBeam.objects.filter(abs_conf=self) | |
692 | #print beams[self.active_beam-1].module_6bits(0) |
|
736 | #print beams[self.active_beam-1].module_6bits(0) | |
693 | active = ABSActive.objects.get(pk=1) |
|
737 | active = ABSActive.objects.get(pk=1) | |
694 | if active.conf != self: |
|
738 | if active.conf != self: | |
695 | self.message = 'La configuracion actual es la del siguiente enlace %s.' % active.conf.get_absolute_url() |
|
739 | self.message = 'La configuracion actual es la del siguiente enlace %s.' % active.conf.get_absolute_url() | |
696 | self.message += "\n" |
|
740 | self.message += "\n" | |
697 | self.message += 'Se debe realizar un write en esta configuracion para luego obtener un status valido.' |
|
741 | self.message += 'Se debe realizar un write en esta configuracion para luego obtener un status valido.' | |
698 |
|
742 | |||
699 | return False |
|
743 | return False | |
700 |
|
744 | |||
701 | # Se manda a cero RC para poder realizar cambio de beam |
|
745 | # Se manda a cero RC para poder realizar cambio de beam | |
702 | if self.experiment is None: |
|
746 | if self.experiment is None: | |
703 | confs = [] |
|
747 | confs = [] | |
704 | else: |
|
748 | else: | |
705 | confs = Configuration.objects.filter(experiment = self.experiment).filter(type=0) |
|
749 | confs = Configuration.objects.filter(experiment = self.experiment).filter(type=0) | |
706 | confdds = '' |
|
750 | confdds = '' | |
707 | confjars = '' |
|
751 | confjars = '' | |
708 | confrc = '' |
|
752 | confrc = '' | |
709 | #TO STOP DEVICES: DDS-JARS-RC |
|
753 | #TO STOP DEVICES: DDS-JARS-RC | |
710 | for i in range(0,len(confs)): |
|
754 | for i in range(0,len(confs)): | |
711 | if i==0: |
|
755 | if i==0: | |
712 | for conf in confs: |
|
756 | for conf in confs: | |
713 | if conf.device.device_type.name == 'dds': |
|
757 | if conf.device.device_type.name == 'dds': | |
714 | confdds = conf |
|
758 | confdds = conf | |
715 | confdds.stop_device() |
|
759 | confdds.stop_device() | |
716 | break |
|
760 | break | |
717 | if i==1: |
|
761 | if i==1: | |
718 | for conf in confs: |
|
762 | for conf in confs: | |
719 | if conf.device.device_type.name == 'jars': |
|
763 | if conf.device.device_type.name == 'jars': | |
720 | confjars = conf |
|
764 | confjars = conf | |
721 | confjars.stop_device() |
|
765 | confjars.stop_device() | |
722 | break |
|
766 | break | |
723 | if i==2: |
|
767 | if i==2: | |
724 | for conf in confs: |
|
768 | for conf in confs: | |
725 | if conf.device.device_type.name == 'rc': |
|
769 | if conf.device.device_type.name == 'rc': | |
726 | confrc = conf |
|
770 | confrc = conf | |
727 | confrc.stop_device() |
|
771 | confrc.stop_device() | |
728 | break |
|
772 | break | |
729 | if beam_pos > 0: |
|
773 | if beam_pos > 0: | |
730 | beam_pos = beam_pos - 1 |
|
774 | beam_pos = beam_pos - 1 | |
731 | else: |
|
775 | else: | |
732 | beam_pos = 0 |
|
776 | beam_pos = 0 | |
733 |
|
777 | |||
734 | #El indice del apunte debe ser menor que el numero total de apuntes |
|
778 | #El indice del apunte debe ser menor que el numero total de apuntes | |
735 | #El servidor tcp en el embebido comienza a contar desde 0 |
|
779 | #El servidor tcp en el embebido comienza a contar desde 0 | |
736 | status = ['0'] * 64 |
|
780 | status = ['0'] * 64 | |
737 | message = 'CHGB{}'.format(beam_pos) |
|
781 | message = 'CHGB{}'.format(beam_pos) | |
738 | sock = self.send_multicast(message) |
|
782 | sock = self.send_multicast(message) | |
739 | while True: |
|
783 | while True: | |
740 | #for i in range(32): |
|
784 | #for i in range(32): | |
741 | try: |
|
785 | try: | |
742 | data, address = sock.recvfrom(1024) |
|
786 | data, address = sock.recvfrom(1024) | |
743 | print (address, data) |
|
787 | print (address, data) | |
744 | data = data.decode() |
|
788 | data = data.decode() | |
745 | if data == '1': |
|
789 | if data == '1': | |
746 | status[int(address[0][10:])-1] = '3' |
|
790 | status[int(address[0][10:])-1] = '3' | |
747 | elif data == '0': |
|
791 | elif data == '0': | |
748 | status[int(address[0][10:])-1] = '1' |
|
792 | status[int(address[0][10:])-1] = '1' | |
749 | except socket.timeout: |
|
793 | except socket.timeout: | |
750 | print('Timeout') |
|
794 | print('Timeout') | |
751 | break |
|
795 | break | |
752 | except Exception as e: |
|
796 | except Exception as e: | |
753 | print ('Error {}'.format(e)) |
|
797 | print ('Error {}'.format(e)) | |
754 | pass |
|
798 | pass | |
755 |
|
799 | |||
756 | sock.close() |
|
800 | sock.close() | |
757 |
|
801 | |||
758 | #Start DDS-RC-JARS |
|
802 | #Start DDS-RC-JARS | |
759 | if confdds: |
|
803 | if confdds: | |
760 | confdds.start_device() |
|
804 | confdds.start_device() | |
761 | if confrc: |
|
805 | if confrc: | |
762 | #print confrc |
|
806 | #print confrc | |
763 | confrc.start_device() |
|
807 | confrc.start_device() | |
764 | if confjars: |
|
808 | if confjars: | |
765 | confjars.start_device() |
|
809 | confjars.start_device() | |
766 |
|
810 | |||
767 | self.message = "ABS Beam has been changed" |
|
811 | self.message = "ABS Beam has been changed" | |
768 | self.module_status = ''.join(status) |
|
812 | self.module_status = ''.join(status) | |
769 | self.save() |
|
813 | self.save() | |
770 | return True |
|
814 | return True | |
771 |
|
815 | |||
772 |
|
816 | |||
773 | def get_absolute_url_import(self): |
|
817 | def get_absolute_url_import(self): | |
774 | return reverse('url_import_abs_conf', args=[str(self.id)]) |
|
818 | return reverse('url_import_abs_conf', args=[str(self.id)]) | |
775 |
|
819 | |||
776 | class ABSActive(models.Model): |
|
820 | class ABSActive(models.Model): | |
777 | conf = models.ForeignKey(ABSConfiguration, null=True, verbose_name='ABS Configuration', on_delete=models.CASCADE) |
|
821 | conf = models.ForeignKey(ABSConfiguration, null=True, verbose_name='ABS Configuration', on_delete=models.CASCADE) | |
778 |
|
822 | |||
779 | class Meta: |
|
823 | class Meta: | |
780 | db_table = 'abs_absactive' |
|
824 | db_table = 'abs_absactive' | |
781 |
|
825 | |||
782 | class ABSBeam(models.Model): |
|
826 | class ABSBeam(models.Model): | |
783 |
|
827 | |||
784 | name = models.CharField(max_length=60, default='Beam') |
|
828 | name = models.CharField(max_length=60, default='Beam') | |
785 | antenna = models.CharField(verbose_name='Antenna', max_length=1000, default=antenna_default) |
|
829 | antenna = models.CharField(verbose_name='Antenna', max_length=1000, default=antenna_default) | |
786 | abs_conf = models.ForeignKey('ABSConfiguration', null=True, |
|
830 | abs_conf = models.ForeignKey('ABSConfiguration', null=True, | |
787 | verbose_name='ABS Configuration', on_delete=models.CASCADE) |
|
831 | verbose_name='ABS Configuration', on_delete=models.CASCADE) | |
788 | tx = models.CharField(verbose_name='Tx', max_length=1000, default=tx_default) |
|
832 | tx = models.CharField(verbose_name='Tx', max_length=1000, default=tx_default) | |
789 | rx = models.CharField(verbose_name='Rx', max_length=1000, default=rx_default) |
|
833 | rx = models.CharField(verbose_name='Rx', max_length=1000, default=rx_default) | |
790 | s_time = models.TimeField(verbose_name='Star Time', default='00:00:00') |
|
834 | s_time = models.TimeField(verbose_name='Star Time', default='00:00:00') | |
791 | e_time = models.TimeField(verbose_name='End Time', default='23:59:59') |
|
835 | e_time = models.TimeField(verbose_name='End Time', default='23:59:59') | |
792 | ues = models.CharField(verbose_name='Ues', max_length=100, default=ues_default) |
|
836 | ues = models.CharField(verbose_name='Ues', max_length=100, default=ues_default) | |
793 | only_rx = models.CharField(verbose_name='Only RX', max_length=40, default=onlyrx_default) |
|
837 | only_rx = models.CharField(verbose_name='Only RX', max_length=40, default=onlyrx_default) | |
794 |
|
838 | |||
795 | class Meta: |
|
839 | class Meta: | |
796 | db_table = 'abs_beams' |
|
840 | db_table = 'abs_beams' | |
797 |
|
841 | |||
798 | def __unicode__(self): |
|
842 | def __unicode__(self): | |
799 | return u'%s' % (self.name) |
|
843 | return u'%s' % (self.name) | |
800 |
|
844 | |||
801 | def parms_to_dict(self): |
|
845 | def parms_to_dict(self): | |
802 |
|
846 | |||
803 | parameters = {} |
|
847 | parameters = {} | |
804 | parameters['name'] = self.name |
|
848 | parameters['name'] = self.name | |
805 | parameters['antenna'] = ast.literal_eval(self.antenna) |
|
849 | parameters['antenna'] = ast.literal_eval(self.antenna) | |
806 | parameters['abs_conf'] = self.abs_conf.name |
|
850 | parameters['abs_conf'] = self.abs_conf.name | |
807 | parameters['tx'] = ast.literal_eval(self.tx) |
|
851 | parameters['tx'] = ast.literal_eval(self.tx) | |
808 | parameters['rx'] = ast.literal_eval(self.rx) |
|
852 | parameters['rx'] = ast.literal_eval(self.rx) | |
809 | parameters['s_time'] = self.s_time.strftime("%H:%M:%S") |
|
853 | parameters['s_time'] = self.s_time.strftime("%H:%M:%S") | |
810 | parameters['e_time'] = self.e_time.strftime("%H:%M:%S") |
|
854 | parameters['e_time'] = self.e_time.strftime("%H:%M:%S") | |
811 | parameters['ues'] = ast.literal_eval(self.ues) |
|
855 | parameters['ues'] = ast.literal_eval(self.ues) | |
812 | parameters['only_rx'] = json.loads(self.only_rx) |
|
856 | parameters['only_rx'] = json.loads(self.only_rx) | |
813 |
|
857 | |||
814 | return parameters |
|
858 | return parameters | |
815 |
|
859 | |||
816 | def dict_to_parms(self, parameters): |
|
860 | def dict_to_parms(self, parameters): | |
817 |
|
861 | |||
818 | self.name = parameters['name'] |
|
862 | self.name = parameters['name'] | |
819 | self.antenna = json.dumps(parameters['antenna']) |
|
863 | self.antenna = json.dumps(parameters['antenna']) | |
820 | #self.abs_conf = parameters['abs_conf'] |
|
864 | #self.abs_conf = parameters['abs_conf'] | |
821 | self.tx = json.dumps(parameters['tx']) |
|
865 | self.tx = json.dumps(parameters['tx']) | |
822 | self.rx = json.dumps(parameters['rx']) |
|
866 | self.rx = json.dumps(parameters['rx']) | |
823 | #self.s_time = parameters['s_time'] |
|
867 | #self.s_time = parameters['s_time'] | |
824 | #self.e_time = parameters['e_time'] |
|
868 | #self.e_time = parameters['e_time'] | |
825 | self.ues = json.dumps(parameters['ues']) |
|
869 | self.ues = json.dumps(parameters['ues']) | |
826 | self.only_rx = json.dumps(parameters['only_rx']) |
|
870 | self.only_rx = json.dumps(parameters['only_rx']) | |
827 | self.save() |
|
871 | self.save() | |
828 |
|
872 | |||
829 |
|
873 | |||
830 | def clone(self, **kwargs): |
|
874 | def clone(self, **kwargs): | |
831 |
|
875 | |||
832 | self.pk = None |
|
876 | self.pk = None | |
833 | self.id = None |
|
877 | self.id = None | |
834 | for attr, value in kwargs.items(): |
|
878 | for attr, value in kwargs.items(): | |
835 | setattr(self, attr, value) |
|
879 | setattr(self, attr, value) | |
836 |
|
880 | |||
837 | self.save() |
|
881 | self.save() | |
838 |
|
882 | |||
839 | return self |
|
883 | return self | |
840 |
|
884 | |||
841 |
|
885 | |||
842 | def module_6bits(self, module): |
|
886 | def module_6bits(self, module): | |
843 | """ |
|
887 | """ | |
844 | This function reads antenna pattern and choose 6bits (upbits-downbits) for one abs module |
|
888 | This function reads antenna pattern and choose 6bits (upbits-downbits) for one abs module | |
845 | """ |
|
889 | """ | |
846 | module += 1 |
|
890 | module += 1 | |
847 | if module > 64: |
|
891 | if module > 64: | |
848 | beam_bits = "" |
|
892 | beam_bits = "" | |
849 | return beam_bits |
|
893 | return beam_bits | |
850 |
|
894 | |||
851 | data = ast.literal_eval(self.antenna) |
|
895 | data = ast.literal_eval(self.antenna) | |
852 | up_data = data['antenna_up'] |
|
896 | up_data = data['antenna_up'] | |
853 | down_data = data['antenna_down'] |
|
897 | down_data = data['antenna_down'] | |
854 |
|
898 | |||
855 | pos = ip2position(module) |
|
899 | pos = ip2position(module) | |
856 | up_value = up_data[pos[0]][pos[1]] |
|
900 | up_value = up_data[pos[0]][pos[1]] | |
857 | down_value = down_data[pos[0]][pos[1]] |
|
901 | down_value = down_data[pos[0]][pos[1]] | |
858 |
|
902 | |||
859 | up_bits = up_conv_bits(up_value) |
|
903 | up_bits = up_conv_bits(up_value) | |
860 | down_bits = down_conv_bits(down_value) |
|
904 | down_bits = down_conv_bits(down_value) | |
861 | beam_bits = up_bits+down_bits |
|
905 | beam_bits = up_bits+down_bits | |
862 |
|
906 | |||
863 | return beam_bits |
|
907 | return beam_bits | |
864 |
|
908 | |||
865 |
|
909 | |||
866 | @property |
|
910 | @property | |
867 | def get_upvalues(self): |
|
911 | def get_upvalues(self): | |
868 | """ |
|
912 | """ | |
869 | This function reads antenna pattern and show the up-value of one abs module |
|
913 | This function reads antenna pattern and show the up-value of one abs module | |
870 | """ |
|
914 | """ | |
871 |
|
915 | |||
872 | data = ast.literal_eval(self.antenna) |
|
916 | data = ast.literal_eval(self.antenna) | |
873 | up_data = data['antenna_up'] |
|
917 | up_data = data['antenna_up'] | |
874 |
|
918 | |||
875 | up_values = [] |
|
919 | up_values = [] | |
876 | for data in up_data: |
|
920 | for data in up_data: | |
877 | for i in range(0,8): |
|
921 | for i in range(0,8): | |
878 | up_values.append(data[i]) |
|
922 | up_values.append(data[i]) | |
879 |
|
923 | |||
880 | return up_values |
|
924 | return up_values | |
881 |
|
925 | |||
882 | @property |
|
926 | @property | |
883 | def antenna_upvalues(self): |
|
927 | def antenna_upvalues(self): | |
884 | """ |
|
928 | """ | |
885 | This function reads antenna pattern and show the up - values of one abs beam |
|
929 | This function reads antenna pattern and show the up - values of one abs beam | |
886 | in a particular order |
|
930 | in a particular order | |
887 | """ |
|
931 | """ | |
888 | data = ast.literal_eval(self.antenna) |
|
932 | data = ast.literal_eval(self.antenna) | |
889 | up_data = data['antenna_up'] |
|
933 | up_data = data['antenna_up'] | |
890 |
|
934 | |||
891 | return up_data |
|
935 | return up_data | |
892 |
|
936 | |||
893 | @property |
|
937 | @property | |
894 | def antenna_downvalues(self): |
|
938 | def antenna_downvalues(self): | |
895 | """ |
|
939 | """ | |
896 | This function reads antenna pattern and show the down - values of one abs beam |
|
940 | This function reads antenna pattern and show the down - values of one abs beam | |
897 | in a particular order |
|
941 | in a particular order | |
898 | """ |
|
942 | """ | |
899 | data = ast.literal_eval(self.antenna) |
|
943 | data = ast.literal_eval(self.antenna) | |
900 | down_data = data['antenna_down'] |
|
944 | down_data = data['antenna_down'] | |
901 |
|
945 | |||
902 | return down_data |
|
946 | return down_data | |
903 |
|
947 | |||
904 | @property |
|
948 | @property | |
905 | def get_downvalues(self): |
|
949 | def get_downvalues(self): | |
906 | """ |
|
950 | """ | |
907 | This function reads antenna pattern and show the down-value of one abs module |
|
951 | This function reads antenna pattern and show the down-value of one abs module | |
908 | """ |
|
952 | """ | |
909 |
|
953 | |||
910 | data = ast.literal_eval(self.antenna) |
|
954 | data = ast.literal_eval(self.antenna) | |
911 | down_data = data['antenna_down'] |
|
955 | down_data = data['antenna_down'] | |
912 |
|
956 | |||
913 | down_values = [] |
|
957 | down_values = [] | |
914 | for data in down_data: |
|
958 | for data in down_data: | |
915 | for i in range(0,8): |
|
959 | for i in range(0,8): | |
916 | down_values.append(data[i]) |
|
960 | down_values.append(data[i]) | |
917 |
|
961 | |||
918 | return down_values |
|
962 | return down_values | |
919 |
|
963 | |||
920 | @property |
|
964 | @property | |
921 | def get_up_ues(self): |
|
965 | def get_up_ues(self): | |
922 | """ |
|
966 | """ | |
923 | This function shows the up-ues-value of one beam |
|
967 | This function shows the up-ues-value of one beam | |
924 | """ |
|
968 | """ | |
925 | data = ast.literal_eval(self.ues) |
|
969 | data = ast.literal_eval(self.ues) | |
926 | up_ues = data['up'] |
|
970 | up_ues = data['up'] | |
927 |
|
971 | |||
928 | return up_ues |
|
972 | return up_ues | |
929 |
|
973 | |||
930 | @property |
|
974 | @property | |
931 | def get_down_ues(self): |
|
975 | def get_down_ues(self): | |
932 | """ |
|
976 | """ | |
933 | This function shows the down-ues-value of one beam |
|
977 | This function shows the down-ues-value of one beam | |
934 | """ |
|
978 | """ | |
935 | data = ast.literal_eval(self.ues) |
|
979 | data = ast.literal_eval(self.ues) | |
936 | down_ues = data['down'] |
|
980 | down_ues = data['down'] | |
937 |
|
981 | |||
938 | return down_ues |
|
982 | return down_ues | |
939 |
|
983 | |||
940 | @property |
|
984 | @property | |
941 | def get_up_onlyrx(self): |
|
985 | def get_up_onlyrx(self): | |
942 | """ |
|
986 | """ | |
943 | This function shows the up-onlyrx-value of one beam |
|
987 | This function shows the up-onlyrx-value of one beam | |
944 | """ |
|
988 | """ | |
945 | data = json.loads(self.only_rx) |
|
989 | data = json.loads(self.only_rx) | |
946 | up_onlyrx = data['up'] |
|
990 | up_onlyrx = data['up'] | |
947 |
|
991 | |||
948 | return up_onlyrx |
|
992 | return up_onlyrx | |
949 |
|
993 | |||
950 | @property |
|
994 | @property | |
951 | def get_down_onlyrx(self): |
|
995 | def get_down_onlyrx(self): | |
952 | """ |
|
996 | """ | |
953 | This function shows the down-onlyrx-value of one beam |
|
997 | This function shows the down-onlyrx-value of one beam | |
954 | """ |
|
998 | """ | |
955 | data = json.loads(self.only_rx) |
|
999 | data = json.loads(self.only_rx) | |
956 | down_onlyrx = data['down'] |
|
1000 | down_onlyrx = data['down'] | |
957 |
|
1001 | |||
958 | return down_onlyrx |
|
1002 | return down_onlyrx | |
959 |
|
1003 | |||
960 | @property |
|
1004 | @property | |
961 | def get_tx(self): |
|
1005 | def get_tx(self): | |
962 | """ |
|
1006 | """ | |
963 | This function shows the tx-values of one beam |
|
1007 | This function shows the tx-values of one beam | |
964 | """ |
|
1008 | """ | |
965 | data = json.loads(self.tx) |
|
1009 | data = json.loads(self.tx) | |
966 |
|
1010 | |||
967 | return data |
|
1011 | return data | |
968 |
|
1012 | |||
969 | @property |
|
1013 | @property | |
970 | def get_uptx(self): |
|
1014 | def get_uptx(self): | |
971 | """ |
|
1015 | """ | |
972 | This function shows the up-tx-values of one beam |
|
1016 | This function shows the up-tx-values of one beam | |
973 | """ |
|
1017 | """ | |
974 | data = json.loads(self.tx) |
|
1018 | data = json.loads(self.tx) | |
975 | up_data = data['up'] |
|
1019 | up_data = data['up'] | |
976 |
|
1020 | |||
977 | up_values = [] |
|
1021 | up_values = [] | |
978 | for data in up_data: |
|
1022 | for data in up_data: | |
979 | for i in range(0,8): |
|
1023 | for i in range(0,8): | |
980 | up_values.append(data[i]) |
|
1024 | up_values.append(data[i]) | |
981 |
|
1025 | |||
982 | return up_values |
|
1026 | return up_values | |
983 |
|
1027 | |||
984 | @property |
|
1028 | @property | |
985 | def get_downtx(self): |
|
1029 | def get_downtx(self): | |
986 | """ |
|
1030 | """ | |
987 | This function shows the down-tx-values of one beam |
|
1031 | This function shows the down-tx-values of one beam | |
988 | """ |
|
1032 | """ | |
989 | data = json.loads(self.tx) |
|
1033 | data = json.loads(self.tx) | |
990 | down_data = data['down'] |
|
1034 | down_data = data['down'] | |
991 |
|
1035 | |||
992 | down_values = [] |
|
1036 | down_values = [] | |
993 | for data in down_data: |
|
1037 | for data in down_data: | |
994 | for i in range(0,8): |
|
1038 | for i in range(0,8): | |
995 | down_values.append(data[i]) |
|
1039 | down_values.append(data[i]) | |
996 |
|
1040 | |||
997 | return down_values |
|
1041 | return down_values | |
998 |
|
1042 | |||
999 |
|
1043 | |||
1000 |
|
1044 | |||
1001 | @property |
|
1045 | @property | |
1002 | def get_rx(self): |
|
1046 | def get_rx(self): | |
1003 | """ |
|
1047 | """ | |
1004 | This function shows the rx-values of one beam |
|
1048 | This function shows the rx-values of one beam | |
1005 | """ |
|
1049 | """ | |
1006 | data = json.loads(self.rx) |
|
1050 | data = json.loads(self.rx) | |
1007 |
|
1051 | |||
1008 | return data |
|
1052 | return data | |
1009 |
|
1053 | |||
1010 | @property |
|
1054 | @property | |
1011 | def get_uprx(self): |
|
1055 | def get_uprx(self): | |
1012 | """ |
|
1056 | """ | |
1013 | This function shows the up-rx-values of one beam |
|
1057 | This function shows the up-rx-values of one beam | |
1014 | """ |
|
1058 | """ | |
1015 | data = json.loads(self.rx) |
|
1059 | data = json.loads(self.rx) | |
1016 | up_data = data['up'] |
|
1060 | up_data = data['up'] | |
1017 |
|
1061 | |||
1018 | up_values = [] |
|
1062 | up_values = [] | |
1019 | for data in up_data: |
|
1063 | for data in up_data: | |
1020 | for i in range(0,8): |
|
1064 | for i in range(0,8): | |
1021 | up_values.append(data[i]) |
|
1065 | up_values.append(data[i]) | |
1022 |
|
1066 | |||
1023 | return up_values |
|
1067 | return up_values | |
1024 |
|
1068 | |||
1025 | @property |
|
1069 | @property | |
1026 | def get_downrx(self): |
|
1070 | def get_downrx(self): | |
1027 | """ |
|
1071 | """ | |
1028 | This function shows the down-rx-values of one beam |
|
1072 | This function shows the down-rx-values of one beam | |
1029 | """ |
|
1073 | """ | |
1030 | data = json.loads(self.rx) |
|
1074 | data = json.loads(self.rx) | |
1031 | down_data = data['down'] |
|
1075 | down_data = data['down'] | |
1032 |
|
1076 | |||
1033 | down_values = [] |
|
1077 | down_values = [] | |
1034 | for data in down_data: |
|
1078 | for data in down_data: | |
1035 | for i in range(0,8): |
|
1079 | for i in range(0,8): | |
1036 | down_values.append(data[i]) |
|
1080 | down_values.append(data[i]) | |
1037 |
|
1081 | |||
1038 | return down_values |
|
1082 | return down_values |
@@ -1,512 +1,485 | |||||
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 |
|
176 | |||
|
177 | # def abs_conf_mqtt(request, id_conf): | |||
|
178 | # # socket.on('beams_ack',function(data){ | |||
|
179 | # ack=data; | |||
|
180 | # console.log("ack") | |||
|
181 | # console.log(ack) | |||
|
182 | # }) | |||
|
183 | ||||
|
184 | ||||
177 | def abs_conf_edit(request, id_conf): |
|
185 | def abs_conf_edit(request, id_conf): | |
178 |
|
186 | |||
179 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
187 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) | |
180 |
|
188 | |||
181 | beams = ABSBeam.objects.filter(abs_conf=conf) |
|
189 | beams = ABSBeam.objects.filter(abs_conf=conf) | |
182 |
|
190 | |||
183 | if request.method=='GET': |
|
191 | if request.method=='GET': | |
184 | form = ABSConfigurationForm(instance=conf) |
|
192 | form = ABSConfigurationForm(instance=conf) | |
185 |
|
193 | |||
186 | if request.method=='POST': |
|
194 | if request.method=='POST': | |
187 | form = ABSConfigurationForm(request.POST, instance=conf) |
|
195 | form = ABSConfigurationForm(request.POST, instance=conf) | |
188 |
|
196 | |||
189 | if form.is_valid(): |
|
197 | if form.is_valid(): | |
190 | conf = form.save(commit=False) |
|
198 | conf = form.save(commit=False) | |
191 | conf.save() |
|
199 | conf.save() | |
192 | return redirect('url_abs_conf', id_conf=conf.id) |
|
200 | return redirect('url_abs_conf', id_conf=conf.id) | |
193 |
|
201 | |||
194 | ###### SIDEBAR ###### |
|
202 | ###### SIDEBAR ###### | |
195 | kwargs = {} |
|
203 | kwargs = {} | |
196 |
|
204 | |||
197 | kwargs['dev_conf'] = conf |
|
205 | kwargs['dev_conf'] = conf | |
198 | #kwargs['id_dev'] = conf.id |
|
206 | #kwargs['id_dev'] = conf.id | |
199 | kwargs['id_conf'] = conf.id |
|
207 | kwargs['id_conf'] = conf.id | |
200 | kwargs['form'] = form |
|
208 | kwargs['form'] = form | |
201 | kwargs['abs_beams'] = beams |
|
209 | kwargs['abs_beams'] = beams | |
202 | kwargs['title'] = 'Device Configuration' |
|
210 | kwargs['title'] = 'Device Configuration' | |
203 | kwargs['suptitle'] = 'Edit' |
|
211 | kwargs['suptitle'] = 'Edit' | |
204 | kwargs['button'] = 'Save' |
|
212 | kwargs['button'] = 'Save' | |
205 |
|
213 | |||
206 | kwargs['edit'] = True |
|
214 | kwargs['edit'] = True | |
207 |
|
215 | |||
208 | return render(request, 'abs_conf_edit.html', kwargs) |
|
216 | return render(request, 'abs_conf_edit.html', kwargs) | |
209 |
|
217 | |||
210 | @csrf_exempt |
|
218 | @csrf_exempt | |
211 | def abs_conf_alert(request): |
|
219 | def abs_conf_alert(request): | |
212 |
|
220 | |||
213 | if request.method == 'POST': |
|
221 | if request.method == 'POST': | |
214 | print (request.POST) |
|
222 | print (request.POST) | |
215 | return HttpResponse(json.dumps({'result':1}), content_type='application/json') |
|
223 | return HttpResponse(json.dumps({'result':1}), content_type='application/json') | |
216 | else: |
|
224 | else: | |
217 | return redirect('index') |
|
225 | return redirect('index') | |
218 |
|
226 | |||
219 |
|
227 | |||
220 | def import_file(request, id_conf): |
|
228 | def import_file(request, id_conf): | |
221 |
|
229 | |||
222 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
230 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) | |
223 | if request.method=='POST': |
|
231 | if request.method=='POST': | |
224 | form = ABSImportForm(request.POST, request.FILES) |
|
232 | form = ABSImportForm(request.POST, request.FILES) | |
225 | if form.is_valid(): |
|
233 | if form.is_valid(): | |
226 | try: |
|
234 | try: | |
227 | parms = conf.import_from_file(request.FILES['file_name']) |
|
235 | parms = conf.import_from_file(request.FILES['file_name']) | |
228 |
|
236 | |||
229 | if parms: |
|
237 | if parms: | |
230 | conf.update_from_file(parms) |
|
238 | conf.update_from_file(parms) | |
231 | messages.success(request, 'Configuration "%s" loaded succesfully' % request.FILES['file_name']) |
|
239 | messages.success(request, 'Configuration "%s" loaded succesfully' % request.FILES['file_name']) | |
232 | return redirect(conf.get_absolute_url_edit()) |
|
240 | return redirect(conf.get_absolute_url_edit()) | |
233 |
|
241 | |||
234 | except Exception as e: |
|
242 | except Exception as e: | |
235 | messages.error(request, 'Error parsing file: "%s" - %s' % (request.FILES['file_name'], e)) |
|
243 | messages.error(request, 'Error parsing file: "%s" - %s' % (request.FILES['file_name'], e)) | |
236 |
|
244 | |||
237 | else: |
|
245 | else: | |
238 | messages.warning(request, 'Your current configuration will be replaced') |
|
246 | messages.warning(request, 'Your current configuration will be replaced') | |
239 | form = ABSImportForm() |
|
247 | form = ABSImportForm() | |
240 |
|
248 | |||
241 | kwargs = {} |
|
249 | kwargs = {} | |
242 | kwargs['form'] = form |
|
250 | kwargs['form'] = form | |
243 | kwargs['title'] = 'ABS Configuration' |
|
251 | kwargs['title'] = 'ABS Configuration' | |
244 | kwargs['suptitle'] = 'Import file' |
|
252 | kwargs['suptitle'] = 'Import file' | |
245 | kwargs['button'] = 'Upload' |
|
253 | kwargs['button'] = 'Upload' | |
246 | kwargs['previous'] = conf.get_absolute_url() |
|
254 | kwargs['previous'] = conf.get_absolute_url() | |
247 |
|
255 | |||
248 | return render(request, 'abs_import.html', kwargs) |
|
256 | return render(request, 'abs_import.html', kwargs) | |
249 |
|
257 | |||
250 |
|
258 | |||
251 | # def send_mqtt(request,id_conf): |
|
|||
252 |
|
||||
253 | # # conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
|||
254 |
|
||||
255 | # # abs_mqtt = Configuration.objects.filter(pk=conf.device.conf_active).first() |
|
|||
256 | # # if abs_mqtt!=conf: |
|
|||
257 | # # url_mqtt = '#' if abs is None else abs.get_absolute_url() |
|
|||
258 | # # label_mqtt = 'None' if abs is None else abs.label |
|
|||
259 | # # messages.warning( |
|
|||
260 | # # request, |
|
|||
261 | # # mark_safe('The current configuration has not been written in the modules, the active configuration is <a href="{}">{}</a>'.format( |
|
|||
262 | # # url_mqtt, |
|
|||
263 | # # label_mqtt |
|
|||
264 | # # )) |
|
|||
265 | # # ) |
|
|||
266 | # # return redirect(conf.get_absolute_url()) |
|
|||
267 |
|
||||
268 | # # beam = get_object_or_404(ABSBeam, pk=id_beam) |
|
|||
269 |
|
||||
270 | # conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
|||
271 |
|
||||
272 | # mqtt_client.publish('abs/beams_up', 'Hola up') |
|
|||
273 | # mqtt_client.publish('abs/beams_down', 'Hola down') |
|
|||
274 |
|
||||
275 | # kwargs = { |
|
|||
276 | # 'title': 'ABS', |
|
|||
277 | # 'suptitle': conf.label, |
|
|||
278 | # 'message': 'Are you sure you want to write ABS Beam?', |
|
|||
279 | # 'delete': False |
|
|||
280 | # } |
|
|||
281 | # kwargs['menu_configurations'] = 'active' |
|
|||
282 |
|
||||
283 | # return render(request, 'confirm.html', kwargs) |
|
|||
284 |
|
||||
285 |
|
||||
286 | def send_beam(request, id_conf, id_beam): |
|
259 | def send_beam(request, id_conf, id_beam): | |
287 |
|
260 | |||
288 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
261 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) | |
289 |
|
262 | |||
290 | abs = Configuration.objects.filter(pk=conf.device.conf_active).first() |
|
263 | abs = Configuration.objects.filter(pk=conf.device.conf_active).first() | |
291 | if abs!=conf: |
|
264 | if abs!=conf: | |
292 | url = '#' if abs is None else abs.get_absolute_url() |
|
265 | url = '#' if abs is None else abs.get_absolute_url() | |
293 | label = 'None' if abs is None else abs.label |
|
266 | label = 'None' if abs is None else abs.label | |
294 | messages.warning( |
|
267 | messages.warning( | |
295 | request, |
|
268 | request, | |
296 | mark_safe('The current configuration has not been written in the modules, the active configuration is <a href="{}">{}</a>'.format( |
|
269 | mark_safe('The current configuration has not been written in the modules, the active configuration is <a href="{}">{}</a>'.format( | |
297 | url, |
|
270 | url, | |
298 | label |
|
271 | label | |
299 | )) |
|
272 | )) | |
300 | ) |
|
273 | ) | |
301 | return redirect(conf.get_absolute_url()) |
|
274 | return redirect(conf.get_absolute_url()) | |
302 |
|
275 | |||
303 | beam = get_object_or_404(ABSBeam, pk=id_beam) |
|
276 | beam = get_object_or_404(ABSBeam, pk=id_beam) | |
304 |
|
277 | |||
305 | if request.method == 'POST': |
|
278 | if request.method == 'POST': | |
306 |
|
279 | |||
307 | beams_list = ABSBeam.objects.filter(abs_conf=conf) |
|
280 | beams_list = ABSBeam.objects.filter(abs_conf=conf) | |
308 | conf.active_beam = id_beam |
|
281 | conf.active_beam = id_beam | |
309 |
|
282 | |||
310 | i = 0 |
|
283 | i = 0 | |
311 | for b in beams_list: |
|
284 | for b in beams_list: | |
312 | if b.id == int(id_beam): |
|
285 | if b.id == int(id_beam): | |
313 | break |
|
286 | break | |
314 | else: |
|
287 | else: | |
315 | i += 1 |
|
288 | i += 1 | |
316 | beam_pos = i + 1 #Estandarizar |
|
289 | beam_pos = i + 1 #Estandarizar | |
317 | print ('%s Position: %s') % (beam.name, str(beam_pos)) |
|
290 | print ('%s Position: %s') % (beam.name, str(beam_pos)) | |
318 | conf.send_beam(beam_pos) |
|
291 | conf.send_beam(beam_pos) | |
319 |
|
292 | |||
320 | return redirect('url_abs_conf', conf.id) |
|
293 | return redirect('url_abs_conf', conf.id) | |
321 |
|
294 | |||
322 | kwargs = { |
|
295 | kwargs = { | |
323 | 'title': 'ABS', |
|
296 | 'title': 'ABS', | |
324 | 'suptitle': conf.label, |
|
297 | 'suptitle': conf.label, | |
325 | 'message': 'Are you sure you want to change ABS Beam to: {}?'.format(beam.name), |
|
298 | 'message': 'Are you sure you want to change ABS Beam to: {}?'.format(beam.name), | |
326 | 'delete': False |
|
299 | 'delete': False | |
327 | } |
|
300 | } | |
328 | kwargs['menu_configurations'] = 'active' |
|
301 | kwargs['menu_configurations'] = 'active' | |
329 |
|
302 | |||
330 | return render(request, 'confirm.html', kwargs) |
|
303 | return render(request, 'confirm.html', kwargs) | |
331 |
|
304 | |||
332 |
|
305 | |||
333 | def add_beam(request, id_conf): |
|
306 | def add_beam(request, id_conf): | |
334 |
|
307 | |||
335 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
308 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) | |
336 | confs = Configuration.objects.all() |
|
309 | confs = Configuration.objects.all() | |
337 |
|
310 | |||
338 | if request.method=='GET': |
|
311 | if request.method=='GET': | |
339 | form = ABSBeamAddForm() |
|
312 | form = ABSBeamAddForm() | |
340 |
|
313 | |||
341 | if request.method=='POST': |
|
314 | if request.method=='POST': | |
342 | form = ABSBeamAddForm(request.POST) |
|
315 | form = ABSBeamAddForm(request.POST) | |
343 |
|
316 | |||
344 | beam_data = get_values_from_form(request.POST) |
|
317 | beam_data = get_values_from_form(request.POST) | |
345 |
|
318 | |||
346 | new_beam = ABSBeam( |
|
319 | new_beam = ABSBeam( | |
347 | name = beam_data['name'], |
|
320 | name = beam_data['name'], | |
348 | antenna = json.dumps(beam_data['antenna']), |
|
321 | antenna = json.dumps(beam_data['antenna']), | |
349 | abs_conf = conf, |
|
322 | abs_conf = conf, | |
350 | tx = json.dumps(beam_data['tx']), |
|
323 | tx = json.dumps(beam_data['tx']), | |
351 | rx = json.dumps(beam_data['rx']), |
|
324 | rx = json.dumps(beam_data['rx']), | |
352 | ues = json.dumps(beam_data['ues']), |
|
325 | ues = json.dumps(beam_data['ues']), | |
353 | only_rx = json.dumps(beam_data['only_rx']) |
|
326 | only_rx = json.dumps(beam_data['only_rx']) | |
354 | ) |
|
327 | ) | |
355 | new_beam.save() |
|
328 | new_beam.save() | |
356 | messages.success(request, 'Beam: "%s" has been added.' % new_beam.name) |
|
329 | messages.success(request, 'Beam: "%s" has been added.' % new_beam.name) | |
357 |
|
330 | |||
358 | return redirect('url_edit_abs_conf', conf.id) |
|
331 | return redirect('url_edit_abs_conf', conf.id) | |
359 |
|
332 | |||
360 | ###### SIDEBAR ###### |
|
333 | ###### SIDEBAR ###### | |
361 | kwargs = {} |
|
334 | kwargs = {} | |
362 |
|
335 | |||
363 | #kwargs['dev_conf'] = conf.device |
|
336 | #kwargs['dev_conf'] = conf.device | |
364 | #kwargs['id_dev'] = conf.device |
|
337 | #kwargs['id_dev'] = conf.device | |
365 | #kwargs['previous'] = conf.get_absolute_url_edit() |
|
338 | #kwargs['previous'] = conf.get_absolute_url_edit() | |
366 | kwargs['id_conf'] = conf.id |
|
339 | kwargs['id_conf'] = conf.id | |
367 | kwargs['form'] = form |
|
340 | kwargs['form'] = form | |
368 | kwargs['title'] = 'ABS Beams' |
|
341 | kwargs['title'] = 'ABS Beams' | |
369 | kwargs['suptitle'] = 'Add Beam' |
|
342 | kwargs['suptitle'] = 'Add Beam' | |
370 | kwargs['button'] = 'Add' |
|
343 | kwargs['button'] = 'Add' | |
371 | kwargs['no_sidebar'] = True |
|
344 | kwargs['no_sidebar'] = True | |
372 | kwargs['edit'] = True |
|
345 | kwargs['edit'] = True | |
373 |
|
346 | |||
374 | return render(request, 'abs_add_beam.html', kwargs) |
|
347 | return render(request, 'abs_add_beam.html', kwargs) | |
375 |
|
348 | |||
376 |
|
349 | |||
377 | def edit_beam(request, id_conf, id_beam): |
|
350 | def edit_beam(request, id_conf, id_beam): | |
378 |
|
351 | |||
379 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
352 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) | |
380 | beam = get_object_or_404(ABSBeam, pk=id_beam) |
|
353 | beam = get_object_or_404(ABSBeam, pk=id_beam) | |
381 |
|
354 | |||
382 | if request.method=='GET': |
|
355 | if request.method=='GET': | |
383 | form = ABSBeamEditForm(initial={'beam': beam}) |
|
356 | form = ABSBeamEditForm(initial={'beam': beam}) | |
384 |
|
357 | |||
385 | if request.method=='POST': |
|
358 | if request.method=='POST': | |
386 | form = ABSBeamEditForm(request.POST) |
|
359 | form = ABSBeamEditForm(request.POST) | |
387 |
|
360 | |||
388 | beam_data = get_values_from_form(request.POST) |
|
361 | beam_data = get_values_from_form(request.POST) | |
389 |
|
362 | |||
390 | beam.dict_to_parms(beam_data) |
|
363 | beam.dict_to_parms(beam_data) | |
391 | beam.save() |
|
364 | beam.save() | |
392 |
|
365 | |||
393 | messages.success(request, 'Beam: "%s" has been updated.' % beam.name) |
|
366 | messages.success(request, 'Beam: "%s" has been updated.' % beam.name) | |
394 |
|
367 | |||
395 | return redirect('url_edit_abs_conf', conf.id) |
|
368 | return redirect('url_edit_abs_conf', conf.id) | |
396 |
|
369 | |||
397 | ###### SIDEBAR ###### |
|
370 | ###### SIDEBAR ###### | |
398 | kwargs = {} |
|
371 | kwargs = {} | |
399 |
|
372 | |||
400 | kwargs['id_conf'] = conf.id |
|
373 | kwargs['id_conf'] = conf.id | |
401 | kwargs['form'] = form |
|
374 | kwargs['form'] = form | |
402 | kwargs['title'] = 'ABS Beams' |
|
375 | kwargs['title'] = 'ABS Beams' | |
403 | kwargs['suptitle'] = 'Edit Beam' |
|
376 | kwargs['suptitle'] = 'Edit Beam' | |
404 | kwargs['button'] = 'Save' |
|
377 | kwargs['button'] = 'Save' | |
405 | kwargs['no_sidebar'] = True |
|
378 | kwargs['no_sidebar'] = True | |
406 |
|
379 | |||
407 | #kwargs['previous'] = conf.get_absolute_url_edit() |
|
380 | #kwargs['previous'] = conf.get_absolute_url_edit() | |
408 | kwargs['edit'] = True |
|
381 | kwargs['edit'] = True | |
409 |
|
382 | |||
410 | return render(request, 'abs_edit_beam.html', kwargs) |
|
383 | return render(request, 'abs_edit_beam.html', kwargs) | |
411 |
|
384 | |||
412 |
|
385 | |||
413 |
|
386 | |||
414 | def remove_beam(request, id_conf, id_beam): |
|
387 | def remove_beam(request, id_conf, id_beam): | |
415 |
|
388 | |||
416 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
389 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) | |
417 | beam = get_object_or_404(ABSBeam, pk=id_beam) |
|
390 | beam = get_object_or_404(ABSBeam, pk=id_beam) | |
418 |
|
391 | |||
419 | if request.method=='POST': |
|
392 | if request.method=='POST': | |
420 | if beam: |
|
393 | if beam: | |
421 | try: |
|
394 | try: | |
422 | beam.delete() |
|
395 | beam.delete() | |
423 | messages.success(request, 'Beam: "%s" has been deleted.' % beam) |
|
396 | messages.success(request, 'Beam: "%s" has been deleted.' % beam) | |
424 | except: |
|
397 | except: | |
425 | messages.error(request, 'Unable to delete beam: "%s".' % beam) |
|
398 | messages.error(request, 'Unable to delete beam: "%s".' % beam) | |
426 |
|
399 | |||
427 | return redirect('url_edit_abs_conf', conf.id) |
|
400 | return redirect('url_edit_abs_conf', conf.id) | |
428 |
|
401 | |||
429 | ###### SIDEBAR ###### |
|
402 | ###### SIDEBAR ###### | |
430 | kwargs = {} |
|
403 | kwargs = {} | |
431 |
|
404 | |||
432 | kwargs['object'] = beam |
|
405 | kwargs['object'] = beam | |
433 | kwargs['delete'] = True |
|
406 | kwargs['delete'] = True | |
434 | kwargs['title'] = 'Delete' |
|
407 | kwargs['title'] = 'Delete' | |
435 | kwargs['suptitle'] = 'Beam' |
|
408 | kwargs['suptitle'] = 'Beam' | |
436 | kwargs['previous'] = conf.get_absolute_url_edit() |
|
409 | kwargs['previous'] = conf.get_absolute_url_edit() | |
437 | return render(request, 'confirm.html', kwargs) |
|
410 | return render(request, 'confirm.html', kwargs) | |
438 |
|
411 | |||
439 |
|
412 | |||
440 |
|
413 | |||
441 | def plot_patterns(request, id_conf, id_beam=None): |
|
414 | def plot_patterns(request, id_conf, id_beam=None): | |
442 |
|
415 | |||
443 | kwargs = {} |
|
416 | kwargs = {} | |
444 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
417 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) | |
445 | beams = ABSBeam.objects.filter(abs_conf=conf) |
|
418 | beams = ABSBeam.objects.filter(abs_conf=conf) | |
446 |
|
419 | |||
447 | if id_beam: |
|
420 | if id_beam: | |
448 | beam = get_object_or_404(ABSBeam, pk=id_beam) |
|
421 | beam = get_object_or_404(ABSBeam, pk=id_beam) | |
449 | kwargs['beam'] = beam |
|
422 | kwargs['beam'] = beam | |
450 |
|
423 | |||
451 | ###### SIDEBAR ###### |
|
424 | ###### SIDEBAR ###### | |
452 |
|
425 | |||
453 | kwargs['dev_conf'] = conf.device |
|
426 | kwargs['dev_conf'] = conf.device | |
454 | kwargs['id_dev'] = conf.device |
|
427 | kwargs['id_dev'] = conf.device | |
455 | kwargs['id_conf'] = conf.id |
|
428 | kwargs['id_conf'] = conf.id | |
456 | kwargs['abs_beams'] = beams |
|
429 | kwargs['abs_beams'] = beams | |
457 | kwargs['title'] = 'ABS Patterns' |
|
430 | kwargs['title'] = 'ABS Patterns' | |
458 | kwargs['suptitle'] = conf.name |
|
431 | kwargs['suptitle'] = conf.name | |
459 | kwargs['no_sidebar'] = True |
|
432 | kwargs['no_sidebar'] = True | |
460 |
|
433 | |||
461 | return render(request, 'abs_patterns.html', kwargs) |
|
434 | return render(request, 'abs_patterns.html', kwargs) | |
462 |
|
435 | |||
463 |
|
436 | |||
464 | def plot_pattern(request, id_conf, id_beam, antenna): |
|
437 | def plot_pattern(request, id_conf, id_beam, antenna): | |
465 |
|
438 | |||
466 | if antenna=='down': |
|
439 | if antenna=='down': | |
467 | sleep(3) |
|
440 | sleep(3) | |
468 |
|
441 | |||
469 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
442 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) | |
470 | beam = get_object_or_404(ABSBeam, pk=id_beam) |
|
443 | beam = get_object_or_404(ABSBeam, pk=id_beam) | |
471 | just_rx = 1 if json.loads(beam.only_rx)[antenna] else 0 |
|
444 | just_rx = 1 if json.loads(beam.only_rx)[antenna] else 0 | |
472 | phases = json.loads(beam.antenna)['antenna_{}'.format(antenna)] |
|
445 | phases = json.loads(beam.antenna)['antenna_{}'.format(antenna)] | |
473 | gain_tx = json.loads(beam.tx)[antenna] |
|
446 | gain_tx = json.loads(beam.tx)[antenna] | |
474 | gain_rx = json.loads(beam.rx)[antenna] |
|
447 | gain_rx = json.loads(beam.rx)[antenna] | |
475 | ues = json.loads(beam.ues)[antenna] |
|
448 | ues = json.loads(beam.ues)[antenna] | |
476 | newOverJro = overJroShow(beam.name) |
|
449 | newOverJro = overJroShow(beam.name) | |
477 | fig = newOverJro.plotPattern2(datetime.today(), phases, gain_tx, gain_rx, ues, just_rx) |
|
450 | fig = newOverJro.plotPattern2(datetime.today(), phases, gain_tx, gain_rx, ues, just_rx) | |
478 | buf = io.BytesIO() |
|
451 | buf = io.BytesIO() | |
479 | fig.savefig(buf, format='png') |
|
452 | fig.savefig(buf, format='png') | |
480 | response = HttpResponse(buf.getvalue(), content_type='image/png') |
|
453 | response = HttpResponse(buf.getvalue(), content_type='image/png') | |
481 | return response |
|
454 | return response | |
482 |
|
455 | |||
483 | import os |
|
456 | import os | |
484 | from django.http import HttpResponse |
|
457 | from django.http import HttpResponse | |
485 |
|
458 | |||
486 | @sio.on('connection-bind') |
|
459 | @sio.on('connection-bind') | |
487 | def abs_connection_bind(sid, data): |
|
460 | def abs_connection_bind(sid, data): | |
488 | print("sid:",sid,"data",data) |
|
461 | print("sid:",sid,"data",data) | |
489 |
|
462 | |||
490 | @sio.on('disconnect') |
|
463 | @sio.on('disconnect') | |
491 | def abs_test_disconnect(sid): |
|
464 | def abs_test_disconnect(sid): | |
492 | print("Disconnected") |
|
465 | print("Disconnected") | |
493 |
|
466 | |||
494 | @sio.event |
|
467 | @sio.event | |
495 | def abs_send_beam_up(sid, message): |
|
468 | def abs_send_beam_up(sid, message): | |
496 | mqtt_client.publish('abs/beams_up', message['data']) |
|
469 | mqtt_client.publish('abs/beams_up', message['data']) | |
497 |
|
470 | |||
498 | @sio.event |
|
471 | @sio.event | |
499 | def abs_send_beam_down(sid, message): |
|
472 | def abs_send_beam_down(sid, message): | |
500 | mqtt_client.publish('abs/beams_down', message['data']) |
|
473 | mqtt_client.publish('abs/beams_down', message['data']) | |
501 |
|
474 | |||
502 | @sio.event |
|
475 | @sio.event | |
503 | def change_beam(sid,message): |
|
476 | def change_beam(sid,message): | |
504 | data=str(message['data']) |
|
477 | data=str(message['data']) | |
505 | data=data[16] |
|
478 | data=data[16] | |
506 | mqtt_client.publish('abs/change_beam',data) |
|
479 | mqtt_client.publish('abs/change_beam',data) | |
507 |
|
480 | |||
508 |
|
481 | |||
509 |
|
482 | |||
510 |
|
483 | |||
511 |
|
484 | |||
512 |
|
485 |
@@ -1,822 +1,829 | |||||
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 | ) |
|
65 | ) | |
66 |
|
66 | |||
67 | DEV_TYPES = ( |
|
67 | DEV_TYPES = ( | |
68 | ('', 'Select a device type'), |
|
68 | ('', 'Select a device type'), | |
69 | ('rc', 'Radar Controller'), |
|
69 | ('rc', 'Radar Controller'), | |
70 | ('dds', 'Direct Digital Synthesizer'), |
|
70 | ('dds', 'Direct Digital Synthesizer'), | |
71 | ('jars', 'Jicamarca Radar Acquisition System'), |
|
71 | ('jars', 'Jicamarca Radar Acquisition System'), | |
72 | ('usrp', 'Universal Software Radio Peripheral'), |
|
72 | ('usrp', 'Universal Software Radio Peripheral'), | |
73 | ('cgs', 'Clock Generator System'), |
|
73 | ('cgs', 'Clock Generator System'), | |
74 | ('abs', 'Automatic Beam Switching'), |
|
74 | ('abs', 'Automatic Beam Switching'), | |
75 | ('dds_rest', 'Direct Digital Synthesizer_REST'), |
|
75 | ('dds_rest', 'Direct Digital Synthesizer_REST'), | |
76 | ('atrad', 'Transmitter ATRAD'), |
|
76 | ('atrad', 'Transmitter ATRAD'), | |
77 | ) |
|
77 | ) | |
78 |
|
78 | |||
79 | EXP_STATES = ( |
|
79 | EXP_STATES = ( | |
80 | (0,'Error'), #RED |
|
80 | (0,'Error'), #RED | |
81 | (1,'Cancelled'), #YELLOW |
|
81 | (1,'Cancelled'), #YELLOW | |
82 | (2,'Running'), #GREEN |
|
82 | (2,'Running'), #GREEN | |
83 | (3,'Scheduled'), #BLUE |
|
83 | (3,'Scheduled'), #BLUE | |
84 | (4,'Unknown'), #WHITE |
|
84 | (4,'Unknown'), #WHITE | |
85 | ) |
|
85 | ) | |
86 |
|
86 | |||
87 | CONF_TYPES = ( |
|
87 | CONF_TYPES = ( | |
88 | (0, 'Active'), |
|
88 | (0, 'Active'), | |
89 | (1, 'Historical'), |
|
89 | (1, 'Historical'), | |
90 | ) |
|
90 | ) | |
91 |
|
91 | |||
92 | class Profile(models.Model): |
|
92 | class Profile(models.Model): | |
93 | user = models.OneToOneField(User, on_delete=models.CASCADE) |
|
93 | user = models.OneToOneField(User, on_delete=models.CASCADE) | |
94 | theme = models.CharField(max_length=30, default='spacelab') |
|
94 | theme = models.CharField(max_length=30, default='spacelab') | |
95 |
|
95 | |||
96 |
|
96 | |||
97 | @receiver(post_save, sender=User) |
|
97 | @receiver(post_save, sender=User) | |
98 | def create_user_profile(sender, instance, created, **kwargs): |
|
98 | def create_user_profile(sender, instance, created, **kwargs): | |
99 | if created: |
|
99 | if created: | |
100 | Profile.objects.create(user=instance) |
|
100 | Profile.objects.create(user=instance) | |
101 |
|
101 | |||
102 | @receiver(post_save, sender=User) |
|
102 | @receiver(post_save, sender=User) | |
103 | def save_user_profile(sender, instance, **kwargs): |
|
103 | def save_user_profile(sender, instance, **kwargs): | |
104 | instance.profile.save() |
|
104 | instance.profile.save() | |
105 |
|
105 | |||
106 |
|
106 | |||
107 | class Location(models.Model): |
|
107 | class Location(models.Model): | |
108 |
|
108 | |||
109 | name = models.CharField(max_length = 30) |
|
109 | name = models.CharField(max_length = 30) | |
110 | description = models.TextField(blank=True, null=True) |
|
110 | description = models.TextField(blank=True, null=True) | |
111 |
|
111 | |||
112 | class Meta: |
|
112 | class Meta: | |
113 | db_table = 'db_location' |
|
113 | db_table = 'db_location' | |
114 |
|
114 | |||
115 | def __str__(self): |
|
115 | def __str__(self): | |
116 | return u'%s' % self.name |
|
116 | return u'%s' % self.name | |
117 |
|
117 | |||
118 | def get_absolute_url(self): |
|
118 | def get_absolute_url(self): | |
119 | return reverse('url_location', args=[str(self.id)]) |
|
119 | return reverse('url_location', args=[str(self.id)]) | |
120 |
|
120 | |||
121 |
|
121 | |||
122 | class DeviceType(models.Model): |
|
122 | class DeviceType(models.Model): | |
123 |
|
123 | |||
124 | name = models.CharField(max_length = 10, choices = DEV_TYPES, default = 'dds_rest') |
|
124 | name = models.CharField(max_length = 10, choices = DEV_TYPES, default = 'dds_rest') | |
125 | sequence = models.PositiveSmallIntegerField(default=55) |
|
125 | sequence = models.PositiveSmallIntegerField(default=55) | |
126 | description = models.TextField(blank=True, null=True) |
|
126 | description = models.TextField(blank=True, null=True) | |
127 |
|
127 | |||
128 | class Meta: |
|
128 | class Meta: | |
129 | db_table = 'db_device_types' |
|
129 | db_table = 'db_device_types' | |
130 |
|
130 | |||
131 | def __str__(self): |
|
131 | def __str__(self): | |
132 | return u'%s' % self.get_name_display() |
|
132 | return u'%s' % self.get_name_display() | |
133 |
|
133 | |||
134 | class Device(models.Model): |
|
134 | class Device(models.Model): | |
135 |
|
135 | |||
136 | device_type = models.ForeignKey('DeviceType', on_delete=models.CASCADE) |
|
136 | device_type = models.ForeignKey('DeviceType', on_delete=models.CASCADE) | |
137 | location = models.ForeignKey('Location', on_delete=models.CASCADE) |
|
137 | location = models.ForeignKey('Location', on_delete=models.CASCADE) | |
138 | ip_address = models.GenericIPAddressField(protocol='IPv4', default='0.0.0.0') |
|
138 | ip_address = models.GenericIPAddressField(protocol='IPv4', default='0.0.0.0') | |
139 | port_address = models.PositiveSmallIntegerField(default=2000) |
|
139 | port_address = models.PositiveSmallIntegerField(default=2000) | |
140 | description = models.TextField(blank=True, null=True) |
|
140 | description = models.TextField(blank=True, null=True) | |
141 | status = models.PositiveSmallIntegerField(default=4, choices=DEV_STATES) |
|
141 | status = models.PositiveSmallIntegerField(default=4, choices=DEV_STATES) | |
142 | conf_active = models.PositiveIntegerField(default=0, verbose_name='Current configuration') |
|
142 | conf_active = models.PositiveIntegerField(default=0, verbose_name='Current configuration') | |
143 |
|
143 | |||
144 | class Meta: |
|
144 | class Meta: | |
145 | db_table = 'db_devices' |
|
145 | db_table = 'db_devices' | |
146 |
|
146 | |||
147 | def __str__(self): |
|
147 | def __str__(self): | |
148 | ret = u'{} [{}]'.format(self.device_type.name.upper(), self.location.name) |
|
148 | ret = u'{} [{}]'.format(self.device_type.name.upper(), self.location.name) | |
149 |
|
149 | |||
150 | return ret |
|
150 | return ret | |
151 |
|
151 | |||
152 | @property |
|
152 | @property | |
153 | def name(self): |
|
153 | def name(self): | |
154 | return str(self) |
|
154 | return str(self) | |
155 |
|
155 | |||
156 | def get_status(self): |
|
156 | def get_status(self): | |
157 | return self.status |
|
157 | return self.status | |
158 |
|
158 | |||
159 | @property |
|
159 | @property | |
160 | def status_color(self): |
|
160 | def status_color(self): | |
161 | color = 'muted' |
|
161 | color = 'muted' | |
162 | if self.status == 0: |
|
162 | if self.status == 0: | |
163 | color = "danger" |
|
163 | color = "danger" | |
164 | elif self.status == 1: |
|
164 | elif self.status == 1: | |
165 | color = "warning" |
|
165 | color = "warning" | |
166 | elif self.status == 2: |
|
166 | elif self.status == 2: | |
167 | color = "info" |
|
167 | color = "info" | |
168 | elif self.status == 3: |
|
168 | elif self.status == 3: | |
169 | color = "success" |
|
169 | color = "success" | |
170 |
|
170 | |||
171 | return color |
|
171 | return color | |
172 |
|
172 | |||
173 | def url(self, path=None): |
|
173 | def url(self, path=None): | |
174 |
|
174 | |||
175 | if path: |
|
175 | if path: | |
176 | return 'http://{}:{}/{}/'.format(self.ip_address, self.port_address, path) |
|
176 | return 'http://{}:{}/{}/'.format(self.ip_address, self.port_address, path) | |
177 | else: |
|
177 | else: | |
178 | return 'http://{}:{}/'.format(self.ip_address, self.port_address) |
|
178 | return 'http://{}:{}/'.format(self.ip_address, self.port_address) | |
179 |
|
179 | |||
180 | def get_absolute_url(self): |
|
180 | def get_absolute_url(self): | |
181 | return reverse('url_device', args=[str(self.id)]) |
|
181 | return reverse('url_device', args=[str(self.id)]) | |
182 |
|
182 | |||
183 | def get_absolute_url_edit(self): |
|
183 | def get_absolute_url_edit(self): | |
184 | return reverse('url_edit_device', args=[str(self.id)]) |
|
184 | return reverse('url_edit_device', args=[str(self.id)]) | |
185 |
|
185 | |||
186 | def get_absolute_url_delete(self): |
|
186 | def get_absolute_url_delete(self): | |
187 | return reverse('url_delete_device', args=[str(self.id)]) |
|
187 | return reverse('url_delete_device', args=[str(self.id)]) | |
188 |
|
188 | |||
189 | def change_ip(self, ip_address, mask, gateway, dns, **kwargs): |
|
189 | def change_ip(self, ip_address, mask, gateway, dns, **kwargs): | |
190 |
|
190 | |||
191 | if self.device_type.name=='dds': |
|
191 | if self.device_type.name=='dds': | |
192 | try: |
|
192 | try: | |
193 | answer = dds_api.change_ip(ip = self.ip_address, |
|
193 | answer = dds_api.change_ip(ip = self.ip_address, | |
194 | port = self.port_address, |
|
194 | port = self.port_address, | |
195 | new_ip = ip_address, |
|
195 | new_ip = ip_address, | |
196 | mask = mask, |
|
196 | mask = mask, | |
197 | gateway = gateway) |
|
197 | gateway = gateway) | |
198 | if answer[0]=='1': |
|
198 | if answer[0]=='1': | |
199 | self.message = '25|DDS - {}'.format(answer) |
|
199 | self.message = '25|DDS - {}'.format(answer) | |
200 | self.ip_address = ip_address |
|
200 | self.ip_address = ip_address | |
201 | self.save() |
|
201 | self.save() | |
202 | else: |
|
202 | else: | |
203 | self.message = '30|DDS - {}'.format(answer) |
|
203 | self.message = '30|DDS - {}'.format(answer) | |
204 | return False |
|
204 | return False | |
205 | except Exception as e: |
|
205 | except Exception as e: | |
206 | self.message = '40|{}'.format(str(e)) |
|
206 | self.message = '40|{}'.format(str(e)) | |
207 | return False |
|
207 | return False | |
208 |
|
208 | |||
209 | elif self.device_type.name=='rc': |
|
209 | elif self.device_type.name=='rc': | |
210 | headers = {'content-type': "application/json", |
|
210 | headers = {'content-type': "application/json", | |
211 | 'cache-control': "no-cache"} |
|
211 | 'cache-control': "no-cache"} | |
212 |
|
212 | |||
213 | ip = [int(x) for x in ip_address.split('.')] |
|
213 | ip = [int(x) for x in ip_address.split('.')] | |
214 | dns = [int(x) for x in dns.split('.')] |
|
214 | dns = [int(x) for x in dns.split('.')] | |
215 | gateway = [int(x) for x in gateway.split('.')] |
|
215 | gateway = [int(x) for x in gateway.split('.')] | |
216 | subnet = [int(x) for x in mask.split('.')] |
|
216 | subnet = [int(x) for x in mask.split('.')] | |
217 |
|
217 | |||
218 | payload = { |
|
218 | payload = { | |
219 | "ip": ip, |
|
219 | "ip": ip, | |
220 | "dns": dns, |
|
220 | "dns": dns, | |
221 | "gateway": gateway, |
|
221 | "gateway": gateway, | |
222 | "subnet": subnet |
|
222 | "subnet": subnet | |
223 | } |
|
223 | } | |
224 |
|
224 | |||
225 | req = requests.post(self.url('changeip'), data=json.dumps(payload), headers=headers) |
|
225 | req = requests.post(self.url('changeip'), data=json.dumps(payload), headers=headers) | |
226 | try: |
|
226 | try: | |
227 | answer = req.json() |
|
227 | answer = req.json() | |
228 | if answer['changeip']=='ok': |
|
228 | if answer['changeip']=='ok': | |
229 | self.message = '25|IP succesfully changed' |
|
229 | self.message = '25|IP succesfully changed' | |
230 | self.ip_address = ip_address |
|
230 | self.ip_address = ip_address | |
231 | self.save() |
|
231 | self.save() | |
232 | else: |
|
232 | else: | |
233 | self.message = '30|An error ocuur when changing IP' |
|
233 | self.message = '30|An error ocuur when changing IP' | |
234 | except Exception as e: |
|
234 | except Exception as e: | |
235 | self.message = '40|{}'.format(str(e)) |
|
235 | self.message = '40|{}'.format(str(e)) | |
236 | else: |
|
236 | else: | |
237 | self.message = 'Not implemented' |
|
237 | self.message = 'Not implemented' | |
238 | return False |
|
238 | return False | |
239 |
|
239 | |||
240 | return True |
|
240 | return True | |
241 |
|
241 | |||
242 |
|
242 | |||
243 | class Campaign(models.Model): |
|
243 | class Campaign(models.Model): | |
244 |
|
244 | |||
245 | template = models.BooleanField(default=False) |
|
245 | template = models.BooleanField(default=False) | |
246 | name = models.CharField(max_length=60, unique=True) |
|
246 | name = models.CharField(max_length=60, unique=True) | |
247 | start_date = models.DateTimeField(blank=True, null=True) |
|
247 | start_date = models.DateTimeField(blank=True, null=True) | |
248 | end_date = models.DateTimeField(blank=True, null=True) |
|
248 | end_date = models.DateTimeField(blank=True, null=True) | |
249 | tags = models.CharField(max_length=40, blank=True, null=True) |
|
249 | tags = models.CharField(max_length=40, blank=True, null=True) | |
250 | description = models.TextField(blank=True, null=True) |
|
250 | description = models.TextField(blank=True, null=True) | |
251 | experiments = models.ManyToManyField('Experiment', blank=True) |
|
251 | experiments = models.ManyToManyField('Experiment', blank=True) | |
252 | author = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE) |
|
252 | author = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE) | |
253 |
|
253 | |||
254 | class Meta: |
|
254 | class Meta: | |
255 | db_table = 'db_campaigns' |
|
255 | db_table = 'db_campaigns' | |
256 | ordering = ('name',) |
|
256 | ordering = ('name',) | |
257 |
|
257 | |||
258 | def __str__(self): |
|
258 | def __str__(self): | |
259 | if self.template: |
|
259 | if self.template: | |
260 | return u'{} (template)'.format(self.name) |
|
260 | return u'{} (template)'.format(self.name) | |
261 | else: |
|
261 | else: | |
262 | return u'{}'.format(self.name) |
|
262 | return u'{}'.format(self.name) | |
263 |
|
263 | |||
264 | def jsonify(self): |
|
264 | def jsonify(self): | |
265 |
|
265 | |||
266 | data = {} |
|
266 | data = {} | |
267 |
|
267 | |||
268 | ignored = ('template') |
|
268 | ignored = ('template') | |
269 |
|
269 | |||
270 | for field in self._meta.fields: |
|
270 | for field in self._meta.fields: | |
271 | if field.name in ignored: |
|
271 | if field.name in ignored: | |
272 | continue |
|
272 | continue | |
273 | data[field.name] = field.value_from_object(self) |
|
273 | data[field.name] = field.value_from_object(self) | |
274 |
|
274 | |||
275 | data['start_date'] = data['start_date'].strftime('%Y-%m-%d') |
|
275 | data['start_date'] = data['start_date'].strftime('%Y-%m-%d') | |
276 | data['end_date'] = data['end_date'].strftime('%Y-%m-%d') |
|
276 | data['end_date'] = data['end_date'].strftime('%Y-%m-%d') | |
277 |
|
277 | |||
278 | return data |
|
278 | return data | |
279 |
|
279 | |||
280 | def parms_to_dict(self): |
|
280 | def parms_to_dict(self): | |
281 |
|
281 | |||
282 | params = Params({}) |
|
282 | params = Params({}) | |
283 | params.add(self.jsonify(), 'campaigns') |
|
283 | params.add(self.jsonify(), 'campaigns') | |
284 |
|
284 | |||
285 | for exp in Experiment.objects.filter(campaign = self): |
|
285 | for exp in Experiment.objects.filter(campaign = self): | |
286 | params.add(exp.jsonify(), 'experiments') |
|
286 | params.add(exp.jsonify(), 'experiments') | |
287 | configurations = Configuration.objects.filter(experiment=exp, type=0) |
|
287 | configurations = Configuration.objects.filter(experiment=exp, type=0) | |
288 |
|
288 | |||
289 | for conf in configurations: |
|
289 | for conf in configurations: | |
290 | params.add(conf.jsonify(), 'configurations') |
|
290 | params.add(conf.jsonify(), 'configurations') | |
291 | if conf.device.device_type.name=='rc': |
|
291 | if conf.device.device_type.name=='rc': | |
292 | for line in conf.get_lines(): |
|
292 | for line in conf.get_lines(): | |
293 | params.add(line.jsonify(), 'lines') |
|
293 | params.add(line.jsonify(), 'lines') | |
294 |
|
294 | |||
295 | return params.data |
|
295 | return params.data | |
296 |
|
296 | |||
297 | def dict_to_parms(self, parms, CONF_MODELS): |
|
297 | def dict_to_parms(self, parms, CONF_MODELS): | |
298 |
|
298 | |||
299 | experiments = Experiment.objects.filter(campaign = self) |
|
299 | experiments = Experiment.objects.filter(campaign = self) | |
300 |
|
300 | |||
301 | if experiments: |
|
301 | if experiments: | |
302 | for experiment in experiments: |
|
302 | for experiment in experiments: | |
303 | experiment.delete() |
|
303 | experiment.delete() | |
304 |
|
304 | |||
305 | for id_exp in parms['experiments']['allIds']: |
|
305 | for id_exp in parms['experiments']['allIds']: | |
306 | exp_parms = parms['experiments']['byId'][id_exp] |
|
306 | exp_parms = parms['experiments']['byId'][id_exp] | |
307 | dum = (datetime.now() - datetime(1970, 1, 1)).total_seconds() |
|
307 | dum = (datetime.now() - datetime(1970, 1, 1)).total_seconds() | |
308 | exp = Experiment(name='{}'.format(dum)) |
|
308 | exp = Experiment(name='{}'.format(dum)) | |
309 | exp.save() |
|
309 | exp.save() | |
310 | exp.dict_to_parms(parms, CONF_MODELS, id_exp=id_exp) |
|
310 | exp.dict_to_parms(parms, CONF_MODELS, id_exp=id_exp) | |
311 | self.experiments.add(exp) |
|
311 | self.experiments.add(exp) | |
312 |
|
312 | |||
313 | camp_parms = parms['campaigns']['byId'][parms['campaigns']['allIds'][0]] |
|
313 | camp_parms = parms['campaigns']['byId'][parms['campaigns']['allIds'][0]] | |
314 |
|
314 | |||
315 | self.name = '{}-{}'.format(camp_parms['name'], datetime.now().strftime('%y%m%d')) |
|
315 | self.name = '{}-{}'.format(camp_parms['name'], datetime.now().strftime('%y%m%d')) | |
316 | self.start_date = camp_parms['start_date'] |
|
316 | self.start_date = camp_parms['start_date'] | |
317 | self.end_date = camp_parms['end_date'] |
|
317 | self.end_date = camp_parms['end_date'] | |
318 | self.tags = camp_parms['tags'] |
|
318 | self.tags = camp_parms['tags'] | |
319 | self.save() |
|
319 | self.save() | |
320 |
|
320 | |||
321 | return self |
|
321 | return self | |
322 |
|
322 | |||
323 | def get_experiments_by_radar(self, radar=None): |
|
323 | def get_experiments_by_radar(self, radar=None): | |
324 |
|
324 | |||
325 | ret = [] |
|
325 | ret = [] | |
326 | if radar: |
|
326 | if radar: | |
327 | locations = Location.objects.filter(pk=radar) |
|
327 | locations = Location.objects.filter(pk=radar) | |
328 | else: |
|
328 | else: | |
329 | locations = set([e.location for e in self.experiments.all()]) |
|
329 | locations = set([e.location for e in self.experiments.all()]) | |
330 |
|
330 | |||
331 | for loc in locations: |
|
331 | for loc in locations: | |
332 | dum = {} |
|
332 | dum = {} | |
333 | dum['name'] = loc.name |
|
333 | dum['name'] = loc.name | |
334 | dum['id'] = loc.pk |
|
334 | dum['id'] = loc.pk | |
335 | dum['experiments'] = [e for e in self.experiments.all() if e.location==loc] |
|
335 | dum['experiments'] = [e for e in self.experiments.all() if e.location==loc] | |
336 | ret.append(dum) |
|
336 | ret.append(dum) | |
337 |
|
337 | |||
338 | return ret |
|
338 | return ret | |
339 |
|
339 | |||
340 | def get_absolute_url(self): |
|
340 | def get_absolute_url(self): | |
341 | return reverse('url_campaign', args=[str(self.id)]) |
|
341 | return reverse('url_campaign', args=[str(self.id)]) | |
342 |
|
342 | |||
343 | def get_absolute_url_edit(self): |
|
343 | def get_absolute_url_edit(self): | |
344 | return reverse('url_edit_campaign', args=[str(self.id)]) |
|
344 | return reverse('url_edit_campaign', args=[str(self.id)]) | |
345 |
|
345 | |||
346 | def get_absolute_url_delete(self): |
|
346 | def get_absolute_url_delete(self): | |
347 | return reverse('url_delete_campaign', args=[str(self.id)]) |
|
347 | return reverse('url_delete_campaign', args=[str(self.id)]) | |
348 |
|
348 | |||
349 | def get_absolute_url_export(self): |
|
349 | def get_absolute_url_export(self): | |
350 | return reverse('url_export_campaign', args=[str(self.id)]) |
|
350 | return reverse('url_export_campaign', args=[str(self.id)]) | |
351 |
|
351 | |||
352 | def get_absolute_url_import(self): |
|
352 | def get_absolute_url_import(self): | |
353 | return reverse('url_import_campaign', args=[str(self.id)]) |
|
353 | return reverse('url_import_campaign', args=[str(self.id)]) | |
354 |
|
354 | |||
355 |
|
355 | |||
356 | class RunningExperiment(models.Model): |
|
356 | class RunningExperiment(models.Model): | |
357 | radar = models.OneToOneField('Location', on_delete=models.CASCADE) |
|
357 | radar = models.OneToOneField('Location', on_delete=models.CASCADE) | |
358 | running_experiment = models.ManyToManyField('Experiment', blank = True) |
|
358 | running_experiment = models.ManyToManyField('Experiment', blank = True) | |
359 | status = models.PositiveSmallIntegerField(default=0, choices=RADAR_STATES) |
|
359 | status = models.PositiveSmallIntegerField(default=0, choices=RADAR_STATES) | |
360 |
|
360 | |||
361 |
|
361 | |||
362 | class Experiment(models.Model): |
|
362 | class Experiment(models.Model): | |
363 |
|
363 | |||
364 | template = models.BooleanField(default=False) |
|
364 | template = models.BooleanField(default=False) | |
365 | name = models.CharField(max_length=40, default='', unique=True) |
|
365 | name = models.CharField(max_length=40, default='', unique=True) | |
366 | location = models.ForeignKey('Location', null=True, blank=True, on_delete=models.CASCADE) |
|
366 | location = models.ForeignKey('Location', null=True, blank=True, on_delete=models.CASCADE) | |
367 | freq = models.FloatField(verbose_name='Operating Freq. (MHz)', validators=[MinValueValidator(1), MaxValueValidator(10000)], default=49.9200) |
|
367 | freq = models.FloatField(verbose_name='Operating Freq. (MHz)', validators=[MinValueValidator(1), MaxValueValidator(10000)], default=49.9200) | |
368 | start_time = models.TimeField(default='00:00:00') |
|
368 | start_time = models.TimeField(default='00:00:00') | |
369 | end_time = models.TimeField(default='23:59:59') |
|
369 | end_time = models.TimeField(default='23:59:59') | |
370 | task = models.CharField(max_length=36, default='', blank=True, null=True) |
|
370 | task = models.CharField(max_length=36, default='', blank=True, null=True) | |
371 | status = models.PositiveSmallIntegerField(default=4, choices=EXP_STATES) |
|
371 | status = models.PositiveSmallIntegerField(default=4, choices=EXP_STATES) | |
372 | author = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE) |
|
372 | author = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE) | |
373 | hash = models.CharField(default='', max_length=64, null=True, blank=True) |
|
373 | hash = models.CharField(default='', max_length=64, null=True, blank=True) | |
374 |
|
374 | |||
375 | class Meta: |
|
375 | class Meta: | |
376 | db_table = 'db_experiments' |
|
376 | db_table = 'db_experiments' | |
377 | ordering = ('template', 'name') |
|
377 | ordering = ('template', 'name') | |
378 |
|
378 | |||
379 | def __str__(self): |
|
379 | def __str__(self): | |
380 | if self.template: |
|
380 | if self.template: | |
381 | return u'%s (template)' % (self.name) |
|
381 | return u'%s (template)' % (self.name) | |
382 | else: |
|
382 | else: | |
383 | return u'%s' % (self.name) |
|
383 | return u'%s' % (self.name) | |
384 |
|
384 | |||
385 | def jsonify(self): |
|
385 | def jsonify(self): | |
386 |
|
386 | |||
387 | data = {} |
|
387 | data = {} | |
388 |
|
388 | |||
389 | ignored = ('template') |
|
389 | ignored = ('template') | |
390 |
|
390 | |||
391 | for field in self._meta.fields: |
|
391 | for field in self._meta.fields: | |
392 | if field.name in ignored: |
|
392 | if field.name in ignored: | |
393 | continue |
|
393 | continue | |
394 | data[field.name] = field.value_from_object(self) |
|
394 | data[field.name] = field.value_from_object(self) | |
395 |
|
395 | |||
396 | data['start_time'] = data['start_time'].strftime('%H:%M:%S') |
|
396 | data['start_time'] = data['start_time'].strftime('%H:%M:%S') | |
397 | data['end_time'] = data['end_time'].strftime('%H:%M:%S') |
|
397 | data['end_time'] = data['end_time'].strftime('%H:%M:%S') | |
398 | data['location'] = self.location.name |
|
398 | data['location'] = self.location.name | |
399 | data['configurations'] = ['{}'.format(conf.pk) for |
|
399 | data['configurations'] = ['{}'.format(conf.pk) for | |
400 | conf in Configuration.objects.filter(experiment=self, type=0)] |
|
400 | conf in Configuration.objects.filter(experiment=self, type=0)] | |
401 |
|
401 | |||
402 | return data |
|
402 | return data | |
403 |
|
403 | |||
404 | @property |
|
404 | @property | |
405 | def radar_system(self): |
|
405 | def radar_system(self): | |
406 | return self.location |
|
406 | return self.location | |
407 |
|
407 | |||
408 | def clone(self, **kwargs): |
|
408 | def clone(self, **kwargs): | |
409 |
|
409 | |||
410 | confs = Configuration.objects.filter(experiment=self, type=0) |
|
410 | confs = Configuration.objects.filter(experiment=self, type=0) | |
411 | self.pk = None |
|
411 | self.pk = None | |
412 | self.name = '{}_{:%y%m%d%H%M%S}'.format(self.name, datetime.now()) |
|
412 | self.name = '{}_{:%y%m%d%H%M%S}'.format(self.name, datetime.now()) | |
413 | for attr, value in kwargs.items(): |
|
413 | for attr, value in kwargs.items(): | |
414 | setattr(self, attr, value) |
|
414 | setattr(self, attr, value) | |
415 |
|
415 | |||
416 | self.save() |
|
416 | self.save() | |
417 |
|
417 | |||
418 | for conf in confs: |
|
418 | for conf in confs: | |
419 | conf.clone(experiment=self, template=False) |
|
419 | conf.clone(experiment=self, template=False) | |
420 |
|
420 | |||
421 | return self |
|
421 | return self | |
422 |
|
422 | |||
423 | def start(self): |
|
423 | def start(self): | |
424 | ''' |
|
424 | ''' | |
425 | Configure and start experiments's devices |
|
425 | Configure and start experiments's devices | |
426 | ABS-CGS-DDS-RC-JARS |
|
426 | ABS-CGS-DDS-RC-JARS | |
427 | ''' |
|
427 | ''' | |
428 |
|
428 | |||
429 | confs = [] |
|
429 | confs = [] | |
430 | allconfs = Configuration.objects.filter(experiment=self, type = 0).order_by('-device__device_type__sequence') |
|
430 | allconfs = Configuration.objects.filter(experiment=self, type = 0).order_by('-device__device_type__sequence') | |
431 | rc_mix = [conf for conf in allconfs if conf.device.device_type.name=='rc' and conf.mix] |
|
431 | rc_mix = [conf for conf in allconfs if conf.device.device_type.name=='rc' and conf.mix] | |
432 | if rc_mix: |
|
432 | if rc_mix: | |
433 | for conf in allconfs: |
|
433 | for conf in allconfs: | |
434 | if conf.device.device_type.name == 'rc' and not conf.mix: |
|
434 | if conf.device.device_type.name == 'rc' and not conf.mix: | |
435 | continue |
|
435 | continue | |
436 | confs.append(conf) |
|
436 | confs.append(conf) | |
437 | else: |
|
437 | else: | |
438 | confs = allconfs |
|
438 | confs = allconfs | |
439 |
|
439 | |||
440 | print("confs: ",confs) |
|
440 | print("confs: ",confs) | |
441 | #try: |
|
441 | #try: | |
442 | for conf in confs: |
|
442 | for conf in confs: | |
443 | print("conf->",conf) |
|
443 | print("conf->",conf) | |
444 | conf.stop_device() |
|
444 | conf.stop_device() | |
445 | conf.write_device() |
|
445 | conf.write_device() | |
446 | conf.device.conf_active = conf.pk |
|
446 | conf.device.conf_active = conf.pk | |
447 | conf.device.save() |
|
447 | conf.device.save() | |
448 | conf.start_device() |
|
448 | conf.start_device() | |
449 | time.sleep(1) |
|
449 | time.sleep(1) | |
450 | #except: |
|
450 | #except: | |
451 | #return 0 |
|
451 | #return 0 | |
452 | return 2 |
|
452 | return 2 | |
453 |
|
453 | |||
454 |
|
454 | |||
455 | def stop(self): |
|
455 | def stop(self): | |
456 | ''' |
|
456 | ''' | |
457 | Stop experiments's devices |
|
457 | Stop experiments's devices | |
458 | DDS-JARS-RC-CGS-ABS |
|
458 | DDS-JARS-RC-CGS-ABS | |
459 | ''' |
|
459 | ''' | |
460 |
|
460 | |||
461 | confs = Configuration.objects.filter(experiment=self, type = 0).order_by('device__device_type__sequence') |
|
461 | confs = Configuration.objects.filter(experiment=self, type = 0).order_by('device__device_type__sequence') | |
462 | confs = confs.exclude(device__device_type__name='cgs') |
|
462 | confs = confs.exclude(device__device_type__name='cgs') | |
463 | try: |
|
463 | try: | |
464 | for conf in confs: |
|
464 | for conf in confs: | |
465 | conf.stop_device() |
|
465 | conf.stop_device() | |
466 | except: |
|
466 | except: | |
467 | return 0 |
|
467 | return 0 | |
468 | return 1 |
|
468 | return 1 | |
469 |
|
469 | |||
470 | def get_status(self): |
|
470 | def get_status(self): | |
471 |
|
471 | |||
472 | if self.status == 3: |
|
472 | if self.status == 3: | |
473 | return |
|
473 | return | |
474 |
|
474 | |||
475 | confs = Configuration.objects.filter(experiment=self, type=0) |
|
475 | confs = Configuration.objects.filter(experiment=self, type=0) | |
476 |
|
476 | |||
477 | for conf in confs: |
|
477 | for conf in confs: | |
478 | conf.status_device() |
|
478 | conf.status_device() | |
479 |
|
479 | |||
480 | total = confs.aggregate(models.Sum('device__status'))['device__status__sum'] |
|
480 | total = confs.aggregate(models.Sum('device__status'))['device__status__sum'] | |
481 |
|
481 | |||
482 | if total==2*confs.count(): |
|
482 | if total==2*confs.count(): | |
483 | status = 1 |
|
483 | status = 1 | |
484 | elif total == 3*confs.count(): |
|
484 | elif total == 3*confs.count(): | |
485 | status = 2 |
|
485 | status = 2 | |
486 | else: |
|
486 | else: | |
487 | status = 0 |
|
487 | status = 0 | |
488 |
|
488 | |||
489 | self.status = status |
|
489 | self.status = status | |
490 | self.save() |
|
490 | self.save() | |
491 |
|
491 | |||
492 | def status_color(self): |
|
492 | def status_color(self): | |
493 | color = 'muted' |
|
493 | color = 'muted' | |
494 | if self.status == 0: |
|
494 | if self.status == 0: | |
495 | color = "danger" |
|
495 | color = "danger" | |
496 | elif self.status == 1: |
|
496 | elif self.status == 1: | |
497 | color = "warning" |
|
497 | color = "warning" | |
498 | elif self.status == 2: |
|
498 | elif self.status == 2: | |
499 | color = "success" |
|
499 | color = "success" | |
500 | elif self.status == 3: |
|
500 | elif self.status == 3: | |
501 | color = "info" |
|
501 | color = "info" | |
502 |
|
502 | |||
503 | return color |
|
503 | return color | |
504 |
|
504 | |||
505 | def parms_to_dict(self): |
|
505 | def parms_to_dict(self): | |
506 |
|
506 | |||
507 | params = Params({}) |
|
507 | params = Params({}) | |
508 | params.add(self.jsonify(), 'experiments') |
|
508 | params.add(self.jsonify(), 'experiments') | |
509 |
|
509 | |||
510 | configurations = Configuration.objects.filter(experiment=self, type=0) |
|
510 | configurations = Configuration.objects.filter(experiment=self, type=0) | |
511 |
|
511 | |||
512 | for conf in configurations: |
|
512 | for conf in configurations: | |
513 | params.add(conf.jsonify(), 'configurations') |
|
513 | params.add(conf.jsonify(), 'configurations') | |
514 | if conf.device.device_type.name=='rc': |
|
514 | if conf.device.device_type.name=='rc': | |
515 | for line in conf.get_lines(): |
|
515 | for line in conf.get_lines(): | |
516 | params.add(line.jsonify(), 'lines') |
|
516 | params.add(line.jsonify(), 'lines') | |
517 |
|
517 | |||
518 | return params.data |
|
518 | return params.data | |
519 |
|
519 | |||
520 | def dict_to_parms(self, parms, CONF_MODELS, id_exp=None): |
|
520 | def dict_to_parms(self, parms, CONF_MODELS, id_exp=None): | |
521 |
|
521 | |||
522 | configurations = Configuration.objects.filter(experiment=self) |
|
522 | configurations = Configuration.objects.filter(experiment=self) | |
523 |
|
523 | |||
524 | if id_exp is not None: |
|
524 | if id_exp is not None: | |
525 | exp_parms = parms['experiments']['byId'][id_exp] |
|
525 | exp_parms = parms['experiments']['byId'][id_exp] | |
526 | else: |
|
526 | else: | |
527 | exp_parms = parms['experiments']['byId'][parms['experiments']['allIds'][0]] |
|
527 | exp_parms = parms['experiments']['byId'][parms['experiments']['allIds'][0]] | |
528 |
|
528 | |||
529 | if configurations: |
|
529 | if configurations: | |
530 | for configuration in configurations: |
|
530 | for configuration in configurations: | |
531 | configuration.delete() |
|
531 | configuration.delete() | |
532 |
|
532 | |||
533 | for id_conf in exp_parms['configurations']: |
|
533 | for id_conf in exp_parms['configurations']: | |
534 | conf_parms = parms['configurations']['byId'][id_conf] |
|
534 | conf_parms = parms['configurations']['byId'][id_conf] | |
535 | device = Device.objects.filter(device_type__name=conf_parms['device_type'])[0] |
|
535 | device = Device.objects.filter(device_type__name=conf_parms['device_type'])[0] | |
536 | model = CONF_MODELS[conf_parms['device_type']] |
|
536 | model = CONF_MODELS[conf_parms['device_type']] | |
537 | conf = model( |
|
537 | conf = model( | |
538 | experiment = self, |
|
538 | experiment = self, | |
539 | device = device, |
|
539 | device = device, | |
540 | ) |
|
540 | ) | |
541 | conf.dict_to_parms(parms, id=id_conf) |
|
541 | conf.dict_to_parms(parms, id=id_conf) | |
542 |
|
542 | |||
543 |
|
543 | |||
544 | location, created = Location.objects.get_or_create(name=exp_parms['location']) |
|
544 | location, created = Location.objects.get_or_create(name=exp_parms['location']) | |
545 | self.name = '{}-{}'.format(exp_parms['name'], datetime.now().strftime('%y%m%d')) |
|
545 | self.name = '{}-{}'.format(exp_parms['name'], datetime.now().strftime('%y%m%d')) | |
546 | self.location = location |
|
546 | self.location = location | |
547 | self.start_time = exp_parms['start_time'] |
|
547 | self.start_time = exp_parms['start_time'] | |
548 | self.end_time = exp_parms['end_time'] |
|
548 | self.end_time = exp_parms['end_time'] | |
549 | self.save() |
|
549 | self.save() | |
550 |
|
550 | |||
551 | return self |
|
551 | return self | |
552 |
|
552 | |||
553 | def get_absolute_url(self): |
|
553 | def get_absolute_url(self): | |
554 | return reverse('url_experiment', args=[str(self.id)]) |
|
554 | return reverse('url_experiment', args=[str(self.id)]) | |
555 |
|
555 | |||
556 | def get_absolute_url_edit(self): |
|
556 | def get_absolute_url_edit(self): | |
557 | return reverse('url_edit_experiment', args=[str(self.id)]) |
|
557 | return reverse('url_edit_experiment', args=[str(self.id)]) | |
558 |
|
558 | |||
559 | def get_absolute_url_delete(self): |
|
559 | def get_absolute_url_delete(self): | |
560 | return reverse('url_delete_experiment', args=[str(self.id)]) |
|
560 | return reverse('url_delete_experiment', args=[str(self.id)]) | |
561 |
|
561 | |||
562 | def get_absolute_url_import(self): |
|
562 | def get_absolute_url_import(self): | |
563 | return reverse('url_import_experiment', args=[str(self.id)]) |
|
563 | return reverse('url_import_experiment', args=[str(self.id)]) | |
564 |
|
564 | |||
565 | def get_absolute_url_export(self): |
|
565 | def get_absolute_url_export(self): | |
566 | return reverse('url_export_experiment', args=[str(self.id)]) |
|
566 | return reverse('url_export_experiment', args=[str(self.id)]) | |
567 |
|
567 | |||
568 | def get_absolute_url_start(self): |
|
568 | def get_absolute_url_start(self): | |
569 | return reverse('url_start_experiment', args=[str(self.id)]) |
|
569 | return reverse('url_start_experiment', args=[str(self.id)]) | |
570 |
|
570 | |||
571 | def get_absolute_url_stop(self): |
|
571 | def get_absolute_url_stop(self): | |
572 | return reverse('url_stop_experiment', args=[str(self.id)]) |
|
572 | return reverse('url_stop_experiment', args=[str(self.id)]) | |
573 |
|
573 | |||
574 |
|
574 | |||
575 | class Configuration(PolymorphicModel): |
|
575 | class Configuration(PolymorphicModel): | |
576 |
|
576 | |||
577 | template = models.BooleanField(default=False) |
|
577 | template = models.BooleanField(default=False) | |
578 | # name = models.CharField(verbose_name="Configuration Name", max_length=40, default='') |
|
578 | # name = models.CharField(verbose_name="Configuration Name", max_length=40, default='') | |
579 | device = models.ForeignKey('Device', verbose_name='Device', null=True, on_delete=models.CASCADE) |
|
579 | device = models.ForeignKey('Device', verbose_name='Device', null=True, on_delete=models.CASCADE) | |
580 | label = models.CharField(verbose_name="Label", max_length=40, default='', blank=True, null=True) |
|
580 | label = models.CharField(verbose_name="Label", max_length=40, default='', blank=True, null=True) | |
581 | experiment = models.ForeignKey('Experiment', verbose_name='Experiment', null=True, blank=True, on_delete=models.CASCADE) |
|
581 | experiment = models.ForeignKey('Experiment', verbose_name='Experiment', null=True, blank=True, on_delete=models.CASCADE) | |
582 | type = models.PositiveSmallIntegerField(default=0, choices=CONF_TYPES) |
|
582 | type = models.PositiveSmallIntegerField(default=0, choices=CONF_TYPES) | |
583 | created_date = models.DateTimeField(auto_now_add=True) |
|
583 | created_date = models.DateTimeField(auto_now_add=True) | |
584 | programmed_date = models.DateTimeField(auto_now=True) |
|
584 | programmed_date = models.DateTimeField(auto_now=True) | |
585 | parameters = models.TextField(default='{}') |
|
585 | parameters = models.TextField(default='{}') | |
586 | author = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE) |
|
586 | author = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE) | |
587 | hash = models.CharField(default='', max_length=64, null=True, blank=True) |
|
587 | hash = models.CharField(default='', max_length=64, null=True, blank=True) | |
588 | message = "" |
|
588 | message = "" | |
589 |
|
589 | |||
590 | class Meta: |
|
590 | class Meta: | |
591 | db_table = 'db_configurations' |
|
591 | db_table = 'db_configurations' | |
592 | ordering = ('device__device_type__name',) |
|
592 | ordering = ('device__device_type__name',) | |
593 |
|
593 | |||
594 | def __str__(self): |
|
594 | def __str__(self): | |
595 |
|
595 | |||
596 | ret = u'{} '.format(self.device.device_type.name.upper()) |
|
596 | ret = u'{} '.format(self.device.device_type.name.upper()) | |
597 |
|
597 | |||
598 | if 'mix' in [f.name for f in self._meta.get_fields()]: |
|
598 | if 'mix' in [f.name for f in self._meta.get_fields()]: | |
599 | if self.mix: |
|
599 | if self.mix: | |
600 | ret = '{} MIX '.format(self.device.device_type.name.upper()) |
|
600 | ret = '{} MIX '.format(self.device.device_type.name.upper()) | |
601 |
|
601 | |||
602 | if 'label' in [f.name for f in self._meta.get_fields()]: |
|
602 | if 'label' in [f.name for f in self._meta.get_fields()]: | |
603 | ret += '{}'.format(self.label) |
|
603 | ret += '{}'.format(self.label) | |
604 |
|
604 | |||
605 | if self.template: |
|
605 | if self.template: | |
606 | ret += ' (template)' |
|
606 | ret += ' (template)' | |
607 |
|
607 | |||
608 | return ret |
|
608 | return ret | |
609 |
|
609 | |||
610 | @property |
|
610 | @property | |
611 | def name(self): |
|
611 | def name(self): | |
612 |
|
612 | |||
613 | return str(self) |
|
613 | return str(self) | |
614 |
|
614 | |||
615 | def jsonify(self): |
|
615 | def jsonify(self): | |
616 |
|
616 | |||
617 | data = {} |
|
617 | data = {} | |
618 |
|
618 | |||
619 | ignored = ('type', 'polymorphic_ctype', 'configuration_ptr', |
|
619 | ignored = ('type', 'polymorphic_ctype', 'configuration_ptr', | |
620 | 'created_date', 'programmed_date', 'template', 'device', |
|
620 | 'created_date', 'programmed_date', 'template', 'device', | |
621 | 'experiment') |
|
621 | 'experiment') | |
622 |
|
622 | |||
623 | for field in self._meta.fields: |
|
623 | for field in self._meta.fields: | |
624 | if field.name in ignored: |
|
624 | if field.name in ignored: | |
625 | continue |
|
625 | continue | |
626 | data[field.name] = field.value_from_object(self) |
|
626 | data[field.name] = field.value_from_object(self) | |
627 |
|
627 | |||
628 | data['device_type'] = self.device.device_type.name |
|
628 | data['device_type'] = self.device.device_type.name | |
629 |
|
629 | |||
630 | if self.device.device_type.name == 'rc': |
|
630 | if self.device.device_type.name == 'rc': | |
631 | data['lines'] = ['{}'.format(line.pk) for line in self.get_lines()] |
|
631 | data['lines'] = ['{}'.format(line.pk) for line in self.get_lines()] | |
632 | data['delays'] = self.get_delays() |
|
632 | data['delays'] = self.get_delays() | |
633 | data['pulses'] = self.get_pulses() |
|
633 | data['pulses'] = self.get_pulses() | |
634 |
|
634 | |||
635 | elif self.device.device_type.name == 'jars': |
|
635 | elif self.device.device_type.name == 'jars': | |
636 | data['decode_type'] = DECODE_TYPE[self.decode_data][1] |
|
636 | data['decode_type'] = DECODE_TYPE[self.decode_data][1] | |
637 |
|
637 | |||
638 | elif self.device.device_type.name == 'dds': |
|
638 | elif self.device.device_type.name == 'dds': | |
639 | data['frequencyA_Mhz'] = float(data['frequencyA_Mhz']) |
|
639 | data['frequencyA_Mhz'] = float(data['frequencyA_Mhz']) | |
640 | data['frequencyB_Mhz'] = float(data['frequencyB_Mhz']) |
|
640 | data['frequencyB_Mhz'] = float(data['frequencyB_Mhz']) | |
641 | data['phaseA'] = dds_data.phase_to_binary(data['phaseA_degrees']) |
|
641 | data['phaseA'] = dds_data.phase_to_binary(data['phaseA_degrees']) | |
642 | data['phaseB'] = dds_data.phase_to_binary(data['phaseB_degrees']) |
|
642 | data['phaseB'] = dds_data.phase_to_binary(data['phaseB_degrees']) | |
643 |
|
643 | |||
644 | elif self.device.device_type.name == 'dds_rest': |
|
644 | elif self.device.device_type.name == 'dds_rest': | |
645 | data['frequencyA_Mhz'] = float(data['frequencyA_Mhz']) |
|
645 | data['frequencyA_Mhz'] = float(data['frequencyA_Mhz']) | |
646 | data['frequencyB_Mhz'] = float(data['frequencyB_Mhz']) |
|
646 | data['frequencyB_Mhz'] = float(data['frequencyB_Mhz']) | |
647 | data['phaseA'] = dds_data.phase_to_binary(data['phaseA_degrees']) |
|
647 | data['phaseA'] = dds_data.phase_to_binary(data['phaseA_degrees']) | |
648 | data['phaseB'] = dds_data.phase_to_binary(data['phaseB_degrees']) |
|
648 | data['phaseB'] = dds_data.phase_to_binary(data['phaseB_degrees']) | |
649 | data['delta_frequency_Mhz'] = float(data['delta_frequency_Mhz'] or 0.00) |
|
649 | data['delta_frequency_Mhz'] = float(data['delta_frequency_Mhz'] or 0.00) | |
650 | data['update_clock_Mhz'] = float(data['update_clock_Mhz'] or 0.00) |
|
650 | data['update_clock_Mhz'] = float(data['update_clock_Mhz'] or 0.00) | |
651 | data['ramp_rate_clock_Mhz'] = float(data['ramp_rate_clock_Mhz'] or 0.0) |
|
651 | data['ramp_rate_clock_Mhz'] = float(data['ramp_rate_clock_Mhz'] or 0.0) | |
652 | return data |
|
652 | return data | |
653 |
|
653 | |||
654 | def clone(self, **kwargs): |
|
654 | def clone(self, **kwargs): | |
655 |
|
655 | |||
656 | self.pk = None |
|
656 | self.pk = None | |
657 | self.id = None |
|
657 | self.id = None | |
658 | for attr, value in kwargs.items(): |
|
658 | for attr, value in kwargs.items(): | |
659 | setattr(self, attr, value) |
|
659 | setattr(self, attr, value) | |
660 |
|
660 | |||
661 | self.save() |
|
661 | self.save() | |
662 |
|
662 | |||
663 | return self |
|
663 | return self | |
664 |
|
664 | |||
665 | def parms_to_dict(self): |
|
665 | def parms_to_dict(self): | |
666 |
|
666 | |||
667 | params = Params({}) |
|
667 | params = Params({}) | |
668 | params.add(self.jsonify(), 'configurations') |
|
668 | params.add(self.jsonify(), 'configurations') | |
669 |
|
669 | |||
670 | if self.device.device_type.name=='rc': |
|
670 | if self.device.device_type.name=='rc': | |
671 | for line in self.get_lines(): |
|
671 | for line in self.get_lines(): | |
672 | params.add(line.jsonify(), 'lines') |
|
672 | params.add(line.jsonify(), 'lines') | |
673 |
|
673 | |||
674 | return params.data |
|
674 | return params.data | |
675 |
|
675 | |||
676 | def parms_to_text(self): |
|
676 | def parms_to_text(self): | |
677 |
|
677 | |||
678 | raise NotImplementedError("This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper()) |
|
678 | raise NotImplementedError("This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper()) | |
679 |
|
679 | |||
680 |
|
680 | |||
681 | def parms_to_binary(self): |
|
681 | def parms_to_binary(self): | |
682 |
|
682 | |||
683 | raise NotImplementedError("This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper()) |
|
683 | raise NotImplementedError("This method should be implemented in %s Configuration model" %str(self.device.device_type.name).upper()) | |
684 |
|
684 | |||
685 |
|
685 | |||
686 | def dict_to_parms(self, parameters, id=None): |
|
686 | def dict_to_parms(self, parameters, id=None): | |
687 |
|
687 | |||
688 | params = Params(parameters) |
|
688 | params = Params(parameters) | |
689 |
|
689 | |||
690 | if id: |
|
690 | if id: | |
691 | data = params.get_conf(id_conf=id) |
|
691 | data = params.get_conf(id_conf=id) | |
692 | else: |
|
692 | else: | |
693 | data = params.get_conf(dtype=self.device.device_type.name) |
|
693 | data = params.get_conf(dtype=self.device.device_type.name) | |
694 |
|
694 | |||
695 | if data['device_type']=='rc': |
|
695 | if data['device_type']=='rc': | |
696 | self.clean_lines() |
|
696 | self.clean_lines() | |
697 | lines = data.pop('lines', None) |
|
697 | lines = data.pop('lines', None) | |
698 | for line_id in lines: |
|
698 | for line_id in lines: | |
699 | pass |
|
699 | pass | |
700 |
|
700 | |||
701 | for key, value in data.items(): |
|
701 | for key, value in data.items(): | |
702 | if key not in ('id', 'device_type'): |
|
702 | if key not in ('id', 'device_type'): | |
703 | setattr(self, key, value) |
|
703 | setattr(self, key, value) | |
704 |
|
704 | |||
705 | self.save() |
|
705 | self.save() | |
706 |
|
706 | |||
707 |
|
707 | |||
708 | def export_to_file(self, format="json"): |
|
708 | def export_to_file(self, format="json"): | |
709 |
|
709 | |||
710 | content_type = '' |
|
710 | content_type = '' | |
711 |
|
711 | |||
712 | if format == 'racp': |
|
712 | if format == 'racp': | |
713 | content_type = 'text/plain' |
|
713 | content_type = 'text/plain' | |
714 | filename = '%s_%s.%s' %(self.device.device_type.name, self.name, 'racp') |
|
714 | filename = '%s_%s.%s' %(self.device.device_type.name, self.name, 'racp') | |
715 | content = self.parms_to_text(file_format = 'racp') |
|
715 | content = self.parms_to_text(file_format = 'racp') | |
716 |
|
716 | |||
717 | if format == 'text': |
|
717 | if format == 'text': | |
718 | content_type = 'text/plain' |
|
718 | content_type = 'text/plain' | |
719 | filename = '%s_%s.%s' %(self.device.device_type.name, self.name, self.device.device_type.name) |
|
719 | filename = '%s_%s.%s' %(self.device.device_type.name, self.name, self.device.device_type.name) | |
720 | content = self.parms_to_text() |
|
720 | content = self.parms_to_text() | |
721 |
|
721 | |||
722 | if format == 'binary': |
|
722 | if format == 'binary': | |
723 | content_type = 'application/octet-stream' |
|
723 | content_type = 'application/octet-stream' | |
724 | filename = '%s_%s.bin' %(self.device.device_type.name, self.name) |
|
724 | filename = '%s_%s.bin' %(self.device.device_type.name, self.name) | |
725 | content = self.parms_to_binary() |
|
725 | content = self.parms_to_binary() | |
726 |
|
726 | |||
727 | if not content_type: |
|
727 | if not content_type: | |
728 | content_type = 'application/json' |
|
728 | content_type = 'application/json' | |
729 | filename = '%s_%s.json' %(self.device.device_type.name, self.name) |
|
729 | filename = '%s_%s.json' %(self.device.device_type.name, self.name) | |
730 | content = json.dumps(self.parms_to_dict(), indent=2) |
|
730 | content = json.dumps(self.parms_to_dict(), indent=2) | |
731 |
|
731 | |||
732 | fields = {'content_type':content_type, |
|
732 | fields = {'content_type':content_type, | |
733 | 'filename':filename, |
|
733 | 'filename':filename, | |
734 | 'content':content |
|
734 | 'content':content | |
735 | } |
|
735 | } | |
736 |
|
736 | |||
737 | return fields |
|
737 | return fields | |
738 |
|
738 | |||
739 | def import_from_file(self, fp): |
|
739 | def import_from_file(self, fp): | |
740 |
|
740 | |||
741 | parms = {} |
|
741 | parms = {} | |
742 |
|
742 | |||
743 | path, ext = os.path.splitext(fp.name) |
|
743 | path, ext = os.path.splitext(fp.name) | |
744 |
|
744 | |||
745 | if ext == '.json': |
|
745 | if ext == '.json': | |
746 | parms = json.load(fp) |
|
746 | parms = json.load(fp) | |
747 |
|
747 | |||
748 | if ext == '.dds': |
|
748 | if ext == '.dds': | |
749 | lines = fp.readlines() |
|
749 | lines = fp.readlines() | |
750 | parms = dds_data.text_to_dict(lines) |
|
750 | parms = dds_data.text_to_dict(lines) | |
751 |
|
751 | |||
752 | if ext == '.racp': |
|
752 | if ext == '.racp': | |
753 | if self.device.device_type.name == 'jars': |
|
753 | if self.device.device_type.name == 'jars': | |
754 | parms = RacpFile(fp).to_dict() |
|
754 | parms = RacpFile(fp).to_dict() | |
755 | parms['filter_parms'] = json.loads(self.filter_parms) |
|
755 | parms['filter_parms'] = json.loads(self.filter_parms) | |
756 | return parms |
|
756 | return parms | |
757 | parms = RCFile(fp).to_dict() |
|
757 | parms = RCFile(fp).to_dict() | |
758 |
|
758 | |||
759 | return parms |
|
759 | return parms | |
760 |
|
760 | |||
761 | def status_device(self): |
|
761 | def status_device(self): | |
762 |
|
762 | |||
763 | self.message = 'Function not implemented' |
|
763 | self.message = 'Function not implemented' | |
764 | return False |
|
764 | return False | |
765 |
|
765 | |||
766 |
|
766 | |||
767 | def stop_device(self): |
|
767 | def stop_device(self): | |
768 |
|
768 | |||
769 | self.message = 'Function not implemented' |
|
769 | self.message = 'Function not implemented' | |
770 | return False |
|
770 | return False | |
771 |
|
771 | |||
772 |
|
772 | |||
773 | def start_device(self): |
|
773 | def start_device(self): | |
774 |
|
774 | |||
775 | self.message = 'Function not implemented' |
|
775 | self.message = 'Function not implemented' | |
776 | return False |
|
776 | return False | |
777 |
|
777 | |||
778 |
|
778 | |||
779 | def write_device(self, parms): |
|
779 | def write_device(self, parms): | |
780 |
|
780 | |||
781 | self.message = 'Function not implemented' |
|
781 | self.message = 'Function not implemented' | |
782 | return False |
|
782 | return False | |
783 |
|
783 | |||
|
784 | def write_device_mqtt(self, parms): | |||
|
785 | ||||
|
786 | self.message = 'Function not implemented' | |||
|
787 | return False | |||
784 |
|
788 | |||
785 | def read_device(self): |
|
789 | def read_device(self): | |
786 |
|
790 | |||
787 | self.message = 'Function not implemented' |
|
791 | self.message = 'Function not implemented' | |
788 | return False |
|
792 | return False | |
789 |
|
793 | |||
790 |
|
794 | |||
791 | def get_absolute_url(self): |
|
795 | def get_absolute_url(self): | |
792 | return reverse('url_%s_conf' % self.device.device_type.name, args=[str(self.id)]) |
|
796 | return reverse('url_%s_conf' % self.device.device_type.name, args=[str(self.id)]) | |
793 |
|
797 | |||
794 | def get_absolute_url_edit(self): |
|
798 | def get_absolute_url_edit(self): | |
795 | return reverse('url_edit_%s_conf' % self.device.device_type.name, args=[str(self.id)]) |
|
799 | return reverse('url_edit_%s_conf' % self.device.device_type.name, args=[str(self.id)]) | |
796 |
|
800 | |||
797 | def get_absolute_url_delete(self): |
|
801 | def get_absolute_url_delete(self): | |
798 | return reverse('url_delete_dev_conf', args=[str(self.id)]) |
|
802 | return reverse('url_delete_dev_conf', args=[str(self.id)]) | |
799 |
|
803 | |||
800 | def get_absolute_url_import(self): |
|
804 | def get_absolute_url_import(self): | |
801 | return reverse('url_import_dev_conf', args=[str(self.id)]) |
|
805 | return reverse('url_import_dev_conf', args=[str(self.id)]) | |
802 |
|
806 | |||
803 | def get_absolute_url_export(self): |
|
807 | def get_absolute_url_export(self): | |
804 | return reverse('url_export_dev_conf', args=[str(self.id)]) |
|
808 | return reverse('url_export_dev_conf', args=[str(self.id)]) | |
805 |
|
809 | |||
806 | def get_absolute_url_write(self): |
|
810 | def get_absolute_url_write(self): | |
807 | return reverse('url_write_dev_conf', args=[str(self.id)]) |
|
811 | return reverse('url_write_dev_conf', args=[str(self.id)]) | |
808 |
|
812 | |||
809 | def get_absolute_url_write_mqtt(self): |
|
813 | def get_absolute_url_write_mqtt(self): | |
810 | return reverse('url_write_mqtt_dev_conf', args=[str(self.id)]) |
|
814 | return reverse('url_write_mqtt_dev_conf', args=[str(self.id)]) | |
811 |
|
815 | |||
812 | def get_absolute_url_read(self): |
|
816 | def get_absolute_url_read(self): | |
813 | return reverse('url_read_dev_conf', args=[str(self.id)]) |
|
817 | return reverse('url_read_dev_conf', args=[str(self.id)]) | |
814 |
|
818 | |||
815 | def get_absolute_url_start(self): |
|
819 | def get_absolute_url_start(self): | |
816 | return reverse('url_start_dev_conf', args=[str(self.id)]) |
|
820 | return reverse('url_start_dev_conf', args=[str(self.id)]) | |
817 |
|
821 | |||
818 | def get_absolute_url_stop(self): |
|
822 | def get_absolute_url_stop(self): | |
819 | return reverse('url_stop_dev_conf', args=[str(self.id)]) |
|
823 | return reverse('url_stop_dev_conf', args=[str(self.id)]) | |
820 |
|
824 | |||
|
825 | def get_absolute_url_stop_mqtt(self): | |||
|
826 | return reverse('url_stop_mqtt_dev_conf', args=[str(self.id)]) | |||
|
827 | ||||
821 | def get_absolute_url_status(self): |
|
828 | def get_absolute_url_status(self): | |
822 | return reverse('url_status_dev_conf', args=[str(self.id)]) |
|
829 | return reverse('url_status_dev_conf', args=[str(self.id)]) |
@@ -1,44 +1,62 | |||||
1 | import os |
|
1 | import os | |
2 | import paho.mqtt.client as mqtt |
|
2 | import paho.mqtt.client as mqtt | |
3 | from radarsys import settings |
|
3 | from radarsys import settings | |
4 | from radarsys.socketconfig import sio as sio |
|
4 | from radarsys.socketconfig import sio as sio | |
5 |
|
5 | |||
6 |
|
6 | |||
7 | lista_ack=[ |
|
7 | lista_ack=[ | |
8 | chr( 33 ), chr( 34 ), chr( 35 ), chr( 36 ), chr( 37 ), chr( 38 ), chr( 39 ), chr( 40 ), chr( 41 ), chr( 42 ), chr( 43 ), chr( 44 ), chr( 45 ), chr( 46 ), chr( 47 ), chr( 48 ), chr( 49 ), chr( 50 ), chr( 51 ), chr( 52 ), chr( 53 ), chr( 54 ), chr( 55 ), chr( 56 ), chr( 57 ), chr( 58 ), chr( 59 ), chr( 60 ), chr( 61 ), chr( 62 ), chr( 63 ), chr( 64 ), chr( 65 ), chr( 66 ), chr( 67 ), chr( 68 ), chr( 69 ), chr( 70 ), chr( 71 ), chr( 72 ), chr( 73 ), chr( 74 ), chr( 75 ), chr( 76 ), chr( 77 ), chr( 78 ), chr( 79 ), chr( 80 ), chr( 81 ), chr( 82 ), chr( 83 ), chr( 84 ), chr( 85 ), chr( 86 ), chr( 87 ), chr( 88 ), chr( 89 ), chr( 90 ), chr( 91 ), chr( 92 ), chr( 93 ), chr( 94 ), chr( 95 ), chr( 96 ) |
|
8 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128 | |
9 | ] |
|
|||
10 |
|
||||
11 | lista_ack_dismatching=[ |
|
|||
12 | chr( 97 ), chr( 98 ), chr( 99 ), chr( 100 ), chr( 101 ), chr( 102 ), chr( 103 ), chr( 104 ), chr( 105 ), chr( 106 ), chr( 107 ), chr( 108 ), chr( 109 ), chr( 110 ), chr( 111 ), chr( 112 ), chr( 113 ), chr( 114 ), chr( 115 ), chr( 116 ), chr( 117 ), chr( 118 ), chr( 119 ), chr( 120 ), chr( 121 ), chr( 122 ), chr( 123 ), chr( 124 ), chr( 125 ), chr( 126 ), chr( 127 ), chr( 128 ), chr( 129 ), chr( 130 ), chr( 131 ), chr( 132 ), chr( 133 ), chr( 134 ), chr( 135 ), chr( 136 ), chr( 137 ), chr( 138 ), chr( 139 ), chr( 140 ), chr( 141 ), chr( 142 ), chr( 143 ), chr( 144 ), chr( 145 ), chr( 146 ), chr( 147 ), chr( 148 ), chr( 149 ), chr( 150 ), chr( 151 ), chr( 152 ), chr( 153 ), chr( 154 ), chr( 155 ), chr( 156 ), chr( 157 ), chr( 158 ), chr( 159 ), chr(160) |
|
|||
13 | ] |
|
9 | ] | |
14 |
|
10 | |||
15 |
|
11 | |||
16 |
|
12 | |||
17 | def on_connect(mqtt_client, userdata, flags, rc): |
|
13 | def on_connect(mqtt_client, userdata, flags, rc): | |
18 | if rc == 0: |
|
14 | if rc == 0: | |
19 | print('Connected successfully') |
|
15 | print('Connected successfully') | |
20 | mqtt_client.subscribe(os.environ.get('TOPIC_ABS_ACK','abs/beams_ack')) |
|
16 | mqtt_client.subscribe(os.environ.get('TOPIC_ABS_ACK','abs/beams_ack')) | |
21 | else: |
|
17 | else: | |
22 | print('Bad connection. Code:', rc) |
|
18 | print('Bad connection. Code:', rc) | |
23 |
|
19 | |||
24 | def on_message(mqtt_client, userdata, msg): |
|
20 | def on_message(mqtt_client, userdata, msg): | |
25 | # print(f'Received message on topic: {msg.topic} with payload: {msg.payload}', flush=True) |
|
21 | # print(f'Received message on topic: {msg.topic} with payload: {msg.payload}', flush=True) | |
26 | # message= str(msg.payload) |
|
22 | # message= str(msg.payload) | |
27 | # sio.emit('abs_ws',data={'msg':message}) |
|
23 | # sio.emit('abs_ws',data={'msg':message}) | |
28 | print("HOLA",flush=True) |
|
24 | #print("HOLA",flush=True) | |
29 | # message=msg.payload[1] |
|
25 | # message=msg.payload[1] | |
30 | # print("HOLAAA ",message,flush=True) |
|
26 | # print("HOLAAA ",message,flush=True) | |
31 | # #lista_ack.remove(msg.payload) |
|
27 | # #lista_ack.remove(msg.payload) | |
32 | # print("LISTA ",lista_ack) |
|
28 | # print("LISTA ",lista_ack) | |
|
29 | global lista_ack | |||
|
30 | global lista_ack_dismatching | |||
|
31 | message= str(msg.payload) | |||
|
32 | message=message[2:len(message)-1] | |||
|
33 | if(message=="UPDATE"): | |||
|
34 | print("UUPDATE") | |||
|
35 | sio.emit('beams_ack',data={'msg':lista_ack}) | |||
|
36 | print(lista_ack,flush=True) | |||
|
37 | lista_ack=[ | |||
|
38 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128 | |||
|
39 | ] | |||
|
40 | else: | |||
|
41 | message=int(message) | |||
|
42 | if(message<=64): | |||
|
43 | # print(message,"\t MATCH",flush=True) | |||
|
44 | lista_ack.remove(message+64) | |||
|
45 | ||||
|
46 | elif(message>64): | |||
|
47 | # print(message,"\t DISMATCH",flush=True) | |||
|
48 | lista_ack.remove(message-64) | |||
|
49 | else: | |||
|
50 | print(len(message)) | |||
33 |
|
51 | |||
34 |
|
52 | |||
35 |
|
53 | |||
36 | client = mqtt.Client() |
|
54 | client = mqtt.Client() | |
37 | client.on_connect = on_connect |
|
55 | client.on_connect = on_connect | |
38 | client.on_message = on_message |
|
56 | client.on_message = on_message | |
39 | client.username_pw_set(os.environ.get('MQTT_USER', 'abs'), os.environ.get('MQTT_PASSWORD', 'abs')) |
|
57 | client.username_pw_set(os.environ.get('MQTT_USER', 'abs'), os.environ.get('MQTT_PASSWORD', 'abs')) | |
40 | client.connect( |
|
58 | client.connect( | |
41 | host=os.environ.get('MQTT_SERVER', '10.10.10.200'), |
|
59 | host=os.environ.get('MQTT_SERVER', '10.10.10.200'), | |
42 | port=int(settings.os.environ.get('MQTT_PORT', 1883)), |
|
60 | port=int(settings.os.environ.get('MQTT_PORT', 1883)), | |
43 | keepalive=int(os.environ.get('MQTT_KEEPALIVE', 60000)) |
|
61 | keepalive=int(os.environ.get('MQTT_KEEPALIVE', 60000)) | |
44 | ) No newline at end of file |
|
62 | ) |
@@ -1,88 +1,89 | |||||
1 | {% extends "base.html" %} |
|
1 | {% extends "base.html" %} | |
2 | {% load django_bootstrap5 %} |
|
2 | {% load django_bootstrap5 %} | |
3 | {% load static %} |
|
3 | {% load static %} | |
4 | {% load main_tags %} |
|
4 | {% load main_tags %} | |
5 | {% block content-title %}{{title}}{% endblock %} |
|
5 | {% block content-title %}{{title}}{% endblock %} | |
6 | {% block content-suptitle %}{{suptitle}}{% endblock %} |
|
6 | {% block content-suptitle %}{{suptitle}}{% endblock %} | |
7 |
|
7 | |||
8 | {% block content %} |
|
8 | {% block content %} | |
9 |
|
9 | |||
10 | {% block menu-actions %} |
|
10 | {% block menu-actions %} | |
11 | <span class=" dropdown pull-right"> |
|
11 | <span class=" dropdown pull-right"> | |
12 | <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="fas fa-bars" aria-hidden="true"></span></a> |
|
12 | <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="fas fa-bars" aria-hidden="true"></span></a> | |
13 | <ul class="dropdown-menu" role="menu"> |
|
13 | <ul class="dropdown-menu" role="menu"> | |
14 | <li><a href="{{ dev_conf.get_absolute_url_edit }}"><span class="fa fa-pencil" aria-hidden="true"></span> Edit</a></li> |
|
14 | <li><a href="{{ dev_conf.get_absolute_url_edit }}"><span class="fa fa-pencil" aria-hidden="true"></span> Edit</a></li> | |
15 | <li><a href="{{ dev_conf.get_absolute_url_delete }}"><span class="fa fa-times" aria-hidden="true"></span> Delete</a></li> |
|
15 | <li><a href="{{ dev_conf.get_absolute_url_delete }}"><span class="fa fa-times" aria-hidden="true"></span> Delete</a></li> | |
16 | <li><a href="{{ dev_conf.get_absolute_url_import }}"><span class="fas fa-cloud-download-alt" aria-hidden="true"></span> Import </a></li> |
|
16 | <li><a href="{{ dev_conf.get_absolute_url_import }}"><span class="fas fa-cloud-download-alt" aria-hidden="true"></span> Import </a></li> | |
17 | <li><a href="{{ dev_conf.get_absolute_url_export }}"><span class="fas fa-cloud-upload-alt" aria-hidden="true"></span> Export </a></li> |
|
17 | <li><a href="{{ dev_conf.get_absolute_url_export }}"><span class="fas fa-cloud-upload-alt" aria-hidden="true"></span> Export </a></li> | |
18 | {% block extra-menu-actions %} |
|
18 | {% block extra-menu-actions %} | |
19 | {% endblock %} |
|
19 | {% endblock %} | |
20 | <li><a>----------------</a></li> |
|
20 | <li><a>----------------</a></li> | |
21 | <li><a href="{{ dev_conf.get_absolute_url_status }}"><span class="fas fa-sync-alt" aria-hidden="true"></span> Status</a></li> |
|
21 | <li><a href="{{ dev_conf.get_absolute_url_status }}"><span class="fas fa-sync-alt" aria-hidden="true"></span> Status</a></li> | |
22 | {% if not no_play %} |
|
22 | {% if not no_play %} | |
23 | {% if not only_stop %} |
|
23 | {% if not only_stop %} | |
24 | <li><a href="{{ dev_conf.get_absolute_url_start}}"><span class="fas fa-play" aria-hidden="true"></span> Start</a></li> |
|
24 | <li><a href="{{ dev_conf.get_absolute_url_start}}"><span class="fas fa-play" aria-hidden="true"></span> Start</a></li> | |
25 | {% endif %} |
|
25 | {% endif %} | |
26 | <li><a href="{{ dev_conf.get_absolute_url_stop }}"><span class="fas fa-stop" aria-hidden="true"></span> Stop</a></li> |
|
26 | <li><a href="{{ dev_conf.get_absolute_url_stop }}"><span class="fas fa-stop" aria-hidden="true"></span> Stop</a></li> | |
27 | {% endif %} |
|
27 | {% endif %} | |
28 | <li><a href="{{ dev_conf.get_absolute_url_write }}"><span class="fas fa-arrow-circle-down" aria-hidden="true"></span> Write</a></li> |
|
28 | <li><a href="{{ dev_conf.get_absolute_url_write }}"><span class="fas fa-arrow-circle-down" aria-hidden="true"></span> Write</a></li> | |
29 | {% if dev_conf.device.device_type.name == 'abs' %} |
|
29 | {% if dev_conf.device.device_type.name == 'abs' %} | |
30 | <li><a href="{{ dev_conf.get_absolute_url_write_mqtt }}"><span class="fas fa-arrow-circle-down" aria-hidden="true"></span> Write MQTT</a></li> |
|
30 | <li><a href="{{ dev_conf.get_absolute_url_write_mqtt }}"><span class="fas fa-arrow-circle-down" aria-hidden="true"></span> Write MQTT</a></li> | |
|
31 | <li><a href="{{ dev_conf.get_absolute_url_stop_mqtt }}"><span class="fas fa-stop" aria-hidden="true"></span> Stop MQTT</a></li> | |||
31 | {% endif %} |
|
32 | {% endif %} | |
32 | {% if dev_conf.device.device_type.name != 'abs' %} |
|
33 | {% if dev_conf.device.device_type.name != 'abs' %} | |
33 | <li><a href="{{ dev_conf.get_absolute_url_read }}"><span class="fas fa-arrow-circle-up" aria-hidden="true"></span> Read</a></li> |
|
34 | <li><a href="{{ dev_conf.get_absolute_url_read }}"><span class="fas fa-arrow-circle-up" aria-hidden="true"></span> Read</a></li> | |
34 | {% endif %} |
|
35 | {% endif %} | |
35 | </ul> |
|
36 | </ul> | |
36 | </span> |
|
37 | </span> | |
37 | {% endblock %} |
|
38 | {% endblock %} | |
38 |
|
39 | |||
39 | {% block content-detail %} |
|
40 | {% block content-detail %} | |
40 | <table class="table table-bordered"> |
|
41 | <table class="table table-bordered"> | |
41 | <tr> |
|
42 | <tr> | |
42 | <th>Status</th> |
|
43 | <th>Status</th> | |
43 | {% if dev_conf.pk == dev_conf.device.conf_active %} |
|
44 | {% if dev_conf.pk == dev_conf.device.conf_active %} | |
44 | <td class="text-{{dev_conf.device.status_color}}"><strong> {% if dev_conf.device.device_type.name == 'abs' %} {{connected_modules}} {% else %} {{dev_conf.device.get_status_display}}{% endif %}</strong></td> |
|
45 | <td class="text-{{dev_conf.device.status_color}}"><strong> {% if dev_conf.device.device_type.name == 'abs' %} {{connected_modules}} {% else %} {{dev_conf.device.get_status_display}}{% endif %}</strong></td> | |
45 | {% else %} |
|
46 | {% else %} | |
46 | <td class="text-info"><strong> Connected </strong></td> |
|
47 | <td class="text-info"><strong> Connected </strong></td> | |
47 | {% endif %} |
|
48 | {% endif %} | |
48 | </tr> |
|
49 | </tr> | |
49 |
|
50 | |||
50 | {% for key in dev_conf_keys %} |
|
51 | {% for key in dev_conf_keys %} | |
51 | <tr> |
|
52 | <tr> | |
52 | <th>{% get_verbose_field_name dev_conf key %}</th> |
|
53 | <th>{% get_verbose_field_name dev_conf key %}</th> | |
53 | <td>{{dev_conf|attr:key}}</td> |
|
54 | <td>{{dev_conf|attr:key}}</td> | |
54 | </tr> |
|
55 | </tr> | |
55 | {% endfor %} |
|
56 | {% endfor %} | |
56 | </table> |
|
57 | </table> | |
57 | {% endblock %} |
|
58 | {% endblock %} | |
58 |
|
59 | |||
59 | {% block extra-content %} |
|
60 | {% block extra-content %} | |
60 | {% endblock %} |
|
61 | {% endblock %} | |
61 |
|
62 | |||
62 | {% endblock %} |
|
63 | {% endblock %} | |
63 |
|
64 | |||
64 | {% block extra-js%} |
|
65 | {% block extra-js%} | |
65 | <script type="text/javascript"> |
|
66 | <script type="text/javascript"> | |
66 |
|
67 | |||
67 | $("#bt_edit").click(function() { |
|
68 | $("#bt_edit").click(function() { | |
68 | document.location = "{{ dev_conf.get_absolute_url_edit }}"; |
|
69 | document.location = "{{ dev_conf.get_absolute_url_edit }}"; | |
69 | }); |
|
70 | }); | |
70 |
|
71 | |||
71 | $("#bt_read").click(function() { |
|
72 | $("#bt_read").click(function() { | |
72 | document.location = "{{ dev_conf.get_absolute_url_read }}"; |
|
73 | document.location = "{{ dev_conf.get_absolute_url_read }}"; | |
73 | }); |
|
74 | }); | |
74 |
|
75 | |||
75 | $("#bt_write").click(function() { |
|
76 | $("#bt_write").click(function() { | |
76 | document.location = "{{ dev_conf.get_absolute_url_write }}"; |
|
77 | document.location = "{{ dev_conf.get_absolute_url_write }}"; | |
77 | }); |
|
78 | }); | |
78 |
|
79 | |||
79 | $("#bt_import").click(function() { |
|
80 | $("#bt_import").click(function() { | |
80 | document.location = "{{ dev_conf.get_absolute_url_import }}"; |
|
81 | document.location = "{{ dev_conf.get_absolute_url_import }}"; | |
81 | }); |
|
82 | }); | |
82 |
|
83 | |||
83 | $("#bt_export").click(function() { |
|
84 | $("#bt_export").click(function() { | |
84 | document.location = "{{ dev_conf.get_absolute_url_export }}"; |
|
85 | document.location = "{{ dev_conf.get_absolute_url_export }}"; | |
85 | }); |
|
86 | }); | |
86 |
|
87 | |||
87 | </script> |
|
88 | </script> | |
88 | {% endblock %} |
|
89 | {% endblock %} |
@@ -1,71 +1,72 | |||||
1 | from django.urls import path |
|
1 | from django.urls import path | |
2 |
|
2 | |||
3 | from . import views |
|
3 | from . import views | |
4 |
|
4 | |||
5 | urlpatterns = ( |
|
5 | urlpatterns = ( | |
6 | path('', views.index, name='index'), |
|
6 | path('', views.index, name='index'), | |
7 |
|
7 | |||
8 | path('realtime/', views.real_time, name='url_real_time'), |
|
8 | path('realtime/', views.real_time, name='url_real_time'), | |
9 |
|
9 | |||
10 | path('theme/(<str:theme>/', views.theme, name='url_theme'), |
|
10 | path('theme/(<str:theme>/', views.theme, name='url_theme'), | |
11 |
|
11 | |||
12 | path('location/new/', views.location_new, name='url_add_location'), |
|
12 | path('location/new/', views.location_new, name='url_add_location'), | |
13 | path('location/', views.locations, name='url_locations'), |
|
13 | path('location/', views.locations, name='url_locations'), | |
14 | path('location/<int:id_loc>/', views.location, name='url_location'), |
|
14 | path('location/<int:id_loc>/', views.location, name='url_location'), | |
15 | path('location/<int:id_loc>/edit/', views.location_edit, name='url_edit_location'), |
|
15 | path('location/<int:id_loc>/edit/', views.location_edit, name='url_edit_location'), | |
16 | path('location/<int:id_loc>/delete/', views.location_delete, name='url_delete_location'), |
|
16 | path('location/<int:id_loc>/delete/', views.location_delete, name='url_delete_location'), | |
17 |
|
17 | |||
18 | path('device/new/', views.device_new, name='url_add_device'), |
|
18 | path('device/new/', views.device_new, name='url_add_device'), | |
19 | path('device/', views.devices, name='url_devices'), |
|
19 | path('device/', views.devices, name='url_devices'), | |
20 | path('device/<int:id_dev>/', views.device, name='url_device'), |
|
20 | path('device/<int:id_dev>/', views.device, name='url_device'), | |
21 | path('device/<int:id_dev>/edit/', views.device_edit, name='url_edit_device'), |
|
21 | path('device/<int:id_dev>/edit/', views.device_edit, name='url_edit_device'), | |
22 | path('device/<int:id_dev>/delete/', views.device_delete, name='url_delete_device'), |
|
22 | path('device/<int:id_dev>/delete/', views.device_delete, name='url_delete_device'), | |
23 | path('device/<int:id_dev>/change_ip/', views.device_change_ip, name='url_change_ip_device'), |
|
23 | path('device/<int:id_dev>/change_ip/', views.device_change_ip, name='url_change_ip_device'), | |
24 |
|
24 | |||
25 | path('campaign/new/', views.campaign_new, name='url_add_campaign'), |
|
25 | path('campaign/new/', views.campaign_new, name='url_add_campaign'), | |
26 | path('campaign/', views.campaigns, name='url_campaigns'), |
|
26 | path('campaign/', views.campaigns, name='url_campaigns'), | |
27 | path('campaign/<int:id_camp>/', views.campaign, name='url_campaign'), |
|
27 | path('campaign/<int:id_camp>/', views.campaign, name='url_campaign'), | |
28 | path('campaign/<int:id_camp>/edit/', views.campaign_edit, name='url_edit_campaign'), |
|
28 | path('campaign/<int:id_camp>/edit/', views.campaign_edit, name='url_edit_campaign'), | |
29 | path('campaign/<int:id_camp>/delete/', views.campaign_delete, name='url_delete_campaign'), |
|
29 | path('campaign/<int:id_camp>/delete/', views.campaign_delete, name='url_delete_campaign'), | |
30 | path('campaign/<int:id_camp>/export/', views.campaign_export, name='url_export_campaign'), |
|
30 | path('campaign/<int:id_camp>/export/', views.campaign_export, name='url_export_campaign'), | |
31 | path('campaign/<int:id_camp>/import/', views.campaign_import, name='url_import_campaign'), |
|
31 | path('campaign/<int:id_camp>/import/', views.campaign_import, name='url_import_campaign'), | |
32 |
|
32 | |||
33 | path('experiment/new/', views.experiment_new, name='url_add_experiment'), |
|
33 | path('experiment/new/', views.experiment_new, name='url_add_experiment'), | |
34 | path('experiment/', views.experiments, name='url_experiments'), |
|
34 | path('experiment/', views.experiments, name='url_experiments'), | |
35 | path('experiment/<int:id_exp>/', views.experiment, name='url_experiment'), |
|
35 | path('experiment/<int:id_exp>/', views.experiment, name='url_experiment'), | |
36 | path('experiment/<int:id_exp>/edit/', views.experiment_edit, name='url_edit_experiment'), |
|
36 | path('experiment/<int:id_exp>/edit/', views.experiment_edit, name='url_edit_experiment'), | |
37 | path('experiment/<int:id_exp>/delete/', views.experiment_delete, name='url_delete_experiment'), |
|
37 | path('experiment/<int:id_exp>/delete/', views.experiment_delete, name='url_delete_experiment'), | |
38 | path('experiment/<int:id_exp>/export/', views.experiment_export, name='url_export_experiment'), |
|
38 | path('experiment/<int:id_exp>/export/', views.experiment_export, name='url_export_experiment'), | |
39 | path('experiment/<int:id_exp>/import/', views.experiment_import, name='url_import_experiment'), |
|
39 | path('experiment/<int:id_exp>/import/', views.experiment_import, name='url_import_experiment'), | |
40 | path('experiment/<int:id_exp>/start/', views.experiment_start, name='url_start_experiment'), |
|
40 | path('experiment/<int:id_exp>/start/', views.experiment_start, name='url_start_experiment'), | |
41 | path('experiment/<int:id_exp>/stop/', views.experiment_stop, name='url_stop_experiment'), |
|
41 | path('experiment/<int:id_exp>/stop/', views.experiment_stop, name='url_stop_experiment'), | |
42 | path('experiment/<int:id_exp>/mix/', views.experiment_mix, name='url_mix_experiment'), |
|
42 | path('experiment/<int:id_exp>/mix/', views.experiment_mix, name='url_mix_experiment'), | |
43 | path('experiment/<int:id_exp>/mix/delete/', views.experiment_mix_delete, name='url_delete_mix_experiment'), |
|
43 | path('experiment/<int:id_exp>/mix/delete/', views.experiment_mix_delete, name='url_delete_mix_experiment'), | |
44 | path('experiment/<int:id_exp>/summary/', views.experiment_summary, name='url_sum_experiment'), |
|
44 | path('experiment/<int:id_exp>/summary/', views.experiment_summary, name='url_sum_experiment'), | |
45 | path('experiment/<int:id_exp>/verify/', views.experiment_verify, name='url_verify_experiment'), |
|
45 | path('experiment/<int:id_exp>/verify/', views.experiment_verify, name='url_verify_experiment'), | |
46 |
|
46 | |||
47 | path('experiment/<int:id_exp>/new_dev_conf/', views.dev_conf_new, name='url_add_dev_conf'), |
|
47 | path('experiment/<int:id_exp>/new_dev_conf/', views.dev_conf_new, name='url_add_dev_conf'), | |
48 | path('experiment/<int:id_exp>/new_dev_conf/<int:id_dev>/', views.dev_conf_new, name='url_add_dev_conf'), |
|
48 | path('experiment/<int:id_exp>/new_dev_conf/<int:id_dev>/', views.dev_conf_new, name='url_add_dev_conf'), | |
49 |
|
49 | |||
50 | path('dev_conf/', views.dev_confs, name='url_dev_confs'), |
|
50 | path('dev_conf/', views.dev_confs, name='url_dev_confs'), | |
51 | path('dev_conf/<int:id_conf>/', views.dev_conf, name='url_dev_conf'), |
|
51 | path('dev_conf/<int:id_conf>/', views.dev_conf, name='url_dev_conf'), | |
52 | path('dev_conf/<int:id_conf>/edit/', views.dev_conf_edit, name='url_edit_dev_conf'), |
|
52 | path('dev_conf/<int:id_conf>/edit/', views.dev_conf_edit, name='url_edit_dev_conf'), | |
53 | path('dev_conf/<int:id_conf>/delete/', views.dev_conf_delete, name='url_delete_dev_conf'), |
|
53 | path('dev_conf/<int:id_conf>/delete/', views.dev_conf_delete, name='url_delete_dev_conf'), | |
54 |
|
54 | |||
55 | path('dev_conf/<int:id_conf>/write/', views.dev_conf_write, name='url_write_dev_conf'), |
|
55 | path('dev_conf/<int:id_conf>/write/', views.dev_conf_write, name='url_write_dev_conf'), | |
56 | path('dev_conf/<int:id_conf>/write_mqtt/', views.dev_conf_write_mqtt, name='url_write_mqtt_dev_conf'), |
|
56 | path('dev_conf/<int:id_conf>/write_mqtt/', views.dev_conf_write_mqtt, name='url_write_mqtt_dev_conf'), | |
57 | path('dev_conf/<int:id_conf>/read/', views.dev_conf_read, name='url_read_dev_conf'), |
|
57 | path('dev_conf/<int:id_conf>/read/', views.dev_conf_read, name='url_read_dev_conf'), | |
58 | path('dev_conf/<int:id_conf>/import/', views.dev_conf_import, name='url_import_dev_conf'), |
|
58 | path('dev_conf/<int:id_conf>/import/', views.dev_conf_import, name='url_import_dev_conf'), | |
59 | path('dev_conf/<int:id_conf>/export/', views.dev_conf_export, name='url_export_dev_conf'), |
|
59 | path('dev_conf/<int:id_conf>/export/', views.dev_conf_export, name='url_export_dev_conf'), | |
60 | path('dev_conf/<int:id_conf>/start/', views.dev_conf_start, name='url_start_dev_conf'), |
|
60 | path('dev_conf/<int:id_conf>/start/', views.dev_conf_start, name='url_start_dev_conf'), | |
61 | path('dev_conf/<int:id_conf>/stop/', views.dev_conf_stop, name='url_stop_dev_conf'), |
|
61 | path('dev_conf/<int:id_conf>/stop/', views.dev_conf_stop, name='url_stop_dev_conf'), | |
|
62 | path('dev_conf/<int:id_conf>/stop_mqtt/', views.dev_conf_stop_mqtt, name='url_stop_mqtt_dev_conf'), | |||
62 | path('dev_conf/<int:id_conf>/status/', views.dev_conf_status, name='url_status_dev_conf'), |
|
63 | path('dev_conf/<int:id_conf>/status/', views.dev_conf_status, name='url_status_dev_conf'), | |
63 |
|
64 | |||
64 | path('operation/', views.operation, name='url_operation'), |
|
65 | path('operation/', views.operation, name='url_operation'), | |
65 | path('operation/<int:id_camp>/', views.operation, name='url_operation'), |
|
66 | path('operation/<int:id_camp>/', views.operation, name='url_operation'), | |
66 | #path('operation/<int:id_camp>/revoke', views.revoke_tasks, name='url_operation_revoke'), |
|
67 | #path('operation/<int:id_camp>/revoke', views.revoke_tasks, name='url_operation_revoke'), | |
67 | #path('operation/<int:id_camp>/show', views.show_tasks, name='url_operation_show'), |
|
68 | #path('operation/<int:id_camp>/show', views.show_tasks, name='url_operation_show'), | |
68 | path('operation/<int:id_camp>/radar/<int:id_radar>/start/', views.radar_start, name='url_radar_start'), |
|
69 | path('operation/<int:id_camp>/radar/<int:id_radar>/start/', views.radar_start, name='url_radar_start'), | |
69 | path('operation/<int:id_camp>/radar/<int:id_radar>/stop/', views.radar_stop, name='url_radar_stop'), |
|
70 | path('operation/<int:id_camp>/radar/<int:id_radar>/stop/', views.radar_stop, name='url_radar_stop'), | |
70 | path('operation/<int:id_camp>/radar/<int:id_radar>/refresh/', views.radar_refresh, name='url_radar_refresh'), |
|
71 | path('operation/<int:id_camp>/radar/<int:id_radar>/refresh/', views.radar_refresh, name='url_radar_refresh'), | |
71 | ) |
|
72 | ) |
@@ -1,2069 +1,2078 | |||||
1 | import ast |
|
1 | import ast | |
2 | import json |
|
2 | import json | |
3 | import hashlib |
|
3 | import hashlib | |
4 | from datetime import datetime, timedelta |
|
4 | from datetime import datetime, timedelta | |
5 |
|
5 | |||
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.utils.safestring import mark_safe |
|
7 | from django.utils.safestring import mark_safe | |
8 | from django.http import HttpResponseRedirect |
|
8 | from django.http import HttpResponseRedirect | |
9 | from django.urls import reverse |
|
9 | from django.urls import reverse | |
10 | from django.db.models import Q |
|
10 | from django.db.models import Q | |
11 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
|
11 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger | |
12 | from django.contrib import messages |
|
12 | from django.contrib import messages | |
13 | from django.http.request import QueryDict |
|
13 | from django.http.request import QueryDict | |
14 | from django.contrib.auth.decorators import login_required, user_passes_test |
|
14 | from django.contrib.auth.decorators import login_required, user_passes_test | |
15 |
|
15 | |||
16 | from django.utils.timezone import is_aware |
|
16 | from django.utils.timezone import is_aware | |
17 |
|
17 | |||
18 | try: |
|
18 | try: | |
19 | from urllib.parse import urlencode |
|
19 | from urllib.parse import urlencode | |
20 | except ImportError: |
|
20 | except ImportError: | |
21 | from urllib import urlencode |
|
21 | from urllib import urlencode | |
22 |
|
22 | |||
23 | from .forms import CampaignForm, ExperimentForm, DeviceForm, ConfigurationForm, LocationForm, UploadFileForm, DownloadFileForm, OperationForm, NewForm |
|
23 | from .forms import CampaignForm, ExperimentForm, DeviceForm, ConfigurationForm, LocationForm, UploadFileForm, DownloadFileForm, OperationForm, NewForm | |
24 | from .forms import OperationSearchForm, FilterForm, ChangeIpForm |
|
24 | from .forms import OperationSearchForm, FilterForm, ChangeIpForm | |
25 |
|
25 | |||
26 | from apps.rc.forms import RCConfigurationForm, RCLineCode, RCMixConfigurationForm |
|
26 | from apps.rc.forms import RCConfigurationForm, RCLineCode, RCMixConfigurationForm | |
27 | from apps.dds.forms import DDSConfigurationForm |
|
27 | from apps.dds.forms import DDSConfigurationForm | |
28 | from apps.jars.forms import JARSConfigurationForm |
|
28 | from apps.jars.forms import JARSConfigurationForm | |
29 | from apps.cgs.forms import CGSConfigurationForm |
|
29 | from apps.cgs.forms import CGSConfigurationForm | |
30 | from apps.abs.forms import ABSConfigurationForm |
|
30 | from apps.abs.forms import ABSConfigurationForm | |
31 | from apps.usrp.forms import USRPConfigurationForm |
|
31 | from apps.usrp.forms import USRPConfigurationForm | |
32 | from apps.dds_rest.forms import DDSRestConfigurationForm |
|
32 | from apps.dds_rest.forms import DDSRestConfigurationForm | |
33 | from apps.atrad.forms import ATRADConfigurationForm |
|
33 | from apps.atrad.forms import ATRADConfigurationForm | |
34 | from .utils import Params |
|
34 | from .utils import Params | |
35 |
|
35 | |||
36 | from .models import Campaign, Experiment, Device, Configuration, Location, RunningExperiment, DEV_STATES |
|
36 | from .models import Campaign, Experiment, Device, Configuration, Location, RunningExperiment, DEV_STATES | |
37 | from apps.cgs.models import CGSConfiguration |
|
37 | from apps.cgs.models import CGSConfiguration | |
38 | from apps.jars.models import JARSConfiguration, EXPERIMENT_TYPE |
|
38 | from apps.jars.models import JARSConfiguration, EXPERIMENT_TYPE | |
39 | from apps.usrp.models import USRPConfiguration |
|
39 | from apps.usrp.models import USRPConfiguration | |
40 | from apps.abs.models import ABSConfiguration |
|
40 | from apps.abs.models import ABSConfiguration | |
41 | from apps.rc.models import RCConfiguration, RCLine, RCLineType, RCClock |
|
41 | from apps.rc.models import RCConfiguration, RCLine, RCLineType, RCClock | |
42 | from apps.dds.models import DDSConfiguration |
|
42 | from apps.dds.models import DDSConfiguration | |
43 | from apps.dds_rest.models import DDSRestConfiguration |
|
43 | from apps.dds_rest.models import DDSRestConfiguration | |
44 | from apps.atrad.models import ATRADConfiguration |
|
44 | from apps.atrad.models import ATRADConfiguration | |
45 |
|
45 | |||
46 | #from .tasks import task_start |
|
46 | #from .tasks import task_start | |
47 | from radarsys.celery import app |
|
47 | from radarsys.celery import app | |
48 |
|
48 | |||
49 |
|
49 | |||
50 | from .mqtt import client as mqtt_client |
|
50 | from .mqtt import client as mqtt_client | |
51 | from radarsys.socketconfig import sio as sio |
|
51 | from radarsys.socketconfig import sio as sio | |
52 |
|
52 | |||
53 | #comentario test |
|
53 | #comentario test | |
54 | CONF_FORMS = { |
|
54 | CONF_FORMS = { | |
55 | 'rc': RCConfigurationForm, |
|
55 | 'rc': RCConfigurationForm, | |
56 | 'dds': DDSConfigurationForm, |
|
56 | 'dds': DDSConfigurationForm, | |
57 | 'dds_rest': DDSRestConfigurationForm, |
|
57 | 'dds_rest': DDSRestConfigurationForm, | |
58 | 'jars': JARSConfigurationForm, |
|
58 | 'jars': JARSConfigurationForm, | |
59 | 'cgs': CGSConfigurationForm, |
|
59 | 'cgs': CGSConfigurationForm, | |
60 | 'abs': ABSConfigurationForm, |
|
60 | 'abs': ABSConfigurationForm, | |
61 | 'usrp': USRPConfigurationForm, |
|
61 | 'usrp': USRPConfigurationForm, | |
62 | 'atrad': ATRADConfigurationForm, |
|
62 | 'atrad': ATRADConfigurationForm, | |
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 | CONF_MODELS = { |
|
65 | CONF_MODELS = { | |
66 | 'rc': RCConfiguration, |
|
66 | 'rc': RCConfiguration, | |
67 | 'dds': DDSConfiguration, |
|
67 | 'dds': DDSConfiguration, | |
68 | 'dds_rest': DDSRestConfiguration, |
|
68 | 'dds_rest': DDSRestConfiguration, | |
69 | 'jars': JARSConfiguration, |
|
69 | 'jars': JARSConfiguration, | |
70 | 'cgs': CGSConfiguration, |
|
70 | 'cgs': CGSConfiguration, | |
71 | 'abs': ABSConfiguration, |
|
71 | 'abs': ABSConfiguration, | |
72 | 'usrp': USRPConfiguration, |
|
72 | 'usrp': USRPConfiguration, | |
73 | 'atrad': ATRADConfiguration, |
|
73 | 'atrad': ATRADConfiguration, | |
74 | } |
|
74 | } | |
75 |
|
75 | |||
76 | MIX_MODES = { |
|
76 | MIX_MODES = { | |
77 | '0': 'P', |
|
77 | '0': 'P', | |
78 | '1': 'S', |
|
78 | '1': 'S', | |
79 | } |
|
79 | } | |
80 |
|
80 | |||
81 | MIX_OPERATIONS = { |
|
81 | MIX_OPERATIONS = { | |
82 | '0': 'OR', |
|
82 | '0': 'OR', | |
83 | '1': 'XOR', |
|
83 | '1': 'XOR', | |
84 | '2': 'AND', |
|
84 | '2': 'AND', | |
85 | '3': 'NAND', |
|
85 | '3': 'NAND', | |
86 | } |
|
86 | } | |
87 |
|
87 | |||
88 |
|
88 | |||
89 | def is_developer(user): |
|
89 | def is_developer(user): | |
90 |
|
90 | |||
91 | groups = [str(g.name) for g in user.groups.all()] |
|
91 | groups = [str(g.name) for g in user.groups.all()] | |
92 | return 'Developer' in groups or user.is_staff |
|
92 | return 'Developer' in groups or user.is_staff | |
93 |
|
93 | |||
94 |
|
94 | |||
95 | def is_operator(user): |
|
95 | def is_operator(user): | |
96 |
|
96 | |||
97 | groups = [str(g.name) for g in user.groups.all()] |
|
97 | groups = [str(g.name) for g in user.groups.all()] | |
98 | return 'Operator' in groups or user.is_staff |
|
98 | return 'Operator' in groups or user.is_staff | |
99 |
|
99 | |||
100 |
|
100 | |||
101 | def has_been_modified(model): |
|
101 | def has_been_modified(model): | |
102 |
|
102 | |||
103 | prev_hash = model.hash |
|
103 | prev_hash = model.hash | |
104 | new_hash = hashlib.sha256(str(model.parms_to_dict).encode()).hexdigest() |
|
104 | new_hash = hashlib.sha256(str(model.parms_to_dict).encode()).hexdigest() | |
105 | if prev_hash != new_hash: |
|
105 | if prev_hash != new_hash: | |
106 | model.hash = new_hash |
|
106 | model.hash = new_hash | |
107 | model.save() |
|
107 | model.save() | |
108 | return True |
|
108 | return True | |
109 | return False |
|
109 | return False | |
110 |
|
110 | |||
111 |
|
111 | |||
112 | def index(request): |
|
112 | def index(request): | |
113 | kwargs = {'no_sidebar': True} |
|
113 | kwargs = {'no_sidebar': True} | |
114 |
|
114 | |||
115 | return render(request, 'index.html', kwargs) |
|
115 | return render(request, 'index.html', kwargs) | |
116 |
|
116 | |||
117 |
|
117 | |||
118 | def locations(request): |
|
118 | def locations(request): | |
119 |
|
119 | |||
120 | page = request.GET.get('page') |
|
120 | page = request.GET.get('page') | |
121 | order = ('name',) |
|
121 | order = ('name',) | |
122 |
|
122 | |||
123 | kwargs = get_paginator(Location, page, order) |
|
123 | kwargs = get_paginator(Location, page, order) | |
124 |
|
124 | |||
125 | kwargs['keys'] = ['name', 'description'] |
|
125 | kwargs['keys'] = ['name', 'description'] | |
126 | kwargs['title'] = 'Radar System' |
|
126 | kwargs['title'] = 'Radar System' | |
127 | kwargs['suptitle'] = 'List' |
|
127 | kwargs['suptitle'] = 'List' | |
128 | kwargs['no_sidebar'] = True |
|
128 | kwargs['no_sidebar'] = True | |
129 |
|
129 | |||
130 | return render(request, 'base_list.html', kwargs) |
|
130 | return render(request, 'base_list.html', kwargs) | |
131 |
|
131 | |||
132 |
|
132 | |||
133 | def location(request, id_loc): |
|
133 | def location(request, id_loc): | |
134 |
|
134 | |||
135 | location = get_object_or_404(Location, pk=id_loc) |
|
135 | location = get_object_or_404(Location, pk=id_loc) | |
136 |
|
136 | |||
137 | kwargs = {} |
|
137 | kwargs = {} | |
138 | kwargs['location'] = location |
|
138 | kwargs['location'] = location | |
139 | kwargs['location_keys'] = ['name', 'description'] |
|
139 | kwargs['location_keys'] = ['name', 'description'] | |
140 |
|
140 | |||
141 | kwargs['title'] = 'Location' |
|
141 | kwargs['title'] = 'Location' | |
142 | kwargs['suptitle'] = 'Details' |
|
142 | kwargs['suptitle'] = 'Details' | |
143 |
|
143 | |||
144 | return render(request, 'location.html', kwargs) |
|
144 | return render(request, 'location.html', kwargs) | |
145 |
|
145 | |||
146 |
|
146 | |||
147 | @login_required |
|
147 | @login_required | |
148 | def location_new(request): |
|
148 | def location_new(request): | |
149 |
|
149 | |||
150 | if request.method == 'GET': |
|
150 | if request.method == 'GET': | |
151 | form = LocationForm() |
|
151 | form = LocationForm() | |
152 |
|
152 | |||
153 | if request.method == 'POST': |
|
153 | if request.method == 'POST': | |
154 | form = LocationForm(request.POST) |
|
154 | form = LocationForm(request.POST) | |
155 |
|
155 | |||
156 | if form.is_valid(): |
|
156 | if form.is_valid(): | |
157 | form.save() |
|
157 | form.save() | |
158 | return redirect('url_locations') |
|
158 | return redirect('url_locations') | |
159 |
|
159 | |||
160 | kwargs = {} |
|
160 | kwargs = {} | |
161 | kwargs['form'] = form |
|
161 | kwargs['form'] = form | |
162 | kwargs['title'] = 'Radar System' |
|
162 | kwargs['title'] = 'Radar System' | |
163 | kwargs['suptitle'] = 'New' |
|
163 | kwargs['suptitle'] = 'New' | |
164 | kwargs['button'] = 'Create' |
|
164 | kwargs['button'] = 'Create' | |
165 |
|
165 | |||
166 | return render(request, 'base_edit.html', kwargs) |
|
166 | return render(request, 'base_edit.html', kwargs) | |
167 |
|
167 | |||
168 |
|
168 | |||
169 | @login_required |
|
169 | @login_required | |
170 | def location_edit(request, id_loc): |
|
170 | def location_edit(request, id_loc): | |
171 |
|
171 | |||
172 | location = get_object_or_404(Location, pk=id_loc) |
|
172 | location = get_object_or_404(Location, pk=id_loc) | |
173 |
|
173 | |||
174 | if request.method == 'GET': |
|
174 | if request.method == 'GET': | |
175 | form = LocationForm(instance=location) |
|
175 | form = LocationForm(instance=location) | |
176 |
|
176 | |||
177 | if request.method == 'POST': |
|
177 | if request.method == 'POST': | |
178 | form = LocationForm(request.POST, instance=location) |
|
178 | form = LocationForm(request.POST, instance=location) | |
179 |
|
179 | |||
180 | if form.is_valid(): |
|
180 | if form.is_valid(): | |
181 | form.save() |
|
181 | form.save() | |
182 | return redirect('url_locations') |
|
182 | return redirect('url_locations') | |
183 |
|
183 | |||
184 | kwargs = {} |
|
184 | kwargs = {} | |
185 | kwargs['form'] = form |
|
185 | kwargs['form'] = form | |
186 | kwargs['title'] = 'Location' |
|
186 | kwargs['title'] = 'Location' | |
187 | kwargs['suptitle'] = 'Edit' |
|
187 | kwargs['suptitle'] = 'Edit' | |
188 | kwargs['button'] = 'Update' |
|
188 | kwargs['button'] = 'Update' | |
189 |
|
189 | |||
190 | return render(request, 'base_edit.html', kwargs) |
|
190 | return render(request, 'base_edit.html', kwargs) | |
191 |
|
191 | |||
192 |
|
192 | |||
193 | @login_required |
|
193 | @login_required | |
194 | def location_delete(request, id_loc): |
|
194 | def location_delete(request, id_loc): | |
195 |
|
195 | |||
196 | location = get_object_or_404(Location, pk=id_loc) |
|
196 | location = get_object_or_404(Location, pk=id_loc) | |
197 |
|
197 | |||
198 | if request.method == 'POST': |
|
198 | if request.method == 'POST': | |
199 |
|
199 | |||
200 | if is_developer(request.user): |
|
200 | if is_developer(request.user): | |
201 | location.delete() |
|
201 | location.delete() | |
202 | return redirect('url_locations') |
|
202 | return redirect('url_locations') | |
203 |
|
203 | |||
204 | messages.error(request, 'Not enough permission to delete this object') |
|
204 | messages.error(request, 'Not enough permission to delete this object') | |
205 | return redirect(location.get_absolute_url()) |
|
205 | return redirect(location.get_absolute_url()) | |
206 |
|
206 | |||
207 | kwargs = { |
|
207 | kwargs = { | |
208 | 'title': 'Delete', |
|
208 | 'title': 'Delete', | |
209 | 'suptitle': 'Location', |
|
209 | 'suptitle': 'Location', | |
210 | 'object': location, |
|
210 | 'object': location, | |
211 | 'delete': True |
|
211 | 'delete': True | |
212 | } |
|
212 | } | |
213 |
|
213 | |||
214 | return render(request, 'confirm.html', kwargs) |
|
214 | return render(request, 'confirm.html', kwargs) | |
215 |
|
215 | |||
216 |
|
216 | |||
217 | def devices(request): |
|
217 | def devices(request): | |
218 |
|
218 | |||
219 | page = request.GET.get('page') |
|
219 | page = request.GET.get('page') | |
220 | order = ('location', 'device_type') |
|
220 | order = ('location', 'device_type') | |
221 |
|
221 | |||
222 | filters = request.GET.copy() |
|
222 | filters = request.GET.copy() | |
223 | kwargs = get_paginator(Device, page, order, filters) |
|
223 | kwargs = get_paginator(Device, page, order, filters) | |
224 | form = FilterForm(initial=request.GET, extra_fields=['tags']) |
|
224 | form = FilterForm(initial=request.GET, extra_fields=['tags']) | |
225 |
|
225 | |||
226 | kwargs['keys'] = ['device_type', 'location', |
|
226 | kwargs['keys'] = ['device_type', 'location', | |
227 | 'ip_address', 'port_address', 'actions'] |
|
227 | 'ip_address', 'port_address', 'actions'] | |
228 | kwargs['title'] = 'Device' |
|
228 | kwargs['title'] = 'Device' | |
229 | kwargs['suptitle'] = 'List' |
|
229 | kwargs['suptitle'] = 'List' | |
230 | kwargs['no_sidebar'] = True |
|
230 | kwargs['no_sidebar'] = True | |
231 | kwargs['form'] = form |
|
231 | kwargs['form'] = form | |
232 | kwargs['add_url'] = reverse('url_add_device') |
|
232 | kwargs['add_url'] = reverse('url_add_device') | |
233 | filters.pop('page', None) |
|
233 | filters.pop('page', None) | |
234 | kwargs['q'] = urlencode(filters) |
|
234 | kwargs['q'] = urlencode(filters) | |
235 | kwargs['menu_devices'] = 'active' |
|
235 | kwargs['menu_devices'] = 'active' | |
236 | return render(request, 'base_list.html', kwargs) |
|
236 | return render(request, 'base_list.html', kwargs) | |
237 |
|
237 | |||
238 |
|
238 | |||
239 | def device(request, id_dev): |
|
239 | def device(request, id_dev): | |
240 |
|
240 | |||
241 | device = get_object_or_404(Device, pk=id_dev) |
|
241 | device = get_object_or_404(Device, pk=id_dev) | |
242 |
|
242 | |||
243 | kwargs = {} |
|
243 | kwargs = {} | |
244 | kwargs['device'] = device |
|
244 | kwargs['device'] = device | |
245 | kwargs['device_keys'] = ['device_type', |
|
245 | kwargs['device_keys'] = ['device_type', | |
246 | 'ip_address', 'port_address', 'description'] |
|
246 | 'ip_address', 'port_address', 'description'] | |
247 |
|
247 | |||
248 | kwargs['title'] = 'Device' |
|
248 | kwargs['title'] = 'Device' | |
249 | kwargs['suptitle'] = 'Details' |
|
249 | kwargs['suptitle'] = 'Details' | |
250 | kwargs['menu_devices'] = 'active' |
|
250 | kwargs['menu_devices'] = 'active' | |
251 |
|
251 | |||
252 | return render(request, 'device.html', kwargs) |
|
252 | return render(request, 'device.html', kwargs) | |
253 |
|
253 | |||
254 |
|
254 | |||
255 | @login_required |
|
255 | @login_required | |
256 | def device_new(request): |
|
256 | def device_new(request): | |
257 |
|
257 | |||
258 | if request.method == 'GET': |
|
258 | if request.method == 'GET': | |
259 | form = DeviceForm() |
|
259 | form = DeviceForm() | |
260 |
|
260 | |||
261 | if request.method == 'POST': |
|
261 | if request.method == 'POST': | |
262 | form = DeviceForm(request.POST) |
|
262 | form = DeviceForm(request.POST) | |
263 |
|
263 | |||
264 | if form.is_valid(): |
|
264 | if form.is_valid(): | |
265 | form.save() |
|
265 | form.save() | |
266 | return redirect('url_devices') |
|
266 | return redirect('url_devices') | |
267 |
|
267 | |||
268 | kwargs = {} |
|
268 | kwargs = {} | |
269 | kwargs['form'] = form |
|
269 | kwargs['form'] = form | |
270 | kwargs['title'] = 'Device' |
|
270 | kwargs['title'] = 'Device' | |
271 | kwargs['suptitle'] = 'New_2' |
|
271 | kwargs['suptitle'] = 'New_2' | |
272 | kwargs['button'] = 'Create' |
|
272 | kwargs['button'] = 'Create' | |
273 | kwargs['menu_devices'] = 'active' |
|
273 | kwargs['menu_devices'] = 'active' | |
274 |
|
274 | |||
275 | return render(request, 'base_edit.html', kwargs) |
|
275 | return render(request, 'base_edit.html', kwargs) | |
276 |
|
276 | |||
277 |
|
277 | |||
278 | @login_required |
|
278 | @login_required | |
279 | def device_edit(request, id_dev): |
|
279 | def device_edit(request, id_dev): | |
280 |
|
280 | |||
281 | device = get_object_or_404(Device, pk=id_dev) |
|
281 | device = get_object_or_404(Device, pk=id_dev) | |
282 |
|
282 | |||
283 | if request.method == 'GET': |
|
283 | if request.method == 'GET': | |
284 | form = DeviceForm(instance=device) |
|
284 | form = DeviceForm(instance=device) | |
285 |
|
285 | |||
286 | if request.method == 'POST': |
|
286 | if request.method == 'POST': | |
287 | form = DeviceForm(request.POST, instance=device) |
|
287 | form = DeviceForm(request.POST, instance=device) | |
288 |
|
288 | |||
289 | if form.is_valid(): |
|
289 | if form.is_valid(): | |
290 | form.save() |
|
290 | form.save() | |
291 | return redirect(device.get_absolute_url()) |
|
291 | return redirect(device.get_absolute_url()) | |
292 |
|
292 | |||
293 | kwargs = {} |
|
293 | kwargs = {} | |
294 | kwargs['form'] = form |
|
294 | kwargs['form'] = form | |
295 | kwargs['title'] = 'Device' |
|
295 | kwargs['title'] = 'Device' | |
296 | kwargs['suptitle'] = 'Edit' |
|
296 | kwargs['suptitle'] = 'Edit' | |
297 | kwargs['button'] = 'Update' |
|
297 | kwargs['button'] = 'Update' | |
298 | kwargs['menu_devices'] = 'active' |
|
298 | kwargs['menu_devices'] = 'active' | |
299 |
|
299 | |||
300 | return render(request, 'base_edit.html', kwargs) |
|
300 | return render(request, 'base_edit.html', kwargs) | |
301 |
|
301 | |||
302 |
|
302 | |||
303 | @login_required |
|
303 | @login_required | |
304 | def device_delete(request, id_dev): |
|
304 | def device_delete(request, id_dev): | |
305 |
|
305 | |||
306 | device = get_object_or_404(Device, pk=id_dev) |
|
306 | device = get_object_or_404(Device, pk=id_dev) | |
307 |
|
307 | |||
308 | if request.method == 'POST': |
|
308 | if request.method == 'POST': | |
309 |
|
309 | |||
310 | if is_developer(request.user): |
|
310 | if is_developer(request.user): | |
311 | device.delete() |
|
311 | device.delete() | |
312 | return redirect('url_devices') |
|
312 | return redirect('url_devices') | |
313 |
|
313 | |||
314 | messages.error(request, 'Not enough permission to delete this object') |
|
314 | messages.error(request, 'Not enough permission to delete this object') | |
315 | return redirect(device.get_absolute_url()) |
|
315 | return redirect(device.get_absolute_url()) | |
316 |
|
316 | |||
317 | kwargs = { |
|
317 | kwargs = { | |
318 | 'title': 'Delete', |
|
318 | 'title': 'Delete', | |
319 | 'suptitle': 'Device', |
|
319 | 'suptitle': 'Device', | |
320 | 'object': device, |
|
320 | 'object': device, | |
321 | 'delete': True |
|
321 | 'delete': True | |
322 | } |
|
322 | } | |
323 | kwargs['menu_devices'] = 'active' |
|
323 | kwargs['menu_devices'] = 'active' | |
324 |
|
324 | |||
325 | return render(request, 'confirm.html', kwargs) |
|
325 | return render(request, 'confirm.html', kwargs) | |
326 |
|
326 | |||
327 |
|
327 | |||
328 | @login_required |
|
328 | @login_required | |
329 | def device_change_ip(request, id_dev): |
|
329 | def device_change_ip(request, id_dev): | |
330 |
|
330 | |||
331 | device = get_object_or_404(Device, pk=id_dev) |
|
331 | device = get_object_or_404(Device, pk=id_dev) | |
332 |
|
332 | |||
333 | if request.method == 'POST': |
|
333 | if request.method == 'POST': | |
334 |
|
334 | |||
335 | if is_developer(request.user): |
|
335 | if is_developer(request.user): | |
336 | device.change_ip(**request.POST.dict()) |
|
336 | device.change_ip(**request.POST.dict()) | |
337 |
|
337 | |||
338 | print(device.ip_address, device.message) |
|
338 | print(device.ip_address, device.message) | |
339 |
|
339 | |||
340 | level, message = device.message.split('|') |
|
340 | level, message = device.message.split('|') | |
341 | messages.add_message(request, level, message) |
|
341 | messages.add_message(request, level, message) | |
342 | else: |
|
342 | else: | |
343 | messages.error( |
|
343 | messages.error( | |
344 | request, 'Not enough permission to delete this object') |
|
344 | request, 'Not enough permission to delete this object') | |
345 | return redirect(device.get_absolute_url()) |
|
345 | return redirect(device.get_absolute_url()) | |
346 |
|
346 | |||
347 | kwargs = { |
|
347 | kwargs = { | |
348 | 'title': 'Device', |
|
348 | 'title': 'Device', | |
349 | 'suptitle': 'Change IP', |
|
349 | 'suptitle': 'Change IP', | |
350 | 'object': device, |
|
350 | 'object': device, | |
351 | 'previous': device.get_absolute_url(), |
|
351 | 'previous': device.get_absolute_url(), | |
352 | 'form': ChangeIpForm(initial={'ip_address': device.ip_address}), |
|
352 | 'form': ChangeIpForm(initial={'ip_address': device.ip_address}), | |
353 | 'message': ' ', |
|
353 | 'message': ' ', | |
354 | } |
|
354 | } | |
355 | kwargs['menu_devices'] = 'active' |
|
355 | kwargs['menu_devices'] = 'active' | |
356 |
|
356 | |||
357 | return render(request, 'confirm.html', kwargs) |
|
357 | return render(request, 'confirm.html', kwargs) | |
358 |
|
358 | |||
359 |
|
359 | |||
360 | def campaigns(request): |
|
360 | def campaigns(request): | |
361 |
|
361 | |||
362 | page = request.GET.get('page') |
|
362 | page = request.GET.get('page') | |
363 | order = ('-start_date',) |
|
363 | order = ('-start_date',) | |
364 | filters = request.GET.copy() |
|
364 | filters = request.GET.copy() | |
365 |
|
365 | |||
366 | kwargs = get_paginator(Campaign, page, order, filters) |
|
366 | kwargs = get_paginator(Campaign, page, order, filters) | |
367 |
|
367 | |||
368 | form = FilterForm(initial=request.GET, extra_fields=[ |
|
368 | form = FilterForm(initial=request.GET, extra_fields=[ | |
369 | 'range_date', 'tags', 'template']) |
|
369 | 'range_date', 'tags', 'template']) | |
370 | kwargs['keys'] = ['name', 'start_date', 'end_date', 'actions'] |
|
370 | kwargs['keys'] = ['name', 'start_date', 'end_date', 'actions'] | |
371 | kwargs['title'] = 'Campaign' |
|
371 | kwargs['title'] = 'Campaign' | |
372 | kwargs['suptitle'] = 'List' |
|
372 | kwargs['suptitle'] = 'List' | |
373 | kwargs['no_sidebar'] = True |
|
373 | kwargs['no_sidebar'] = True | |
374 | kwargs['form'] = form |
|
374 | kwargs['form'] = form | |
375 | kwargs['add_url'] = reverse('url_add_campaign') |
|
375 | kwargs['add_url'] = reverse('url_add_campaign') | |
376 | filters.pop('page', None) |
|
376 | filters.pop('page', None) | |
377 | kwargs['q'] = urlencode(filters) |
|
377 | kwargs['q'] = urlencode(filters) | |
378 | kwargs['menu_campaigns'] = 'active' |
|
378 | kwargs['menu_campaigns'] = 'active' | |
379 |
|
379 | |||
380 | return render(request, 'base_list.html', kwargs) |
|
380 | return render(request, 'base_list.html', kwargs) | |
381 |
|
381 | |||
382 |
|
382 | |||
383 | def campaign(request, id_camp): |
|
383 | def campaign(request, id_camp): | |
384 |
|
384 | |||
385 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
385 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
386 | experiments = Experiment.objects.filter(campaign=campaign) |
|
386 | experiments = Experiment.objects.filter(campaign=campaign) | |
387 |
|
387 | |||
388 | form = CampaignForm(instance=campaign) |
|
388 | form = CampaignForm(instance=campaign) | |
389 |
|
389 | |||
390 | kwargs = {} |
|
390 | kwargs = {} | |
391 | kwargs['campaign'] = campaign |
|
391 | kwargs['campaign'] = campaign | |
392 | kwargs['campaign_keys'] = ['template', 'name', |
|
392 | kwargs['campaign_keys'] = ['template', 'name', | |
393 | 'start_date', 'end_date', 'tags', 'description'] |
|
393 | 'start_date', 'end_date', 'tags', 'description'] | |
394 |
|
394 | |||
395 | kwargs['experiments'] = experiments |
|
395 | kwargs['experiments'] = experiments | |
396 | kwargs['experiment_keys'] = [ |
|
396 | kwargs['experiment_keys'] = [ | |
397 | 'name', 'radar_system', 'start_time', 'end_time'] |
|
397 | 'name', 'radar_system', 'start_time', 'end_time'] | |
398 |
|
398 | |||
399 | kwargs['title'] = 'Campaign' |
|
399 | kwargs['title'] = 'Campaign' | |
400 | kwargs['suptitle'] = 'Details' |
|
400 | kwargs['suptitle'] = 'Details' | |
401 |
|
401 | |||
402 | kwargs['form'] = form |
|
402 | kwargs['form'] = form | |
403 | kwargs['button'] = 'Add Experiment' |
|
403 | kwargs['button'] = 'Add Experiment' | |
404 | kwargs['menu_campaigns'] = 'active' |
|
404 | kwargs['menu_campaigns'] = 'active' | |
405 |
|
405 | |||
406 | return render(request, 'campaign.html', kwargs) |
|
406 | return render(request, 'campaign.html', kwargs) | |
407 |
|
407 | |||
408 |
|
408 | |||
409 | @login_required |
|
409 | @login_required | |
410 | def campaign_new(request): |
|
410 | def campaign_new(request): | |
411 |
|
411 | |||
412 | kwargs = {} |
|
412 | kwargs = {} | |
413 |
|
413 | |||
414 | if request.method == 'GET': |
|
414 | if request.method == 'GET': | |
415 |
|
415 | |||
416 | if 'template' in request.GET: |
|
416 | if 'template' in request.GET: | |
417 | if request.GET['template'] == '0': |
|
417 | if request.GET['template'] == '0': | |
418 | form = NewForm(initial={'create_from': 2}, |
|
418 | form = NewForm(initial={'create_from': 2}, | |
419 | template_choices=Campaign.objects.filter(template=True).values_list('id', 'name')) |
|
419 | template_choices=Campaign.objects.filter(template=True).values_list('id', 'name')) | |
420 | else: |
|
420 | else: | |
421 | kwargs['button'] = 'Create' |
|
421 | kwargs['button'] = 'Create' | |
422 | kwargs['experiments'] = Configuration.objects.filter( |
|
422 | kwargs['experiments'] = Configuration.objects.filter( | |
423 | experiment=request.GET['template']) |
|
423 | experiment=request.GET['template']) | |
424 | kwargs['experiment_keys'] = ['name', 'start_time', 'end_time'] |
|
424 | kwargs['experiment_keys'] = ['name', 'start_time', 'end_time'] | |
425 | camp = Campaign.objects.get(pk=request.GET['template']) |
|
425 | camp = Campaign.objects.get(pk=request.GET['template']) | |
426 | form = CampaignForm(instance=camp, |
|
426 | form = CampaignForm(instance=camp, | |
427 | initial={'name': '{}_{:%Y%m%d%H%M%S}'.format(camp.name, datetime.now()), |
|
427 | initial={'name': '{}_{:%Y%m%d%H%M%S}'.format(camp.name, datetime.now()), | |
428 | 'template': False}) |
|
428 | 'template': False}) | |
429 | elif 'blank' in request.GET: |
|
429 | elif 'blank' in request.GET: | |
430 | kwargs['button'] = 'Create' |
|
430 | kwargs['button'] = 'Create' | |
431 | form = CampaignForm() |
|
431 | form = CampaignForm() | |
432 | else: |
|
432 | else: | |
433 | form = NewForm() |
|
433 | form = NewForm() | |
434 |
|
434 | |||
435 | if request.method == 'POST': |
|
435 | if request.method == 'POST': | |
436 | kwargs['button'] = 'Create' |
|
436 | kwargs['button'] = 'Create' | |
437 | post = request.POST.copy() |
|
437 | post = request.POST.copy() | |
438 | experiments = [] |
|
438 | experiments = [] | |
439 |
|
439 | |||
440 | for id_exp in post.getlist('experiments'): |
|
440 | for id_exp in post.getlist('experiments'): | |
441 | exp = Experiment.objects.get(pk=id_exp) |
|
441 | exp = Experiment.objects.get(pk=id_exp) | |
442 | new_exp = exp.clone(template=False) |
|
442 | new_exp = exp.clone(template=False) | |
443 | experiments.append(new_exp) |
|
443 | experiments.append(new_exp) | |
444 |
|
444 | |||
445 | post.setlist('experiments', []) |
|
445 | post.setlist('experiments', []) | |
446 |
|
446 | |||
447 | form = CampaignForm(post) |
|
447 | form = CampaignForm(post) | |
448 |
|
448 | |||
449 | if form.is_valid(): |
|
449 | if form.is_valid(): | |
450 | campaign = form.save(commit=False) |
|
450 | campaign = form.save(commit=False) | |
451 | campaign.author = request.user |
|
451 | campaign.author = request.user | |
452 | campaign.save() |
|
452 | campaign.save() | |
453 | for exp in experiments: |
|
453 | for exp in experiments: | |
454 | campaign.experiments.add(exp) |
|
454 | campaign.experiments.add(exp) | |
455 |
|
455 | |||
456 | return redirect('url_campaign', id_camp=campaign.id) |
|
456 | return redirect('url_campaign', id_camp=campaign.id) | |
457 |
|
457 | |||
458 | kwargs['form'] = form |
|
458 | kwargs['form'] = form | |
459 | kwargs['title'] = 'Campaign' |
|
459 | kwargs['title'] = 'Campaign' | |
460 | kwargs['suptitle'] = 'New' |
|
460 | kwargs['suptitle'] = 'New' | |
461 | kwargs['menu_campaigns'] = 'active' |
|
461 | kwargs['menu_campaigns'] = 'active' | |
462 |
|
462 | |||
463 | return render(request, 'campaign_edit.html', kwargs) |
|
463 | return render(request, 'campaign_edit.html', kwargs) | |
464 |
|
464 | |||
465 |
|
465 | |||
466 | @login_required |
|
466 | @login_required | |
467 | def campaign_edit(request, id_camp): |
|
467 | def campaign_edit(request, id_camp): | |
468 |
|
468 | |||
469 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
469 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
470 |
|
470 | |||
471 | if request.method == 'GET': |
|
471 | if request.method == 'GET': | |
472 | form = CampaignForm(instance=campaign) |
|
472 | form = CampaignForm(instance=campaign) | |
473 |
|
473 | |||
474 | if request.method == 'POST': |
|
474 | if request.method == 'POST': | |
475 | exps = campaign.experiments.all().values_list('pk', flat=True) |
|
475 | exps = campaign.experiments.all().values_list('pk', flat=True) | |
476 | post = request.POST.copy() |
|
476 | post = request.POST.copy() | |
477 | new_exps = post.getlist('experiments') |
|
477 | new_exps = post.getlist('experiments') | |
478 | post.setlist('experiments', []) |
|
478 | post.setlist('experiments', []) | |
479 | form = CampaignForm(post, instance=campaign) |
|
479 | form = CampaignForm(post, instance=campaign) | |
480 |
|
480 | |||
481 | if form.is_valid(): |
|
481 | if form.is_valid(): | |
482 | camp = form.save() |
|
482 | camp = form.save() | |
483 | for id_exp in new_exps: |
|
483 | for id_exp in new_exps: | |
484 | if int(id_exp) in exps: |
|
484 | if int(id_exp) in exps: | |
485 | exps.pop(id_exp) |
|
485 | exps.pop(id_exp) | |
486 | else: |
|
486 | else: | |
487 | exp = Experiment.objects.get(pk=id_exp) |
|
487 | exp = Experiment.objects.get(pk=id_exp) | |
488 | if exp.template: |
|
488 | if exp.template: | |
489 | camp.experiments.add(exp.clone(template=False)) |
|
489 | camp.experiments.add(exp.clone(template=False)) | |
490 | else: |
|
490 | else: | |
491 | camp.experiments.add(exp) |
|
491 | camp.experiments.add(exp) | |
492 |
|
492 | |||
493 | for id_exp in exps: |
|
493 | for id_exp in exps: | |
494 | camp.experiments.remove(Experiment.objects.get(pk=id_exp)) |
|
494 | camp.experiments.remove(Experiment.objects.get(pk=id_exp)) | |
495 |
|
495 | |||
496 | return redirect('url_campaign', id_camp=id_camp) |
|
496 | return redirect('url_campaign', id_camp=id_camp) | |
497 |
|
497 | |||
498 | kwargs = {} |
|
498 | kwargs = {} | |
499 | kwargs['form'] = form |
|
499 | kwargs['form'] = form | |
500 | kwargs['title'] = 'Campaign' |
|
500 | kwargs['title'] = 'Campaign' | |
501 | kwargs['suptitle'] = 'Edit' |
|
501 | kwargs['suptitle'] = 'Edit' | |
502 | kwargs['button'] = 'Update' |
|
502 | kwargs['button'] = 'Update' | |
503 | kwargs['menu_campaigns'] = 'active' |
|
503 | kwargs['menu_campaigns'] = 'active' | |
504 |
|
504 | |||
505 | return render(request, 'campaign_edit.html', kwargs) |
|
505 | return render(request, 'campaign_edit.html', kwargs) | |
506 |
|
506 | |||
507 |
|
507 | |||
508 | @login_required |
|
508 | @login_required | |
509 | def campaign_delete(request, id_camp): |
|
509 | def campaign_delete(request, id_camp): | |
510 |
|
510 | |||
511 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
511 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
512 |
|
512 | |||
513 | if request.method == 'POST': |
|
513 | if request.method == 'POST': | |
514 | if is_developer(request.user): |
|
514 | if is_developer(request.user): | |
515 |
|
515 | |||
516 | for exp in campaign.experiments.all(): |
|
516 | for exp in campaign.experiments.all(): | |
517 | for conf in Configuration.objects.filter(experiment=exp): |
|
517 | for conf in Configuration.objects.filter(experiment=exp): | |
518 | conf.delete() |
|
518 | conf.delete() | |
519 | exp.delete() |
|
519 | exp.delete() | |
520 | campaign.delete() |
|
520 | campaign.delete() | |
521 |
|
521 | |||
522 | return redirect('url_campaigns') |
|
522 | return redirect('url_campaigns') | |
523 |
|
523 | |||
524 | messages.error(request, 'Not enough permission to delete this object') |
|
524 | messages.error(request, 'Not enough permission to delete this object') | |
525 | return redirect(campaign.get_absolute_url()) |
|
525 | return redirect(campaign.get_absolute_url()) | |
526 |
|
526 | |||
527 | kwargs = { |
|
527 | kwargs = { | |
528 | 'title': 'Delete', |
|
528 | 'title': 'Delete', | |
529 | 'suptitle': 'Campaign', |
|
529 | 'suptitle': 'Campaign', | |
530 | 'object': campaign, |
|
530 | 'object': campaign, | |
531 | 'delete': True |
|
531 | 'delete': True | |
532 | } |
|
532 | } | |
533 | kwargs['menu_campaigns'] = 'active' |
|
533 | kwargs['menu_campaigns'] = 'active' | |
534 |
|
534 | |||
535 | return render(request, 'confirm.html', kwargs) |
|
535 | return render(request, 'confirm.html', kwargs) | |
536 |
|
536 | |||
537 |
|
537 | |||
538 | @login_required |
|
538 | @login_required | |
539 | def campaign_export(request, id_camp): |
|
539 | def campaign_export(request, id_camp): | |
540 |
|
540 | |||
541 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
541 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
542 | content = campaign.parms_to_dict() |
|
542 | content = campaign.parms_to_dict() | |
543 | content_type = 'application/json' |
|
543 | content_type = 'application/json' | |
544 | filename = '%s_%s.json' % (campaign.name, campaign.id) |
|
544 | filename = '%s_%s.json' % (campaign.name, campaign.id) | |
545 |
|
545 | |||
546 | response = HttpResponse(content_type=content_type) |
|
546 | response = HttpResponse(content_type=content_type) | |
547 | response['Content-Disposition'] = 'attachment; filename="%s"' % filename |
|
547 | response['Content-Disposition'] = 'attachment; filename="%s"' % filename | |
548 | response.write(json.dumps(content, indent=2)) |
|
548 | response.write(json.dumps(content, indent=2)) | |
549 |
|
549 | |||
550 | return response |
|
550 | return response | |
551 |
|
551 | |||
552 |
|
552 | |||
553 | @login_required |
|
553 | @login_required | |
554 | def campaign_import(request, id_camp): |
|
554 | def campaign_import(request, id_camp): | |
555 |
|
555 | |||
556 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
556 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
557 |
|
557 | |||
558 | if request.method == 'GET': |
|
558 | if request.method == 'GET': | |
559 | file_form = UploadFileForm() |
|
559 | file_form = UploadFileForm() | |
560 |
|
560 | |||
561 | if request.method == 'POST': |
|
561 | if request.method == 'POST': | |
562 | file_form = UploadFileForm(request.POST, request.FILES) |
|
562 | file_form = UploadFileForm(request.POST, request.FILES) | |
563 |
|
563 | |||
564 | if file_form.is_valid(): |
|
564 | if file_form.is_valid(): | |
565 | new_camp = campaign.dict_to_parms( |
|
565 | new_camp = campaign.dict_to_parms( | |
566 | json.load(request.FILES['file']), CONF_MODELS) |
|
566 | json.load(request.FILES['file']), CONF_MODELS) | |
567 | messages.success( |
|
567 | messages.success( | |
568 | request, "Parameters imported from: '%s'." % request.FILES['file'].name) |
|
568 | request, "Parameters imported from: '%s'." % request.FILES['file'].name) | |
569 | return redirect(new_camp.get_absolute_url_edit()) |
|
569 | return redirect(new_camp.get_absolute_url_edit()) | |
570 |
|
570 | |||
571 | messages.error(request, "Could not import parameters from file") |
|
571 | messages.error(request, "Could not import parameters from file") | |
572 |
|
572 | |||
573 | kwargs = {} |
|
573 | kwargs = {} | |
574 | kwargs['title'] = 'Campaign' |
|
574 | kwargs['title'] = 'Campaign' | |
575 | kwargs['form'] = file_form |
|
575 | kwargs['form'] = file_form | |
576 | kwargs['suptitle'] = 'Importing file' |
|
576 | kwargs['suptitle'] = 'Importing file' | |
577 | kwargs['button'] = 'Import' |
|
577 | kwargs['button'] = 'Import' | |
578 | kwargs['menu_campaigns'] = 'active' |
|
578 | kwargs['menu_campaigns'] = 'active' | |
579 |
|
579 | |||
580 | return render(request, 'campaign_import.html', kwargs) |
|
580 | return render(request, 'campaign_import.html', kwargs) | |
581 |
|
581 | |||
582 |
|
582 | |||
583 | def experiments(request): |
|
583 | def experiments(request): | |
584 |
|
584 | |||
585 | page = request.GET.get('page') |
|
585 | page = request.GET.get('page') | |
586 | order = ('location',) |
|
586 | order = ('location',) | |
587 | filters = request.GET.copy() |
|
587 | filters = request.GET.copy() | |
588 |
|
588 | |||
589 | if 'my experiments' in filters: |
|
589 | if 'my experiments' in filters: | |
590 | filters.pop('my experiments', None) |
|
590 | filters.pop('my experiments', None) | |
591 | filters['mine'] = request.user.id |
|
591 | filters['mine'] = request.user.id | |
592 |
|
592 | |||
593 | kwargs = get_paginator(Experiment, page, order, filters) |
|
593 | kwargs = get_paginator(Experiment, page, order, filters) | |
594 |
|
594 | |||
595 | fields = ['tags', 'template'] |
|
595 | fields = ['tags', 'template'] | |
596 | if request.user.is_authenticated: |
|
596 | if request.user.is_authenticated: | |
597 | fields.append('my experiments') |
|
597 | fields.append('my experiments') | |
598 |
|
598 | |||
599 | form = FilterForm(initial=request.GET, extra_fields=fields) |
|
599 | form = FilterForm(initial=request.GET, extra_fields=fields) | |
600 |
|
600 | |||
601 | kwargs['keys'] = ['name', 'radar_system', |
|
601 | kwargs['keys'] = ['name', 'radar_system', | |
602 | 'start_time', 'end_time', 'actions'] |
|
602 | 'start_time', 'end_time', 'actions'] | |
603 | kwargs['title'] = 'Experiment' |
|
603 | kwargs['title'] = 'Experiment' | |
604 | kwargs['suptitle'] = 'List' |
|
604 | kwargs['suptitle'] = 'List' | |
605 | kwargs['no_sidebar'] = True |
|
605 | kwargs['no_sidebar'] = True | |
606 | kwargs['form'] = form |
|
606 | kwargs['form'] = form | |
607 | kwargs['add_url'] = reverse('url_add_experiment') |
|
607 | kwargs['add_url'] = reverse('url_add_experiment') | |
608 | filters = request.GET.copy() |
|
608 | filters = request.GET.copy() | |
609 | filters.pop('page', None) |
|
609 | filters.pop('page', None) | |
610 | kwargs['q'] = urlencode(filters) |
|
610 | kwargs['q'] = urlencode(filters) | |
611 | kwargs['menu_experiments'] = 'active' |
|
611 | kwargs['menu_experiments'] = 'active' | |
612 |
|
612 | |||
613 | return render(request, 'base_list.html', kwargs) |
|
613 | return render(request, 'base_list.html', kwargs) | |
614 |
|
614 | |||
615 |
|
615 | |||
616 | def experiment(request, id_exp): |
|
616 | def experiment(request, id_exp): | |
617 |
|
617 | |||
618 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
618 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
619 |
|
619 | |||
620 | configurations = Configuration.objects.filter( |
|
620 | configurations = Configuration.objects.filter( | |
621 | experiment=experiment, type=0) |
|
621 | experiment=experiment, type=0) | |
622 |
|
622 | |||
623 | kwargs = {} |
|
623 | kwargs = {} | |
624 |
|
624 | |||
625 | kwargs['experiment_keys'] = ['template', 'radar_system', |
|
625 | kwargs['experiment_keys'] = ['template', 'radar_system', | |
626 | 'name', 'freq', 'start_time', 'end_time'] |
|
626 | 'name', 'freq', 'start_time', 'end_time'] | |
627 | kwargs['experiment'] = experiment |
|
627 | kwargs['experiment'] = experiment | |
628 | kwargs['configuration_keys'] = ['name', 'device__ip_address', |
|
628 | kwargs['configuration_keys'] = ['name', 'device__ip_address', | |
629 | 'device__port_address', 'device__status'] |
|
629 | 'device__port_address', 'device__status'] | |
630 | kwargs['configurations'] = configurations |
|
630 | kwargs['configurations'] = configurations | |
631 | kwargs['title'] = 'Experiment' |
|
631 | kwargs['title'] = 'Experiment' | |
632 | kwargs['suptitle'] = 'Details' |
|
632 | kwargs['suptitle'] = 'Details' | |
633 | kwargs['button'] = 'Add Configuration' |
|
633 | kwargs['button'] = 'Add Configuration' | |
634 | kwargs['menu_experiments'] = 'active' |
|
634 | kwargs['menu_experiments'] = 'active' | |
635 |
|
635 | |||
636 | ###### SIDEBAR ###### |
|
636 | ###### SIDEBAR ###### | |
637 | kwargs.update(sidebar(experiment=experiment)) |
|
637 | kwargs.update(sidebar(experiment=experiment)) | |
638 |
|
638 | |||
639 | return render(request, 'experiment.html', kwargs) |
|
639 | return render(request, 'experiment.html', kwargs) | |
640 |
|
640 | |||
641 |
|
641 | |||
642 | @login_required |
|
642 | @login_required | |
643 | def experiment_new(request, id_camp=None): |
|
643 | def experiment_new(request, id_camp=None): | |
644 |
|
644 | |||
645 | if not is_developer(request.user): |
|
645 | if not is_developer(request.user): | |
646 | messages.error( |
|
646 | messages.error( | |
647 | request, 'Developer required, to create new Experiments') |
|
647 | request, 'Developer required, to create new Experiments') | |
648 | return redirect('index') |
|
648 | return redirect('index') | |
649 | kwargs = {} |
|
649 | kwargs = {} | |
650 |
|
650 | |||
651 | if request.method == 'GET': |
|
651 | if request.method == 'GET': | |
652 | if 'template' in request.GET: |
|
652 | if 'template' in request.GET: | |
653 | if request.GET['template'] == '0': |
|
653 | if request.GET['template'] == '0': | |
654 | form = NewForm(initial={'create_from': 2}, |
|
654 | form = NewForm(initial={'create_from': 2}, | |
655 | template_choices=Experiment.objects.filter(template=True).values_list('id', 'name')) |
|
655 | template_choices=Experiment.objects.filter(template=True).values_list('id', 'name')) | |
656 | else: |
|
656 | else: | |
657 | kwargs['button'] = 'Create' |
|
657 | kwargs['button'] = 'Create' | |
658 | kwargs['configurations'] = Configuration.objects.filter( |
|
658 | kwargs['configurations'] = Configuration.objects.filter( | |
659 | experiment=request.GET['template']) |
|
659 | experiment=request.GET['template']) | |
660 | kwargs['configuration_keys'] = ['name', 'device__name', |
|
660 | kwargs['configuration_keys'] = ['name', 'device__name', | |
661 | 'device__ip_address', 'device__port_address'] |
|
661 | 'device__ip_address', 'device__port_address'] | |
662 | exp = Experiment.objects.get(pk=request.GET['template']) |
|
662 | exp = Experiment.objects.get(pk=request.GET['template']) | |
663 | form = ExperimentForm(instance=exp, |
|
663 | form = ExperimentForm(instance=exp, | |
664 | initial={'name': '{}_{:%y%m%d}'.format(exp.name, datetime.now()), |
|
664 | initial={'name': '{}_{:%y%m%d}'.format(exp.name, datetime.now()), | |
665 | 'template': False}) |
|
665 | 'template': False}) | |
666 | elif 'blank' in request.GET: |
|
666 | elif 'blank' in request.GET: | |
667 | kwargs['button'] = 'Create' |
|
667 | kwargs['button'] = 'Create' | |
668 | form = ExperimentForm() |
|
668 | form = ExperimentForm() | |
669 | else: |
|
669 | else: | |
670 | form = NewForm() |
|
670 | form = NewForm() | |
671 |
|
671 | |||
672 | if request.method == 'POST': |
|
672 | if request.method == 'POST': | |
673 | form = ExperimentForm(request.POST) |
|
673 | form = ExperimentForm(request.POST) | |
674 | if form.is_valid(): |
|
674 | if form.is_valid(): | |
675 | experiment = form.save(commit=False) |
|
675 | experiment = form.save(commit=False) | |
676 | experiment.author = request.user |
|
676 | experiment.author = request.user | |
677 | experiment.save() |
|
677 | experiment.save() | |
678 |
|
678 | |||
679 | if 'template' in request.GET: |
|
679 | if 'template' in request.GET: | |
680 | configurations = Configuration.objects.filter( |
|
680 | configurations = Configuration.objects.filter( | |
681 | experiment=request.GET['template'], type=0) |
|
681 | experiment=request.GET['template'], type=0) | |
682 | for conf in configurations: |
|
682 | for conf in configurations: | |
683 | conf.clone(experiment=experiment, template=False) |
|
683 | conf.clone(experiment=experiment, template=False) | |
684 |
|
684 | |||
685 | return redirect('url_experiment', id_exp=experiment.id) |
|
685 | return redirect('url_experiment', id_exp=experiment.id) | |
686 |
|
686 | |||
687 | kwargs['form'] = form |
|
687 | kwargs['form'] = form | |
688 | kwargs['title'] = 'Experiment' |
|
688 | kwargs['title'] = 'Experiment' | |
689 | kwargs['suptitle'] = 'New' |
|
689 | kwargs['suptitle'] = 'New' | |
690 | kwargs['menu_experiments'] = 'active' |
|
690 | kwargs['menu_experiments'] = 'active' | |
691 |
|
691 | |||
692 | return render(request, 'experiment_edit.html', kwargs) |
|
692 | return render(request, 'experiment_edit.html', kwargs) | |
693 |
|
693 | |||
694 |
|
694 | |||
695 | @login_required |
|
695 | @login_required | |
696 | def experiment_edit(request, id_exp): |
|
696 | def experiment_edit(request, id_exp): | |
697 |
|
697 | |||
698 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
698 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
699 |
|
699 | |||
700 | if request.method == 'GET': |
|
700 | if request.method == 'GET': | |
701 | form = ExperimentForm(instance=experiment) |
|
701 | form = ExperimentForm(instance=experiment) | |
702 |
|
702 | |||
703 | if request.method == 'POST': |
|
703 | if request.method == 'POST': | |
704 | form = ExperimentForm(request.POST, instance=experiment) |
|
704 | form = ExperimentForm(request.POST, instance=experiment) | |
705 |
|
705 | |||
706 | if form.is_valid(): |
|
706 | if form.is_valid(): | |
707 | experiment = form.save() |
|
707 | experiment = form.save() | |
708 | return redirect('url_experiment', id_exp=experiment.id) |
|
708 | return redirect('url_experiment', id_exp=experiment.id) | |
709 |
|
709 | |||
710 | kwargs = {} |
|
710 | kwargs = {} | |
711 | kwargs['form'] = form |
|
711 | kwargs['form'] = form | |
712 | kwargs['title'] = 'Experiment' |
|
712 | kwargs['title'] = 'Experiment' | |
713 | kwargs['suptitle'] = 'Edit' |
|
713 | kwargs['suptitle'] = 'Edit' | |
714 | kwargs['button'] = 'Update' |
|
714 | kwargs['button'] = 'Update' | |
715 | kwargs['menu_experiments'] = 'active' |
|
715 | kwargs['menu_experiments'] = 'active' | |
716 |
|
716 | |||
717 | return render(request, 'experiment_edit.html', kwargs) |
|
717 | return render(request, 'experiment_edit.html', kwargs) | |
718 |
|
718 | |||
719 |
|
719 | |||
720 | @login_required |
|
720 | @login_required | |
721 | def experiment_delete(request, id_exp): |
|
721 | def experiment_delete(request, id_exp): | |
722 |
|
722 | |||
723 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
723 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
724 |
|
724 | |||
725 | if request.method == 'POST': |
|
725 | if request.method == 'POST': | |
726 | if is_developer(request.user): |
|
726 | if is_developer(request.user): | |
727 | for conf in Configuration.objects.filter(experiment=experiment): |
|
727 | for conf in Configuration.objects.filter(experiment=experiment): | |
728 | conf.delete() |
|
728 | conf.delete() | |
729 | experiment.delete() |
|
729 | experiment.delete() | |
730 | return redirect('url_experiments') |
|
730 | return redirect('url_experiments') | |
731 |
|
731 | |||
732 | messages.error(request, 'Not enough permission to delete this object') |
|
732 | messages.error(request, 'Not enough permission to delete this object') | |
733 | return redirect(experiment.get_absolute_url()) |
|
733 | return redirect(experiment.get_absolute_url()) | |
734 |
|
734 | |||
735 | kwargs = { |
|
735 | kwargs = { | |
736 | 'title': 'Delete', |
|
736 | 'title': 'Delete', | |
737 | 'suptitle': 'Experiment', |
|
737 | 'suptitle': 'Experiment', | |
738 | 'object': experiment, |
|
738 | 'object': experiment, | |
739 | 'delete': True |
|
739 | 'delete': True | |
740 | } |
|
740 | } | |
741 |
|
741 | |||
742 | return render(request, 'confirm.html', kwargs) |
|
742 | return render(request, 'confirm.html', kwargs) | |
743 |
|
743 | |||
744 |
|
744 | |||
745 | @login_required |
|
745 | @login_required | |
746 | def experiment_export(request, id_exp): |
|
746 | def experiment_export(request, id_exp): | |
747 |
|
747 | |||
748 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
748 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
749 | content = experiment.parms_to_dict() |
|
749 | content = experiment.parms_to_dict() | |
750 | content_type = 'application/json' |
|
750 | content_type = 'application/json' | |
751 | filename = '%s_%s.json' % (experiment.name, experiment.id) |
|
751 | filename = '%s_%s.json' % (experiment.name, experiment.id) | |
752 |
|
752 | |||
753 | response = HttpResponse(content_type=content_type) |
|
753 | response = HttpResponse(content_type=content_type) | |
754 | response['Content-Disposition'] = 'attachment; filename="%s"' % filename |
|
754 | response['Content-Disposition'] = 'attachment; filename="%s"' % filename | |
755 | response.write(json.dumps(content, indent=2)) |
|
755 | response.write(json.dumps(content, indent=2)) | |
756 |
|
756 | |||
757 | return response |
|
757 | return response | |
758 |
|
758 | |||
759 |
|
759 | |||
760 | @login_required |
|
760 | @login_required | |
761 | def experiment_import(request, id_exp): |
|
761 | def experiment_import(request, id_exp): | |
762 |
|
762 | |||
763 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
763 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
764 | configurations = Configuration.objects.filter(experiment=experiment) |
|
764 | configurations = Configuration.objects.filter(experiment=experiment) | |
765 |
|
765 | |||
766 | if request.method == 'GET': |
|
766 | if request.method == 'GET': | |
767 | file_form = UploadFileForm() |
|
767 | file_form = UploadFileForm() | |
768 |
|
768 | |||
769 | if request.method == 'POST': |
|
769 | if request.method == 'POST': | |
770 | file_form = UploadFileForm(request.POST, request.FILES) |
|
770 | file_form = UploadFileForm(request.POST, request.FILES) | |
771 |
|
771 | |||
772 | if file_form.is_valid(): |
|
772 | if file_form.is_valid(): | |
773 | new_exp = experiment.dict_to_parms( |
|
773 | new_exp = experiment.dict_to_parms( | |
774 | json.load(request.FILES['file']), CONF_MODELS) |
|
774 | json.load(request.FILES['file']), CONF_MODELS) | |
775 | messages.success( |
|
775 | messages.success( | |
776 | request, "Parameters imported from: '%s'." % request.FILES['file'].name) |
|
776 | request, "Parameters imported from: '%s'." % request.FILES['file'].name) | |
777 | return redirect(new_exp.get_absolute_url_edit()) |
|
777 | return redirect(new_exp.get_absolute_url_edit()) | |
778 |
|
778 | |||
779 | messages.error(request, "Could not import parameters from file") |
|
779 | messages.error(request, "Could not import parameters from file") | |
780 |
|
780 | |||
781 | kwargs = {} |
|
781 | kwargs = {} | |
782 | kwargs['title'] = 'Experiment' |
|
782 | kwargs['title'] = 'Experiment' | |
783 | kwargs['form'] = file_form |
|
783 | kwargs['form'] = file_form | |
784 | kwargs['suptitle'] = 'Importing file' |
|
784 | kwargs['suptitle'] = 'Importing file' | |
785 | kwargs['button'] = 'Import' |
|
785 | kwargs['button'] = 'Import' | |
786 | kwargs['menu_experiments'] = 'active' |
|
786 | kwargs['menu_experiments'] = 'active' | |
787 |
|
787 | |||
788 | kwargs.update(sidebar(experiment=experiment)) |
|
788 | kwargs.update(sidebar(experiment=experiment)) | |
789 |
|
789 | |||
790 | return render(request, 'experiment_import.html', kwargs) |
|
790 | return render(request, 'experiment_import.html', kwargs) | |
791 |
|
791 | |||
792 |
|
792 | |||
793 | @login_required |
|
793 | @login_required | |
794 | def experiment_start(request, id_exp): |
|
794 | def experiment_start(request, id_exp): | |
795 |
|
795 | |||
796 | exp = get_object_or_404(Experiment, pk=id_exp) |
|
796 | exp = get_object_or_404(Experiment, pk=id_exp) | |
797 |
|
797 | |||
798 | if exp.status == 2: |
|
798 | if exp.status == 2: | |
799 | messages.warning(request, 'Experiment {} already runnnig'.format(exp)) |
|
799 | messages.warning(request, 'Experiment {} already runnnig'.format(exp)) | |
800 | else: |
|
800 | else: | |
801 | exp.status = exp.start() |
|
801 | exp.status = exp.start() | |
802 | if exp.status == 0: |
|
802 | if exp.status == 0: | |
803 | messages.error(request, 'Experiment {} not start'.format(exp)) |
|
803 | messages.error(request, 'Experiment {} not start'.format(exp)) | |
804 | if exp.status == 2: |
|
804 | if exp.status == 2: | |
805 | messages.success(request, 'Experiment {} started'.format(exp)) |
|
805 | messages.success(request, 'Experiment {} started'.format(exp)) | |
806 |
|
806 | |||
807 | exp.save() |
|
807 | exp.save() | |
808 |
|
808 | |||
809 | return redirect(exp.get_absolute_url()) |
|
809 | return redirect(exp.get_absolute_url()) | |
810 |
|
810 | |||
811 |
|
811 | |||
812 | @login_required |
|
812 | @login_required | |
813 | def experiment_stop(request, id_exp): |
|
813 | def experiment_stop(request, id_exp): | |
814 |
|
814 | |||
815 | exp = get_object_or_404(Experiment, pk=id_exp) |
|
815 | exp = get_object_or_404(Experiment, pk=id_exp) | |
816 |
|
816 | |||
817 | if exp.status == 2: |
|
817 | if exp.status == 2: | |
818 | exp.status = exp.stop() |
|
818 | exp.status = exp.stop() | |
819 | exp.save() |
|
819 | exp.save() | |
820 | messages.success(request, 'Experiment {} stopped'.format(exp)) |
|
820 | messages.success(request, 'Experiment {} stopped'.format(exp)) | |
821 | else: |
|
821 | else: | |
822 | messages.error(request, 'Experiment {} not running'.format(exp)) |
|
822 | messages.error(request, 'Experiment {} not running'.format(exp)) | |
823 |
|
823 | |||
824 | return redirect(exp.get_absolute_url()) |
|
824 | return redirect(exp.get_absolute_url()) | |
825 |
|
825 | |||
826 |
|
826 | |||
827 | def experiment_status(request, id_exp): |
|
827 | def experiment_status(request, id_exp): | |
828 |
|
828 | |||
829 | exp = get_object_or_404(Experiment, pk=id_exp) |
|
829 | exp = get_object_or_404(Experiment, pk=id_exp) | |
830 |
|
830 | |||
831 | exp.get_status() |
|
831 | exp.get_status() | |
832 |
|
832 | |||
833 | return redirect(exp.get_absolute_url()) |
|
833 | return redirect(exp.get_absolute_url()) | |
834 |
|
834 | |||
835 |
|
835 | |||
836 | @login_required |
|
836 | @login_required | |
837 | def experiment_mix(request, id_exp): |
|
837 | def experiment_mix(request, id_exp): | |
838 |
|
838 | |||
839 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
839 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
840 | rc_confs = [conf for conf in RCConfiguration.objects.filter( |
|
840 | rc_confs = [conf for conf in RCConfiguration.objects.filter( | |
841 | experiment=id_exp, |
|
841 | experiment=id_exp, | |
842 | type=0, |
|
842 | type=0, | |
843 | mix=False)] |
|
843 | mix=False)] | |
844 |
|
844 | |||
845 | if len(rc_confs) < 2: |
|
845 | if len(rc_confs) < 2: | |
846 | messages.warning( |
|
846 | messages.warning( | |
847 | request, 'You need at least two RC Configurations to make a mix') |
|
847 | request, 'You need at least two RC Configurations to make a mix') | |
848 | return redirect(experiment.get_absolute_url()) |
|
848 | return redirect(experiment.get_absolute_url()) | |
849 |
|
849 | |||
850 | mix_confs = RCConfiguration.objects.filter(experiment=id_exp, mix=True, type=0) |
|
850 | mix_confs = RCConfiguration.objects.filter(experiment=id_exp, mix=True, type=0) | |
851 |
|
851 | |||
852 | if mix_confs: |
|
852 | if mix_confs: | |
853 | mix = mix_confs[0] |
|
853 | mix = mix_confs[0] | |
854 | else: |
|
854 | else: | |
855 | mix = RCConfiguration(experiment=experiment, |
|
855 | mix = RCConfiguration(experiment=experiment, | |
856 | device=rc_confs[0].device, |
|
856 | device=rc_confs[0].device, | |
857 | ipp=rc_confs[0].ipp, |
|
857 | ipp=rc_confs[0].ipp, | |
858 | clock_in=rc_confs[0].clock_in, |
|
858 | clock_in=rc_confs[0].clock_in, | |
859 | clock_divider=rc_confs[0].clock_divider, |
|
859 | clock_divider=rc_confs[0].clock_divider, | |
860 | mix=True, |
|
860 | mix=True, | |
861 | parameters='') |
|
861 | parameters='') | |
862 | mix.save() |
|
862 | mix.save() | |
863 |
|
863 | |||
864 | line_type = RCLineType.objects.get(name='mix') |
|
864 | line_type = RCLineType.objects.get(name='mix') | |
865 | print("VIew obteniendo len getlines") |
|
865 | print("VIew obteniendo len getlines") | |
866 | print(len(rc_confs[0].get_lines())) |
|
866 | print(len(rc_confs[0].get_lines())) | |
867 | for i in range(len(rc_confs[0].get_lines())): |
|
867 | for i in range(len(rc_confs[0].get_lines())): | |
868 | line = RCLine(rc_configuration=mix, line_type=line_type, channel=i) |
|
868 | line = RCLine(rc_configuration=mix, line_type=line_type, channel=i) | |
869 | line.save() |
|
869 | line.save() | |
870 |
|
870 | |||
871 | initial = {'name': mix.name, |
|
871 | initial = {'name': mix.name, | |
872 | 'result': parse_mix_result(mix.parameters), |
|
872 | 'result': parse_mix_result(mix.parameters), | |
873 | 'delay': 0, |
|
873 | 'delay': 0, | |
874 | 'mask': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] |
|
874 | 'mask': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] | |
875 | } |
|
875 | } | |
876 |
|
876 | |||
877 | if request.method == 'GET': |
|
877 | if request.method == 'GET': | |
878 | form = RCMixConfigurationForm(confs=rc_confs, initial=initial) |
|
878 | form = RCMixConfigurationForm(confs=rc_confs, initial=initial) | |
879 |
|
879 | |||
880 | if request.method == 'POST': |
|
880 | if request.method == 'POST': | |
881 | result = mix.parameters |
|
881 | result = mix.parameters | |
882 |
|
882 | |||
883 | if '{}|'.format(request.POST['experiment']) in result: |
|
883 | if '{}|'.format(request.POST['experiment']) in result: | |
884 | messages.error(request, 'Configuration already added') |
|
884 | messages.error(request, 'Configuration already added') | |
885 | else: |
|
885 | else: | |
886 | if 'operation' in request.POST: |
|
886 | if 'operation' in request.POST: | |
887 | operation = MIX_OPERATIONS[request.POST['operation']] |
|
887 | operation = MIX_OPERATIONS[request.POST['operation']] | |
888 | else: |
|
888 | else: | |
889 | operation = ' ' |
|
889 | operation = ' ' | |
890 |
|
890 | |||
891 | mode = MIX_MODES[request.POST['mode']] |
|
891 | mode = MIX_MODES[request.POST['mode']] | |
892 |
|
892 | |||
893 | if result: |
|
893 | if result: | |
894 | result = '{}-{}|{}|{}|{}|{}'.format(mix.parameters, |
|
894 | result = '{}-{}|{}|{}|{}|{}'.format(mix.parameters, | |
895 | request.POST['experiment'], |
|
895 | request.POST['experiment'], | |
896 | mode, |
|
896 | mode, | |
897 | operation, |
|
897 | operation, | |
898 | float( |
|
898 | float( | |
899 | request.POST['delay']), |
|
899 | request.POST['delay']), | |
900 | parse_mask( |
|
900 | parse_mask( | |
901 | request.POST.getlist('mask')) |
|
901 | request.POST.getlist('mask')) | |
902 | ) |
|
902 | ) | |
903 | else: |
|
903 | else: | |
904 | result = '{}|{}|{}|{}|{}'.format(request.POST['experiment'], |
|
904 | result = '{}|{}|{}|{}|{}'.format(request.POST['experiment'], | |
905 | mode, |
|
905 | mode, | |
906 | operation, |
|
906 | operation, | |
907 | float(request.POST['delay']), |
|
907 | float(request.POST['delay']), | |
908 | parse_mask( |
|
908 | parse_mask( | |
909 | request.POST.getlist('mask')) |
|
909 | request.POST.getlist('mask')) | |
910 | ) |
|
910 | ) | |
911 |
|
911 | |||
912 | mix.parameters = result |
|
912 | mix.parameters = result | |
913 | mix.save() |
|
913 | mix.save() | |
914 | mix.update_pulses() |
|
914 | mix.update_pulses() | |
915 |
|
915 | |||
916 | initial['result'] = parse_mix_result(result) |
|
916 | initial['result'] = parse_mix_result(result) | |
917 | initial['name'] = mix.name |
|
917 | initial['name'] = mix.name | |
918 |
|
918 | |||
919 | form = RCMixConfigurationForm(initial=initial, confs=rc_confs) |
|
919 | form = RCMixConfigurationForm(initial=initial, confs=rc_confs) | |
920 |
|
920 | |||
921 | kwargs = { |
|
921 | kwargs = { | |
922 | 'title': 'Experiment', |
|
922 | 'title': 'Experiment', | |
923 | 'suptitle': 'Mix Configurations', |
|
923 | 'suptitle': 'Mix Configurations', | |
924 | 'form': form, |
|
924 | 'form': form, | |
925 | 'extra_button': 'Delete', |
|
925 | 'extra_button': 'Delete', | |
926 | 'button': 'Add', |
|
926 | 'button': 'Add', | |
927 | 'cancel': 'Back', |
|
927 | 'cancel': 'Back', | |
928 | 'previous': experiment.get_absolute_url(), |
|
928 | 'previous': experiment.get_absolute_url(), | |
929 | 'id_exp': id_exp, |
|
929 | 'id_exp': id_exp, | |
930 |
|
930 | |||
931 | } |
|
931 | } | |
932 | kwargs['menu_experiments'] = 'active' |
|
932 | kwargs['menu_experiments'] = 'active' | |
933 |
|
933 | |||
934 | return render(request, 'experiment_mix.html', kwargs) |
|
934 | return render(request, 'experiment_mix.html', kwargs) | |
935 |
|
935 | |||
936 |
|
936 | |||
937 | @login_required |
|
937 | @login_required | |
938 | def experiment_mix_delete(request, id_exp): |
|
938 | def experiment_mix_delete(request, id_exp): | |
939 |
|
939 | |||
940 | conf = RCConfiguration.objects.get(experiment=id_exp, mix=True, type=0) |
|
940 | conf = RCConfiguration.objects.get(experiment=id_exp, mix=True, type=0) | |
941 | values = conf.parameters.split('-') |
|
941 | values = conf.parameters.split('-') | |
942 | conf.parameters = '-'.join(values[:-1]) |
|
942 | conf.parameters = '-'.join(values[:-1]) | |
943 | conf.save() |
|
943 | conf.save() | |
944 |
|
944 | |||
945 | return redirect('url_mix_experiment', id_exp=id_exp) |
|
945 | return redirect('url_mix_experiment', id_exp=id_exp) | |
946 |
|
946 | |||
947 |
|
947 | |||
948 | def experiment_summary(request, id_exp): |
|
948 | def experiment_summary(request, id_exp): | |
949 |
|
949 | |||
950 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
950 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
951 | configurations = Configuration.objects.filter( |
|
951 | configurations = Configuration.objects.filter( | |
952 | experiment=experiment, type=0) |
|
952 | experiment=experiment, type=0) | |
953 |
|
953 | |||
954 | kwargs = {} |
|
954 | kwargs = {} | |
955 | kwargs['experiment_keys'] = ['radar_system', |
|
955 | kwargs['experiment_keys'] = ['radar_system', | |
956 | 'name', 'freq', 'start_time', 'end_time'] |
|
956 | 'name', 'freq', 'start_time', 'end_time'] | |
957 | kwargs['experiment'] = experiment |
|
957 | kwargs['experiment'] = experiment | |
958 | kwargs['configurations'] = [] |
|
958 | kwargs['configurations'] = [] | |
959 | kwargs['title'] = 'Experiment Summary' |
|
959 | kwargs['title'] = 'Experiment Summary' | |
960 | kwargs['suptitle'] = 'Details' |
|
960 | kwargs['suptitle'] = 'Details' | |
961 | kwargs['button'] = 'Verify Parameters' |
|
961 | kwargs['button'] = 'Verify Parameters' | |
962 |
|
962 | |||
963 | c_vel = 3.0*(10**8) # m/s |
|
963 | c_vel = 3.0*(10**8) # m/s | |
964 | ope_freq = experiment.freq*(10**6) # 1/s |
|
964 | ope_freq = experiment.freq*(10**6) # 1/s | |
965 | radar_lambda = c_vel/ope_freq # m |
|
965 | radar_lambda = c_vel/ope_freq # m | |
966 | kwargs['radar_lambda'] = radar_lambda |
|
966 | kwargs['radar_lambda'] = radar_lambda | |
967 |
|
967 | |||
968 | ipp = None |
|
968 | ipp = None | |
969 | nsa = 1 |
|
969 | nsa = 1 | |
970 | code_id = 0 |
|
970 | code_id = 0 | |
971 | tx_line = {} |
|
971 | tx_line = {} | |
972 |
|
972 | |||
973 | for configuration in configurations.filter(device__device_type__name = 'rc'): |
|
973 | for configuration in configurations.filter(device__device_type__name = 'rc'): | |
974 |
|
974 | |||
975 | if configuration.mix: |
|
975 | if configuration.mix: | |
976 | continue |
|
976 | continue | |
977 | conf = {'conf': configuration} |
|
977 | conf = {'conf': configuration} | |
978 | conf['keys'] = [] |
|
978 | conf['keys'] = [] | |
979 | conf['NTxs'] = configuration.ntx |
|
979 | conf['NTxs'] = configuration.ntx | |
980 | conf['keys'].append('NTxs') |
|
980 | conf['keys'].append('NTxs') | |
981 | ipp = configuration.ipp |
|
981 | ipp = configuration.ipp | |
982 | conf['IPP'] = ipp |
|
982 | conf['IPP'] = ipp | |
983 | conf['keys'].append('IPP') |
|
983 | conf['keys'].append('IPP') | |
984 | lines = configuration.get_lines(line_type__name='tx') |
|
984 | lines = configuration.get_lines(line_type__name='tx') | |
985 |
|
985 | |||
986 | for tx_line in lines: |
|
986 | for tx_line in lines: | |
987 | tx_params = json.loads(tx_line.params) |
|
987 | tx_params = json.loads(tx_line.params) | |
988 | conf[tx_line.get_name()] = '{} Km'.format(tx_params['pulse_width']) |
|
988 | conf[tx_line.get_name()] = '{} Km'.format(tx_params['pulse_width']) | |
989 | conf['keys'].append(tx_line.get_name()) |
|
989 | conf['keys'].append(tx_line.get_name()) | |
990 | delays = tx_params['delays'] |
|
990 | delays = tx_params['delays'] | |
991 | if delays not in ('', '0'): |
|
991 | if delays not in ('', '0'): | |
992 | n = len(delays.split(',')) |
|
992 | n = len(delays.split(',')) | |
993 | taus = '{} Taus: {}'.format(n, delays) |
|
993 | taus = '{} Taus: {}'.format(n, delays) | |
994 | else: |
|
994 | else: | |
995 | taus = '-' |
|
995 | taus = '-' | |
996 | conf['Taus ({})'.format(tx_line.get_name())] = taus |
|
996 | conf['Taus ({})'.format(tx_line.get_name())] = taus | |
997 | conf['keys'].append('Taus ({})'.format(tx_line.get_name())) |
|
997 | conf['keys'].append('Taus ({})'.format(tx_line.get_name())) | |
998 | for code_line in configuration.get_lines(line_type__name='codes'): |
|
998 | for code_line in configuration.get_lines(line_type__name='codes'): | |
999 | code_params = json.loads(code_line.params) |
|
999 | code_params = json.loads(code_line.params) | |
1000 | code_id = code_params['code'] |
|
1000 | code_id = code_params['code'] | |
1001 | if tx_line.pk == int(code_params['TX_ref']): |
|
1001 | if tx_line.pk == int(code_params['TX_ref']): | |
1002 | conf['Code ({})'.format(tx_line.get_name())] = '{}:{}'.format(RCLineCode.objects.get(pk=code_params['code']), |
|
1002 | conf['Code ({})'.format(tx_line.get_name())] = '{}:{}'.format(RCLineCode.objects.get(pk=code_params['code']), | |
1003 | '-'.join(code_params['codes'])) |
|
1003 | '-'.join(code_params['codes'])) | |
1004 | conf['keys'].append('Code ({})'.format(tx_line.get_name())) |
|
1004 | conf['keys'].append('Code ({})'.format(tx_line.get_name())) | |
1005 |
|
1005 | |||
1006 | for windows_line in configuration.get_lines(line_type__name='windows'): |
|
1006 | for windows_line in configuration.get_lines(line_type__name='windows'): | |
1007 | win_params = json.loads(windows_line.params) |
|
1007 | win_params = json.loads(windows_line.params) | |
1008 | if tx_line.pk == int(win_params['TX_ref']): |
|
1008 | if tx_line.pk == int(win_params['TX_ref']): | |
1009 | windows = '' |
|
1009 | windows = '' | |
1010 | nsa = win_params['params'][0]['number_of_samples'] |
|
1010 | nsa = win_params['params'][0]['number_of_samples'] | |
1011 | for i, params in enumerate(win_params['params']): |
|
1011 | for i, params in enumerate(win_params['params']): | |
1012 | windows += 'W{}: Ho={first_height} km DH={resolution} km NSA={number_of_samples}<br>'.format( |
|
1012 | windows += 'W{}: Ho={first_height} km DH={resolution} km NSA={number_of_samples}<br>'.format( | |
1013 | i, **params) |
|
1013 | i, **params) | |
1014 | conf['Window'] = mark_safe(windows) |
|
1014 | conf['Window'] = mark_safe(windows) | |
1015 | conf['keys'].append('Window') |
|
1015 | conf['keys'].append('Window') | |
1016 |
|
1016 | |||
1017 | kwargs['configurations'].append(conf) |
|
1017 | kwargs['configurations'].append(conf) | |
1018 |
|
1018 | |||
1019 | for configuration in configurations.filter(device__device_type__name = 'jars'): |
|
1019 | for configuration in configurations.filter(device__device_type__name = 'jars'): | |
1020 |
|
1020 | |||
1021 | conf = {'conf': configuration} |
|
1021 | conf = {'conf': configuration} | |
1022 | conf['keys'] = [] |
|
1022 | conf['keys'] = [] | |
1023 | conf['Type of Data'] = EXPERIMENT_TYPE[configuration.exp_type][1] |
|
1023 | conf['Type of Data'] = EXPERIMENT_TYPE[configuration.exp_type][1] | |
1024 | conf['keys'].append('Type of Data') |
|
1024 | conf['keys'].append('Type of Data') | |
1025 | channels_number = configuration.channels_number |
|
1025 | channels_number = configuration.channels_number | |
1026 | exp_type = configuration.exp_type |
|
1026 | exp_type = configuration.exp_type | |
1027 | fftpoints = configuration.fftpoints |
|
1027 | fftpoints = configuration.fftpoints | |
1028 | filter_parms = json.loads(configuration.filter_parms) |
|
1028 | filter_parms = json.loads(configuration.filter_parms) | |
1029 | spectral_number = configuration.spectral_number |
|
1029 | spectral_number = configuration.spectral_number | |
1030 | acq_profiles = configuration.acq_profiles |
|
1030 | acq_profiles = configuration.acq_profiles | |
1031 | cohe_integr = configuration.cohe_integr |
|
1031 | cohe_integr = configuration.cohe_integr | |
1032 | profiles_block = configuration.profiles_block |
|
1032 | profiles_block = configuration.profiles_block | |
1033 |
|
1033 | |||
1034 | conf['Num of Profiles'] = acq_profiles |
|
1034 | conf['Num of Profiles'] = acq_profiles | |
1035 | conf['keys'].append('Num of Profiles') |
|
1035 | conf['keys'].append('Num of Profiles') | |
1036 |
|
1036 | |||
1037 | conf['Prof per Block'] = profiles_block |
|
1037 | conf['Prof per Block'] = profiles_block | |
1038 | conf['keys'].append('Prof per Block') |
|
1038 | conf['keys'].append('Prof per Block') | |
1039 |
|
1039 | |||
1040 | conf['Blocks per File'] = configuration.raw_data_blocks |
|
1040 | conf['Blocks per File'] = configuration.raw_data_blocks | |
1041 | conf['keys'].append('Blocks per File') |
|
1041 | conf['keys'].append('Blocks per File') | |
1042 |
|
1042 | |||
1043 | if exp_type == 0: # Short |
|
1043 | if exp_type == 0: # Short | |
1044 | bytes_ = 2 |
|
1044 | bytes_ = 2 | |
1045 | b = nsa*2*bytes_*channels_number |
|
1045 | b = nsa*2*bytes_*channels_number | |
1046 | else: # Float |
|
1046 | else: # Float | |
1047 | bytes_ = 4 |
|
1047 | bytes_ = 4 | |
1048 | channels = channels_number + spectral_number |
|
1048 | channels = channels_number + spectral_number | |
1049 | b = nsa*2*bytes_*fftpoints*channels |
|
1049 | b = nsa*2*bytes_*fftpoints*channels | |
1050 |
|
1050 | |||
1051 | codes_num = 7 |
|
1051 | codes_num = 7 | |
1052 | if code_id == 2: |
|
1052 | if code_id == 2: | |
1053 | codes_num = 7 |
|
1053 | codes_num = 7 | |
1054 | elif code_id == 12: |
|
1054 | elif code_id == 12: | |
1055 | codes_num = 15 |
|
1055 | codes_num = 15 | |
1056 |
|
1056 | |||
1057 | #Jars filter values: |
|
1057 | #Jars filter values: | |
1058 |
|
1058 | |||
1059 | clock = float(filter_parms['clock']) |
|
1059 | clock = float(filter_parms['clock']) | |
1060 | filter_2 = int(filter_parms['cic_2']) |
|
1060 | filter_2 = int(filter_parms['cic_2']) | |
1061 | filter_5 = int(filter_parms['cic_5']) |
|
1061 | filter_5 = int(filter_parms['cic_5']) | |
1062 | filter_fir = int(filter_parms['fir']) |
|
1062 | filter_fir = int(filter_parms['fir']) | |
1063 | Fs_MHz = clock/(filter_2*filter_5*filter_fir) |
|
1063 | Fs_MHz = clock/(filter_2*filter_5*filter_fir) | |
1064 |
|
1064 | |||
1065 | #Jars values: |
|
1065 | #Jars values: | |
1066 | if ipp is not None: |
|
1066 | if ipp is not None: | |
1067 | IPP_units = ipp/0.15*Fs_MHz |
|
1067 | IPP_units = ipp/0.15*Fs_MHz | |
1068 | IPP_us = IPP_units / Fs_MHz |
|
1068 | IPP_us = IPP_units / Fs_MHz | |
1069 | IPP_s = IPP_units / (Fs_MHz * (10**6)) |
|
1069 | IPP_s = IPP_units / (Fs_MHz * (10**6)) | |
1070 | Ts = 1/(Fs_MHz*(10**6)) |
|
1070 | Ts = 1/(Fs_MHz*(10**6)) | |
1071 |
|
1071 | |||
1072 | Va = radar_lambda/(4*Ts*cohe_integr) |
|
1072 | Va = radar_lambda/(4*Ts*cohe_integr) | |
1073 | rate_bh = ((nsa-codes_num)*channels_number*2 * |
|
1073 | rate_bh = ((nsa-codes_num)*channels_number*2 * | |
1074 | bytes_/IPP_us)*(36*(10**8)/cohe_integr) |
|
1074 | bytes_/IPP_us)*(36*(10**8)/cohe_integr) | |
1075 | rate_gh = rate_bh/(1024*1024*1024) |
|
1075 | rate_gh = rate_bh/(1024*1024*1024) | |
1076 |
|
1076 | |||
1077 | conf['Time per Block'] = IPP_s * profiles_block * cohe_integr |
|
1077 | conf['Time per Block'] = IPP_s * profiles_block * cohe_integr | |
1078 | conf['keys'].append('Time per Block') |
|
1078 | conf['keys'].append('Time per Block') | |
1079 | conf['Acq time'] = IPP_s * acq_profiles |
|
1079 | conf['Acq time'] = IPP_s * acq_profiles | |
1080 | conf['keys'].append('Acq time') |
|
1080 | conf['keys'].append('Acq time') | |
1081 | conf['Data rate'] = str(rate_gh)+" (GB/h)" |
|
1081 | conf['Data rate'] = str(rate_gh)+" (GB/h)" | |
1082 | conf['keys'].append('Data rate') |
|
1082 | conf['keys'].append('Data rate') | |
1083 | conf['Va (m/s)'] = Va |
|
1083 | conf['Va (m/s)'] = Va | |
1084 | conf['keys'].append('Va (m/s)') |
|
1084 | conf['keys'].append('Va (m/s)') | |
1085 | conf['Vrange (m/s)'] = 3/(2*IPP_s*cohe_integr) |
|
1085 | conf['Vrange (m/s)'] = 3/(2*IPP_s*cohe_integr) | |
1086 | conf['keys'].append('Vrange (m/s)') |
|
1086 | conf['keys'].append('Vrange (m/s)') | |
1087 |
|
1087 | |||
1088 | kwargs['configurations'].append(conf) |
|
1088 | kwargs['configurations'].append(conf) | |
1089 | kwargs['menu_experiments'] = 'active' |
|
1089 | kwargs['menu_experiments'] = 'active' | |
1090 |
|
1090 | |||
1091 | ###### SIDEBAR ###### |
|
1091 | ###### SIDEBAR ###### | |
1092 | kwargs.update(sidebar(experiment=experiment)) |
|
1092 | kwargs.update(sidebar(experiment=experiment)) | |
1093 |
|
1093 | |||
1094 | return render(request, 'experiment_summary.html', kwargs) |
|
1094 | return render(request, 'experiment_summary.html', kwargs) | |
1095 |
|
1095 | |||
1096 |
|
1096 | |||
1097 | @login_required |
|
1097 | @login_required | |
1098 | def experiment_verify(request, id_exp): |
|
1098 | def experiment_verify(request, id_exp): | |
1099 |
|
1099 | |||
1100 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
1100 | experiment = get_object_or_404(Experiment, pk=id_exp) | |
1101 | experiment_data = experiment.parms_to_dict() |
|
1101 | experiment_data = experiment.parms_to_dict() | |
1102 | configurations = Configuration.objects.filter( |
|
1102 | configurations = Configuration.objects.filter( | |
1103 | experiment=experiment, type=0) |
|
1103 | experiment=experiment, type=0) | |
1104 |
|
1104 | |||
1105 | kwargs = {} |
|
1105 | kwargs = {} | |
1106 |
|
1106 | |||
1107 | kwargs['experiment_keys'] = ['template', |
|
1107 | kwargs['experiment_keys'] = ['template', | |
1108 | 'radar_system', 'name', 'start_time', 'end_time'] |
|
1108 | 'radar_system', 'name', 'start_time', 'end_time'] | |
1109 | kwargs['experiment'] = experiment |
|
1109 | kwargs['experiment'] = experiment | |
1110 |
|
1110 | |||
1111 | kwargs['configuration_keys'] = ['name', 'device__ip_address', |
|
1111 | kwargs['configuration_keys'] = ['name', 'device__ip_address', | |
1112 | 'device__port_address', 'device__status'] |
|
1112 | 'device__port_address', 'device__status'] | |
1113 | kwargs['configurations'] = configurations |
|
1113 | kwargs['configurations'] = configurations | |
1114 | kwargs['experiment_data'] = experiment_data |
|
1114 | kwargs['experiment_data'] = experiment_data | |
1115 |
|
1115 | |||
1116 | kwargs['title'] = 'Verify Experiment' |
|
1116 | kwargs['title'] = 'Verify Experiment' | |
1117 | kwargs['suptitle'] = 'Parameters' |
|
1117 | kwargs['suptitle'] = 'Parameters' | |
1118 |
|
1118 | |||
1119 | kwargs['button'] = 'Update' |
|
1119 | kwargs['button'] = 'Update' | |
1120 |
|
1120 | |||
1121 | jars_conf = False |
|
1121 | jars_conf = False | |
1122 | rc_conf = False |
|
1122 | rc_conf = False | |
1123 | dds_conf = False |
|
1123 | dds_conf = False | |
1124 |
|
1124 | |||
1125 | for configuration in configurations: |
|
1125 | for configuration in configurations: | |
1126 | #-------------------- JARS -----------------------: |
|
1126 | #-------------------- JARS -----------------------: | |
1127 | if configuration.device.device_type.name == 'jars': |
|
1127 | if configuration.device.device_type.name == 'jars': | |
1128 | jars_conf = True |
|
1128 | jars_conf = True | |
1129 | jars = configuration |
|
1129 | jars = configuration | |
1130 | kwargs['jars_conf'] = jars_conf |
|
1130 | kwargs['jars_conf'] = jars_conf | |
1131 | filter_parms = json.loads(jars.filter_parms) |
|
1131 | filter_parms = json.loads(jars.filter_parms) | |
1132 | kwargs['filter_parms'] = filter_parms |
|
1132 | kwargs['filter_parms'] = filter_parms | |
1133 | #--Sampling Frequency |
|
1133 | #--Sampling Frequency | |
1134 | clock = filter_parms['clock'] |
|
1134 | clock = filter_parms['clock'] | |
1135 | filter_2 = filter_parms['cic_2'] |
|
1135 | filter_2 = filter_parms['cic_2'] | |
1136 | filter_5 = filter_parms['cic_5'] |
|
1136 | filter_5 = filter_parms['cic_5'] | |
1137 | filter_fir = filter_parms['fir'] |
|
1137 | filter_fir = filter_parms['fir'] | |
1138 | samp_freq_jars = clock/filter_2/filter_5/filter_fir |
|
1138 | samp_freq_jars = clock/filter_2/filter_5/filter_fir | |
1139 |
|
1139 | |||
1140 | kwargs['samp_freq_jars'] = samp_freq_jars |
|
1140 | kwargs['samp_freq_jars'] = samp_freq_jars | |
1141 | kwargs['jars'] = configuration |
|
1141 | kwargs['jars'] = configuration | |
1142 |
|
1142 | |||
1143 | #--------------------- RC ----------------------: |
|
1143 | #--------------------- RC ----------------------: | |
1144 | if configuration.device.device_type.name == 'rc' and not configuration.mix: |
|
1144 | if configuration.device.device_type.name == 'rc' and not configuration.mix: | |
1145 | rc_conf = True |
|
1145 | rc_conf = True | |
1146 | rc = configuration |
|
1146 | rc = configuration | |
1147 |
|
1147 | |||
1148 | rc_parms = configuration.parms_to_dict() |
|
1148 | rc_parms = configuration.parms_to_dict() | |
1149 |
|
1149 | |||
1150 | win_lines = rc.get_lines(line_type__name='windows') |
|
1150 | win_lines = rc.get_lines(line_type__name='windows') | |
1151 | if win_lines: |
|
1151 | if win_lines: | |
1152 | dh = json.loads(win_lines[0].params)['params'][0]['resolution'] |
|
1152 | dh = json.loads(win_lines[0].params)['params'][0]['resolution'] | |
1153 | #--Sampling Frequency |
|
1153 | #--Sampling Frequency | |
1154 | samp_freq_rc = 0.15/dh |
|
1154 | samp_freq_rc = 0.15/dh | |
1155 | kwargs['samp_freq_rc'] = samp_freq_rc |
|
1155 | kwargs['samp_freq_rc'] = samp_freq_rc | |
1156 |
|
1156 | |||
1157 | kwargs['rc_conf'] = rc_conf |
|
1157 | kwargs['rc_conf'] = rc_conf | |
1158 | kwargs['rc'] = configuration |
|
1158 | kwargs['rc'] = configuration | |
1159 |
|
1159 | |||
1160 | #-------------------- DDS ----------------------: |
|
1160 | #-------------------- DDS ----------------------: | |
1161 | if configuration.device.device_type.name == 'dds': |
|
1161 | if configuration.device.device_type.name == 'dds': | |
1162 | dds_conf = True |
|
1162 | dds_conf = True | |
1163 | dds = configuration |
|
1163 | dds = configuration | |
1164 | dds_parms = configuration.parms_to_dict() |
|
1164 | dds_parms = configuration.parms_to_dict() | |
1165 |
|
1165 | |||
1166 | kwargs['dds_conf'] = dds_conf |
|
1166 | kwargs['dds_conf'] = dds_conf | |
1167 | kwargs['dds'] = configuration |
|
1167 | kwargs['dds'] = configuration | |
1168 |
|
1168 | |||
1169 | #------------Validation------------: |
|
1169 | #------------Validation------------: | |
1170 | #Clock |
|
1170 | #Clock | |
1171 | if dds_conf and rc_conf and jars_conf: |
|
1171 | if dds_conf and rc_conf and jars_conf: | |
1172 | if float(filter_parms['clock']) != float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']) and float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']) != float(dds_parms['configurations']['byId'][str(dds.pk)]['clock']): |
|
1172 | if float(filter_parms['clock']) != float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']) and float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']) != float(dds_parms['configurations']['byId'][str(dds.pk)]['clock']): | |
1173 | messages.warning(request, "Devices don't have the same clock.") |
|
1173 | messages.warning(request, "Devices don't have the same clock.") | |
1174 | elif rc_conf and jars_conf: |
|
1174 | elif rc_conf and jars_conf: | |
1175 | if float(filter_parms['clock']) != float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']): |
|
1175 | if float(filter_parms['clock']) != float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']): | |
1176 | messages.warning(request, "Devices don't have the same clock.") |
|
1176 | messages.warning(request, "Devices don't have the same clock.") | |
1177 | elif rc_conf and dds_conf: |
|
1177 | elif rc_conf and dds_conf: | |
1178 | if float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']) != float(dds_parms['configurations']['byId'][str(dds.pk)]['clock']): |
|
1178 | if float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']) != float(dds_parms['configurations']['byId'][str(dds.pk)]['clock']): | |
1179 | messages.warning(request, "Devices don't have the same clock.") |
|
1179 | messages.warning(request, "Devices don't have the same clock.") | |
1180 | if float(samp_freq_rc) != float(dds_parms['configurations']['byId'][str(dds.pk)]['frequencyA']): |
|
1180 | if float(samp_freq_rc) != float(dds_parms['configurations']['byId'][str(dds.pk)]['frequencyA']): | |
1181 | messages.warning( |
|
1181 | messages.warning( | |
1182 | request, "Devices don't have the same Frequency A.") |
|
1182 | request, "Devices don't have the same Frequency A.") | |
1183 |
|
1183 | |||
1184 | #------------POST METHOD------------: |
|
1184 | #------------POST METHOD------------: | |
1185 | if request.method == 'POST': |
|
1185 | if request.method == 'POST': | |
1186 | if request.POST['suggest_clock']: |
|
1186 | if request.POST['suggest_clock']: | |
1187 | try: |
|
1187 | try: | |
1188 | suggest_clock = float(request.POST['suggest_clock']) |
|
1188 | suggest_clock = float(request.POST['suggest_clock']) | |
1189 | except: |
|
1189 | except: | |
1190 | messages.warning(request, "Invalid value in CLOCK IN.") |
|
1190 | messages.warning(request, "Invalid value in CLOCK IN.") | |
1191 | return redirect('url_verify_experiment', id_exp=experiment.id) |
|
1191 | return redirect('url_verify_experiment', id_exp=experiment.id) | |
1192 | else: |
|
1192 | else: | |
1193 | suggest_clock = "" |
|
1193 | suggest_clock = "" | |
1194 | if suggest_clock: |
|
1194 | if suggest_clock: | |
1195 | if rc_conf: |
|
1195 | if rc_conf: | |
1196 | rc.clock_in = suggest_clock |
|
1196 | rc.clock_in = suggest_clock | |
1197 | rc.save() |
|
1197 | rc.save() | |
1198 | if jars_conf: |
|
1198 | if jars_conf: | |
1199 | filter_parms = jars.filter_parms |
|
1199 | filter_parms = jars.filter_parms | |
1200 | filter_parms = ast.literal_eval(filter_parms) |
|
1200 | filter_parms = ast.literal_eval(filter_parms) | |
1201 | filter_parms['clock'] = suggest_clock |
|
1201 | filter_parms['clock'] = suggest_clock | |
1202 | jars.filter_parms = json.dumps(filter_parms) |
|
1202 | jars.filter_parms = json.dumps(filter_parms) | |
1203 | jars.save() |
|
1203 | jars.save() | |
1204 | kwargs['filter_parms'] = filter_parms |
|
1204 | kwargs['filter_parms'] = filter_parms | |
1205 | if dds_conf: |
|
1205 | if dds_conf: | |
1206 | dds.clock = suggest_clock |
|
1206 | dds.clock = suggest_clock | |
1207 | dds.save() |
|
1207 | dds.save() | |
1208 |
|
1208 | |||
1209 | if request.POST['suggest_frequencyA']: |
|
1209 | if request.POST['suggest_frequencyA']: | |
1210 | try: |
|
1210 | try: | |
1211 | suggest_frequencyA = float(request.POST['suggest_frequencyA']) |
|
1211 | suggest_frequencyA = float(request.POST['suggest_frequencyA']) | |
1212 | except: |
|
1212 | except: | |
1213 | messages.warning(request, "Invalid value in FREQUENCY A.") |
|
1213 | messages.warning(request, "Invalid value in FREQUENCY A.") | |
1214 | return redirect('url_verify_experiment', id_exp=experiment.id) |
|
1214 | return redirect('url_verify_experiment', id_exp=experiment.id) | |
1215 | else: |
|
1215 | else: | |
1216 | suggest_frequencyA = "" |
|
1216 | suggest_frequencyA = "" | |
1217 | if suggest_frequencyA: |
|
1217 | if suggest_frequencyA: | |
1218 | if jars_conf: |
|
1218 | if jars_conf: | |
1219 | filter_parms = jars.filter_parms |
|
1219 | filter_parms = jars.filter_parms | |
1220 | filter_parms = ast.literal_eval(filter_parms) |
|
1220 | filter_parms = ast.literal_eval(filter_parms) | |
1221 | filter_parms['fch'] = suggest_frequencyA |
|
1221 | filter_parms['fch'] = suggest_frequencyA | |
1222 | jars.filter_parms = json.dumps(filter_parms) |
|
1222 | jars.filter_parms = json.dumps(filter_parms) | |
1223 | jars.save() |
|
1223 | jars.save() | |
1224 | kwargs['filter_parms'] = filter_parms |
|
1224 | kwargs['filter_parms'] = filter_parms | |
1225 | if dds_conf: |
|
1225 | if dds_conf: | |
1226 | dds.frequencyA_Mhz = request.POST['suggest_frequencyA'] |
|
1226 | dds.frequencyA_Mhz = request.POST['suggest_frequencyA'] | |
1227 | dds.save() |
|
1227 | dds.save() | |
1228 |
|
1228 | |||
1229 | kwargs['menu_experiments'] = 'active' |
|
1229 | kwargs['menu_experiments'] = 'active' | |
1230 | kwargs.update(sidebar(experiment=experiment)) |
|
1230 | kwargs.update(sidebar(experiment=experiment)) | |
1231 | return render(request, 'experiment_verify.html', kwargs) |
|
1231 | return render(request, 'experiment_verify.html', kwargs) | |
1232 |
|
1232 | |||
1233 |
|
1233 | |||
1234 | def parse_mix_result(s): |
|
1234 | def parse_mix_result(s): | |
1235 |
|
1235 | |||
1236 | values = s.split('-') |
|
1236 | values = s.split('-') | |
1237 | html = 'EXP MOD OPE DELAY MASK\r\n' |
|
1237 | html = 'EXP MOD OPE DELAY MASK\r\n' | |
1238 |
|
1238 | |||
1239 | if not values or values[0] in ('', ' '): |
|
1239 | if not values or values[0] in ('', ' '): | |
1240 | return mark_safe(html) |
|
1240 | return mark_safe(html) | |
1241 |
|
1241 | |||
1242 | for i, value in enumerate(values): |
|
1242 | for i, value in enumerate(values): | |
1243 | if not value: |
|
1243 | if not value: | |
1244 | continue |
|
1244 | continue | |
1245 | pk, mode, operation, delay, mask = value.split('|') |
|
1245 | pk, mode, operation, delay, mask = value.split('|') | |
1246 | conf = RCConfiguration.objects.get(pk=pk) |
|
1246 | conf = RCConfiguration.objects.get(pk=pk) | |
1247 | if i == 0: |
|
1247 | if i == 0: | |
1248 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( |
|
1248 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( | |
1249 | conf.name, |
|
1249 | conf.name, | |
1250 | mode, |
|
1250 | mode, | |
1251 | ' ', |
|
1251 | ' ', | |
1252 | delay, |
|
1252 | delay, | |
1253 | mask) |
|
1253 | mask) | |
1254 | else: |
|
1254 | else: | |
1255 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( |
|
1255 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( | |
1256 | conf.name, |
|
1256 | conf.name, | |
1257 | mode, |
|
1257 | mode, | |
1258 | operation, |
|
1258 | operation, | |
1259 | delay, |
|
1259 | delay, | |
1260 | mask) |
|
1260 | mask) | |
1261 |
|
1261 | |||
1262 | return mark_safe(html) |
|
1262 | return mark_safe(html) | |
1263 |
|
1263 | |||
1264 |
|
1264 | |||
1265 | def parse_mask(l): |
|
1265 | def parse_mask(l): | |
1266 |
|
1266 | |||
1267 | values = [] |
|
1267 | values = [] | |
1268 |
|
1268 | |||
1269 | for x in range(16): |
|
1269 | for x in range(16): | |
1270 | if '{}'.format(x) in l: |
|
1270 | if '{}'.format(x) in l: | |
1271 | values.append(1) |
|
1271 | values.append(1) | |
1272 | else: |
|
1272 | else: | |
1273 | values.append(0) |
|
1273 | values.append(0) | |
1274 |
|
1274 | |||
1275 | values.reverse() |
|
1275 | values.reverse() | |
1276 |
|
1276 | |||
1277 | return int(''.join([str(x) for x in values]), 2) |
|
1277 | return int(''.join([str(x) for x in values]), 2) | |
1278 |
|
1278 | |||
1279 |
|
1279 | |||
1280 | def dev_confs(request): |
|
1280 | def dev_confs(request): | |
1281 |
|
1281 | |||
1282 | page = request.GET.get('page') |
|
1282 | page = request.GET.get('page') | |
1283 | order = ('-programmed_date', ) |
|
1283 | order = ('-programmed_date', ) | |
1284 | filters = request.GET.copy() |
|
1284 | filters = request.GET.copy() | |
1285 | if 'my configurations' in filters: |
|
1285 | if 'my configurations' in filters: | |
1286 | filters.pop('my configurations', None) |
|
1286 | filters.pop('my configurations', None) | |
1287 | filters['mine'] = request.user.id |
|
1287 | filters['mine'] = request.user.id | |
1288 | kwargs = get_paginator(Configuration, page, order, filters) |
|
1288 | kwargs = get_paginator(Configuration, page, order, filters) | |
1289 | fields = ['tags', 'template', 'historical'] |
|
1289 | fields = ['tags', 'template', 'historical'] | |
1290 | if request.user.is_authenticated: |
|
1290 | if request.user.is_authenticated: | |
1291 | fields.append('my configurations') |
|
1291 | fields.append('my configurations') | |
1292 | form = FilterForm(initial=request.GET, extra_fields=fields) |
|
1292 | form = FilterForm(initial=request.GET, extra_fields=fields) | |
1293 | kwargs['keys'] = ['name', 'experiment', |
|
1293 | kwargs['keys'] = ['name', 'experiment', | |
1294 | 'type', 'programmed_date', 'actions'] |
|
1294 | 'type', 'programmed_date', 'actions'] | |
1295 | kwargs['title'] = 'Configuration' |
|
1295 | kwargs['title'] = 'Configuration' | |
1296 | kwargs['suptitle'] = 'List' |
|
1296 | kwargs['suptitle'] = 'List' | |
1297 | kwargs['no_sidebar'] = True |
|
1297 | kwargs['no_sidebar'] = True | |
1298 | kwargs['form'] = form |
|
1298 | kwargs['form'] = form | |
1299 | kwargs['add_url'] = reverse('url_add_dev_conf', args=[0]) |
|
1299 | kwargs['add_url'] = reverse('url_add_dev_conf', args=[0]) | |
1300 | filters = request.GET.copy() |
|
1300 | filters = request.GET.copy() | |
1301 | filters.pop('page', None) |
|
1301 | filters.pop('page', None) | |
1302 | kwargs['q'] = urlencode(filters) |
|
1302 | kwargs['q'] = urlencode(filters) | |
1303 | kwargs['menu_configurations'] = 'active' |
|
1303 | kwargs['menu_configurations'] = 'active' | |
1304 |
|
1304 | |||
1305 | return render(request, 'base_list.html', kwargs) |
|
1305 | return render(request, 'base_list.html', kwargs) | |
1306 |
|
1306 | |||
1307 |
|
1307 | |||
1308 | def dev_conf(request, id_conf): |
|
1308 | def dev_conf(request, id_conf): | |
1309 |
|
1309 | |||
1310 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1310 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1311 |
|
1311 | |||
1312 | return redirect(conf.get_absolute_url()) |
|
1312 | return redirect(conf.get_absolute_url()) | |
1313 |
|
1313 | |||
1314 |
|
1314 | |||
1315 | @login_required |
|
1315 | @login_required | |
1316 | def dev_conf_new(request, id_exp=0, id_dev=0): |
|
1316 | def dev_conf_new(request, id_exp=0, id_dev=0): | |
1317 |
|
1317 | |||
1318 | if not is_developer(request.user): |
|
1318 | if not is_developer(request.user): | |
1319 | messages.error( |
|
1319 | messages.error( | |
1320 | request, 'Developer required, to create new configurations') |
|
1320 | request, 'Developer required, to create new configurations') | |
1321 | return redirect('index') |
|
1321 | return redirect('index') | |
1322 |
|
1322 | |||
1323 | initial = {} |
|
1323 | initial = {} | |
1324 | kwargs = {} |
|
1324 | kwargs = {} | |
1325 |
|
1325 | |||
1326 | if id_exp != 0: |
|
1326 | if id_exp != 0: | |
1327 | initial['experiment'] = id_exp |
|
1327 | initial['experiment'] = id_exp | |
1328 |
|
1328 | |||
1329 | if id_dev != 0: |
|
1329 | if id_dev != 0: | |
1330 | initial['device'] = id_dev |
|
1330 | initial['device'] = id_dev | |
1331 |
|
1331 | |||
1332 | if request.method == 'GET': |
|
1332 | if request.method == 'GET': | |
1333 |
|
1333 | |||
1334 | if id_dev: |
|
1334 | if id_dev: | |
1335 | kwargs['button'] = 'Create' |
|
1335 | kwargs['button'] = 'Create' | |
1336 | device = Device.objects.get(pk=id_dev) |
|
1336 | device = Device.objects.get(pk=id_dev) | |
1337 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
1337 | DevConfForm = CONF_FORMS[device.device_type.name] | |
1338 | initial['name'] = request.GET['name'] |
|
1338 | initial['name'] = request.GET['name'] | |
1339 | form = DevConfForm(initial=initial) |
|
1339 | form = DevConfForm(initial=initial) | |
1340 | else: |
|
1340 | else: | |
1341 | if 'template' in request.GET: |
|
1341 | if 'template' in request.GET: | |
1342 | if request.GET['template'] == '0': |
|
1342 | if request.GET['template'] == '0': | |
1343 | choices = [(conf.pk, '{}'.format(conf)) |
|
1343 | choices = [(conf.pk, '{}'.format(conf)) | |
1344 | for conf in Configuration.objects.filter(template=True)] |
|
1344 | for conf in Configuration.objects.filter(template=True)] | |
1345 | form = NewForm(initial={'create_from': 2}, |
|
1345 | form = NewForm(initial={'create_from': 2}, | |
1346 | template_choices=choices) |
|
1346 | template_choices=choices) | |
1347 | else: |
|
1347 | else: | |
1348 | kwargs['button'] = 'Create' |
|
1348 | kwargs['button'] = 'Create' | |
1349 | conf = Configuration.objects.get( |
|
1349 | conf = Configuration.objects.get( | |
1350 | pk=request.GET['template']) |
|
1350 | pk=request.GET['template']) | |
1351 | id_dev = conf.device.pk |
|
1351 | id_dev = conf.device.pk | |
1352 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1352 | DevConfForm = CONF_FORMS[conf.device.device_type.name] | |
1353 | form = DevConfForm(instance=conf, |
|
1353 | form = DevConfForm(instance=conf, | |
1354 | initial={'name': '{}_{:%y%m%d}'.format(conf.name, datetime.now()), |
|
1354 | initial={'name': '{}_{:%y%m%d}'.format(conf.name, datetime.now()), | |
1355 | 'template': False, |
|
1355 | 'template': False, | |
1356 | 'experiment': id_exp}) |
|
1356 | 'experiment': id_exp}) | |
1357 | elif 'blank' in request.GET: |
|
1357 | elif 'blank' in request.GET: | |
1358 | kwargs['button'] = 'Create' |
|
1358 | kwargs['button'] = 'Create' | |
1359 | form = ConfigurationForm(initial=initial) |
|
1359 | form = ConfigurationForm(initial=initial) | |
1360 | else: |
|
1360 | else: | |
1361 | form = NewForm() |
|
1361 | form = NewForm() | |
1362 |
|
1362 | |||
1363 | if request.method == 'POST': |
|
1363 | if request.method == 'POST': | |
1364 |
|
1364 | |||
1365 | device = Device.objects.get(pk=request.POST['device']) |
|
1365 | device = Device.objects.get(pk=request.POST['device']) | |
1366 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
1366 | DevConfForm = CONF_FORMS[device.device_type.name] | |
1367 |
|
1367 | |||
1368 | form = DevConfForm(request.POST) |
|
1368 | form = DevConfForm(request.POST) | |
1369 | kwargs['button'] = 'Create' |
|
1369 | kwargs['button'] = 'Create' | |
1370 | if form.is_valid(): |
|
1370 | if form.is_valid(): | |
1371 | conf = form.save(commit=False) |
|
1371 | conf = form.save(commit=False) | |
1372 | conf.author = request.user |
|
1372 | conf.author = request.user | |
1373 | conf.save() |
|
1373 | conf.save() | |
1374 |
|
1374 | |||
1375 | if 'template' in request.GET and conf.device.device_type.name == 'rc': |
|
1375 | if 'template' in request.GET and conf.device.device_type.name == 'rc': | |
1376 | lines = RCLine.objects.filter( |
|
1376 | lines = RCLine.objects.filter( | |
1377 | rc_configuration=request.GET['template']) |
|
1377 | rc_configuration=request.GET['template']) | |
1378 | for line in lines: |
|
1378 | for line in lines: | |
1379 | line.clone(rc_configuration=conf) |
|
1379 | line.clone(rc_configuration=conf) | |
1380 |
|
1380 | |||
1381 | new_lines = conf.get_lines() |
|
1381 | new_lines = conf.get_lines() | |
1382 | for line in new_lines: |
|
1382 | for line in new_lines: | |
1383 | line_params = json.loads(line.params) |
|
1383 | line_params = json.loads(line.params) | |
1384 | if 'TX_ref' in line_params: |
|
1384 | if 'TX_ref' in line_params: | |
1385 | ref_line = RCLine.objects.get(pk=line_params['TX_ref']) |
|
1385 | ref_line = RCLine.objects.get(pk=line_params['TX_ref']) | |
1386 | line_params['TX_ref'] = ['{}'.format( |
|
1386 | line_params['TX_ref'] = ['{}'.format( | |
1387 | l.pk) for l in new_lines if l.get_name() == ref_line.get_name()][0] |
|
1387 | l.pk) for l in new_lines if l.get_name() == ref_line.get_name()][0] | |
1388 | line.params = json.dumps(line_params) |
|
1388 | line.params = json.dumps(line_params) | |
1389 | line.save() |
|
1389 | line.save() | |
1390 | elif conf.device.device_type.name == 'rc': |
|
1390 | elif conf.device.device_type.name == 'rc': | |
1391 | clk = RCClock(rc_configuration=conf) |
|
1391 | clk = RCClock(rc_configuration=conf) | |
1392 | clk.save() |
|
1392 | clk.save() | |
1393 |
|
1393 | |||
1394 | return redirect('url_dev_conf', id_conf=conf.pk) |
|
1394 | return redirect('url_dev_conf', id_conf=conf.pk) | |
1395 |
|
1395 | |||
1396 | kwargs['id_exp'] = id_exp |
|
1396 | kwargs['id_exp'] = id_exp | |
1397 | kwargs['form'] = form |
|
1397 | kwargs['form'] = form | |
1398 | kwargs['title'] = 'Configuration' |
|
1398 | kwargs['title'] = 'Configuration' | |
1399 | kwargs['suptitle'] = 'New' |
|
1399 | kwargs['suptitle'] = 'New' | |
1400 | kwargs['menu_configurations'] = 'active' |
|
1400 | kwargs['menu_configurations'] = 'active' | |
1401 |
|
1401 | |||
1402 | if id_dev != 0: |
|
1402 | if id_dev != 0: | |
1403 | device = Device.objects.get(pk=id_dev) |
|
1403 | device = Device.objects.get(pk=id_dev) | |
1404 | kwargs['device'] = device.device_type.name |
|
1404 | kwargs['device'] = device.device_type.name | |
1405 | print(id_exp) |
|
1405 | print(id_exp) | |
1406 | return render(request, 'dev_conf_edit.html', kwargs) |
|
1406 | return render(request, 'dev_conf_edit.html', kwargs) | |
1407 |
|
1407 | |||
1408 |
|
1408 | |||
1409 | @login_required |
|
1409 | @login_required | |
1410 | def dev_conf_edit(request, id_conf): |
|
1410 | def dev_conf_edit(request, id_conf): | |
1411 |
|
1411 | |||
1412 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1412 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1413 |
|
1413 | |||
1414 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1414 | DevConfForm = CONF_FORMS[conf.device.device_type.name] | |
1415 |
|
1415 | |||
1416 | if request.method == 'GET': |
|
1416 | if request.method == 'GET': | |
1417 | form = DevConfForm(instance=conf) |
|
1417 | form = DevConfForm(instance=conf) | |
1418 |
|
1418 | |||
1419 | if request.method == 'POST': |
|
1419 | if request.method == 'POST': | |
1420 | form = DevConfForm(request.POST, instance=conf) |
|
1420 | form = DevConfForm(request.POST, instance=conf) | |
1421 |
|
1421 | |||
1422 | if form.is_valid(): |
|
1422 | if form.is_valid(): | |
1423 | form.save() |
|
1423 | form.save() | |
1424 | return redirect('url_dev_conf', id_conf=id_conf) |
|
1424 | return redirect('url_dev_conf', id_conf=id_conf) | |
1425 |
|
1425 | |||
1426 | kwargs = {} |
|
1426 | kwargs = {} | |
1427 | kwargs['form'] = form |
|
1427 | kwargs['form'] = form | |
1428 | kwargs['title'] = 'Device Configuration' |
|
1428 | kwargs['title'] = 'Device Configuration' | |
1429 | kwargs['suptitle'] = 'Edit' |
|
1429 | kwargs['suptitle'] = 'Edit' | |
1430 | kwargs['button'] = 'Update' |
|
1430 | kwargs['button'] = 'Update' | |
1431 | kwargs['menu_configurations'] = 'active' |
|
1431 | kwargs['menu_configurations'] = 'active' | |
1432 |
|
1432 | |||
1433 | ###### SIDEBAR ###### |
|
1433 | ###### SIDEBAR ###### | |
1434 | kwargs.update(sidebar(conf=conf)) |
|
1434 | kwargs.update(sidebar(conf=conf)) | |
1435 |
|
1435 | |||
1436 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1436 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) | |
1437 |
|
1437 | |||
1438 |
|
1438 | |||
1439 | @login_required |
|
1439 | @login_required | |
1440 | def dev_conf_start(request, id_conf): |
|
1440 | def dev_conf_start(request, id_conf): | |
1441 |
|
1441 | |||
1442 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1442 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1443 |
|
1443 | |||
1444 | if conf.start_device(): |
|
1444 | if conf.start_device(): | |
1445 | messages.success(request, conf.message) |
|
1445 | messages.success(request, conf.message) | |
1446 | else: |
|
1446 | else: | |
1447 | messages.error(request, conf.message) |
|
1447 | messages.error(request, conf.message) | |
1448 |
|
1448 | |||
1449 | #conf.status_device() |
|
1449 | #conf.status_device() | |
1450 |
|
1450 | |||
1451 | return redirect(conf.get_absolute_url()) |
|
1451 | return redirect(conf.get_absolute_url()) | |
1452 |
|
1452 | |||
1453 |
|
1453 | |||
1454 | @login_required |
|
1454 | @login_required | |
1455 | def dev_conf_stop(request, id_conf): |
|
1455 | def dev_conf_stop(request, id_conf): | |
1456 |
|
1456 | |||
1457 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1457 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1458 |
|
1458 | |||
1459 | if conf.stop_device(): |
|
1459 | if conf.stop_device(): | |
1460 | messages.success(request, conf.message) |
|
1460 | messages.success(request, conf.message) | |
1461 | else: |
|
1461 | else: | |
1462 | messages.error(request, conf.message) |
|
1462 | messages.error(request, conf.message) | |
1463 |
|
1463 | |||
1464 | #conf.status_device() |
|
1464 | #conf.status_device() | |
1465 |
|
1465 | |||
1466 | return redirect(conf.get_absolute_url()) |
|
1466 | return redirect(conf.get_absolute_url()) | |
1467 |
|
1467 | |||
|
1468 | @login_required | |||
|
1469 | def dev_conf_stop_mqtt(request, id_conf): | |||
|
1470 | ||||
|
1471 | conf = get_object_or_404(Configuration, pk=id_conf) | |||
|
1472 | ||||
|
1473 | if conf.stop_device_mqtt(): | |||
|
1474 | messages.success(request, conf.message) | |||
|
1475 | else: | |||
|
1476 | messages.error(request, conf.message) | |||
|
1477 | ||||
|
1478 | #conf.status_device() | |||
|
1479 | ||||
|
1480 | return redirect(conf.get_absolute_url()) | |||
1468 |
|
1481 | |||
1469 | @login_required |
|
1482 | @login_required | |
1470 | def dev_conf_status(request, id_conf): |
|
1483 | def dev_conf_status(request, id_conf): | |
1471 |
|
1484 | |||
1472 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1485 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1473 |
|
1486 | |||
1474 | conf_active = Configuration.objects.filter(pk=conf.device.conf_active).first() |
|
1487 | conf_active = Configuration.objects.filter(pk=conf.device.conf_active).first() | |
1475 | if conf_active!=conf: |
|
1488 | if conf_active!=conf: | |
1476 | url = '#' if conf_active is None else conf_active.get_absolute_url() |
|
1489 | url = '#' if conf_active is None else conf_active.get_absolute_url() | |
1477 | label = 'None' if conf_active is None else conf_active.label |
|
1490 | label = 'None' if conf_active is None else conf_active.label | |
1478 | messages.warning( |
|
1491 | messages.warning( | |
1479 | request, |
|
1492 | request, | |
1480 | mark_safe('The current configuration has not been written to device, the active configuration is <a href="{}">{}</a>'.format( |
|
1493 | mark_safe('The current configuration has not been written to device, the active configuration is <a href="{}">{}</a>'.format( | |
1481 | url, |
|
1494 | url, | |
1482 | label |
|
1495 | label | |
1483 | )) |
|
1496 | )) | |
1484 | ) |
|
1497 | ) | |
1485 |
|
1498 | |||
1486 | return redirect(conf.get_absolute_url()) |
|
1499 | return redirect(conf.get_absolute_url()) | |
1487 |
|
1500 | |||
1488 | if conf.status_device(): |
|
1501 | if conf.status_device(): | |
1489 | messages.success(request, conf.message) |
|
1502 | messages.success(request, conf.message) | |
1490 | else: |
|
1503 | else: | |
1491 | messages.error(request, conf.message) |
|
1504 | messages.error(request, conf.message) | |
1492 |
|
1505 | |||
1493 | return redirect(conf.get_absolute_url()) |
|
1506 | return redirect(conf.get_absolute_url()) | |
1494 |
|
1507 | |||
1495 |
|
1508 | |||
1496 | @login_required |
|
1509 | @login_required | |
1497 | def dev_conf_reset(request, id_conf): |
|
1510 | def dev_conf_reset(request, id_conf): | |
1498 |
|
1511 | |||
1499 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1512 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1500 |
|
1513 | |||
1501 | if conf.reset_device(): |
|
1514 | if conf.reset_device(): | |
1502 | messages.success(request, conf.message) |
|
1515 | messages.success(request, conf.message) | |
1503 | else: |
|
1516 | else: | |
1504 | messages.error(request, conf.message) |
|
1517 | messages.error(request, conf.message) | |
1505 |
|
1518 | |||
1506 | return redirect(conf.get_absolute_url()) |
|
1519 | return redirect(conf.get_absolute_url()) | |
1507 |
|
1520 | |||
1508 |
|
1521 | |||
1509 | @login_required |
|
1522 | @login_required | |
1510 | def dev_conf_write(request, id_conf): |
|
1523 | def dev_conf_write(request, id_conf): | |
1511 |
|
1524 | |||
1512 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1525 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1513 |
|
1526 | |||
1514 | if request.method == 'POST': |
|
1527 | if request.method == 'POST': | |
1515 | if conf.write_device(): |
|
1528 | if conf.write_device(): | |
1516 | mqtt_client.publish('abs/beams_up', 'Write normal') |
|
|||
1517 | conf.device.conf_active = conf.pk |
|
1529 | conf.device.conf_active = conf.pk | |
1518 | conf.device.save() |
|
1530 | conf.device.save() | |
1519 | messages.success(request, conf.message) |
|
1531 | messages.success(request, conf.message) | |
1520 | if has_been_modified(conf): |
|
1532 | if has_been_modified(conf): | |
1521 | conf.clone(type=1, template=False) |
|
1533 | conf.clone(type=1, template=False) | |
1522 | else: |
|
1534 | else: | |
1523 | messages.error(request, conf.message) |
|
1535 | messages.error(request, conf.message) | |
1524 |
|
1536 | |||
1525 | return redirect(get_object_or_404(Configuration, pk=id_conf).get_absolute_url()) |
|
1537 | return redirect(get_object_or_404(Configuration, pk=id_conf).get_absolute_url()) | |
1526 |
|
1538 | |||
1527 | kwargs = { |
|
1539 | kwargs = { | |
1528 | 'title': 'Write Configuration', |
|
1540 | 'title': 'Write Configuration', | |
1529 | 'suptitle': conf.label, |
|
1541 | 'suptitle': conf.label, | |
1530 | 'message': 'Are you sure yo want to write this {} configuration?'.format(conf.device), |
|
1542 | 'message': 'Are you sure yo want to write this {} configuration?'.format(conf.device), | |
1531 | 'delete': False |
|
1543 | 'delete': False | |
1532 | } |
|
1544 | } | |
1533 | kwargs['menu_configurations'] = 'active' |
|
1545 | kwargs['menu_configurations'] = 'active' | |
1534 |
|
1546 | |||
1535 | return render(request, 'confirm.html', kwargs) |
|
1547 | return render(request, 'confirm.html', kwargs) | |
1536 |
|
1548 | |||
1537 |
|
1549 | |||
1538 | @login_required |
|
1550 | @login_required | |
1539 | def dev_conf_write_mqtt(request,id_conf): |
|
1551 | def dev_conf_write_mqtt(request,id_conf): | |
1540 |
|
1552 | |||
1541 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1553 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1542 |
|
1554 | |||
1543 | if request.method == 'POST': |
|
1555 | if request.method == 'POST': | |
1544 | if conf.write_device_mqtt(): |
|
1556 | if conf.write_device_mqtt(): | |
1545 | mqtt_client.publish('abs/beams_up', 'Mqtt') |
|
|||
1546 | #mqtt_client.publish('abs/beams_down', 'Hola down') |
|
|||
1547 |
|
||||
1548 | conf.device.conf_active = conf.pk |
|
1557 | conf.device.conf_active = conf.pk | |
1549 | conf.device.save() |
|
1558 | conf.device.save() | |
1550 | messages.success(request, conf.message) |
|
1559 | messages.success(request, conf.message) | |
1551 | if has_been_modified(conf): |
|
1560 | if has_been_modified(conf): | |
1552 | conf.clone(type=1, template=False) |
|
1561 | conf.clone(type=1, template=False) | |
1553 | else: |
|
1562 | else: | |
1554 | messages.error(request, conf.message) |
|
1563 | messages.error(request, conf.message) | |
1555 |
|
1564 | print("return",flush=True) | ||
1556 | return redirect(get_object_or_404(Configuration, pk=id_conf).get_absolute_url()) |
|
1565 | return redirect(get_object_or_404(Configuration, pk=id_conf).get_absolute_url()) | |
1557 |
|
1566 | |||
1558 | kwargs = { |
|
1567 | kwargs = { | |
1559 | 'title': 'MQTT Write Configuration', |
|
1568 | 'title': 'MQTT Write Configuration', | |
1560 | 'suptitle': conf.label, |
|
1569 | 'suptitle': conf.label, | |
1561 | 'message': 'Are you sure yo want to write through MQTT this {} configuration?'.format(conf.device), |
|
1570 | 'message': 'Are you sure yo want to write through MQTT this {} configuration?'.format(conf.device), | |
1562 | 'delete': False |
|
1571 | 'delete': False | |
1563 | } |
|
1572 | } | |
1564 | kwargs['menu_configurations'] = 'active' |
|
1573 | kwargs['menu_configurations'] = 'active' | |
1565 |
|
1574 | print("Confirm",flush=True) | ||
1566 | return render(request, 'confirm.html', kwargs) |
|
1575 | return render(request, 'confirm.html', kwargs) | |
1567 |
|
1576 | |||
1568 |
|
1577 | |||
1569 | @login_required |
|
1578 | @login_required | |
1570 | def dev_conf_read(request, id_conf): |
|
1579 | def dev_conf_read(request, id_conf): | |
1571 |
|
1580 | |||
1572 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1581 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1573 |
|
1582 | |||
1574 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1583 | DevConfForm = CONF_FORMS[conf.device.device_type.name] | |
1575 |
|
1584 | |||
1576 | if request.method == 'GET': |
|
1585 | if request.method == 'GET': | |
1577 |
|
1586 | |||
1578 | parms = conf.read_device() |
|
1587 | parms = conf.read_device() | |
1579 | #conf.status_device() |
|
1588 | #conf.status_device() | |
1580 |
|
1589 | |||
1581 | if not parms: |
|
1590 | if not parms: | |
1582 | messages.error(request, conf.message) |
|
1591 | messages.error(request, conf.message) | |
1583 | return redirect(conf.get_absolute_url()) |
|
1592 | return redirect(conf.get_absolute_url()) | |
1584 |
|
1593 | |||
1585 | form = DevConfForm(initial=parms, instance=conf) |
|
1594 | form = DevConfForm(initial=parms, instance=conf) | |
1586 |
|
1595 | |||
1587 | if request.method == 'POST': |
|
1596 | if request.method == 'POST': | |
1588 | form = DevConfForm(request.POST, instance=conf) |
|
1597 | form = DevConfForm(request.POST, instance=conf) | |
1589 |
|
1598 | |||
1590 | if form.is_valid(): |
|
1599 | if form.is_valid(): | |
1591 | form.save() |
|
1600 | form.save() | |
1592 | return redirect(conf.get_absolute_url()) |
|
1601 | return redirect(conf.get_absolute_url()) | |
1593 |
|
1602 | |||
1594 | messages.error(request, "Parameters could not be saved") |
|
1603 | messages.error(request, "Parameters could not be saved") | |
1595 |
|
1604 | |||
1596 | kwargs = {} |
|
1605 | kwargs = {} | |
1597 | kwargs['id_dev'] = conf.id |
|
1606 | kwargs['id_dev'] = conf.id | |
1598 | kwargs['form'] = form |
|
1607 | kwargs['form'] = form | |
1599 | kwargs['title'] = 'Device Configuration' |
|
1608 | kwargs['title'] = 'Device Configuration' | |
1600 | kwargs['suptitle'] = 'Parameters read from device' |
|
1609 | kwargs['suptitle'] = 'Parameters read from device' | |
1601 | kwargs['button'] = 'Save' |
|
1610 | kwargs['button'] = 'Save' | |
1602 |
|
1611 | |||
1603 | ###### SIDEBAR ###### |
|
1612 | ###### SIDEBAR ###### | |
1604 | kwargs.update(sidebar(conf=conf)) |
|
1613 | kwargs.update(sidebar(conf=conf)) | |
1605 |
|
1614 | |||
1606 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1615 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) | |
1607 |
|
1616 | |||
1608 |
|
1617 | |||
1609 | @login_required |
|
1618 | @login_required | |
1610 | def dev_conf_import(request, id_conf): |
|
1619 | def dev_conf_import(request, id_conf): | |
1611 |
|
1620 | |||
1612 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1621 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1613 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1622 | DevConfForm = CONF_FORMS[conf.device.device_type.name] | |
1614 |
|
1623 | |||
1615 | if request.method == 'GET': |
|
1624 | if request.method == 'GET': | |
1616 | file_form = UploadFileForm() |
|
1625 | file_form = UploadFileForm() | |
1617 |
|
1626 | |||
1618 | if request.method == 'POST': |
|
1627 | if request.method == 'POST': | |
1619 | file_form = UploadFileForm(request.POST, request.FILES) |
|
1628 | file_form = UploadFileForm(request.POST, request.FILES) | |
1620 |
|
1629 | |||
1621 | if file_form.is_valid(): |
|
1630 | if file_form.is_valid(): | |
1622 |
|
1631 | |||
1623 | data = conf.import_from_file(request.FILES['file']) |
|
1632 | data = conf.import_from_file(request.FILES['file']) | |
1624 | parms = Params(data=data).get_conf( |
|
1633 | parms = Params(data=data).get_conf( | |
1625 | dtype=conf.device.device_type.name) |
|
1634 | dtype=conf.device.device_type.name) | |
1626 |
|
1635 | |||
1627 | if parms: |
|
1636 | if parms: | |
1628 |
|
1637 | |||
1629 | form = DevConfForm(initial=parms, instance=conf) |
|
1638 | form = DevConfForm(initial=parms, instance=conf) | |
1630 |
|
1639 | |||
1631 | kwargs = {} |
|
1640 | kwargs = {} | |
1632 | kwargs['id_dev'] = conf.id |
|
1641 | kwargs['id_dev'] = conf.id | |
1633 | kwargs['form'] = form |
|
1642 | kwargs['form'] = form | |
1634 | kwargs['title'] = 'Device Configuration' |
|
1643 | kwargs['title'] = 'Device Configuration' | |
1635 | kwargs['suptitle'] = 'Parameters imported' |
|
1644 | kwargs['suptitle'] = 'Parameters imported' | |
1636 | kwargs['button'] = 'Save' |
|
1645 | kwargs['button'] = 'Save' | |
1637 | kwargs['action'] = conf.get_absolute_url_edit() |
|
1646 | kwargs['action'] = conf.get_absolute_url_edit() | |
1638 | kwargs['previous'] = conf.get_absolute_url() |
|
1647 | kwargs['previous'] = conf.get_absolute_url() | |
1639 |
|
1648 | |||
1640 | ###### SIDEBAR ###### |
|
1649 | ###### SIDEBAR ###### | |
1641 | kwargs.update(sidebar(conf=conf)) |
|
1650 | kwargs.update(sidebar(conf=conf)) | |
1642 |
|
1651 | |||
1643 | messages.success( |
|
1652 | messages.success( | |
1644 | request, "Parameters imported from: '%s'." % request.FILES['file'].name) |
|
1653 | request, "Parameters imported from: '%s'." % request.FILES['file'].name) | |
1645 |
|
1654 | |||
1646 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1655 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) | |
1647 |
|
1656 | |||
1648 | messages.error(request, "Could not import parameters from file") |
|
1657 | messages.error(request, "Could not import parameters from file") | |
1649 |
|
1658 | |||
1650 | kwargs = {} |
|
1659 | kwargs = {} | |
1651 | kwargs['id_dev'] = conf.id |
|
1660 | kwargs['id_dev'] = conf.id | |
1652 | kwargs['title'] = 'Device Configuration' |
|
1661 | kwargs['title'] = 'Device Configuration' | |
1653 | kwargs['form'] = file_form |
|
1662 | kwargs['form'] = file_form | |
1654 | kwargs['suptitle'] = 'Importing file' |
|
1663 | kwargs['suptitle'] = 'Importing file' | |
1655 | kwargs['button'] = 'Import' |
|
1664 | kwargs['button'] = 'Import' | |
1656 | kwargs['menu_configurations'] = 'active' |
|
1665 | kwargs['menu_configurations'] = 'active' | |
1657 |
|
1666 | |||
1658 | kwargs.update(sidebar(conf=conf)) |
|
1667 | kwargs.update(sidebar(conf=conf)) | |
1659 |
|
1668 | |||
1660 | return render(request, 'dev_conf_import.html', kwargs) |
|
1669 | return render(request, 'dev_conf_import.html', kwargs) | |
1661 |
|
1670 | |||
1662 |
|
1671 | |||
1663 | @login_required |
|
1672 | @login_required | |
1664 | def dev_conf_export(request, id_conf): |
|
1673 | def dev_conf_export(request, id_conf): | |
1665 |
|
1674 | |||
1666 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1675 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1667 |
|
1676 | |||
1668 | if request.method == 'GET': |
|
1677 | if request.method == 'GET': | |
1669 | file_form = DownloadFileForm(conf.device.device_type.name) |
|
1678 | file_form = DownloadFileForm(conf.device.device_type.name) | |
1670 |
|
1679 | |||
1671 | if request.method == 'POST': |
|
1680 | if request.method == 'POST': | |
1672 | file_form = DownloadFileForm( |
|
1681 | file_form = DownloadFileForm( | |
1673 | conf.device.device_type.name, request.POST) |
|
1682 | conf.device.device_type.name, request.POST) | |
1674 |
|
1683 | |||
1675 | if file_form.is_valid(): |
|
1684 | if file_form.is_valid(): | |
1676 | fields = conf.export_to_file( |
|
1685 | fields = conf.export_to_file( | |
1677 | format=file_form.cleaned_data['format']) |
|
1686 | format=file_form.cleaned_data['format']) | |
1678 | if not fields['content']: |
|
1687 | if not fields['content']: | |
1679 | messages.error(request, conf.message) |
|
1688 | messages.error(request, conf.message) | |
1680 | return redirect(conf.get_absolute_url_export()) |
|
1689 | return redirect(conf.get_absolute_url_export()) | |
1681 | response = HttpResponse(content_type=fields['content_type']) |
|
1690 | response = HttpResponse(content_type=fields['content_type']) | |
1682 | response['Content-Disposition'] = 'attachment; filename="%s"' % fields['filename'] |
|
1691 | response['Content-Disposition'] = 'attachment; filename="%s"' % fields['filename'] | |
1683 | response.write(fields['content']) |
|
1692 | response.write(fields['content']) | |
1684 |
|
1693 | |||
1685 | return response |
|
1694 | return response | |
1686 |
|
1695 | |||
1687 | messages.error(request, "Could not export parameters") |
|
1696 | messages.error(request, "Could not export parameters") | |
1688 |
|
1697 | |||
1689 | kwargs = {} |
|
1698 | kwargs = {} | |
1690 | kwargs['id_dev'] = conf.id |
|
1699 | kwargs['id_dev'] = conf.id | |
1691 | kwargs['title'] = 'Device Configuration' |
|
1700 | kwargs['title'] = 'Device Configuration' | |
1692 | kwargs['form'] = file_form |
|
1701 | kwargs['form'] = file_form | |
1693 | kwargs['suptitle'] = 'Exporting file' |
|
1702 | kwargs['suptitle'] = 'Exporting file' | |
1694 | kwargs['button'] = 'Export' |
|
1703 | kwargs['button'] = 'Export' | |
1695 | kwargs['menu_configurations'] = 'active' |
|
1704 | kwargs['menu_configurations'] = 'active' | |
1696 |
|
1705 | |||
1697 | return render(request, 'dev_conf_export.html', kwargs) |
|
1706 | return render(request, 'dev_conf_export.html', kwargs) | |
1698 |
|
1707 | |||
1699 |
|
1708 | |||
1700 | @login_required |
|
1709 | @login_required | |
1701 | def dev_conf_delete(request, id_conf): |
|
1710 | def dev_conf_delete(request, id_conf): | |
1702 |
|
1711 | |||
1703 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1712 | conf = get_object_or_404(Configuration, pk=id_conf) | |
1704 |
|
1713 | |||
1705 | if request.method == 'POST': |
|
1714 | if request.method == 'POST': | |
1706 | if is_developer(request.user): |
|
1715 | if is_developer(request.user): | |
1707 | conf.delete() |
|
1716 | conf.delete() | |
1708 | return redirect('url_dev_confs') |
|
1717 | return redirect('url_dev_confs') | |
1709 |
|
1718 | |||
1710 | messages.error(request, 'Not enough permission to delete this object') |
|
1719 | messages.error(request, 'Not enough permission to delete this object') | |
1711 | return redirect(conf.get_absolute_url()) |
|
1720 | return redirect(conf.get_absolute_url()) | |
1712 |
|
1721 | |||
1713 | kwargs = { |
|
1722 | kwargs = { | |
1714 | 'title': 'Delete', |
|
1723 | 'title': 'Delete', | |
1715 | 'suptitle': 'Configuration', |
|
1724 | 'suptitle': 'Configuration', | |
1716 | 'object': conf, |
|
1725 | 'object': conf, | |
1717 | 'delete': True |
|
1726 | 'delete': True | |
1718 | } |
|
1727 | } | |
1719 | kwargs['menu_configurations'] = 'active' |
|
1728 | kwargs['menu_configurations'] = 'active' | |
1720 |
|
1729 | |||
1721 | return render(request, 'confirm.html', kwargs) |
|
1730 | return render(request, 'confirm.html', kwargs) | |
1722 |
|
1731 | |||
1723 |
|
1732 | |||
1724 | def sidebar(**kwargs): |
|
1733 | def sidebar(**kwargs): | |
1725 |
|
1734 | |||
1726 | side_data = {} |
|
1735 | side_data = {} | |
1727 |
|
1736 | |||
1728 | conf = kwargs.get('conf', None) |
|
1737 | conf = kwargs.get('conf', None) | |
1729 | experiment = kwargs.get('experiment', None) |
|
1738 | experiment = kwargs.get('experiment', None) | |
1730 |
|
1739 | |||
1731 | if not experiment: |
|
1740 | if not experiment: | |
1732 | experiment = conf.experiment |
|
1741 | experiment = conf.experiment | |
1733 |
|
1742 | |||
1734 | if experiment: |
|
1743 | if experiment: | |
1735 | side_data['experiment'] = experiment |
|
1744 | side_data['experiment'] = experiment | |
1736 | campaign = experiment.campaign_set.all() |
|
1745 | campaign = experiment.campaign_set.all() | |
1737 | if campaign: |
|
1746 | if campaign: | |
1738 | side_data['campaign'] = campaign[0] |
|
1747 | side_data['campaign'] = campaign[0] | |
1739 | experiments = campaign[0].experiments.all().order_by('name') |
|
1748 | experiments = campaign[0].experiments.all().order_by('name') | |
1740 | else: |
|
1749 | else: | |
1741 | experiments = [experiment] |
|
1750 | experiments = [experiment] | |
1742 | configurations = experiment.configuration_set.filter(type=0) |
|
1751 | configurations = experiment.configuration_set.filter(type=0) | |
1743 | side_data['side_experiments'] = experiments |
|
1752 | side_data['side_experiments'] = experiments | |
1744 | side_data['side_configurations'] = configurations.order_by( |
|
1753 | side_data['side_configurations'] = configurations.order_by( | |
1745 | 'device__device_type__name') |
|
1754 | 'device__device_type__name') | |
1746 |
|
1755 | |||
1747 | return side_data |
|
1756 | return side_data | |
1748 |
|
1757 | |||
1749 | def get_paginator(model, page, order, filters={}, n=8): |
|
1758 | def get_paginator(model, page, order, filters={}, n=8): | |
1750 | kwargs = {} |
|
1759 | kwargs = {} | |
1751 | query = Q() |
|
1760 | query = Q() | |
1752 | if isinstance(filters, QueryDict): |
|
1761 | if isinstance(filters, QueryDict): | |
1753 | filters = filters.dict() |
|
1762 | filters = filters.dict() | |
1754 | copy_filters=filters.copy() |
|
1763 | copy_filters=filters.copy() | |
1755 | [filters.pop(key) for key in copy_filters.keys() if copy_filters[key] in (' ', ' ')] |
|
1764 | [filters.pop(key) for key in copy_filters.keys() if copy_filters[key] in (' ', ' ')] | |
1756 | filters.pop('page', None) |
|
1765 | filters.pop('page', None) | |
1757 |
|
1766 | |||
1758 | fields = [f.name for f in model._meta.get_fields()] |
|
1767 | fields = [f.name for f in model._meta.get_fields()] | |
1759 |
|
1768 | |||
1760 | if 'template' in copy_filters: |
|
1769 | if 'template' in copy_filters: | |
1761 | filters['template'] = True |
|
1770 | filters['template'] = True | |
1762 | if 'historical' in copy_filters: |
|
1771 | if 'historical' in copy_filters: | |
1763 | filters.pop('historical') |
|
1772 | filters.pop('historical') | |
1764 | filters['type'] = 1 |
|
1773 | filters['type'] = 1 | |
1765 | elif 'type' in fields: |
|
1774 | elif 'type' in fields: | |
1766 | filters['type'] = 0 |
|
1775 | filters['type'] = 0 | |
1767 | if 'start_date' in copy_filters: |
|
1776 | if 'start_date' in copy_filters: | |
1768 | filters['start_date__gte'] =filters.pop('start_date') |
|
1777 | filters['start_date__gte'] =filters.pop('start_date') | |
1769 | if 'end_date' in copy_filters: |
|
1778 | if 'end_date' in copy_filters: | |
1770 | filters['start_date__lte'] =filters.pop('end_date') |
|
1779 | filters['start_date__lte'] =filters.pop('end_date') | |
1771 | if 'tags' in copy_filters: |
|
1780 | if 'tags' in copy_filters: | |
1772 | tags =filters.pop('tags') |
|
1781 | tags =filters.pop('tags') | |
1773 | if 'tags' in fields: |
|
1782 | if 'tags' in fields: | |
1774 | query = query | Q(tags__icontains=tags) |
|
1783 | query = query | Q(tags__icontains=tags) | |
1775 | if 'label' in fields: |
|
1784 | if 'label' in fields: | |
1776 | query = query | Q(label__icontains=tags) |
|
1785 | query = query | Q(label__icontains=tags) | |
1777 | if 'location' in fields: |
|
1786 | if 'location' in fields: | |
1778 | query = query | Q(location__name__icontains=tags) |
|
1787 | query = query | Q(location__name__icontains=tags) | |
1779 | if 'device' in fields: |
|
1788 | if 'device' in fields: | |
1780 | query = query | Q(device__device_type__name__icontains=tags) |
|
1789 | query = query | Q(device__device_type__name__icontains=tags) | |
1781 | query = query | Q(device__location__name__icontains=tags) |
|
1790 | query = query | Q(device__location__name__icontains=tags) | |
1782 | if 'device_type' in fields: |
|
1791 | if 'device_type' in fields: | |
1783 | query = query | Q(device_type__name__icontains=tags) |
|
1792 | query = query | Q(device_type__name__icontains=tags) | |
1784 |
|
1793 | |||
1785 | if 'mine' in copy_filters: |
|
1794 | if 'mine' in copy_filters: | |
1786 | filters['author_id'] =filters['mine'] |
|
1795 | filters['author_id'] =filters['mine'] | |
1787 | filters.pop('mine') |
|
1796 | filters.pop('mine') | |
1788 | object_list = model.objects.filter(query, **filters).order_by(*order) |
|
1797 | object_list = model.objects.filter(query, **filters).order_by(*order) | |
1789 | paginator = Paginator(object_list, n) |
|
1798 | paginator = Paginator(object_list, n) | |
1790 |
|
1799 | |||
1791 | try: |
|
1800 | try: | |
1792 | objects = paginator.page(page) |
|
1801 | objects = paginator.page(page) | |
1793 | except PageNotAnInteger: |
|
1802 | except PageNotAnInteger: | |
1794 | objects = paginator.page(1) |
|
1803 | objects = paginator.page(1) | |
1795 | except EmptyPage: |
|
1804 | except EmptyPage: | |
1796 | objects = paginator.page(paginator.num_pages) |
|
1805 | objects = paginator.page(paginator.num_pages) | |
1797 |
|
1806 | |||
1798 | kwargs['objects'] = objects |
|
1807 | kwargs['objects'] = objects | |
1799 | kwargs['offset'] = (int(page)-1)*n if page else 0 |
|
1808 | kwargs['offset'] = (int(page)-1)*n if page else 0 | |
1800 |
|
1809 | |||
1801 | return kwargs |
|
1810 | return kwargs | |
1802 |
|
1811 | |||
1803 | # def get_paginator(model, page, order, filters={}, n=8): |
|
1812 | # def get_paginator(model, page, order, filters={}, n=8): | |
1804 | # kwargs = {} |
|
1813 | # kwargs = {} | |
1805 | # query = Q() |
|
1814 | # query = Q() | |
1806 | # if isinstance(filters, QueryDict): |
|
1815 | # if isinstance(filters, QueryDict): | |
1807 | # filters = filters.dict() |
|
1816 | # filters = filters.dict() | |
1808 | # [filters.pop(key) for key in filters.keys() if filters[key] in ('', ' ')] |
|
1817 | # [filters.pop(key) for key in filters.keys() if filters[key] in ('', ' ')] | |
1809 | # filters.pop('page', None) |
|
1818 | # filters.pop('page', None) | |
1810 |
|
1819 | |||
1811 | # fields = [f.name for f in model._meta.get_fields()] |
|
1820 | # fields = [f.name for f in model._meta.get_fields()] | |
1812 |
|
1821 | |||
1813 | # if 'template' in filters: |
|
1822 | # if 'template' in filters: | |
1814 | # filters['template'] = True |
|
1823 | # filters['template'] = True | |
1815 | # if 'historical' in filters: |
|
1824 | # if 'historical' in filters: | |
1816 | # filters.pop('historical') |
|
1825 | # filters.pop('historical') | |
1817 | # filters['type'] = 1 |
|
1826 | # filters['type'] = 1 | |
1818 | # elif 'type' in fields: |
|
1827 | # elif 'type' in fields: | |
1819 | # filters['type'] = 0 |
|
1828 | # filters['type'] = 0 | |
1820 | # if 'start_date' in filters: |
|
1829 | # if 'start_date' in filters: | |
1821 | # filters['start_date__gte'] = filters.pop('start_date') |
|
1830 | # filters['start_date__gte'] = filters.pop('start_date') | |
1822 | # if 'end_date' in filters: |
|
1831 | # if 'end_date' in filters: | |
1823 | # filters['start_date__lte'] = filters.pop('end_date') |
|
1832 | # filters['start_date__lte'] = filters.pop('end_date') | |
1824 | # if 'tags' in filters: |
|
1833 | # if 'tags' in filters: | |
1825 | # tags = filters.pop('tags') |
|
1834 | # tags = filters.pop('tags') | |
1826 | # if 'tags' in fields: |
|
1835 | # if 'tags' in fields: | |
1827 | # query = query | Q(tags__icontains=tags) |
|
1836 | # query = query | Q(tags__icontains=tags) | |
1828 | # if 'label' in fields: |
|
1837 | # if 'label' in fields: | |
1829 | # query = query | Q(label__icontains=tags) |
|
1838 | # query = query | Q(label__icontains=tags) | |
1830 | # if 'location' in fields: |
|
1839 | # if 'location' in fields: | |
1831 | # query = query | Q(location__name__icontains=tags) |
|
1840 | # query = query | Q(location__name__icontains=tags) | |
1832 | # if 'device' in fields: |
|
1841 | # if 'device' in fields: | |
1833 | # query = query | Q(device__device_type__name__icontains=tags) |
|
1842 | # query = query | Q(device__device_type__name__icontains=tags) | |
1834 | # query = query | Q(device__location__name__icontains=tags) |
|
1843 | # query = query | Q(device__location__name__icontains=tags) | |
1835 | # if 'device_type' in fields: |
|
1844 | # if 'device_type' in fields: | |
1836 | # query = query | Q(device_type__name__icontains=tags) |
|
1845 | # query = query | Q(device_type__name__icontains=tags) | |
1837 |
|
1846 | |||
1838 | # if 'mine' in filters: |
|
1847 | # if 'mine' in filters: | |
1839 | # filters['author_id'] = filters['mine'] |
|
1848 | # filters['author_id'] = filters['mine'] | |
1840 | # filters.pop('mine') |
|
1849 | # filters.pop('mine') | |
1841 | # object_list = model.objects.filter(query, **filters).order_by(*order) |
|
1850 | # object_list = model.objects.filter(query, **filters).order_by(*order) | |
1842 | # paginator = Paginator(object_list, n) |
|
1851 | # paginator = Paginator(object_list, n) | |
1843 |
|
1852 | |||
1844 | # try: |
|
1853 | # try: | |
1845 | # objects = paginator.page(page) |
|
1854 | # objects = paginator.page(page) | |
1846 | # except PageNotAnInteger: |
|
1855 | # except PageNotAnInteger: | |
1847 | # objects = paginator.page(1) |
|
1856 | # objects = paginator.page(1) | |
1848 | # except EmptyPage: |
|
1857 | # except EmptyPage: | |
1849 | # objects = paginator.page(paginator.num_pages) |
|
1858 | # objects = paginator.page(paginator.num_pages) | |
1850 |
|
1859 | |||
1851 | # kwargs['objects'] = objects |
|
1860 | # kwargs['objects'] = objects | |
1852 | # kwargs['offset'] = (int(page)-1)*n if page else 0 |
|
1861 | # kwargs['offset'] = (int(page)-1)*n if page else 0 | |
1853 |
|
1862 | |||
1854 | # return kwargs |
|
1863 | # return kwargs | |
1855 |
|
1864 | |||
1856 |
|
1865 | |||
1857 | def operation(request, id_camp=None): |
|
1866 | def operation(request, id_camp=None): | |
1858 |
|
1867 | |||
1859 | kwargs = {} |
|
1868 | kwargs = {} | |
1860 | kwargs['title'] = 'Radars Operation' |
|
1869 | kwargs['title'] = 'Radars Operation' | |
1861 | kwargs['no_sidebar'] = True |
|
1870 | kwargs['no_sidebar'] = True | |
1862 | kwargs['menu_operation'] = 'active' |
|
1871 | kwargs['menu_operation'] = 'active' | |
1863 | campaigns = Campaign.objects.filter(start_date__lte=datetime.now(), |
|
1872 | campaigns = Campaign.objects.filter(start_date__lte=datetime.now(), | |
1864 | end_date__gte=datetime.now()).order_by('-start_date') |
|
1873 | end_date__gte=datetime.now()).order_by('-start_date') | |
1865 |
|
1874 | |||
1866 | if id_camp: |
|
1875 | if id_camp: | |
1867 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
1876 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
1868 | form = OperationForm( |
|
1877 | form = OperationForm( | |
1869 | initial={'campaign': campaign.id}, campaigns=campaigns) |
|
1878 | initial={'campaign': campaign.id}, campaigns=campaigns) | |
1870 | kwargs['campaign'] = campaign |
|
1879 | kwargs['campaign'] = campaign | |
1871 | else: |
|
1880 | else: | |
1872 | # form = OperationForm(campaigns=campaigns) |
|
1881 | # form = OperationForm(campaigns=campaigns) | |
1873 | kwargs['campaigns'] = campaigns |
|
1882 | kwargs['campaigns'] = campaigns | |
1874 | return render(request, 'operation.html', kwargs) |
|
1883 | return render(request, 'operation.html', kwargs) | |
1875 |
|
1884 | |||
1876 | #---Experiment |
|
1885 | #---Experiment | |
1877 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] |
|
1886 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] | |
1878 | kwargs['experiment_keys'] = keys[1:] |
|
1887 | kwargs['experiment_keys'] = keys[1:] | |
1879 | kwargs['experiments'] = experiments |
|
1888 | kwargs['experiments'] = experiments | |
1880 | #---Radar |
|
1889 | #---Radar | |
1881 | kwargs['locations'] = campaign.get_experiments_by_radar() |
|
1890 | kwargs['locations'] = campaign.get_experiments_by_radar() | |
1882 | kwargs['form'] = form |
|
1891 | kwargs['form'] = form | |
1883 |
|
1892 | |||
1884 | return render(request, 'operation.html', kwargs) |
|
1893 | return render(request, 'operation.html', kwargs) | |
1885 |
|
1894 | |||
1886 |
|
1895 | |||
1887 | @login_required |
|
1896 | @login_required | |
1888 | def radar_start(request, id_camp, id_radar): |
|
1897 | def radar_start(request, id_camp, id_radar): | |
1889 |
|
1898 | |||
1890 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
1899 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
1891 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] |
|
1900 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] | |
1892 | now = datetime.now() |
|
1901 | now = datetime.now() | |
1893 |
|
1902 | |||
1894 | for exp in experiments: |
|
1903 | for exp in experiments: | |
1895 | #app.control.revoke(exp.task) |
|
1904 | #app.control.revoke(exp.task) | |
1896 | print("----------------------") |
|
1905 | print("----------------------") | |
1897 | print("status:->", exp.status) |
|
1906 | print("status:->", exp.status) | |
1898 | start = datetime.combine(datetime.now().date(), exp.start_time) |
|
1907 | start = datetime.combine(datetime.now().date(), exp.start_time) | |
1899 | end = datetime.combine(datetime.now().date(), exp.end_time) |
|
1908 | end = datetime.combine(datetime.now().date(), exp.end_time) | |
1900 | print("start exp: ",exp.start_time) |
|
1909 | print("start exp: ",exp.start_time) | |
1901 | print("end exp: ",exp.end_time) |
|
1910 | print("end exp: ",exp.end_time) | |
1902 |
|
1911 | |||
1903 | print("start comb: ",start) |
|
1912 | print("start comb: ",start) | |
1904 | print("end comb: ",end) |
|
1913 | print("end comb: ",end) | |
1905 | print(is_aware(start)) |
|
1914 | print(is_aware(start)) | |
1906 | print("start camp",campaign.start_date) |
|
1915 | print("start camp",campaign.start_date) | |
1907 | print("end camp",campaign.end_date) |
|
1916 | print("end camp",campaign.end_date) | |
1908 | print(is_aware(campaign.start_date)) |
|
1917 | print(is_aware(campaign.start_date)) | |
1909 | if end < start: |
|
1918 | if end < start: | |
1910 | end += timedelta(1) |
|
1919 | end += timedelta(1) | |
1911 |
|
1920 | |||
1912 | if exp.status == 2: |
|
1921 | if exp.status == 2: | |
1913 | messages.warning( |
|
1922 | messages.warning( | |
1914 | request, 'Experiment {} already running'.format(exp)) |
|
1923 | request, 'Experiment {} already running'.format(exp)) | |
1915 | #continue |
|
1924 | #continue | |
1916 |
|
1925 | |||
1917 | if exp.status == 3: |
|
1926 | if exp.status == 3: | |
1918 | messages.warning( |
|
1927 | messages.warning( | |
1919 | request, 'Experiment {} already programmed'.format(exp)) |
|
1928 | request, 'Experiment {} already programmed'.format(exp)) | |
1920 | #continue |
|
1929 | #continue | |
1921 |
|
1930 | |||
1922 | if exp.status == 1: |
|
1931 | if exp.status == 1: | |
1923 | messages.warning( |
|
1932 | messages.warning( | |
1924 | request, 'Experiment {} stopped'.format(exp)) |
|
1933 | request, 'Experiment {} stopped'.format(exp)) | |
1925 | #continue |
|
1934 | #continue | |
1926 |
|
1935 | |||
1927 | if start > campaign.end_date: |
|
1936 | if start > campaign.end_date: | |
1928 | messages.warning( |
|
1937 | messages.warning( | |
1929 | request, 'Experiment {} out of date'.format(exp)) |
|
1938 | request, 'Experiment {} out of date'.format(exp)) | |
1930 |
|
1939 | |||
1931 | #app.control.revoke(exp.task) |
|
1940 | #app.control.revoke(exp.task) | |
1932 | print("Llego luego del revoke") |
|
1941 | print("Llego luego del revoke") | |
1933 | if now >= start and now <= end: |
|
1942 | if now >= start and now <= end: | |
1934 |
|
1943 | |||
1935 | print("Caso now > start and < end -- (1)") |
|
1944 | print("Caso now > start and < end -- (1)") | |
1936 |
|
1945 | |||
1937 | # ------------------------------------------- |
|
1946 | # ------------------------------------------- | |
1938 |
|
1947 | |||
1939 | # task = task_start.delay(exp.id) |
|
1948 | # task = task_start.delay(exp.id) | |
1940 | # exp.task = task.id |
|
1949 | # exp.task = task.id | |
1941 | # exp.status = task.get() |
|
1950 | # exp.status = task.get() | |
1942 | # ------------------------------------------- |
|
1951 | # ------------------------------------------- | |
1943 |
|
1952 | |||
1944 | #exp.status = task.wait() |
|
1953 | #exp.status = task.wait() | |
1945 |
|
1954 | |||
1946 | if exp.status == 0: |
|
1955 | if exp.status == 0: | |
1947 | messages.error(request, 'Experiment {} not start'.format(exp)) |
|
1956 | messages.error(request, 'Experiment {} not start'.format(exp)) | |
1948 | if exp.status == 2: |
|
1957 | if exp.status == 2: | |
1949 | messages.success(request, 'Experiment {} started'.format(exp)) |
|
1958 | messages.success(request, 'Experiment {} started'.format(exp)) | |
1950 | elif now < start: |
|
1959 | elif now < start: | |
1951 | print("Caso now <= start -- (2)",exp.pk) |
|
1960 | print("Caso now <= start -- (2)",exp.pk) | |
1952 | #task = task_start.apply_async((exp.pk, ), eta=start)#start+timedelta(hours=5)) |
|
1961 | #task = task_start.apply_async((exp.pk, ), eta=start)#start+timedelta(hours=5)) | |
1953 | # task = task_start.apply_async((exp.pk, ), eta=start+timedelta(hours=5))#) |
|
1962 | # task = task_start.apply_async((exp.pk, ), eta=start+timedelta(hours=5))#) | |
1954 | # exp.task = task.id |
|
1963 | # exp.task = task.id | |
1955 | # exp.status = 3 |
|
1964 | # exp.status = 3 | |
1956 | messages.success(request, 'Experiment {} programmed to start at {}'.format(exp, start)) |
|
1965 | messages.success(request, 'Experiment {} programmed to start at {}'.format(exp, start)) | |
1957 | else: |
|
1966 | else: | |
1958 | print("Caso now > end -- (3)") |
|
1967 | print("Caso now > end -- (3)") | |
1959 | exp.status = 4 |
|
1968 | exp.status = 4 | |
1960 | messages.warning( |
|
1969 | messages.warning( | |
1961 | request, 'Experiment {} out of date'.format(exp)) |
|
1970 | request, 'Experiment {} out of date'.format(exp)) | |
1962 |
|
1971 | |||
1963 | exp.save() |
|
1972 | exp.save() | |
1964 |
|
1973 | |||
1965 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1974 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) | |
1966 |
|
1975 | |||
1967 |
|
1976 | |||
1968 | @login_required |
|
1977 | @login_required | |
1969 | def radar_stop(request, id_camp, id_radar): |
|
1978 | def radar_stop(request, id_camp, id_radar): | |
1970 |
|
1979 | |||
1971 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
1980 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
1972 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] |
|
1981 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] | |
1973 | print("Ingreso en stop radar_stop") |
|
1982 | print("Ingreso en stop radar_stop") | |
1974 | #for exp in experiments: |
|
1983 | #for exp in experiments: | |
1975 |
|
1984 | |||
1976 | # if exp.task: |
|
1985 | # if exp.task: | |
1977 | # print("Ingreso antes de revoke stop") |
|
1986 | # print("Ingreso antes de revoke stop") | |
1978 | # app.control.revoke(exp.task) |
|
1987 | # app.control.revoke(exp.task) | |
1979 |
|
1988 | |||
1980 |
|
1989 | |||
1981 | # if exp.status == 2: #status 2 es started |
|
1990 | # if exp.status == 2: #status 2 es started | |
1982 | # print("llama a exp.stop") |
|
1991 | # print("llama a exp.stop") | |
1983 | # exp.stop() |
|
1992 | # exp.stop() | |
1984 | # messages.warning(request, 'Experiment {} stopped'.format(exp)) |
|
1993 | # messages.warning(request, 'Experiment {} stopped'.format(exp)) | |
1985 | # exp.status = 1 |
|
1994 | # exp.status = 1 | |
1986 | # exp.save() |
|
1995 | # exp.save() | |
1987 |
|
1996 | |||
1988 | #return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1997 | #return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) | |
1989 |
|
1998 | |||
1990 |
|
1999 | |||
1991 | @login_required |
|
2000 | @login_required | |
1992 | def radar_refresh(request, id_camp, id_radar): |
|
2001 | def radar_refresh(request, id_camp, id_radar): | |
1993 |
|
2002 | |||
1994 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
2003 | campaign = get_object_or_404(Campaign, pk=id_camp) | |
1995 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] |
|
2004 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] | |
1996 |
|
2005 | |||
1997 | i = app.control.inspect() |
|
2006 | i = app.control.inspect() | |
1998 | print("inspect",i) |
|
2007 | print("inspect",i) | |
1999 | print(i.scheduled()) |
|
2008 | print(i.scheduled()) | |
2000 | print(i.scheduled().values()) |
|
2009 | print(i.scheduled().values()) | |
2001 | scheduled = list(i.scheduled().values())[0] |
|
2010 | scheduled = list(i.scheduled().values())[0] | |
2002 | revoked = list(i.revoked().values())[0] |
|
2011 | revoked = list(i.revoked().values())[0] | |
2003 |
|
2012 | |||
2004 | # for exp in experiments: |
|
2013 | # for exp in experiments: | |
2005 | # if exp.task in revoked: |
|
2014 | # if exp.task in revoked: | |
2006 | # exp.status = 1 |
|
2015 | # exp.status = 1 | |
2007 | # elif exp.task in [t['request']['id'] for t in scheduled if 'task_stop' in t['request']['name']]: |
|
2016 | # elif exp.task in [t['request']['id'] for t in scheduled if 'task_stop' in t['request']['name']]: | |
2008 | # exp.status = 2 |
|
2017 | # exp.status = 2 | |
2009 | # elif exp.task in [t['request']['id'] for t in scheduled if 'task_start' in t['request']['name']]: |
|
2018 | # elif exp.task in [t['request']['id'] for t in scheduled if 'task_start' in t['request']['name']]: | |
2010 | # exp.status = 3 |
|
2019 | # exp.status = 3 | |
2011 | # else: |
|
2020 | # else: | |
2012 | # exp.status = 4 |
|
2021 | # exp.status = 4 | |
2013 | # exp.save() |
|
2022 | # exp.save() | |
2014 | # return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
2023 | # return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) | |
2015 |
|
2024 | |||
2016 | #@login_required |
|
2025 | #@login_required | |
2017 | # def revoke_tasks(request, id_camp): |
|
2026 | # def revoke_tasks(request, id_camp): | |
2018 |
|
2027 | |||
2019 | # i = app.control.inspect() |
|
2028 | # i = app.control.inspect() | |
2020 | # scheduled = list(i.scheduled().values())[0] |
|
2029 | # scheduled = list(i.scheduled().values())[0] | |
2021 | # revoked = list(i.revoked().values())[0] |
|
2030 | # revoked = list(i.revoked().values())[0] | |
2022 |
|
2031 | |||
2023 | # for t in scheduled: |
|
2032 | # for t in scheduled: | |
2024 | # if t['request']['id'] in revoked: |
|
2033 | # if t['request']['id'] in revoked: | |
2025 | # continue |
|
2034 | # continue | |
2026 | # app.control.revoke(t['request']['id']) |
|
2035 | # app.control.revoke(t['request']['id']) | |
2027 | # exp = Experiment.objects.get(pk=eval(str(t['request']['args']))[0]) |
|
2036 | # exp = Experiment.objects.get(pk=eval(str(t['request']['args']))[0]) | |
2028 | # eta = t['eta'] |
|
2037 | # eta = t['eta'] | |
2029 | # #task = t['request']['name'].split('.')[-1] |
|
2038 | # #task = t['request']['name'].split('.')[-1] | |
2030 | # messages.warning(request, 'Scheduled {} at {} for experiment {} revoked'.format(task, eta, exp.name)) |
|
2039 | # messages.warning(request, 'Scheduled {} at {} for experiment {} revoked'.format(task, eta, exp.name)) | |
2031 |
|
2040 | |||
2032 | # return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
2041 | # return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) | |
2033 |
|
2042 | |||
2034 | # @login_required |
|
2043 | # @login_required | |
2035 | # def show_tasks(request, id_camp): |
|
2044 | # def show_tasks(request, id_camp): | |
2036 |
|
2045 | |||
2037 | # i = app.control.inspect() |
|
2046 | # i = app.control.inspect() | |
2038 | # scheduled = list(i.scheduled().values())[0] |
|
2047 | # scheduled = list(i.scheduled().values())[0] | |
2039 | # revoked = list(i.revoked().values())[0] |
|
2048 | # revoked = list(i.revoked().values())[0] | |
2040 |
|
2049 | |||
2041 | # for t in scheduled: |
|
2050 | # for t in scheduled: | |
2042 | # if t['request']['id'] in revoked: |
|
2051 | # if t['request']['id'] in revoked: | |
2043 | # continue |
|
2052 | # continue | |
2044 | # exp = Experiment.objects.get(pk=eval(str(t['request']['args']))[0]) |
|
2053 | # exp = Experiment.objects.get(pk=eval(str(t['request']['args']))[0]) | |
2045 | # eta = t['eta'] |
|
2054 | # eta = t['eta'] | |
2046 | # #task = t['request']['name'].split('.')[-1] |
|
2055 | # #task = t['request']['name'].split('.')[-1] | |
2047 | # #messages.success(request, 'Task {} scheduled at {} for experiment {}'.format(task, eta, exp.name)) |
|
2056 | # #messages.success(request, 'Task {} scheduled at {} for experiment {}'.format(task, eta, exp.name)) | |
2048 |
|
2057 | |||
2049 | # return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
2058 | # return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) | |
2050 |
|
2059 | |||
2051 | def real_time(request): |
|
2060 | def real_time(request): | |
2052 |
|
2061 | |||
2053 | graphic_path = "/home/fiorella/Pictures/catwbeanie.jpg" |
|
2062 | graphic_path = "/home/fiorella/Pictures/catwbeanie.jpg" | |
2054 |
|
2063 | |||
2055 | kwargs = {} |
|
2064 | kwargs = {} | |
2056 | kwargs['title'] = 'CLAIRE' |
|
2065 | kwargs['title'] = 'CLAIRE' | |
2057 | kwargs['suptitle'] = 'Real Time' |
|
2066 | kwargs['suptitle'] = 'Real Time' | |
2058 | kwargs['no_sidebar'] = True |
|
2067 | kwargs['no_sidebar'] = True | |
2059 | kwargs['graphic_path'] = graphic_path |
|
2068 | kwargs['graphic_path'] = graphic_path | |
2060 | kwargs['graphic1_path'] = 'http://www.bluemaize.net/im/girls-accessories/shark-beanie-11.jpg' |
|
2069 | kwargs['graphic1_path'] = 'http://www.bluemaize.net/im/girls-accessories/shark-beanie-11.jpg' | |
2061 |
|
2070 | |||
2062 | return render(request, 'real_time.html', kwargs) |
|
2071 | return render(request, 'real_time.html', kwargs) | |
2063 |
|
2072 | |||
2064 | def theme(request, theme): |
|
2073 | def theme(request, theme): | |
2065 |
|
2074 | |||
2066 | user = request.user |
|
2075 | user = request.user | |
2067 | user.profile.theme = theme |
|
2076 | user.profile.theme = theme | |
2068 | user.save() |
|
2077 | user.save() | |
2069 | return redirect('index') |
|
2078 | return redirect('index') |
General Comments 0
You need to be logged in to leave comments.
Login now