Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -98,6 +98,112 @@ def logout():
|
|
98 |
session.pop('user_phone', None)
|
99 |
return redirect(url_for('index'))
|
100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
if __name__ == '__main__':
|
102 |
start_firestore_listener()
|
103 |
app.run(host='0.0.0.0', port=7860, debug=True)
|
|
|
98 |
session.pop('user_phone', None)
|
99 |
return redirect(url_for('index'))
|
100 |
|
101 |
+
|
102 |
+
|
103 |
+
import requests
|
104 |
+
import firebase_admin
|
105 |
+
from firebase_admin import credentials, firestore
|
106 |
+
import time
|
107 |
+
|
108 |
+
# Replace with your actual credentials
|
109 |
+
THINGSPEAK_API_KEY = "P54KXM40TA3CB6W4"
|
110 |
+
CHANNEL_ID = "2784385"
|
111 |
+
FIREBASE_CREDENTIALS_PATH = r"snippetscript-37175-firebase-adminsdk-cf1z8-7d509b09fd.json"
|
112 |
+
|
113 |
+
# Global variable to track the last entry ID
|
114 |
+
last_entry_id = None
|
115 |
+
|
116 |
+
def initialize_firebase():
|
117 |
+
"""Initialize Firebase connection"""
|
118 |
+
try:
|
119 |
+
# Initialize Firebase app (only once)
|
120 |
+
cred = credentials.Certificate(FIREBASE_CREDENTIALS_PATH)
|
121 |
+
firebase_admin.initialize_app(cred)
|
122 |
+
|
123 |
+
# Create Firestore client
|
124 |
+
return firestore.client()
|
125 |
+
except Exception as e:
|
126 |
+
print(f"Firebase initialization error: {e}")
|
127 |
+
return None
|
128 |
+
|
129 |
+
def fetch_thingspeak_data():
|
130 |
+
"""Fetch data from ThingSpeak"""
|
131 |
+
global last_entry_id
|
132 |
+
try:
|
133 |
+
url = f"https://api.thingspeak.com/channels/2784385/feeds.json?api_key=P54KXM40TA3CB6W4"
|
134 |
+
response = requests.get(url)
|
135 |
+
response.raise_for_status() # Raise an exception for bad responses
|
136 |
+
data = response.json()
|
137 |
+
|
138 |
+
# Check if there are feeds
|
139 |
+
if not data.get('feeds'):
|
140 |
+
print("No new feeds found.")
|
141 |
+
return None
|
142 |
+
|
143 |
+
# Get the latest feed
|
144 |
+
latest_feed = data['feeds'][-1]
|
145 |
+
|
146 |
+
# Check if this is a new entry
|
147 |
+
current_entry_id = latest_feed.get('entry_id')
|
148 |
+
|
149 |
+
if current_entry_id != last_entry_id:
|
150 |
+
last_entry_id = current_entry_id
|
151 |
+
return latest_feed
|
152 |
+
|
153 |
+
print("No new entries since last check.")
|
154 |
+
return None
|
155 |
+
|
156 |
+
except requests.RequestException as e:
|
157 |
+
print(f"ThingSpeak data fetch error: {e}")
|
158 |
+
return None
|
159 |
+
|
160 |
+
def store_data_in_firestore(firestore_client, data):
|
161 |
+
"""Store data in Firestore"""
|
162 |
+
try:
|
163 |
+
# Validate data
|
164 |
+
if not data:
|
165 |
+
print("No data to store.")
|
166 |
+
return
|
167 |
+
|
168 |
+
# Create a document with timestamp
|
169 |
+
doc_data = {
|
170 |
+
'field1': data.get('field1'),
|
171 |
+
'field2': data.get('field2'),
|
172 |
+
'entry_id': data.get('entry_id'),
|
173 |
+
'timestamp': firestore.SERVER_TIMESTAMP
|
174 |
+
}
|
175 |
+
|
176 |
+
# Add to Firestore collection
|
177 |
+
firestore_client.collection('thingspeak_data').add(doc_data)
|
178 |
+
print("New data successfully stored in Firestore")
|
179 |
+
except Exception as e:
|
180 |
+
print(f"Firestore storage error: {e}")
|
181 |
+
|
182 |
+
|
183 |
+
firestore_client = initialize_firebase()
|
184 |
+
|
185 |
+
if not firestore_client:
|
186 |
+
print("Failed to initialize Firestore. Exiting.")
|
187 |
+
return
|
188 |
+
|
189 |
+
# Main data collection loop
|
190 |
+
try:
|
191 |
+
while True:
|
192 |
+
# Fetch data from ThingSpeak
|
193 |
+
thingspeak_data = fetch_thingspeak_data()
|
194 |
+
|
195 |
+
if thingspeak_data:
|
196 |
+
# Store data in Firestore only if there's a new entry
|
197 |
+
store_data_in_firestore(firestore_client, thingspeak_data)
|
198 |
+
|
199 |
+
# Wait before next iteration
|
200 |
+
time.sleep(5) # Adjust interval as needed
|
201 |
+
|
202 |
+
except KeyboardInterrupt:
|
203 |
+
print("Data collection stopped by user.")
|
204 |
+
except Exception as e:
|
205 |
+
print(f"Unexpected error: {e}")
|
206 |
+
|
207 |
if __name__ == '__main__':
|
208 |
start_firestore_listener()
|
209 |
app.run(host='0.0.0.0', port=7860, debug=True)
|