def ether_restart(self, bot, update, groups): """ Restart ether service. """ query = update.callback_query api_id = groups[0] chat_id = query.message.chat_id bot.send_chat_action(chat_id=chat_id, action=ChatAction.TYPING) try: api = API.get(id=api_id) Barrenero.restart(api.url, api.token, 'Ether') response_text = f'*API {api.name}*\n' \ f'Restarting Ether.' except peewee.DoesNotExist: self.logger.error('Chat unregistered') response_text = 'Configure me first' except BarreneroRequestException as e: self.logger.exception(e.message) response_text = e.message except: self.logger.exception('Cannot restart API %s Ether miner', api.name) response_text = f'*API {api.name} - Ether miner*\nCannot restart miner' bot.edit_message_text(text=response_text, parse_mode=ParseMode.MARKDOWN, chat_id=chat_id, message_id=query.message.message_id)
def _add_api_config_done(self, bot, update): """ Adds configured api. """ chat_id = update.message.chat_id try: chat, _ = Chat.get_or_create(id=chat_id, defaults={'last_transaction': None}) config = Barrenero.get_token_or_register( url=self.tmp_config[chat_id]['url'], username=self.tmp_config[chat_id]['username'], password=self.tmp_config[chat_id]['password'], account=self.tmp_config[chat_id]['wallet'], api_password=self.tmp_config[chat_id]['api_password'], ) API.create(name=self.tmp_config[chat_id]['name'], url=self.tmp_config[chat_id]['url'], token=config['token'], superuser=config['superuser'], chat=chat) except: self.logger.exception('Cannot register Barrenero API') update.message.reply_text('Cannot register Barrenero API') else: update.message.reply_text('Api stored successfully') return self.start(bot, update)
def wallet_job_transactions(self, bot, job): """ Check last transaction and notify if there are new payments. """ self.logger.debug('Job: Check transactions') for chat in Chat.select(): api = random.choice(chat.apis) data = Barrenero.wallet(api.url, api.token) try: self.logger.debug('Current transaction: %s', str(chat.last_transaction)) self.logger.debug('Retrieved transactions: %s', str(data['transactions'])) first_transaction_hash = data['transactions'][0]['hash'] if not chat.last_transaction: # If last transaction is unknown, simply update it chat.last_transaction = first_transaction_hash else: # Show transactions until last known for tx in takewhile(lambda x: x['hash'] != chat.last_transaction, data['transactions']): text = f'\n\n*Transaction completed*\n' \ f' - Token: `{tx["token"]["name"]}`\n' \ f' - Value: `{tx["value"]} {tx["token"]["symbol"]}`\n' \ f' - Date: `{humanize_iso_date(tx["timestamp"])}`' bot.send_message(text=text, parse_mode=ParseMode.MARKDOWN, chat_id=chat.id) chat.last_transaction = first_transaction_hash chat.save() except (KeyError, IndexError): self.logger.debug('No transactions found for Chat %s', chat.id) time.sleep(1)
def wallet(self, bot, update): """ Call for Storj miner status and restarting service. """ chat_id = update.message.chat.id bot.send_chat_action(chat_id=chat_id, action=ChatAction.TYPING) try: chat = Chat.get(id=chat_id) api = random.choice(chat.apis) data = Barrenero.wallet(api.url, api.token) response_text = f'*Tokens*\n' response_text += '\n'.join( [f' - {t["name"]}: `{t["balance"]} {t["symbol"]}` ({t.get("balance_usd", "Unknown")} $)' for t in data['tokens'].values()]) for i, tx in zip(range(1, 4), data['transactions']): response_text += f'\n\n*Last transaction #{i}*\n' \ f' - Token: `{tx["token"]["name"]}`\n' \ f' - Hash: `{tx["hash"]}`\n' \ f' - Source: `{tx["source"]}`\n' \ f' - Value: `{tx["value"]} {tx["token"]["symbol"]}`\n' \ f' - Date: `{humanize_iso_date(tx["timestamp"])}`' except peewee.DoesNotExist: self.logger.error('Chat unregistered') response_text = 'Configure me first' except BarreneroRequestException as e: self.logger.exception(e.message) response_text = e.message except: self.logger.exception('Error retrieving wallet info') response_text = 'Cannot retrieve wallet info' bot.send_message(chat_id, response_text, parse_mode=ParseMode.MARKDOWN)
def ether_job_status(self, bot, job): """ Check miner status """ self.logger.debug('Job: Check Ether status') # Create new state machines global status_machines with lock: new_machines = { a: StatusStateMachine('Ether', a.name) for a in API.select().where(API.superuser == True).join(Chat) if a not in status_machines } status_machines.update(new_machines) self.logger.debug('Ether Status Machines: %s', str(status_machines)) for api, status in status_machines.items(): try: data = Barrenero.ether(api.url, api.token) if data['active']: status.start(bot=bot, chat=api.chat.id) else: status.stop(bot=bot, chat=api.chat.id) except BarreneroRequestException: if status.is_active: bot.send_message(api.chat.id, f'Cannot access `{api.name}`', parse_mode=ParseMode.MARKDOWN) status.stop(bot=bot, chat=api.chat.id)
def miner_status(self, bot, update, groups): """ Query Miner status and return it properly formatted. """ query = update.callback_query api_id = groups[0] chat_id = query.message.chat_id bot.send_chat_action(chat_id=chat_id, action=ChatAction.TYPING) try: api = API.get(id=api_id) data = Barrenero.miner(api.url, api.token) response_text = f'*API {api.name}*\n' response_text += '*Services*\n' response_text += '\n'.join([ f' - {service["name"]}: `{service["status"]}`' for service in data['services'] ]) for graphic in data['graphics']: response_text += f'\n\n*Graphic card #{graphic["id"]}*\n' response_text += f' - Power: `{graphic["power"]} W`\n' response_text += f' - Fan speed: `{graphic["fan"]} %`\n' response_text += f' - GPU: `{graphic["gpu_usage"]} %` - `{graphic["gpu_clock"]} Mhz`\n' response_text += f' - MEM: `{graphic["mem_usage"]} %` - `{graphic["mem_clock"]} Mhz`' except peewee.DoesNotExist: self.logger.error('Chat unregistered') response_text = 'Configure me first' except BarreneroRequestException as e: self.logger.exception(e.message) response_text = f'*API {api.name} - Ether miner*\n{e.message}' except: response_text = f'*API {api.name} - Ether miner*\nCannot retrieve Ether miner status' self.logger.exception( 'Barrenero API wrong response for Ether miner status: %s', str(data)) bot.edit_message_text(text=response_text, parse_mode=ParseMode.MARKDOWN, chat_id=chat_id, message_id=query.message.message_id)
def ether_nanopool(self, bot, update): """ Query for Nanopool account info. """ query = update.callback_query chat_id = query.message.chat_id bot.send_chat_action(chat_id=chat_id, action=ChatAction.TYPING) try: chat = Chat.get(id=chat_id) api = random.choice(chat.apis) data = Barrenero.ether(api.url, api.token) response_text = f'*Ether miner*\n' \ f' - Balance: `{data["nanopool"]["balance"]["confirmed"]} ETH`\n\n' \ f'*Hashrate*\n' \ f' - Current: `{data["nanopool"]["hashrate"]["current"]} MH/s`\n' \ f' - 1 hour: `{data["nanopool"]["hashrate"]["one_hour"]} MH/s`\n' \ f' - 3 hours: `{data["nanopool"]["hashrate"]["three_hours"]} MH/s`\n' \ f' - 6 hours: `{data["nanopool"]["hashrate"]["six_hours"]} MH/s`\n' \ f' - 12 hours: `{data["nanopool"]["hashrate"]["twelve_hours"]} MH/s`\n' \ f' - 24 hours: `{data["nanopool"]["hashrate"]["twenty_four_hours"]} MH/s`\n\n' \ f'*Last payment*\n' \ f' - Date: `{humanize_iso_date(data["nanopool"]["last_payment"]["date"])}`\n' \ f' - Value: `{data["nanopool"]["last_payment"]["value"]} ETH`\n\n' \ f'*Workers*\n' + \ '\n'.join(f' - {w}: `{v} MH/s`' for w, v in data['nanopool']['workers'].items()) except peewee.DoesNotExist: self.logger.error('Chat unregistered') response_text = 'Configure me first' except BarreneroRequestException as e: self.logger.exception(e.message) response_text = e.message except: response_text = 'Cannot retrieve Nanopool info' self.logger.exception( 'Barrenero API wrong response for Nanopool info: %s', str(data)) bot.edit_message_text(text=response_text, parse_mode=ParseMode.MARKDOWN, chat_id=query.message.chat_id, message_id=query.message.message_id)
def ether_status(self, bot, update, groups): """ Check Ether miner status. """ query = update.callback_query api_id = groups[0] chat_id = query.message.chat_id bot.send_chat_action(chat_id=chat_id, action=ChatAction.TYPING) try: api = API.get(id=api_id) data = Barrenero.ether(api.url, api.token) response_text = f'*API {api.name}*\n' \ f'*Ether miner*\n' \ f' - Status: {data["active"]}\n\n' \ f'*Hashrate*\n' \ + '\n'.join([f' - Graphic card #{h["graphic_card"]}: `{h["hashrate"]:.2f} MH/s`' for h in data['hashrate']]) except peewee.DoesNotExist: self.logger.error('Chat unregistered') response_text = 'Configure me first' except BarreneroRequestException as e: self.logger.exception(e.message) response_text = f'*API {api.name} - Ether miner*\n{e.message}' except: response_text = f'*API {api.name} - Ether miner*\nCannot retrieve Ether miner status' self.logger.exception( 'Barrenero API wrong response for Ether miner status: %s', str(data)) bot.edit_message_text(text=response_text, parse_mode=ParseMode.MARKDOWN, chat_id=chat_id, message_id=query.message.message_id)