/
RTLogger.py
469 lines (409 loc) · 15.3 KB
/
RTLogger.py
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/usr/local/bin/python3
# coding: utf-8
import os
import time
import json
import sys
import traceback
import collections
from .ScriptFunctions import ScriptFunctions
from .NetworkFunctions import NetworkFunctions
from .DeviceFunctions import DeviceFunctions
from .EventActionFunctions import EventActionFunctions
from .RT_data import RT_data
from ..LoggerPlugin import LoggerPlugin
from . import RTRemote
import logging as log
log.basicConfig(level=log.INFO)
logging = log.getLogger(__name__)
try:
from .telegramBot import telegramBot
except ImportError:
logging.warning(
'Telegram for python not installed. Please install with "pip3 install python-telegram-bot"')
telegramBot = None
# logging.warning(traceback.format_exc())
defaultconfig = {
"global": {
"language": "en",
"recordLength": 500000,
"name": "RTOC-Remote",
"documentfolder": "~/.RTOC",
"webserver_port": 8050,
"globalActionsActivated": False,
"globalEventsActivated": False
},
"postgresql": {
"active": False,
"user": "postgres",
"password": "",
"host": "127.0.0.1",
"port": "5432",
"database": "postgres",
"onlyPush": True,
},
"GUI": {
"darkmode": True, # nicht richtig implementiert
"scriptWidget": True,
"deviceWidget": True,
"deviceRAWWidget": True,
"pluginsWidget": False,
"eventWidget": True,
"restoreWidgetPosition": False,
"newSignalSymbols": True,
"plotLabelsEnabled": True,
"plotGridEnabled": True,
"showEvents": True,
"grid": [
True,
True,
1.0
],
"plotLegendEnabled": False,
"blinkingIdentifier": False,
"plotRate": 8,
"plotInverted": False,
"xTimeBase": True,
"timeAxis": True,
"xRelative": True,
"systemTray": False,
"signalInactivityTimeout": 2,
"autoShowGraph": False,
"antiAliasing": True,
"openGL": True,
"useWeave": True,
"csv_profiles": {},
},
"telegram": {
"active": False,
"token": "",
"default_eventlevel": 0,
"default_permission": 'blocked', # or 'read' or 'write' or 'admin'
# "chat_ids": {},
"inlineMenu": False,
"onlyAdmin": False
},
"tcp": {
"active": False,
"port": 5050,
"password": '',
"knownHosts": {},
"remoteRefreshRate": 1,
},
"backup": {
"active": False,
"path": '~/.RTOC/backup',
"clear": False,
"autoIfFull": True,
"autoOnClose": True,
"loadOnOpen": True,
"intervall": 240,
"resample": 0,
},
}
class _Config(collections.MutableMapping, dict):
# def __init__(self, default):
# for key in default.keys():
# value = default[key]
# dict.__setitem__(self,key,value)
def __getitem__(self, key):
return dict.__getitem__(self, key)
def __setitem__(self, key, value):
dict.__setitem__(self, key, value)
with open(self['documentfolder']+"/config.json", 'w', encoding="utf-8") as fp:
json.dump(self, fp, sort_keys=False, indent=4, separators=(',', ': '))
logging.info('Config saved')
def __delitem__(self, key):
dict.__delitem__(self, key)
def __iter__(self):
return dict.__iter__(self)
def __len__(self):
return dict.__len__(self)
def __contains__(self, x):
return dict.__contains__(self, x)
# , QObject):
class RTLogger(DeviceFunctions, EventActionFunctions, ScriptFunctions, NetworkFunctions):
"""
This is the main backend-class.
It combines data-storage, tcp-server, scripting, event/action-system and the telegram-bot.
Check out the following modules for more information:
DeviceFunctions
EventActionFunctions
NetworkFunctions
ScriptFunctions
RTRemote
RT_data
telegramBot
"""
def __init__(self, enableTCP=None, tcpport=None, isGUI=False, forceLocal=False):
self.run = True
self.forceLocal = forceLocal
self.isGUI = isGUI
self.__config = _Config({})
self._load_config()
self.pluginObjects = {} # dict with all plugins
self.pluginFunctions = {}
self.pluginParameters = {}
self.pluginStatus = {}
self.starttime = time.time()
# self.maxLength = self.config['global']['recordLength']
self.__latestSignal = []
self.devicenames = {}
self.triggerExpressions = []
self.triggerValues = []
self.telegramBot = None
self.globalEvents = {}
self.activeGlobalEvents = {}
self.globalActions = {}
self.getDeviceList()
self.tcp = None
if enableTCP is not None:
self.config['tcp']['active'] = enableTCP
if tcpport is not None:
self.config['tcp']['port'] = int(tcpport)
# self.clearSignals()
self.callback = None
self.newSignalCallback = None
self.scriptExecutedCallback = None
self.handleScriptCallback = None
self.clearCallback = None
self.newEventCallback = None
self.startDeviceCallback = None
self.stopDeviceCallback = None
self.recordingLengthChangedCallback = None
self.reloadDevicesCallback = None
self.reloadDevicesRAWCallback = None
self.reloadSignalsGUICallback = None
self.executedCallback = None
self.tcpclient = LoggerPlugin(None, None, None)
self.rtoc_web = None
self.__tcpserver = None
self.database = RT_data(self)
self.toggleTcpServer(self.config['tcp']['active'])
self.remote = RTRemote.RTRemote(self)
# if telegramBot is not None:
# self.telegramBot = telegramBot(self)
self.toggleTelegramBot(self.config['telegram']['active'])
self._load_autorun_plugins()
# self.check_for_updates()
self.loadGlobalActions()
self.loadGlobalEvents()
# if self.config['backup']['active'] and not self.forceLocal: # or self.config['postgresql']['active']:
# self.database.start()
def getDir(self, dir=None):
"""
Returns directory of this module
"""
if dir is None:
dir = __file__
if getattr(sys, 'frozen', False):
# frozen
packagedir = os.path.dirname(sys.executable)+'/RTOC'
else:
# unfrozen
packagedir = os.path.dirname(os.path.realpath(dir))
return packagedir
def getThread(self):
"""
Returns the :mod:`threading.Thread` object of :meth:`._tcpListener`
"""
return self.__tcpserver
def stop(self):
"""
Stops everything (TCP-Server, Telegram-Bot, Plugins)
"""
# Stops all plugins
self.run = False
self.tcpRunning = False
if self.tcp:
self.tcp.close()
logging.info("TCPServer beendet")
self.remote.stop()
for name in self.devicenames.keys():
self.stopPlugin(name)
self.database.close()
self.save_config()
self.saveGlobalActions()
self.saveGlobalEvents()
if self.telegramBot:
self.telegramBot.stop()
# Signal functions ############################################################
def clearCB(self, database=False):
"""
GUI-callback to clear data.
"""
if self.clearCallback:
self.clearCallback()
self.clear(database)
# time.sleep(1)
def clear(self, database=False):
"""
Clear all data from local (and database)
"""
self.selectedTriggerSignals = []
self.database.clear( dev=True, sig=True, ev=True, database=database)
# Other functions #########################################################
def exportData(self, filename=None, filetype="json", overwrite=False):
"""
Export all local data to file
Args:
filename (str)
filetype ('xlsx','json','csv')
overwrite (bool): If True, existing file will be overwritten
"""
if filename is None:
filename = self._generateFilename()
if filetype == "xlsx":
self.database.exportXLSX(filename)
elif filetype == "json":
self.database.exportJSON(filename, overwrite)
else:
self.database.exportCSV(filename)
def _generateFilename(self):
minx = []
# maxx = []
for signal in self.database.signals():
minx.append(min(list(signal[2])))
minx = max(minx)
now = time.strftime("%d_%m_%y_%H_%M", time.localtime(minx))
return self.config['global']['documentfolder']+"/"+str(now)
@property
def config(self):
"""
Global configuration dict
"""
return self.__config
@config.setter
def config(self, config):
self.__config = config
logging.info('Config changed and saved!')
self.save_config()
def _load_config(self):
# self.lastEditedList = []
userpath = os.path.expanduser('~/.RTOC')
if not os.path.exists(userpath):
os.mkdir(userpath)
if not os.path.exists(userpath+'/backup'):
os.mkdir(userpath+'/backup')
if os.path.exists(userpath+"/config.json"):
try:
with open(userpath+"/config.json", encoding="UTF-8") as jsonfile:
self.__config = _Config(json.load(jsonfile, encoding="UTF-8"))
# newlist = []
# self.config['global']['documentfolder'] = userpath
# for path in self.__config["lastSessions"]:
# if os.path.exists(path):
# newlist.append(path)
# self.__config["lastSessions"] = newlist
# for lastpath in self.__config["lastSessions"]:
# self.lastEditedList.append(lastpath)
for key in defaultconfig.keys():
if key not in self.__config.keys():
self.__config[key] = defaultconfig[key]
elif type(defaultconfig[key])==dict:
for key2 in defaultconfig[key].keys():
if key2 not in self.__config[key].keys():
self.__config[key][key2] = defaultconfig[key][key2]
except Exception as error:
logging.debug(traceback.format_exc())
logging.error('Error loading config.json\n{}'.format(error))
self.__config = _Config(defaultconfig)
else:
logging.warning('No config-file found.')
self.__config = _Config(defaultconfig)
self.__config['backup']['path'] = userpath+'/backup'
self.__config['global']['documentfolder'] = userpath
if 'chat_ids' in self.__config.keys():
if type(self.__config['telegram']['chat_ids']) == list:
newdict = {}
for id in self.__config['telegram']['chat_ids']:
newdict[id] = self.__config['telegram']['eventlevel']
self.__config['telegram']['chat_ids'] = newdict
logging.warning('Telegram chat ids were saved as list, changed to dict.')
elif type(self.__config['telegram']['chat_ids']) == dict:
for id in self.__config['telegram']['chat_ids'].keys():
if type(self.__config['telegram']['chat_ids'][id]) == int:
self.__config['telegram']['chat_ids'][id] = [
self.__config['telegram']['chat_ids'][id], [[], []]]
logging.warning(
'Telegram chat ids were saved without shortcuts. Empty list added')
if type(self.__config['tcp']['knownHosts']) == list:
newdict = {}
for id in self.__config['tcp']['knownHosts']:
newdict[id] = ['RenameDevice', '']
self.__config['tcp']['knownHosts'] = newdict
# conf = dict(self.config)
# conf['telegram_bot'] = False
# conf['rtoc_web'] = False
# with open(self.config['global']['documentfolder']+"/config.json", 'w', encoding="utf-8") as fp:
# json.dump(conf, fp, sort_keys=False, indent=4, separators=(',', ': '))
def clearCache(self):
self.__config = _Config(defaultconfig)
# self.save_config()
filename = self.config['global']['documentfolder']+"/plotStyles.json"
if os.path.exists(filename):
os.remove(filename)
def save_config(self):
"""
DEPRECATED. Saves config to file.
"""
# self.config['GUI']['deviceWidget'] = True
# self.config['GUI']['pluginsWidget'] = False
with open(self.config['global']['documentfolder']+"/config.json", 'w', encoding="utf-8") as fp:
json.dump(self.config, fp, sort_keys=False, indent=4, separators=(',', ': '))
def _load_autorun_plugins(self):
userpath = os.path.expanduser('~/.RTOC/autorun_devices')
if not os.path.exists(userpath):
with open(userpath, 'w', encoding="UTF-8") as f:
f.write('')
else:
plugins = []
try:
with open(userpath, 'r', encoding="UTF-8") as f:
content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
plugins = [x.strip() for x in content]
except Exception:
logging.debug(traceback.format_exc())
logging.error('error in '+userpath)
for p in plugins:
self.startPlugin(p)
def check_for_updates(self):
"""
Checks for updates from pypi.
Returns:
str: Current installed version
str: Newest available version
"""
import xmlrpc.client
try:
from pip._internal.utils.misc import get_installed_distributions
except (ImportError, SystemError): # pip<10
from pip import get_installed_distributions
pypi = xmlrpc.client.ServerProxy('http://pypi.python.org/pypi')
available = pypi.package_releases('RTOC')
current = None
for pack in get_installed_distributions():
if pack.project_name == 'RTOC':
current = pack.version
break
if current is not None:
logging.info('\nInstalled version: '+str(current))
else:
logging.info(
'RTOC was not installed with PyPi. To enable version-checking, please install it with "pip3 install RTOC"')
if not available:
logging.info(
"Sorry. Couldn't get version information from PyPi. Please visit 'https://pypi.org/project/RTOC/'")
else:
logging.info('Newest version: '+str(available[0]))
if current is not None and available:
if current == available[0]:
logging.info('RTOC is up to date.')
else:
logging.info('New version available! Please update\n\npip3 install RTOC --upgrade\n')
return current, available
# if __name__ == "__main__":
# kl = RTLogger()
# time.sleep(1)
# kl.stop()