def __handler(self, update: Update, _: CallbackContext) -> None: if self.__dao is not None: self.__dao.close() self.__dao = DAO() user = update.effective_user search = self.__dao.search_user(user.id) if search is None: if update.message: update.message.reply_text('You cannot use this bot.') return if update.message: message = update.message logger.info('%s %s %s', 'chat', user.id, message.text) if '/start' in message.text: text = self.__view.welcome(user.first_name) message.reply_text(text) devices = self.__dao.get_all_devices() text, inline_keyboard = self.__view.menu(devices) message.reply_text( text, reply_markup=InlineKeyboardMarkup(inline_keyboard)) else: message.reply_text('Command not found') elif update.callback_query: query = update.callback_query logger.info('%s %s %s', 'callback_query', user.id, query.data) action = query.data.split('|')[0] data = query.data.split('|')[1:] if 'menu' == action: devices = self.__dao.get_all_devices() text, inline_keyboard = self.__view.menu(devices) query.edit_message_text( text, reply_markup=InlineKeyboardMarkup(inline_keyboard)) elif 'device' == action: """ 0: device_id 1: day """ self.__device(query, data[0], int(data[1])) elif 'lock' == action: """ 0: device_id 1: delay before unlock (optional) """ if len(data) == 1: text, inline_keyboard = self.__view.choose_delay(data[0]) query.edit_message_text( text, reply_markup=InlineKeyboardMarkup(inline_keyboard)) elif len(data) == 2: self.__modal.lock_device(data[0], int(data[1])) self.__device(query, data[0], 0) query.answer('Dispositivo bloccato') elif 'unlock' == action: """ 0: device_id """ self.__modal.unlock_device(data[0]) self.__device(query, data[0], 0) query.answer('Dispositivo sbloccato') else: query.answer('Command not found', show_alert=True) query.answer()
from datetime import datetime, timedelta from database.DAO import DAO # Variables dao = DAO() # Functions def get_devices(): response = [] devices = dao.get_all_devices() for row in devices: device_id = row.intIdDevice times = [] for _row in dao.get_device_times_always_power_on(device_id): times.append({ 'start': (datetime.fromtimestamp(_row.timePowerOn.seconds) - timedelta(hours=1)).time(), 'end': (datetime.fromtimestamp(_row.timePowerOff.seconds) - timedelta(hours=1)).time() }) response.append({ 'id': device_id, 'name': row.strName, 'currentPowerUsage': row.intUsage, 'solarPowerOn': bool(row.boolSolarPowerOn), 'timeDelayBeforePowerOff': row.timeDelayBeforePowerOff,