1- # LoggerPlugin v2.5
1+ # LoggerPlugin v2.6
22import traceback
33import time
44import sys
@@ -40,18 +40,20 @@ def __init__(self, stream=None, plot=None, event=None):
4040 event (method): The callback-method for the event-method
4141
4242 """
43- def __init__ (self , stream = None , plot = None , event = None ):
43+ def __init__ (self , stream = None , plot = None , event = None , telegramBot = None , * args , ** kwargs ):
4444 # Plugin setup
4545 # self.setDeviceName()
4646 self ._deviceName = "noDevice"
4747 self ._cb = stream
4848 self ._ev = event
4949 self ._plt = plot
50+ self ._bot = telegramBot
5051 self ._sock = None
5152 self ._tcppassword = ''
5253 self ._tcpport = 5050
5354 self ._tcpaddress = ''
5455 self ._tcpthread = False
56+ self .widget = None
5557 self ._pluginThread = None
5658 self ._oldPerpetualTimer = False
5759 self .lockPerpetialTimer = Lock ()
@@ -159,12 +161,12 @@ def stream(self, y=[], snames=[], dname=None, unit=None, x=None, slist=None, sdi
159161 unit .append (sdict [dev ][sig ][1 ])
160162 x .append (now )
161163 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 ) )
163165 else :
164- logging .error ('STREAM_ERROR: One signal was malformed' )
166+ logging .error ('STREAM_ERROR:signal {}.{} was malformed.' . format ( dname , sig ) )
165167 self ._cb (y = y , snames = snames , dname = dname , unit = unit , x = x )
166168 else :
167- logging .error ('STREAM_ERROR: One device was malformed' )
169+ logging .error ('STREAM_ERROR: device {} was malformed.' . format ( dname ) )
168170 return True
169171 else :
170172 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
265267 self ._tcppassword = password
266268 self ._sock .setKeyword (password )
267269
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 ):
269271 """
270272 Use any of the arguments described in :doc:`TCP`.
271273
@@ -289,6 +291,7 @@ def sendTCP(self, y=None, sname=None, dname=None, unit=None, x=None, getSignal=N
289291 getEventList: :py:meth:`.NetworkFunctions.getEventList`
290292 getPluginList: :py:meth:`.NetworkFunctions.getPluginList`
291293 getSession: :py:meth:`.RT_data.generateSessionJSON`
294+ timeout: TCP-Client Timeout (Default: 5s)
292295
293296 Returns:
294297 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
297300
298301 """
299302 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 ))
301304 t .start ()
302305 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 )
304307
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 ):
306309 with lock :
307310
308311 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=
343346 # dicti['password'] = hex_dig
344347 if self ._sock :
345348 try :
346- self ._sock .connect (self ._tcpaddress , self ._tcpport , self ._tcppassword )
349+ self ._sock .connect (self ._tcpaddress , self ._tcpport , self ._tcppassword , timeout = timeout )
347350 self ._sock .send (dicti )
348351 response = self ._sock .recv ()
349352 # self._sock.close()
@@ -540,6 +543,45 @@ def __updateT(self, func):
540543 func ()
541544 diff = (time .time () - start_time )
542545
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+
543585
544586
545587class _perpetualTimer ():
@@ -571,7 +613,8 @@ def _handle_function(self):
571613 timedelta = timedelta + self ._correction
572614 if timedelta < 0 :
573615 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:
575618 self ._thread = Timer (timedelta , self ._handle_function )
576619 self .thread_counter += 1
577620 self ._thread .start ()
0 commit comments