-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
64 lines (53 loc) · 2.43 KB
/
app.py
1
2
3
4
5
6
7
8
9
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
61
62
63
64
#Created: 11/17/21
#Updated: 12/5/21
#Author: William Baltus
#This script is a very basic API. It connects to a raspberry pi, pulls temperature sensor data,
#which is collected separately and stored in a firebase database
#Populates dictionary, jsonify's string, and returns string.
#Route is programmed to handle GET requests
from flask import Flask, jsonify
import firebase_admin
from firebase_admin import credentials, firestore
from os import environ
from dotenv import load_dotenv
import json
from bme680 import getData
load_dotenv()
app = Flask(__name__)
deployed = environ.get('IS_HEROKU')
print(deployed)
if not firebase_admin._apps:
if( int(deployed) < 1):
print("RUNNING LOCAL, NOT HEROKU")
cred = credentials.Certificate(r"iot_smart_contract.json")
firebase_admin.initialize_app(cred)
else:
#create dictioanry containing environmental variables from heroku config vars
print("RUNNING HEROKU, NOT LOCAL")
credentialsDict = { "type": environ['FIREBASE_TYPE'],
"project_id":environ['FIREBASE_PROJECT_ID'],
"private_key_id":environ['FIREBASE_PRIVATE_KEY_ID'],
"private_key":environ['FIREBASE_PRIVATE_KEY'].replace("\\n", "\n"),
"client_email":environ['FIREBASE_CLIENT_ID'],
"client_id": environ['FIREBASE_CLIENT_ID'],
"auth_uri":environ['FIREBASE_AUTH_URI'],
"token_uri":environ['FIREBASE_TOKEN_URI'],
"auth_provider_x509_cert_url":environ['FIREBASE_AUTH_PROVIDER_X509_CERT_URL'],
"client_x509_cert_url":environ['FIREBASE_CLIENT_X509_CERT_URL']}
#print("ENV KEYS:", credentialsDict)
CREDENTIALS = credentials.Certificate(credentialsDict)
firebase_admin.initialize_app(CREDENTIALS)
firebase= firestore.client()
@app.route('/', methods=['GET'])
def index():
#data_ref = firebase.collection('12-05-21').document('temp')
#temp = getData['Temperature']
#data_ref.set(u'Temperature': temp)
data_ref = firebase.collection('12-05-21').document('08:00:08AM')
dataDict = data_ref.get().to_dict()
temp = dataDict['Temperature']
temp = float(temp)
return jsonify({'Device': 'Raspberry Pi',
'Temperature': temp })
if __name__ == '__main__':
app.run()