-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
78 lines (62 loc) · 2.54 KB
/
main.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
78
# * author : Philippe Vo
# * date : Mar-07-2020 12:32:55
# * Imports
# 3rd Party Imports
import time
# User Imports
from firebase.firebase_plant_com import FirebasePlantCom
from plant.Sensors.sensor_controller import SensorController
from plant.plant import Plant
from ai.img_plant_controller import run_img_guessing
class MainRun:
def __init__(self):
"""
init.
"""
self.firebaseCOM = FirebasePlantCom(piID = "123")
# setinng up listener for image of plant
self.imgUpdatePiIDPath = self.firebaseCOM.piPath + "/imgUpdate"
self.imgPiIDPath = self.firebaseCOM.piPath + "/Image"
my_stream = self.firebaseCOM.db.child(self.imgUpdatePiIDPath).stream(self.stream_img_handler)
# NOTE : NEW
def stream_img_handler(self, message):
"""
monitors the imgs that gets taken and activates a function when there is a change
"""
# Get the image data
data = self.firebaseCOM.db.child(self.imgPiIDPath).get()
base64Img = data.val()
# Guess the plant
plantType = run_img_guessing(base64Img)
print("Plant guessed : ", plantType)
# write the type on firebase
self.firebaseCOM.db.child(self.firebaseCOM.piPath).update({"type": plantType})
# update imgUpdate
self.firebaseCOM.db.child(self.firebaseCOM.piPath).update({"imgUpdate": "true"})
# * Code
def main(self):
"""
starting point of the code
"""
print("Starting Pot-O-Duino ...")
# Sign in 2 Firebase
self.firebaseCOM = FirebasePlantCom(piID = "1")
# Make Plant and its SensorController
sensorController1 = SensorController(moisture=0, light=1, ph=-1, humidity = 0, temperature = 0)
plant0 = Plant(name=self.firebaseCOM.plantName, type_="Aloe", sensorController=sensorController1)
# Gather data and send 2 Firebase
print("Gathering data from sensors and sending to Firebase ...")
try:
while True:
time.sleep(0.01)
plant0.set_all_data()
plantData = plant0.get_all_data()
piIDPath = self.firebaseCOM.get_firebasePlantPath() # getting the path to the appropriate branch to update on firebase
self.firebaseCOM.update_pi_data(piIDPath, plantData)
plant0.print_plant()
if plant0.dry():
plant0.sensorController.watering()
except KeyboardInterrupt:
print('interrupted!')
if __name__ == '__main__':
main()