您好,我有这个连接到树莓派的 Firebase 设置,该树莓派根据 Firebase 数据库朗读文本。我的功能每 5 秒运行一次,我想检查是否有新消息。我将每条消息的时间存储在数据库中,我现在的方式是检查新消息和旧消息的时间是否相同。
我的两个问题是:
有没有更好的方法来检查消息时间,看看消息是否是新的?
如何修复此代码,以免出现UnboundLocalError: local variable 'the_time' referenced before assignment"
错误
这是我的代码
import time
import subprocess
from firebase import firebase
firebase = firebase.FirebaseApplication('----', None)
message = firebase.get('/message', None)
name = firebase.get('/name', None)
the_time = firebase.get('the_time',None)
speak_message = message+" from "+ name
def showmessage():
message=firebase.get('/message',None)
name=firebase.get('/name',None)
current_time = firebase.get('/the_time',None)
speak_message=message+' from '+name
#this is to set the audio jack on raspi
subprocess.call(['amixer','cset','numid=3','1'])
if current_time == the_time:
#message is NOT new
print 'message is NOT new'
elif current_time != the_time:
#message IS new
#Shell script to run text-to-speech
subprocess.call(['/home/pi/./speech.sh',speak_message])
the_time = current_time
time.sleep(5)
while True:
showmessage()