Skip to content

Commit 1245f97

Browse files
authored
Merge pull request #44 from kaulketh/develop
Develop
2 parents 0592dd1 + 366b443 commit 1245f97

22 files changed

+196
-138
lines changed

bot/conf/ext_greenhouse_lib.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
3-
# author: Thomas Kaulke, [email protected]
3+
# ext_greenhouse_lib.py
4+
5+
"""
6+
author: Thomas Kaulke, [email protected]
7+
"""
48

59
from __future__ import absolute_import
610
from conf.access import token, thk

bot/conf/greenhouse_config.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
3-
# configs, command strings and constants
4-
# author: Thomas Kaulke, [email protected]
3+
# greenhouse_config.py
4+
5+
"""
6+
configs, command strings and constants
7+
author: Thomas Kaulke, [email protected]
8+
"""
59

610
from __future__ import absolute_import
711
import conf.access as access

bot/conf/greenhouse_lib_english.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
3-
# author: Thomas Kaulke, [email protected]
3+
# greenhouse_lib_english.py
4+
"""
5+
author: Thomas Kaulke, [email protected]
6+
"""
47

58
empty = ''
69
space = ' '
@@ -38,7 +41,7 @@
3841
msg_live = '[Click here for the live stream]({})'
3942
msg_temperature = '`{}Current values\n{}, {}\n{}`'
4043
msg_welcome = '`Hello {}!`'
41-
msg_stop = '` S T A N D B Y `'
44+
msg_stop = '` S T A N D B Y \n Restart ->` /start'
4245
msg_duration = '`Specify switching time for \'{}\' in ' + time_units_name[time_units_index] + ':`'
4346
water_on = '`\'{}\' is switched on for {}' + time_units_sign[time_units_index] + '.`'
4447
water_on_group = '`{} are switched on for {}' + time_units_sign[time_units_index] + '.`'

bot/conf/greenhouse_lib_german.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
3-
# author: Thomas Kaulke, [email protected]
3+
# greenhouse_lib_german.py
4+
"""
5+
author: Thomas Kaulke, [email protected]
6+
"""
47

58
empty = ''
69
space = ' '
@@ -37,7 +40,7 @@
3740
msg_live = '[Hier gehts zum Live Stream]({})'
3841
msg_temperature = '`{}Aktuelle Werte\n{}, {}\n{}`'
3942
msg_welcome = '`Hallo {}!`'
40-
msg_stop = '` S T A N D B Y `'
43+
msg_stop = '` S T A N D B Y \n Neustart ->` /start'
4144
msg_duration = '`Schaltzeit für \'{}\' in ' + time_units_name[time_units_index] + ' angeben:`'
4245
water_on = '`\'{}\' wird jetzt für {}' + time_units_sign[time_units_index] + ' eingeschaltet.`'
4346
water_on_group = '`{} werden jetzt für {}' + time_units_sign[time_units_index] + ' eingeschalten.`'

bot/ext_greenhouse.py

+56-64
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
3-
# script for "panic" mode - extended bot
4-
# using telepot as Python framework for Telegram Bot API
5-
# https://telepot.readthedocs.io/en/latest/reference.html
6-
# author: Thomas Kaulke, [email protected]
7-
3+
# ext_greenhouse.py
4+
"""
5+
script for "panic" mode - extended bot
6+
using telepot as Python framework for Telegram Bot API
7+
https://telepot.readthedocs.io/en/latest/reference.html
8+
author: Thomas Kaulke, [email protected]
9+
"""
810

911
from __future__ import absolute_import
1012
import conf.greenhouse_config as conf
@@ -39,44 +41,30 @@
3941
group_two = (relais06, relais07, relais08)
4042
group_three = (relais04, relais05)
4143

42-
# API token and chat Id
43-
apiToken = conf.token
44-
Id = sys.argv[1]
45-
4644

4745
# water a group of targets
48-
def _water_on_group(group):
46+
def __water_on_group(group):
4947
for member in group:
5048
utils.switch_on(member)
5149
return
5250

5351

5452
# water off for a group of targets
55-
def _water_off_group(group):
53+
def __water_off_group(group):
5654
for member in group:
5755
utils.switch_off(member)
5856
return
5957

6058

61-
# Assign default output (stdout 1 and stderr 2) to file and read in
62-
# variable and get back
63-
def _read_cmd(cmd):
64-
os.system(cmd + ' > ' + lib.tmp_file + ' 2>&1')
65-
file = open(lib.tmp_file, 'r')
66-
data = file.read()
67-
file.close()
68-
return data
69-
70-
71-
def _send_msg(message, parse_mode):
59+
def __send_msg(message, parse_mode):
7260
os.system(
7361
'curl -s -k https://api.telegram.org/bot{0}/sendMessage -d text="{1}" -d chat_id={2} {3}'.format(
74-
apiToken, message.replace("`", "\\`"), str(Id), parse_mode))
62+
apiToken, message.replace("`", "\\`"), str(chat_id), parse_mode))
7563
logging.info('Message send: {0}'.format(message))
7664
return
7765

7866

79-
def _check_pins_state():
67+
def __check_pins_state():
8068
global pins_state
8169
for pin in group_all:
8270
if not utils.get_pin_state(pin):
@@ -86,10 +74,13 @@ def _check_pins_state():
8674
else:
8775
display.show_off()
8876
pins_state = True
77+
78+
if not pins_state:
79+
__send_msg('Attention, something is still opened!', no_parse_mode)
8980
return
9081

9182

92-
def _handle(msg):
83+
def __handle(msg):
9384
if pins_state:
9485
display.show_extended()
9586

@@ -98,66 +89,69 @@ def _handle(msg):
9889

9990
# commands
10091
if command == lib.cmd_restart:
101-
_send_msg(_read_cmd('sudo reboot'), no_parse_mode)
92+
__send_msg(utils.read_cmd('sudo reboot', lib.tmp_file), no_parse_mode)
10293
display.show_boot()
10394
elif command == lib.cmd_update:
104-
_read_cmd(lib.update_bot)
105-
_send_msg(lib.msg_update, no_parse_mode)
95+
utils.read_cmd(lib.update_bot, lib.tmp_file)
96+
__send_msg(lib.msg_update, no_parse_mode)
10697
display.show_update()
10798
time.sleep(3)
10899
elif command == lib.cmd_logrotate:
109-
_send_msg(_read_cmd(lib.logrotate_bot), no_parse_mode)
100+
__send_msg(utils.read_cmd(lib.logrotate_bot, lib.tmp_file), no_parse_mode)
110101
elif command == lib.cmd_all_on:
111-
_send_msg(utils.get_timestamp() + ' all on', no_parse_mode)
112-
_water_on_group(group_all)
102+
__send_msg(utils.get_timestamp() + ' all on', no_parse_mode)
103+
__water_on_group(group_all)
113104
elif command == lib.cmd_all_off:
114-
_send_msg('all off.', no_parse_mode)
115-
_water_off_group(group_all)
105+
__send_msg('all off.', no_parse_mode)
106+
__water_off_group(group_all)
116107
elif command == lib.cmd_group1_on:
117-
_send_msg(utils.get_timestamp() + 'group 1 on', no_parse_mode)
118-
_water_on_group(group_one)
108+
__send_msg(utils.get_timestamp() + 'group 1 on', no_parse_mode)
109+
__water_on_group(group_one)
119110
elif command == lib.cmd_group1_off:
120-
_send_msg('group 1 off', no_parse_mode)
121-
_water_off_group(group_one)
111+
__send_msg('group 1 off', no_parse_mode)
112+
__water_off_group(group_one)
122113
elif command == lib.cmd_group2_on:
123-
_send_msg(utils.get_timestamp() + 'group 2 on', no_parse_mode)
124-
_water_on_group(group_two)
114+
__send_msg(utils.get_timestamp() + 'group 2 on', no_parse_mode)
115+
__water_on_group(group_two)
125116
elif command == lib.cmd_group2_off:
126-
_send_msg('group 2 off', no_parse_mode)
127-
_water_off_group(group_two)
117+
__send_msg('group 2 off', no_parse_mode)
118+
__water_off_group(group_two)
128119
elif command == lib.cmd_group3_on:
129-
_send_msg(conf.get_timestamp() + 'group 3 on', no_parse_mode)
130-
_water_on_group(group_three)
120+
__send_msg(utils.get_timestamp() + 'group 3 on', no_parse_mode)
121+
__water_on_group(group_three)
131122
elif command == lib.cmd_group3_off:
132-
_send_msg('group 3 off', no_parse_mode)
133-
_water_off_group(group_three)
123+
__send_msg('group 3 off', no_parse_mode)
124+
__water_off_group(group_three)
134125
elif command == lib.cmd_kill:
135126
# disable camera
136127
logging.info('Disable camera module.')
137-
_read_cmd(conf.disable_camera)
138-
pid2 = _read_cmd(lib.get_pid2)
139-
# logging.info('Got own PID to kill me and prepare the other bot for proper using: {0}'.format(str(pid2)))
140-
_read_cmd(lib.restart_bot)
141-
_send_msg(conf.lib.msg_stop, markdown)
142-
_read_cmd('kill -9 ' + pid2)
128+
utils.read_cmd(conf.disable_camera, lib.tmp_file)
129+
pid2 = utils.read_cmd(lib.get_pid2, lib.tmp_file)
130+
utils.read_cmd(lib.restart_bot, lib.tmp_file)
131+
__send_msg(conf.lib.msg_stop, markdown)
132+
utils.read_cmd('kill -9 ' + pid2, lib.tmp_file)
143133
elif command == '/live':
144-
_send_msg(conf.lib.msg_live.format(conf.live), markdown)
134+
__send_msg(conf.lib.msg_live.format(conf.live), markdown)
145135
elif command == '/help':
146-
_send_msg(lib.msg_help, no_parse_mode)
136+
__send_msg(lib.msg_help, no_parse_mode)
147137
else:
148-
_send_msg(lib.msg_unknown, no_parse_mode)
149-
_check_pins_state()
138+
__send_msg(lib.msg_unknown, no_parse_mode)
139+
__check_pins_state()
140+
150141

142+
def __init_and_start():
143+
# API token and chat Id
144+
global apiToken, chat_id
145+
apiToken = conf.token
146+
chat_id = sys.argv[1]
151147

152-
def init_and_start():
153-
# kill the still running greenhouse bot script
154-
pid1 = _read_cmd(lib.get_pid1)
155-
# logging.info('{0} is PID of running default bot, used to kill.'.format(str(pid1)))
156-
_read_cmd('kill -9 {0}'.format(str(pid1)))
148+
# kill the still running greenhouse bot script.
149+
pid1 = utils.read_cmd(lib.get_pid1, lib.tmp_file)
150+
utils.read_cmd('kill -9 {0}'.format(str(pid1)), lib.tmp_file)
157151

158152
utils.set_pins()
159153
bot = telepot.Bot(apiToken)
160-
bot.message_loop(_handle)
154+
bot.message_loop(__handle)
161155
logging.info('I am listening...')
162156
display.show_extended()
163157
while 1:
@@ -174,7 +168,5 @@ def init_and_start():
174168
display.show_error()
175169

176170

177-
init_and_start()
178-
179171
if __name__ == '__main__':
180-
pass
172+
__init_and_start()

bot/greenhouse.py

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
3-
# main script for greenhouse bot
4-
# using telegram.ext as Python framework for Telegram Bot API
5-
# https://core.telegram.org/api#bot-api
6-
# original: author: Stefan Weigert http://www.stefan-weigert.de/php_loader/raspi.php
7-
# adapted: author: Thomas Kaulke, [email protected]
3+
# greenhouse.py
4+
5+
"""
6+
main script for greenhouse bot
7+
using telegram.ext as Python framework for Telegram Bot API
8+
https://core.telegram.org/api#bot-api
9+
original: author: Stefan Weigert http://www.stefan-weigert.de/php_loader/raspi.php
10+
adapted: author: Thomas Kaulke, [email protected]
11+
"""
812

913
from __future__ import absolute_import
1014
import os
@@ -103,7 +107,7 @@ def __start(bot, update):
103107
return SELECTION
104108

105109

106-
# set the target, member of group or group
110+
""" set the target, member of group or group """
107111
def __selection(bot, update):
108112
global target
109113
target = update.message.text
@@ -137,7 +141,7 @@ def __selection(bot, update):
137141
return DURATION
138142

139143

140-
# set water duration
144+
""" set water duration """
141145
def __duration(bot, update):
142146
global water_time
143147
global g_duration_update
@@ -233,7 +237,7 @@ def __duration(bot, update):
233237
return SELECTION
234238

235239

236-
# watering targets
240+
""" watering targets """
237241
def __all_off():
238242
logging.info('Switch all off.')
239243
for channel in all_groups:
@@ -305,9 +309,9 @@ def __water_group(bot, update, group):
305309
return
306310

307311

308-
# humidity and temperature
312+
""" humidity and temperature """
309313
def __message_values(update):
310-
# to avoid refresh intervals shorter than 3 seconds
314+
""" to avoid refresh intervals shorter than 3 seconds """
311315
time.sleep(3)
312316
dht.get_values()
313317
if dht.temperature == 0:
@@ -341,7 +345,8 @@ def __stop(bot, update):
341345
return ConversationHandler.END
342346

343347

344-
# emergency stop
348+
""" [#39] Implement emergency stop"""
349+
"""# emergency stop """
345350
@run_async
346351
def __emergency_stop_handler(bot, update, chat_data):
347352
emergency = update.message.text
@@ -355,11 +360,11 @@ def __emergency_stop_handler(bot, update, chat_data):
355360
def __start_emergency_stop(bot, update):
356361
global emergency_job
357362
emergency_job = jq.run_once(__job_stop_and_restart, 0, context=update)
358-
logging.info("Initialize emergency stop immediately.")
363+
logging.warning("Initialize emergency stop immediately.")
359364
return
360365

361366

362-
# timer
367+
""" [#30] implement standby after given time without user activity """
363368
def __start_standby_timer(bot, update):
364369
global timer_job
365370
timer_job = jq.run_once(__job_stop_and_restart, conf.standby_timeout, context=update)
@@ -373,6 +378,7 @@ def __stop_standby_timer(bot, upadate):
373378
return
374379

375380

381+
""" job to stop and restart application """
376382
def __job_stop_and_restart(bot, job):
377383
logging.warning("Job: Stop and restart called!")
378384
stop_and_restart.stop_and_restart(job.context)

bot/logger/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# logger.__init__py
2+
"""
3+
author: Thomas Kaulke, [email protected]
4+
"""
15
from .logger import get_logger
26
from .logger import logging
37
logging.getLogger(__name__).addHandler(logging.NullHandler())

bot/logger/logger.ini

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1+
# logger.ini
2+
# author: Thomas Kaulke, [email protected]
13
[loggers]
24
keys=root
35

46
[handlers]
5-
# keys=file_handler
67
keys=stream_handler,file_handler
78

89
[formatters]
9-
# keys=file
1010
keys=console,file
1111

1212
[logger_root]
1313
level=DEBUG
14-
# handlers=file_handler
1514
handlers=stream_handler,file_handler
1615

1716
[handler_stream_handler]
18-
# class=StreamHandler
1917
class=FileHandler
2018
level=DEBUG
2119
formatter=console
22-
# args=(sys.stderr,)
2320
args=('/greenhouse_console.log',)
2421

2522
[handler_file_handler]

0 commit comments

Comments
 (0)