Skip to content

Commit b0757b6

Browse files
authored
Merge pull request #3 from Hanagotchi/HAN-5
Criterio envio paquetes
2 parents 80e9363 + 41d9122 commit b0757b6

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

src/data_packet.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@
1010
- Humidity and Watering should be between 0 and 100, since they are percentages.
1111
- Light has to be positive or 0.
1212
'''
13+
14+
1315
def create_packet(temperature: float = None, humidity: float = None, light: float = None, watering: float = None):
1416
if not (temperature and humidity and light and watering):
1517
return None
16-
18+
1719
if humidity < 0 or humidity > 100:
1820
raise Exception(f"Humidity has to be between 0 and 100. Current value: {humidity}")
19-
21+
2022
if watering < 0 or watering > 100:
2123
raise Exception(f"Watering has to be between 0 and 100. Current value: {watering}")
22-
24+
2325
if light < 0:
2426
raise Exception(f"Light has to be positive or 0. Current value: {light}")
25-
27+
2628
return {
2729
"temperature": temperature,
2830
"humidity": humidity,
@@ -39,15 +41,18 @@ def create_packet(temperature: float = None, humidity: float = None, light: floa
3941
-
4042
-
4143
'''
44+
45+
4246
def generate_data():
43-
#TODO
47+
# TODO
4448
temperature = rnd.randint(-5, 30)
4549
humidity = rnd.randint(0, 100)
4650
light = rnd.randint(0, 400)
4751
watering = rnd.randint(0, 100)
4852

4953
return temperature, humidity, light, watering
5054

55+
5156
'''
5257
Compares the current packet and the last sent packet, based in the deviations.
5358
@@ -64,10 +69,20 @@ def generate_data():
6469
- last_sent: {temperature: float, humidity: float, light: float, watering: float}
6570
- deviations: {temperature: float, humidity: float, light: float, watering: float}
6671
'''
67-
def current_packet_differs_from_last_sent(current, last_sent, deviations):
68-
#TODO
6972

73+
74+
def data_has_changed(current, last_sent, deviations):
7075
if not last_sent:
7176
return True
7277

73-
return True
78+
if parameter_has_changed(current["temperature"], last_sent["temperature"], deviations["temperature"])\
79+
or parameter_has_changed(current["humidity"], last_sent["humidity"], deviations["humidity"])\
80+
or parameter_has_changed(current["light"], last_sent["light"], deviations["light"])\
81+
or parameter_has_changed(current["watering"], last_sent["watering"], deviations["watering"]):
82+
return True
83+
84+
return False
85+
86+
87+
def parameter_has_changed(current, last, deviation):
88+
return abs(current - last) > deviation

src/main.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,36 @@
1212
import time, random
1313
import logging
1414

15-
from data_packet import generate_data, create_packet, current_packet_differs_from_last_sent
15+
from data_packet import generate_data, create_packet, data_has_changed
16+
1617

1718
def simulate_packets(config):
1819
last_sent_packet = None
1920
while True:
2021
try:
2122
temperature, humidity, light, watering = generate_data()
2223
current_packet = create_packet(temperature, humidity, light, watering)
23-
24-
if not current_packet:
25-
continue
2624

27-
if current_packet_differs_from_last_sent(current_packet, last_sent_packet, config["deviations"]):
28-
# TODO: Send packet to the RabbitMQ queue
29-
logging.info(f"Packet sent: {current_packet}")
30-
31-
last_sent_packet = current_packet
25+
if not current_packet or not data_has_changed(current_packet, last_sent_packet, config["deviations"]):
26+
continue
3227

28+
# TODO: Send packet to the RabbitMQ queue
29+
logging.info(f"Packet sent: {current_packet}")
30+
last_sent_packet = current_packet
3331

3432
except Exception as err:
3533
logging.warning(err)
3634
finally:
3735
time.sleep(config["packet_period"])
38-
36+
3937

4038
'''
4139
Reads the config file. At this moment, it is mocked.
4240
'''
41+
42+
4343
def read_config_file(path):
44-
#TODO
44+
# TODO
4545
return {
4646
"packet_period": 1,
4747
"device_id": str(random.getrandbits(128)),
@@ -50,8 +50,9 @@ def read_config_file(path):
5050
"humidity": 5,
5151
"light": 25,
5252
"watering": 5
53+
}
5354
}
54-
}
55+
5556

5657
if __name__ == '__main__':
5758
logging.basicConfig(level=logging.INFO)

0 commit comments

Comments
 (0)