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