-
Notifications
You must be signed in to change notification settings - Fork 0
/
ext_greenhouse.py
167 lines (141 loc) · 5.09 KB
/
ext_greenhouse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/python
# -*- coding: utf-8 -*-
# ext_greenhouse.py
"""
script for "panic" mode - extended bot
using telepot as Python framework for Telegram Bot API
https://telepot.readthedocs.io/en/latest/reference.html
author: Thomas Kaulke, kaulketh@gmail.com
"""
from __future__ import absolute_import
import conf.greenhouse_config as conf
import conf.lib_ext_greenhouse as lib
import utils.utils as utils
import peripherals.four_digit.display as display
import sys
import time
import telepot
import os
import logger.logger as log
import threading
import peripherals.monitor as monitor
logging = log.get_logger()
thread = threading.Thread(target=monitor.main, name='ExtendedBot temperature monitoring')
thread.start()
pins_state = False
markdown = "-d parse_mode='Markdown'"
no_parse_mode = conf.lib.empty
group_all = (conf.RELAY_01, conf.RELAY_02, conf.RELAY_03, conf.RELAY_04,
conf.RELAY_05, conf.RELAY_06, conf.RELAY_07, conf.RELAY_08)
group_one = (conf.RELAY_01, conf.RELAY_02, conf.RELAY_03)
group_two = (conf.RELAY_06, conf.RELAY_07, conf.RELAY_08)
group_three = (conf.RELAY_04, conf.RELAY_05)
# water a group of targets
def __water_on_group(group):
for member in group:
utils.switch_on(member)
return
# water off for a group of targets
def __water_off_group(group):
for member in group:
utils.switch_off(member)
return
def __send_msg(message, parse_mode):
os.system(
'curl -s -k https://api.telegram.org/bot{0}/sendMessage -d text="{1}" -d chat_id={2} {3}'.format(
apiToken, message.replace("`", "\\`"), str(chat_id), parse_mode))
logging.info('Message send: {0}'.format(message))
return
def __check_pins_state():
global pins_state
for pin in group_all:
if not utils.get_pin_state(pin):
display.show_on()
pins_state = False
break
else:
display.show_off()
pins_state = True
if not pins_state:
__send_msg('Attention, something is still opened!', no_parse_mode)
return
def __handle(msg):
if pins_state:
display.show_extended()
command = msg['text']
logging.info('Got command: %s' % command)
# commands
if command == lib.cmd_restart:
__send_msg(utils.read_cmd('sudo reboot', lib.tmp_file), no_parse_mode)
display.show_boot()
elif command == lib.cmd_update:
utils.read_cmd(lib.update_bot, lib.tmp_file)
__send_msg(lib.msg_update, no_parse_mode)
display.show_update()
time.sleep(3)
elif command == lib.cmd_logrotate:
__send_msg(utils.read_cmd(lib.logrotate_bot, lib.tmp_file), no_parse_mode)
elif command == lib.cmd_all_on:
__send_msg(utils.get_timestamp() + ' all on', no_parse_mode)
__water_on_group(group_all)
elif command == lib.cmd_all_off:
__send_msg('all off.', no_parse_mode)
__water_off_group(group_all)
elif command == lib.cmd_group1_on:
__send_msg(utils.get_timestamp() + 'group 1 on', no_parse_mode)
__water_on_group(group_one)
elif command == lib.cmd_group1_off:
__send_msg('group 1 off', no_parse_mode)
__water_off_group(group_one)
elif command == lib.cmd_group2_on:
__send_msg(utils.get_timestamp() + 'group 2 on', no_parse_mode)
__water_on_group(group_two)
elif command == lib.cmd_group2_off:
__send_msg('group 2 off', no_parse_mode)
__water_off_group(group_two)
elif command == lib.cmd_group3_on:
__send_msg(utils.get_timestamp() + 'group 3 on', no_parse_mode)
__water_on_group(group_three)
elif command == lib.cmd_group3_off:
__send_msg('group 3 off', no_parse_mode)
__water_off_group(group_three)
elif command == lib.cmd_kill:
# disable camera
logging.info('Disable camera module.')
utils.read_cmd(conf.disable_camera, lib.tmp_file)
pid2 = utils.read_cmd(lib.get_pid2, lib.tmp_file)
utils.read_cmd(lib.restart_bot, lib.tmp_file)
__send_msg(conf.lib.msg_stop, markdown)
utils.read_cmd('kill -9 ' + pid2, lib.tmp_file)
elif command == '/live':
__send_msg(conf.lib.msg_live.format(conf.live), markdown)
elif command == '/help':
__send_msg(lib.msg_help, no_parse_mode)
else:
__send_msg(lib.msg_unknown, no_parse_mode)
__check_pins_state()
def __init_and_start():
# API token and chat Id
global apiToken, chat_id
apiToken = conf.token
chat_id = sys.argv[1]
""" kill the still running greenhouse bot script """
pid1 = utils.read_cmd(lib.get_pid1, lib.tmp_file)
utils.read_cmd('kill -9 {0}'.format(str(pid1)), lib.tmp_file)
utils.set_pins()
bot = telepot.Bot(apiToken)
bot.message_loop(__handle)
logging.info('I am listening...')
display.show_extended()
while 1:
try:
time.sleep(5)
except KeyboardInterrupt:
logging.warning('Program interrupted')
display.show_error()
exit()
except Exception:
logging.warning('Any error occurs')
display.show_error()
if __name__ == '__main__':
__init_and_start()