Skip to content

Commit

Permalink
Merge pull request #63 from kaulketh/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
kaulketh authored Mar 15, 2019
2 parents f775a88 + e64355f commit 661f4d0
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 146 deletions.
1 change: 0 additions & 1 deletion bot/conf/greenhouse_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
# keyboard configs
kb1 = [[lib.group1[1], lib.group1[2], lib.group1[3], lib.group3[1]],
[lib.group3[2], lib.group2[1], lib.group2[2], lib.group2[3]],
# [lib.group1[0], lib.group3[0], lib.group2[0]],
[lib.grouping],
[lib.stop_bot, lib.live_stream, lib.reload]
]
Expand Down
4 changes: 0 additions & 4 deletions bot/conf/lib_english.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@
msg_stop = '` S T A N D B Y \n Restart ->` /start'
msg_duration = '`Specify switching time for \'{}\' in ' + time_units_name[time_units_index] + ':`'
water_on = '`\'{}\' is switched on for {}' + time_units_sign[time_units_index] + '.`'
water_on_group = '`{} are switched on for {}' + time_units_sign[time_units_index] + '.`'
water_on_all = '`\'{}\' is switched on for {}' + time_units_sign[time_units_index] + '.`'
water_off = '`\'{}\' was switched off after {}' + time_units_sign[time_units_index] + '.\n\n`'
water_off_group = '`\'{}\' were switched off after {}' + time_units_sign[time_units_index] + '.\n\n`'
water_off_all = '`All was switched off after {}' + time_units_sign[time_units_index] + '.`\n\n'
msg_choice = '`Please select:`'
msg_new_choice = '`New choice or end?`'
msg_panic = '`PANIC MODE!`'
Expand Down
2 changes: 1 addition & 1 deletion bot/conf/lib_ext_greenhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
msg_help = 'Usage and possible commands in special mode:{0}' \
'{1} - this info{0}' \
'{2} - restart the whole RSBPi{0}' \
'{3} - force update{0}' \
'{3} - force update from repository{0}' \
'{4} - force archiving and cleaning of log files{0}' \
'{5} - stop this mode{0}' \
'{6} - switch all on{0}' \
Expand Down
4 changes: 0 additions & 4 deletions bot/conf/lib_german.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@
msg_stop = '` S T A N D B Y \n Neustart ->` /start'
msg_duration = '`Schaltzeit für \'{}\' in ' + time_units_name[time_units_index] + ' angeben:`'
water_on = '`\'{}\' wird jetzt für {}' + time_units_sign[time_units_index] + ' eingeschaltet.`'
water_on_group = '`{} werden jetzt für {}' + time_units_sign[time_units_index] + ' eingeschalten.`'
water_on_all = '`\'{}\' wird jetzt für {}' + time_units_sign[time_units_index] + ' eingeschalten.`'
water_off = '`\'{}\' nach {}' + time_units_sign[time_units_index] + ' abgeschalten.\n\n`'
water_off_group = '`\'{}\' wurden nach {}' + time_units_sign[time_units_index] + ' abgeschalten.\n\n`'
water_off_all = '`Alles wurde nach {}' + time_units_sign[time_units_index] + ' wieder abgeschalten.`\n\n'
msg_choice = '`Bitte auswählen:`'
msg_new_choice = '`Neue Auswahl oder Beenden?`'
msg_panic = '*PANIK-MODUS*'
Expand Down
2 changes: 1 addition & 1 deletion bot/conf/lib_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""
timeout if no user activity
"""
timeout = 6000000
timeout = 120


""" language settings
Expand Down
142 changes: 53 additions & 89 deletions bot/greenhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
group_three = conf.GROUP_03

# api and bot settings
SELECTION, DURATION = range(2)
SELECTION, DURATION, GROUPING = range(3)
# LIST_OF_ADMINS = ['mock to test']
list_of_admins = conf.admins
token = conf.token
Expand Down Expand Up @@ -148,6 +148,7 @@ def __selected_target(bot, update, selected_target):
logger.info('Selection: {0}'.format(str(selected_target)))
__start_standby_timer(bot, update)
return DURATION
# end: set targets


# set water duration
Expand Down Expand Up @@ -219,7 +220,9 @@ def __duration(bot, update):
__water(bot, update, group_three[1])

elif target == str(lib.grouping):
logger.warning("selected to water..." + str(selection))
""" starts separate thread """
display.show_switch_group_duration(int(water_time))

__water_group(bot, update, selection)

else:
Expand All @@ -237,30 +240,6 @@ def __all_off():
return


# TODO: check if still needed!
@run_async
def __water_all(bot, update):
logger.info('Duration: {0}'.format(water_time))
__stop_standby_timer(bot, update)
update.message.reply_text(lib.water_on_all.format(target, water_time),
parse_mode=ParseMode.MARKDOWN, reply_markup=markup3)

""" starts separate thread """
display.show_switch_group_duration(0, int(water_time))

for channel in all_groups:
utils.switch_on(channel)
time.sleep(int(water_time) * int(lib.time_conversion))
__all_off()

update.message.reply_text('{0}{1}{2}'.format(
__timestamp(), lib.water_off_all.format(water_time), lib.msg_new_choice),
parse_mode=ParseMode.MARKDOWN, reply_markup=markup1)
display.show_off()
__start_standby_timer(bot, update)
return


@run_async
def __water(bot, update, channel):
logger.info('Duration: {0}'.format(water_time))
Expand All @@ -286,7 +265,7 @@ def __water_group(bot, update, group):
logger.info('Duration: {0}'.format(water_time))
logger.info('Toggle {0}'.format(str(group)))
__stop_standby_timer(bot, update)
update.message.reply_text(lib.water_on_group.format(target, water_time),
update.message.reply_text(lib.water_on.format(target, water_time),
parse_mode=ParseMode.MARKDOWN, reply_markup=markup3)

for channel in group:
Expand All @@ -295,11 +274,12 @@ def __water_group(bot, update, group):
for channel in group:
utils.switch_off(channel)
update.message.reply_text('{0}{1}{2}'.format(
__timestamp(), lib.water_off_group.format(target, water_time), lib.msg_new_choice),
__timestamp(), lib.water_off.format(target, water_time), lib.msg_new_choice),
parse_mode=ParseMode.MARKDOWN, reply_markup=markup1)
display.show_off()
__start_standby_timer(bot, update)
return
# end watering targets


# get humidity and temperature values
Expand Down Expand Up @@ -350,27 +330,29 @@ def __emergency_stop_handler(bot, update, chat_data):
def __start_emergency_stop(bot, update):
global emergency_job
emergency_job = jq.run_once(__job_stop_and_restart, 0, context=update)
logger.warning("Initialize emergency stop immediately.")
logger.info("Initialize emergency stop immediately.")
return
# end: emergency stop


# [#30] implement standby init after given time without user activity
def __start_standby_timer(bot, update):
global timer_job
timer_job = jq.run_once(__job_stop_and_restart, conf.standby_timeout, context=update)
logger.warning("Init standby timer of {0} seconds, added to queue.".format(conf.standby_timeout))
logger.info("Init standby timer of {0} seconds, added to queue.".format(conf.standby_timeout))
return


def __stop_standby_timer(bot, update):
timer_job.schedule_removal()
logger.warning("Timer job removed from the queue.")
logger.info("Timer job removed from the queue.")
return
# end: standby


# job to stop and restart application
def __job_stop_and_restart(bot, job):
logger.warning("Job: Stop and restart called!")
logger.info("Job: Stop and restart called!")
stop_and_restart.stop_and_restart(job.context)
return

Expand All @@ -391,6 +373,7 @@ def __timestamp():

def __start_time():
return utils.get_timestamp()
# end: time stamps


# camera
Expand All @@ -404,13 +387,15 @@ def __cam_off():
logger.info('Disable camera module.')
os.system(conf.disable_camera)
return
# end: camera


# grouping
def __button(bot, update, chat_data):
# [#31] grouping
def __push_button(bot, update, chat_data):
global selection
query = update.callback_query
added_selection = str(query.data)

if not (added_selection == str(lib.btn_finished) or added_selection == str(lib.cancel)):
if not selection.__contains__(int(added_selection)):
selection += (int(added_selection),)
Expand All @@ -420,13 +405,9 @@ def __button(bot, update, chat_data):
parse_mode=ParseMode.MARKDOWN,
reply_markup=reply_markup)

logger.info(selection)

elif added_selection == str(lib.btn_finished):
elif added_selection == str(lib.btn_finished) and len(selection) > 0:
global target
target = lib.grouping
logger.info('current selection: ' + str(selection))
logger.info('current target: ' + str(target))
bot.delete_message(chat_id=query.message.chat_id,
message_id=query.message.message_id)
bot.send_message(text=lib.msg_duration.format(target),
Expand All @@ -444,30 +425,31 @@ def __button(bot, update, chat_data):
chat_id=query.message.chat_id,
parse_mode=ParseMode.MARKDOWN,
reply_markup=markup1)
logger.info(lib.msg_new_choice)
return SELECTION


def __get_inline_btn(text, callback):
return InlineKeyboardButton(text, callback_data=callback)


def __group(bot, update):
def __group_menu(bot, update):
global selection
selection = ()
logger.info('Grouping mode called.')
inline_keyboard = [
[__get_inline_btn(lib.group1[1], conf.RELAIS_01), __get_inline_btn(lib.group1[2], conf.RELAIS_02),
__get_inline_btn(lib.group1[3], conf.RELAIS_03), __get_inline_btn(lib.group3[1], conf.RELAIS_04)],
[__get_inline_btn(lib.group3[2], conf.RELAIS_05), __get_inline_btn(lib.group2[1], conf.RELAIS_06),
__get_inline_btn(lib.group2[2], conf.RELAIS_07), __get_inline_btn(lib.group2[3], conf.RELAIS_08)],
[__get_inline_btn(lib.btn_finished, lib.btn_finished), __get_inline_btn(lib.btn_cancel, lib.btn_cancel)]
[__get_btn(lib.group1[1], conf.RELAIS_01), __get_btn(lib.group1[2], conf.RELAIS_02),
__get_btn(lib.group1[3], conf.RELAIS_03), __get_btn(lib.group3[1], conf.RELAIS_04)],
[__get_btn(lib.group3[2], conf.RELAIS_05), __get_btn(lib.group2[1], conf.RELAIS_06),
__get_btn(lib.group2[2], conf.RELAIS_07), __get_btn(lib.group2[3], conf.RELAIS_08)],
[InlineKeyboardButton(lib.btn_finished, callback_data=lib.btn_finished),
InlineKeyboardButton(lib.btn_cancel, callback_data=lib.btn_cancel)]
]

global reply_markup
reply_markup = InlineKeyboardMarkup(inline_keyboard)
update.message.reply_text(lib.msg_grouping, reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN)
__start_standby_timer(bot, update)
return GROUPING


def __get_btn(text, callback):
return InlineKeyboardButton('{0} ({1})'.format(text, callback), callback_data=callback)
# end: grouping


def main():
Expand All @@ -482,53 +464,35 @@ def main():

dp = updater.dispatcher

group_handler = CallbackQueryHandler(__button, pass_chat_data=True)

emergency_stop_handler = RegexHandler('^{0}$'.format(str(lib.emergency_stop)),
__emergency_stop_handler,
pass_chat_data=True)
ch = ConversationHandler(
entry_points=[CommandHandler('start', __start)],
states={
SELECTION: [RegexHandler(
'^({0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}|{10}|{11})$'.format(
str(lib.group1[1]),
str(lib.group1[2]),
str(lib.group1[3]),
str(lib.group2[1]),
str(lib.group2[2]),
str(lib.group2[3]),
str(lib.group3[1]),
str(lib.group3[2]),
str(lib.panic),
str(lib.live_stream),
str(lib.reload),
str(selection)),
SELECTION: [RegexHandler('^({0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}|{10})$'.format(
str(lib.group1[1]), str(lib.group1[2]), str(lib.group1[3]),
str(lib.group2[1]), str(lib.group2[2]), str(lib.group2[3]),
str(lib.group3[1]), str(lib.group3[2]),
str(lib.panic), str(lib.live_stream), str(lib.reload)),
__selection),
RegexHandler(
'^{0}$'.format(
lib.stop_bot),
__stop),
RegexHandler(
'^{0}$'.format(
lib.grouping),
__group)
],

DURATION: [RegexHandler(
'^([0-9]+|{0}|{1})$'.format(
str(lib.cancel),
str(lib.panic)),
__duration),
RegexHandler(
'^{0}$'.format(
lib.stop_bot),
__stop)]
},

RegexHandler('^{0}$'.format(lib.stop_bot), __stop),
RegexHandler('^{0}$'.format(lib.grouping), __group_menu)],

DURATION: [RegexHandler('^([0-9]+|{0}|{1})$'.format(str(lib.cancel), str(lib.panic)), __duration),
RegexHandler('^{0}$'.format(lib.stop_bot), __stop)],

GROUPING: [CallbackQueryHandler(__push_button, pass_chat_data=True),
RegexHandler('^({0}|{1}|{2})$'.format(
str(lib.cancel), str(lib.btn_finished), str(selection)),
__selection)]
},
fallbacks=[CommandHandler('stop', __stop)],
allow_reentry=True
allow_reentry=True,
per_chat=True,
per_user=True
)
dp.add_handler(group_handler)

dp.add_handler(emergency_stop_handler)

Expand Down
4 changes: 2 additions & 2 deletions bot/logger/logger.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ formatter=file
args=('/greenhouse.log',)

[formatter_console]
format=%(asctime)s %(levelname)-8s %(name)-15s %(module)-16s > %(message)s
format=%(asctime)s %(levelname)-8s %(name)s %(module)s > %(message)s

[formatter_file]
format=%(asctime)s %(levelname)-8s %(module)-16s %(threadName)-38s > %(message)s
format=%(asctime)s %(levelname)-8s %(module)-20s %(threadName)-40s > %(message)s
Loading

0 comments on commit 661f4d0

Please sign in to comment.