Skip to content

Commit

Permalink
Merge pull request #83 from kaulketh/develop
Browse files Browse the repository at this point in the history
update settings and translations
  • Loading branch information
kaulketh authored Dec 21, 2023
2 parents 9f3fe7e + cce1224 commit 05e9350
Show file tree
Hide file tree
Showing 20 changed files with 497 additions and 475 deletions.
14 changes: 8 additions & 6 deletions MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ Note: The authorized Telegram chat ID(s) and bot token are stored in a hidden fi
* **Clock 5** Red hour hand, blue minute hand and warm yellow second 'pendulum' over all LEDs.
* **Clock 6** Similar Clock 4, but w/o seconds and Green and Blue as major colors.
* **Clock 7** white minute hand from LED 1 to 12 in 5 min steps (right half of circle), blue hour hand from LED 12 to 24 (left half of circle)
* _**Colorful animations**_
* All LEDs at once can switch to: **red**, **blue**, **green**, **white**, **yellow**, **orange**, **violet**
* **Colors**: Switching simple colors in random time periods
* **Colors II**: Fading over simple colors in random time periods
* **Rainbow**: Rainbow animation with circular fading effect
* **Rainbow II**: Rainbow animation with chaser, included fading effect.
* **Theater**: Extremely colorful animation with spinning and wiping effects
* **Theater II**: Another colorful animation with spinning effects
* **Advent**: Advent calendar, works in Advent time only! For every day of December will one LED flicker like a candlelight. If it is Advent Sunday it flickers red. Should be time before December but in Advent period all LEDs are working as candlelight. If it is other than Advent time LEDs will circle in orange as warning!
* **Candles**: Each LED simulates candlelight
* **Rainbow**: Rainbow animation with circular fading effect
* **Theater**: Extremely colorful animation with chaser, spinning, wiping effects
* **Strobe**: Emitting brief and rapid flashes of white light in random frequency
* _**Colors**_
* **Colors**: Switching simple colors in random time periods
* **Colors 2**: Fading over simple colors in random time periods
* All LEDs at once can switch to: **red**, **blue**, **green**, **white**, **yellow**, **orange**, **violet**
* **Service menu** no app-in menu, will build due runtime but will be handled like an app-in command because of the slash in front
* **/Reboot** - What should I write here?
* **/Info** - Information about the latest commit/release versions on GitHub, host name, IP, memory usage, disk usage, cpu load
Expand Down
3 changes: 0 additions & 3 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
__status__ = "Production"
__doc__ = "Call instance of framework class."

import threading

from urllib3.exceptions import HTTPError, ProtocolError

from bot_framework import *
Expand All @@ -17,7 +15,6 @@
if __name__ == '__main__':

try:
threading.Thread(target=peripheral_functions.get(2)).start()
telepot_bot.main()
except (ConnectionResetError, ProtocolError, HTTPError) as e:
LOGGER.error(f"Connection error occurs: {e}")
Expand Down
57 changes: 45 additions & 12 deletions bot_framework/telepot_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import codecs
import signal
import traceback

import telepot
from telepot.loop import MessageLoop
Expand All @@ -16,11 +17,14 @@

from config import AUTO_REBOOT_ENABLED, AUTO_REBOOT_TIME, ID_CHAT_THK, \
RUNNING, TOKEN_TELEGRAM_BOT, commands, m_not_allowed, m_pls_select, \
m_rebooted, m_started, m_stopped, m_updated, m_wrong_id
from control import peripheral_functions, run_thread, service, stop_threads
m_rebooted, m_restarted, m_started, m_stopped, m_updated, m_wrong_id, \
AUTO_START
from control import peripheral_functions, run_thread, service, \
stop_threads
from control.reboot import AutoReboot
from control.update import update_bot
from logger import LOGGER
from functions import STOP_CMD
from logger import LOGGER, HISTORY

admins = [ID_CHAT_THK]

Expand All @@ -47,11 +51,12 @@ def __init__(self, t, ids):

self.__log.debug(f"Build app keyboards and buttons.")
self._remove_keyboard = ReplyKeyboardRemove()
# keys order (config)
self.__keyboard_markup = ReplyKeyboardMarkup(keyboard=[
self.__buttons([2, 3, 6, 7, 16]),
self.__buttons([4, 5, 17, 18, 19, 21, 22]),
self.__buttons([15, 20]),
self.__buttons([8, 9, 10, 13, 11, 12, 14])
self.__buttons([8, 9, 10, 13, 11, 12, 14]),
self.__buttons([15, 20, 6, 23, 7, 24]),
self.__buttons([2, 3, 16])
])
self.__func_thread = None

Expand Down Expand Up @@ -124,7 +129,8 @@ def __reply_wrong_command(self, ch_id, content):
def __stop_function(self, ch_id, msg):
if msg is not None:
self.__send(ch_id, msg, reply_markup=self.rm_kb)
return True if stop_threads() else False
# return True if stop_threads() else False
return stop_threads()

def __handle(self, msg):
content_type, chat_type, chat_id = telepot.glance(msg)
Expand Down Expand Up @@ -165,6 +171,9 @@ def __handle(self, msg):
elif command == service.Service.c_reboot:
self.__send(chat_id, m_rebooted, reply_markup=self.rm_kb)
service.reboot_device(m_rebooted)
elif command == service.Service.c_restart:
self.__send(chat_id, m_restarted, reply_markup=self.rm_kb)
service.restart_service(m_restarted)
elif command == service.Service.c_info:
if self.__stop_function(chat_id, msg=None):
info = service.system_info()
Expand Down Expand Up @@ -198,13 +207,36 @@ def start(self):

MessageLoop(self.__bot,
{'chat': self.__handle}).run_as_thread()
# TODO: nfo string/text as constant w/ translations (II)
as_nfo = f"Autostart"
self.__log.info(f"{as_nfo} = {AUTO_START}")
with open(HISTORY, "r") as f:
line = f.readlines()[-1]
self.__log.warning(line)
# TODO: implement considering of translation of stored command after language change
# - search key of value/stored string and gather translations with this key
# - depending of set language execute/set command text
cmd = line.partition(" HISTORY ")[2].replace("\n", "")
_stop = (cmd == STOP_CMD)
self.__log.warning(_stop)
if AUTO_START:
if not _stop:
self.__func_thread = run_thread(cmd, ID_CHAT_THK, self)
for a in self.__admins:
self.__send(a, f"{as_nfo}: {cmd}",
reply_markup=self.kb_stop)
else:
open(HISTORY, "w").close()
self.__stop_function(ID_CHAT_THK, msg=None)

auto_reboot = f"Auto reboot enabled"
self.__log.info(f"auto_reboot = {AUTO_REBOOT_ENABLED}")
# TODO: nfo string/text as constant w/ translations (I)
ar_nfo = f"Auto-Reboot"
self.__log.info(f"{ar_nfo} = {AUTO_REBOOT_ENABLED}")
if AUTO_REBOOT_ENABLED:
for a in self.__admins:
self.__send(a, f"{auto_reboot} at {AUTO_REBOOT_TIME} CET",
reply_markup=self.rm_kb)
kb = self.kb_stop if AUTO_START else self.rm_kb
self.__send(a, f"{ar_nfo}: {AUTO_REBOOT_TIME} CET",
reply_markup=kb)
AutoReboot(reboot_time=AUTO_REBOOT_TIME, bot=self).start()

while True:
Expand All @@ -214,7 +246,8 @@ def start(self):
self.__log.warning('Program interrupted')
exit()
except Exception as e:
self.__log.error(f"Any error occurs: {e}")
self.__log.error(f"Any error occurs: {traceback.format_exc()}")
self.__log.exception(e)
exit()
finally:
peripheral_functions.get(3)()
Expand Down
35 changes: 27 additions & 8 deletions config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,34 @@
__maintainer__ = "Thomas Kaulke"
__status__ = "Production"

from .dictionary import *
from .secret import * # no public deployment!
import json
import os

from logger import LOGGER
from .settings import *
from .secret import * # no public deployment!

HERE = os.path.dirname(os.path.abspath(__file__))

build_text_libraries()
set_language(LANGUAGE)
with open(os.path.join(HERE, TRANSLATIONS), 'r', encoding='utf-8') as file:
translations = json.load(file)

m_wrong_id, m_not_allowed, m_pls_select, m_called, m_started, m_rebooted, \
m_rotated, m_stopped, m_standby, m_stop_f, m_killed, \
m_updated = get_texts(MSG)
commands = []
# not really needed, but better to avoid error messages in the IDE
(m_wrong_id, m_not_allowed, m_pls_select, m_called, m_started, m_rebooted,
m_restarted, m_stopped, m_standby, m_stop_f, m_killed, m_updated) \
= 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

commands = get_texts(CMD)
LOGGER.debug(f"Dynamically creating variables")
for item in translations.items():
_type = item[1].get('type')
_name = item[1].get('name')
_value = item[1].get(LANGUAGE)
_n = item[0]
globals()[_name] = _value
if _type == "function":
# commands
_n = len(commands)
commands.append(globals().get(_name).title())
_value_r = _value.replace("\n", "")
LOGGER.debug(f"{_type} {_n} '{_name}': '{_value_r}'")
Loading

0 comments on commit 05e9350

Please sign in to comment.