1
- # LoggerPlugin v2.5
1
+ # LoggerPlugin v2.6
2
2
import traceback
3
3
import time
4
4
import sys
@@ -40,18 +40,20 @@ def __init__(self, stream=None, plot=None, event=None):
40
40
event (method): The callback-method for the event-method
41
41
42
42
"""
43
- def __init__ (self , stream = None , plot = None , event = None ):
43
+ def __init__ (self , stream = None , plot = None , event = None , telegramBot = None , * args , ** kwargs ):
44
44
# Plugin setup
45
45
# self.setDeviceName()
46
46
self ._deviceName = "noDevice"
47
47
self ._cb = stream
48
48
self ._ev = event
49
49
self ._plt = plot
50
+ self ._bot = telegramBot
50
51
self ._sock = None
51
52
self ._tcppassword = ''
52
53
self ._tcpport = 5050
53
54
self ._tcpaddress = ''
54
55
self ._tcpthread = False
56
+ self .widget = None
55
57
self ._pluginThread = None
56
58
self ._oldPerpetualTimer = False
57
59
self .lockPerpetialTimer = Lock ()
@@ -159,12 +161,12 @@ def stream(self, y=[], snames=[], dname=None, unit=None, x=None, slist=None, sdi
159
161
unit .append (sdict [dev ][sig ][1 ])
160
162
x .append (now )
161
163
else :
162
- logging .error ('STREAM ERROR, signal has not this format: [y, "unit"]' )
164
+ logging .error ('STREAM ERROR, signal {}.{} has not this format: [y, "unit"]' . format ( dname , sig ) )
163
165
else :
164
- logging .error ('STREAM_ERROR: One signal was malformed' )
166
+ logging .error ('STREAM_ERROR:signal {}.{} was malformed.' . format ( dname , sig ) )
165
167
self ._cb (y = y , snames = snames , dname = dname , unit = unit , x = x )
166
168
else :
167
- logging .error ('STREAM_ERROR: One device was malformed' )
169
+ logging .error ('STREAM_ERROR: device {} was malformed.' . format ( dname ) )
168
170
return True
169
171
else :
170
172
logging .error ('STREAM_ERROR: The data you provided with in your plugin was wrong."' )
@@ -265,7 +267,7 @@ def createTCPClient(self, address="localhost", password=None, tcpport=5050, thre
265
267
self ._tcppassword = password
266
268
self ._sock .setKeyword (password )
267
269
268
- def sendTCP (self , y = None , sname = None , dname = None , unit = None , x = None , getSignal = None , getLatest = None , getEvent = None , getSignalList = False , getEventList = False , getPluginList = False , getSession = False , plot = False , event = None , remove = None , plugin = None , logger = None , stream = None ):
270
+ def sendTCP (self , y = None , sname = None , dname = None , unit = None , x = None , getSignal = None , getLatest = None , getEvent = None , getSignalList = False , getEventList = False , getPluginList = False , getSession = False , plot = False , event = None , remove = None , plugin = None , logger = None , stream = None , timeout = 5 ):
269
271
"""
270
272
Use any of the arguments described in :doc:`TCP`.
271
273
@@ -289,6 +291,7 @@ def sendTCP(self, y=None, sname=None, dname=None, unit=None, x=None, getSignal=N
289
291
getEventList: :py:meth:`.NetworkFunctions.getEventList`
290
292
getPluginList: :py:meth:`.NetworkFunctions.getPluginList`
291
293
getSession: :py:meth:`.RT_data.generateSessionJSON`
294
+ timeout: TCP-Client Timeout (Default: 5s)
292
295
293
296
Returns:
294
297
tcp_response (dict), if createTCPClient(threaded=False)
@@ -297,12 +300,12 @@ def sendTCP(self, y=None, sname=None, dname=None, unit=None, x=None, getSignal=N
297
300
298
301
"""
299
302
if self ._tcpthread :
300
- t = Thread (target = self ._sendTCP , args = (y , sname , dname , unit , x , getSignal , getLatest , getEvent , getSignalList , getEventList , getPluginList , getSession , plot , event , remove , plugin , logger , stream ,))
303
+ t = Thread (target = self ._sendTCP , args = (y , sname , dname , unit , x , getSignal , getLatest , getEvent , getSignalList , getEventList , getPluginList , getSession , plot , event , remove , plugin , logger , stream , timeout ))
301
304
t .start ()
302
305
else :
303
- return self ._sendTCP (y , sname , dname , unit , x , getSignal , getLatest , getEvent , getSignalList , getEventList , getPluginList , getSession , plot , event , remove , plugin , logger , stream )
306
+ return self ._sendTCP (y , sname , dname , unit , x , getSignal , getLatest , getEvent , getSignalList , getEventList , getPluginList , getSession , plot , event , remove , plugin , logger , stream , timeout )
304
307
305
- def _sendTCP (self , y = None , sname = None , dname = None , unit = None , x = None , getSignal = None , getLatest = None , getEvent = None , getSignalList = False , getEventList = False , getPluginList = False , getSession = False , plot = False , event = None , remove = None , plugin = None , logger = None , stream = None ):
308
+ def _sendTCP (self , y = None , sname = None , dname = None , unit = None , x = None , getSignal = None , getLatest = None , getEvent = None , getSignalList = False , getEventList = False , getPluginList = False , getSession = False , plot = False , event = None , remove = None , plugin = None , logger = None , stream = None , timeout = 5 ):
306
309
with lock :
307
310
308
311
if x is None and y is not None and not plot :
@@ -343,7 +346,7 @@ def _sendTCP(self, y=None, sname=None, dname=None, unit=None, x=None, getSignal=
343
346
# dicti['password'] = hex_dig
344
347
if self ._sock :
345
348
try :
346
- self ._sock .connect (self ._tcpaddress , self ._tcpport , self ._tcppassword )
349
+ self ._sock .connect (self ._tcpaddress , self ._tcpport , self ._tcppassword , timeout = timeout )
347
350
self ._sock .send (dicti )
348
351
response = self ._sock .recv ()
349
352
# self._sock.close()
@@ -540,6 +543,45 @@ def __updateT(self, func):
540
543
func ()
541
544
diff = (time .time () - start_time )
542
545
546
+ def telegram_send_message (self , text , onlyAdmin = False ):
547
+ """
548
+ Sends a message to all clients (or only admins).
549
+
550
+ Args:
551
+ text (str): Text to be send to the clients.
552
+ onlyAdmin (bool): If True, only admins will get this message
553
+ """
554
+ if self ._bot is not None :
555
+ self ._bot .send_message_to_all (text , onlyAdmin )
556
+ else :
557
+ logging .warning ('TelegramBot is not enabled or wrong configured! Can not send message "{}"' .format (text ))
558
+
559
+ def telegram_send_photo (self , path , onlyAdmin = False ):
560
+ """
561
+ Sends the picture at a given path to all clients (or only admins).
562
+
563
+ Args:
564
+ path (str): Path to the picture to send.
565
+ onlyAdmin (bool): If True, only admins will get this message
566
+ """
567
+ if self ._bot is not None :
568
+ self ._bot .send_photo (path , onlyAdmin )
569
+ else :
570
+ logging .warning ('TelegramBot is not enabled or wrong configured! Can not send photo "{}"' .format (path ))
571
+
572
+ def telegram_send_document (self , path , onlyAdmin = False ):
573
+ """
574
+ Sends any document at a given path to all clients (or only admins).
575
+
576
+ Args:
577
+ path (str): Path to the file to send.
578
+ onlyAdmin (bool): If True, only admins will get this message
579
+ """
580
+ if self ._bot is not None :
581
+ self ._bot .send_document (path , onlyAdmin )
582
+ else :
583
+ logging .warning ('TelegramBot is not enabled or wrong configured! Can not send file "{}"' .format (path ))
584
+
543
585
544
586
545
587
class _perpetualTimer ():
@@ -571,7 +613,8 @@ def _handle_function(self):
571
613
timedelta = timedelta + self ._correction
572
614
if timedelta < 0 :
573
615
timedelta = 0
574
- if not self ._lock .locked () and not self ._cancel :
616
+ if not self ._cancel and not self ._lock .locked ():
617
+ # with self._lock:
575
618
self ._thread = Timer (timedelta , self ._handle_function )
576
619
self .thread_counter += 1
577
620
self ._thread .start ()
0 commit comments