Skip to content

Commit 2525bd6

Browse files
authored
Merge pull request #46 from kaulketh/develop
Develop
2 parents 1245f97 + 5bc6203 commit 2525bd6

11 files changed

+125
-72
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,11 @@ _**Make them executable and chown root:root!**_
188188
* access.py - external file, content will not provided, contains api token, chat IDs and other sensitive data
189189
* [greenhouse_config.py](bot/conf/greenhouse_config.py) - contains settings and properties
190190
* [greenhouse.py](bot/greenhouse.py) - main bot
191-
* [greenhouse_lib_german.py](bot/conf/greenhouse_lib_german.py) - file with constants, contains strings for descriptions and messages
191+
* [lib_global.py](bot/conf/lib_global.py) - global constants and time settings
192+
* [lib_german.py](bot/conf/lib_german.py) - file with constants, contains strings for descriptions and messages
192193
* [ext_greenhouse.py](bot/ext_greenhouse.py) - extended bot
193-
* [ext_greenhouse_lib.py](bot/conf/ext_greenhouse_lib.py) - some constants, contains strings for commands and texts
194-
* [greenhouse_lib_english.py](bot/conf/greenhouse_lib_english.py) - English translation of German version, if required adapt import in config!
194+
* [lib_ext_greenhouse.py](bot/conf/lib_ext_greenhouse.py) - some constants, contains strings for commands and texts
195+
* [lib_english.py](bot/conf/lib_english.py) - English translation of German version, if required adapt import in config!
195196
* [gpio_check.py](bot/peripherals/gpio_check.py) - to check state of GPIOs and logs state, in case it is wished
196197
* [update_bot.sh](bot/update_bot.sh) - to updates all scripts from this repository by using last commit ID
197198

bot/conf/greenhouse_config.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66
configs, command strings and constants
77
author: Thomas Kaulke, [email protected]
88
"""
9-
109
from __future__ import absolute_import
1110
import conf.access as access
12-
13-
# language selection
14-
""" for English import greenhouse_lib_english """
15-
import conf.greenhouse_lib_german as lib
16-
11+
from conf.lib_global import language_index
12+
"""
13+
language settings
14+
import required lib
15+
set index in global library
16+
"""
17+
if language_index == 0:
18+
import conf.lib_english as lib
19+
elif language_index == 1:
20+
import conf.lib_german as lib
21+
else:
22+
import conf.lib_english as lib
1723

1824
# API Token and Chat Id's from external file
1925
admins = [access.thk, access.annett]

bot/conf/greenhouse_lib_english.py bot/conf/lib_english.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
3-
# greenhouse_lib_english.py
3+
# lib_english.py
44
"""
55
author: Thomas Kaulke, [email protected]
66
"""
7-
8-
empty = ''
9-
space = ' '
10-
colon_space = ': '
11-
pipe_space = '| '
12-
line_break = '\n'
7+
from __future__ import absolute_import
8+
import conf.lib_global as global_lib
9+
10+
empty = global_lib.empty
11+
space = global_lib.space
12+
colon_space = global_lib.colon_space
13+
pipe_space = global_lib.pipe_space
14+
line_break = global_lib.line_break
1315
decimal = '.'
1416

15-
# time unit
16-
# time units settings (0 == seconds, 1 == minutes)
17-
time_units_index = 0
18-
time_units_name = ('seconds', 'minutes')
19-
time_units_sign = ('s', 'm')
20-
time_units_conversion = (1, 60)
21-
time_conversion = time_units_conversion[time_units_index]
17+
# time units
18+
time_units_index = global_lib.time_units_index
19+
time_units_name = ('seconds', 'minutes', 'hours')
20+
time_units_sign = global_lib.time_units_sign
21+
time_units_conversion = global_lib.time_units_conversion
22+
time_conversion = global_lib.time_conversion
2223

2324
# commands and descriptions
2425
panic = 'Panic'
@@ -36,7 +37,6 @@
3637
hum = 'Humidity'
3738
core = 'Core temperature'
3839

39-
4040
# messages
4141
msg_live = '[Click here for the live stream]({})'
4242
msg_temperature = '`{}Current values\n{}, {}\n{}`'

bot/conf/ext_greenhouse_lib.py bot/conf/lib_ext_greenhouse.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
3-
# ext_greenhouse_lib.py
3+
# lib_ext_greenhouse.py
44

55
"""
66
author: Thomas Kaulke, [email protected]
77
"""
88

99
from __future__ import absolute_import
1010
from conf.access import token, thk
11+
from conf.lib_global import line_break, commit_id, bot_dir, bot_backup
1112

12-
newline = '\n'
13+
newline = line_break
1314
cmd_prefix = '/'
1415
cmd_restart = '{0}RESTART'.format(cmd_prefix)
1516
cmd_update = '{0}UPDATE'.format(cmd_prefix)
@@ -53,20 +54,17 @@
5354
tmp_file = 'cmd.tmp'
5455
del_tmp = 'rm -r ' + tmp_file
5556

56-
# get_pid1 = 'pgrep -lfo greenhouse.py | awk \'{print $1}\''
5757
get_pid1 = 'ps -o pid,args -C python | awk \'/greenhouse.py/ { print $1 }\''
58-
# get_pid2 = 'pgrep -lfo ext_greenhouse.py | awk \'{print $1}\''
5958
get_pid2 = 'ps -o pid,args -C python | awk \'/ext_greenhouse.py/ { print $1 }\''
6059

61-
restart_bot = 'python /home/pi/scripts/TelegramBot/greenhouse.py &'
60+
restart_bot = 'python ' + str(bot_dir) + 'greenhouse.py &'
6261

63-
"""Ensure same file names in update_bot.sh!"""
64-
"""Ensure same file names in peripherals/oled/display.py!"""
65-
update_bot = 'rm /greenhouseRepoCommit.id && bash /home/pi/scripts/TelegramBot/update_bot.sh ' \
62+
update_bot = 'rm '+ str(commit_id) + ' && bash ' + str(bot_dir) + 'update_bot.sh ' \
6663
+ str(token) + ' ' + str(thk) + ' &'
6764

68-
backup_all = 'tar -zcf /home/pi/backups/greenhouse.tgz --exclude=\'*.pyc\' /home/pi/scripts/TelegramBot/ &'
65+
backup_all = 'tar -zcf ' + str(bot_backup) + ' --exclude=\'*.pyc\' ' + str(bot_dir) + ' &'
6966
logrotate_bot = 'logrotate -f /etc/logrotate.conf &'
7067

68+
7169
if __name__ == '__main__':
7270
pass

bot/conf/greenhouse_lib_german.py bot/conf/lib_german.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
3-
# greenhouse_lib_german.py
3+
# lib_german.py
44
"""
55
author: Thomas Kaulke, [email protected]
66
"""
7+
from __future__ import absolute_import
8+
import conf.lib_global as global_lib
79

8-
empty = ''
9-
space = ' '
10-
colon_space = ': '
11-
pipe_space = '| '
12-
line_break = '\n'
10+
empty = global_lib.empty
11+
space = global_lib.space
12+
colon_space = global_lib.colon_space
13+
pipe_space = global_lib.pipe_space
14+
line_break = global_lib.line_break
1315
decimal = ','
1416

15-
# time unit
16-
# time units settings (0 == seconds, 1 == minutes)
17-
time_units_index = 0
18-
time_units_name = ('Sekunden', 'Minuten')
19-
time_units_sign = ('s', 'm')
20-
time_units_conversion = (1, 60)
21-
time_conversion = time_units_conversion[time_units_index]
17+
# time units
18+
time_units_index = global_lib.time_units_index
19+
time_units_name = ('Sekunden', 'Minuten', 'Stunden')
20+
time_units_sign = global_lib.time_units_sign
21+
time_units_conversion = global_lib.time_units_conversion
22+
time_conversion = global_lib.time_conversion
2223

2324
# commands and descriptions
2425
panic = 'Panik'

bot/conf/lib_global.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# lib_global.py
4+
"""
5+
author: Thomas Kaulke, [email protected]
6+
"""
7+
8+
empty = ''
9+
space = ' '
10+
colon_space = ': '
11+
pipe_space = '| '
12+
line_break = '\n'
13+
14+
""" language settings
15+
0 - English
16+
1 - German
17+
"""
18+
language_index = 1
19+
20+
""" time units settings
21+
0 == seconds
22+
1 == minutes
23+
2 == hours
24+
"""
25+
time_units_index = 0
26+
time_units_conversion = (1, 60, 3600)
27+
time_units_sign = ('s', 'm', 'h')
28+
time_conversion = time_units_conversion[time_units_index]
29+
30+
31+
# TODO: make accessible in bash and vice versa!!!!
32+
file_log_greenhouse = '/greenhouse.log'
33+
file_log_debug = '/greenhouse_console.log'
34+
file_log_update = '/update_bot.log'
35+
commit_id = '/greenhouseRepoCommit.id'
36+
cloned_branch = '/greenhouseRepoBranch.name'
37+
bot_dir = '/home/pi/scripts/TelegramBot/'
38+
bot_backup = '/home/pi/backups/greenhouse.tgz'
39+
40+
41+
if __name__ == '__main__':
42+
pass

bot/ext_greenhouse.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from __future__ import absolute_import
1212
import conf.greenhouse_config as conf
13-
import conf.ext_greenhouse_lib as lib
13+
import conf.lib_ext_greenhouse as lib
1414
import utils.utils as utils
1515
import peripherals.four_digit.display as display
1616
import sys

bot/greenhouse.py

+22-15
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
# greenhouse.py
44

55
"""
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]
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: Thomas Kaulke, [email protected]
1111
"""
12-
1312
from __future__ import absolute_import
1413
import os
1514
import time
@@ -91,12 +90,12 @@ def __start(bot, update):
9190
return ConversationHandler.END
9291
else:
9392
display.show_run()
94-
logging.info('Bot started.')
93+
logging.info('Bot started...')
9594
__message_values(update)
9695
__cam_on()
9796
display.show_ready()
9897
update.message.reply_text('{0}{1}{2}'.format(
99-
lib.msg_welcome.format(update.message.from_user.first_name), lib.line_break, lib.msg_choice),
98+
lib.msg_welcome.format(update.message.from_user.first_name), lib.space, lib.msg_choice),
10099
parse_mode=ParseMode.MARKDOWN, reply_markup=markup1)
101100
logging.info('Bot is using by: {0} - {1},{2}'.format(
102101
str(user_id), update.message.from_user.last_name, update.message.from_user.first_name))
@@ -309,7 +308,9 @@ def __water_group(bot, update, group):
309308
return
310309

311310

312-
""" humidity and temperature """
311+
"""
312+
get humidity and temperature values
313+
"""
313314
def __message_values(update):
314315
""" to avoid refresh intervals shorter than 3 seconds """
315316
time.sleep(3)
@@ -345,8 +346,9 @@ def __stop(bot, update):
345346
return ConversationHandler.END
346347

347348

348-
""" [#39] Implement emergency stop"""
349-
"""# emergency stop """
349+
"""
350+
[#39] Implement emergency stop
351+
"""
350352
@run_async
351353
def __emergency_stop_handler(bot, update, chat_data):
352354
emergency = update.message.text
@@ -364,21 +366,26 @@ def __start_emergency_stop(bot, update):
364366
return
365367

366368

367-
""" [#30] implement standby after given time without user activity """
369+
"""
370+
[#30] implement standby
371+
init after given time without user activity
372+
"""
368373
def __start_standby_timer(bot, update):
369374
global timer_job
370375
timer_job = jq.run_once(__job_stop_and_restart, conf.standby_timeout, context=update)
371-
logging.info("Init standby timer of {0} seconds, added to queue.".format(conf.standby_timeout))
376+
logging.warning("Init standby timer of {0} seconds, added to queue.".format(conf.standby_timeout))
372377
return
373378

374379

375380
def __stop_standby_timer(bot, upadate):
376381
timer_job.schedule_removal()
377-
logging.info("Timer job removed from the queue.")
382+
logging.warning("Timer job removed from the queue.")
378383
return
379384

380385

381-
""" job to stop and restart application """
386+
"""
387+
job to stop and restart application
388+
"""
382389
def __job_stop_and_restart(bot, job):
383390
logging.warning("Job: Stop and restart called!")
384391
stop_and_restart.stop_and_restart(job.context)

bot/logger/logger.ini

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# logger.ini
2-
# author: Thomas Kaulke, [email protected]
31
[loggers]
42
keys=root
53

bot/peripherals/oled/display.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from PIL import Image, ImageFont, ImageDraw
1111
from lib_oled96 import Ssd1306
1212
from smbus import SMBus
13+
from ...conf.lib_global import commit_id, cloned_branch, bot_dir
1314

1415

1516
# Display setup, methods and members
@@ -23,24 +24,23 @@
2324
switch_time = 15
2425

2526

26-
"""Ensure same file names in update_bot.sh!"""
2727
def get_last_commit():
2828
commit = None
2929
branch = None
3030

3131
try:
32-
commit = open("/greenhouseRepoCommit.id").read()
32+
commit = open(str(commit_id)).read()
3333
if commit is None:
3434
commit = '-------'
3535
else:
3636
commit = commit[0:7]
37-
branch = open("/greenhouseRepoBranch.name").read()
37+
branch = open(str(cloned_branch)).read()
3838
if branch is None:
3939
branch = '-------'
4040
else:
4141
branch = branch.replace("\n", "")
4242
except Exception:
43-
build = '!!!ERROR!!!'
43+
build = '---ERROR---'
4444
return build
4545

4646
build = commit + " " + branch
@@ -56,8 +56,8 @@ def get_core_temp():
5656

5757

5858
# Fonts
59-
font = ImageFont.truetype('/home/pi/scripts/TelegramBot/peripherals/oled/fonts/arial.ttf', 12)
60-
font2 = ImageFont.truetype('/home/pi/scripts/TelegramBot/peripherals/oled/fonts/FreeSans.ttf', 12)
59+
font = ImageFont.truetype(str(bot_dir) + 'peripherals/oled/fonts/arial.ttf', 12)
60+
font2 = ImageFont.truetype(str(bot_dir) + 'peripherals/oled/fonts/FreeSans.ttf', 12)
6161

6262

6363
def animate(time):
@@ -80,7 +80,7 @@ def show_pi(time):
8080
oled.cls()
8181
# image inverted
8282
draw.rectangle((32, top - 3, 95, 63), outline=1, fill=1)
83-
draw.bitmap((32, top - 3), Image.open('/home/pi/scripts/TelegramBot/peripherals/oled/pi_logo.png'), fill=0)
83+
draw.bitmap((32, top - 3), Image.open(str(bot_dir) + 'peripherals/oled/pi_logo.png'), fill=0)
8484
oled.display()
8585
sleep(time)
8686

bot/utils/stop_and_restart.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from __future__ import absolute_import
1010
from telegram import (ReplyKeyboardRemove, ParseMode)
1111
import time
12-
import conf.ext_greenhouse_lib as lib
12+
import conf.lib_ext_greenhouse as lib
1313
import conf.greenhouse_config as conf
1414
import peripherals.four_digit.display as display
1515
import logger.logger as log

0 commit comments

Comments
 (0)