【问题标题】:Python: Cannot read returned values from functionsPython:无法从函数中读取返回值
【发布时间】:2018-05-12 11:37:50
【问题描述】:

我正在研究跌倒检测系统。我编写了 Arduino 代码并连接到 Firebase。所以现在我有两个状态为 1 或 0 的变量,并且我创建了一个移动应用程序,以便在系统检测到跌倒 Firebase+Pusher 时接收自动推送通知。我用 PyCharm 编写了这个 Python 代码,并使用流函数从 Firebase 读取实时数据并发送自动通知。该代码适用于变量“Fall_Detection_Status”,我能够在每次跌倒检测时正常接收推送通知。但我尝试修改代码以从另一个变量“Fall_Detection_Status1”读取数据,如果两个变量都给出 1,我希望我的代码现在发送通知。我想出了这段代码,但似乎最后一个 if 语句不起作用,因为我无法接收通知,并且 if 语句末尾的 print(response['publishId']) 没有显示任何结果。

那么有什么问题呢?

import pyrebase

from pusher_push_notifications import PushNotifications
config = {
    'apiKey': "***********************************",
    'authDomain': "arfduinopushnotification.firebaseapp.com",
    'databaseURL': "https://arduinopushnotification.firebaseio.com",
    'projectId': "arduinopushnotification",
    'storageBucket': "arduinopushnotification.appspot.com",
    'messagingSenderId': "************"
  }

firebase = pyrebase.initialize_app(config)
db = firebase.database()
pn_client = PushNotifications(
    instance_id='*****************************',
    secret_key='**************************',
)

value = 0
value1 = 0


def stream_handler(message):
    global value
    print(message)
    if message['data'] is 1:
        value = message['data']
    return value


def stream_handler1(message):
    global value1
    print(message)
    if message['data'] is 1:
        value1 = message['data']
    return value1


if value == 1 & value1 == 1:
        response = pn_client.publish(
            interests=['hello'],
            publish_body={
                'apns': {
                    'aps': {
                        'alert': 'Hello!',
                    },
                },
                'fcm': {
                 'notification': {
                    'title': 'Notification',
                    'body': 'Fall Detected !!',
                    },
                },
            },
        )
        print(response['publishId'])


my_stream = db.child("Fall_Detection_Status").stream(stream_handler)
my_stream1 = db.child("Fall_Detection_Status1").stream(stream_handler1)

【问题讨论】:

  • 如果您已将其设为全局,则不需要返回该值。尝试取出返回值。这可能会有所帮助
  • 还是不行
  • 也许消息是str .. 试试value = int(message['data'])
  • 也可以尝试在if 语句中使用and 而不是&
  • 没用 :(

标签: python firebase firebase-realtime-database push-notification


【解决方案1】:

您使用了错误的运算符“&”来组合两个测试的结果。在 Python 中,'&' 是按位与运算符!我相信您想要“和”的逻辑版本。

其次,假设 stream_handler/1 调用由您的最后两个语句运行,那么这两个语句位于您测试 if 语句中的值的位置之后。将这些行移到 if 块上方。

【讨论】:

    猜你喜欢
    • 2022-01-27
    • 2013-04-22
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多