Skip to content

Commit 1d0467d

Browse files
author
Sebastian
committed
bugfixes
1 parent 56d826d commit 1d0467d

File tree

4 files changed

+98
-77
lines changed

4 files changed

+98
-77
lines changed

RTOC/LoggerPlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LoggerPlugin v2.3
1+
# LoggerPlugin v2.4
22
import traceback
33
import time
44
import sys

RTOC/RTLogger/RT_data.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ def __init__(self, t, hFunction, useqtimer=False):
5757
def _handle_function(self):
5858
try:
5959
self._hFunction()
60-
except Exception:
60+
except Exception as error:
61+
logging.info(traceback.format_exc())
62+
logging.info(error)
6163
logging.info('Backup failed')
6264
if QTimer is not None and self._useqtimer:
6365
self._thread = QTimer()
@@ -1126,7 +1128,7 @@ def _addSQLSignal(self, sigID, signal):
11261128
dev = self._SQLdeviceExists(devicename)
11271129
if dev is False:
11281130
self._SQLcreateDevice(signal[0], devicename)
1129-
if not self.signalExists(signalname, devicename):
1131+
if not self._SQLsignalExists(signalname, devicename):
11301132
sigID = self._SQLcreateSignal(sigID, signalname, devicename, x, y, unit)
11311133
# add data
11321134
sql = 'UPDATE '+SIGNAL_TABLE_NAME+' SET X = ARRAY'+str(x)+'::NUMERIC[] WHERE ID ='+str(sigID)+';'
@@ -1146,6 +1148,16 @@ def _SQLdeviceExists(self, devicename):
11461148
sig = False
11471149
return sig
11481150

1151+
def _SQLsignalExists(self, signalname):
1152+
existtest = "SELECT EXISTS (select ID from "+SIGNAL_TABLE_NAME + \
1153+
" where NAME = '"+str(signalname)+"');"
1154+
sig = self._execute_n_fetchall(existtest)
1155+
if sig is not None and sig != []:
1156+
sig = bool(sig[0][0])
1157+
else:
1158+
sig = False
1159+
return sig
1160+
11491161
def _SQLcreateDevice(self, devID, devicename):
11501162
# logging.info('Creating new device: '+str(devicename))
11511163
timestamp = time.time()
@@ -1199,7 +1211,7 @@ def _SQLcreateEvent(self, devID, sigID, evID, strung, x, value, priority, eventi
11991211
devID = self._SQLgetDeviceID(devicename)
12001212
# check_sigID = self._SQLgetSignalID(devicename, signalname)
12011213
# if check_sigID == -1:
1202-
if not self.signalExists(signalname, devicename):
1214+
if not self._SQLsignalExists(signalname, devicename):
12031215
sigID = self._SQLcreateSignal(sigID, devicename, signalname)
12041216
# sigID = self._SQLgetSignalID(devicename, signalname)
12051217
if devID != -1 and sigID != -1:
@@ -1308,7 +1320,7 @@ def _SQLplot(self, x=[], y=[], sname="noName", dname="noDevice", unit="", c=Fals
13081320
if len(x) == len(y):
13091321
if not self._SQLdeviceExists(dname):
13101322
self._SQLcreateDevice(dname)
1311-
if not self.signalExists(sname, dname):
1323+
if not self._SQLsignalExists(sname, dname):
13121324
sigID = self._SQLcreateSignal(sname, dname)
13131325
self._SQLplotNewData(x, y, unit, dname, sname,
13141326
c, hold, autoResize)

RTOC/RTLogger/telegramBot.py

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -931,9 +931,7 @@ def signalsSelectRangeHandler(self, bot, chat_id):
931931
xmin_s, xmax_s = self.signals_range[chat_id]
932932
xmin_s = dt.datetime.fromtimestamp(xmin_s).strftime("%d.%m.%Y %H:%M:%S")
933933
xmax_s = dt.datetime.fromtimestamp(xmax_s).strftime("%d.%m.%Y %H:%M:%S")
934-
text = 'Hier kannst du einstellen, welchen Bereich ich darstellen soll.\nGib dazu jetzt das Startdatum und Enddatum in folgendem Format ein: "' + \
935-
dt.datetime.fromtimestamp(time.time()-1000).strftime("%d.%m.%Y %H:%M:%S") + \
936-
' - '+dt.datetime.fromtimestamp(time.time()).strftime("%d.%m.%Y %H:%M:%S")+'"'
934+
text = 'Hier kannst du einstellen, welchen Bereich ich darstellen soll.\nWenn du nur ein Datum angibst, gehe ich davon aus, du willst die Daten ab diesem Zeitpunkt bis jetzt haben.\nWenn du einen Zeitraum angeben willst, trenne zwei Zeitpunkte mit einem "-". Du musst keine Uhrzeit angeben.\nBeispiel: "16.05.19 - 13.06.19 14:33"'
937935
text += translate('RTOC', '\nAusgew\xe4hlter Zeitraum:\n{} - {}\nVerf\xfcgbarer Zeitraum:\n{} - {}\n').format(xmin_s, xmax_s, xmin, xmax)
938936
commands = [translate('RTOC', 'Letzte Minute'), translate('RTOC', 'Letzten 10 Minuten'), translate('RTOC', 'Letzte Stunde'), translate('RTOC', 'Letzte 24h'), translate('RTOC', 'Letzte Woche'), translate('RTOC', 'Letzter Monat'), translate('RTOC', 'Alles')]
939937
self.sendMenuMessage(bot, chat_id, commands, text)
@@ -964,56 +962,11 @@ def signalsSelectRangeHandlerAns(self, bot, chat_id, strung):
964962
xmin = self.logger.database.getGlobalXmin() -100
965963
xmax = self.logger.database.getGlobalXmax() +100
966964
else:
967-
if len(strung.split('-')) == 2:
968-
times = strung.split('-')
969-
while times[0].endswith(' '):
970-
times[0] = times[0][0:-1]
971-
while times[0].startswith(' '):
972-
times[0] = times[0][1:]
973-
while times[1].endswith(' '):
974-
times[1] = times[1][0:-1]
975-
while times[0].startswith(' '):
976-
times[1] = times[1][1:]
977-
foundXmin = False
978-
foundXmax = False
979-
for format in ['%d.%m.%Y %H:%M:%S', '%d.%m %H:%M:%S', '%d.%m %H:%M', '%d.%m.%Y %H:%M', '%d.%m.%Y', '%d.%m']:
980-
try:
981-
xmin = dt.datetime.strptime(times[0], format).timestamp()
982-
foundXmin = True
983-
break
984-
except Exception:
985-
pass
986-
for format in ['%d.%m.%Y %H:%M:%S', '%d.%m %H:%M:%S', '%d.%m %H:%M', '%d.%m.%Y %H:%M', '%d.%m.%Y', '%d.%m']:
987-
try:
988-
xmax = dt.datetime.strptime(times[1], format).timestamp()
989-
foundXmax = True
990-
break
991-
except Exception:
992-
pass
993-
994-
if not foundXmin or not foundXmax:
995-
# print(traceback.format_exc())
996-
self.send_message(chat_id=chat_id,
997-
text=translate('RTOC', 'Bitte sende mir einen Zeitraum, den ich verstehen kann:\n')+'"'+dt.datetime.fromtimestamp(time.time()-1000).strftime("%d.%m.%Y %H:%M:%S")+' - '+dt.datetime.fromtimestamp(time.time()).strftime("%d.%m.%Y %H:%M:%S")+'"')
998-
return
999-
else:
1000-
foundXmin = False
1001-
while strung.endswith(' '):
1002-
strung = strung[0:-1]
1003-
while strung.startswith(' '):
1004-
strung = strung[1:]
1005-
for format in ['%d.%m.%Y %H:%M:%S', '%d.%m %H:%M:%S', '%d.%m %H:%M', '%d.%m.%Y %H:%M', '%d.%m.%Y', '%d.%m']:
1006-
try:
1007-
xmin = dt.datetime.strptime(strung, format).timestamp()
1008-
xmax = time.time()
1009-
foundXmin = True
1010-
break
1011-
except Exception:
1012-
pass
1013-
if not foundXmin:
1014-
self.send_message(chat_id=chat_id,
1015-
text=translate('RTOC', 'Bitte sende mir einen Zeitraum, den ich verstehen kann:\n')+'"'+dt.datetime.fromtimestamp(time.time()-1000).strftime("%d.%m.%Y %H:%M:%S")+' - '+dt.datetime.fromtimestamp(time.time()).strftime("%d.%m.%Y %H:%M:%S")+'"')
1016-
return
965+
found, xmin, xmax = _strToTimerange(strung)
966+
if not found:
967+
self.send_message(chat_id=chat_id,
968+
text=translate('RTOC', 'Bitte sende mir einen Zeitraum, den ich verstehen kann.'))
969+
return
1017970
self.signals_range[chat_id] = [xmin, xmax]
1018971
xmin = dt.datetime.fromtimestamp(xmin).strftime("%d.%m.%Y %H:%M:%S")
1019972
xmax = dt.datetime.fromtimestamp(xmax).strftime("%d.%m.%Y %H:%M:%S")
@@ -2030,3 +1983,52 @@ def executeUserAction(self, bot, chat_id, strung):
20301983
# Name angeben ...
20311984
#
20321985
# <--
1986+
1987+
1988+
def _strToTimestamp(datetimestr):
1989+
while datetimestr.endswith(' '):
1990+
datetimestr = datetimestr[0:-1]
1991+
while datetimestr.startswith(' '):
1992+
datetimestr = datetimestr[1:]
1993+
formats = []
1994+
dates = ['%d.%m.%Y', '%d.%m', '%m.%Y', '%d.%m.%y', '%m/%d', '%m/%d/%y',
1995+
'%m/%d/%Y', '%m-%d-%y', '%m-%d-%Y', '%d. %B %Y', '%B %Y']
1996+
times = ['%H:%M:%S', '%H:%M']
1997+
1998+
for d in dates:
1999+
formats += [d]
2000+
for t in times:
2001+
formats += [t]
2002+
formats += [d+' '+t]
2003+
formats += [t+' '+d]
2004+
2005+
for format in formats:
2006+
try:
2007+
ts = dt.datetime.strptime(datetimestr, format)
2008+
if 'y' not in format.lower():
2009+
ts = ts.replace(year=2019)
2010+
ts = ts.timestamp()
2011+
2012+
return True, ts
2013+
break
2014+
except Exception:
2015+
pass
2016+
return False, None
2017+
2018+
2019+
def _strToTimerange(rangestr):
2020+
if len(rangestr.split('-')) == 2:
2021+
times = rangestr.split('-')
2022+
foundXmin, xmin = _strToTimestamp(times[0])
2023+
foundXmax, xmax = _strToTimestamp(times[1])
2024+
if not foundXmin or not foundXmax:
2025+
return False, None, None
2026+
else:
2027+
return True, xmin, xmax
2028+
else:
2029+
xmax = time.time()
2030+
foundXmin, xmin = _strToTimestamp(rangestr)
2031+
if not foundXmin:
2032+
return False, None, None
2033+
else:
2034+
return True, xmin, xmax

RTOC/jsonsocket.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# jsonsocket.py v1.6
1+
# jsonsocket.py v1.7
22

33
import json
44
import socket
@@ -26,6 +26,7 @@ class NoPasswordProtectionError(Exception):
2626
expression -- input expression in which the error occurred
2727
message -- explanation of the error
2828
"""
29+
2930
def __init__(self, expression, message):
3031
self.expression = expression
3132
self.message = message
@@ -39,9 +40,10 @@ class WrongPasswordError(Exception):
3940
expression -- input expression in which the error occurred
4041
message -- explanation of the error
4142
"""
43+
4244
def __init__(self, expression, message):
43-
self.expression = expression
44-
self.message = message
45+
self.expression = expression
46+
self.message = message
4547

4648

4749
class PasswordProtectedError(Exception):
@@ -52,6 +54,7 @@ class PasswordProtectedError(Exception):
5254
expression -- input expression in which the error occurred
5355
message -- explanation of the error
5456
"""
57+
5558
def __init__(self, expression, message):
5659
self.expression = expression
5760
self.message = message
@@ -69,6 +72,7 @@ def __init__(self, expression, message):
6972
# self.expression = expression
7073
# self.message = message
7174

75+
BACKLOG = 5
7276

7377

7478
class Server(object):
@@ -83,11 +87,10 @@ class Server(object):
8387
reuse_port (bool): Enable/disable reuse_port (default: True)
8488
"""
8589

86-
backlog = 5
87-
client = None
88-
8990
def __init__(self, host, port, keyword=None, reuse_port=True):
9091
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
92+
self.client = None
93+
9194
if reuse_port:
9295
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
9396
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
@@ -99,7 +102,7 @@ def __init__(self, host, port, keyword=None, reuse_port=True):
99102
# self.socket.setblocking(0)
100103
self.socket.settimeout(5.0)
101104
self.socket.bind((host, port))
102-
self.socket.listen(self.backlog)
105+
self.socket.listen(BACKLOG)
103106

104107
self.keyword = keyword
105108

@@ -112,8 +115,8 @@ def setKeyword(self, keyword=None):
112115
"""
113116
self.keyword = keyword
114117

115-
def __del__(self):
116-
self.close()
118+
# def __del__(self):
119+
# self.close()
117120

118121
def accept(self):
119122
"""
@@ -181,9 +184,10 @@ class Client(object):
181184
keyword (str or None): Set a keyword for encrypted communication. Leaf blank for unsecure connection. (default: None)
182185
"""
183186

184-
socket = None
185-
keyword = None
186-
host = None
187+
def __init__(self):
188+
self.socket = None
189+
self.keyword = None
190+
self.host = None
187191

188192
def setKeyword(self, keyword=None):
189193
"""
@@ -194,8 +198,8 @@ def setKeyword(self, keyword=None):
194198
"""
195199
self.keyword = keyword
196200

197-
def __del__(self):
198-
self.close()
201+
# def __del__(self):
202+
# self.close()
199203

200204
def connect(self, host, port, keyword=None, reuse_port=True):
201205
"""
@@ -208,6 +212,7 @@ def connect(self, host, port, keyword=None, reuse_port=True):
208212
reuse_port (bool): Enable/disable reuse_port (default: True)
209213
"""
210214
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
215+
211216
if reuse_port:
212217
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
213218
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
@@ -312,10 +317,10 @@ def _recv(socket, key=None):
312317
# read the length of the data, letter by letter until we reach EOL
313318
length_str = b''
314319
try:
315-
char = socket.recv(1)#.decode()
320+
char = socket.recv(1) # .decode()
316321
while char != b'\n':
317322
length_str += char
318-
char = socket.recv(1)#.decode()
323+
char = socket.recv(1) # .decode()
319324
except Exception:
320325
print(traceback.format_exc())
321326
return False
@@ -328,7 +333,7 @@ def _recv(socket, key=None):
328333
total = 0
329334
tagTotal = 0
330335
nonceTotal = 0
331-
if len(lens) ==1:
336+
if len(lens) == 1:
332337
total = int(lens[0])
333338
# not encrypted
334339
elif len(lens) == 3:
@@ -364,15 +369,17 @@ def _recv(socket, key=None):
364369
logging.error(tb)
365370
raise WrongPasswordError("SOCKET PASSWORD ERROR, The provided password is wrong!")
366371
else:
367-
raise NoPasswordProtectionError('SOCKET PASSWORD ERROR, No password provided!\nCannot receive data')
372+
raise NoPasswordProtectionError(
373+
'SOCKET PASSWORD ERROR, No password provided!\nCannot receive data')
368374
else:
369375
if len(tagView) == 0 and len(nonceView) == 0:
370376
try:
371377
return deserializeJSON(view)
372378
except (TypeError, ValueError):
373379
tb = traceback.format_exc()
374380
logging.debug(tb)
375-
raise PasswordProtectedError('JSON SOCKET ERROR, Data received was not in JSON format. Maybe the RTOC-Server is password-protected')
381+
raise PasswordProtectedError(
382+
'JSON SOCKET ERROR, Data received was not in JSON format. Maybe the RTOC-Server is password-protected')
376383
else:
377384
raise PasswordProtectedError('SOCKET ERROR, The server is password protected')
378385

0 commit comments

Comments
 (0)