我在使用这个应用程序时遇到了一些问题。我需要的是,如果我检测到数据库 (FIREBASE) 发生变化,特别是在“sala”和“ventilacion”节点中,该函数将执行它必须执行的操作。如果数据库中没有任何变化,它就什么都不做。我正在使用 python 和 pyrebase 库。这是代码。非常感谢您的帮助。
import pyrebase
import serial
import time
config = {
#firebase configurations
}
firebase = pyrebase.initialize_app(config)
db = firebase.database()
def ReconfiguracionFabrica():
ser.write('AT')
time.sleep(0.2)
ser.write('AT+RENEW')
time.sleep(0.3)
def ConfiguracionMaster():
time.sleep(0.5)
ser.write('AT+IMME1')
time.sleep(0.350)
ser.write('AT+ROLE1')
time.sleep(0.2)
ser = serial.Serial(port="/dev/ttyAMA0", baudrate=9600, timeout=1)
ReconfiguracionFabrica()
time.sleep(0.1)
ConfiguracionMaster()
time.sleep(0.1)
print "********** INICIO *************"
ser.flushInput()
contador = 0
prender = ''
ventilacion1 = ''
checkeo = ''
while True:
#if db.child("sala").: # It is the line where would be the conditional that allows me to detect any change only in the sala's node.
salidaLed1 = db.child("sala").get()
ser.write('AT')
time.sleep(0.1)
ser.write('AT+CON508CB16A7014')
time.sleep(0.1)
if salidaLed1.val() == True:
prender = ";"
if salidaLed1.val() == False:
prender = ","
ser.write('luz: %s \n' %(prender))
print ('luz: %s \n' %(prender))
time.sleep(1)
ser.read(checkeo)
if checkeo == 'j':
ReconfiguracionFabrica()
time.sleep(0.1)
ConfiguracionMaster()
请您参考如下方法:
Question: How to detect changes in firebase child
Note: All Examples use Public Access
设置示例数据并验证其可读性。
此操作必须一次!temperature_c = 30 data = {'date':time.strftime('%Y-%m-%d'), 'time':time.strftime('%H:%M:%S'), 'temperature':temperature_c} db.child('public').child('Device_1').set(data) response = db.child('public').child('Device_1').get() print(response.val())创建第一个执行更新的脚本:
for t in [25, 26, 27, 28, 29, 30, 31, 32, 33, 35]: temperature_c = t data = {'date':time.strftime('%Y-%m-%d'), 'time':time.strftime('%H:%M:%S'), 'temperature':temperature_c} db.child('public').child('Device_1').update(data) time.sleep(60)使用流处理程序创建第二个脚本
def stream_handler(message): print('event={m[event]}; path={m[path]}; data={m[data]}' .format(m=message)) my_stream =db.child('public').child('Device_1').stream(stream_handler) # Run Stream Handler forever while True: data = input("[{}] Type exit to disconnect: ".format('?')) if data.strip().lower() == 'exit': print('Stop Stream Handler') if my_stream: my_stream.close() break运行流处理器脚本:
启动后
def stream_handler的响应输出(初始数据):event="put"; path=/; data={'Device_1': {'temperature': 30, 'time': '13:34:24', 'date': '2017-07-20'}}运行更新程序脚本:
观察流处理器脚本的输出
def stream_handler在第一次 更新数据后的响应输出:event=patch; path=/Device_1; data={'temperature': 25, 'time': '13:49:12'}
使用 Python 测试:3.4.2
Pyrebase
streamingYou can listen to live changes to your data with the stream() method.
def stream_handler(message): print(message["event"]) # put print(message["path"]) # /-K7yGTTEp7O549EzTYtI print(message["data"]) # {'title': 'Pyrebase', "body": "etc..."} my_stream = db.child("posts").stream(stream_handler)You should at least handle put and patch events. Refer to "Streaming from the REST API" for details.
