@@ -1,245 +1,245 | |||
|
1 | 1 | import ast |
|
2 | 2 | import json |
|
3 | 3 | import requests |
|
4 | 4 | import base64 |
|
5 | 5 | import struct |
|
6 | 6 | from struct import pack |
|
7 | 7 | import time |
|
8 | 8 | from django.contrib import messages |
|
9 | 9 | from django.db import models |
|
10 | 10 | from django.urls import reverse |
|
11 | 11 | from django.core.validators import MinValueValidator, MaxValueValidator |
|
12 | 12 | |
|
13 | 13 | from apps.main.models import Configuration |
|
14 | 14 | |
|
15 | 15 | MODE_VALUE = ( |
|
16 | 16 | ('position', 'Position'), |
|
17 | 17 | ('speed', 'Speed'), |
|
18 | 18 | ('table', 'Table') |
|
19 | 19 | ) |
|
20 | 20 | |
|
21 | 21 | class PedestalConfiguration(Configuration): |
|
22 | 22 | |
|
23 | 23 | mode = models.CharField( |
|
24 | 24 | verbose_name='Mode', |
|
25 | 25 | max_length=10, |
|
26 | 26 | choices=MODE_VALUE, |
|
27 | 27 | null=False, |
|
28 | 28 | blank=False |
|
29 | 29 | ) |
|
30 | 30 | |
|
31 | 31 | axis = models.CharField( |
|
32 | 32 | verbose_name="Axis", |
|
33 | 33 | max_length=100, |
|
34 | 34 | default='az', |
|
35 | 35 | blank=False, |
|
36 | 36 | null=False, |
|
37 | 37 | help_text="Please separate the values with commas when using table mode" |
|
38 | 38 | ) |
|
39 | 39 | |
|
40 | 40 | speed = models.CharField( |
|
41 | 41 | verbose_name='Speed', |
|
42 | 42 | max_length=100, |
|
43 | 43 | blank=False, |
|
44 | 44 | null=False, |
|
45 | 45 | default=6 |
|
46 | 46 | ) |
|
47 | 47 | |
|
48 | 48 | angle = models.CharField( |
|
49 | 49 | verbose_name="Angle(s)", |
|
50 | 50 | max_length=100, |
|
51 | 51 | default='0', |
|
52 | 52 | blank=False, |
|
53 | 53 | null=False, |
|
54 | 54 | help_text="Please separate the values with commas when using table mode" |
|
55 | 55 | ) |
|
56 | 56 | |
|
57 | 57 | min_value = models.FloatField( |
|
58 | 58 | verbose_name='Min angle', |
|
59 | 59 | validators=[MinValueValidator(-5), MaxValueValidator(185)], |
|
60 | 60 | blank=False, |
|
61 | 61 | null=False, |
|
62 | 62 | default=0 |
|
63 | 63 | ) |
|
64 | 64 | |
|
65 | 65 | max_value = models.FloatField( |
|
66 | 66 | verbose_name='Max angle', |
|
67 | 67 | validators=[MinValueValidator(-5), MaxValueValidator(185)], |
|
68 | 68 | blank=False, |
|
69 | 69 | null=False, |
|
70 | 70 | default=40 |
|
71 | 71 | ) |
|
72 | 72 | |
|
73 | 73 | class Meta: |
|
74 | 74 | db_table = 'pedestal_configurations' |
|
75 | 75 | |
|
76 | 76 | def __str__(self): |
|
77 | 77 | if self.mode=='position': |
|
78 | 78 | return u'Position: {}ΒΊ {}'.format(self.angle, self.axis.upper()) |
|
79 | 79 | if self.mode=='speed': |
|
80 | 80 | return u'Speed: {}ΒΊ/s {}'.format(self.speed, self.axis.upper()) |
|
81 | 81 | if self.mode=='table': |
|
82 | 82 | axis = [x.strip().upper() for x in self.axis.split(',')] |
|
83 |
speeds = [ |
|
|
84 |
table = [ |
|
|
83 | speeds = [float(x.strip()) for x in self.speed.split(',')] | |
|
84 | table = [float(x.strip()) for x in self.angle.split(',')] | |
|
85 | 85 | return u'Table: Axis {}, Speed {}ΒΊ/s, Steps {}'.format(axis, speeds, table) |
|
86 | 86 | |
|
87 | 87 | @property |
|
88 | 88 | def label(self): |
|
89 | 89 | return str(self) |
|
90 | 90 | |
|
91 | 91 | def get_absolute_url_plot(self): |
|
92 | 92 | return reverse('url_plot_pedestal_pulses', args=[str(self.id)]) |
|
93 | 93 | |
|
94 | 94 | def request(self, cmd, method='get', **kwargs): |
|
95 | 95 | |
|
96 | 96 | req = getattr(requests, method)(self.device.url(cmd), **kwargs) |
|
97 | 97 | payload = req.json() |
|
98 | 98 | |
|
99 | 99 | return payload |
|
100 | 100 | |
|
101 | 101 | def status_device(self): |
|
102 | 102 | |
|
103 | 103 | try: |
|
104 | 104 | payload = requests.get(self.device.url()) |
|
105 | 105 | |
|
106 | 106 | if payload: |
|
107 | 107 | self.device.status = 1 |
|
108 | 108 | elif payload['status']=='disable': |
|
109 | 109 | self.device.status = 2 |
|
110 | 110 | else: |
|
111 | 111 | self.device.status = 1 |
|
112 | 112 | self.device.save() |
|
113 | 113 | self.message = 'Pedestal status: {}'.format(payload['status']) |
|
114 | 114 | return False |
|
115 | 115 | except Exception as e: |
|
116 | 116 | if 'No route to host' not in str(e): |
|
117 | 117 | self.device.status = 4 |
|
118 | 118 | self.device.save() |
|
119 | 119 | self.message = 'Pedestal status: {}'.format(str(e)) |
|
120 | 120 | return False |
|
121 | 121 | |
|
122 | 122 | self.device.save() |
|
123 | 123 | return True |
|
124 | 124 | |
|
125 | 125 | def reset_device(self): |
|
126 | 126 | |
|
127 | 127 | try: |
|
128 |
url = self.device.url() + " |
|
|
128 | url = self.device.url() + "position?params=" | |
|
129 | 129 | |
|
130 | 130 | payload_el = {'axis': 'elevation', 'position': 0} |
|
131 | json_data_el = json.dumps(payload) | |
|
132 | base64_table_el = base64.standard_b64encode(json_data.encode('ascii')) | |
|
131 | json_data_el = json.dumps(payload_el) | |
|
132 | base64_table_el = base64.standard_b64encode(json_data_el.encode('ascii')) | |
|
133 | 133 | |
|
134 | 134 | r = requests.get(url + base64_table_el.decode('ascii')) |
|
135 | 135 | |
|
136 | 136 | payload_az = {'axis': 'azimuth', 'position': 0} |
|
137 | json_data_az = json.dumps(payload) | |
|
138 | base64_table_az = base64.standard_b64encode(json_data.encode('ascii')) | |
|
137 | json_data_az = json.dumps(payload_az) | |
|
138 | base64_table_az = base64.standard_b64encode(json_data_az.encode('ascii')) | |
|
139 | 139 | |
|
140 | 140 | r = requests.get(url + base64_table_az.decode('ascii')) |
|
141 | 141 | |
|
142 | 142 | if r: |
|
143 | 143 | self.device.status = 3 |
|
144 | 144 | self.device.save() |
|
145 | 145 | self.message = 'Pedestal reset' |
|
146 | 146 | else: |
|
147 | 147 | return False |
|
148 | 148 | |
|
149 | 149 | except Exception as e: |
|
150 | 150 | self.message = 'Pedestal reset: {}'.format(str(e)) |
|
151 | 151 | return False |
|
152 | 152 | |
|
153 | 153 | return True |
|
154 | 154 | |
|
155 | 155 | def stop_device(self): |
|
156 | 156 | |
|
157 | 157 | try: |
|
158 | 158 | command = self.device.url() + "stop" |
|
159 | 159 | r = requests.get(command) |
|
160 | 160 | if r: |
|
161 | 161 | self.device.status = 4 |
|
162 | 162 | self.device.save() |
|
163 | 163 | self.message = 'Pedestal stopped' |
|
164 | 164 | else: |
|
165 | 165 | self.device.status = 4 |
|
166 | 166 | self.device.save() |
|
167 | 167 | return False |
|
168 | 168 | except Exception as e: |
|
169 | 169 | if 'No route to host' not in str(e): |
|
170 | 170 | self.device.status = 4 |
|
171 | 171 | else: |
|
172 | 172 | self.device.status = 0 |
|
173 | 173 | #self.message = 'Pedestal stop: {}'.format(str(e)) |
|
174 | 174 | self.message = "Pedestal can't start, please check network/device connection or IP address/port configuration" |
|
175 | 175 | self.device.save() |
|
176 | 176 | return False |
|
177 | 177 | |
|
178 | 178 | return True |
|
179 | 179 | |
|
180 | 180 | def start_device(self): |
|
181 | 181 | |
|
182 | self.reset_device() | |
|
183 | ||
|
184 | 182 | AX = {'az':'azimuth', 'el':'elevation'} |
|
185 | 183 | axis = [AX[x.lower().strip()] for x in self.axis.split(',')] |
|
186 | 184 | if len(axis)==1: |
|
187 | 185 | axis = axis[0] |
|
188 | 186 | |
|
189 | 187 | try: |
|
190 | 188 | if self.mode == 'position': |
|
191 |
url = self.device.url() + " |
|
|
189 | url = self.device.url() + "position?params=" | |
|
192 | 190 | payload = {'axis': axis, 'position': float(self.angle)} |
|
193 | 191 | elif self.mode == 'speed': |
|
194 |
url = self.device.url() + " |
|
|
192 | url = self.device.url() + "speed?params=" | |
|
195 | 193 | payload = {'axis': axis, 'speed': float(self.speed)} |
|
196 | 194 | elif self.mode == 'table': |
|
195 | self.reset_device() | |
|
197 | 196 | url = self.device.url() + "combinedtable?params=" |
|
198 | 197 | list_of_floats = [float(x.strip()) for x in self.angle.split(",")] |
|
199 | 198 | byte_table = [] |
|
200 | 199 | for x in list_of_floats: |
|
201 | 200 | temp = bytearray(struct.pack("f", x)) |
|
202 | 201 | byte_table.append(temp[3]) |
|
203 | 202 | byte_table.append(temp[2]) |
|
204 | 203 | byte_table.append(temp[1]) |
|
205 | 204 | byte_table.append(temp[0]) |
|
206 | 205 | |
|
207 | 206 | coded_table = base64.standard_b64encode(bytes(byte_table)) |
|
208 | 207 | coded_table_ascii = coded_table.decode('ascii') |
|
209 | 208 | speed = [float(x.strip()) for x in self.speed.split(',')] |
|
210 | 209 | payload = { |
|
211 | 210 | 'arraylength': len(speed), |
|
212 | 211 | 'axis': axis, |
|
213 | 212 | 'speed': speed, |
|
214 |
' |
|
|
213 | 'bottom': self.min_value, | |
|
215 | 214 | 'top': self.max_value, |
|
216 |
' |
|
|
215 | 'table': coded_table_ascii | |
|
217 | 216 | } |
|
217 | time.sleep(15) | |
|
218 | 218 | |
|
219 | 219 | json_data = json.dumps(payload) |
|
220 | 220 | print(json_data) |
|
221 | 221 | base64_table = base64.standard_b64encode(json_data.encode('ascii')) |
|
222 | 222 | url += base64_table.decode('ascii') |
|
223 | 223 | print(url) |
|
224 | 224 | r = requests.get(url) |
|
225 | 225 | |
|
226 | 226 | if r: |
|
227 | 227 | self.device.status = 3 |
|
228 | 228 | self.device.save() |
|
229 | 229 | self.message = 'Pedestal configured and started' |
|
230 | 230 | else: |
|
231 | 231 | return False |
|
232 | 232 | except Exception as e: |
|
233 | 233 | if 'No route to host' not in str(e): |
|
234 | 234 | self.device.status = 4 |
|
235 | 235 | else: |
|
236 | 236 | self.device.status = 0 |
|
237 | 237 | #self.message = 'Pedestal start: {}'.format(str(e)) |
|
238 | 238 | self.message = "Pedestal can't start, please check network/device connection or IP address/port configuration" |
|
239 | 239 | self.device.save() |
|
240 | 240 | return False |
|
241 | 241 | |
|
242 | 242 | return True |
|
243 | 243 | |
|
244 | 244 | def get_absolute_url_import(self): |
|
245 | 245 | return reverse('url_import_pedestal_conf', args=[str(self.id)]) |
General Comments 0
You need to be logged in to leave comments.
Login now