22Wraps the ihcclient in a more user friendly interface to handle lost connection
33Notify thread to handle change notifications
44"""
5+
56# pylint: disable=invalid-name, bare-except, too-many-instance-attributes
67from datetime import datetime , timedelta
78import logging
89import requests
9- import sys
1010import threading
1111import time
1212from ihcsdk .ihcclient import IHCSoapClient , IHCSTATE_READY
1313
1414_LOGGER = logging .getLogger (__name__ )
1515
16+
1617class IHCController :
1718 """
1819 Implements the notification thread and
@@ -49,17 +50,17 @@ def is_ihc_controller(url: str) -> bool:
4950 return False
5051 return True
5152 except requests .exceptions .RequestException as exp :
52- _LOGGER .warning ( "is_ihc_controller %s" ,exp )
53+ _LOGGER .warning ("is_ihc_controller %s" , exp )
5354 return False
5455
5556 def authenticate (self ) -> bool :
5657 """Authenticate and enable the registered notifications"""
5758 with IHCController ._mutex :
58- _LOGGER .debug ( "Authenticating login on ihc controller" )
59+ _LOGGER .debug ("Authenticating login on ihc controller" )
5960 if not self .client .authenticate (self ._username , self ._password ):
60- _LOGGER .debug ( "Authentication failed" )
61+ _LOGGER .debug ("Authentication failed" )
6162 return False
62- _LOGGER .debug ( "Authentication was successful" )
63+ _LOGGER .debug ("Authentication was successful" )
6364 if self ._ihcevents :
6465 self .client .enable_runtime_notifications (self ._ihcevents .keys ())
6566 return True
@@ -131,15 +132,18 @@ def set_runtime_value_time(
131132 self .re_authenticate ()
132133 return self .client .set_runtime_value_time (ihcid , hours , minutes , seconds )
133134
134- def get_project (self ) -> str :
135+ def get_project (self , insegments : bool = True ) -> str :
135136 """Get the ihc project and make sure controller is ready before"""
136137 with IHCController ._mutex :
137138 if self ._project is None :
138139 if self .client .get_state () != IHCSTATE_READY :
139140 ready = self .client .wait_for_state_change (IHCSTATE_READY , 10 )
140141 if ready != IHCSTATE_READY :
141142 return None
142- self ._project = self .client .get_project_in_segments ()
143+ if insegments :
144+ self ._project = self .client .get_project_in_segments ()
145+ else :
146+ self ._project = self .client .get_project ()
143147 return self ._project
144148
145149 def add_notify_event (self , resourceid : int , callback , delayed = False ):
@@ -165,7 +169,7 @@ def add_notify_event(self, resourceid: int, callback, delayed=False):
165169
166170 def _notify_fn (self ):
167171 """The notify thread function."""
168- _LOGGER .debug ( "Starting notify thread" )
172+ _LOGGER .debug ("Starting notify thread" )
169173 while self ._notifyrunning :
170174 try :
171175 with IHCController ._mutex :
@@ -178,7 +182,7 @@ def _notify_fn(self):
178182 if changes is False :
179183 self .re_authenticate (True )
180184 continue
181- for ( ihcid , value ) in changes :
185+ for ihcid , value in changes :
182186 if ihcid in self ._ihcevents :
183187 for callback in self ._ihcevents [ihcid ]:
184188 if (
@@ -188,7 +192,7 @@ def _notify_fn(self):
188192 callback (ihcid , value )
189193 self ._ihcvalues [ihcid ] = value
190194 except Exception as exp :
191- _LOGGER .error ( "Exception in notify thread %s" ,exp )
195+ _LOGGER .error ("Exception in notify thread %s" , exp )
192196 self .re_authenticate (True )
193197
194198 def re_authenticate (self , notify : bool = False ) -> bool :
@@ -200,10 +204,12 @@ def re_authenticate(self, notify: bool = False) -> bool:
200204 """
201205 timeout = datetime .now () + timedelta (seconds = self .reauthenticatetimeout )
202206 while True :
203- _LOGGER .debug ( "Reauthenticating login on ihc controller" )
207+ _LOGGER .debug ("Reauthenticating login on ihc controller" )
204208 if self .authenticate ():
205209 return True
206- _LOGGER .debug ( "Authenticate failed - Reauthenticating login on ihc controller in 10 sec" )
210+ _LOGGER .debug (
211+ "Authenticate failed - Reauthenticating login on ihc controller in 10 sec"
212+ )
207213
208214 # if called from the notify and notify a cancled we do not want to retry
209215 if notify :
0 commit comments