限制程序只发送一个emai

2024-09-29 06:28:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个程序来控制我的鸡舍的很多事情,包括在规定的时间开门和关门。偶尔会发生一些事情,门开不开也关不上,我现在已经让它在这种情况下发送电子邮件。问题是,它将发送6个或更多的电子邮件,我一直在努力研究如何限制它只发送一个可能使用while或if-但我需要重新设置它,以便如果在另一天再次发生,它将发送另一封电子邮件。这就是我的循环

def loop():
# retrieve current datetime
now = datetime.time(datetime.datetime.now().hour, datetime.datetime.now().minute)

# Open door on all days at the correct time
if ((now.hour == HOUR_ON.hour) and (now.minute == HOUR_ON.minute)):
  if (gpio.digitalRead(17) == 0):
    openplay()

# Close door on all days at the correct time
if ((now.hour == HOUR_OFF.hour) and (now.minute == HOUR_OFF.minute)):
  if (gpio.digitalRead(22) == 1):
    closeplay()

# check if door is open, 2 minutes after set time
if ((now.hour == HOUR_ON.hour) and (now.minute == HOUR_ON.minute + 120) and (now.second == 0) and (gpio.digitalRead(25) == 0)):
  # send email
  sendemail()

# check if door is closed, 2 minutes after set time
if ((now.hour == HOUR_OFF.hour) and (now.minute == HOUR_OFF.minute + 120) and (now.second == 0) and (gpio.digitalRead(25) == 1)):
  # send email
  sendemail()

# gives CPU some time before looping again
webiopi.sleep(1)

这只是一个爱好,我把大部分搜索的东西放在一起,但不能破解这个,所以如果有任何帮助,我将不胜感激


Tags: anddatetimegpioiftimeon电子邮件事情
2条回答

这似乎可以做到。你知道吗

def loop():
  global emailsent1
  global emailsent2

  # retrieve current datetime
  now = datetime.time(datetime.datetime.now().hour, datetime.datetime.now().minute)

  # Open door on all days at the correct time
  if ((now.hour == HOUR_ON.hour) and (now.minute == HOUR_ON.minute)):
    if (gpio.digitalRead(17) == 0):
      openplay()

  # Close door on all days at the correct time
  if ((now.hour == HOUR_OFF.hour) and (now.minute == HOUR_OFF.minute)):
    if (gpio.digitalRead(22) == 1):
      closeplay()

  # check if door is open, 10 minutes after set time and send email if not already sent
  if ((now.hour == HOUR_ON.hour) and (now.minute == HOUR_ON.minute + 10) and (now.second == 0) and (gpio.digitalRead(25) == 1) and not emailsent1):
    # send email
    a = 'opened'
    sendemail(a)
    emailsent1 = True

  if ((now.hour == HOUR_ON.hour) and (now.minute == HOUR_ON.minute + 11) and emailsent1): # reset if email has been sent
emailsent1 = False

  # check if door is closed, 10 minutes after set time and send email if not already sent
  if ((now.hour == HOUR_OFF.hour) and (now.minute == HOUR_OFF.minute + 10) and (now.second == 0) and (gpio.digitalRead(25) == 0) and not emailsent2):
    # send email
    a = 'closed'
    sendemail(a)
    emailsent2 = True

    if ((now.hour == HOUR_OFF.hour) and (now.minute == HOUR_OFF.minute + 11) and emailsent2): # resset if email has been sent
    emailsent2 = False

    # gives CPU some time before looping again
    webiopi.sleep(1)

关门10分钟后,它检查门是否关上,是否调用sendmail(a),emailsent1是否设置为True—一分钟后,它再次设置为false。你知道吗

我是否需要emailsent1和emailsent2作为全局?你知道吗

假设sendemail()是您定义的函数,您可以重写它来存储它上次发送电子邮件的时间,如果时间不够长,就不要发送它。你知道吗

对我来说now.minute == HOUR_ON.minute + 120没有意义,你说的是2小时,而不是2分钟,除非现在。分钟以秒为单位返回值?你知道吗

另一种可能性是:这个python程序是否有多个实例同时运行?pgrep python3查看有多少python实例正在运行。你知道吗

相关问题 更多 >