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