简体   繁体   English

Notify2从Python3 IDLE开始工作,但并不总是在终端中

[英]Notify2 works from Python3 IDLE but not always in terminal

I have been fighting with this code for days to get it to display notifications on a Raspbian Jessie desktop. 我一直在与该代码争斗好几天,以使其在Raspbian Jessie桌面上显示通知。 I have tried Notify, notify2, and notify-send and they all work from IDLE but not from not from the command line. 我尝试了Notify,notify2和notify-send,它们都从IDLE运行,但不是从命令行运行。 Notify crashed the python code, notify-send (called via subprocess.Popen) did nothing (works fine when called directly from terminal), notify2 worked for a while from the command line after running it from IDLE but eventually stops providing notifications. 通知使python代码崩溃,通知发送(通过subprocess.Popen调用)什么都没做(当直接从终端调用时工作正常),从IDLE运行它后,notify2在命令行工作了一段时间,但最终停止提供通知。 No errors are ever given. 没有错误。

I am using the notification system because it can take upwards of 10 seconds for the system to start the rtsp camera feed once the button is pushed and I want the user to know that their input was received. 我使用的是通知系统,因为一旦按下按钮,系统可能需要10秒钟以上的时间才能启动rtsp摄像机供稿,并且我希望用户知道收到了他们的输入。 If there is another way to popup a quick notification from Python, I'm open to ideas (tkinter?). 如果还有另一种方法可以从Python弹出快速通知,则可以公开提出想法(tkinter?)。

Code is called from a bash script called in LXDE-pi/autostart: 从在LXDE-pi / autostart中调用的bash脚本中调用代码:

#!/bin/sh
# launcher.sh
cd /
cd home/pi/py_switch
while true; do
             python3.4 buttons.py
             echo -----------------------RESTARTING CODE------------
         done
cd /

Here is buttons.py: 这是buttons.py:

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import schedule
import subprocess
import os
from subprocess import call
import notify2
import sys; print(sys.executable)
print(os.getcwd())
notify2.init("Buttons")

os.environ.setdefault('XAUTHORITY', '/home/user/.Xauthority')
os.environ.setdefault('DISPLAY', ':0.0')
GPIO.setmode(GPIO.BCM)

GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)   #*
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP)   #MIC
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)   #CAM
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)    #BRIGHTNESS
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)    #POWER


input_state1 = GPIO.input(26)
input_state2 = GPIO.input(19)
input_state3 = GPIO.input(13)
input_state4 = GPIO.input(6)
input_state5 = GPIO.input(5)
print(time.asctime( time.localtime(time.time())))
print('Button State: ', input_state1, input_state2, input_state3, input_state4, input_state5)
cam_state=False

n = notify2.Notification("Buttons!", "Waiting for you to push something.")
n.show()
brightness = str(subprocess.check_output(["ddcutil", "getvcp", "10"]))
brightness = brightness[brightness.find('=',4):brightness.find(',')]
brightness = brightness[1:].strip(' ')
if brightness == '0':
    disp_state=1
    print('Startup brightness is 0%')
elif brightness == '1':
    disp_state=0
    print('Display is off')
else:
    disp_state=2
    print('Startup brightness is >0%')  

def job():
    rc = subprocess.call(["/home/pi/py_switch/cams.sh", "repair"])
    print('Checking for down cameras')

def night():
    rc = subprocess.call(["ddcutil", "setvcp", "10", "1"])
    rc = subprocess.call(["ddcutil", "setvcp", "0xd6", "04"])
    disp_state=0
    print('Starting night mode')

def day():
    rc = subprocess.call(["ddcutil", "setvcp", "0xd6", "01"])
    rc = subprocess.call(["ddcutil", "setvcp", "10", "100"])
    disp_state=2
    print('Starting day mode')

schedule.every().day.at("23:30").do(night)
schedule.every().day.at("06:00").do(day)
schedule.every().wednesday.at("07:15").do(night)
schedule.every().thursday.at("07:15").do(night)

while True:
    input_state1 = GPIO.input(26)   #*
    input_state2 = GPIO.input(19)   #MIC
    input_state3 = GPIO.input(13)   #CAM
    input_state4 = GPIO.input(6)    #BRIGHTNESS
    input_state5 = GPIO.input(5)    #POWER

    if input_state3 == False:
        print('Camera Button Pressed')
        time.sleep(0.2)
        if cam_state == False:
            n = notify2.Notification("Starting Cameras", "May take up to 10 seconds...", "camera-video")
            rc = subprocess.call(["/home/pi/py_switch/cams.sh", "repair"]) #repair like start but won't double start if running
            cam_state=True
            schedule.every(3).minutes.do(job).tag('repair-cams')
        else:
            n = notify2.Notification("Stopping Cameras", "Standby...", "camera-video")
            rc = subprocess.call(["/home/pi/py_switch/cams.sh", "stop"])
            cam_state=False
            schedule.clear('repair-cams')
    if input_state4 == False:
        print('Brightness button Pressed')
#        rc = subprocess.Popen('DISPLAY=:0 notify-send "Changing Brightness..." "100% -> 0% -> Off ->" -i dialog-information', shell=True)
        n = notify2.Notification("Changing brightness", "100% -> 0% -> Off ->", "dialog-information")
        time.sleep(0.2)
        if disp_state == 3:
               rc = subprocess.call(["ddcutil", "setvcp", "10", "50"])
               disp_state=2
        elif disp_state == 2:
                rc = subprocess.call(["ddcutil", "setvcp", "10", "0"])
                disp_state=1
        elif disp_state == 1:
                rc = subprocess.call(["ddcutil", "setvcp", "10", "1"])
                rc = subprocess.call(["ddcutil", "setvcp", "0xd6", "04"])
                disp_state=0
        elif disp_state == 0:
                rc = subprocess.call(["ddcutil", "setvcp", "0xd6", "01"])
                rc = subprocess.call(["ddcutil", "setvcp", "10", "100"])
                disp_state=2 #changed to 2 to from 3 to bypass 50% brighness
    if input_state5 == False:
        print('Power button Pressed')
        start = time.time()
        time.sleep(0.2)
 #       rc = subprocess.Popen('DISPLAY=:0 notify-send "Hold for 5s to shutdown." -i system-shutdown', shell=True)
        n = notify2.Notification("Shutdown?", "Hold for 5s to shutdown.", "system-shutdown")
        while input_state5 == False:
            time.sleep(0.1)
            print('Holding down 3')
            end = time.time()
            press_time = end-start
           # print(press_time)
            input_state5 = GPIO.input(5)
            if press_time > 5:
                print('Shutting down?')
                os.system('sudo shutdown -h now')

    if input_state2 == False:
        print('Microphone button Pressed')
        time.sleep(0.2)
        #rc = subprocess.call(["ddcutil", "setvcp", "10", "100"])
    if input_state1 == False:
        print('Star button Pressed')
        time.sleep(0.2)
    time.sleep(0.1)
    schedule.run_pending()

Do I need to do something special to keep the notification daemon running? 我需要做一些特殊的事情来保持通知守护程序运行吗? I have notification-daemon, notify-osd and about every other package Google searches suggested I install: 我有notification-daemon,notify-osd以及有关我建议安装的所有其他Google搜索软件包的信息:

(zcat $(ls -tr /var/log/apt/history.log*.gz); cat /var/log/apt/history.log) 2>/dev/null |
  egrep '^(Start-Date:|Commandline:)' |
  grep -v aptdaemon |
  egrep -B1 '^Commandline:'
Commandline: apt-get install notification-daemon
Commandline: apt-get install python-notify
Commandline: apt-get install python-dev python-rpi.gpio
Commandline: apt-get install python-gobject libnotify-bin libnotify-dev
Commandline: apt-get install xorg-dev libglu1-mesa-dev
Commandline: apt-get install libudev-dev
Commandline: apt-get install libusb-dev
Commandline: apt-get install libusb-1.0.0-dev
Commandline: apt-get install screen
Commandline: apt-get install xosd-bin
Commandline: apt-get install libgtk2.0-dev libglib2.0-dev libnotify-dev
Commandline: apt-get install libnotify-cil-dev
Commandline: apt-get install notify-osd
Commandline: apt-get install unclutter
Commandline: apt-get install python3-notify2

The code posted in the question worked intermittently but didn't cause any error messages. 问题中发布的代码可以间歇性地工作,但是没有引起任何错误消息。 I tried adding setting the urgency to critical ( n.set_urgency(notify2.URGENCY_CRITICAL) ) but it didn't help. 我尝试将紧急性设置为紧急( n.set_urgency(notify2.URGENCY_CRITICAL) ),但这没有帮助。

I found that by calling notify-send using --urgency=critical the notification appears reliably. 我发现通过使用--urgency=critical调用notify-send,通知可以可靠地显示。

Working example: 工作示例:

rc = subprocess.Popen('DISPLAY=:0 notify-send --urgency=critical "Changing Brightness..." "100% -> 0% -> Off ->" -i dialog-information', shell=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM