Skip to content

Commit

Permalink
Added a mutex to prevent calling the cloud API concurrently. Yuck...
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoAcheron committed Dec 8, 2018
1 parent fdba29f commit 3292bf9
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions midea/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import datetime
import json

from threading import Lock

from midea.security import security

# The Midea cloud client is by far the more obscure part of this library, and without some serious reverse engineering
Expand Down Expand Up @@ -30,6 +32,8 @@ def __init__(self, appKey, email, password):
self.security = security(self.appKey)
self._retries = 0

self._mutex = Lock()

def api_request(self, endpoint, args):
"""Sends an API request to the Midea cloud service and returns the results
or raises ValueError if there is an error
Expand All @@ -54,8 +58,12 @@ def api_request(self, endpoint, args):

data['sign'] = self.security.sign(url, data)

# POST the endpoint with the payload
r = requests.post(url=url, data=data)
# POST the endpoint with the payload, but one at a time
self._mutex.acquire()
try:
r = requests.post(url=url, data=data)
finally:
self._mutex.release()
response = json.loads(r.text)
# Check for errors, raise if there are any
if response['errorCode'] != '0':
Expand Down

0 comments on commit 3292bf9

Please sign in to comment.