Skip to content

Commit 661f4d0

Browse files
authored
Merge pull request #63 from kaulketh/develop
Develop
2 parents f775a88 + e64355f commit 661f4d0

9 files changed

+77
-146
lines changed

bot/conf/greenhouse_config.py

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
# keyboard configs
3333
kb1 = [[lib.group1[1], lib.group1[2], lib.group1[3], lib.group3[1]],
3434
[lib.group3[2], lib.group2[1], lib.group2[2], lib.group2[3]],
35-
# [lib.group1[0], lib.group3[0], lib.group2[0]],
3635
[lib.grouping],
3736
[lib.stop_bot, lib.live_stream, lib.reload]
3837
]

bot/conf/lib_english.py

-4
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@
4848
msg_stop = '` S T A N D B Y \n Restart ->` /start'
4949
msg_duration = '`Specify switching time for \'{}\' in ' + time_units_name[time_units_index] + ':`'
5050
water_on = '`\'{}\' is switched on for {}' + time_units_sign[time_units_index] + '.`'
51-
water_on_group = '`{} are switched on for {}' + time_units_sign[time_units_index] + '.`'
52-
water_on_all = '`\'{}\' is switched on for {}' + time_units_sign[time_units_index] + '.`'
5351
water_off = '`\'{}\' was switched off after {}' + time_units_sign[time_units_index] + '.\n\n`'
54-
water_off_group = '`\'{}\' were switched off after {}' + time_units_sign[time_units_index] + '.\n\n`'
55-
water_off_all = '`All was switched off after {}' + time_units_sign[time_units_index] + '.`\n\n'
5652
msg_choice = '`Please select:`'
5753
msg_new_choice = '`New choice or end?`'
5854
msg_panic = '`PANIC MODE!`'

bot/conf/lib_ext_greenhouse.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
msg_help = 'Usage and possible commands in special mode:{0}' \
3030
'{1} - this info{0}' \
3131
'{2} - restart the whole RSBPi{0}' \
32-
'{3} - force update{0}' \
32+
'{3} - force update from repository{0}' \
3333
'{4} - force archiving and cleaning of log files{0}' \
3434
'{5} - stop this mode{0}' \
3535
'{6} - switch all on{0}' \

bot/conf/lib_german.py

-4
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@
5050
msg_stop = '` S T A N D B Y \n Neustart ->` /start'
5151
msg_duration = '`Schaltzeit für \'{}\' in ' + time_units_name[time_units_index] + ' angeben:`'
5252
water_on = '`\'{}\' wird jetzt für {}' + time_units_sign[time_units_index] + ' eingeschaltet.`'
53-
water_on_group = '`{} werden jetzt für {}' + time_units_sign[time_units_index] + ' eingeschalten.`'
54-
water_on_all = '`\'{}\' wird jetzt für {}' + time_units_sign[time_units_index] + ' eingeschalten.`'
5553
water_off = '`\'{}\' nach {}' + time_units_sign[time_units_index] + ' abgeschalten.\n\n`'
56-
water_off_group = '`\'{}\' wurden nach {}' + time_units_sign[time_units_index] + ' abgeschalten.\n\n`'
57-
water_off_all = '`Alles wurde nach {}' + time_units_sign[time_units_index] + ' wieder abgeschalten.`\n\n'
5854
msg_choice = '`Bitte auswählen:`'
5955
msg_new_choice = '`Neue Auswahl oder Beenden?`'
6056
msg_panic = '*PANIK-MODUS*'

bot/conf/lib_global.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""
1515
timeout if no user activity
1616
"""
17-
timeout = 6000000
17+
timeout = 120
1818

1919

2020
""" language settings

bot/greenhouse.py

+53-89
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
group_three = conf.GROUP_03
4141

4242
# api and bot settings
43-
SELECTION, DURATION = range(2)
43+
SELECTION, DURATION, GROUPING = range(3)
4444
# LIST_OF_ADMINS = ['mock to test']
4545
list_of_admins = conf.admins
4646
token = conf.token
@@ -148,6 +148,7 @@ def __selected_target(bot, update, selected_target):
148148
logger.info('Selection: {0}'.format(str(selected_target)))
149149
__start_standby_timer(bot, update)
150150
return DURATION
151+
# end: set targets
151152

152153

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

221222
elif target == str(lib.grouping):
222-
logger.warning("selected to water..." + str(selection))
223+
""" starts separate thread """
224+
display.show_switch_group_duration(int(water_time))
225+
223226
__water_group(bot, update, selection)
224227

225228
else:
@@ -237,30 +240,6 @@ def __all_off():
237240
return
238241

239242

240-
# TODO: check if still needed!
241-
@run_async
242-
def __water_all(bot, update):
243-
logger.info('Duration: {0}'.format(water_time))
244-
__stop_standby_timer(bot, update)
245-
update.message.reply_text(lib.water_on_all.format(target, water_time),
246-
parse_mode=ParseMode.MARKDOWN, reply_markup=markup3)
247-
248-
""" starts separate thread """
249-
display.show_switch_group_duration(0, int(water_time))
250-
251-
for channel in all_groups:
252-
utils.switch_on(channel)
253-
time.sleep(int(water_time) * int(lib.time_conversion))
254-
__all_off()
255-
256-
update.message.reply_text('{0}{1}{2}'.format(
257-
__timestamp(), lib.water_off_all.format(water_time), lib.msg_new_choice),
258-
parse_mode=ParseMode.MARKDOWN, reply_markup=markup1)
259-
display.show_off()
260-
__start_standby_timer(bot, update)
261-
return
262-
263-
264243
@run_async
265244
def __water(bot, update, channel):
266245
logger.info('Duration: {0}'.format(water_time))
@@ -286,7 +265,7 @@ def __water_group(bot, update, group):
286265
logger.info('Duration: {0}'.format(water_time))
287266
logger.info('Toggle {0}'.format(str(group)))
288267
__stop_standby_timer(bot, update)
289-
update.message.reply_text(lib.water_on_group.format(target, water_time),
268+
update.message.reply_text(lib.water_on.format(target, water_time),
290269
parse_mode=ParseMode.MARKDOWN, reply_markup=markup3)
291270

292271
for channel in group:
@@ -295,11 +274,12 @@ def __water_group(bot, update, group):
295274
for channel in group:
296275
utils.switch_off(channel)
297276
update.message.reply_text('{0}{1}{2}'.format(
298-
__timestamp(), lib.water_off_group.format(target, water_time), lib.msg_new_choice),
277+
__timestamp(), lib.water_off.format(target, water_time), lib.msg_new_choice),
299278
parse_mode=ParseMode.MARKDOWN, reply_markup=markup1)
300279
display.show_off()
301280
__start_standby_timer(bot, update)
302281
return
282+
# end watering targets
303283

304284

305285
# get humidity and temperature values
@@ -350,27 +330,29 @@ def __emergency_stop_handler(bot, update, chat_data):
350330
def __start_emergency_stop(bot, update):
351331
global emergency_job
352332
emergency_job = jq.run_once(__job_stop_and_restart, 0, context=update)
353-
logger.warning("Initialize emergency stop immediately.")
333+
logger.info("Initialize emergency stop immediately.")
354334
return
335+
# end: emergency stop
355336

356337

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

364345

365346
def __stop_standby_timer(bot, update):
366347
timer_job.schedule_removal()
367-
logger.warning("Timer job removed from the queue.")
348+
logger.info("Timer job removed from the queue.")
368349
return
350+
# end: standby
369351

370352

371353
# job to stop and restart application
372354
def __job_stop_and_restart(bot, job):
373-
logger.warning("Job: Stop and restart called!")
355+
logger.info("Job: Stop and restart called!")
374356
stop_and_restart.stop_and_restart(job.context)
375357
return
376358

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

392374
def __start_time():
393375
return utils.get_timestamp()
376+
# end: time stamps
394377

395378

396379
# camera
@@ -404,13 +387,15 @@ def __cam_off():
404387
logger.info('Disable camera module.')
405388
os.system(conf.disable_camera)
406389
return
390+
# end: camera
407391

408392

409-
# grouping
410-
def __button(bot, update, chat_data):
393+
# [#31] grouping
394+
def __push_button(bot, update, chat_data):
411395
global selection
412396
query = update.callback_query
413397
added_selection = str(query.data)
398+
414399
if not (added_selection == str(lib.btn_finished) or added_selection == str(lib.cancel)):
415400
if not selection.__contains__(int(added_selection)):
416401
selection += (int(added_selection),)
@@ -420,13 +405,9 @@ def __button(bot, update, chat_data):
420405
parse_mode=ParseMode.MARKDOWN,
421406
reply_markup=reply_markup)
422407

423-
logger.info(selection)
424-
425-
elif added_selection == str(lib.btn_finished):
408+
elif added_selection == str(lib.btn_finished) and len(selection) > 0:
426409
global target
427410
target = lib.grouping
428-
logger.info('current selection: ' + str(selection))
429-
logger.info('current target: ' + str(target))
430411
bot.delete_message(chat_id=query.message.chat_id,
431412
message_id=query.message.message_id)
432413
bot.send_message(text=lib.msg_duration.format(target),
@@ -444,30 +425,31 @@ def __button(bot, update, chat_data):
444425
chat_id=query.message.chat_id,
445426
parse_mode=ParseMode.MARKDOWN,
446427
reply_markup=markup1)
447-
logger.info(lib.msg_new_choice)
448428
return SELECTION
449429

450430

451-
def __get_inline_btn(text, callback):
452-
return InlineKeyboardButton(text, callback_data=callback)
453-
454-
455-
def __group(bot, update):
431+
def __group_menu(bot, update):
456432
global selection
457433
selection = ()
458434
logger.info('Grouping mode called.')
459435
inline_keyboard = [
460-
[__get_inline_btn(lib.group1[1], conf.RELAIS_01), __get_inline_btn(lib.group1[2], conf.RELAIS_02),
461-
__get_inline_btn(lib.group1[3], conf.RELAIS_03), __get_inline_btn(lib.group3[1], conf.RELAIS_04)],
462-
[__get_inline_btn(lib.group3[2], conf.RELAIS_05), __get_inline_btn(lib.group2[1], conf.RELAIS_06),
463-
__get_inline_btn(lib.group2[2], conf.RELAIS_07), __get_inline_btn(lib.group2[3], conf.RELAIS_08)],
464-
[__get_inline_btn(lib.btn_finished, lib.btn_finished), __get_inline_btn(lib.btn_cancel, lib.btn_cancel)]
436+
[__get_btn(lib.group1[1], conf.RELAIS_01), __get_btn(lib.group1[2], conf.RELAIS_02),
437+
__get_btn(lib.group1[3], conf.RELAIS_03), __get_btn(lib.group3[1], conf.RELAIS_04)],
438+
[__get_btn(lib.group3[2], conf.RELAIS_05), __get_btn(lib.group2[1], conf.RELAIS_06),
439+
__get_btn(lib.group2[2], conf.RELAIS_07), __get_btn(lib.group2[3], conf.RELAIS_08)],
440+
[InlineKeyboardButton(lib.btn_finished, callback_data=lib.btn_finished),
441+
InlineKeyboardButton(lib.btn_cancel, callback_data=lib.btn_cancel)]
465442
]
466443

467444
global reply_markup
468445
reply_markup = InlineKeyboardMarkup(inline_keyboard)
469446
update.message.reply_text(lib.msg_grouping, reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN)
470-
__start_standby_timer(bot, update)
447+
return GROUPING
448+
449+
450+
def __get_btn(text, callback):
451+
return InlineKeyboardButton('{0} ({1})'.format(text, callback), callback_data=callback)
452+
# end: grouping
471453

472454

473455
def main():
@@ -482,53 +464,35 @@ def main():
482464

483465
dp = updater.dispatcher
484466

485-
group_handler = CallbackQueryHandler(__button, pass_chat_data=True)
486-
487467
emergency_stop_handler = RegexHandler('^{0}$'.format(str(lib.emergency_stop)),
488468
__emergency_stop_handler,
489469
pass_chat_data=True)
490470
ch = ConversationHandler(
491471
entry_points=[CommandHandler('start', __start)],
492472
states={
493-
SELECTION: [RegexHandler(
494-
'^({0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}|{10}|{11})$'.format(
495-
str(lib.group1[1]),
496-
str(lib.group1[2]),
497-
str(lib.group1[3]),
498-
str(lib.group2[1]),
499-
str(lib.group2[2]),
500-
str(lib.group2[3]),
501-
str(lib.group3[1]),
502-
str(lib.group3[2]),
503-
str(lib.panic),
504-
str(lib.live_stream),
505-
str(lib.reload),
506-
str(selection)),
473+
SELECTION: [RegexHandler('^({0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}|{10})$'.format(
474+
str(lib.group1[1]), str(lib.group1[2]), str(lib.group1[3]),
475+
str(lib.group2[1]), str(lib.group2[2]), str(lib.group2[3]),
476+
str(lib.group3[1]), str(lib.group3[2]),
477+
str(lib.panic), str(lib.live_stream), str(lib.reload)),
507478
__selection),
508-
RegexHandler(
509-
'^{0}$'.format(
510-
lib.stop_bot),
511-
__stop),
512-
RegexHandler(
513-
'^{0}$'.format(
514-
lib.grouping),
515-
__group)
516-
],
517-
518-
DURATION: [RegexHandler(
519-
'^([0-9]+|{0}|{1})$'.format(
520-
str(lib.cancel),
521-
str(lib.panic)),
522-
__duration),
523-
RegexHandler(
524-
'^{0}$'.format(
525-
lib.stop_bot),
526-
__stop)]
527-
},
479+
480+
RegexHandler('^{0}$'.format(lib.stop_bot), __stop),
481+
RegexHandler('^{0}$'.format(lib.grouping), __group_menu)],
482+
483+
DURATION: [RegexHandler('^([0-9]+|{0}|{1})$'.format(str(lib.cancel), str(lib.panic)), __duration),
484+
RegexHandler('^{0}$'.format(lib.stop_bot), __stop)],
485+
486+
GROUPING: [CallbackQueryHandler(__push_button, pass_chat_data=True),
487+
RegexHandler('^({0}|{1}|{2})$'.format(
488+
str(lib.cancel), str(lib.btn_finished), str(selection)),
489+
__selection)]
490+
},
528491
fallbacks=[CommandHandler('stop', __stop)],
529-
allow_reentry=True
492+
allow_reentry=True,
493+
per_chat=True,
494+
per_user=True
530495
)
531-
dp.add_handler(group_handler)
532496

533497
dp.add_handler(emergency_stop_handler)
534498

bot/logger/logger.ini

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ formatter=file
2424
args=('/greenhouse.log',)
2525

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

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

0 commit comments

Comments
 (0)