Skip to content

Commit

Permalink
Merge pull request #4 from Hanagotchi/HAN-6/simulate-data
Browse files Browse the repository at this point in the history
Han 6/simulate data
  • Loading branch information
Agustinefe authored Jan 20, 2024
2 parents b0757b6 + b1a5eab commit 3b95414
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 48 deletions.
173 changes: 132 additions & 41 deletions src/data_packet.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,105 @@
import random as rnd
import requests as req
from requests import Response
from dotenv import load_dotenv
from os import environ
from typing import Tuple
from datetime import datetime
import math

'''
Creates a data packet with simulated data, validating the data before that.
load_dotenv()

If all the parameters are empty, returns None. Else, return a dictionary with the data.
def fetch_temperature_and_humidity(location: str) -> Tuple[int, int]:

base_params = {
"q": location,
"APPID": environ['WEATHER_API_KEY']
}
res: Response = req.get(environ["WEATHER_API_URL"], params=base_params)

if not res.ok:
raise Exception(
"Could fetch temperature and humidity from weather API"
)

result = res.json()
temperature = int(result["main"]["temp"] - 273.15)
humidity = result["main"]["humidity"]

return temperature, humidity


def get_decimal_hour():
current_hour = datetime.now()
return current_hour.hour + (
60 * current_hour.minute + current_hour.second
) / 3600


def solar_irradiation_simulator(x):
if x < 7 or x > 19:
return 0

x += 14
x *= (math.pi / 6)
x = math.sin(x)
x += 1
x *= 500
return x


def fetch_solar_irradiation():
x = get_decimal_hour()
return round(solar_irradiation_simulator(x))


def watering_simulator(x):
x %= 6
x /= 6
x *= 100
x = 100 - x
return x


def fetch_watering():
x = get_decimal_hour()
return round(watering_simulator(x))


def create_packet(temperature: int = None,
humidity: int = None,
light: int = None,
watering: int = None):
'''
Creates a data packet with simulated data, validating the data before that.
Constraints:
- Humidity and Watering should be between 0 and 100, since they are percentages.
- Light has to be positive or 0.
'''
If all the parameters are empty, returns None. Else, return a dictionary
with the data.
Constraints:
- Humidity and Watering should be between 0 and 100, since they are
percentages.
- Light has to be positive or 0.
'''

if temperature is None and humidity is None and light is None and watering is None:
return None

if humidity < 0 or humidity > 100:
raise Exception(
f"Humidity has to be between 0 and 100. Current value: {humidity}"
)

if watering < 0 or watering > 100:
raise Exception(
f"Watering has to be between 0 and 100. Current value: {watering}"
)

if light < 0:
raise Exception(
f"Light has to be positive or 0. Current value: {light}"
)

def create_packet(temperature: float = None, humidity: float = None, light: float = None, watering: float = None):
if not (temperature and humidity and light and watering):
return None
Expand All @@ -25,6 +113,7 @@ def create_packet(temperature: float = None, humidity: float = None, light: floa
if light < 0:
raise Exception(f"Light has to be positive or 0. Current value: {light}")


return {
"temperature": temperature,
"humidity": humidity,
Expand All @@ -33,45 +122,47 @@ def create_packet(temperature: float = None, humidity: float = None, light: floa
}


'''
Generates the parameters data: temperature, humidity, light and watering.
[Describe the criteria of every parameter here]
-
-
-
'''


def generate_data():
# TODO
temperature = rnd.randint(-5, 30)
humidity = rnd.randint(0, 100)
light = rnd.randint(0, 400)
watering = rnd.randint(0, 100)

return temperature, humidity, light, watering


'''
Compares the current packet and the last sent packet, based in the deviations.
If any parameter differs enough from the last sent packet, then the packet
must be sent.
def generate_data(location="Pilar, AR") -> Tuple[int, int, int, int]:
'''
Generates the parameters data: temperature, humidity, light and watering.
In other words: if (|current[parameter] - last_sent[parameter]| > deviations[parameter]), return True.
Else, return False
[Describe the criteria of every parameter here]
-
-
-
'''

If there is no last_sent, then the current must be sent.
temperature, humidity = fetch_temperature_and_humidity(location)
light = fetch_solar_irradiation()
watering = fetch_watering()

Types:
- current: {temperature: float, humidity: float, light: float, watering: float}
- last_sent: {temperature: float, humidity: float, light: float, watering: float}
- deviations: {temperature: float, humidity: float, light: float, watering: float}
'''
return temperature, humidity, light, watering


def data_has_changed(current, last_sent, deviations):
'''
Compares the current packet and the last sent packet,
based in the deviations.
If any parameter differs enough from the last sent packet, then the packet
must be sent.
In other words:
if (|current[parameter] - last_sent[parameter]| > deviations[parameter]),
return True.
Else, return False
If there is no last_sent, then the current must be sent.
Types:
- current: {temperature: float, humidity: float, light: float,
watering: float}
- last_sent: {temperature: float, humidity: float, light: float,
watering: float}
- deviations: {temperature: float, humidity: float, light: float,
watering: float}
'''

if not last_sent:
return True

Expand Down
14 changes: 7 additions & 7 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
- Enviar paquete a la queue.
'''

import time, random
import time
import random
import logging

from data_packet import generate_data, create_packet, data_has_changed


Expand All @@ -29,18 +29,18 @@ def simulate_packets(config):
logging.info(f"Packet sent: {current_packet}")
last_sent_packet = current_packet


except Exception as err:
logging.warning(err)
finally:
print(current_packet)
time.sleep(config["packet_period"])


'''
Reads the config file. At this moment, it is mocked.
'''


def read_config_file(path):
'''
Reads the config file. At this moment, it is mocked.
'''
# TODO
return {
"packet_period": 1,
Expand Down
2 changes: 2 additions & 0 deletions src/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
python-dotenv

0 comments on commit 3b95414

Please sign in to comment.