78 lines
1.7 KiB
Python
78 lines
1.7 KiB
Python
import firebase_admin, threading
|
|
from firebase_admin import credentials
|
|
from firebase_admin import firestore
|
|
|
|
class Light:
|
|
def __init__(self):
|
|
self.state = False
|
|
self.brightness = 0
|
|
self.colour = '000000'
|
|
self.cred = credentials.Certificate("secrets/de-iot-firebase-adminsdk-puz9e-7388634d69.json")
|
|
firebase_admin.initialize_app(self.cred)
|
|
self.db = firestore.client()
|
|
self.callback_done = threading.Event()
|
|
self.apiMonitor()
|
|
|
|
def apiMonitor(self):
|
|
self.doc_ref = self.db.collection(u'apiData').document(u'light')
|
|
self.doc_watch = self.doc_ref.on_snapshot(self.on_snapshot)
|
|
|
|
def on_snapshot(self, doc_snapshot, changes, read_time):
|
|
for doc in doc_snapshot:
|
|
if doc.id == 'light':
|
|
print('|')
|
|
doc = doc.to_dict()
|
|
state = doc['isOn']
|
|
brightness = doc['brightness']
|
|
colour = doc['color']
|
|
self.update(state=state, brightness=brightness, colour=colour)
|
|
self.callback_done.set()
|
|
|
|
|
|
def update(self, state=False, brightness=0, colour='000000'):
|
|
self.state = state
|
|
self.brightness = brightness
|
|
self.colour = colour
|
|
self.updateHW()
|
|
|
|
def updateHW(self):
|
|
print("Sending HW state: " + str(self.state))
|
|
self.changeState(self.state)
|
|
print("Sending HW brightness: " + str(self.state))
|
|
self.changeBrightness(self.brightness)
|
|
print("Sending HW colour: " + str(self.state))
|
|
self.changeColour(self.colour)
|
|
|
|
def changeState(self, state):
|
|
print("Toggling FET to: " + str(self.state))
|
|
|
|
def changeBrightness(self, brightness):
|
|
print("Toggling Brightness PWM to: " + str(brightness))
|
|
|
|
def changeColour(self, colour):
|
|
print("Toggling Colour to RGB: ", tuple(int(colour[i:i+2], 16) for i in (0, 2, 4)))
|
|
|
|
light = Light()
|
|
|
|
while True:
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|