iot-logic/src/sender/soundsender.py

46 lines
1.3 KiB
Python
Raw Normal View History

2020-08-24 20:38:38 +02:00
#!/usr/bin/env python3
2020-08-25 01:40:09 +02:00
import os
import json
import logging
from datetime import datetime
import requests
2020-08-24 20:38:38 +02:00
from .abcsender import AbcSender
2020-08-25 01:40:09 +02:00
from utils import config
2021-11-30 20:09:00 +01:00
from urllib.parse import urljoin
2020-08-24 20:38:38 +02:00
"""
Send a sound sample
"""
__author__ = "@tormakris"
__copyright__ = "Copyright 2020, Birbnetes Team"
__module_name__ = "soundsender"
__version__text__ = "1"
class SoundSender(AbcSender):
"""
SoundSender class, responsible for sending sound samples to the cloud.
"""
2020-08-25 01:40:09 +02:00
def sendvalue(self, value: str, decision: bool) -> None:
2020-08-24 20:38:38 +02:00
"""
Send a sound sample to the cloud.
2021-11-18 23:23:46 +01:00
value: is the file name
decision: if true the sample is sent, if false then not
2020-08-24 20:38:38 +02:00
:return:
"""
2020-08-25 01:40:09 +02:00
if decision:
files = {
"file": (os.path.basename(value), open(value, 'rb').read(), 'audio/wave',
{'Content-length': os.path.getsize(value)}),
"description": (
None, json.dumps({'date': datetime.now().isoformat(), 'device_id': config.DEVICE_ID}),
"application/json")
}
2021-12-11 00:19:07 +01:00
r = requests.post(urljoin(config.API_URL, config.FEED_TYPE), files=files)
2020-09-30 06:27:26 +02:00
logging.debug(f"Content: {r.content.decode()}")
logging.debug(f"Headers: {r.headers}")
2020-08-25 01:40:09 +02:00
r.raise_for_status()