Skip to content

Commit b926d38

Browse files
committed
send push notification
1 parent c222cf8 commit b926d38

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

app/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
from service.calculator_service import CalculatorService
77
from router import api_router
88

9+
910
app = FastAPI()
1011
service = CalculatorService()
1112
controller = CalculatorController(service)
1213

14+
15+
1316
logger = logging.getLogger("measurements")
1417
logger.setLevel("DEBUG")
1518

app/main_rabbitmq.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import os
22
import logging
33
from service.rabbitmq.consumer import Consumer
4-
4+
import firebase_admin
5+
from firebase_admin import credentials
56

67
def main():
8+
cred = credentials.Certificate("firebase_credentials.json")
9+
firebase_admin.initialize_app(cred)
710
logger = logging.getLogger("rabbitmq_consumer")
811
# DEBUG, INFO, WARNING, ERROR, CRITICAL
912
logging_level = os.environ.get("LOGGING_LEVEL")

app/schemas/measurement.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ class MeasurementReadingSchema(BaseModel):
2323
watering: Optional[int]
2424
id_device: str
2525
time_stamp: str
26+
device_token: Optional[str]

app/service/rabbitmq/consumer.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from database.database import SQLAlchemyClient
1818
from resources.parser import apply_rules
1919
from os import environ
20+
from firebase_admin import messaging
2021

2122

2223
Base = declarative_base(
@@ -50,6 +51,7 @@ def obtain_device_plant(self, measurement_from_rabbit):
5051
logger.error(f"{err} - {type(err)}")
5152
raise RowNotFoundError(measurement_from_rabbit.id_device, "DEVICE_PLANT")
5253

54+
5355
def check_package(self, measurement):
5456
empty_values = []
5557
if measurement.temperature is None:
@@ -69,11 +71,16 @@ def check_package(self, measurement):
6971

7072
def send_notification(self, id_user, measurement, error, details):
7173
logger.info(LoggerMessages.USER_NOTIFIED.format(id_user))
72-
logger.info(
73-
"New notification: {}: {} sent on {}".format(
74-
error, details, measurement.time_stamp
75-
)
76-
)
74+
75+
print(f"details: {details}")
76+
print(f"error: {error}")
77+
78+
if measurement.device_token is not None:
79+
message = messaging.Message(
80+
notification=messaging.Notification(title="Estado de tu planta", body="details"),
81+
token=measurement.device_token,
82+
)
83+
messaging.send(message)
7784

7885
def apply_rules(self, measurement, device_plant):
7986
deviated_parameters = apply_rules(measurement, device_plant.plant_type)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ flake8
55
psycopg2
66
pandas
77
httpx
8+
firebase-admin
89

910
### RabbitMQ ###
1011
pika

0 commit comments

Comments
 (0)