-
Notifications
You must be signed in to change notification settings - Fork 0
/
demon
executable file
·317 lines (266 loc) · 11.2 KB
/
demon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python3
import sys
import json
import threading
import socket
import requests
import zlib
import glob
import os
from datetime import datetime, timedelta
try:
from flask import Flask, request, jsonify
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QPlainTextEdit
except ModuleNotFoundError as ex:
print('DE-Mon requires Python 3.6 or later with the modules PyQT6 and Flask')
print('Run the following command to install the dependencies:')
print('pip install PyQt5 flask requests')
print('\n\n')
print('If you don\'t have pip installed run:')
print('pip install --user --upgrade pip')
exit(0)
class DataProcessor:
def start_time(self, timestamp):
self._dt_start_time = datetime.strptime(timestamp[0], '%Y-%m-%d %H:%M:%S.%f')
def calculate_datetime_from_offset(self, time_offset):
return self._dt_start_time + timedelta(seconds=float(time_offset))
def sensor_capture(self, sensor_data):
self._data['capture'].append({
'time': datetime.strftime(self.calculate_datetime_from_offset(sensor_data[0]), '%H:%M:%S %d-%m-%Y'),
'hfr': round(float(sensor_data[3]), 2),
'ecc': round(float(sensor_data[7]), 2)
})
def sensor_mount(self, sensor_data):
self._data['mount'].append({
'time': datetime.strftime(self.calculate_datetime_from_offset(sensor_data[0]), '%H:%M:%S %d-%m-%Y'),
'ra': round(float(sensor_data[1]), 2),
'dec': round(float(sensor_data[2]), 2),
'azi': round(float(sensor_data[3]), 2),
'alt': round(float(sensor_data[4]), 2),
'pier': 'W' if sensor_data[5] == 0 else 'E'
})
if len(self._data['mount']) > 1:
self._data['mount'].pop(0)
def sensor_guiding(self, sensor_data):
ra_err = round(float(sensor_data[1]), 2)
dec_err = round(float(sensor_data[2]), 2)
total_err = round(pow(pow(ra_err, 2) + pow(dec_err, 2), 0.5), 2)
self._data['guiding'].append({
'time': datetime.strftime(self.calculate_datetime_from_offset(sensor_data[0]), '%H:%M:%S %d-%m-%Y'),
'ra_err': ra_err,
'dec_err': dec_err,
'total_err': total_err
})
if len(self._data['guiding']) > 200:
self._data['guiding'].pop(0)
def sensor_temperature(self, sensor_data):
self._data['temperature'].append({
'time': datetime.strftime(self.calculate_datetime_from_offset(sensor_data[0]), '%H:%M:%S %d-%m-%Y'),
'value': round(float(sensor_data[1]), 1)
})
def sensor_humidity(self, sensor_data):
self._data['humidity'].append({
'time': datetime.strftime(self.calculate_datetime_from_offset(sensor_data[0]), '%H:%M:%S %d-%m-%Y'),
'value': round(float(sensor_data[1]), 1)
})
def parse_sensor_line(self, sensor_line):
line_array = sensor_line.split(',')
data_type = line_array[0]
if data_type in self._data_types.keys():
self._data_types[data_type](line_array[1:])
def parse_file(self, filename=''):
analyze_file = self.get_latest_analyze_file() if filename == '' else filename
if not analyze_file:
self.logger('No ekos .analyze file(s) found.')
return
self.logger(f"Parsing {analyze_file}")
self.logger(f'Session started at: {self._dt_start_time}')
file = open(analyze_file, 'r')
while True:
line = file.readline()
if not line:
break
self.parse_sensor_line(line.replace('\n', ''))
for sensor in self._data:
sensor_data = self._data[sensor][-1] if len(self._data[sensor]) > 0 else None
if sensor_data is not None:
self.logger(f'{sensor}: {self._data[sensor][-1]}')
def get_data(self):
return self._data
def set_logger(self, logger):
self.logger = logger
def set_analyze_base_path(self, analyze_base_path):
self.analyze_base_path = analyze_base_path
def reset_data(self):
self._data = {
'capture': [],
'mount': [],
'guiding': [],
'temperature': [],
'humidity': []
}
def update_remote_data(self, token):
with open('demon.json', 'w') as output_file:
json.dump(self.get_data(), output_file)
try:
request_body = zlib.compress(json.dumps(self.get_data()).encode('utf-8'))
headers = {
'Content-Encoding': 'gzip'
}
r = requests.post(f'https://demon.astropills.it?update&stk={token}', data=request_body, headers=headers)
return r.status_code == 200
except requests.exceptions.RequestException as e:
print(f"Something went wrong while uploading the data:{str(e)}")
return False
def get_latest_analyze_file(self):
list_of_files = glob.glob(f'{self.analyze_base_path}*.analyze')
try:
latest_file = max(list_of_files, key=os.path.getctime)
except ValueError: # max raises an exception if the iterable is empty
latest_file = None
return latest_file
def __init__(self):
self._data_types = {
'AnalyzeStartTime': self.start_time,
'Temperature': self.sensor_temperature,
'Humidity': self.sensor_humidity,
'CaptureComplete': self.sensor_capture,
'MountCoords': self.sensor_mount,
'GuideStats': self.sensor_guiding
}
self._data = {
'capture': [],
'mount': [],
'guiding': [],
'temperature': [],
'humidity': []
}
self._dt_start_time = datetime.now()
self.analyze_base_path = '/home/astroberry/.local/share/kstars/analyze'
self.logger = print
class DEMonWidget(QtWidgets.QWidget):
def __init__(self, data_processor):
super().__init__()
self.token_input = QtWidgets.QLineEdit()
self.token_input.setPlaceholderText('Enter your DE-Mon token here...')
self.analyze_files_path = QtWidgets.QLineEdit()
self.analyze_files_path.setPlaceholderText(
'Path to analyze files folder, if blank will use the default: /home/astroberry/.local/share/kstars/analyze')
self.button = QtWidgets.QPushButton("Start monitoring")
self.clear_button = QtWidgets.QPushButton("Clear logs")
self.output_log = QPlainTextEdit(self)
self.layout = QtWidgets.QVBoxLayout(self)
self.layout.addWidget(self.token_input)
self.layout.addWidget(self.analyze_files_path)
self.layout.addWidget(self.button)
self.layout.addWidget(self.clear_button)
self.layout.addWidget(self.output_log)
self.button.clicked.connect(self.start_monitoring)
self.clear_button.clicked.connect(self.output_log.clear)
self.data_processor = data_processor
self.data_processor.set_logger(self.__print_log)
self.monitor_timer = QTimer()
self.monitor_timer.setInterval(15000)
self.monitor_timer.timeout.connect(self.monitor)
def closeEvent(self, event):
try:
os.remove('demon.json')
except FileNotFoundError:
print('Local data does not exist, nothing to remove')
self.monitor_timer.stop()
def get_ip(self):
host_name = socket.gethostname()
ip = socket.gethostbyname(host_name)
return ip
@QtCore.pyqtSlot()
def start_monitoring(self):
token = self.token_input.text().strip()
self.token_input.setText(token)
if token == '':
self.__print_log("Please enter your DE-Mon token before starting to monitor!")
return
analyze_files_path = self.analyze_files_path.text().strip()
if analyze_files_path != '':
self.data_processor.set_analyze_base_path(self.analyze_files_path.text())
self.monitor_timer.start()
self.button.setText("Stop monitoring")
self.button.clicked.disconnect()
self.button.clicked.connect(self.stop_monitoring)
self.__print_log("Monitoring started...")
self.__print_log(
f"Open your browser and go to <a href='https://demon.astropills.it/?stk={token}'>https://demon.astropills.it/?stk={token}</a> to get access to your DE-Mon dashboard.")
self.__print_log(
f"A local server is also running on <a href='{self.get_ip()}:5000'>{self.get_ip()}:5000</a> for development purposes.")
@QtCore.pyqtSlot()
def stop_monitoring(self):
self.monitor_timer.stop()
self.button.setText("Start monitoring")
self.button.clicked.disconnect()
self.button.clicked.connect(self.start_monitoring)
self.__print_log("Monitoring stopped")
def __get_timestamp(self):
return datetime.now().strftime('%d/%m/%Y %H:%M:%S')
def __print_log(self, text):
self.output_log.appendHtml(f'<b>{self.__get_timestamp()}</b> - {text}')
def monitor(self):
try:
self.data_processor.parse_file()
data_updated = self.data_processor.update_remote_data(self.token_input.text())
self.data_processor.reset_data()
if data_updated:
self.__print_log('Ekos data parsed and uploaded correctly!')
else:
self.__print_log(
'Could not update the data! Did you provide a valid token? Do you have a *.analyze file available?'
)
except Exception as e:
print(f"Something went wrong:{str(e)}")
self.__print_log(f"Something went wrong:{str(e)}")
def start_server():
server = Flask(__name__)
kwargs = {'host': '0.0.0.0', 'port': 5000, 'threaded': True, 'use_reloader': False, 'debug': False}
threading.Thread(target=server.run, daemon=True, kwargs=kwargs).start()
def read_data_file():
try:
with open('demon.json', 'r') as f:
data = json.load(f)
return data
except FileNotFoundError:
return None
def error_route():
return "<h1>Monitoring not running!</h1>"
@server.route('/', methods=['GET'])
def dashboard():
data = read_data_file()
if data is None:
return error_route()
response = {}
for sensor in data:
response[sensor] = data[sensor][-1] if len(data[sensor]) > 0 else {}
return json.dumps(response)
@server.route('/all', methods=['GET'])
def all_data():
data = read_data_file()
if data is None:
return error_route()
return json.dumps(data)
@server.route('/sensor', methods=['GET'])
def sensor_data():
data = read_data_file()
if data is None:
return error_route()
if 'id' in request.args:
sensor_id = str(request.args['id'])
if sensor_id in data:
return json.dumps({sensor_id: data[sensor_id]})
if __name__ == '__main__':
app = QtWidgets.QApplication([])
app.setApplicationName('DE-Mon')
start_server()
dp = DataProcessor()
widget = DEMonWidget(dp)
widget.resize(800, 600)
widget.show()
sys.exit(app.exec())