@@ -1,263 +1,257 | |||
|
1 | 1 | from django.db import models |
|
2 | 2 | from apps.main.models import Configuration |
|
3 | 3 | from django.core.validators import MinValueValidator, MaxValueValidator |
|
4 | 4 | |
|
5 | 5 | from .files import read_json_file |
|
6 | 6 | import requests |
|
7 | 7 | # Create your models here. validators=[MinValueValidator(62.5e6), MaxValueValidator(450e6)] |
|
8 | 8 | |
|
9 | 9 | class CGSConfiguration(Configuration): |
|
10 | 10 | |
|
11 | 11 | freq0 = models.PositiveIntegerField(verbose_name='Frequency 0 (Hz)',validators=[MaxValueValidator(450e6)], default = 60) |
|
12 | 12 | freq1 = models.PositiveIntegerField(verbose_name='Frequency 1 (Hz)',validators=[MaxValueValidator(450e6)], default = 60) |
|
13 | 13 | freq2 = models.PositiveIntegerField(verbose_name='Frequency 2 (Hz)',validators=[MaxValueValidator(450e6)], default = 60) |
|
14 | 14 | freq3 = models.PositiveIntegerField(verbose_name='Frequency 3 (Hz)',validators=[MaxValueValidator(450e6)], default = 60) |
|
15 | 15 | |
|
16 | 16 | def verify_frequencies(self): |
|
17 | 17 | |
|
18 | 18 | return True |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | def update_from_file(self, fp): |
|
22 | 22 | |
|
23 | 23 | kwargs = read_json_file(fp) |
|
24 | 24 | |
|
25 | 25 | if not kwargs: |
|
26 | 26 | return False |
|
27 | 27 | |
|
28 | 28 | self.freq0 = kwargs['freq0'] |
|
29 | 29 | self.freq1 = kwargs['freq1'] |
|
30 | 30 | self.freq2 = kwargs['freq2'] |
|
31 | 31 | self.freq3 = kwargs['freq3'] |
|
32 | 32 | |
|
33 | 33 | return True |
|
34 | 34 | |
|
35 | 35 | def parms_to_dict(self): |
|
36 | 36 | |
|
37 | 37 | parameters = {} |
|
38 | 38 | |
|
39 | 39 | parameters['device_id'] = self.device.id |
|
40 | 40 | parameters['device_type'] = self.device.device_type.name |
|
41 | 41 | |
|
42 | 42 | if self.freq0 == None or self.freq0 == '': |
|
43 | 43 | parameters['freq0'] = 0 |
|
44 | 44 | else: |
|
45 | 45 | parameters['freq0'] = self.freq0 |
|
46 | 46 | |
|
47 | 47 | if self.freq1 == None or self.freq1 == '': |
|
48 | 48 | parameters['freq1'] = 0 |
|
49 | 49 | else: |
|
50 | 50 | parameters['freq1'] = self.freq1 |
|
51 | 51 | |
|
52 | 52 | if self.freq2 == None or self.freq2 == '': |
|
53 | 53 | parameters['freq2'] = 0 |
|
54 | 54 | else: |
|
55 | 55 | parameters['freq2'] = self.freq2 |
|
56 | 56 | |
|
57 | 57 | if self.freq3 == None or self.freq3 == '': |
|
58 | 58 | parameters['freq3'] = 0 |
|
59 | 59 | else: |
|
60 | 60 | parameters['freq3'] = self.freq3 |
|
61 | 61 | |
|
62 | 62 | return parameters |
|
63 | 63 | |
|
64 | 64 | |
|
65 | 65 | def dict_to_parms(self, parameters): |
|
66 | 66 | |
|
67 | 67 | self.freq0 = parameters['freq0'] |
|
68 | 68 | self.freq1 = parameters['freq1'] |
|
69 | 69 | self.freq2 = parameters['freq2'] |
|
70 | 70 | self.freq3 = parameters['freq3'] |
|
71 | 71 | |
|
72 | 72 | |
|
73 | 73 | def status_device(self): |
|
74 | 74 | |
|
75 | 75 | import requests |
|
76 | 76 | |
|
77 | 77 | ip=self.device.ip_address |
|
78 | 78 | port=self.device.port_address |
|
79 | 79 | |
|
80 | 80 | route = "http://" + str(ip) + ":" + str(port) + "/status/ad9548" |
|
81 | 81 | try: |
|
82 | 82 | r = requests.get(route, timeout=0.8) |
|
83 | 83 | except Exception as e: |
|
84 | 84 | self.device.status = 0 |
|
85 | 85 | self.device.save() |
|
86 | 86 | self.message = 'Could not read CGS status: ' + str(e) |
|
87 | 87 | return False |
|
88 | 88 | |
|
89 | 89 | response = str(r.text) |
|
90 | 90 | response = response.split(";") |
|
91 | 91 | icon = response[0] |
|
92 | 92 | status = response[-1] |
|
93 | 93 | |
|
94 | 94 | #"icon" could be: "alert" or "okay" |
|
95 | 95 | if "alert" in icon: |
|
96 | 96 | self.device.status = 1 |
|
97 | 97 | self.device.save() |
|
98 | 98 | self.message = status |
|
99 | 99 | return False |
|
100 | 100 | elif "okay" in icon: |
|
101 | 101 | self.device.status = 3 |
|
102 | 102 | else: |
|
103 | 103 | self.device.status = 1 |
|
104 | 104 | |
|
105 | 105 | self.message = status |
|
106 | 106 | self.device.save() |
|
107 | 107 | |
|
108 | 108 | return True |
|
109 | 109 | |
|
110 | 110 | |
|
111 | 111 | def start_device(self): |
|
112 | 112 | |
|
113 | 113 | ip=self.device.ip_address |
|
114 | 114 | port=self.device.port_address |
|
115 | 115 | |
|
116 | 116 | #---Frequencies from form |
|
117 | 117 | f0 = self.freq0 |
|
118 | 118 | f1 = self.freq1 |
|
119 | 119 | f2 = self.freq2 |
|
120 | 120 | f3 = self.freq3 |
|
121 | 121 | post_data = {"f0":f0, "f1":f1, "f2":f2, "f3":f3} |
|
122 | 122 | route = "http://" + str(ip) + ":" + str(port) + "/frequencies/" |
|
123 | 123 | |
|
124 | 124 | try: |
|
125 | 125 | r = requests.post(route, post_data, timeout=0.7) |
|
126 | 126 | except: |
|
127 | 127 | self.message = "Could not start CGS device" |
|
128 | 128 | return False |
|
129 | 129 | |
|
130 | 130 | text = r.text |
|
131 | 131 | text = text.split(',') |
|
132 | 132 | |
|
133 | 133 | if len(text)>1: |
|
134 | 134 | title = text[0] |
|
135 | 135 | status = text[1] |
|
136 | 136 | if title == "okay": |
|
137 | 137 | self.message = status |
|
138 | 138 | self.device.status = 3 |
|
139 | 139 | self.device.save() |
|
140 | 140 | return True |
|
141 | 141 | else: |
|
142 | 142 | self.message = title + ", " + status |
|
143 | 143 | self.device.status = 1 |
|
144 | 144 | self.device.save() |
|
145 | 145 | return False |
|
146 | 146 | |
|
147 | 147 | return False |
|
148 | 148 | |
|
149 | 149 | |
|
150 | 150 | def stop_device(self): |
|
151 | 151 | |
|
152 | 152 | ip=self.device.ip_address |
|
153 | 153 | port=self.device.port_address |
|
154 | 154 | |
|
155 | 155 | f0 = 0 |
|
156 | 156 | f1 = 0 |
|
157 | 157 | f2 = 0 |
|
158 | 158 | f3 = 0 |
|
159 | 159 | post_data = {"f0":f0, "f1":f1, "f2":f2, "f3":f3} |
|
160 | 160 | route = "http://" + str(ip) + ":" + str(port) + "/frequencies/" |
|
161 | 161 | |
|
162 | 162 | try: |
|
163 | 163 | r = requests.post(route, post_data, timeout=0.7) |
|
164 | 164 | except: |
|
165 | 165 | self.message = "Could not stop CGS device" |
|
166 | 166 | return False |
|
167 | 167 | |
|
168 | 168 | text = r.text |
|
169 | 169 | text = text.split(',') |
|
170 | 170 | |
|
171 | 171 | if len(text)>1: |
|
172 | 172 | title = text[0] |
|
173 | 173 | status = text[1] |
|
174 | 174 | if title == "okay": |
|
175 | 175 | self.message = status |
|
176 | 176 | self.device.status = 1 |
|
177 | 177 | self.device.save() |
|
178 | 178 | self.message = "CGS device has been stopped" |
|
179 | 179 | return True |
|
180 | 180 | else: |
|
181 | 181 | self.message = title + ", " + status |
|
182 | 182 | self.device.status = 0 |
|
183 | 183 | self.device.save() |
|
184 | 184 | return False |
|
185 | 185 | |
|
186 | 186 | return False |
|
187 | 187 | |
|
188 | 188 | |
|
189 | 189 | def read_device(self): |
|
190 | 190 | |
|
191 | 191 | import requests |
|
192 | 192 | |
|
193 | 193 | ip=self.device.ip_address |
|
194 | 194 | port=self.device.port_address |
|
195 | 195 | |
|
196 | 196 | route = "http://" + str(ip) + ":" + str(port) + "/read/" |
|
197 | 197 | try: |
|
198 | 198 | frequencies = requests.get(route,timeout=0.7) |
|
199 | 199 | except: |
|
200 | 200 | self.message = "Could not read CGS parameters from this device" |
|
201 | 201 | return None |
|
202 | 202 | |
|
203 | 203 | frequencies = frequencies.json() |
|
204 | 204 | if frequencies: |
|
205 | 205 | frequencies = frequencies.get("Frequencies") |
|
206 | 206 | freq0 = frequencies.get("freq0") |
|
207 | 207 | freq1 = frequencies.get("freq1") |
|
208 | 208 | freq2 = frequencies.get("freq2") |
|
209 | 209 | freq3 = frequencies.get("freq3") |
|
210 | 210 | |
|
211 | 211 | parms = {'freq0': freq0, |
|
212 | 212 | 'freq1': freq1, |
|
213 | 213 | 'freq2': freq2, |
|
214 | 214 | 'freq3': freq3} |
|
215 | 215 | |
|
216 | 216 | self.message = "CGS parameters have been successfully read" |
|
217 | 217 | return parms |
|
218 | 218 | else: |
|
219 | 219 | self.message = "Error reading CGS parameters" |
|
220 | 220 | return None |
|
221 | 221 | |
|
222 | 222 | |
|
223 | 223 | def write_device(self): |
|
224 | 224 | |
|
225 | 225 | ip=self.device.ip_address |
|
226 | 226 | port=self.device.port_address |
|
227 | 227 | |
|
228 | 228 | #---Frequencies from form |
|
229 | f0 = self.freq0 | |
|
230 | f1 = self.freq1 | |
|
231 | f2 = self.freq2 | |
|
232 | f3 = self.freq3 | |
|
233 | post_data = {"f0":f0, "f1":f1, "f2":f2, "f3":f3} | |
|
234 | route = "http://" + str(ip) + ":" + str(port) + "/frequencies/" | |
|
229 | frequencies = self.parms_to_dict() | |
|
230 | post_data = {} | |
|
231 | for data in frequencies: | |
|
232 | if data in ['freq0','freq1','freq2','freq3']: | |
|
233 | post_data[data] = frequencies[data] | |
|
234 | ||
|
235 | route = "http://" + str(ip) + ":" + str(port) + "/write/" | |
|
235 | 236 | |
|
236 | 237 | try: |
|
237 | 238 | r = requests.post(route, post_data, timeout=0.7) |
|
238 | 239 | except: |
|
239 | 240 | self.message = "Could not write CGS parameters" |
|
241 | self.device.status = 0 | |
|
242 | self.device.save() | |
|
240 | 243 | return False |
|
241 | 244 | |
|
242 | text = r.text | |
|
243 | text = text.split(',') | |
|
244 | ||
|
245 | if len(text)>1: | |
|
246 | title = text[0] | |
|
247 | status = text[1] | |
|
248 | if title == "okay": | |
|
249 | self.message = status | |
|
250 | self.device.status = 3 | |
|
251 | self.device.save() | |
|
252 | return True | |
|
253 | else: | |
|
254 | self.message = title + ", " + status | |
|
255 | self.device.status = 1 | |
|
256 | self.device.save() | |
|
257 | return False | |
|
245 | response = r.json() | |
|
246 | self.message = response['message'] | |
|
247 | self.device.status = response['status'] | |
|
248 | self.device.save() | |
|
258 | 249 | |
|
259 | return False | |
|
250 | if self.device.status==1: | |
|
251 | return False | |
|
252 | ||
|
253 | return True | |
|
260 | 254 | |
|
261 | 255 | |
|
262 | 256 | class Meta: |
|
263 | 257 | db_table = 'cgs_configurations' |
General Comments 0
You need to be logged in to leave comments.
Login now