iot-logic/src/sender/soundsender.py

43 lines
1.2 KiB
Python

#!/usr/bin/env python3
import os
import json
import logging
from datetime import datetime
import requests
from .abcsender import AbcSender
from utils import config
"""
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.
"""
def sendvalue(self, value: str, decision: bool) -> None:
"""
Send a sound sample to the cloud.
:return:
"""
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")
}
r = requests.post(config.API_URL + "/sample", files=files)
logging.info("Content: ", r.content)
logging.info("Headers:", r.headers)
r.raise_for_status()