冷却功能

时间:2019-09-10 16:14:15

标签: python async-await

你好,我有一个Person Detector脚本,如果有人检测到邮件,我想发送一个信息。为了防止垃圾邮件,我需要一个sendMail函数的计时器。该函数可以随时触发,但只有在未触发时才会响应冷却后。

我尝试使用异步任务,但未能实现,因为如果有人检测到它,就会进入循环状态,即每隔5分钟发送一次电子邮件,即使在第一次见面后也没有检测到任何人。

示例:

  1. 人员检测脚本正在运行。
  2. 在相机上检测到人员->发送电子邮件(开始5分钟的冷却时间)
  3. 2分钟后,该人再次被发现(由于仍有3分钟的冷却时间,因此未发送任何电子邮件)。
  4. 6分钟后看到的人再发送一封电子邮件(因为5分钟的冷却时间已结束)。

我的代码摘要。(仅必要部分检测和发送邮件有效,冷却时间(定时器)不起作用

async def sendAlert():
        server.sendmail(sender_email, receiver_email, message)
        print('sent!')
        await asyncio.sleep(300)




if __name__ == "__main__":
    while True:
        for i in range(len(boxes)):
            if classes[i] == 1 and scores[i] > threshold:
                with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
                    sendAlert(server)     
                box = boxes[i]
                cv2.rectangle(img,(box[1],box[0]),(box[3],box[2]),(255,0,0),2)

如果检测到有人,脚本将通过电子邮件发送警报。然后如果在5分钟内再次检测到某人,则sendAlert函数要等到5分钟后才能响应

1 个答案:

答案 0 :(得分:0)

我同意@Prune的观点,您需要创建一个小(最小)用例并展示您的代码,以便它不仅与您有关,而且与其他人有关。此外,您的问题应包含一个带有可验证示例的部分。没有这些属性,您的问题将变得难以让人理解,解决和/或提出任何可验证的解决方案。

但是,据我了解,您需要在某个cool-off时间段之后执行某些操作(如果检测到某人,则发送电子邮件)。因此,换句话说,您需要一种跟踪时间的机制。因此,您将需要datetime库。

因此,您的伪代码应如下所示:

伪代码

import datetime

start = capture_timestamp()
cutoff = '00:05:00'
dt_cutoff = read_in_cutoff_as_timedelta(cutoff)
if person_detected:
    now = capture_timestamp()
    dt = now - start
    if dt >= dt_cutoff:
        # Send notification
        send_email_notification()
    else:
        # Do not Send Notification
        print('now: {} | dt: {}'.format(now, dt))

您可以将datetime.datetime.utcnow()用于时间戳记。还有datetime.timedelta()用于定义dt_cutoff。要按时间读取时间字符串,可以执行以下操作:

tm = datetime.datetime.strptime(cutoff).time()
dt_cutoff = datetime.timedelta(hours = tm.hour, minutes = tm.minute, seconds = tm.second)

我希望这会给您一些有关如何建模的想法。

其他资源

  1. https://www.guru99.com/date-time-and-datetime-classes-in-python.html
  2. https://docs.python.org/3/library/datetime.html
  3. https://thispointer.com/python-how-to-convert-a-timestamp-string-to-a-datetime-object-using-datetime-strptime/

完整解决方案

现在,最后,如果您急于使用现成的解决方案,则可以使用如下所示的类对象。您只需要通过指定冷静期(timer_cutoff)来实例化类对象,然后调用方法is_timeout()。如果返回True,则发送通知。还有一个obj.istimeout属性可存储此决定(True / False)。

import time

# Set cutoff time to 2 seconds to test the output 
# after 5 seconds: expect istimeout = True
# and instantiate the TimeTracker class object.
ttk = TimeTracker(timer_cutoff = '00:00:02') # 'HH:MM:SS'
# Wait for 3 seconds
time.sleep(3)
print('start timestamp: {}'.format(ttk.timestamp_start_str))
print('cutoff timestamp'.format(ttk.timestamp_cutoff_str))
print('timer_cutoff: {}'.format(ttk.timer_cutoff_str))
# Now check if cutoff time reached
ttk.is_timeout()
print('Send Notification: {}'.format(ttk.istimeout))
print('now_timestamp: {}'.format(ttk.timestamp_now_str))

TimeTracker类

这是TimeTracker类:

import datetime

class TimeTracker(object):
    def __init__(self, 
                 timer_cutoff = '00:05:00', 
                 cutoff_strformat = '%H:%M:%S'):
        self.timer_cutoff_str = timer_cutoff
        self.cutoff_strformat = cutoff_strformat
        self.timestamp_start, self.timestamp_start_str = self.get_timestamp()
        self.dt_cutoff = None # timedelta for cutoff
        self.timestamp_cutoff = None
        self.timestamp_cutoff_str = None
        self.update_timestamp_cutoff()
        self.timestamp_now = None
        self.timestamp_now_str = None
        self.dt_elapsed = None
        self.istimeout = False

    def get_timestamp(self):
        ts = datetime.datetime.utcnow()
        tss = str(ts)
        return (ts, tss)

    def readin_cutoff_as_timedelta(self):
        td = datetime.datetime.strptime(self.timer_cutoff_str, 
                                        self.cutoff_strformat)
        tdm = td.time()
        self.dt_cutoff = datetime.timedelta(hours = tdm.hour, 
                                            minutes = tdm.minute, 
                                            seconds = tdm.second)


    def update_timestamp_cutoff(self):
        self.readin_cutoff_as_timedelta()
        self.timestamp_cutoff = self.timestamp_start + self.dt_cutoff
        self.timestamp_cutoff_str = str(self.timestamp_cutoff)

    def time_elapsed(self):
        self.dt_elapsed = self.timestamp_now - self.timestamp_start

    def is_timeout(self):
        self.timestamp_now, self.timestamp_now_str = self.get_timestamp()
        self.time_elapsed()
        if (self.dt_elapsed < self.dt_cutoff):
            self.istimeout = False
        else:
            self.istimeout = True

        return self.istimeout