2017-07-17 23 views
0

我有这个应用程序的一些麻烦。我需要的是,如果我检测到数据库(FIREBASE)的变化,特别是在'sala'和'ventilacion'节点中,该功能就会做它必须做的事情。如果数据库中没有任何变化,它不会无所事事。我正在使用python和pyrebase库。这是代码。非常感谢您的帮助。如何使用python检测firebase孩子的变化?

  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() 

回答

1

问题:如何检测孩子火力变化


注意:所有的例子使用公共访问

  1. 设置示例数据并验证它是否可读。
    这是要做的一旦

    enter image description here

    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()) 
    
  2. 创建的第一个Script做更新:

    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) 
    
  3. 与流处理器创建第二个脚本

    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 
    
  4. 运行流处理器脚本:

    def stream_handler启动后

    响应输出(初始数据):

    event="put"; path=/; data={'Device_1': {'temperature': 30, 'time': '13:34:24', 'date': '2017-07-20'}} 
    
  5. 运行更新脚本:从流处理器脚本

    响应输出

  6. 观看输出def stream_handler首先更新数据:

    event=patch; path=/Device_1; data={'temperature': 25, 'time': '13:49:12'} 
    

测试使用Python 3.4.2


Pyrebase

您可以聆听现场更改您的数据流()方法。

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) 

您应该至少处理放置和修补事件。有关详细信息,请参阅“从REST API进行流式传输”。

+0

嗨stofvl,感谢您的快速回答,我已经看到pyrebase库中的流方法,但不幸的是我不明白如何使用它。你能帮我用这个吗?我的firebase数据库没有任何路径。我有名叫'sala'和'ventilacion'的孩子。在这种情况下,我想检测孩子萨拉的变化。再次感谢您的帮助。 – JoaquinMiguens

+0

@JoaquinMiguens:阅读[rest/database /#section-streaming](https://firebase.google.com/docs/reference/rest/database/#section-streaming)并相应地设置你的'sala'。 – stovfl

+0

阅读文档后,我无法使其工作。我想如果有人能帮助我或指导我如何使其工作。谢谢。 – JoaquinMiguens