Spaces:
Sleeping
Sleeping
Update firestore_twilio_listener.py
Browse files- firestore_twilio_listener.py +83 -60
firestore_twilio_listener.py
CHANGED
@@ -1,60 +1,83 @@
|
|
1 |
-
from twilio.rest import Client
|
2 |
-
import firebase_admin
|
3 |
-
from firebase_admin import credentials, firestore
|
4 |
-
|
5 |
-
# Twilio Configuration
|
6 |
-
ACCOUNT_SID = 'AC97ad50edcf94fdd7d4576be9651bf4bf'
|
7 |
-
AUTH_TOKEN = '6d8b2559ffbbdbc5d06542e545220aaa'
|
8 |
-
FROM_PHONE = '+12708175529'
|
9 |
-
TO_PHONE = '+916382792828'
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from twilio.rest import Client
|
2 |
+
import firebase_admin
|
3 |
+
from firebase_admin import credentials, firestore
|
4 |
+
|
5 |
+
# Twilio Configuration
|
6 |
+
ACCOUNT_SID = 'AC97ad50edcf94fdd7d4576be9651bf4bf'
|
7 |
+
AUTH_TOKEN = '6d8b2559ffbbdbc5d06542e545220aaa'
|
8 |
+
FROM_PHONE = '+12708175529'
|
9 |
+
TO_PHONE = '+916382792828'
|
10 |
+
import numpy as np
|
11 |
+
import joblib
|
12 |
+
|
13 |
+
# Load the saved Gradient Boosting model
|
14 |
+
loaded_model = joblib.load('dlmodel.pkl')
|
15 |
+
|
16 |
+
def predict_conc(pdiff):
|
17 |
+
if pdiff>3.19:
|
18 |
+
return 0
|
19 |
+
pdiff_value = pdiff # Replace 1.5 with your input value for "Average Potential Difference"
|
20 |
+
new_data = np.array([[pdiff_value]])
|
21 |
+
prediction = loaded_model.predict(new_data)
|
22 |
+
concentration = 10**prediction[0] # Reverse log transformation
|
23 |
+
return f"{concentration:.4f}"
|
24 |
+
|
25 |
+
|
26 |
+
# Firebase Configuration
|
27 |
+
cred = credentials.Certificate("snippetscript-37175-firebase-adminsdk-cf1z8-7d509b09fd.json")
|
28 |
+
firebase_admin.initialize_app(cred)
|
29 |
+
db = firestore.client()
|
30 |
+
|
31 |
+
# Function to send SMS using Twilio
|
32 |
+
def send_msg(field1,suggestion):
|
33 |
+
# if field;1
|
34 |
+
|
35 |
+
client = Client(ACCOUNT_SID, AUTH_TOKEN)
|
36 |
+
try:
|
37 |
+
message = client.messages.create(
|
38 |
+
from_=FROM_PHONE,
|
39 |
+
body=f"""Test Results:\nE.coli Level: {field1} CFU/mL.
|
40 |
+
Status: safe
|
41 |
+
Suggestions:\n
|
42 |
+
{suggestion}""",
|
43 |
+
to=TO_PHONE
|
44 |
+
)
|
45 |
+
print(f"Message sent successfully! SID: {message.sid}")
|
46 |
+
except Exception as e:
|
47 |
+
print(f"Failed to send message: {e}")
|
48 |
+
|
49 |
+
# Firestore Listener for new data in the 'thingspeak_data' collection
|
50 |
+
def on_snapshot(doc_snapshot, changes, read_time):
|
51 |
+
for change in changes:
|
52 |
+
if change.type.name == 'ADDED': # Detects new documents added to the collection
|
53 |
+
new_data = change.document.to_dict()
|
54 |
+
field1 = new_data.get('field1', 'N/A') # Replace 'field1' with your actual field key
|
55 |
+
print(f"New data detected: {new_data}")
|
56 |
+
concentration=predict_conc(field1)
|
57 |
+
if concentration==0:
|
58 |
+
send_msg(concentration,"The water is safe for use!!")
|
59 |
+
|
60 |
+
else:
|
61 |
+
send_msg(concentration,"You may boil the water to 50°C to eradicate the bacteria.")
|
62 |
+
|
63 |
+
# Trigger the Twilio SMS function
|
64 |
+
|
65 |
+
# Initialize Firestore Listener
|
66 |
+
def start_firestore_listener():
|
67 |
+
thingspeak_ref = db.collection('thingspeak_data')
|
68 |
+
thingspeak_watch = thingspeak_ref.on_snapshot(on_snapshot)
|
69 |
+
print("Listening for new data in Firestore...")
|
70 |
+
|
71 |
+
if __name__ == "__main__":
|
72 |
+
try:
|
73 |
+
# Start the listener
|
74 |
+
start_firestore_listener()
|
75 |
+
|
76 |
+
# Keep the script running to listen for changes
|
77 |
+
import time
|
78 |
+
while True:
|
79 |
+
time.sleep(10)
|
80 |
+
except KeyboardInterrupt:
|
81 |
+
print("\nListener stopped.")
|
82 |
+
except Exception as e:
|
83 |
+
print(f"An error occurred: {e}")
|