|
1 | | -import requests |
2 | 1 | import json |
3 | 2 | from ..config import ConnectionMode |
| 3 | +from ..errors import AnedyaInvalidConfig, AnedyaTxFailure |
4 | 4 |
|
5 | 5 |
|
6 | | -def bind_device(self, binding_secret: str): |
| 6 | +def bind_device(self, binding_secret: str, timeout: float | None = None): |
7 | 7 | """ |
8 | | - Bind device to Anedya Cloud |
| 8 | + :param binding_secret: Binding secret to be used for binding the device |
| 9 | + :raises AnedyaInvalidConfig: If the configuration is not provided |
| 10 | + :raises AnedyaTxFailure: If the transaction fails |
| 11 | +
|
| 12 | + This function provides a way to bind a device to the Anedya platform. |
9 | 13 | """ |
| 14 | + if self._config is None: |
| 15 | + raise AnedyaInvalidConfig('Configuration not provided') |
10 | 16 | if self._config.connection_mode == ConnectionMode.MQTT: |
11 | | - result = _bind_device_mqtt(self, binding_secret) |
| 17 | + return _bind_device_mqtt(self, binding_secret=binding_secret, timeout=timeout) |
12 | 18 | elif self._config.connection_mode == ConnectionMode.HTTP: |
13 | | - result = _bind_device_http(self, binding_secret) |
14 | | - return result |
15 | | - |
16 | | - |
17 | | -def _bind_device_mqtt(self, binding_secret: str): |
18 | | - return |
| 19 | + return _bind_device_http(self, binding_secret=binding_secret, timeout=timeout) |
| 20 | + else: |
| 21 | + raise AnedyaInvalidConfig('Invalid connection mode') |
19 | 22 |
|
20 | 23 |
|
21 | | -def _bind_device_http(self, binding_secret: str): |
22 | | - headers = {'Content-type': 'application/json', |
23 | | - 'Auth-mode': self._config.authmode, |
24 | | - 'Authorization': self._config.connection_key} |
| 24 | +def _bind_device_http(self, binding_secret: str, timeout: float | None = None): |
25 | 25 | if self._config._testmode: |
26 | | - url = "https://device.stageapi.anedya.io/v1/bindDevice" |
| 26 | + url = "https://device.stageapi.anedya.io/v1/submitData" |
27 | 27 | else: |
28 | | - url = self._baseurl + "/v1/bindDevice/json" |
| 28 | + url = self._baseurl + "v1/bindDevice" |
29 | 29 | requestPayload = {"bindingsecret": binding_secret, |
30 | 30 | "deviceid": str(self._config._deviceID)} |
31 | | - r = requests.post(url, data=json.dumps(requestPayload), headers=headers) |
32 | | - jsonResponse = r.json() |
33 | | - if r.status_code != 200: |
34 | | - raise RuntimeError(jsonResponse) |
35 | | - # Check whether the call was successful or not |
36 | | - if jsonResponse["success"] is not True: |
37 | | - raise Exception(jsonResponse) |
38 | | - # Check whether the call was successful or not |
39 | | - if jsonResponse["success"] is not True: |
40 | | - raise Exception(jsonResponse) |
41 | | - return True |
| 31 | + r = self._httpsession.post(url, data=json.dumps(requestPayload), timeout=timeout) |
| 32 | + try: |
| 33 | + jsonResponse = r.json() |
| 34 | + payload = json.loads(jsonResponse) |
| 35 | + if payload['success'] is not True: |
| 36 | + raise AnedyaTxFailure(payload['error'], payload['errCode']) |
| 37 | + except ValueError: |
| 38 | + raise AnedyaTxFailure(message="Invalid JSON response") |
| 39 | + return |
| 40 | + |
| 41 | + |
| 42 | +def _bind_device_mqtt(self, binding_secret: str, timeout: float | None = None): |
| 43 | + # Create and register a transaction |
| 44 | + tr = self._transactions.create_transaction() |
| 45 | + # Encode the payload |
| 46 | + requestPayload = { |
| 47 | + "reqId": tr.get_id(), |
| 48 | + "bindingsecret": binding_secret, |
| 49 | + "deviceid": str(self._config._deviceID)} |
| 50 | + payload = json.dumps(requestPayload) |
| 51 | + # Publish the message |
| 52 | + topic_prefix = "$anedya/device/" + str(self._config._deviceID) |
| 53 | + msginfo = self._mqttclient.publish(topic=topic_prefix + "/bindDevice/json", |
| 54 | + payload=payload, qos=1) |
| 55 | + try: |
| 56 | + msginfo.wait_for_publish(timeout=timeout) |
| 57 | + except ValueError: |
| 58 | + raise AnedyaTxFailure(message="Publish queue full") |
| 59 | + except RuntimeError as err: |
| 60 | + raise AnedyaTxFailure(message=str(err)) |
| 61 | + # Wait for transaction to complete |
| 62 | + tr.wait_to_complete() |
| 63 | + # Transaction completed |
| 64 | + # Get the data from the transaction |
| 65 | + data = tr.get_data() |
| 66 | + # Clear transaction |
| 67 | + self._transactions.clear_transaction(tr) |
| 68 | + # Check if transaction is successful or not |
| 69 | + if data['success'] is not True: |
| 70 | + raise AnedyaTxFailure(data['error'], data['errCode']) |
| 71 | + return |
0 commit comments