@@ -1,207 +1,254 | |||
|
1 | 1 | from django.shortcuts import redirect, render, get_object_or_404 |
|
2 | 2 | from django.contrib import messages |
|
3 | 3 | |
|
4 | 4 | from apps.main.models import Experiment, Configuration |
|
5 | 5 | from .models import CGSConfiguration |
|
6 | 6 | |
|
7 | 7 | from .forms import CGSConfigurationForm |
|
8 | 8 | from apps.main.views import sidebar |
|
9 | 9 | |
|
10 | 10 | import requests |
|
11 | import json | |
|
12 | from __builtin__ import None | |
|
11 | 13 | # Create your views here. |
|
12 | 14 | |
|
13 | 15 | def cgs_conf(request, id_conf): |
|
14 | 16 | |
|
15 | 17 | conf = get_object_or_404(CGSConfiguration, pk=id_conf) |
|
16 | 18 | |
|
17 | 19 | ip=conf.device.ip_address |
|
18 | 20 | port=conf.device.port_address |
|
19 | 21 | |
|
20 | 22 | kwargs = {} |
|
21 | 23 | |
|
22 | 24 | if request.method=='GET': |
|
23 | 25 | #r: response = icon, status |
|
24 | route = "http://" + str(ip) + ":" + str(port) + "/status/ad9548" | |
|
25 | r = requests.get(route) | |
|
26 | response = str(r.text) | |
|
27 |
response = |
|
|
28 |
|
|
|
29 |
|
|
|
30 | #print r.text | |
|
31 | #"icon" could be: "alert" or "okay" | |
|
32 | # Si hay alerta pero esta conectado | |
|
33 | if "alert" in icon: | |
|
34 |
if " |
|
|
35 | kwargs['connected'] = False | |
|
36 | else: | |
|
26 | try: | |
|
27 | route = "http://" + str(ip) + ":" + str(port) + "/status/ad9548" | |
|
28 | r = requests.get(route) | |
|
29 | response = str(r.text) | |
|
30 | response = response.split(";") | |
|
31 | icon = response[0] | |
|
32 | status = response[-1] | |
|
33 | #print r.text | |
|
34 | #"icon" could be: "alert" or "okay" | |
|
35 | # Si hay alerta pero esta conectado | |
|
36 | if "alert" in icon: | |
|
37 | if "Starting Up" in status: #No Esta conectado | |
|
38 | kwargs['connected'] = False | |
|
39 | else: | |
|
40 | kwargs['connected'] = True | |
|
41 | elif "okay" in icon: | |
|
37 | 42 | kwargs['connected'] = True |
|
38 | elif "okay" in icon: | |
|
39 |
kwargs['connected'] = |
|
|
40 | else: | |
|
43 | else: | |
|
44 | kwargs['connected'] = False | |
|
45 | ||
|
46 | except: | |
|
41 | 47 | kwargs['connected'] = False |
|
48 | status = "The Device is not connected." | |
|
42 | 49 | |
|
43 | 50 | if not kwargs['connected']: |
|
44 | 51 | messages.error(request, message=status) |
|
45 | 52 | |
|
46 | 53 | kwargs['dev_conf'] = conf |
|
47 | 54 | kwargs['dev_conf_keys'] = ['experiment', 'device', |
|
48 | 55 | 'freq0', 'freq1', |
|
49 | 56 | 'freq2', 'freq3'] |
|
50 | 57 | |
|
51 | 58 | kwargs['title'] = 'CGS Configuration' |
|
52 | 59 | kwargs['suptitle'] = 'Details' |
|
53 | 60 | |
|
54 | 61 | kwargs['button'] = 'Edit Configuration' |
|
55 | 62 | |
|
56 | 63 | ###### SIDEBAR ###### |
|
57 | 64 | kwargs.update(sidebar(conf)) |
|
58 | 65 | |
|
59 | 66 | return render(request, 'cgs_conf.html', kwargs) |
|
60 | 67 | |
|
61 | 68 | def cgs_conf_edit(request, id_conf): |
|
62 | 69 | |
|
63 | 70 | conf = get_object_or_404(CGSConfiguration, pk=id_conf) |
|
64 | 71 | |
|
65 | 72 | if request.method=='GET': |
|
66 | 73 | form = CGSConfigurationForm(instance=conf) |
|
67 | 74 | |
|
68 | 75 | if request.method=='POST': |
|
69 | 76 | form = CGSConfigurationForm(request.POST, instance=conf) |
|
70 | 77 | |
|
71 | 78 | if form.is_valid(): |
|
72 | 79 | conf = form.save(commit=False) |
|
73 | 80 | |
|
74 | 81 | if conf.verify_frequencies(): |
|
75 | 82 | |
|
83 | if conf.freq0 == None: conf.freq0 = 0 | |
|
84 | if conf.freq1 == None: conf.freq0 = 0 | |
|
85 | if conf.freq2 == None: conf.freq0 = 0 | |
|
86 | if conf.freq3 == None: conf.freq0 = 0 | |
|
87 | ||
|
88 | ||
|
89 | data = {'f0': str(conf.freq0), 'f1': str(conf.freq1), | |
|
90 | 'f2': str(conf.freq2), 'f3': str(conf.freq3)} | |
|
91 | json_data = json.dumps(data) | |
|
92 | conf.parameters = json_data | |
|
93 | ||
|
76 | 94 | conf.save() |
|
77 | 95 | return redirect('url_cgs_conf', id_conf=conf.id) |
|
78 | 96 | |
|
79 | 97 | ##ERRORS |
|
80 | 98 | |
|
81 | 99 | kwargs = {} |
|
82 | 100 | kwargs['id_dev'] = conf.id |
|
83 | 101 | kwargs['form'] = form |
|
84 | 102 | kwargs['title'] = 'Device Configuration' |
|
85 | 103 | kwargs['suptitle'] = 'Edit' |
|
86 | 104 | kwargs['button'] = 'Save' |
|
87 | 105 | |
|
106 | ||
|
88 | 107 | ###### SIDEBAR ###### |
|
89 | 108 | kwargs.update(sidebar(conf)) |
|
90 | 109 | |
|
91 | 110 | return render(request, 'cgs_conf_edit.html', kwargs) |
|
92 | 111 | |
|
93 | 112 | def cgs_conf_write(request, id_conf): |
|
94 | 113 | |
|
95 | 114 | conf = get_object_or_404(CGSConfiguration, pk=id_conf) |
|
96 | 115 | ip=conf.device.ip_address |
|
97 | 116 | port=conf.device.port_address |
|
98 | 117 | |
|
99 | 118 | #Frequencies from form |
|
119 | if conf.freq0 == None: conf.freq0 = 0 | |
|
120 | if conf.freq1 == None: conf.freq0 = 0 | |
|
121 | if conf.freq2 == None: conf.freq0 = 0 | |
|
122 | if conf.freq3 == None: conf.freq0 = 0 | |
|
100 | 123 | f0 = int(conf.freq0) |
|
101 | 124 | f1 = int(conf.freq1) |
|
102 | 125 | f2 = int(conf.freq2) |
|
103 | 126 | f3 = int(conf.freq3) |
|
127 | ||
|
128 | ||
|
104 | 129 | post_data = {"f0":f0, "f1":f1, "f2":f2, "f3":f3} |
|
105 | 130 | route = "http://" + str(ip) + ":" + str(port) + "/frequencies/" |
|
106 | 131 | r = requests.post(route, post_data) |
|
107 | 132 | text = r.text |
|
108 | 133 | text = text.split(',') |
|
109 | 134 | |
|
110 | 135 | try: |
|
111 |
|
|
|
112 |
|
|
|
113 |
status |
|
|
114 | if status_ok == 200 or title == "okay": | |
|
115 | messages.success(request, status) | |
|
136 | if len(text)>1: | |
|
137 | title = text[0] | |
|
138 | status = text[1] | |
|
139 | status_ok = r.status_code | |
|
140 | if title == "okay": | |
|
141 | messages.success(request, status) | |
|
142 | else: | |
|
143 | messages.error(request, status) | |
|
144 | ||
|
116 | 145 | else: |
|
117 | messages.error(request, status) | |
|
146 | title = text[0] | |
|
147 | messages.error(request, title) | |
|
148 | ||
|
118 | 149 | except: |
|
119 | 150 | messages.error(request, "An hardware error was found") |
|
120 | 151 | |
|
121 | 152 | |
|
122 | 153 | |
|
123 | 154 | |
|
124 | 155 | return redirect('url_cgs_conf', id_conf=conf.id) |
|
125 | 156 | |
|
126 | 157 | def cgs_conf_read(request, id_conf): |
|
127 | 158 | |
|
128 | 159 | conf = get_object_or_404(CGSConfiguration, pk=id_conf) |
|
160 | ||
|
129 | 161 | ip=conf.device.ip_address |
|
130 | 162 | port=conf.device.port_address |
|
131 | 163 | |
|
132 | 164 | if request.method=='POST': |
|
133 | 165 | form = CGSConfigurationForm(request.POST, instance=conf) |
|
134 | 166 | |
|
135 | 167 | if form.is_valid(): |
|
136 | 168 | cgs_model = form.save(commit=False) |
|
137 | 169 | |
|
138 | 170 | if cgs_model.verify_frequencies(): |
|
139 | 171 | |
|
140 | 172 | cgs_model.save() |
|
141 | 173 | return redirect('url_cgs_conf', id_conf=conf.id) |
|
142 | 174 | |
|
143 | 175 | messages.error(request, "Parameters could not be saved. Invalid parameters") |
|
144 | 176 | |
|
145 | 177 | data = {} |
|
146 | 178 | |
|
147 | 179 | |
|
148 | 180 | if request.method=='GET': |
|
149 | 181 | #r: response = icon, status |
|
150 | 182 | route = "http://" + str(ip) + ":" + str(port) + "/status/ad9548" |
|
151 | r = requests.get(route) | |
|
152 | response = str(r.text) | |
|
153 |
response = |
|
|
154 |
|
|
|
155 |
|
|
|
156 | print r.text | |
|
157 | #"icon" could be: "alert" or "okay" | |
|
158 | if "okay" in icon: | |
|
159 | messages.success(request, status) | |
|
160 | else: | |
|
161 | messages.error(request, status) | |
|
162 | #Get Frequencies | |
|
163 | route = "http://" + str(ip) + ":" + str(port) + "/frequencies/" | |
|
164 | #frequencies = requests.get('http://10.10.10.175:8080/frequencies/') | |
|
165 |
frequencies = requests.get( |
|
|
166 |
frequencies = |
|
|
167 |
frequencies = frequencies. |
|
|
168 |
|
|
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
|
172 | print f0,f1,f2,f3 | |
|
173 | ||
|
183 | try: | |
|
184 | r = requests.get(route) | |
|
185 | response = str(r.text) | |
|
186 | response = response.split(";") | |
|
187 | icon = response[0] | |
|
188 | status = response[-1] | |
|
189 | print r.text | |
|
190 | #"icon" could be: "alert" or "okay" | |
|
191 | if "okay" in icon: | |
|
192 | messages.success(request, status) | |
|
193 | else: | |
|
194 | messages.error(request, status) | |
|
195 | #Get Frequencies | |
|
196 | route = "http://" + str(ip) + ":" + str(port) + "/frequencies/" | |
|
197 | #frequencies = requests.get('http://10.10.10.175:8080/frequencies/') | |
|
198 | frequencies = requests.get(route) | |
|
199 | frequencies = frequencies.json() | |
|
200 | frequencies = frequencies.get("Frecuencias") | |
|
201 | f0 = frequencies.get("f0") | |
|
202 | f1 = frequencies.get("f1") | |
|
203 | f2 = frequencies.get("f2") | |
|
204 | f3 = frequencies.get("f3") | |
|
205 | print f0,f1,f2,f3 | |
|
174 | 206 | |
|
175 | if not response: | |
|
176 | messages.error(request, "Could not read parameters from Device") | |
|
177 | return redirect('url_cgs_conf', id_conf=conf.id) | |
|
178 | ||
|
179 | data = {'experiment' : conf.experiment.id, | |
|
180 |
|
|
|
181 |
' |
|
|
182 |
'freq |
|
|
183 |
'freq |
|
|
184 |
'freq |
|
|
185 |
|
|
|
207 | ||
|
208 | if not response: | |
|
209 | messages.error(request, "Could not read parameters from Device") | |
|
210 | return redirect('url_cgs_conf', id_conf=conf.id) | |
|
211 | ||
|
212 | data = {'experiment' : conf.experiment.id, | |
|
213 | 'device' : conf.device.id, | |
|
214 | 'freq0' : f0, | |
|
215 | 'freq1' : f1, | |
|
216 | 'freq2' : f2, | |
|
217 | 'freq3' : f3, | |
|
218 | } | |
|
219 | except: | |
|
220 | messages.error(request, "Could not read parameters from Device") | |
|
221 | data = {'experiment' : conf.experiment.id, | |
|
222 | 'device' : conf.device.id, | |
|
223 | 'freq0' : None, | |
|
224 | 'freq1' : None, | |
|
225 | 'freq2' : None, | |
|
226 | 'freq3' : None, | |
|
227 | } | |
|
186 | 228 | |
|
187 | 229 | form = CGSConfigurationForm(initial = data) |
|
188 | 230 | |
|
189 | 231 | kwargs = {} |
|
190 | 232 | kwargs['id_dev'] = conf.id |
|
191 | 233 | kwargs['form'] = form |
|
192 | 234 | kwargs['title'] = 'Device Configuration' |
|
193 | 235 | kwargs['suptitle'] = 'Parameters read from device' |
|
194 | 236 | kwargs['button'] = 'Save' |
|
195 | 237 | |
|
196 | 238 | ###### SIDEBAR ###### |
|
197 | 239 | kwargs.update(sidebar(conf)) |
|
198 | 240 | |
|
199 | 241 | return render(request, 'cgs_conf_edit.html', kwargs) |
|
200 | 242 | |
|
201 |
|
|
|
243 | def cgs_conf_export(request, id_conf): | |
|
202 | 244 | |
|
203 |
|
|
|
204 |
|
|
|
205 |
|
|
|
245 | conf = get_object_or_404(CGSConfiguration, pk=id_conf) | |
|
246 | ip=conf.device.ip_address | |
|
247 | port=conf.device.port_address | |
|
206 | 248 | |
|
207 | # return render(request, 'cgs_conf_export.html', kwargs) No newline at end of file | |
|
249 | if request.method=='POST': | |
|
250 | return HttpResponse(conf.parameters, content_type="application/json") | |
|
251 | ||
|
252 | return render(request, 'cgs_conf.html') | |
|
253 | #return redirect(conf.get_absolute_url()) | |
|
254 |
General Comments 0
You need to be logged in to leave comments.
Login now