限制程序只发送一封电子邮件

时间:2016-09-15 22:58:24

标签: python raspberry-pi

我有一个程序来控制我鸡舍的很多东西,包括在设定的时间打开和关闭门。偶尔会发生一些事情并且门没有打开或关闭,我现在已经让它在发生这种情况时发送电子邮件。问题是它将发送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)

这只是一个爱好,我把搜索的内容放在一起,但是不能解决这个问题,所以会很感激任何帮助

2 个答案:

答案 0 :(得分:0)

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

now.minute == HOUR_ON.minute + 120对我来说没有意义 - 看起来你说的是2小时,而不是2分钟,除非now.minute以秒为单位返回一个值?

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

答案 1 :(得分:0)

这似乎是这样做的。

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作为全球?