Example #1
0
 def __init__(self):
     DAO.__init__(self)
Example #2
0
    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()
Example #3
0
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,
Example #4
0
class App:
    def __init__(self, loop):
        self.__loop = loop
        self.__dao = None

        self.__updater = Updater(
            config.telegram.token,
            defaults=Defaults(parse_mode=ParseMode.MARKDOWN))
        self.__view = View(self.__updater, self.__handler)

        self.__modal = Modal(config.meross, config.solaredge, config.sun)
        self.__loop.run_until_complete(self.__modal.async_init())

    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()

    def __device(self, query, device_id, day=0):
        date = datetime.today() + timedelta(days=day)

        _type = 'local'
        device = self.__modal.get_device(device_id)
        if device is None:  # get from DB if not exist
            _type = 'db'
            device = self.__dao.get_device(device_id)
        powers_on = self.__dao.get_device_powers_on(device_id,
                                                    date.strftime('%Y-%m-%d'))
        attributes = self.__dao.get_device_attributes(device_id)

        if device is not None:
            text, inline_keyboard = self.__view.device(_type, device,
                                                       powers_on, attributes,
                                                       day)
            query.edit_message_text(
                text, reply_markup=InlineKeyboardMarkup(inline_keyboard))
        else:
            query.answer('Device not found', show_alert=True)

    def close(self):
        self.__updater.stop()
        self.__loop.run_until_complete(self.__modal.async_close())
 def __init__(self, username):
     DAO.__init__(self)
     self.username = username
     self.stat_id_dao = StatIdDAO()
     self.stat_look_up_dao = StatLookUpDAO()
     self.expanded_data_dao = ExpandedDataDAO()