Commit 2ca9ba76 authored by Samuel Bancal's avatar Samuel Bancal
Browse files

*waiting for ping time is fixed in minutes now

parent b50d1c44
import json
import logging
import datetime
import subprocess
from telegram.ext import Updater, CommandHandler
......@@ -7,7 +8,7 @@ SECRET_JSON = './secret.json'
WOL_BIN = '/usr/bin/wakeonlan'
PING_BIN = '/bin/ping'
CHECK_INTERVAL = 1 # seconds
MAX_PING_HOST = 30 # attemps to ping WOLed host
MINUTES_TO_WAIT_FOR_PING = 2 # max nb minutes to wait for a WOLed host to answer to ping
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
......@@ -75,7 +76,7 @@ def wol_command(bot, update, args, job_queue):
CHECK_INTERVAL,
context={
'chat_id': chat_id,
'checks_left': MAX_PING_HOST,
'forget_it_datetime': datetime.datetime.now() + datetime.timedelta(minutes=MINUTES_TO_WAIT_FOR_PING),
'hostname': host['hostname'],
'job_queue': job_queue,
}
......@@ -83,7 +84,7 @@ def wol_command(bot, update, args, job_queue):
update.message.reply_text(
'''\
WakeOnLan packet was sent to %s
We'll let you know within 2 minutes when %s is up.''' % (host['mac'], host['hostname']))
We'll let you know within %d minutes when %s is up.''' % (host['mac'], MINUTES_TO_WAIT_FOR_PING, host['hostname']))
except KeyError:
update.message.reply_text('unknown host %s' % name)
else:
......@@ -98,7 +99,7 @@ def ping_host(bot, job):
job.context['chat_id'],
text='%s is up now!' % job.context['hostname'])
else:
if job.context['checks_left'] <= 0:
if datetime.datetime.now() > job.context['forget_it_datetime']:
bot.send_message(
job.context['chat_id'],
text='%s could not be awake : (' % job.context['hostname'])
......@@ -106,12 +107,7 @@ def ping_host(bot, job):
job.context['job_queue'].run_once(
ping_host,
CHECK_INTERVAL,
context={
'chat_id': job.context['chat_id'],
'checks_left': job.context['checks_left'] - 1,
'hostname': job.context['hostname'],
'job_queue': job.context['job_queue'],
}
context=job.context
)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment