Skip to content

Commit

Permalink
Nuki:
Browse files Browse the repository at this point in the history
Added timeout to request handling against api to prevent unlimited blocks of lock
  • Loading branch information
Marc René Frieß committed Feb 27, 2024
1 parent a22dde5 commit f156c88
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 7 additions & 7 deletions nuki/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import requests
import cherrypy
import time
import threading
from lib.model.smartplugin import SmartPlugin, SmartPluginWebIf
from lib.item import Items
from bin.smarthome import VERSION
Expand Down Expand Up @@ -57,14 +58,14 @@ def __init__(self, sh, *args, **kwargs):
self._base_url = self.get_parameter_value('protocol') + '://' + self.get_parameter_value(
'bridge_ip') + ":" + str(self.get_parameter_value('bridge_port')) + '/'
self._token = self.get_parameter_value('bridge_api_token')
self._lock = False
self._action = ''
self._noWait = self.get_parameter_value('no_wait')
self.items = Items.get_instance()

if not self.init_webinterfaces():
self._init_complete = False

self._nuki_lock = threading.Lock()
self._callback_ip = self.mod_http.get_local_ip_address() # get_parameter_value('bridge_callback_ip')
self._callback_port = self.mod_http.get_local_servicesport() # get_parameter_value('bridge_callback_port')

Expand Down Expand Up @@ -301,13 +302,12 @@ def _get_nuki_status(self):

def _api_call(self, base_url, endpoint=None, nuki_id=None, token=None, action=None, no_wait=None, callback_url=None,
id=None):

while self._lock:
while self._nuki_lock.locked():
time.sleep(0.1)
self.logger.debug("Plugin '{}': Waiting for lock to release...".format(self.get_shortname()))
self._nuki_lock.acquire()
self.logger.debug("Plugin '{}': Lock set.".format(self.get_shortname()))
try:
self._lock = True
self.logger.debug("Plugin '{}': Lock set.".format(self.get_shortname()))
payload = {}
if nuki_id is not None:
payload['nukiID'] = nuki_id
Expand All @@ -326,14 +326,14 @@ def _api_call(self, base_url, endpoint=None, nuki_id=None, token=None, action=No
self.logger.debug(
"Plugin '{}': starting API Call to Nuki Bridge at {} with payload {}.".format(self.get_shortname(), url,
payload))
response = requests.get(url=urllib.parse.urljoin(base_url, endpoint), params=payload)
response = requests.get(url=urllib.parse.urljoin(base_url, endpoint), params=payload, timeout=120)
self.logger.debug("Plugin '{}': finishing API Call to Nuki Bridge at {}.".format(self.get_shortname(), url))
self.logger.debug("Plugin '{}': response.raise_for_status: {}".format(self.get_shortname(), response.raise_for_status()))
return json.loads(response.text)
except Exception as ex:
self.logger.error(ex)
finally:
self._lock = False
self._nuki_lock.release()
self.logger.debug("Plugin '{"
"}': Lock removed.".format(self.get_shortname()))

Expand Down
2 changes: 1 addition & 1 deletion nuki/webif/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<tr>
<td class="py-1"><strong>{{ _('Erkannte Nuki IDs') }}</strong></td>
<td class="py-1" colspan="2">{% for item in paired_nukis %}{{ item }}{% if not loop.last %}, {% endif %}{% endfor %}</td>
<td class="py-1"><strong>{{ _('API Request Lock') }}</strong>: {{ p._lock }}</td>
<td class="py-1"><strong>{{ _('API Request Lock') }}</strong>: {{ p._nuki_lock.locked() }}</td>
</tr>
</tbody>
</table>
Expand Down

0 comments on commit f156c88

Please sign in to comment.