-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase_io.py
77 lines (65 loc) · 2.05 KB
/
firebase_io.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
65
66
67
68
69
70
71
72
73
74
75
76
77
# importing all required modules
import time
from firebase import firebase
# Getting Firebase DB Path from firebase_link.conf
# Reading link from firebase_link.conf
try:
with open("/home/pi/Projects/WaterLevelSensor/firebase_link.conf","r") as fblink:
FIREBASE_PATH=fblink.readline().strip()
if not len(FIREBASE_PATH)>0:
raise Exception("File is empty!")
except:
print("firebase_link.conf file not found!\nOR, File is empty!")
# Setting up communication with the Firebase DB
# Change the FIREBASE_PATH variable in firebase_link.py file to connect to
# a different Firebase DB
firebase = firebase.FirebaseApplication(FIREBASE_PATH, authentication=None)
# Getting the status of the MAIN SWITCH
def is_main_switch_on():
main_switch = firebase.get('/main_switch', None)
if main_switch == "ON":
return True
else:
return False
# Getting the status of Motor Switch
def is_motor_switch_on():
motor_switch = firebase.get('/motor_switch', None)
if motor_switch == "ON":
return True
else:
return False
# Check if sump is present
def is_sump_present():
sump_present = firebase.get('/is_sump_present', None)
if sump_present == "YES":
return True
else:
return False
# Change the MOTOR SWITCH status in the FB-DB
def set_motor_switch_status(status):
if status:
print('Motor Switched ON')
firebase.put('/', "motor_switch", "ON")
else:
print('Motor Swtiched OFF')
firebase.put('/', "motor_switch", "OFF")
# Change the MAIN SWITCH status in the FB-DB
def set_main_switch_status(status):
if status:
firebase.put('/', "main_switch", "ON")
else:
firebase.put('/', "main_switch", "OFF")
# Put the changed data into FB-DB
def set_distance(distance):
firebase.put('/', 'distance', distance)
print('Distance Updated!')
# Fetch the distance from FB-DB
def get_distance():
return firebase.get('/distance', None)
# Fetch the distance_sump from FB-DB
def get_distance_sump():
return firebase.get('/distance_sump', None)
# Set the distance_sump into FB-DB
def set_distance_sump(distance):
firebase.put('/', 'distance_sump', distance)
print('Distance Sump updated!')