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