Skip to content

API

API

This is used to communicate with the API.

  • Register the device
  • Post audio to the API
  • Post video to the API
  • [Optional] Queue speech to text
Source code in Client/Listener/api.py
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
class API:
    """
    This is used to communicate with the API.

    - Register the device
    - Post audio to the API
    - Post video to the API
    - [Optional] Queue speech to text
    """

    def __init__(
        self,
        domain: str = API_DOMAIN,
        token: str = "",
        home_id: Optional[int] = None,
        track_cluster: Optional[str] = None,
    ):
        """
        The API class for the responder

        It will require the token and the endpoint to communicate with the API.

        If you deploy the API to a cloud server, do not forget to change the domain to the server's domain.

        Args:
            domain (str): The domain of the API.
            token (str): The token for the API.
            home_id (int): The home ID.
            track_cluster (str): The track cluster.

        """
        self.domain = domain
        self.token = token
        self.mac_address = get_mac_address()
        self.home_id = home_id
        self.track_cluster = track_cluster

    def set_track_id(self):
        if self.track_cluster is None:
            return None
        uid = str(uuid4())
        uid = uid.replace("-", "")
        track_id = f"T-{self.track_cluster}-{uid}"
        logger.info(track_id)
        return track_id

    def register_device(
        self,
        device_name: Optional[str] = None,
        device_type: Optional[str] = None,
        description: Optional[str] = None,
    ):
        """
        Register the device to the API.
        Args:
            device_name (Optional[str]): The device name, you can name it if you want to distinguish it better later
            device_type (Optional[str]): The device type, this can be used to distinguish the device type
            description (Optional[str]): The description of the device

        Returns:

        """
        url = f"{self.domain}/hardware/register/"

        r = requests.post(
            url,
            data={
                "home": self.home_id,
                "mac_address": self.mac_address,
                "device_name": device_name,
                "device_type": device_type,
                "description": description,
            },
            headers={"Authorization": f"Token {self.token}"},
            timeout=30,
        )
        logger.info(url)

        logger.info(f"POST {url} {r.status_code}")

    def post_audio(
        self,
        uid: str,
        sequence_index: int,
        audio_file: str,
        start_time: datetime,
        end_time: datetime,
        track_id: str = None,
    ):
        """
        Post metadata of the audio to the API.
        Args:
            uid (str): uuid of the audio
            sequence_index (int): The sequence index of the audio in this loop, together with uuid,
                                  it can be used to identify the audio
            audio_file (str): Path to the audio file, which will be synced to the API disk storage via another parameter
            start_time (datetime): The start time of the audio
            end_time (datetime): The end time of the audio
            track_id (str): The track id of the task

        Returns:

        """
        url = f"{self.domain}/hardware/audio/"
        r = requests.post(
            url,
            data={
                "home": self.home_id,
                "uid": uid,
                "sequence_index": sequence_index,
                "audio_file": audio_file,
                "start_time": start_time,
                "end_time": end_time,
                "hardware_device_mac_address": self.mac_address,
                "track_id": track_id,
            },
            headers={"Authorization": f"Token {self.token}"},
            timeout=30,
        )
        logger.info(f"POST {url} {r.status_code}")
        if r.status_code != 201:
            return None
        return r.json()

    def post_video(
        self, uid: str, video_file: str, start_time: datetime, end_time: datetime
    ):
        """
        Post metadata of the video to the API.
        Args:
            uid (str): uuid of this video section
            video_file (str): Path to the video file, which will be synced to the API disk storage via another parameter
                              it will also hold the information in the file name about the start/end time
            start_time (datetime): The start time of the video
            end_time (datetime): The end time of the video
        Returns:

        """
        url = f"{self.domain}/hardware/video/"
        data = {
            "home": self.home_id,
            "uid": uid,
            "hardware_device_mac_address": self.mac_address,
            "video_file": video_file,
            "start_time": start_time.isoformat(),
            "end_time": end_time.isoformat(),
        }
        logger.info(data)
        r = requests.post(
            url, data=data, headers={"Authorization": f"Token {self.token}"}, timeout=30
        )
        logger.info(f"POST {url} {r.status_code}")
        if r.status_code != 200:
            return None
        return r.json()

    def queue_speech_to_text(
        self, uid: str, audio_index: str, start_time: datetime, end_time: datetime
    ) -> str:
        """
        Optional, used to queue the speech to text task
        Args:
            uid (str): uuid of the audio
            audio_index (str): The audio index, which can be used to identify the audio
            start_time (datetime): The start time of the audio
            end_time (datetime): The end time of the audio

        Returns:
            (str): The track id of the task

        """
        track_id = self.set_track_id()
        url = f"{self.domain}/queue_task/ai_task/"
        data = {
            "name": "speech_to_text",
            "task_name": "speech2text",
            "parameters": json.dumps(
                {
                    "uid": uid,
                    "home_id": self.home_id,
                    "audio_index": audio_index,
                    "start_time": start_time.isoformat(),
                    "end_time": end_time.isoformat(),
                    "hardware_device_mac_address": self.mac_address,
                }
            ),
            "track_id": track_id,
        }
        r = requests.post(
            url, data=data, headers={"Authorization": f"Token {self.token}"}, timeout=30
        )
        logger.info(f"POST {url} {r.status_code}")
        if r.status_code != 200:
            logger.info(data)
            return None
        logger.info(r.json())
        return track_id

    def get_storage_solution(self):
        """
        Get the storage solution from the API
        Returns:

        """
        url = f"{self.domain}/hardware/storage_solution/"
        r = requests.get(
            url, headers={"Authorization": f"Token {self.token}"}, timeout=30
        )
        logger.info(f"GET {url} {r.status_code}")
        if r.status_code != 200:
            return None
        data = r.json()
        logger.info(data)
        return data.get("storage_solution", "local")

    def upload_file(
        self,
        source_file: str,
        dest_path: str,
    ):
        """
        Upload the file to the API
        """
        url = f"{self.domain}/hardware/upload_file/"
        files = {"file": open(source_file, "rb")}
        data = {
            "dest_path": dest_path,
        }
        r = requests.post(
            url,
            files=files,
            data=data,
            headers={"Authorization": f"Token {self.token}"},
            timeout=30,
        )
        logger.info(f"POST {url} {r.status_code}")
        if r.status_code != 200:
            return None
        return True

__init__(domain=API_DOMAIN, token='', home_id=None, track_cluster=None)

The API class for the responder

It will require the token and the endpoint to communicate with the API.

If you deploy the API to a cloud server, do not forget to change the domain to the server's domain.

Parameters:

Name Type Description Default
domain str

The domain of the API.

API_DOMAIN
token str

The token for the API.

''
home_id int

The home ID.

None
track_cluster str

The track cluster.

None
Source code in Client/Listener/api.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def __init__(
    self,
    domain: str = API_DOMAIN,
    token: str = "",
    home_id: Optional[int] = None,
    track_cluster: Optional[str] = None,
):
    """
    The API class for the responder

    It will require the token and the endpoint to communicate with the API.

    If you deploy the API to a cloud server, do not forget to change the domain to the server's domain.

    Args:
        domain (str): The domain of the API.
        token (str): The token for the API.
        home_id (int): The home ID.
        track_cluster (str): The track cluster.

    """
    self.domain = domain
    self.token = token
    self.mac_address = get_mac_address()
    self.home_id = home_id
    self.track_cluster = track_cluster

get_storage_solution()

Get the storage solution from the API Returns:

Source code in Client/Listener/api.py
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
def get_storage_solution(self):
    """
    Get the storage solution from the API
    Returns:

    """
    url = f"{self.domain}/hardware/storage_solution/"
    r = requests.get(
        url, headers={"Authorization": f"Token {self.token}"}, timeout=30
    )
    logger.info(f"GET {url} {r.status_code}")
    if r.status_code != 200:
        return None
    data = r.json()
    logger.info(data)
    return data.get("storage_solution", "local")

post_audio(uid, sequence_index, audio_file, start_time, end_time, track_id=None)

Post metadata of the audio to the API. Args: uid (str): uuid of the audio sequence_index (int): The sequence index of the audio in this loop, together with uuid, it can be used to identify the audio audio_file (str): Path to the audio file, which will be synced to the API disk storage via another parameter start_time (datetime): The start time of the audio end_time (datetime): The end time of the audio track_id (str): The track id of the task

Returns:

Source code in Client/Listener/api.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def post_audio(
    self,
    uid: str,
    sequence_index: int,
    audio_file: str,
    start_time: datetime,
    end_time: datetime,
    track_id: str = None,
):
    """
    Post metadata of the audio to the API.
    Args:
        uid (str): uuid of the audio
        sequence_index (int): The sequence index of the audio in this loop, together with uuid,
                              it can be used to identify the audio
        audio_file (str): Path to the audio file, which will be synced to the API disk storage via another parameter
        start_time (datetime): The start time of the audio
        end_time (datetime): The end time of the audio
        track_id (str): The track id of the task

    Returns:

    """
    url = f"{self.domain}/hardware/audio/"
    r = requests.post(
        url,
        data={
            "home": self.home_id,
            "uid": uid,
            "sequence_index": sequence_index,
            "audio_file": audio_file,
            "start_time": start_time,
            "end_time": end_time,
            "hardware_device_mac_address": self.mac_address,
            "track_id": track_id,
        },
        headers={"Authorization": f"Token {self.token}"},
        timeout=30,
    )
    logger.info(f"POST {url} {r.status_code}")
    if r.status_code != 201:
        return None
    return r.json()

post_video(uid, video_file, start_time, end_time)

Post metadata of the video to the API. Args: uid (str): uuid of this video section video_file (str): Path to the video file, which will be synced to the API disk storage via another parameter it will also hold the information in the file name about the start/end time start_time (datetime): The start time of the video end_time (datetime): The end time of the video Returns:

Source code in Client/Listener/api.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
def post_video(
    self, uid: str, video_file: str, start_time: datetime, end_time: datetime
):
    """
    Post metadata of the video to the API.
    Args:
        uid (str): uuid of this video section
        video_file (str): Path to the video file, which will be synced to the API disk storage via another parameter
                          it will also hold the information in the file name about the start/end time
        start_time (datetime): The start time of the video
        end_time (datetime): The end time of the video
    Returns:

    """
    url = f"{self.domain}/hardware/video/"
    data = {
        "home": self.home_id,
        "uid": uid,
        "hardware_device_mac_address": self.mac_address,
        "video_file": video_file,
        "start_time": start_time.isoformat(),
        "end_time": end_time.isoformat(),
    }
    logger.info(data)
    r = requests.post(
        url, data=data, headers={"Authorization": f"Token {self.token}"}, timeout=30
    )
    logger.info(f"POST {url} {r.status_code}")
    if r.status_code != 200:
        return None
    return r.json()

queue_speech_to_text(uid, audio_index, start_time, end_time)

Optional, used to queue the speech to text task Args: uid (str): uuid of the audio audio_index (str): The audio index, which can be used to identify the audio start_time (datetime): The start time of the audio end_time (datetime): The end time of the audio

Returns:

Type Description
str

The track id of the task

Source code in Client/Listener/api.py
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
def queue_speech_to_text(
    self, uid: str, audio_index: str, start_time: datetime, end_time: datetime
) -> str:
    """
    Optional, used to queue the speech to text task
    Args:
        uid (str): uuid of the audio
        audio_index (str): The audio index, which can be used to identify the audio
        start_time (datetime): The start time of the audio
        end_time (datetime): The end time of the audio

    Returns:
        (str): The track id of the task

    """
    track_id = self.set_track_id()
    url = f"{self.domain}/queue_task/ai_task/"
    data = {
        "name": "speech_to_text",
        "task_name": "speech2text",
        "parameters": json.dumps(
            {
                "uid": uid,
                "home_id": self.home_id,
                "audio_index": audio_index,
                "start_time": start_time.isoformat(),
                "end_time": end_time.isoformat(),
                "hardware_device_mac_address": self.mac_address,
            }
        ),
        "track_id": track_id,
    }
    r = requests.post(
        url, data=data, headers={"Authorization": f"Token {self.token}"}, timeout=30
    )
    logger.info(f"POST {url} {r.status_code}")
    if r.status_code != 200:
        logger.info(data)
        return None
    logger.info(r.json())
    return track_id

register_device(device_name=None, device_type=None, description=None)

Register the device to the API. Args: device_name (Optional[str]): The device name, you can name it if you want to distinguish it better later device_type (Optional[str]): The device type, this can be used to distinguish the device type description (Optional[str]): The description of the device

Returns:

Source code in Client/Listener/api.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def register_device(
    self,
    device_name: Optional[str] = None,
    device_type: Optional[str] = None,
    description: Optional[str] = None,
):
    """
    Register the device to the API.
    Args:
        device_name (Optional[str]): The device name, you can name it if you want to distinguish it better later
        device_type (Optional[str]): The device type, this can be used to distinguish the device type
        description (Optional[str]): The description of the device

    Returns:

    """
    url = f"{self.domain}/hardware/register/"

    r = requests.post(
        url,
        data={
            "home": self.home_id,
            "mac_address": self.mac_address,
            "device_name": device_name,
            "device_type": device_type,
            "description": description,
        },
        headers={"Authorization": f"Token {self.token}"},
        timeout=30,
    )
    logger.info(url)

    logger.info(f"POST {url} {r.status_code}")

upload_file(source_file, dest_path)

Upload the file to the API

Source code in Client/Listener/api.py
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
def upload_file(
    self,
    source_file: str,
    dest_path: str,
):
    """
    Upload the file to the API
    """
    url = f"{self.domain}/hardware/upload_file/"
    files = {"file": open(source_file, "rb")}
    data = {
        "dest_path": dest_path,
    }
    r = requests.post(
        url,
        files=files,
        data=data,
        headers={"Authorization": f"Token {self.token}"},
        timeout=30,
    )
    logger.info(f"POST {url} {r.status_code}")
    if r.status_code != 200:
        return None
    return True