ThalamOS
a powerful Flask web application designed to enhance your storage management.
Loading...
Searching...
No Matches
weigh_fi_manager.py
Go to the documentation of this file.
1"""
2wifiscalemanager.py
3
4This module provides functionality to interact with a WiFi-enabled scale.
5It retrieves the weight from the scale by sending a GET request to the specified API endpoint.
6
7Functions:
8 get_weight(): Retrieves the weight of the scale.
9
10Environment Variables:
11 SCALE_HOST: The hostname or IP address of the WiFi scale, loaded from a .env file.
12
13Dependencies:
14 - requests: To send HTTP requests.
15 - os: To handle file paths and environment variables.
16 - dotenv: To load environment variables from a .env file.
17"""
18import os
19from typing import Annotated
20import requests
21from dotenv import load_dotenv
22
23from logger_config import logger
24
25
26ENV_PATH: Annotated[str, "path to environment variables"] = os.path.join(
27 os.path.dirname(__file__), "data/.env"
28)
29load_dotenv(dotenv_path=ENV_PATH)
30
31SCALE_HOST: Annotated[str, "environment variable for wifi scale address"] = os.getenv(
32 "SCALE_HOST"
33)
34logger.info(f"WifiScale host is: {SCALE_HOST}")
35
36
37def get_weight() -> Annotated[float, "weight in grams"]:
38 """
39 Retrieve the weight of the scale.
40 This function sends a GET request to the specified API endpoint
41 to retrieve the weight of the scale.
42 Returns:
43 float: The weight of the scale.
44 """
45 api = f"http://{SCALE_HOST}/getWeight"
46 try:
47 response = requests.get(api, timeout=10)
48 response.raise_for_status()
49 except requests.RequestException as e:
50 logger.error(f"Error retrieving weight: {e}")
51 return "ERROR SCALE NOT FOUND"
52 weight = response.content.decode("utf-8")
53 return weight
Annotated[float, "weight in grams"] get_weight()