我编写了一个简单的python代码来打开自动门,代码如下:
#!/usr/bin/env python
"""
open my automatic gate
"""
import gpiozero
import time
import logging
from functools import wraps
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
# log
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
#user id
id = [XXXXX, YYYYY, ZZZZZ, WWWWW] #riccardo, papà, letizia, mamma
#relay object
relay = gpiozero.OutputDevice(4, active_high=True, initial_value=False)
# access restricted def restricted(func):
@wraps(func)
def wrapped(update, context, *args, **kwargs):
user_id = update.effective_user.id
if user_id not in id:
print("accesso non autorizzato per {}.".format(user_id))
update.message.reply_text('Utente non autorizzato')
return
return func(update, context, *args, **kwargs)
return wrapped
# open the gate
@restricted
def apri(update, context):
relay.on()
time.sleep(1)
relay.off()
update.message.reply_text('Apro il cancello!')
time.sleep(30)
update.message.reply_text('Cancello aperto!')
time.sleep(82)
update.message.reply_text('Chiudo il cancello')
time.sleep(30)
relay.off()
update.message.reply_text('Cancello chiuso!')
# help
def aiuto(update, context):
update.message.reply_text('Digitare /apri per aprire il cancello di casa. Solo utenti autorizzati')
# echo all other messages
def echo(update, context):
update.message.reply_text(update.message.text)
# main def main():
"""start the bot"""
updater = Updater("TOKEN_HERE", use_context=True)
#register
dp = updater.dispatcher
#message handler
dp.add_handler(CommandHandler("apri", apri))
dp.add_handler(CommandHandler("aiuto", aiuto))
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))
#listening
updater.start_polling()
# exiting
updater.idle()
if __name__ == '__main__':
main()
问题是,如果出于某种原因,我的Raspberry Pi关闭了,那么在关闭时收到的所有消息在我重新启动队列时都会在队列中执行,这是不必要的行为的结果。是否有一种方法可以检查用户发送消息的日期时间,并仅在有限的时间内执行?例如。: 用户A发送OPEN命令 如果我的脚本在发送时间后1分钟内收到: 执行命令 其他的 丢弃消息
希望我已经说清楚了
致意
里卡多
我确实设法获得了消息时间,这样我就可以与实际时间进行比较并丢弃旧消息
谢谢大家
里卡多
相关问题 更多 >
编程相关推荐